Skip to content

Commit b0130c3

Browse files
fancywebnicolas-grekas
authored andcommitted
[Uid] Add UuidFactory to create Ulid and Uuid from timestamps, namespaces and nodes
1 parent fb46e43 commit b0130c3

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* Added the `dispatcher` option to `debug:event-dispatcher`
1111
* Added the `event_dispatcher.dispatcher` tag
1212
* Added `assertResponseFormatSame()` in `BrowserKitAssertionsTrait`
13+
* Add support for configuring UUID factory services
1314

1415
5.2.0
1516
-----

DependencyInjection/Configuration.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Symfony\Component\RateLimiter\Policy\TokenBucketLimiter;
3535
use Symfony\Component\Serializer\Serializer;
3636
use Symfony\Component\Translation\Translator;
37+
use Symfony\Component\Uid\Factory\UuidFactory;
3738
use Symfony\Component\Validator\Validation;
3839
use Symfony\Component\WebLink\HttpHeaderSerializer;
3940
use Symfony\Component\Workflow\WorkflowEvents;
@@ -136,6 +137,7 @@ public function getConfigTreeBuilder()
136137
$this->addSecretsSection($rootNode);
137138
$this->addNotifierSection($rootNode);
138139
$this->addRateLimiterSection($rootNode);
140+
$this->addUidSection($rootNode);
139141

140142
return $treeBuilder;
141143
}
@@ -1891,4 +1893,37 @@ private function addRateLimiterSection(ArrayNodeDefinition $rootNode)
18911893
->end()
18921894
;
18931895
}
1896+
1897+
private function addUidSection(ArrayNodeDefinition $rootNode)
1898+
{
1899+
$rootNode
1900+
->children()
1901+
->arrayNode('uid')
1902+
->info('Uid configuration')
1903+
->{class_exists(UuidFactory::class) ? 'canBeDisabled' : 'canBeEnabled'}()
1904+
->addDefaultsIfNotSet()
1905+
->children()
1906+
->enumNode('default_uuid_version')
1907+
->defaultValue(6)
1908+
->values([6, 4, 1])
1909+
->end()
1910+
->enumNode('name_based_uuid_version')
1911+
->defaultValue(5)
1912+
->values([5, 3])
1913+
->end()
1914+
->scalarNode('name_based_uuid_namespace')
1915+
->cannotBeEmpty()
1916+
->end()
1917+
->enumNode('time_based_uuid_version')
1918+
->defaultValue(6)
1919+
->values([6, 1])
1920+
->end()
1921+
->scalarNode('time_based_uuid_node')
1922+
->cannotBeEmpty()
1923+
->end()
1924+
->end()
1925+
->end()
1926+
->end()
1927+
;
1928+
}
18941929
}

DependencyInjection/FrameworkExtension.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@
160160
use Symfony\Component\Translation\Command\XliffLintCommand as BaseXliffLintCommand;
161161
use Symfony\Component\Translation\PseudoLocalizationTranslator;
162162
use Symfony\Component\Translation\Translator;
163+
use Symfony\Component\Uid\Factory\UuidFactory;
164+
use Symfony\Component\Uid\UuidV4;
163165
use Symfony\Component\Validator\ConstraintValidatorInterface;
164166
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
165167
use Symfony\Component\Validator\ObjectInitializerInterface;
@@ -449,6 +451,14 @@ public function load(array $configs, ContainerBuilder $container)
449451
$loader->load('web_link.php');
450452
}
451453

454+
if ($this->isConfigEnabled($container, $config['uid'])) {
455+
if (!class_exists(UuidFactory::class)) {
456+
throw new LogicException('Uid support cannot be enabled as the Uid component is not installed. Try running "composer require symfony/uid".');
457+
}
458+
459+
$this->registerUidConfiguration($config['uid'], $container, $loader);
460+
}
461+
452462
$this->addAnnotatedClassesToCompile([
453463
'**\\Controller\\',
454464
'**\\Entity\\',
@@ -2322,6 +2332,27 @@ public static function registerRateLimiter(ContainerBuilder $container, string $
23222332
$container->registerAliasForArgument($limiterId, RateLimiterFactory::class, $name.'.limiter');
23232333
}
23242334

2335+
private function registerUidConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)
2336+
{
2337+
$loader->load('uid.php');
2338+
2339+
$container->getDefinition('uuid.factory')
2340+
->setArguments([
2341+
$config['default_uuid_version'],
2342+
$config['time_based_uuid_version'],
2343+
$config['name_based_uuid_version'],
2344+
UuidV4::class,
2345+
$config['time_based_uuid_node'] ?? null,
2346+
$config['name_based_uuid_namespace'] ?? null,
2347+
])
2348+
;
2349+
2350+
if (isset($config['name_based_uuid_namespace'])) {
2351+
$container->getDefinition('name_based_uuid.factory')
2352+
->setArguments([$config['name_based_uuid_namespace']]);
2353+
}
2354+
}
2355+
23252356
private function resolveTrustedHeaders(array $headers): int
23262357
{
23272358
$trustedHeaders = 0;

Resources/config/schema/symfony-1.0.xsd

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<xsd:element name="mailer" type="mailer" minOccurs="0" maxOccurs="1" />
3636
<xsd:element name="http-cache" type="http_cache" minOccurs="0" maxOccurs="1" />
3737
<xsd:element name="rate-limiter" type="rate_limiter" minOccurs="0" maxOccurs="1" />
38+
<xsd:element name="uid" type="uid" minOccurs="0" maxOccurs="1" />
3839
</xsd:choice>
3940

4041
<xsd:attribute name="http-method-override" type="xsd:boolean" />
@@ -692,4 +693,35 @@
692693
<xsd:attribute name="interval" type="xsd:string" />
693694
<xsd:attribute name="amount" type="xsd:int" />
694695
</xsd:complexType>
696+
697+
<xsd:complexType name="uid">
698+
<xsd:attribute name="enabled" type="xsd:boolean" />
699+
<xsd:attribute name="default_uuid_version" type="default_uuid_version" />
700+
<xsd:attribute name="name_based_uuid_version" type="name_based_uuid_version" />
701+
<xsd:attribute name="time_based_uuid_version" type="time_based_uuid_version" />
702+
<xsd:attribute name="name_based_uuid_namespace" type="xsd:string" />
703+
<xsd:attribute name="time_based_uuid_node" type="xsd:string" />
704+
</xsd:complexType>
705+
706+
<xsd:simpleType name="default_uuid_version">
707+
<xsd:restriction base="xsd:int">
708+
<xsd:enumeration value="6" />
709+
<xsd:enumeration value="4" />
710+
<xsd:enumeration value="1" />
711+
</xsd:restriction>
712+
</xsd:simpleType>
713+
714+
<xsd:simpleType name="name_based_uuid_version">
715+
<xsd:restriction base="xsd:int">
716+
<xsd:enumeration value="5" />
717+
<xsd:enumeration value="3" />
718+
</xsd:restriction>
719+
</xsd:simpleType>
720+
721+
<xsd:simpleType name="time_based_uuid_version">
722+
<xsd:restriction base="xsd:int">
723+
<xsd:enumeration value="6" />
724+
<xsd:enumeration value="1" />
725+
</xsd:restriction>
726+
</xsd:simpleType>
695727
</xsd:schema>

Resources/config/uid.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
13+
14+
use Symfony\Component\Uid\Factory\NameBasedUuidFactory;
15+
use Symfony\Component\Uid\Factory\RandomBasedUuidFactory;
16+
use Symfony\Component\Uid\Factory\TimeBasedUuidFactory;
17+
use Symfony\Component\Uid\Factory\UlidFactory;
18+
use Symfony\Component\Uid\Factory\UuidFactory;
19+
20+
return static function (ContainerConfigurator $container) {
21+
$container->services()
22+
->set('ulid.factory', UlidFactory::class)
23+
->alias(UlidFactory::class, 'ulid.factory')
24+
25+
->set('uuid.factory', UuidFactory::class)
26+
->alias(UuidFactory::class, 'uuid.factory')
27+
28+
->set('name_based_uuid.factory', NameBasedUuidFactory::class)
29+
->factory([service('uuid.factory'), 'nameBased'])
30+
->args([abstract_arg('Please set the "framework.uid.name_based_uuid_namespace" configuration option to use the "name_based_uuid.factory" service')])
31+
->alias(NameBasedUuidFactory::class, 'name_based_uuid.factory')
32+
33+
->set('random_based_uuid.factory', RandomBasedUuidFactory::class)
34+
->factory([service('uuid.factory'), 'randomBased'])
35+
->alias(RandomBasedUuidFactory::class, 'random_based_uuid.factory')
36+
37+
->set('time_based_uuid.factory', TimeBasedUuidFactory::class)
38+
->factory([service('uuid.factory'), 'timeBased'])
39+
->alias(TimeBasedUuidFactory::class, 'time_based_uuid.factory')
40+
;
41+
};

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Mailer\Mailer;
2323
use Symfony\Component\Messenger\MessageBusInterface;
2424
use Symfony\Component\Notifier\Notifier;
25+
use Symfony\Component\Uid\Factory\UuidFactory;
2526

2627
class ConfigurationTest extends TestCase
2728
{
@@ -563,6 +564,12 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
563564
'enabled' => false,
564565
'limiters' => [],
565566
],
567+
'uid' => [
568+
'enabled' => class_exists(UuidFactory::class),
569+
'default_uuid_version' => 6,
570+
'name_based_uuid_version' => 5,
571+
'time_based_uuid_version' => 6,
572+
],
566573
];
567574
}
568575
}

0 commit comments

Comments
 (0)