Skip to content

[DependencyInjection] Allow using BackedEnum in Target attribute #46363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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 @@ -11,6 +11,7 @@

namespace Symfony\Component\DependencyInjection\Attribute;

use BackedEnum;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;

/**
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Stringable;

enum FooBackedEnum: string
{
case BAR = 'bar';
case FOO = 'foo';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\Attribute\Target;

class WithEnumTarget
{
public function __construct(
#[Target(FooBackedEnum::BAR)]
BarInterface $bar
) {
}
}