Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
if (
$value->isAutowired()
&& !$value->hasTag('container.ignore_attributes')
&& ($parameter->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF)
|| $parameter->getAttributes(Target::class, \ReflectionAttribute::IS_INSTANCEOF))
&& $parameter->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF)
) {
continue;
}
Expand All @@ -202,13 +201,17 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
if ($typeHint && (
\array_key_exists($k = preg_replace('/(^|[(|&])\\\\/', '\1', $typeHint).' $'.$name, $bindings)
|| \array_key_exists($k = preg_replace('/(^|[(|&])\\\\/', '\1', $typeHint).' $'.$parsedName, $bindings)
|| ($name !== $parameter->name && \array_key_exists($k = preg_replace('/(^|[(|&])\\\\/', '\1', $typeHint).' $'.$parameter->name, $bindings))
)) {
$arguments[$key] = $this->getBindingValue($bindings[$k]);

continue;
}

if (\array_key_exists($k = '$'.$name, $bindings) || \array_key_exists($k = '$'.$parsedName, $bindings)) {
if (\array_key_exists($k = '$'.$name, $bindings)
|| \array_key_exists($k = '$'.$parsedName, $bindings)
|| ($name !== $parameter->name && \array_key_exists($k = '$'.$parameter->name, $bindings))
) {
$arguments[$key] = $this->getBindingValue($bindings[$k]);

continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\Attribute\Target;
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
use Symfony\Component\DependencyInjection\Compiler\DefinitionErrorExceptionPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
Expand Down Expand Up @@ -276,4 +277,44 @@ public function testAbstractArg()

$this->assertEquals(['C', 'K'], $definition->getArguments());
}

public function testBindingsOnTargetedArguments()
{
$container = new ContainerBuilder();
$container->register('service', TargetedBindingsService::class)
->setAutowired(true)
->setBindings([
'$targetName' => 'bound_via_target',
'$variableName' => 'bound_via_variable',
'$commonName' => 'bound_via_common_name',
]);

(new ResolveBindingsPass())->process($container);

$definition = $container->getDefinition('service');

// 1. Priority: Binding matches the #[Target] name
$this->assertSame('bound_via_target', $definition->getArgument(0));

// 2. Fallback: Binding matches the variable name (Target name 'unusedTarget' has no binding)
$this->assertSame('bound_via_variable', $definition->getArgument(1));

// 3. Equality: Target name and variable name are identical
$this->assertSame('bound_via_common_name', $definition->getArgument(2));
}
}

class TargetedBindingsService
{
public function __construct(
#[Target('targetName')]
public $arg1,

#[Target('unusedTarget')]
public $variableName,

#[Target('commonName')]
public $commonName,
) {
}
}