8000 [HttpKernel] Do not attempt to register enum arguments in controller service locator by ogizanagi · Pull Request #44826 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public function process(ContainerBuilder $container)
$type = ltrim($target = (string) ProxyHelper::getTypeHint($r, $p), '\\');
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;

if (is_subclass_of($type, \UnitEnum::class, true)) {
// do not attempt to register enum typed arguments
continue;
}

if (isset($arguments[$r->name][$p->name])) {
$target = $arguments[$r->name][$p->name];
if ('?' !== $target[0]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\DependencyInjection\TypedReference;
use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass;
use Symfony\Component\HttpKernel\Tests\Fixtures\Suit;

class RegisterControllerArgumentLocatorsPassTest extends TestCase
{
Expand Down Expand Up @@ -369,6 +370,25 @@ public function testNotTaggedControllerServiceReceivesLocatorArgument()

$this->assertInstanceOf(Reference::class, $locatorArgument);
}

/**
* @requires PHP 8.1
*/
public function testEnumArgumentIsIgnored()
{
$container = new ContainerBuilder();
$resolver = $container->register('argument_resolver.service')->addArgument([]);

$container->register('foo', NonNullableEnumArgumentWithDefaultController::class)
->addTag('controller.service_arguments')
;

$pass = new RegisterControllerArgumentLocatorsPass();
$pass->process($container);

$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
$this->assertEmpty(array_keys($locator), 'enum typed argument is ignored');
}
}

class RegisterTestController
Expand Down Expand Up @@ -430,3 +450,10 @@ public function fooAction(string $someArg)
{
}
}

class NonNullableEnumArgumentWithDefaultController
{
public function fooAction(Suit $suit = Suit::Spades)
{
}
}
20 changes: 20 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/Fixtures/Suit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Tests\Fixtures;

enum Suit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}
0