Skip to content

Commit 95c55bb

Browse files
committed
feature #47801 [DependencyInjection] Allow array for the value of Autowire attribute (willemverspyck)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [DependencyInjection] Allow array for the value of Autowire attribute | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes | Deprecations? |no | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Since Symfony 6.1 it's possible to use #[Autowire] for the service autowiring logic. Array as autowire parameters is not supported yet. In YAML or the attribute Autoconfigure it worked. ```php #[Autoconfigure(bind: [ '$parameters' => [ 'PARAMETER_DAY_START' => '%env(string:PARAMETER_DAY_START)%', 'PARAMETER_DAY_END' => '%env(string:PARAMETER_DAY_END)%', 'FILTER_LIMIT' => '%env(int:FILTER_LIMIT)%', 'FILTER_OFFSET' => '%env(int:FILTER_OFFSET)%', ], ])] class WidgetService { public function __construct(private readonly array $parameters) { } } ``` After this PR there is support for an array (including some extra tests): ```php class WidgetService { public function __construct(#[Autowire([ 'PARAMETER_DAY_START' => '%env(string:PARAMETER_DAY_START)%', 'PARAMETER_DAY_END' => '%env(string:PARAMETER_DAY_END)%', 'FILTER_LIMIT' => '%env(int:FILTER_LIMIT)%', 'FILTER_OFFSET' => '%env(int:FILTER_OFFSET)%', ])] private readonly array $parameters) { } } ``` Commits ------- e983f64aee [DependencyInjection] Allow array for the value of Autowire attribute
2 parents 0877de2 + 41abb14 commit 95c55bb

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)