Skip to content

Commit 1fc2ba1

Browse files
committed
minor #21408 [Uid] Default to UuidV7 when using UuidFactory (javiereguiluz)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [Uid] Default to UuidV7 when using UuidFactory Fixes #21403. Commits ------- e918166 [Uid] Default to UuidV7 when using UuidFactory
2 parents 32ee550 + e918166 commit 1fc2ba1

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
@@ -158,8 +158,42 @@ following methods to create a ``Uuid`` object from it::
158158
$uuid = Uuid::fromBase58('TuetYWNHhmuSQ3xPoVLv9M');
159159
$uuid = Uuid::fromRfc4122('d9e7a184-5d5b-11ea-a62a-3499710062d0');
160160

161-
You can also use the ``UuidFactory`` to generate UUIDs. First, you may
162-
configure the behavior of the factory using configuration files::
161+
You can also use the ``UuidFactory`` to generate UUIDs. Inject the factory in
162+
your services and use it as follows:
163+
164+
namespace App\Service;
165+
166+
use Symfony\Component\Uid\Factory\UuidFactory;
167+
168+
class FooService
169+
{
170+
public function __construct(
171+
private UuidFactory $uuidFactory,
172+
) {
173+
}
174+
175+
public function generate(): void
176+
{
177+
$uuid = $this->uuidFactory->create();
178+
179+
$randomBasedUuid = $this->uuidFactory->randomBased()->create();
180+
// $namespace can be omitted if a default namespace is configured in the factory (see below)
181+
$nameBasedUuid = $this->uuidFactory->nameBased($namespace)->create($name);
182+
// $node can be omitted if a default node is configured in the factory (see below)
183+
$timestampBased = $this->uuidFactory->timeBased($node)->create();
184+
185+
// ...
186+
}
187+
}
188+
189+
By default, this factory generates the folllowing UUIDs:
190+
191+
* Default and time-based UUIDs: UUIDv7
192+
* Name-based UUIDs: UUIDv5
193+
* Random-based UUIDs: UUIDv4
194+
* Time-based node and UUID namespace: ``null``
195+
196+
You can configure these default values::
163197

164198
.. configuration-block::
165199

@@ -168,10 +202,10 @@ configure the behavior of the factory using configuration files::
168202
# config/packages/uid.yaml
169203
framework:
170204
uid:
171-
default_uuid_version: 7
172-
name_based_uuid_version: 5
205+
default_uuid_version: 6
206+
name_based_uuid_version: 3
173207
name_based_uuid_namespace: 6ba7b810-9dad-11d1-80b4-00c04fd430c8
174-
time_based_uuid_version: 7
208+
time_based_uuid_version: 6
175209
time_based_uuid_node: 121212121212
176210
177211
.. code-block:: xml
@@ -187,10 +221,10 @@ configure the behavior of the factory using configuration files::
187221
188222
<framework:config>
189223
<framework:uid
190-
default_uuid_version="7"
191-
name_based_uuid_version="5"
224+
default_uuid_version="6"
225+
name_based_uuid_version="6"
192226
name_based_uuid_namespace="6ba7b810-9dad-11d1-80b4-00c04fd430c8"
193-
time_based_uuid_version="7"
227+
time_based_uuid_version="6"
194228
time_based_uuid_node="121212121212"
195229
/>
196230
</framework:config>
@@ -209,41 +243,19 @@ configure the behavior of the factory using configuration files::
209243
210244
$container->extension('framework', [
211245
'uid' => [
212-
'default_uuid_version' => 7,
213-
'name_based_uuid_version' => 5,
246+
'default_uuid_version' => 6,
247+
'name_based_uuid_version' => 3,
214248
'name_based_uuid_namespace' => '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
215-
'time_based_uuid_version' => 7,
249+
'time_based_uuid_version' => 6,
216250
'time_based_uuid_node' => 121212121212,
217251
],
218252
]);
219253
};
220254
221-
Then, you can inject the factory in your services and use it to generate UUIDs based
222-
on the configuration you defined::
223-
224-
namespace App\Service;
225-
226-
use Symfony\Component\Uid\Factory\UuidFactory;
227-
228-
class FooService
229-
{
230-
public function __construct(
231-
private UuidFactory $uuidFactory,
232-
) {
233-
}
234-
235-
public function generate(): void
236-
{
237-
// This creates a UUID of the version given in the configuration file (v7 by default)
238-
$uuid = $this->uuidFactory->create();
239-
240-
$nameBasedUuid = $this->uuidFactory->nameBased(/** ... */);
241-
$randomBasedUuid = $this->uuidFactory->randomBased();
242-
$timestampBased = $this->uuidFactory->timeBased();
255+
.. versionadded:: 7.4
243256

244-
// ...
245-
}
246-
}
257+
Starting from Symfony 7.4, the default version for both UUIDs and time-based
258+
UUIDs is v7. In previous versions, the default was v6.
247259

248260
Converting UUIDs
249261
~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)