8000 [Uid] Add UidFactory to create Ulid and Uuid from timestamps and rand… · symfony/symfony@27bffd6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 27bffd6

Browse files
committed
[Uid] Add UidFactory to create Ulid and Uuid from timestamps and randomness/nodes
1 parent b3de641 commit 27bffd6

File tree

13 files changed

+660
-21
lines changed

13 files changed

+660
-21
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
use Symfony\Component\RateLimiter\Policy\TokenBucketLimiter;
3535
use Symfony\Component\Serializer\Serializer;
3636
use Symfony\Component\Translation\Translator;
37+
use Symfony\Component\Uid\AbstractUid;
38+
use Symfony\Component\Uid\UuidV1;
39+
use Symfony\Component\Uid\UuidV3;
40+
use Symfony\Component\Uid\UuidV5;
41+
use Symfony\Component\Uid\UuidV6;
3742
use Symfony\Component\Validator\Validation;
3843
use Symfony\Component\WebLink\HttpHeaderSerializer;
3944
use Symfony\Component\Workflow\WorkflowEvents;
@@ -136,6 +141,7 @@ public function getConfigTreeBuilder()
136141
$this->addSecretsSection($rootNode);
137142
$this->addNotifierSection($rootNode);
138143
$this->addRateLimiterSection($rootNode);
144+
$this->addUidSection($rootNode);
139145

140146
return $treeBuilder;
141147
}
@@ -1888,4 +1894,59 @@ private function addRateLimiterSection(ArrayNodeDefinition $rootNode)
18881894
->end()
18891895
;
18901896
}
1897+
1898+
private function addUidSection(ArrayNodeDefinition $rootNode)
1899+
{
1900+
$rootNode
1901+
->children()
1902+
->arrayNode('uid')
1903+
->info('Uid configuration')
1904+
->{class_exists(AbstractUid::class) ? 'canBeDisabled' : 'canBeEnabled'}()
1905+
->children()
1906+
->arrayNode('uid_factory')
1907+
->addDefaultsIfNotSet()
1908+
->children()
1909+
->enumNode('default_named_version')
1910+
->defaultNull()
1911+
->values([null, UuidV5::class, UuidV3::class, 'v5', 'v3'])
1912+
->beforeNormalization()
1913+
->always()
1914+
->then(static function (?string $value): ?string {
1915+
if ('v5' === $value) {
1916+
return UuidV5::class;
1917+
}
1918+
1919+
if ('v3' === $value) {
1920+
return UuidV3::class;
1921+
}
1922+
1923+
return $value;
1924+
})
1925+
->end()
1926+
->end()
1927+
->enumNode('default_timed_version')
1928+
->defaultNull()
1929+
->values([null, UuidV6::class, UuidV1::class, 'v6', 'v1'])
1930+
->beforeNormalization()
1931+
->always()
1932+
->then(static function (?string $value): ?string {
1933+
if ('v6' === $value) {
1934+
return UuidV6::class;
1935+
}
1936+
1937+
if ('v1' === $value) {
1938+
return UuidV1::class;
1939+
}
1940+
1941+
return $value;
1942+
})
1943+
->end()
1944+
->end()
1945+
->end()
1946+
->end()
1947+
->end()
1948+
->end()
1949+
->end()
1950+
;
1951+
}
18911952
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
use Symfony\Component\Translation\Command\XliffLintCommand as BaseXliffLintCommand;
151151
use Symfony\Component\Translation\PseudoLocalizationTranslator;
152152
use Symfony\Component\Translation\Translator;
153+
use Symfony\Component\Uid\AbstractUid;
153154
use Symfony\Component\Validator\ConstraintValidatorInterface;
154155
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
155156
use Symfony\Component\Validator\ObjectInitializerInterface;
@@ -439,6 +440,14 @@ public function load(array $configs, ContainerBuilder $container)
439440
$loader->load('web_link.php');
440441
}
441442

443+
if ($this->isConfigEnabled($container, $config['uid'])) {
444+
if (!class_exists(AbstractUid::class)) {
445+
throw new LogicException('Uid support cannot be enabled as the Uid component is not installed. Try running "composer require symfony/uid".');
446+
}
447+
448+
$this->registerUidConfiguration($config['uid'], $container, $loader);
449+
}
450+
442451
$this->addAnnotatedClassesToCompile([
443452
'**\\Controller\\',
444453
'**\\Entity\\',
@@ -2292,6 +2301,17 @@ public static function registerRateLimiter(ContainerBuilder $container, string $
22922301
$container->registerAliasForArgument($limiterId, RateLimiterFactory::class, $name.'.limiter');
22932302
}
22942303

2304+
private function registerUidConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)
2305+
{
2306+
$loader->load('uid.php');
2307+
2308+
$uidFactory = $container->getDefinition('uid.factory');
2309+
$uidFactory->setArguments([
2310+
$config['uid_factory']['default_named_version'],
2311+
$config['uid_factory']['default_timed_version'],
2312+
]);
2313+
}
2314+
22952315
private function resolveTrustedHeaders(array $headers): int
22962316
{
22972317
$trustedHeaders = 0;

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 27 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,30 @@
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:sequence>
699+
<xsd:element name="uid_factory" type="uid_factory" minOccurs="0" maxOccurs="1" />
700+
</xsd:sequence>
701+
<xsd:attribute name="enabled" type="xsd:boolean" />
702+
</xsd:complexType>
703+
704+
<xsd:complexType name="uid_factory">
705+
<xsd:attribute name="default_named_version" type="uid_named_version" />
706+
<xsd:attribute name="default_timed_version" type="uid_timed_version" />
707+
</xsd:complexType>
< 8090 /code>708+
709+
<xsd:simpleType name="uid_named_version">
710+
<xsd:restriction base="xsd:string">
711+
<xsd:enumeration value="v5" />
712+
<xsd:enumeration value="v3" />
713+
</xsd:restriction>
714+
</xsd:simpleType>
715+
716+
<xsd:simpleType name="uid_timed_version">
717+
<xsd:restriction base="xsd:string">
718+
<xsd:enumeration value="v6" />
719+
<xsd:enumeration value="v1" />
720+
</xsd:restriction>
721+
</xsd:simpleType>
695722
</xsd:schema>
Lines changed: 25 additions & 0 deletions
EDC8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\UidFactory;
15+
use Symfony\Component\Uid\UlidFactory;
16+
17+
return static function (ContainerConfigurator $container) {
18+
$container->services()
19+
->set('uid.factory', UidFactory::class)
20+
->alias(UidFactory::class, 'uid.factory')
21+
22+
->set('ulid.factory', UlidFactory::class)
23+
->alias(UlidFactory::class, 'ulid.factory')
24+
;
25+
};

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,13 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
535535
'enabled' => false,
536536
'limiters' => [],
537537
],
538+
'uid' => [
539+
'enabled' => true,
540+
'uid_factory' => [
541+
'default_named_version' => null,
542+
'default_timed_version' => null,
543+
],
544+
],
538545
];
539546
}
540547
}

src/Symfony/Component/Uid/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* added UidFactory and UlidFactory
8+
49
5.2.0
510
-----
611

0 commit comments

Comments
 (0)
0