diff --git a/src/Symfony/Component/DependencyInjection/Attribute/Target.php b/src/Symfony/Component/DependencyInjection/Attribute/Target.php index 7751b3813bada..9c8fd04a9659e 100644 --- a/src/Symfony/Component/DependencyInjection/Attribute/Target.php +++ b/src/Symfony/Component/DependencyInjection/Attribute/Target.php @@ -11,6 +11,7 @@ namespace Symfony\Component\DependencyInjection\Attribute; +use BackedEnum; use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; /** @@ -23,9 +24,9 @@ final class Target { public string $name; - public function __construct(string $name) + public function __construct(string|BackedEnum $name) { - $this->name = lcfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $name)))); + $this->name = lcfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $name instanceof BackedEnum ? $name->value : $name)))); } public static function parseName(\ReflectionParameter $parameter): string diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php index eac1ec023d7a9..dc57d34096253 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php @@ -29,6 +29,7 @@ use Symfony\Component\DependencyInjection\Tests\Fixtures\BarInterface; use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass; use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\FooVariadic; +use Symfony\Component\DependencyInjection\Tests\Fixtures\WithEnumTarget; use Symfony\Component\DependencyInjection\Tests\Fixtures\WithTarget; use Symfony\Component\DependencyInjection\TypedReference; use Symfony\Component\ExpressionLanguage\Expression; @@ -1106,6 +1107,20 @@ public function testArgumentWithTarget() $this->assertSame(BarInterface::class.' $imageStorage', (string) $container->getDefinition('with_target')->getArgument(0)); } + public function testArgumentWithEnumTarget() + { + $container = new ContainerBuilder(); + + $container->register(BarInterface::class, BarInterface::class); + $container->register(BarInterface::class.' $bar', BarInterface::class); + $container->register('with_target', WithEnumTarget::class) + ->setAutowired(true); + + (new AutowirePass())->process($container); + + $this->assertSame(BarInterface::class.' $bar', (string) $container->getDefinition('with_target')->getArgument(0)); + } + public function testDecorationWithServiceAndAliasedInterface() { $container = new ContainerBuilder(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooBackedEnum.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooBackedEnum.php new file mode 100644 index 0000000000000..f64c3d93ebf54 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooBackedEnum.php @@ -0,0 +1,11 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures; + +use Symfony\Component\DependencyInjection\Attribute\Target; + +class WithEnumTarget +{ + public function __construct( + #[Target(FooBackedEnum::BAR)] + BarInterface $bar + ) { + } +}