Skip to content

Commit 49c3f64

Browse files
committed
[Uid] Add the UidValueResolver argument value resolver
1 parent 0f03f94 commit 49c3f64

File tree

19 files changed

+380
-1
lines changed

19 files changed

+380
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,9 @@ private function addUidSection(ArrayNodeDefinition $rootNode, callable $enableIf
20382038
->scalarNode('time_based_uuid_node')
20392039
->cannotBeEmpty()
20402040
->end()
2041+
->arrayNode('argument_value_resolver')
2042+
->canBeEnabled()
2043+
->end()
20412044
->end()
20422045
->end()
20432046
->end()

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@
190190
use Symfony\Component\Translation\Command\XliffLintCommand as BaseXliffLintCommand;
191191
use Symfony\Component\Translation\PseudoLocalizationTranslator;
192192
use Symfony\Component\Translation\Translator;
193+
use Symfony\Component\Uid\AbstractUid;
194+
use Symfony\Component\Uid\ArgumentResolver\UidValueResolver;
193195
use Symfony\Component\Uid\Factory\UuidFactory;
194196
use Symfony\Component\Uid\UuidV4;
195197
use Symfony\Component\Validator\ConstraintValidatorInterface;
@@ -2545,6 +2547,18 @@ private function registerUidConfiguration(array $config, ContainerBuilder $conta
25452547
$container->getDefinition('name_based_uuid.factory')
25462548
->setArguments([$config['name_based_uuid_namespace']]);
25472549
}
2550+
2551+
if (!$config['argument_value_resolver']['enabled']) {
2552+
$container->removeDefinition('argument_resolver.uid');
2553+
2554+
return;
2555+
} elseif (!class_exists(UidValueResolver::class)) {
2556+
throw new LogicException('Uid argument value resolver cannot be enabled as the installed Uid component version is too low. Try running "composer require symfony/uid:^6.1".');
2557+
}
2558+
2559+
$container->setParameter('uid.base58', AbstractUid::BASE58);
2560+
$container->setParameter('uid.base32', AbstractUid::BASE32);
2561+
$container->setParameter('uid.rfc4122', AbstractUid::RFC4122);
25482562
}
25492563

25502564
private function resolveTrustedHeaders(array $headers): int

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,9 @@
735735
</xsd:complexType>
736736

737737
<xsd:complexType name="uid">
738+
<xsd:sequence>
739+
<xsd:element name="argument_value_resolver" type="uid_argument_value_resolver" minOccurs="0" />
740+
</xsd:sequence>
738741
<xsd:attribute name="enabled" type="xsd:boolean" />
739742
<xsd:attribute name="default_uuid_version" type="default_uuid_version" />
740743
<xsd:attribute name="name_based_uuid_version" type="name_based_uuid_version" />
@@ -765,6 +768,10 @@
765768
</xsd:restriction>
766769
</xsd:simpleType>
767770

771+
<xsd:complexType name="uid_argument_value_resolver">
772+
<xsd:attribute name="enabled" type="xsd:boolean" />
773+
</xsd:complexType>
774+
768775
<xsd:complexType name="notifier">
769776
<xsd:sequence>
770777
<xsd:element name="chatter-transport" type="chatter-transport" minOccurs="0" maxOccurs="unbounded" />

src/Symfony/Bundle/FrameworkBundle/Resources/config/uid.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

14+
use Symfony\Component\Uid\ArgumentResolver\UidValueResolver;
1415
use Symfony\Component\Uid\Factory\NameBasedUuidFactory;
1516
use Symfony\Component\Uid\Factory\RandomBasedUuidFactory;
1617
use Symfony\Component\Uid\Factory\TimeBasedUuidFactory;
@@ -37,5 +38,8 @@
3738
->set('time_based_uuid.factory', TimeBasedUuidFactory::class)
3839
->factory([service('uuid.factory'), 'timeBased'])
3940
->alias(TimeBasedUuidFactory::class, 'time_based_uuid.factory')
41+
42+
->set('argument_resolver.uid', UidValueResolver::class)
43+
->tag('controller.argument_value_resolver', ['priority' => 150]) // Higher than RequestAttributeValueResolver
4044
;
4145
};

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
580580
'default_uuid_version' => 6,
581581
'name_based_uuid_version' => 5,
582582
'time_based_uuid_version' => 6,
583+
'argument_value_resolver' => [
584+
'enabled' => false,
585+
],
583586
],
584587
'exceptions' => [],
585588
];
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\Routing\Annotation\Route;
16+
use Symfony\Component\Uid\Ulid;
17+
use Symfony\Component\Uid\UuidV1;
18+
use Symfony\Component\Uid\UuidV6;
19+
20+
class UidController
21+
{
22+
#[Route(path: '/1/uuid-v1/{userId}')]
23+
public function anyFormat(UuidV1 $userId): Response
24+
{
25+
return new Response($userId);
26+
}
27+
28+
#[Route(path: '/2/ulid/{id}', requirements: ['id' => Ulid::BASE58])]
29+
public function specificFormatInAttribute(Ulid $id): Response
30+
{
31+
return new Response($id);
32+
}
33+
34+
#[Route(path: '/3/uuid-v1/{id<%uid.base32%>}')]
35+
public function specificFormatInPath(UuidV1 $id): Response
36+
{
37+
return new Response($id);
38+
}
39+
40+
#[Route(path: '/4/uuid-v1/{postId}/custom-uid/{commentId}')]
41+
public function manyUids(UuidV1 $postId, TestCommentIdentifier $commentId): Response
42+
{
43+
return new Response($postId."\n".$commentId);
44+
}
45+
}
46+
47+
class TestCommentIdentifier extends Ulid
48+
{
49+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ array_controller:
6060
send_email:
6161
path: /send_email
6262
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\EmailController::indexAction }
63+
64+
uid:
65+
resource: "../../Controller/UidController.php"
66+
type: "annotation"
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\UidController;
15+
use Symfony\Component\Uid\ArgumentResolver\UidValueResolver;
16+
use Symfony\Component\Uid\Ulid;
17+
use Symfony\Component\Uid\UuidV1;
18+
use Symfony\Component\Uid\UuidV4;
19+
use Symfony\Component\Uid\UuidV6;
20+
21+
class UidTest extends AbstractWebTestCase
22+
{
23+
/**
24+
* @see UidController
25+
*/
26+
public function testArgumentValueResolverConfigAllDefaults()
27+
{
28+
if (!class_exists(UidValueResolver::class)) {
29+
$this->markTestSkipped('Needs symfony/uid >= 6.1');
30+
}
31+
32+
$client = $this->createClient(['test_case' => 'Uid', 'root_config' => 'config.yml']);
33+
34+
// Any format
35+
$client->request('GET', '/1/uuid-v1/'.$uuidV1 = new UuidV1());
36+
$this->assertSame((string) $uuidV1, $client->getResponse()->getContent());
37+
$client->request('GET', '/1/uuid-v1/'.$uuidV1->toBase58());
38+
$this->assertSame((string) $uuidV1, $client->getResponse()->getContent());
39+
$client->request('GET', '/1/uuid-v1/'.$uuidV1->toRfc4122());
40+
$this->assertSame((string) $uuidV1, $client->getResponse()->getContent());
41+
// Bad version
42+
$client->request('GET', '/1/uuid-v1/'.$uuidV4 = new UuidV4());
43+
$this->assertSame(404, $client->getResponse()->getStatusCode());
44+
45+
// Only base58 format
46+
$client->request('GET', '/2/ulid/'.($ulid = new Ulid())->toBase58());
47+
$this->assertSame((string) $ulid, $client->getResponse()->getContent());
48+
$client->request('GET', '/2/ulid/'.$ulid);
49+
$this->assertSame(404, $client->getResponse()->getStatusCode());
50+
$client->request('GET', '/2/ulid/'.$ulid->toRfc4122());
51+
$this->assertSame(404, $client->getResponse()->getStatusCode());
52+
53+
// Only base32 format
54+
$client->request('GET', '/3/uuid-v1/'.$uuidV1->toBase32());
55+
$this->assertSame((string) $uuidV1, $client->getResponse()->getContent());
56+
$client->request('GET', '/3/uuid-v1/'.$uuidV1);
57+
$this->assertSame(404, $client->getResponse()->getStatusCode());
58+
$client->request('GET', '/3/uuid-v1/'.$uuidV1->toBase58());
59+
$this->assertSame(404, $client->getResponse()->getStatusCode());
60+
// Bad version
61+
$client->request('GET', '/3/uuid-v1/'.(new UuidV6())->toBase32());
62+
$this->assertSame(404, $client->getResponse()->getStatusCode());
63+
64+
// Any format for both
65+
$client->request('GET', '/4/uuid-v1/'.$uuidV1.'/custom-uid/'.$ulid->toRfc4122());
66+
$this->assertSame($uuidV1."\n".$ulid, $client->getResponse()->getContent());
67+
$client->request('GET', '/4/uuid-v1/'.$uuidV1->toBase58().'/custom-uid/'.$ulid->toBase58());
68+
$this->assertSame($uuidV1."\n".$ulid, $client->getResponse()->getContent());
69+
// Bad version
70+
$client->request('GET', '/4/uuid-v1/'.$uuidV4.'/custom-uid/'.$ulid);
71+
$this->assertSame(404, $client->getResponse()->getStatusCode());
72+
}
73+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
14+
15+
return [
16+
new FrameworkBundle(),
17+
new TestBundle(),
18+
];
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
imports:
2+
- { resource: "../config/default.yml" }
3+
4+
framework:
5+
uid:
6+
argument_value_resolver: ~

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy