|
16 | 16 | use Symfony\Component\DependencyInjection\Argument\BoundArgument; |
17 | 17 | use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; |
18 | 18 | use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; |
| 19 | +use Symfony\Component\DependencyInjection\Attribute\Target; |
19 | 20 | use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass; |
20 | 21 | use Symfony\Component\DependencyInjection\Compiler\DefinitionErrorExceptionPass; |
21 | 22 | use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass; |
@@ -276,4 +277,44 @@ public function testAbstractArg() |
276 | 277 |
|
277 | 278 | $this->assertEquals(['C', 'K'], $definition->getArguments()); |
278 | 279 | } |
| 280 | + |
| 281 | + public function testBindingsOnTargetedArguments() |
| 282 | + { |
| 283 | + $container = new ContainerBuilder(); |
| 284 | + $container->register('service', TargetedBindingsService::class) |
| 285 | + ->setAutowired(true) |
| 286 | + ->setBindings([ |
| 287 | + '$targetName' => 'bound_via_target', |
| 288 | + '$variableName' => 'bound_via_variable', |
| 289 | + '$commonName' => 'bound_via_common_name', |
| 290 | + ]); |
| 291 | + |
| 292 | + (new ResolveBindingsPass())->process($container); |
| 293 | + |
| 294 | + $definition = $container->getDefinition('service'); |
| 295 | + |
| 296 | + // 1. Priority: Binding matches the #[Target] name |
| 297 | + $this->assertSame('bound_via_target', $definition->getArgument(0)); |
| 298 | + |
| 299 | + // 2. Fallback: Binding matches the variable name (Target name 'unusedTarget' has no binding) |
| 300 | + $this->assertSame('bound_via_variable', $definition->getArgument(1)); |
| 301 | + |
| 302 | + // 3. Equality: Target name and variable name are identical |
| 303 | + $this->assertSame('bound_via_common_name', $definition->getArgument(2)); |
| 304 | + } |
| 305 | +} |
| 306 | + |
| 307 | +class TargetedBindingsService |
| 308 | +{ |
| 309 | + public function __construct( |
| 310 | + #[Target('targetName')] |
| 311 | + public $arg1, |
| 312 | + |
| 313 | + #[Target('unusedTarget')] |
| 314 | + public $variableName, |
| 315 | + |
| 316 | + #[Target('commonName')] |
| 317 | + public $commonName, |
| 318 | + ) { |
| 319 | + } |
279 | 320 | } |
0 commit comments