diff --git a/components/uid.rst b/components/uid.rst index c2a0c79315b..b2d35f4154f 100644 --- a/components/uid.rst +++ b/components/uid.rst @@ -87,6 +87,103 @@ following methods to create a ``Uuid`` object from it:: The ``fromBinary()``, ``fromBase32()``, ``fromBase58()`` and ``fromRfc4122()`` methods were introduced in Symfony 5.3. +You can also use the ``UuidFactory`` to generate UUIDs. First, you may +configure the behavior of the factory using configuration files:: + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/uid.yaml + framework: + uid: + default_uuid_version: 6 + name_based_uuid_version: 5 + name_based_uuid_namespace: ~ + time_based_uuid_version: 6 + time_based_uuid_node: ~ + + .. code-block:: xml + + + + + + + + + + + .. code-block:: php + + // config/packages/uid.php + services() + ->defaults() + ->autowire() + ->autoconfigure(); + + $configurator->extension('framework', [ + 'uid' => [ + 'default_uuid_version' => 6, + 'name_based_uuid_version' => 5, + 'name_based_uuid_namespace' => '', + 'time_based_uuid_version' => 6, + 'time_based_uuid_node' => '', + ], + ]); + }; + +Then, you can inject the factory in your services and use it to generate UUIDs based +on the configuration you defined:: + + uuidFactory = $uuidFactory; + } + + public function generate(): void + { + // This creates a UUID of the version given in the configuration file (v6 by default) + $uuid = $this->uuidFactory->create(); + + $nameBasedUuid = $this->uuidFactory->nameBased(/** ... */); + $randomBasedUuid = $this->uuidFactory->randomBased(); + $timestampBased = $this->uuidFactory->timeBased(); + + // ... + } + } + +.. versionadded:: 5.3 + + The ``UuidFactory`` was introduced in Symfony 5.3. + Converting UUIDs ~~~~~~~~~~~~~~~~ @@ -268,6 +365,35 @@ following methods to create a ``Ulid`` object from it:: The ``fromBinary()``, ``fromBase32()``, ``fromBase58()`` and ``fromRfc4122()`` methods were introduced in Symfony 5.3. +Like UUIDs, ULIDs have their own factory, ``UlidFactory``, that can be used to generate them:: + + ulidFactory = $ulidFactory; + } + + public function generate(): void + { + $ulid = $this->ulidFactory->create(); + + // ... + } + } + +.. versionadded:: 5.3 + + The ``UlidFactory`` was introduced in Symfony 5.3. + There's also a special ``NilUlid`` class to represent ULID ``null`` values:: use Symfony\Component\Uid\NilUlid;