Skip to content

Commit 6b62e5d

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: [Uid] Default to UuidV7 when using UuidFactory
2 parents bd3077c + 1fc2ba1 commit 6b62e5d

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

components/uid.rst

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,42 @@ following methods to create a ``Uuid`` object from it::
154154
$uuid = Uuid::fromBase58('TuetYWNHhmuSQ3xPoVLv9M');
155155
$uuid = Uuid::fromRfc4122('d9e7a184-5d5b-11ea-a62a-3499710062d0');
156156

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\Service;
161+
162+
use Symfony\Component\Uid\Factory\UuidFactory;
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::
159193

160194
.. configuration-block::
161195

@@ -164,10 +198,10 @@ configure the behavior of the factory using configuration files::
164198
# config/packages/uid.yaml
165199
framework:
166200
uid:
167-
default_uuid_version: 7
168-
name_based_uuid_version: 5
201+
default_uuid_version: 6
202+
name_based_uuid_version: 3
169203
name_based_uuid_namespace: 6ba7b810-9dad-11d1-80b4-00c04fd430c8
170-
time_based_uuid_version: 7
204+
time_based_uuid_version: 6
171205
time_based_uuid_node: 121212121212
172206
173207
.. code-block:: xml
@@ -183,10 +217,10 @@ configure the behavior of the factory using configuration files::
183217
184218
<framework:config>
185219
<framework:uid
186-
default_uuid_version="7"
187-
name_based_uuid_version="5"
220+
default_uuid_version="6"
221+
name_based_uuid_version="6"
188222
name_based_uuid_namespace="6ba7b810-9dad-11d1-80b4-00c04fd430c8"
189-
time_based_uuid_version="7"
223+
time_based_uuid_version="6"
190224
time_based_uuid_node="121212121212"
191225
/>
192226
</framework:config>
@@ -205,41 +239,19 @@ configure the behavior of the factory using configuration files::
205239
206240
$container->extension('framework', [
207241
'uid' => [
208-
'default_uuid_version' => 7,
209-
'name_based_uuid_version' => 5,
242+
'default_uuid_version' => 6,
243+
'name_based_uuid_version' => 3,
210244
'name_based_uuid_namespace' => '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
211-
'time_based_uuid_version' => 7,
245+
'time_based_uuid_version' => 6,
212246
'time_based_uuid_node' => 121212121212,
213247
],
214248
]);
215249
};
216250
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
223252

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.
243255

244256
Converting UUIDs
245257
~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)