@@ -73,6 +73,99 @@ following methods to create a ``Uuid`` object from it::
73
73
$uuid = Uuid::fromBase58('TuetYWNHhmuSQ3xPoVLv9M');
74
74
$uuid = Uuid::fromRfc4122('d9e7a184-5d5b-11ea-a62a-3499710062d0');
75
75
76
+ You can also use the ``UuidFactory `` to generate UUIDs. First, you may
77
+ configure the behavior of the factory using configuration files::
78
+
79
+ .. configuration-block ::
80
+
81
+ .. code-block :: yaml
82
+
83
+ # config/packages/uid.yaml
84
+ framework :
85
+ uid :
86
+ default_uuid_version : 6
87
+ name_based_uuid_version : 5
88
+ name_based_uuid_namespace : ~
89
+ time_based_uuid_version : 6
90
+ time_based_uuid_node : ~
91
+
92
+ .. code-block :: xml
93
+
94
+ <!-- config/packages/uid.xml -->
95
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
96
+ <container xmlns =" http://symfony.com/schema/dic/services"
97
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
98
+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
99
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
100
+ https://symfony.com/schema/dic/services/services-1.0.xsd
101
+ http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
102
+
103
+ <framework : config >
104
+ <framework : uid
105
+ default_uuid_version =" 6"
106
+ name_based_uuid_version =" 5"
107
+ name_based_uuid_namespace =" "
108
+ time_based_uuid_version =" 6"
109
+ time_based_uuid_node =" "
110
+ />
111
+ </framework : config >
112
+ </container >
113
+
114
+ .. code-block :: php
115
+
116
+ // config/packages/uid.php
117
+ <?php
118
+
119
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
120
+
121
+ return static function (ContainerConfigurator $configurator): void {
122
+ $services = $configurator->services()
123
+ ->defaults()
124
+ ->autowire()
125
+ ->autoconfigure();
126
+
127
+ $configurator->extension('framework', [
128
+ 'uid' => [
129
+ 'default_uuid_version' => 6,
130
+ 'name_based_uuid_version' => 5,
131
+ 'name_based_uuid_namespace' => '',
132
+ 'time_based_uuid_version' => 6,
133
+ 'time_based_uuid_node' => '',
134
+ ],
135
+ ]);
136
+ };
137
+
138
+ Then, you can inject the factory in your services and use it to generate UUIDs based
139
+ on the configuration you defined::
140
+
141
+ <?php
142
+
143
+ namespace App\Service;
144
+
145
+ use Symfony\Component\Uid\Factory\UuidFactory;
146
+
147
+ class FooService
148
+ {
149
+ private UuidFactory $uuidFactory;
150
+
151
+ public function __construct(UuidFactory $uuidFactory)
152
+ {
153
+ $this->uuidFactory = $uuidFactory;
154
+ }
155
+
156
+ public function generate(): void
157
+ {
158
+ // This creates a UUID of the version given in the configuration file (v6 by default)
159
+ $uuid = $this->uuidFactory->create();
160
+
161
+ $nameBasedUuid = $this->uuidFactory->nameBased(/** ... */);
162
+ $randomBasedUuid = $this->uuidFactory->randomBased();
163
+ $timestampBased = $this->uuidFactory->timeBased();
164
+
165
+ // ...
166
+ }
167
+ }
168
+
76
169
Converting UUIDs
77
170
~~~~~~~~~~~~~~~~
78
171
@@ -234,6 +327,31 @@ following methods to create a ``Ulid`` object from it::
234
327
$ulid = Ulid::fromBase58('1BKocMc5BnrVcuq2ti4Eqm');
235
328
$ulid = Ulid::fromRfc4122('0171069d-593d-97d3-8b3e-23d06de5b308');
236
329
330
+ Like UUIDs, ULIDs have their own factory, ``UlidFactory ``, that can be used to generate them::
331
+
332
+ <?php
333
+
334
+ namespace App\Service;
335
+
336
+ use Symfony\Component\Uid\Factory\UlidFactory;
337
+
338
+ class FooService
339
+ {
340
+ private UlidFactory $ulidFactory;
341
+
342
+ public function __construct(UlidFactory $ulidFactory)
343
+ {
344
+ $this->ulidFactory = $ulidFactory;
345
+ }
346
+
347
+ public function generate(): void
348
+ {
349
+ $ulid = $this->ulidFactory->create();
350
+
351
+ // ...
352
+ }
353
+ }
354
+
237
355
There's also a special ``NilUlid `` class to represent ULID ``null `` values::
238
356
239
8FB3
357
use Symfony\Component\Uid\NilUlid;
0 commit comments