Skip to content

[DependencyInjection] Allow array for the value of Autowire attribute #47801

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

Merged
merged 1 commit into from
Oct 18, 2022
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 @@ -23,7 +23,7 @@
#[\Attribute(\Attribute::TARGET_PARAMETER)]
class Autowire
{
public readonly string|Expression|Reference $value;
public readonly string|array|Expression|Reference $value;

/**
* Use only ONE of the following.
Expand All @@ -33,15 +33,15 @@ class Autowire
* @param string|null $expression Expression (ie 'service("some.service").someMethod()')
*/
public function __construct(
string $value = null,
string|array $value = null,
string $service = null,
string $expression = null,
) {
if (!($service xor $expression xor null !== $value)) {
throw new LogicException('#[Autowire] attribute must declare exactly one of $service, $expression, or $value.');
}

if (null !== $value && str_starts_with($value, '@')) {
if (\is_string($value) && str_starts_with($value, '@')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For an array, wouldn't this need to process the strings inside that array to convert their prefixes too ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to allow nesting services into arrays.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do allow that in Yaml or XML (which is how you passed an array of objects indexed by some keys for instance)

match (true) {
str_starts_with($value, '@@') => $value = substr($value, 1),
str_starts_with($value, '@=') => $expression = substr($value, 2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;

class AutowireTest extends TestCase
{
Expand All @@ -35,4 +37,24 @@ public function testCanUseZeroForValue()
{
$this->assertSame('0', (new Autowire(value: '0'))->value);
}

public function testCanUseArrayForValue()
{
$this->assertSame(['FOO' => 'BAR'], (new Autowire(value: ['FOO' => 'BAR']))->value);
}

public function testCanUseValueWithAtSign()
{
$this->assertInstanceOf(Reference::class, (new Autowire(value: '@service'))->value);
}

public function testCanUseValueWithDoubleAtSign()
{
$this->assertSame('@service', (new Autowire(value: '@@service'))->value);
}

public function testCanUseValueWithAtAndEqualSign()
{
$this->assertInstanceOf(Expression::class, (new Autowire(value: '@=service'))->value);
}
}