Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0df232a

Browse files
committed
[Uid] Add UidFactory to create Ulid and Uuid from timestamps and randomness/nodes
1 parent 72a82c3 commit 0df232a

File tree

14 files changed

+506
-22
lines changed

14 files changed

+506
-22
lines changed

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

Lines changed: 39 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\AbstractUid;
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,41 @@ 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(AbstractUid::class) ? 'canBeDisabled' : 'canBeEnabled'}()
1904+
->children()
1905+
->arrayNode('uuid_factory')
1906+
->addDefaultsIfNotSet()
1907+
->children()
1908+
->enumNode('default_named_version')
1909+
->defaultValue('5')
1910+
->cannotBeEmpty()
1911+
->values(['5', '3'])
1912+
->end()
1913+
->end()
1914+
->enumNode('default_timed_version')
1915+
->defaultValue('6')
1916+
->cannotBeEmpty()
1917+
->values(['6', '1'])
1918+
->end()
1919+
->end()
1920+
->scalarNode('default_namespace')
1921+
->cannotBeEmpty()
1922+
->end()
1923+
->scalarNode('default_node')
1924+
->cannotBeEmpty()
1925+
->end()
1926+
->end()
1927+
->end()
1928+
->end()
1929+
->end()
1930+
->end()
1931+
;
1932+
}
18941933
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
use Symfony\Component\Translation\Command\XliffLintCommand as BaseXliffLintCommand;
159159
use Symfony\Component\Translation\PseudoLocalizationTranslator;
160160
use Symfony\Component\Translation\Translator;
161+
use Symfony\Component\Uid\AbstractUid;
161162
use Symfony\Component\Validator\ConstraintValidatorInterface;
162163
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
163164
use Symfony\Component\Validator\ObjectInitializerInterface;
@@ -447,6 +448,14 @@ public function load(array $configs, ContainerBuilder $container)
447448
$loader->load('web_link.php');
448449
}
449450

451+
if ($this->isConfigEnabled($container, $config['uid'])) {
452+
if (!class_exists(AbstractUid::class)) {
453+
throw new LogicException('Uid support cannot be enabled as the Uid component is not installed. Try running "composer require symfony/uid".');
454+
}
455+
456+
$this->registerUidConfiguration($config['uid'], $container, $loader);
457+
}
458+
450459
$this->addAnnotatedClassesToCompile([
451460
'**\\Controller\\',
452461
'**\\Entity\\',
@@ -2312,6 +2321,25 @@ public static function registerRateLimiter(ContainerBuilder $container, string $
23122321
$container->registerAliasForArgument($limiterId, RateLimiterFactory::class, $name.'.limiter');
23132322
}
23142323

2324+
private function registerUidConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)
2325+
{
2326+
$loader->load('uid.php');
2327+
2328+
$uidFactory = $container->getDefinition('uuid.factory');
2329+
$uidFactory->setArguments([
2330+
$config['uuid_factory']['default_named_version'],
2331+
$config['uuid_factory']['default_timed_version'],
2332+
]);
2333+
2334+
if (isset($config['uuid_factory']['default_namespace'])) {
2335+
$uidFactory->addMethodCall('withDefaultNamespace', [$config['uuid_factory']['default_namespace']], true);
2336+
}
2337+
2338+
if (isset($config['uuid_factory']['default_node'])) {
2339+
$uidFactory->addMethodCall('withDefaultNode', [$config['uuid_factory']['default_node']], true);
2340+
}
2341+
}
2342+
23152343
private function resolveTrustedHeaders(array $headers): int
23162344
{
23172345
$trustedHeaders = 0;

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

Lines changed: 29 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,32 @@
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="uuid_factory" type="uuid_factory" minOccurs="0" maxOccurs="1" />
700+
</xsd:sequence>
701+
<xsd:attribute name="enabled" type="xsd:boolean" />
702+
</xsd:complexType>
703+
704+
<xsd:complexType name="uuid_factory">
705+
<xsd:attribute name="default_named_version" type="uuid_named_version" />
706+
<xsd:attribute name="default_timed_version" type="uuid_timed_version" />
707+
<xsd:attribute name="default_namespace" type="xsd:string" />
708+
<xsd:attribute name="default_node" type="xsd:string" />
709+
</xsd:complexType>
710+
711+
<xsd:simpleType name="uuid_named_version">
712+
<xsd:restriction base="xsd:string">
713+
<xsd:enumeration value="5" />
714+
<xsd:enumeration value="3" />
715+
</xsd:restriction>
716+
</xsd:simpleType>
717+
718+
<xsd:simpleType name="uuid_timed_version">
719+
<xsd:restriction base="xsd:string">
720+
<xsd:enumeration value="6" />
721+
<xsd:enumeration value="1" />
722+
</xsd:restriction>
723+
</xsd:simpleType>
695724
</xsd:schema>
Lines changed: 25 additions & 0 deletions
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 <[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\UlidFactory;
15+
use Symfony\Component\Uid\UuidFactory;
16+
17+
return static function (ContainerConfigurator $container) {
18+
$container->services()
19+
->set('uuid.factory', UuidFactory::class)
20+
->alias(UuidFactory::class, 'uuid.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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Symfony\Component\Mailer\Mailer;
2323
use Symfony\Component\Messenger\MessageBusInterface;
2424
use Symfony\Component\Notifier\Notifier;
25+
use Symfony\Component\Uid\UuidV5;
26+
use Symfony\Component\Uid\UuidV6;
2527

2628
class ConfigurationTest extends TestCase
2729
{
@@ -563,6 +565,13 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
563565
'enabled' => false,
564566
'limiters' => [],
565567
],
568+
'uid' => [
569+
'enabled' => true,
570+
'uuid_factory' => [
571+
'default_named_version' => UuidV5::class,
572+
'default_timed_version' => UuidV6::class,
573+
],
574+
],
566575
];
567576
}
568577
}

src/Symfony/Component/Uid/BinaryUtil.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,19 @@ public static function timeToFloat(string $time): float
126126

127127
return self::toBase($time, self::BASE10) / 10000000;
128128
}
129+
130+
/**
131+
* @param float $time Seconds since the Unix epoch 1970-01-01 00:00:00
132+
*
133+
* @return string Count of 100-nanosecond intervals since the UUID epoch 1582-10-15 00:00:00 in hexadecimal
134+
*/
135+
public static function floatToTime(float $time): string
136+
{
137+
if (\PHP_INT_SIZE >= 8) {
138+
return str_pad(dechex(self::TIME_OFFSET_INT + (int) round($time * 10000000)), 16, '0', \STR_PAD_LEFT);
139+
}
140+
141+
// TODO
142+
return 'foo';
143+
}
129144
}

src/Symfony/Component/Uid/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `AbstractUid::fromBinary()`, `AbstractUid::fromBase58()`, `AbstractUid::fromBase32()` and `AbstractUid::fromRfc4122()`
8+
* Add `UuidFactory` and `UlidFactory`
89

910
5.2.0
1011
-----
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Uid\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Uid\UlidFactory;
16+
17+
final class UlidFactoryTest extends TestCase
18+
{
19+
public function testCreate()
20+
{
21+
$ulidFactory = new UlidFactory();
22+
23+
$ulidFactory->create();
24+
25+
$ulid1 = $ulidFactory->create(999999.123);
26+
$this->assertSame(999999.123, $ulid1->getTime());
27+
$ulid2 = $ulidFactory->create(999999.123);
28+
$this->assertSame(999999.123, $ulid2->getTime());
29+
30+
$this->assertFalse($ulid1->equals($ulid2));
31+
$this->assertSame(-1, ($ulid1->compare($ulid2)));
32+
33+
$ulid3 = $ulidFactory->create(1234.16252444);
34+
$this->assertSame(1234.163, $ulid3->getTime());
35+
}
36+
37+
public function testCreateWithInvalidTimestamp()
38+
{
39+
$this->expectException(\InvalidArgumentException::class);
40+
$this->expectExceptionMessage('The timestamp must be positive.');
41+
42+
(new UlidFactory())->create(-1000);
43+
}
44+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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\Uid\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Uid\Uuid;
16+
use Symfony\Component\Uid\UuidFactory;
17+
use Symfony\Component\Uid\UuidV1;
18+
use Symfony\Component\Uid\UuidV3;
19+
use Symfony\Component\Uid\UuidV4;
20+
use Symfony\Component\Uid\UuidV5;
21+
use Symfony\Component\Uid\UuidV6;
22+
23+
final class UuidFactoryTest extends TestCase
24+
{
25+
public function testCreateNamedDefaultVersion()
26+
{
27+
$this->assertInstanceOf(UuidV5::class, (new UuidFactory())->createNamed('foo'));
28+
$this->assertInstanceOf(UuidV3::class, (new UuidFactory('3'))->createNamed('foo'));
29+
}
30+
31+
public function testCreateNamed()
32+
{
33+
$uuidFactory = new UuidFactory();
34+
35+
// Test custom namespace
36+
$uuid1 = $uuidFactory->createNamed('foo', Uuid::fromString('6f80c216-0492-4421-bd82-c10ab929ae84'));
37+
$this->assertInstanceOf(UuidV5::class, $uuid1);
38+
$this->assertSame('d521ceb7-3e31-5954-b873-92992c697ab9', (string) $uuid1);
39+
40+
// Test default namespace
41+
$uuidFactory = $uuidFactory->withDefaultNamespace(Uuid::fromString('6f80c216-0492-4421-bd82-c10ab929ae84'));
42+
$uuid2 = $uuidFactory->createNamed('foo');
43+
$this->assertInstanceOf(UuidV5::class, $uuid2);
44+
$this->assertTrue($uuid1->equals($uuid2));
45+
46+
// Test default namespace override
47+
$uuid3 = $uuidFactory->createNamed('foo', Uuid::v4());
48+
$this->assertFalse($uuid2->equals($uuid3));
49+
50+
// Test version override
51+
$uuid4 = $uuidFactory->createNamed('foo', null, '3');
52+
$this->assertInstanceOf(UuidV3::class, $uuid4);
53+
}
54+
55+
public function testCreateNamedWithUnsupportedVersion()
56+
{
57+
$this->expectException(\InvalidArgumentException::class);
58+
$this->expectExceptionMessage('The version "1" is unsupported by this method.');
59+
60+
(new UuidFactory())->createNamed('foo', null, '1');
61+
}
62+
63+
public function testCreateTimedDefaultVersion()
64+
{
65+
$this->assertInstanceOf(UuidV6::class, (new UuidFactory())->createTimed());
66+
$this->assertInstanceOf(UuidV1::class, (new UuidFactory(null, '1'))->createTimed());
67+
}
68+
69+
public function testCreateTimed()
70+
{
71+
$uuidFactory = new UuidFactory();
72+
73+
// Test custom timestamp and node
74+
$uuid1 = $uuidFactory->createTimed(1611076938.0578, Uuid::fromString('6f80c216-0492-4421-bd82-c10ab929ae84'));
75+
$this->assertInstanceOf(UuidV6::class, $uuid1);
76+
$this->assertSame(1611076938.0578, $uuid1->getTime());
77+
$this->assertSame('c10ab929ae84', $uuid1->getNode());
78+
79+
// Test default node
80+
$uuidFactory = $uuidFactory->withDefaultNode(Uuid::fromString('6f80c216-0492-4421-bd82-c10ab929ae84'));
81+
$uuid2 = $uuidFactory->createTimed();
82+
$this->assertInstanceOf(UuidV6::class, $uuid2);
83+
$this->assertSame('c10ab929ae84', $uuid2->getNode());
84+
85+
// Test default node override
86+
$uuid3 = $uuidFactory->createTimed(null, Uuid::fromString('7c1ede70-3586-48ed-a984-23c8018d9174'));
87+
$this->assertInstanceOf(UuidV6::class, $uuid3);
88+
$this->assertSame('23c8018d9174', $uuid3->getNode());
89+
90+
// Test version override
91+
$uuid4 = $uuidFactory->createTimed(null, null, '1');
92+
$this->assertInstanceOf(UuidV1::class, $uuid4);
93+
$this->assertSame($uuid2->getNode(), $uuid4->getNode());
94+
95+
// Test negative timestamp and round
96+
$uuid5 = $uuidFactory->createTimed(-42424242.12318765525);
97+
$this->assertSame(-42424242.1231877, $uuid5->getTime());
98+
}
99+
100+
public function testCreateTimedWithUnsupportedVersion()
101+
{
102+
$this->expectException(\InvalidArgumentException::class);
103+
$this->expectExceptionMessage('The version "4" is unsupported by this method.');
104+
105+
(new UuidFactory())->createTimed(null, null, '4');
106+
}
107+
108+
public function testCreateRandom()
109+
{
110+
$this->assertInstanceOf(UuidV4::class, (new UuidFactory())->createRandom());
111+
}
112+
}

0 commit comments

Comments
 (0)