@@ -154,8 +154,42 @@ following methods to create a ``Uuid`` object from it::
154
154
$uuid = Uuid::fromBase58('TuetYWNHhmuSQ3xPoVLv9M');
155
155
$uuid = Uuid::fromRfc4122('d9e7a184-5d5b-11ea-a62a-3499710062d0');
156
156
157
- You can also use the ``UuidFactory `` to generate UUIDs. First, you may
158
- configure the behavior of the factory using configuration files::
157
+ You can also use the ``UuidFactory `` to generate UUIDs. Inject the factory in
158
+ your services and use it as follows:
159
+
160
+ namespace App\S ervice;
161
+
162
+ use Symfony\C omponent\U id\F actory\U uidFactory;
163
+
164
+ class FooService
165
+ {
166
+ public function __construct(
167
+ private UuidFactory $uuidFactory,
168
+ ) {
169
+ }
170
+
171
+ public function generate(): void
172
+ {
173
+ $uuid = $this->uuidFactory->create();
174
+
175
+ $randomBasedUuid = $this->uuidFactory->randomBased()->create();
176
+ // $namespace can be omitted if a default namespace is configured in the factory (see below)
177
+ $nameBasedUuid = $this->uuidFactory->nameBased($namespace)->create($name);
178
+ // $node can be omitted if a default node is configured in the factory (see below)
179
+ $timestampBased = $this->uuidFactory->timeBased($node)->create();
180
+
181
+ // ...
182
+ }
183
+ }
184
+
185
+ By default, this factory generates the folllowing UUIDs:
186
+
187
+ * Default and time-based UUIDs: UUIDv7
188
+ * Name-based UUIDs: UUIDv5
189
+ * Random-based UUIDs: UUIDv4
190
+ * Time-based node and UUID namespace: ``null ``
191
+
192
+ You can configure these default values::
159
193
160
194
.. configuration-block ::
161
195
@@ -164,10 +198,10 @@ configure the behavior of the factory using configuration files::
164
198
# config/packages/uid.yaml
165
199
framework :
166
200
uid :
167
- default_uuid_version : 7
168
- name_based_uuid_version : 5
201
+ default_uuid_version : 6
202
+ name_based_uuid_version : 3
169
203
name_based_uuid_namespace : 6ba7b810-9dad-11d1-80b4-00c04fd430c8
170
- time_based_uuid_version : 7
204
+ time_based_uuid_version : 6
171
205
time_based_uuid_node : 121212121212
172
206
173
207
.. code-block :: xml
@@ -183,10 +217,10 @@ configure the behavior of the factory using configuration files::
183
217
184
218
<framework : config >
185
219
<framework : uid
186
- default_uuid_version =" 7 "
187
- name_based_uuid_version =" 5 "
220
+ default_uuid_version =" 6 "
221
+ name_based_uuid_version =" 6 "
188
222
name_based_uuid_namespace =" 6ba7b810-9dad-11d1-80b4-00c04fd430c8"
189
- time_based_uuid_version =" 7 "
223
+ time_based_uuid_version =" 6 "
190
224
time_based_uuid_node =" 121212121212"
191
225
/>
192
226
</framework : config >
@@ -205,41 +239,19 @@ configure the behavior of the factory using configuration files::
205
239
206
240
$container->extension('framework', [
207
241
'uid' => [
208
- 'default_uuid_version' => 7 ,
209
- 'name_based_uuid_version' => 5 ,
242
+ 'default_uuid_version' => 6 ,
243
+ 'name_based_uuid_version' => 3 ,
210
244
'name_based_uuid_namespace' => '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
211
- 'time_based_uuid_version' => 7 ,
245
+ 'time_based_uuid_version' => 6 ,
212
246
'time_based_uuid_node' => 121212121212,
213
247
],
214
248
]);
215
249
};
216
250
217
- Then, you can inject the factory in your services and use it to generate UUIDs based
218
- on the configuration you defined::
219
-
220
- namespace App\Service;
221
-
222
- use Symfony\Component\Uid\Factory\UuidFactory;
251
+ .. versionadded :: 7.4
223
252
224
- class FooService
225
- {
226
- public function __construct(
227
- private UuidFactory $uuidFactory,
228
- ) {
229
- }
230
-
231
- public function generate(): void
232
- {
233
- // This creates a UUID of the version given in the configuration file (v7 by default)
234
- $uuid = $this->uuidFactory->create();
235
-
236
- $nameBasedUuid = $this->uuidFactory->nameBased(/** ... */);
237
- $randomBasedUuid = $this->uuidFactory->randomBased();
238
- $timestampBased = $this->uuidFactory->timeBased();
239
-
240
- // ...
241
- }
242
- }
253
+ Starting from Symfony 7.4, the default version for both UUIDs and time-based
254
+ UUIDs is v7. In previous versions, the default was v6.
243
255
244
256
Converting UUIDs
245
257
~~~~~~~~~~~~~~~~
0 commit comments