@@ -87,6 +87,103 @@ following methods to create a ``Uuid`` object from it::
8787 The ``fromBinary() ``, ``fromBase32() ``, ``fromBase58() `` and ``fromRfc4122() ``
8888 methods were introduced in Symfony 5.3.
8989
90+ You can also use the ``UuidFactory `` to generate UUIDs. First, you may
91+ configure the behavior of the factory using configuration files::
92+
93+ .. configuration-block ::
94+
95+ .. code-block :: yaml
96+
97+ # config/packages/uid.yaml
98+ framework :
99+ uid :
100+ default_uuid_version : 6
101+ name_based_uuid_version : 5
102+ name_based_uuid_namespace : ~
103+ time_based_uuid_version : 6
104+ time_based_uuid_node : ~
105+
106+ .. code-block :: xml
107+
108+ <!-- config/packages/uid.xml -->
109+ <?xml version =" 1.0" encoding =" UTF-8" ?>
110+ <container xmlns =" http://symfony.com/schema/dic/services"
111+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
112+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
113+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
114+ https://symfony.com/schema/dic/services/services-1.0.xsd
115+ http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
116+
117+ <framework : config >
118+ <framework : uid
119+ default_uuid_version =" 6"
120+ name_based_uuid_version =" 5"
121+ name_based_uuid_namespace =" "
122+ time_based_uuid_version =" 6"
123+ time_based_uuid_node =" "
124+ />
125+ </framework : config >
126+ </container >
127+
128+ .. code-block :: php
129+
130+ // config/packages/uid.php
131+ <?php
132+
133+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
134+
135+ return static function (ContainerConfigurator $configurator): void {
136+ $services = $configurator->services()
137+ ->defaults()
138+ ->autowire()
139+ ->autoconfigure();
140+
141+ $configurator->extension('framework', [
142+ 'uid' => [
143+ 'default_uuid_version' => 6,
144+ 'name_based_uuid_version' => 5,
145+ 'name_based_uuid_namespace' => '',
146+ 'time_based_uuid_version' => 6,
147+ 'time_based_uuid_node' => '',
148+ ],
149+ ]);
150+ };
151+
152+ Then, you can inject the factory in your services and use it to generate UUIDs based
153+ on the configuration you defined::
154+
155+ <?php
156+
157+ namespace App\Service;
158+
159+ use Symfony\Component\Uid\Factory\UuidFactory;
160+
161+ class FooService
162+ {
163+ private UuidFactory $uuidFactory;
164+
165+ public function __construct(UuidFactory $uuidFactory)
166+ {
167+ $this->uuidFactory = $uuidFactory;
168+ }
169+
170+ public function generate(): void
171+ {
172+ // This creates a UUID of the version given in the configuration file (v6 by default)
173+ $uuid = $this->uuidFactory->create();
174+
175+ $nameBasedUuid = $this->uuidFactory->nameBased(/** ... */);
176+ $randomBasedUuid = $this->uuidFactory->randomBased();
177+ $timestampBased = $this->uuidFactory->timeBased();
178+
179+ // ...
180+ }
181+ }
182+
183+ .. versionadded :: 5.3
184+
185+ The ``UuidFactory `` was introduced in Symfony 5.3.
186+
90187Converting UUIDs
91188~~~~~~~~~~~~~~~~
92189
@@ -268,6 +365,35 @@ following methods to create a ``Ulid`` object from it::
268365 The ``fromBinary() ``, ``fromBase32() ``, ``fromBase58() `` and ``fromRfc4122() ``
269366 methods were introduced in Symfony 5.3.
270367
368+ Like UUIDs, ULIDs have their own factory, ``UlidFactory ``, that can be used to generate them::
369+
370+ <?php
371+
372+ namespace App\Service;
373+
374+ use Symfony\Component\Uid\Factory\UlidFactory;
375+
376+ class FooService
377+ {
378+ private UlidFactory $ulidFactory;
379+
380+ public function __construct(UlidFactory $ulidFactory)
381+ {
382+ $this->ulidFactory = $ulidFactory;
383+ }
384+
385+ public function generate(): void
386+ {
387+ $ulid = $this->ulidFactory->create();
388+
389+ // ...
390+ }
391+ }
392+
393+ .. versionadded :: 5.3
394+
395+ The ``UlidFactory `` was introduced in Symfony 5.3.
396+
271397There's also a special ``NilUlid `` class to represent ULID ``null `` values::
272398
273399 use Symfony\Component\Uid\NilUlid;
0 commit comments