Skip to content

Commit 41abb14

Browse files
willemverspyckfabpot
authored andcommitted
[DependencyInjection] Allow array for the value of Autowire attribute
1 parent b0638da commit 41abb14

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Attribute/Autowire.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#[\Attribute(\Attribute::TARGET_PARAMETER)]
2424
class Autowire
2525
{
26-
public readonly string|Expression|Reference $value;
26+
public readonly string|array|Expression|Reference $value;
2727

2828
/**
2929
* Use only ONE of the following.
@@ -33,15 +33,15 @@ class Autowire
3333
* @param string|null $expression Expression (ie 'service("some.service").someMethod()')
3434
*/
3535
public function __construct(
36-
string $value = null,
36+
string|array $value = null,
3737
string $service = null,
3838
string $expression = null,
3939
) {
4040
if (!($service xor $expression xor null !== $value)) {
4141
throw new LogicException('#[Autowire] attribute must declare exactly one of $service, $expression, or $value.');
4242
}
4343

44-
if (null !== $value && str_starts_with($value, '@')) {
44+
if (\is_string($value) && str_starts_with($value, '@')) {
4545
match (true) {
4646
str_starts_with($value, '@@') => $value = substr($value, 1),
4747
str_starts_with($value, '@=') => $expression = substr($value, 2),

Tests/Attribute/AutowireTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1616
use Symfony\Component\DependencyInjection\Exception\LogicException;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\ExpressionLanguage\Expression;
1719

1820
class AutowireTest extends TestCase
1921
{
@@ -35,4 +37,24 @@ public function testCanUseZeroForValue()
3537
{
3638
$this->assertSame('0', (new Autowire(value: '0'))->value);
3739
}
40+
41+
public function testCanUseArrayForValue()
42+
{
43+
$this->assertSame(['FOO' => 'BAR'], (new Autowire(value: ['FOO' => 'BAR']))->value);
44+
}
45+
46+
public function testCanUseValueWithAtSign()
47+
{
48+
$this->assertInstanceOf(Reference::class, (new Autowire(value: '@service'))->value);
49+
}
50+
51+
public function testCanUseValueWithDoubleAtSign()
52+
{
53+
$this->assertSame('@service', (new Autowire(value: '@@service'))->value);
54+
}
55+
56+
public function testCanUseValueWithAtAndEqualSign()
57+
{
58+
$this->assertInstanceOf(Expression::class, (new Autowire(value: '@=service'))->value);
59+
}
3860
}

0 commit comments

Comments
 (0)