8000 minor #17690 [Uid] Add `UuidFactory` and `UlidFactory` mentions (alex… · symfony/symfony-docs@138546c · GitHub
[go: up one dir, main page]

Skip to content

Commit 138546c

Browse files
committed
minor #17690 [Uid] Add UuidFactory and UlidFactory mentions (alexandre-daubois)
This PR was merged into the 5.4 branch. Discussion ---------- [Uid] Add `UuidFactory` and `UlidFactory` mentions Solves #14960 Commits ------- de628c8 [Uid] Add `UuidFactory` and `UlidFactory` mentions
2 parents 6dfbdfe + de628c8 commit 138546c

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

components/uid.rst

Lines changed: 126 additions & 0 deletions
< 8000 td data-grid-cell-id="diff-a1edff01ea4496e8d30fe239a7ec041d8b28a2920ef5a5f8d9756a9f470c86cf-89-157-1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">157
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,103 @@ following methods to create a ``Uuid`` object from it::
8787
The ``fromBinary()``, ``fromBase32()``, ``fromBase58()`` and ``fromRfc4122()``
8888
methods were introduced in Symfony 5.3.
8989

90+
You can also use the ``UuidFactory`` to generate UUIDs. First, you may
91+
configure the behavior of the factory using configuration files::
92+
93+
.. configuration-block::
94+
95+
.. code-block:: yaml
96+
97+
# config/packages/uid.yaml
98+
framework:
99+
uid:
100+
default_uuid_version: 6
101+
name_based_uuid_version: 5
102+
name_based_uuid_namespace: ~
103+
time_based_uuid_version: 6
104+
time_based_uuid_node: ~
105+
106+
.. code-block:: xml
107+
108+
<!-- config/packages/uid.xml -->
109+
<?xml version="1.0" encoding="UTF-8" ?>
110+
<container xmlns="http://symfony.com/schema/dic/services"
111+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
112+
xmlns:framework="http://symfony.com/schema/dic/symfony"
113+
xsi:schemaLocation="http://symfony.com/schema/dic/services
114+
https://symfony.com/schema/dic/services/services-1.0.xsd
115+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
116+
117+
<framework:config>
118+
<framework:uid
119+
default_uuid_version="6"
120+
name_based_uuid_version="5"
121+
name_based_uuid_namespace=""
122+
time_based_uuid_version="6"
123+
time_based_uuid_node=""
124+
/>
125+
</framework:config>
126+
</container>
127+
128+
.. code-block:: php
129+
130+
// config/packages/uid.php
131+
<?php
132+
133+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
134+
135+
return static function (ContainerConfigurator $configurator): void {
136+
$services = $configurator->services()
137+
->defaults()
138+
->autowire()
139+
->autoconfigure();
140+
141+
$configurator->extension('framework', [
142+
'uid' => [
143+
'default_uuid_version' => 6,
144+
'name_based_uuid_version' => 5,
145+
'name_based_uuid_namespace' => '',
146+
'time_based_uuid_version' => 6,
147+
'time_based_uuid_node' => '',
148+
],
149+
]);
150+
};
151+
152+
Then, you can inject the factory in your services and use it to generate UUIDs based
153+
on the configuration you defined::
154+
155+
<?php
156+
+
namespace App\Service;
158+
159+
use Symfony\Component\Uid\Factory\UuidFactory;
160+
161+
class FooService
162+
{
163+
private UuidFactory $uuidFactory;
164+
165+
public function __construct(UuidFactory $uuidFactory)
166+
{
167+
$this->uuidFactory = $uuidFactory;
168+
}
169+
170+
public function generate(): void
171+
{
172+
// This creates a UUID of the version given in the configuration file (v6 by default)
173+
$uuid = $this->uuidFactory->create();
174+
175+
$nameBasedUuid = $this->uuidFactory->nameBased(/** ... */);
176+
$randomBasedUuid = $this->uuidFactory->randomBased();
177+
$timestampBased = $this->uuidFactory->timeBased();
178+
179+
// ...
180+
}
181+
}
182+
183+
.. versionadded:: 5.3
184+
185+
The ``UuidFactory`` was introduced in Symfony 5.3.
186+
90187
Converting UUIDs
91188
~~~~~~~~~~~~~~~~
92189

@@ -268,6 +365,35 @@ following methods to create a ``Ulid`` object from it::
268365
The ``fromBinary()``, ``fromBase32()``, ``fromBase58()`` and ``fromRfc4122()``
269366
methods were introduced in Symfony 5.3.
270367

368+
Like UUIDs, ULIDs have their own factory, ``UlidFactory``, that can be used to generate them::
369+
370+
<?php
371+
372+
namespace App\Service;
373+
374+
use Symfony\Component\Uid\Factory\UlidFactory;
375+
376+
class FooService
377+
{
378+
private UlidFactory $ulidFactory;
379+
380+
public function __construct(UlidFactory $ulidFactory)
381+
{
382+
$this->ulidFactory = $ulidFactory;
383+
}
384+
385+
public function generate(): void
386+
{
387+
$ulid = $this->ulidFactory->create();
388+
389+
// ...
390+
}
391+
}
392+
393+
.. versionadded:: 5.3
394+
395+
The ``UlidFactory`` was introduced in Symfony 5.3.
396+
271397
There's also a special ``NilUlid`` class to represent ULID ``null`` values::
272398

273399
use Symfony\Component\Uid\NilUlid;

0 commit comments

Comments
 (0)
0