diff --git a/core/core.services.yml b/core/core.services.yml index db46b57..9c67b01 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -60,7 +60,7 @@ services: arguments: ['@config.cachedstorage.storage', '@cache.config'] config.context.factory: class: Drupal\Core\Config\Context\ConfigContextFactory - arguments: ['@event_dispatcher'] + arguments: ['@event_dispatcher', '@uuid'] config.context: class: Drupal\Core\Config\Context\ContextInterface tags: diff --git a/core/includes/config.inc b/core/includes/config.inc index 32ba50d..e8122f2 100644 --- a/core/includes/config.inc +++ b/core/includes/config.inc @@ -34,7 +34,8 @@ function config_install_default_config($type, $name) { Drupal::service('event_dispatcher'), Drupal::service('config.factory'), Drupal::entityManager(), - Drupal::lock() + Drupal::lock(), + Drupal::service('uuid') ); $installer->import(); } diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index e7ee511..5542f42 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -372,7 +372,8 @@ function install_begin_request(&$install_state) { $container->register('config.storage', 'Drupal\Core\Config\InstallStorage'); $container->register('config.context.factory', 'Drupal\Core\Config\Context\ConfigContextFactory') - ->addArgument(new Reference('event_dispatcher')); + ->addArgument(new Reference('event_dispatcher')) + ->addArgument(new Reference('uuid')); $container->register('config.context', 'Drupal\Core\Config\Context\ContextInterface') ->setFactoryService(new Reference('config.context.factory')) @@ -430,6 +431,9 @@ function install_begin_request(&$install_state) { $container->register('url_generator', 'Drupal\Core\Routing\NullGenerator'); + // Register Uuid. + CoreBundle::registerUuid($container); + Drupal::setContainer($container); } diff --git a/core/includes/update.inc b/core/includes/update.inc index 327dce2..524b9e4 100644 --- a/core/includes/update.inc +++ b/core/includes/update.inc @@ -13,7 +13,6 @@ use Drupal\Core\Config\FileStorage; use Drupal\Core\Config\ConfigException; use Drupal\Core\DrupalKernel; -use Drupal\Component\Uuid\Uuid; use Drupal\Component\Utility\NestedArray; use Symfony\Component\HttpFoundation\Request; @@ -1487,7 +1486,7 @@ function update_variables_to_state(array $variable_map) { * A $primary_key values of rows to be updated. */ function update_add_uuids(&$sandbox, $table, $primary_key, $values) { - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); foreach ($values as $value) { db_update($table) ->fields(array( diff --git a/core/lib/Drupal/Component/Uuid/Com.php b/core/lib/Drupal/Component/Uuid/Com.php index 945a30b..df73a71 100644 --- a/core/lib/Drupal/Component/Uuid/Com.php +++ b/core/lib/Drupal/Component/Uuid/Com.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\Component\Uuid\Com. + * Contains \Drupal\Component\Uuid\Com. */ namespace Drupal\Component\Uuid; @@ -12,9 +12,14 @@ * * @see http://php.net/com_create_guid */ -class Com implements UuidInterface { +class Com extends UuidBase implements UuidInterface { + + /** + * {@inheritdoc} + */ public function generate() { // Remove {} wrapper and make lower case to keep result consistent. return strtolower(trim(com_create_guid(), '{}')); } + } diff --git a/core/lib/Drupal/Component/Uuid/Pecl.php b/core/lib/Drupal/Component/Uuid/Pecl.php index 3d4cb27..32ff7cf 100644 --- a/core/lib/Drupal/Component/Uuid/Pecl.php +++ b/core/lib/Drupal/Component/Uuid/Pecl.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\Component\Uuid\Pecl. + * Contains \Drupal\Component\Uuid\Pecl. */ namespace Drupal\Component\Uuid; @@ -10,12 +10,13 @@ /** * UUID implementation using the PECL extension. */ -class Pecl implements UuidInterface { +class Pecl extends UuidBase implements UuidInterface { /** - * Implements Drupal\Component\Uuid\UuidInterface::generate(). + * {@inheritdoc} */ public function generate() { return uuid_create(UUID_TYPE_DEFAULT); } + } diff --git a/core/lib/Drupal/Component/Uuid/Php.php b/core/lib/Drupal/Component/Uuid/Php.php index 3841f36..0991525 100644 --- a/core/lib/Drupal/Component/Uuid/Php.php +++ b/core/lib/Drupal/Component/Uuid/Php.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\Component\Uuid\Php. + * Contains \Drupal\Component\Uuid\Php. */ namespace Drupal\Component\Uuid; @@ -16,10 +16,10 @@ * * @see http://uuidtools.rubyforge.org/api/classes/UUIDTools/UUID.html */ -class Php implements UuidInterface { +class Php extends UuidBase implements UuidInterface { /** - * Implements Drupal\Component\Uuid\UuidInterface::generate(). + * {@inheritdoc} */ public function generate() { $hex = substr(hash('sha256', Crypt::randomBytes(16)), 0, 32); @@ -46,4 +46,5 @@ public function generate() { return $uuid; } + } diff --git a/core/lib/Drupal/Component/Uuid/Uuid.php b/core/lib/Drupal/Component/Uuid/Uuid.php deleted file mode 100644 index 5df83ce..0000000 --- a/core/lib/Drupal/Component/Uuid/Uuid.php +++ /dev/null @@ -1,87 +0,0 @@ -determinePlugin(); - $this->plugin = new $class(); - } - - /** - * Generates a universally unique identifier. - * - * @see Drupal\Component\Uuid\UuidInterface::generate() - */ - public function generate() { - return $this->plugin->generate(); - } - - /** - * 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 string $uuid - * The string to test. - * - * @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); - } - - /** - * Determines the optimal implementation to use for generating UUIDs. - * - * The selection is made based on the enabled PHP extensions with the - * most performant available option chosen. - * - * @return string - * The class name for the optimal UUID generator. - */ - protected function determinePlugin() { - static $plugin; - if (!empty($plugin)) { - return $plugin; - } - - $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 = 'Drupal\Component\Uuid\Pecl'; - } - // Try to use the COM implementation for Windows users. - elseif (function_exists('com_create_guid')) { - $plugin = 'Drupal\Component\Uuid\Com'; - } - return $plugin; - } -} diff --git a/core/lib/Drupal/Component/Uuid/UuidBase.php b/core/lib/Drupal/Component/Uuid/UuidBase.php new file mode 100644 index 0000000..2bd285b --- /dev/null +++ b/core/lib/Drupal/Component/Uuid/UuidBase.php @@ -0,0 +1,31 @@ +storageComparer = $storage_comparer; $this->eventDispatcher = $event_dispatcher; $this->configFactory = $config_factory; $this->entityManager = $entity_manager; $this->lock = $lock; + $this->uuidService = $uuid; $this->processed = $this->storageComparer->getEmptyChangelist(); // Use an override free context for importing so that overrides to do not // pollute the imported data. The context is hard coded to ensure this is // the case. - $this->context = new FreeConfigContext($this->eventDispatcher); + $this->context = new FreeConfigContext($this->eventDispatcher, $this->uuidService); } /** diff --git a/core/lib/Drupal/Core/Config/Context/ConfigContext.php b/core/lib/Drupal/Core/Config/Context/ConfigContext.php index a5382fd..fbd8b96 100644 --- a/core/lib/Drupal/Core/Config/Context/ConfigContext.php +++ b/core/lib/Drupal/Core/Config/Context/ConfigContext.php @@ -10,7 +10,7 @@ use Drupal\Core\Config\Config; use Drupal\Core\Config\ConfigEvent; use Drupal\Component\Utility\NestedArray; -use Drupal\Component\Uuid\Uuid; +use Drupal\Component\Uuid\UuidInterface; use Symfony\Component\EventDispatcher\EventDispatcher; /** @@ -50,13 +50,23 @@ class ConfigContext implements ContextInterface { protected $uuid; /** + * The Uuid service. + * + * @var \Drupal\Core\Component\Uuid\UuidInterface + */ + protected $uuidService; + + /** * Constructs the configuration context. * * @param \Symfony\Component\EventDispatcher\EventDispatcher $event_dispatcher * An event dispatcher instance to use for configuration events. + * @param \Drupal\Core\Component\Uuid\UuidInterface + * The Uuid service. */ - public function __construct(EventDispatcher $event_dispatcher) { + public function __construct(EventDispatcher $event_dispatcher, UuidInterface $uuid) { $this->eventDispatcher = $event_dispatcher; + $this->uuidService = $uuid; } /** @@ -89,8 +99,7 @@ public function set($key, $value) { * Implements \Drupal\Core\Config\Context\ContextInterface::setUuid(). */ public function setUuid() { - $uuid = new Uuid(); - $this->uuid = $uuid->generate(); + $this->uuid = $this->uuidService->generate(); } /** diff --git a/core/lib/Drupal/Core/Config/Context/ConfigContextFactory.php b/core/lib/Drupal/Core/Config/Context/ConfigContextFactory.php index 1642380..12a1ab9 100644 --- a/core/lib/Drupal/Core/Config/Context/ConfigContextFactory.php +++ b/core/lib/Drupal/Core/Config/Context/ConfigContextFactory.php @@ -9,6 +9,7 @@ use Drupal\Core\Config\Config; use Drupal\Core\Config\ConfigException; +use Drupal\Component\Uuid\UuidInterface; use Symfony\Component\EventDispatcher\EventDispatcher; /** @@ -28,13 +29,23 @@ class ConfigContextFactory { protected $eventDispatcher; /** + * The Uuid service. + * + * @var \Drupal\Core\Component\Uuid\UuidInterface + */ + protected $uuidService; + + /** * Constructs the configuration context. * * @param \Symfony\Component\EventDispatcher\EventDispatcher $event_dispatcher * An event dispatcher instance to use for configuration events. + * @param \Drupal\Core\Component\Uuid\UuidInterface + * The Uuid service. */ - public function __construct(EventDispatcher $event_dispatcher) { + public function __construct(EventDispatcher $event_dispatcher, UuidInterface $uuid) { $this->eventDispatcher = $event_dispatcher; + $this->uuidService = $uuid; } /** @@ -52,7 +63,7 @@ public function get($class = NULL) { $class = 'Drupal\Core\Config\Context\ConfigContext'; } if (class_exists($class)) { - $context = new $class($this->eventDispatcher); + $context = new $class($this->eventDispatcher, $this->uuidService); } else { throw new ConfigException(sprintf('Unknown config context class: %s', $class)); diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php b/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php index e35a533..f8b8266 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php @@ -7,13 +7,13 @@ namespace Drupal\Core\Config\Entity; -use Drupal\Component\Uuid\Uuid; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityMalformedException; use Drupal\Core\Entity\EntityStorageControllerBase; use Drupal\Core\Config\Config; use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Config\StorageInterface; +use Drupal\Component\Uuid\UuidInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -41,6 +41,13 @@ class ConfigStorageController extends EntityStorageControllerBase { protected $uuidKey = 'uuid'; /** + * The Uuid service. + * + * @var \Drupal\Component\Uuid\UuidInterface + */ + protected $uuidService; + + /** * Name of the entity's status key or FALSE if a status is not supported. * * @var string|bool @@ -72,8 +79,10 @@ class ConfigStorageController extends EntityStorageControllerBase { * The config factory service. * @param \Drupal\Core\Config\StorageInterface $config_storage * The config storage service. + * @param \Drupal\Component\Uuid\UuidInterface $uuid_service + * The uuid service. */ - public function __construct($entity_type, array $entity_info, ConfigFactory $config_factory, StorageInterface $config_storage) { + public function __construct($entity_type, array $entity_info, ConfigFactory $config_factory, StorageInterface $config_storage, UuidInterface $uuid_service) { parent::__construct($entity_type, $entity_info); $this->idKey = $this->entityInfo['entity_keys']['id']; @@ -87,6 +96,7 @@ public function __construct($entity_type, array $entity_info, ConfigFactory $con $this->configFactory = $config_factory; $this->configStorage = $config_storage; + $this->uuidService = $uuid_service; } /** @@ -97,7 +107,8 @@ public static function createInstance(ContainerInterface $container, $entity_typ $entity_type, $entity_info, $container->get('config.factory'), - $container->get('config.storage') + $container->get('config.storage'), + $container->get('uuid') ); } @@ -290,8 +301,7 @@ public function create(array $values) { // Assign a new UUID if there is none yet. if (!isset($entity->{$this->uuidKey})) { - $uuid = new Uuid(); - $entity->{$this->uuidKey} = $uuid->generate(); + $entity->{$this->uuidKey} = $this->uuidService->generate(); } $entity->postCreate($this); diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index 9af0c74..46da6af 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -48,6 +48,7 @@ public function build(ContainerBuilder $container) { $container->addScope(new Scope('request')); $this->registerTwig($container); $this->registerModuleHandler($container); + $this->registerUuid($container); $container->addCompilerPass(new RegisterMatchersPass()); $container->addCompilerPass(new RegisterRouteFiltersPass()); @@ -125,4 +126,31 @@ public static function registerTwig(ContainerBuilder $container) { ->addMethodCall('addExtension', array(new Definition('Twig_Extension_Debug'))); } + /** + * Determines and registers the Uuid service. + * + * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container + * The container. + * + * @return string + * Class name for the Uuid service. + */ + public static function registerUuid(ContainerBuilder $container) { + $uuid_class = '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')) { + $uuid_class = 'Drupal\Component\Uuid\Pecl'; + } + // Try to use the COM implementation for Windows users. + elseif (function_exists('com_create_guid')) { + $uuid_class = 'Drupal\Component\Uuid\Com'; + } + + $container->register('uuid', $uuid_class); + return $uuid_class; + } + } diff --git a/core/lib/Drupal/Core/Entity/DatabaseStorageController.php b/core/lib/Drupal/Core/Entity/DatabaseStorageController.php index a3cd9df..a118e62 100644 --- a/core/lib/Drupal/Core/Entity/DatabaseStorageController.php +++ b/core/lib/Drupal/Core/Entity/DatabaseStorageController.php @@ -7,11 +7,11 @@ namespace Drupal\Core\Entity; +use Drupal\Component\Uuid\UuidInterface; use Drupal\Core\Language\Language; use PDO; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Entity\Query\QueryInterface; -use Drupal\Component\Uuid\Uuid; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Database\Connection; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -43,6 +43,13 @@ class DatabaseStorageController extends EntityStorageControllerBase { protected $fieldDefinitions; /** + * The Uuid service. + * + * @var \Drupal\Core\Component\Uuid\UuidInterface + */ + protected $uuidService; + + /** * Name of entity's revision database table field, if it supports revisions. * * Has the value FALSE if this entity does not use revisions. @@ -81,7 +88,8 @@ public static function createInstance(ContainerInterface $container, $entity_typ return new static( $entity_type, $entity_info, - $container->get('database') + $container->get('database'), + $container->get('uuid') ); } @@ -94,11 +102,14 @@ public static function createInstance(ContainerInterface $container, $entity_typ * An array of entity info for the entity type. * @param \Drupal\Core\Database\Connection $database * The database connection to be used. + * @param \Drupal\Component\Uuid\UuidInterface $uuid_service + * The uuid service. */ - public function __construct($entity_type, array $entity_info, Connection $database) { + public function __construct($entity_type, array $entity_info, Connection $database, UuidInterface $uuid_service) { parent::__construct($entity_type, $entity_info); $this->database = $database; + $this->uuidService = $uuid_service; // Check if the entity type supports IDs. if (isset($this->entityInfo['entity_keys']['id'])) { @@ -377,8 +388,7 @@ public function create(array $values) { // Assign a new UUID if there is none yet. if ($this->uuidKey && !isset($entity->{$this->uuidKey})) { - $uuid = new Uuid(); - $entity->{$this->uuidKey} = $uuid->generate(); + $entity->{$this->uuidKey} = $this->uuidService->generate(); } $entity->postCreate($this); diff --git a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php index c3a76e2..8e677f8 100644 --- a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php +++ b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php @@ -9,13 +9,12 @@ use Drupal\Core\Language\Language; use PDO; - use Drupal\Core\Entity\Query\QueryInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\DatabaseStorageController; use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\TypedData\ComplexDataInterface; -use Drupal\Component\Uuid\Uuid; +use Drupal\Component\Uuid\UuidInterface; use Drupal\Core\Database\Connection; /** @@ -54,8 +53,8 @@ class DatabaseStorageControllerNG extends DatabaseStorageController { /** * Overrides DatabaseStorageController::__construct(). */ - public function __construct($entity_type, array $entity_info, Connection $database) { - parent::__construct($entity_type,$entity_info, $database); + public function __construct($entity_type, array $entity_info, Connection $database, UuidInterface $uuid_service) { + parent::__construct($entity_type,$entity_info, $database, $uuid_service); $this->bundleKey = !empty($this->entityInfo['entity_keys']['bundle']) ? $this->entityInfo['entity_keys']['bundle'] : FALSE; $this->entityClass = $this->entityInfo['class']; @@ -118,8 +117,7 @@ public function create(array $values) { // Assign a new UUID if there is none yet. if ($this->uuidKey && !isset($entity->{$this->uuidKey}->value)) { - $uuid = new Uuid(); - $entity->{$this->uuidKey} = $uuid->generate(); + $entity->{$this->uuidKey} = $this->uuidService->generate(); } $entity->postCreate($this); diff --git a/core/lib/Drupal/Core/Entity/Entity.php b/core/lib/Drupal/Core/Entity/Entity.php index f8f8eb1..ea77314 100644 --- a/core/lib/Drupal/Core/Entity/Entity.php +++ b/core/lib/Drupal/Core/Entity/Entity.php @@ -7,7 +7,6 @@ namespace Drupal\Core\Entity; -use Drupal\Component\Uuid\Uuid; use Drupal\Core\Language\Language; use Drupal\Core\TypedData\TypedDataInterface; use Drupal\user\UserInterface; @@ -368,8 +367,9 @@ public function createDuplicate() { // Check if the entity type supports UUIDs and generate a new one if so. if (!empty($entity_info['entity_keys']['uuid'])) { - $uuid = new Uuid(); - $duplicate->{$entity_info['entity_keys']['uuid']} = $uuid->generate(); + // @todo Inject the uuid service into the storage controller once + // possible. + $duplicate->{$entity_info['entity_keys']['uuid']} = \Drupal::service('uuid')->generate(); } return $duplicate; } diff --git a/core/lib/Drupal/Core/Entity/EntityNG.php b/core/lib/Drupal/Core/Entity/EntityNG.php index de0fb46..7f48095 100644 --- a/core/lib/Drupal/Core/Entity/EntityNG.php +++ b/core/lib/Drupal/Core/Entity/EntityNG.php @@ -9,7 +9,6 @@ use Drupal\Core\Language\Language; use Drupal\Core\TypedData\TypedDataInterface; -use Drupal\Component\Uuid\Uuid; use ArrayIterator; use InvalidArgumentException; @@ -549,8 +548,9 @@ public function createDuplicate() { // Check if the entity type supports UUIDs and generate a new one if so. if (!empty($entity_info['entity_keys']['uuid'])) { - $uuid = new Uuid(); - $duplicate->{$entity_info['entity_keys']['uuid']}->value = $uuid->generate(); + // @todo Inject the uuid service into the storage controller once + // possible. + $duplicate->{$entity_info['entity_keys']['uuid']}->value = \Drupal::service('uuid')->generate(); } // Check whether the entity type supports revisions and initialize it if so. diff --git a/core/modules/block/block.install b/core/modules/block/block.install index ec72d06..8d6c756 100644 --- a/core/modules/block/block.install +++ b/core/modules/block/block.install @@ -4,7 +4,7 @@ * @file * Install, update and uninstall functions for the block module. */ -use Drupal\Component\Uuid\Uuid; + use Drupal\Core\Language\Language; /** @@ -194,7 +194,7 @@ function block_update_8007() { // Populate the {custom_block} and {custom_block_revision} table. $results = db_select('block_custom', 'bc')->fields('bc')->execute(); - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); $execute = FALSE; $block_insert = db_insert('custom_block')->fields(array( 'id', diff --git a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php index 435a960..d075f29 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php @@ -9,7 +9,6 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\DatabaseStorageControllerNG; -use Drupal\Component\Uuid\Uuid; use LogicException; /** diff --git a/core/modules/config/config.admin.inc b/core/modules/config/config.admin.inc index 2d30e49..3985cad 100644 --- a/core/modules/config/config.admin.inc +++ b/core/modules/config/config.admin.inc @@ -128,7 +128,8 @@ function config_admin_import_form_submit($form, &$form_state) { Drupal::service('event_dispatcher'), Drupal::service('config.factory'), Drupal::entityManager(), - Drupal::lock() + Drupal::lock(), + Drupal::service('uuid') ); if ($config_importer->alreadyImporting()) { drupal_set_message(t('Another request may be synchronizing configuration already.')); diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigImporterTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigImporterTest.php index aa133d5..f85150f 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigImporterTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigImporterTest.php @@ -60,7 +60,8 @@ function setUp() { $this->container->get('event_dispatcher'), $this->container->get('config.factory'), $this->container->get('plugin.manager.entity'), - $this->container->get('lock') + $this->container->get('lock'), + $this->container->get('uuid') ); $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.staging')); } diff --git a/core/modules/contact/contact.install b/core/modules/contact/contact.install index eaac51c..8dab029 100644 --- a/core/modules/contact/contact.install +++ b/core/modules/contact/contact.install @@ -5,7 +5,6 @@ * Install, update and uninstall functions for the contact module. */ -use Drupal\Component\Uuid\Uuid; use Drupal\Core\Language\Language; /** @@ -54,7 +53,7 @@ function contact_update_8000() { * @ingroup config_upgrade */ function contact_update_8001() { - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); $result = db_query('SELECT * FROM {contact}'); foreach ($result as $category) { // Take over the category's serial ID as new machine name. diff --git a/core/modules/entity/entity.install b/core/modules/entity/entity.install index af2aba1..5a118c0 100644 --- a/core/modules/entity/entity.install +++ b/core/modules/entity/entity.install @@ -5,8 +5,6 @@ * Install, update and uninstall functions for the entity module. */ -use Drupal\Component\Uuid\Uuid; - /** * Returns the raw configuration object for an EntityDisplay entity. * @@ -31,7 +29,7 @@ function _update_8000_entity_get_display($entity_type, $bundle, $view_mode) { } // Initialize a fresh structure. - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); $properties = array( 'id' => $id, 'uuid' => $uuid->generate(), @@ -70,7 +68,7 @@ function _update_8000_entity_get_form_display($entity_type, $bundle, $form_mode) } // Initialize a fresh structure. - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); $properties = array( 'id' => $id, 'uuid' => $uuid->generate(), diff --git a/core/modules/field/field.install b/core/modules/field/field.install index 7cfa1f6..976b99a 100644 --- a/core/modules/field/field.install +++ b/core/modules/field/field.install @@ -5,7 +5,6 @@ * Install, update, and uninstall functions for the Field module. */ -use Drupal\Component\Uuid\Uuid; use Drupal\field\Plugin\Core\Entity\Field; /** @@ -20,7 +19,7 @@ * @ingroup update_api */ function _update_8003_field_create_field(array &$field_config) { - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); // Merge in default values. $field_config += array( @@ -69,7 +68,7 @@ function _update_8003_field_create_field(array &$field_config) { * @ingroup update_api */ function _update_8003_field_create_instance(array $field_config, array &$instance_config) { - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); // Merge in defaults. $instance_config += array( @@ -241,8 +240,7 @@ function field_update_8002() { * Convert fields and instances to config. */ function field_update_8003() { - $uuid = new Uuid(); - + $uuid = Drupal::service('uuid'); $state = Drupal::state(); $deleted_fields = $state->get('field.field.deleted') ?: array(); $deleted_instances = $state->get('field.instance.deleted') ?: array(); diff --git a/core/modules/field/lib/Drupal/field/FieldInstanceStorageController.php b/core/modules/field/lib/Drupal/field/FieldInstanceStorageController.php index cb412ef..b16b9a6 100644 --- a/core/modules/field/lib/Drupal/field/FieldInstanceStorageController.php +++ b/core/modules/field/lib/Drupal/field/FieldInstanceStorageController.php @@ -11,6 +11,7 @@ use Drupal\Core\Config\Entity\ConfigStorageController; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Config\ConfigFactory; +use Drupal\Component\Uuid\UuidInterface; use Drupal\Core\Config\StorageInterface; use Drupal\Core\Entity\EntityManager; use Drupal\Core\Extension\ModuleHandler; @@ -65,8 +66,8 @@ class FieldInstanceStorageController extends ConfigStorageController { * @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $state * The state key value store. */ - public function __construct($entity_type, array $entity_info, ConfigFactory $config_factory, StorageInterface $config_storage, EntityManager $entity_manager, ModuleHandler $module_handler, KeyValueStoreInterface $state) { - parent::__construct($entity_type, $entity_info, $config_factory, $config_storage); + public function __construct($entity_type, array $entity_info, ConfigFactory $config_factory, StorageInterface $config_storage, UuidInterface $uuid_service, EntityManager $entity_manager, ModuleHandler $module_handler, KeyValueStoreInterface $state) { + parent::__construct($entity_type, $entity_info, $config_factory, $config_storage, $uuid_service); $this->entityManager = $entity_manager; $this->moduleHandler = $module_handler; $this->state = $state; @@ -81,6 +82,7 @@ public static function createInstance(ContainerInterface $container, $entity_typ $entity_info, $container->get('config.factory'), $container->get('config.storage'), + $container->get('uuid'), $container->get('plugin.manager.entity'), $container->get('module_handler'), $container->get('state') diff --git a/core/modules/field/lib/Drupal/field/FieldStorageController.php b/core/modules/field/lib/Drupal/field/FieldStorageController.php index 4eb2c89..972b0f1 100644 --- a/core/modules/field/lib/Drupal/field/FieldStorageController.php +++ b/core/modules/field/lib/Drupal/field/FieldStorageController.php @@ -7,6 +7,7 @@ namespace Drupal\field; +use Drupal\Component\Uuid\UuidInterface; use Drupal\Core\Config\Config; use Drupal\Core\Config\Entity\ConfigStorageController; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -53,6 +54,8 @@ class FieldStorageController extends ConfigStorageController { * The config factory service. * @param \Drupal\Core\Config\StorageInterface $config_storage * The config storage service. + * @param \Drupal\Component\Uuid\UuidInterface $uuid_service + * The uuid service. * @param \Drupal\Core\Entity\EntityManager $entity_manager * The entity manager. * @param \Drupal\Core\Extension\ModuleHandler $module_handler @@ -60,8 +63,8 @@ class FieldStorageController extends ConfigStorageController { * @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $state * The state key value store. */ - public function __construct($entity_type, array $entity_info, ConfigFactory $config_factory, StorageInterface $config_storage, EntityManager $entity_manager, ModuleHandler $module_handler, KeyValueStoreInterface $state) { - parent::__construct($entity_type, $entity_info, $config_factory, $config_storage); + public function __construct($entity_type, array $entity_info, ConfigFactory $config_factory, StorageInterface $config_storage, UuidInterface $uuid_service, EntityManager $entity_manager, ModuleHandler $module_handler, KeyValueStoreInterface $state) { + parent::__construct($entity_type, $entity_info, $config_factory, $config_storage, $uuid_service); $this->entityManager = $entity_manager; $this->moduleHandler = $module_handler; $this->state = $state; @@ -76,6 +79,7 @@ public static function createInstance(ContainerInterface $container, $entity_typ $entity_info, $container->get('config.factory'), $container->get('config.storage'), + $container->get('uuid'), $container->get('plugin.manager.entity'), $container->get('module_handler'), $container->get('state') diff --git a/core/modules/filter/filter.install b/core/modules/filter/filter.install index b450c36..42512a6 100644 --- a/core/modules/filter/filter.install +++ b/core/modules/filter/filter.install @@ -5,8 +5,6 @@ * Install, update, and uninstall functions for the Filter module. */ -use Drupal\Component\Uuid\Uuid; - /** * @addtogroup updates-7.x-to-8.x * @{ @@ -29,7 +27,7 @@ function filter_update_8000() { * @ingroup config_upgrade */ function filter_update_8001() { - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); $result = db_query('SELECT format, name, cache, status, weight FROM {filter_format}', array(), array('fetch' => PDO::FETCH_ASSOC)); foreach ($result as $format) { $id = $format['format']; diff --git a/core/modules/image/image.install b/core/modules/image/image.install index 8a05660..aa78b94 100644 --- a/core/modules/image/image.install +++ b/core/modules/image/image.install @@ -5,8 +5,6 @@ * Install, update and uninstall functions for the image module. */ -use Drupal\Component\Uuid\Uuid; - /** * Implements hook_install(). */ @@ -129,7 +127,7 @@ function _image_update_get_style_with_effects(array $style) { $effect['data'] = unserialize($effect['data']); // Generate a unique image effect ID for the effect. - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); $effect['ieid'] = $uuid->generate(); $effects[$effect['ieid']] = $effect; diff --git a/core/modules/image/image.module b/core/modules/image/image.module index bdcb5e9..7a69a67 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -14,7 +14,6 @@ use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; use Drupal\Component\Utility\Crypt; -use Drupal\Component\Uuid\Uuid; use Drupal\file\Plugin\Core\Entity\File; use Drupal\image\Plugin\Core\Entity\ImageStyle; @@ -946,7 +945,7 @@ function image_effect_save($style, &$effect) { // Generate a unique image effect ID for a new effect. if (empty($effect['ieid'])) { - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); $effect['ieid'] = $uuid->generate(); } $style->effects[$effect['ieid']] = $effect; diff --git a/core/modules/menu/menu.install b/core/modules/menu/menu.install index ae94f90..5b2fc2b 100644 --- a/core/modules/menu/menu.install +++ b/core/modules/menu/menu.install @@ -5,8 +5,6 @@ * Install, update and uninstall functions for the menu module. */ -use Drupal\Component\Uuid\Uuid; - /** * Implements hook_uninstall(). */ @@ -76,7 +74,7 @@ function menu_update_8003() { * @ingroup config_upgrade */ function menu_update_8004() { - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); $result = db_query('SELECT * FROM {menu_custom}'); foreach ($result as $menu) { // Save the config object. diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php index 493b939..c51b082 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php @@ -11,6 +11,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Database\Connection; +use Drupal\Component\Uuid\UuidInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Cmf\Component\Routing\RouteProviderInterface; @@ -55,8 +56,8 @@ class MenuLinkStorageController extends DatabaseStorageController implements Men * @param \Symfony\Cmf\Component\Routing\RouteProviderInterface $route_provider * The route provider service. */ - public function __construct($entity_type, array $entity_info, Connection $database, RouteProviderInterface $route_provider) { - parent::__construct($entity_type, $entity_info, $database); + public function __construct($entity_type, array $entity_info, Connection $database, UuidInterface $uuid_service, RouteProviderInterface $route_provider) { + parent::__construct($entity_type, $entity_info, $database, $uuid_service); $this->routeProvider = $route_provider; @@ -85,6 +86,7 @@ public static function createInstance(ContainerInterface $container, $entity_typ $entity_type, $entity_info, $container->get('database'), + $container->get('uuid'), $container->get('router.route_provider') ); } diff --git a/core/modules/shortcut/shortcut.install b/core/modules/shortcut/shortcut.install index d78fa3b..ba7c324 100644 --- a/core/modules/shortcut/shortcut.install +++ b/core/modules/shortcut/shortcut.install @@ -5,8 +5,6 @@ * Install, update and uninstall functions for the shortcut module. */ -use Drupal\Component\Uuid\Uuid; - /** * Implements hook_uninstall(). */ @@ -68,7 +66,7 @@ function shortcut_schema() { * Migrate shortcuts into configuration. */ function shortcut_update_8000() { - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); $result = db_query('SELECT * from {shortcut_set}'); $ids = array(); foreach ($result as $set) { diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php index d3ba7b3..b688f21 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php @@ -1305,7 +1305,8 @@ public function configImporter() { $this->container->get('event_dispatcher'), $this->container->get('config.factory'), $this->container->get('plugin.manager.entity'), - $this->container->get('lock') + $this->container->get('lock'), + $this->container->get('uuid') ); } // Always recalculate the changelist when called. diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUUIDTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUUIDTest.php index 0168bb3..86ae833 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUUIDTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUUIDTest.php @@ -7,8 +7,6 @@ namespace Drupal\system\Tests\Entity; -use Drupal\Component\Uuid\Uuid; - /** * Tests creation, saving, and loading of entity UUIDs. */ @@ -54,7 +52,7 @@ function testCRUD() { */ protected function assertCRUD($entity_type) { // Verify that no UUID is auto-generated when passing one for creation. - $uuid_service = new Uuid(); + $uuid_service = $this->container->get('uuid'); $uuid = $uuid_service->generate(); $custom_entity = entity_create($entity_type, array( 'name' => $this->randomName(), diff --git a/core/modules/system/lib/Drupal/system/Tests/Uuid/UuidUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Uuid/UuidUnitTest.php deleted file mode 100644 index 579ff33..0000000 --- a/core/modules/system/lib/Drupal/system/Tests/Uuid/UuidUnitTest.php +++ /dev/null @@ -1,77 +0,0 @@ - '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(); - } - - /** - * Tests generating a UUID. - */ - public function testGenerateUuid() { - $uuid = $this->uuid->generate(); - $this->assertTrue($this->uuid->isValid($uuid), 'UUID generation works.'); - } - - /** - * Tests 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.'); - } - - /** - * Tests 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), format_string('FQDN namespace UUID (@uuid) is valid', array('@uuid' => $uuid_fqdn))); - $this->assertTrue($this->uuid->isValid($uuid_min), format_string('Minimum UUID value (@uuid) is valid', array('@uuid' => $uuid_min))); - $this->assertTrue($this->uuid->isValid($uuid_max), format_string('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), format_string('@uuid is not a valid UUID', array('@uuid' => $invalid_format))); - $this->assertFalse($this->uuid->isValid($invalid_length), format_string('@uuid is not a valid UUID', array('@uuid' => $invalid_length))); - - } -} diff --git a/core/modules/taxonomy/taxonomy.install b/core/modules/taxonomy/taxonomy.install index d89b89c..3380888 100644 --- a/core/modules/taxonomy/taxonomy.install +++ b/core/modules/taxonomy/taxonomy.install @@ -5,8 +5,6 @@ * Install, update and uninstall functions for the taxonomy module. */ -use Drupal\Component\Uuid\Uuid; - /** * Implements hook_uninstall(). */ @@ -295,7 +293,7 @@ function taxonomy_update_8004() { * Convert vocabularies into configuration. */ function taxonomy_update_8005() { - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); $result = db_query('SELECT * FROM {taxonomy_vocabulary}'); foreach ($result as $vocabulary) { diff --git a/core/modules/user/lib/Drupal/user/UserStorageController.php b/core/modules/user/lib/Drupal/user/UserStorageController.php index c7b2619..d6a6022 100644 --- a/core/modules/user/lib/Drupal/user/UserStorageController.php +++ b/core/modules/user/lib/Drupal/user/UserStorageController.php @@ -11,6 +11,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Password\PasswordInterface; use Drupal\Core\Database\Connection; +use Drupal\Component\Uuid\UuidInterface; use Drupal\user\UserDataInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Entity\DatabaseStorageControllerNG; @@ -51,8 +52,8 @@ class UserStorageController extends DatabaseStorageControllerNG implements UserS * @param \Drupal\user\UserDataInterface $user_data * The user data service. */ - public function __construct($entity_type, $entity_info, Connection $database, PasswordInterface $password, UserDataInterface $user_data) { - parent::__construct($entity_type, $entity_info, $database); + public function __construct($entity_type, $entity_info, Connection $database, UuidInterface $uuid_service, PasswordInterface $password, UserDataInterface $user_data) { + parent::__construct($entity_type, $entity_info, $database, $uuid_service); $this->password = $password; $this->userData = $user_data; @@ -66,6 +67,7 @@ public static function createInstance(ContainerInterface $container, $entity_typ $entity_type, $entity_info, $container->get('database'), + $container->get('uuid'), $container->get('password'), $container->get('user.data') ); diff --git a/core/modules/user/tests/Drupal/user/Tests/Views/Argument/RolesRidTest.php b/core/modules/user/tests/Drupal/user/Tests/Views/Argument/RolesRidTest.php index 7537bf7..fe36ab5 100644 --- a/core/modules/user/tests/Drupal/user/Tests/Views/Argument/RolesRidTest.php +++ b/core/modules/user/tests/Drupal/user/Tests/Views/Argument/RolesRidTest.php @@ -8,6 +8,7 @@ namespace Drupal\user\Tests\Views\Argument; use Drupal\Component\Utility\String; +use Drupal\Component\Uuid\Php; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Tests\UnitTestCase; use Drupal\user\Plugin\views\argument\RolesRid; @@ -60,10 +61,12 @@ public function testTitleQuery() { $config_factory = $this->getConfigFactoryStub($config); $config_storage = $this->getConfigStorageStub($config); + $uuid = new Php(); + // Creates a stub role storage controller and replace the attachLoad() // method with an empty version, because attachLoad() calls // module_implements(). - $role_storage_controller = $this->getMock('Drupal\user\RoleStorageController', array('attachLoad'), array('user_role', static::$entityInfo, $config_factory, $config_storage)); + $role_storage_controller = $this->getMock('Drupal\user\RoleStorageController', array('attachLoad'), array('user_role', static::$entityInfo, $config_factory, $config_storage, $uuid)); $entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager') diff --git a/core/modules/user/user.install b/core/modules/user/user.install index 94c9d5d..1b12f7a 100644 --- a/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -5,7 +5,6 @@ * Install, update and uninstall functions for the user module. */ -use Drupal\Component\Uuid\Uuid; use Drupal\Core\Language\Language; /** @@ -688,7 +687,7 @@ function user_update_8011() { // of file_save_data() by updating an eventually existing record for that // file. file_unmanaged_save_data($default_image, $destination, FILE_EXISTS_REPLACE); - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); db_merge('file_managed') ->key(array( 'uri' => $destination, @@ -1031,7 +1030,7 @@ function user_update_8016() { * @ingroup config_upgrade */ function user_update_8017() { - $uuid = new Uuid(); + $uuid = Drupal::service('uuid'); $roles = db_select('role', 'r') ->fields('r') diff --git a/core/modules/views/lib/Drupal/views/Tests/ViewTestData.php b/core/modules/views/lib/Drupal/views/Tests/ViewTestData.php index a86e2b3..8249a8d 100644 --- a/core/modules/views/lib/Drupal/views/Tests/ViewTestData.php +++ b/core/modules/views/lib/Drupal/views/Tests/ViewTestData.php @@ -66,7 +66,8 @@ public static function importTestViews($class, $modules = array()) { \Drupal::service('event_dispatcher'), \Drupal::service('config.factory'), \Drupal::entityManager(), - \Drupal::lock() + \Drupal::lock(), + \Drupal::service('uuid') ); $installer->import(); } diff --git a/core/tests/Drupal/Tests/Component/Uuid/UuidTest.php b/core/tests/Drupal/Tests/Component/Uuid/UuidTest.php new file mode 100644 index 0000000..1d587f7 --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Uuid/UuidTest.php @@ -0,0 +1,102 @@ + 'UUID handling', + 'description' => "Test the handling of Universally Unique Identifiers (UUIDs).", + 'group' => 'UUID', + ); + } + + /** + * {@inheritdoc} + */ + protected function setUp() { + // Initiate the generator. We use the CoreBundle to save repeating the + // logic. + $container = new ContainerBuilder(); + $class = CoreBundle::registerUuid($container); + $this->uuidInstances[] = new $class(); + + // Add additional uuid implementations when available. + if ($class != 'Drupal\Component\Uuid\Php') { + $this->uuidInstances[] = new \Drupal\Component\Uuid\Php(); + // If we are on windows add the com implementation as well. + if ($class != 'Drupal\Component\Uuid\Com' && function_exists('com_create_guid')) { + $this->uuidInstances[] = new \Drupal\Component\Uuid\Com(); + } + } + } + + /** + * Tests generating a UUID. + */ + public function testGenerateUuid() { + foreach ($this->uuidInstances as $instance) { + $uuid = $instance->generate(); + $this->assertTrue((bool) $instance->isValid($uuid)); + } + } + + /** + * Tests that generated UUIDs are unique. + */ + public function testUuidIsUnique() { + foreach ($this->uuidInstances as $instance) { + $uuid1 = $instance->generate(); + $uuid2 = $instance->generate(); + $this->assertNotEquals($uuid1, $uuid2); + } + } + + /** + * Tests UUID validation. + */ + public 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'; + + foreach ($this->uuidInstances as $instance) { + $this->assertTrue((bool) $instance->isValid($uuid_fqdn)); + $this->assertTrue((bool) $instance->isValid($uuid_min)); + $this->assertTrue((bool) $instance->isValid($uuid_max)); + } + + // These are invalid UUIDs. + $invalid_format = '0ab26e6b-f074-4e44-9da-601205fa0e976'; + $invalid_length = '0ab26e6b-f074-4e44-9daf-1205fa0e9761f'; + + foreach ($this->uuidInstances as $instance) { + $this->assertFalse((bool) $instance->isValid($invalid_format)); + $this->assertFalse((bool) $instance->isValid($invalid_length)); + } + } + +}