@@ -88,6 +88,99 @@ following methods to create a ``Uuid`` object from it::
8888 $uuid = Uuid::fromBase58('TuetYWNHhmuSQ3xPoVLv9M');
8989 $uuid = Uuid::fromRfc4122('d9e7a184-5d5b-11ea-a62a-3499710062d0');
9090
91+ You can also use the ``UuidFactory `` to generate UUIDs. First, you may
92+ configure the behavior of the factory using configuration files::
93+
94+ .. configuration-block ::
95+
96+ .. code-block :: yaml
97+
98+ # config/packages/uid.yaml
99+ framework :
100+ uid :
101+ default_uuid_version : 6
102+ name_based_uuid_version : 5
103+ name_based_uuid_namespace : ~
104+ time_based_uuid_version : 6
105+ time_based_uuid_node : ~
106+
107+ .. code-block :: xml
108+
109+ <!-- config/packages/uid.xml -->
110+ <?xml version =" 1.0" encoding =" UTF-8" ?>
111+ <container xmlns =" http://symfony.com/schema/dic/services"
112+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
113+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
114+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
115+ https://symfony.com/schema/dic/services/services-1.0.xsd
116+ http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
117+
118+ <framework : config >
119+ <framework : uid
120+ default_uuid_version =" 6"
121+ name_based_uuid_version =" 5"
122+ name_based_uuid_namespace =" "
123+ time_based_uuid_version =" 6"
124+ time_based_uuid_node =" "
125+ />
126+ </framework : config >
127+ </container >
128+
129+ .. code-block :: php
130+
131+ // config/packages/uid.php
132+ <?php
133+
134+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
135+
136+ return static function (ContainerConfigurator $configurator): void {
137+ $services = $configurator->services()
138+ ->defaults()
139+ ->autowire()
140+ ->autoconfigure();
141+
142+ $configurator->extension('framework', [
143+ 'uid' => [
144+ 'default_uuid_version' => 6,
145+ 'name_based_uuid_version' => 5,
146+ 'name_based_uuid_namespace' => '',
147+ 'time_based_uuid_version' => 6,
148+ 'time_based_uuid_node' => '',
149+ ],
150+ ]);
151+ };
152+
153+ Then, you can inject the factory in your services and use it to generate UUIDs based
154+ on the configuration you defined::
155+
156+ <?php
157+
158+ namespace App\Service;
159+
160+ use Symfony\Component\Uid\Factory\UuidFactory;
161+
162+ class FooService
163+ {
164+ private UuidFactory $uuidFactory;
165+
166+ public function __construct(UuidFactory $uuidFactory)
167+ {
168+ $this->uuidFactory = $uuidFactory;
169+ }
170+
171+ public function generate(): void
172+ {
173+ // This creates a UUID of the version given in the configuration file (v6 by default)
174+ $uuid = $this->uuidFactory->create();
175+
176+ $nameBasedUuid = $this->uuidFactory->nameBased(/** ... */);
177+ $randomBasedUuid = $this->uuidFactory->randomBased();
178+ $timestampBased = $this->uuidFactory->timeBased();
179+
180+ // ...
181+ }
182+ }
183+
91184Converting UUIDs
92185~~~~~~~~~~~~~~~~
93186
@@ -262,6 +355,31 @@ following methods to create a ``Ulid`` object from it::
262355 $ulid = Ulid::fromBase58('1BKocMc5BnrVcuq2ti4Eqm');
263356 $ulid = Ulid::fromRfc4122('0171069d-593d-97d3-8b3e-23d06de5b308');
264357
358+ Like UUIDs, ULIDs have their own factory, ``UlidFactory ``, that can be used to generate them::
359+
360+ <?php
361+
362+ namespace App\Service;
363+
364+ use Symfony\Component\Uid\Factory\UlidFactory;
365+
366+ class FooService
367+ {
368+ private UlidFactory $ulidFactory;
369+
370+ public function __construct(UlidFactory $ulidFactory)
371+ {
372+ $this->ulidFactory = $ulidFactory;
373+ }
374+
375+ public function generate(): void
376+ {
377+ $ulid = $this->ulidFactory->create();
378+
379+ // ...
380+ }
381+ }
382+
265383There's also a special ``NilUlid `` class to represent ULID ``null `` values::
266384
267385 use Symfony\Component\Uid\NilUlid;
0 commit comments