From e983f64aee5130f9213508a4e5838fd58ea49993 Mon Sep 17 00:00:00 2001 From: Willem Verspyck Date: Thu, 6 Oct 2022 16:01:21 +0200 Subject: [PATCH] [DependencyInjection] Allow array for the value of Autowire attribute --- .../Attribute/Autowire.php | 6 ++--- .../Tests/Attribute/AutowireTest.php | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Attribute/Autowire.php b/src/Symfony/Component/DependencyInjection/Attribute/Autowire.php index e4c386f7da2f8..0c34f9035160b 100644 --- a/src/Symfony/Component/DependencyInjection/Attribute/Autowire.php +++ b/src/Symfony/Component/DependencyInjection/Attribute/Autowire.php @@ -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. @@ -33,7 +33,7 @@ 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, ) { @@ -41,7 +41,7 @@ public function __construct( 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, '@')) { match (true) { str_starts_with($value, '@@') => $value = substr($value, 1), str_starts_with($value, '@=') => $expression = substr($value, 2), diff --git a/src/Symfony/Component/DependencyInjection/Tests/Attribute/AutowireTest.php b/src/Symfony/Component/DependencyInjection/Tests/Attribute/AutowireTest.php index 5ec03a724f829..d45434a5817e0 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Attribute/AutowireTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Attribute/AutowireTest.php @@ -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 { @@ -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); + } }