8000 Merge branch '5.4' into 6.0 · symfony/symfony-docs@6f2875c · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f2875c

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: [Uid] Add `UuidFactory` and `UlidFactory` mentions Update caution resolve doctrine example
2 parents 348ba08 + 138546c commit 6f2875c

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
@@ -73,6 +73,99 @@ following methods to create a ``Uuid`` object from it::
7373
$uuid = Uuid::fromBase58('TuetYWNHhmuSQ3xPoVLv9M');
7474
$uuid = Uuid::fromRfc4122('d9e7a184-5d5b-11ea-a62a-3499710062d0');
7575

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

@@ -234,6 +327,31 @@ following methods to create a ``Ulid`` object from it::
234327
$ulid = Ulid::fromBase58('1BKocMc5BnrVcuq2ti4Eqm');
235328
$ulid = Ulid::fromRfc4122('0171069d-593d-97d3-8b3e-23d06de5b308');
236329

330+
Like UUIDs, ULIDs have their own factory, ``UlidFactory``, that can be used to generate them::
331+
332+
<?php
333+
334+
namespace App\Service;
335+
336+
use Symfony\Component\Uid\Factory\UlidFactory;
337+
338+
class FooService
339+
{
340+
private UlidFactory $ulidFactory;
341+
342+
public function __construct(UlidFactory $ulidFactory)
343+
{
344+
$this->ulidFactory = $ulidFactory;
345+
}
346+
347+
public function generate(): void
348+
{
349+
$ulid = $this->ulidFactory->create();
350+
351+
// ...
352+
}
353+
}
354+
237355
There's also a special ``NilUlid`` class to represent ULID ``null`` values::
238356

239 8FB3 357
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)
0