Skip to content

Commit c679e90

Browse files
committed
Merge branch '6.2' into 6.3
* 6.2: [Uid] Add `UuidFactory` and `UlidFactory` mentions Update caution resolve doctrine example
2 parents f8f0f7c + ea448cd commit c679e90

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

components/uid.rst

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,99 @@ following methods to create a ``Uuid`` object from it::
8888
$uuid = Uuid::fromBase58('TuetYWNHhmuSQ3xPoVLv9M');
8989
$uuid = Uuid::fromRfc4122('d9e7a184-5d5b-11ea-a62a-3499710062d0');
9090

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

@@ -262,6 +355,31 @@ following methods to create a ``Ulid`` object from it::
262355
$ulid = Ulid::fromBase58('1BKocMc5BnrVcuq2ti4Eqm');
263356
$ulid = Ulid::fromRfc4122('0171069d-593d-97d3-8b3e-23d06de5b308');
264357

358+
Like UUIDs, ULIDs have their own factory, ``UlidFactory``, that can be used to generate them::
359+
360+
<?php
361+
362+
namespace App\Service;
363+
364+
use Symfony\Component\Uid\Factory\UlidFactory;
365+
366+
class FooService
367+
{
368+
private UlidFactory $ulidFactory;
369+
370+
public function __construct(UlidFactory $ulidFactory)
371+
{
372+
$this->ulidFactory = $ulidFactory;
373+
}
374+
375+
public function generate(): void
376+
{
377+
$ulid = $this->ulidFactory->create();
378+
379+
// ...
380+
}
381+
}
382+
265383
There's also a special ``NilUlid`` class to represent ULID ``null`` values::
266384

267385
use Symfony\Component\Uid\NilUlid;

doctrine.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ The database connection information is stored as an environment variable called
6565
you must encode them. See `RFC 3986`_ for the full list of reserved characters or
6666
use the :phpfunction:`urlencode` function to encode them. In this case you need to
6767
remove the ``resolve:`` prefix in ``config/packages/doctrine.yaml`` to avoid errors:
68-
``url: '%env(resolve:DATABASE_URL)%'``
68+
``url: '%env(DATABASE_URL)%'``
6969

7070
Now that your connection parameters are setup, Doctrine can create the ``db_name``
7171
database for you:

0 commit comments

Comments
 (0)