8000 [HttpKernel] Do not attempt to register enum arguments in controller … · symfony/symfony@89232ee · GitHub
[go: up one dir, main page]

Skip to content

Commit 89232ee

Browse files
committed
[HttpKernel] Do not attempt to register enum arguments in controller service locator
1 parent 9d8638c commit 89232ee

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public function process(ContainerBuilder $container)
127127
$type = ltrim($target = (string) ProxyHelper::getTypeHint($r, $p), '\\');
128128
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
129129

130+
if (is_subclass_of($type, \UnitEnum::class, true)) {
131+
// do not attempt to register enum typed arguments
132+
continue;
133+
}
134+
130135
if (isset($arguments[$r->name][$p->name])) {
131136
$target = $arguments[$r->name][$p->name];
132137
if ('?' !== $target[0]) {

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\DependencyInjection\ServiceLocator;
2424
use Symfony\Component\DependencyInjection\TypedReference;
2525
use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass;
26+
use Symfony\Component\HttpKernel\Tests\Fixtures\Suit;
2627

2728
class RegisterControllerArgumentLocatorsPassTest extends TestCase
2829
{
@@ -369,6 +370,25 @@ public function testNotTaggedControllerServiceReceivesLocatorArgument()
369370

370371
$this->assertInstanceOf(Reference::class, $locatorArgument);
371372
}
373+
374+
/**
375+
* @requires PHP 8.1
376+
*/
377+
public function testEnumArgumentIsIgnored()
378+
{
379+
$container = new ContainerBuilder();
380+
$resolver = $container->register('argument_resolver.service')->addArgument([]);
381+
382+
$container->register('foo', NonNullableEnumArgumentWithDefaultController::class)
383+
->addTag('controller.service_arguments')
384+
;
385+
386+
$pass = new RegisterControllerArgumentLocatorsPass();
387+
$pass->process($container);
388+
389+
$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
390+
$this->assertEmpty(array_keys($locator), 'enum typed argument is ignored');
391+
}
372392
}
373393

374394
class RegisterTestController
@@ -430,3 +450,10 @@ public function fooAction(string $someArg)
430450
{
431451
}
432452
}
453+
454+
class NonNullableEnumArgumentWithDefaultController
455+
{
456+
public function fooAction(Suit $suit = Suit::Spades)
457+
{
458+
}
459+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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\HttpKernel\Tests\Fixtures;
13+
14+
enum Suit: string
15+
{
16+
case Hearts = 'H';
17+
case Diamonds = 'D';
18+
case Clubs = 'C';
19+
case Spades = 'S';
20+
}

0 commit comments

Comments
 (0)
0