Skip to content

Commit b7acee8

Browse files
committed
[DependencyInjection] adjust Autowire attribute implementation
1 parent 92edf0f commit b7acee8

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

Attribute/Autowire.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,27 @@ class Autowire
2828
/**
2929
* Use only ONE of the following.
3030
*
31+
* @param string|null $value Parameter value (ie "%kernel.project_dir%/some/path")
3132
* @param string|null $service Service ID (ie "some.service")
3233
* @param string|null $expression Expression (ie 'service("some.service").someMethod()')
33-
* @param string|null $value Parameter value (ie "%kernel.project_dir%/some/path")
3434
*/
3535
public function __construct(
36+
?string $value = null,
3637
?string $service = null,
3738
?string $expression = null,
38-
?string $value = 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, '@')) {
45+
match (true) {
46+
str_starts_with($value, '@@') => $value = substr($value, 1),
47+
str_starts_with($value, '@=') => $expression = substr($value, 2),
48+
default => $service = substr($value, 1),
49+
};
50+
}
51+
4452
$this->value = match (true) {
4553
null !== $service => new Reference($service),
4654
null !== $expression => class_exists(Expression::class) ? new Expression($expression) : throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed. Try running "composer require symfony/expression-language".'),

Tests/Compiler/AutowirePassTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,11 +1141,15 @@ public function testAutowireAttribute()
11411141

11421142
$definition = $container->getDefinition(AutowireAttribute::class);
11431143

1144-
$this->assertCount(4, $definition->getArguments());
1144+
$this->assertCount(8, $definition->getArguments());
11451145
$this->assertEquals(new Reference('some.id'), $definition->getArgument(0));
11461146
$this->assertEquals(new Expression("parameter('some.parameter')"), $definition->getArgument(1));
11471147
$this->assertSame('%some.parameter%/bar', $definition->getArgument(2));
1148-
$this->assertEquals(new Reference('invalid.id', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(3));
1148+
$this->assertEquals(new Reference('some.id'), $definition->getArgument(3));
1149+
$this->assertEquals(new Expression("parameter('some.parameter')"), $definition->getArgument(4));
1150+
$this->assertSame('bar', $definition->getArgument(5));
1151+
$this->assertSame('@bar', $definition->getArgument(6));
1152+
$this->assertEquals(new Reference('invalid.id', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(7));
11491153

11501154
$container->compile();
11511155

@@ -1154,6 +1158,10 @@ public function testAutowireAttribute()
11541158
$this->assertInstanceOf(\stdClass::class, $service->service);
11551159
$this->assertSame('foo', $service->expression);
11561160
$this->assertSame('foo/bar', $service->value);
1161+
$this->assertInstanceOf(\stdClass::class, $service->serviceAsValue);
1162+
$this->assertSame('foo', $service->expressionAsValue);
1163+
$this->assertSame('bar', $service->rawValue);
1164+
$this->assertSame('@bar', $service->escapedRawValue);
11571165
$this->assertNull($service->invalid);
11581166
}
11591167
}

Tests/Fixtures/includes/autowiring_classes_80.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ public function __construct(
3737
public string $expression,
3838
#[Autowire(value: '%some.parameter%/bar')]
3939
public string $value,
40+
#[Autowire('@some.id')]
41+
public \stdClass $serviceAsValue,
42+
#[Autowire("@=parameter('some.parameter')")]
43+
public string $expressionAsValue,
44+
#[Autowire('bar')]
45+
public string $rawValue,
46+
#[Autowire('@@bar')]
47+
public string $escapedRawValue,
4048
#[Autowire(service: 'invalid.id')]
4149
public ?\stdClass $invalid = null,
4250
) {

0 commit comments

Comments
 (0)