Commit 982087f2 authored by catch's avatar catch

Issue #1471368 by amateescu: Convert uuid.inc to PSR-0.

parent 62ad5568
<?php
/**
* @file
* Definition of Drupal\Component\Uuid\Com.
*/
namespace Drupal\Component\Uuid;
/**
* UUID implementation using the Windows internal GUID extension.
*
* @see http://php.net/com_create_guid
*/
class Com implements UuidInterface {
public function generate() {
// Remove {} wrapper and make lower case to keep result consistent.
return drupal_strtolower(trim(com_create_guid(), '{}'));
}
}
<?php
/**
* @file
* Definition of Drupal\Component\Uuid\Pecl.
*/
namespace Drupal\Component\Uuid;
/**
* UUID implementation using the PECL extension.
*/
class Pecl implements UuidInterface {
/**
* Implements Drupal\Component\Uuid\UuidInterface::generate().
*/
public function generate() {
return uuid_create(UUID_TYPE_DEFAULT);
}
}
<?php
/**
* @file
* Definition of Drupal\Component\Uuid\Php.
*/
namespace Drupal\Component\Uuid;
/**
* Generates an UUID v4 using PHP code.
*
* Loosely based on Ruby's UUIDTools generate_random logic.
*
* @see http://uuidtools.rubyforge.org/api/classes/UUIDTools/UUID.html
*/
class Php implements UuidInterface {
/**
* Implements Drupal\Component\Uuid\UuidInterface::generate().
*/
public function generate() {
$hex = substr(hash('sha256', drupal_random_bytes(16)), 0, 32);
// The field names refer to RFC 4122 section 4.1.2.
$time_low = substr($hex, 0, 8);
$time_mid = substr($hex, 8, 4);
$time_hi_and_version = base_convert(substr($hex, 12, 4), 16, 10);
$time_hi_and_version &= 0x0FFF;
$time_hi_and_version |= (4 << 12);
$clock_seq_hi_and_reserved = base_convert(substr($hex, 16, 4), 16, 10);
$clock_seq_hi_and_reserved &= 0x3F;
$clock_seq_hi_and_reserved |= 0x80;
$clock_seq_low = substr($hex, 20, 2);
$nodes = substr($hex, 20);
$uuid = sprintf('%s-%s-%04x-%02x%02x-%s',
$time_low, $time_mid,
$time_hi_and_version, $clock_seq_hi_and_reserved,
$clock_seq_low, $nodes);
return $uuid;
}
}
......@@ -2,23 +2,10 @@
/**
* @file
* Handling of universally unique identifiers.
* Definition of Drupal\Component\Uuid\Uuid.
*/
/**
* Interface that defines a UUID backend.
*/
interface UuidInterface {
/**
* Generates a Universally Unique IDentifier (UUID).
*
* @return
* A 32 byte integer represented as a hex string formatted with 4 hypens.
*/
public function generate();
}
namespace Drupal\Component\Uuid;
/**
* Factory class for UUIDs.
......@@ -30,11 +17,13 @@ class Uuid {
/**
* Holds the UUID implementation.
*
* @var Drupal\Component\Uuid\UuidInterface
*/
protected $plugin;
/**
* This constructor instantiates the correct UUID object.
* Instantiates the correct UUID object.
*/
public function __construct() {
$class = $this->determinePlugin();
......@@ -44,23 +33,23 @@ public function __construct() {
/**
* Generates an universally unique identifier.
*
* @see UuidInterface::generate()
* @see Drupal\Component\Uuid\UuidInterface::generate()
*/
public function generate() {
return $this->plugin->generate();
}
/**
* Check that a string appears to be in the format of a UUID.
* Checks that a string appears to be in the format of a UUID.
*
* Plugins should not implement validation, since UUIDs should be in a
* consistent format across all plugins.
*
* @param $uuid
* @param string $uuid
* The string to test.
*
* @return
* TRUE if the string is well formed.
* @return bool
* TRUE if the string is well formed, FALSE otherwise.
*/
public function isValid($uuid) {
return preg_match("/^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/", $uuid);
......@@ -72,7 +61,7 @@ public function isValid($uuid) {
* The selection is made based on the enabled PHP extensions with the
* most performant available option chosen.
*
* @return
* @return string
* The class name for the optimal UUID generator.
*/
protected function determinePlugin() {
......@@ -81,74 +70,18 @@ protected function determinePlugin() {
return $plugin;
}
$plugin = 'UuidPhp';
$plugin = 'Drupal\Component\Uuid\Php';
// Debian/Ubuntu uses the (broken) OSSP extension as their UUID
// implementation. The OSSP implementation is not compatible with the
// PECL functions.
if (function_exists('uuid_create') && !function_exists('uuid_make')) {
$plugin = 'UuidPecl';
$plugin = 'Drupal\Component\Uuid\Pecl';
}
// Try to use the COM implementation for Windows users.
elseif (function_exists('com_create_guid')) {
$plugin = 'UuidCom';
$plugin = 'Drupal\Component\Uuid\Com';
}
return $plugin;
}
}
/**
* UUID implementation using the PECL extension.
*/
class UuidPecl implements UuidInterface {
public function generate() {
return uuid_create(UUID_TYPE_DEFAULT);
}
}
/**
* UUID implementation using the Windows internal GUID extension.
*
* @see http://php.net/com_create_guid
*/
class UuidCom implements UuidInterface {
public function generate() {
// Remove {} wrapper and make lower case to keep result consistent.
return drupal_strtolower(trim(com_create_guid(), '{}'));
}
}
/**
* Generates an UUID v4 using PHP code.
*
* Loosely based on Ruby's UUIDTools generate_random logic.
*
* @see http://uuidtools.rubyforge.org/api/classes/UUIDTools/UUID.html
*/
class UuidPhp implements UuidInterface {
public function generate() {
$hex = substr(hash('sha256', drupal_random_bytes(16)), 0, 32);
// The field names refer to RFC 4122 section 4.1.2.
$time_low = substr($hex, 0, 8);
$time_mid = substr($hex, 8, 4);
$time_hi_and_version = base_convert(substr($hex, 12, 4), 16, 10);
$time_hi_and_version &= 0x0FFF;
$time_hi_and_version |= (4 << 12);
$clock_seq_hi_and_reserved = base_convert(substr($hex, 16, 4), 16, 10);
$clock_seq_hi_and_reserved &= 0x3F;
$clock_seq_hi_and_reserved |= 0x80;
$clock_seq_low = substr($hex, 20, 2);
$nodes = substr($hex, 20);
$uuid = sprintf('%s-%s-%04x-%02x%02x-%s',
$time_low, $time_mid,
$time_hi_and_version, $clock_seq_hi_and_reserved,
$clock_seq_low, $nodes);
return $uuid;
}
}
<?php
/**
* @file
* Definition of Drupal\Component\Uuid\UuidInterface.
*/
namespace Drupal\Component\Uuid;
/**
* Interface that defines a UUID backend.
*/
interface UuidInterface {
/**
* Generates a Universally Unique IDentifier (UUID).
*
* @return
* A 32 byte integer represented as a hex string formatted with 4 hypens.
*/
public function generate();
}
......@@ -37,6 +37,7 @@ files[] = tests/tablesort.test
files[] = tests/theme.test
files[] = tests/unicode.test
files[] = tests/update.test
files[] = tests/uuid.test
files[] = tests/xmlrpc.test
files[] = tests/upgrade/upgrade.test
files[] = tests/upgrade/upgrade_bare.test
......
<?php
use Drupal\Component\Uuid\Uuid;
/**
* Tests the Drupal\Component\Uuid\Uuid class.
*/
class UuidUnitTestCase extends DrupalUnitTestCase {
/**
* The UUID object to be used for generating UUIDs.
*
* @var Drupal\Component\Uuid\UuidInterface
*/
protected $uuid;
public static function getInfo() {
return array(
'name' => 'UUID handling',
'description' => "Test the handling of Universally Unique IDentifiers (UUIDs).",
'group' => 'UUID',
);
}
public function setUp() {
// Initiate the generator. This will lazy-load uuid.inc.
$this->uuid = new Uuid();
parent::setUp();
}
/**
* Test generating a UUID.
*/
public function testGenerateUuid() {
$uuid = $this->uuid->generate();
$this->assertTrue($this->uuid->isValid($uuid), 'UUID generation works.');
}
/**
* Test that generated UUIDs are unique.
*/
public function testUuidIsUnique() {
$uuid1 = $this->uuid->generate();
$uuid2 = $this->uuid->generate();
$this->assertNotEqual($uuid1, $uuid2, 'Same UUID was not generated twice.');
}
/**
* Test UUID validation.
*/
function testUuidValidation() {
// These valid UUIDs.
$uuid_fqdn = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
$uuid_min = '00000000-0000-0000-0000-000000000000';
$uuid_max = 'ffffffff-ffff-ffff-ffff-ffffffffffff';
$this->assertTrue($this->uuid->isValid($uuid_fqdn), t('FQDN namespace UUID (@uuid) is valid', array('@uuid' => $uuid_fqdn)));
$this->assertTrue($this->uuid->isValid($uuid_min), t('Minimum UUID value (@uuid) is valid', array('@uuid' => $uuid_min)));
$this->assertTrue($this->uuid->isValid($uuid_max), t('Maximum UUID value (@uuid) is valid', array('@uuid' => $uuid_max)));
// These are invalid UUIDs.
$invalid_format = '0ab26e6b-f074-4e44-9da-601205fa0e976';
$invalid_length = '0ab26e6b-f074-4e44-9daf-1205fa0e9761f';
$this->assertFalse($this->uuid->isValid($invalid_format), t('@uuid is not a valid UUID', array('@uuid' => $invalid_format)));
$this->assertFalse($this->uuid->isValid($invalid_length), t('@uuid is not a valid UUID', array('@uuid' => $invalid_length)));
}
}
......@@ -2686,68 +2686,3 @@ class SystemIndexPhpTest extends DrupalWebTestCase {
$this->assertResponse(404, t("Make sure index.php/user returns a 'page not found'."));
}
}
/**
* Tests uuid.inc and related functions.
*/
class UuidUnitTestCase extends DrupalUnitTestCase {
/**
* The UUID object to be used for generating UUIDs.
*
* @var Uuid
*/
protected $uuid;
public static function getInfo() {
return array(
'name' => 'UUID handling',
'description' => "Test the handling of Universally Unique IDentifiers (UUIDs).",
'group' => 'System',
);
}
public function setUp() {
// Initiate the generator. This will lazy-load uuid.inc.
$this->uuid = new Uuid();
parent::setUp();
}
/**
* Test generating a UUID.
*/
public function testGenerateUuid() {
$uuid = $this->uuid->generate();
$this->assertTrue($this->uuid->isValid($uuid), 'UUID generation works.');
}
/**
* Test that generated UUIDs are unique.
*/
public function testUuidIsUnique() {
$uuid1 = $this->uuid->generate();
$uuid2 = $this->uuid->generate();
$this->assertNotEqual($uuid1, $uuid2, 'Same UUID was not generated twice.');
}
/**
* Test UUID validation.
*/
function testUuidValidation() {
// These valid UUIDs.
$uuid_fqdn = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
$uuid_min = '00000000-0000-0000-0000-000000000000';
$uuid_max = 'ffffffff-ffff-ffff-ffff-ffffffffffff';
$this->assertTrue($this->uuid->isValid($uuid_fqdn), t('FQDN namespace UUID (@uuid) is valid', array('@uuid' => $uuid_fqdn)));
$this->assertTrue($this->uuid->isValid($uuid_min), t('Minimum UUID value (@uuid) is valid', array('@uuid' => $uuid_min)));
$this->assertTrue($this->uuid->isValid($uuid_max), t('Maximum UUID value (@uuid) is valid', array('@uuid' => $uuid_max)));
// These are invalid UUIDs.
$invalid_format = '0ab26e6b-f074-4e44-9da-601205fa0e976';
$invalid_length = '0ab26e6b-f074-4e44-9daf-1205fa0e9761f';
$this->assertFalse($this->uuid->isValid($invalid_format), t('@uuid is not a valid UUID', array('@uuid' => $invalid_format)));
$this->assertFalse($this->uuid->isValid($invalid_length), t('@uuid is not a valid UUID', array('@uuid' => $invalid_length)));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment