Skip to content

Commit 5d6a211

Browse files
committed
Do not ignore enum when Autowire attribute in RegisterControllerArgumentLocatorsPass
When moving services injected from the constructor to the controller arguments, I noticed a bug. We were auto wiring an env var to a backed enum like this: ```php class Foo { public function __construct( #[Autowire(env: 'enum:App\Enum:SOME_ENV_KEY')] private \App\Enum $someEnum, ) {} public function __invoke() {} } ``` This works fine with normal Symfony Dependency Injection. But when we switch to controller arguments like this: ```php class Foo { public function __invoke( #[Autowire(env: 'enum:App\Enum:SOME_ENV_KEY')] \App\Enum $someEnum, ) {} } ``` This stops working. The issue is that BackedEnum's are excluded. But this should only be excluded when there is no Autowire attribute.
1 parent f8f3fc4 commit 5d6a211

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function process(ContainerBuilder $container)
159159
continue;
160160
} elseif (!$autowire || (!($autowireAttributes ??= $p->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF)) && (!$type || '\\' !== $target[0]))) {
161161
continue;
162-
} elseif (is_subclass_of($type, \UnitEnum::class)) {
162+
} elseif (!$autowireAttributes && is_subclass_of($type, \UnitEnum::class)) {
163163
// do not attempt to register enum typed arguments if not already present in bindings
164164
continue;
165165
} elseif (!$p->allowsNull()) {

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -498,13 +498,14 @@ public function testAutowireAttribute()
498498

499499
$locator = $container->get($locatorId)->get('foo::fooAction');
500500

501-
$this->assertCount(9, $locator->getProvidedServices());
501+
$this->assertCount(10, $locator->getProvidedServices());
502502
$this->assertInstanceOf(\stdClass::class, $locator->get('service1'));
503503
$this->assertSame('foo/bar', $locator->get('value'));
504504
$this->assertSame('foo', $locator->get('expression'));
505505
$this->assertInstanceOf(\stdClass::class, $locator->get('serviceAsValue'));
506506
$this->assertInstanceOf(\stdClass::class, $locator->get('expressionAsValue'));
507507
$this->assertSame('bar', $locator->get('rawValue'));
508+
$this->stringContains('Symfony_Component_HttpKernel_Tests_Fixtures_Suit_APP_SUIT', $locator->get('suit'));
508509
$this->assertSame('@bar', $locator->get('escapedRawValue'));
509510
$this->assertSame('foo', $locator->get('customAutowire'));
510511
$this->assertInstanceOf(FooInterface::class, $autowireCallable = $locator->get('autowireCallable'));
@@ -719,6 +720,8 @@ public function fooAction(
719720
\stdClass $expressionAsValue,
720721
#[Autowire('bar')]
721722
string $rawValue,
723+
#[Autowire(env: 'enum:\Symfony\Component\HttpKernel\Tests\Fixtures\Suit:APP_SUIT')]
724+
Suit $suit,
722725
#[Autowire('@@bar')]
723726
string $escapedRawValue,
724727
#[CustomAutowire('some.parameter')]

0 commit comments

Comments
 (0)