class Uuid

Factory class for UUIDs.

Determines which UUID implementation to use, and uses that to generate and validate UUIDs.

Hierarchy

  • class \Drupal\Component\Uuid\Uuid

Expanded class hierarchy of Uuid

21 files declare their use of Uuid
block.install in drupal/core/modules/block/block.install
Install, update and uninstall functions for the block module.
CommentStorageController.php in drupal/core/modules/comment/lib/Drupal/comment/CommentStorageController.php
Definition of Drupal\comment\CommentStorageController.
ConfigContext.php in drupal/core/lib/Drupal/Core/Config/Context/ConfigContext.php
Contains \Drupal\Core\Config\Context\ConfigContext.
ConfigStorageController.php in drupal/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php
Definition of Drupal\Core\Config\Entity\ConfigStorageController.
contact.install in drupal/core/modules/contact/contact.install
Install, update and uninstall functions for the contact module.

... See full list

File

drupal/core/lib/Drupal/Component/Uuid/Uuid.php, line 16
Definition of Drupal\Component\Uuid\Uuid.

Namespace

Drupal\Component\Uuid
View source
class Uuid {

  /**
   * Holds the UUID implementation.
   *
   * @var Drupal\Component\Uuid\UuidInterface
   */
  protected $plugin;

  /**
   * Instantiates the correct UUID object.
   */
  public function __construct() {
    $class = $this
      ->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';
    }
    elseif (function_exists('com_create_guid')) {
      $plugin = 'Drupal\\Component\\Uuid\\Com';
    }
    return $plugin;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Uuid::$plugin protected property Holds the UUID implementation.
Uuid::determinePlugin protected function Determines the optimal implementation to use for generating UUIDs.
Uuid::generate public function Generates a universally unique identifier.
Uuid::isValid public function Checks that a string appears to be in the format of a UUID.
Uuid::__construct public function Instantiates the correct UUID object.