File tree Expand file tree Collapse file tree 2 files changed +25
-3
lines changed Expand file tree Collapse file tree 2 files changed +25
-3
lines changed Original file line number Diff line number Diff line change 23
23
#[\Attribute(\Attribute::TARGET_PARAMETER )]
24
24
class Autowire
25
25
{
26
- public readonly string |Expression |Reference $ value ;
26
+ public readonly string |array | Expression |Reference $ value ;
27
27
28
28
/**
29
29
* Use only ONE of the following.
@@ -33,15 +33,15 @@ class Autowire
33
33
* @param string|null $expression Expression (ie 'service("some.service").someMethod()')
34
34
*/
35
35
public function __construct (
36
- string $ value = null ,
36
+ string | array $ value = null ,
37
37
string $ service = null ,
38
38
string $ expression = null ,
39
39
) {
40
40
if (!($ service xor $ expression xor null !== $ value )) {
41
41
throw new LogicException ('#[Autowire] attribute must declare exactly one of $service, $expression, or $value. ' );
42
42
}
43
43
44
- if (null !== $ value && str_starts_with ($ value , '@ ' )) {
44
+ if (\is_string ( $ value) && str_starts_with ($ value , '@ ' )) {
45
45
match (true ) {
46
46
str_starts_with ($ value , '@@ ' ) => $ value = substr ($ value , 1 ),
47
47
str_starts_with ($ value , '@= ' ) => $ expression = substr ($ value , 2 ),
Original file line number Diff line number Diff line change 14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Component \DependencyInjection \Attribute \Autowire ;
16
16
use Symfony \Component \DependencyInjection \Exception \LogicException ;
17
+ use Symfony \Component \DependencyInjection \Reference ;
18
+ use Symfony \Component \ExpressionLanguage \Expression ;
17
19
18
20
class AutowireTest extends TestCase
19
21
{
@@ -35,4 +37,24 @@ public function testCanUseZeroForValue()
35
37
{
36
38
$ this ->assertSame ('0 ' , (new Autowire (value: '0 ' ))->value );
37
39
}
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
+ }
38
60
}
You can’t perform that action at this time.
0 commit comments