8000 [DependencyInjection] Fix order of arguments when mixing positional and named ones by nicolas-grekas · Pull Request #49126 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Fix order of arguments when mixing positional and named ones #49126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2023
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 @@ -240,6 +240,10 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
foreach ($parameters as $index => $parameter) {
$this->defaultArgument->names[$index] = $parameter->name;

if (\array_key_exists($parameter->name, $arguments)) {
$arguments[$index] = $arguments[$parameter->name];
unset($arguments[$parameter->name]);
}
if (\array_key_exists($index, $arguments) && '' !== $arguments[$index]) {
continue;
}
Expand Down Expand Up @@ -341,7 +345,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a

// it's possible index 1 was set, then index 0, then 2, etc
// make sure that we re-order so they're injected as expected
ksort($arguments);
ksort($arguments, \SORT_NATURAL);

return $arguments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,17 @@ protected function processValue($value, bool $isRoot = false)
}
}

$names = [];

foreach ($reflectionMethod->getParameters() as $key => $parameter) {
$names[$key] = $parameter->name;

if (\array_key_exists($key, $arguments) && '' !== $arguments[$key]) {
continue;
}
if (\array_key_exists($parameter->name, $arguments) && '' !== $arguments[$parameter->name]) {
continue;
}

$typeHint = ProxyHelper::getTypeHint($reflectionMethod, $parameter);
$name = Target::parseName($parameter);
Expand Down Expand Up @@ -210,8 +217,15 @@ protected function processValue($value, bool $isRoot = false)
}
}

foreach ($names as $key => $name) {
if (\array_key_exists($name, $arguments) && (0 === $key || \array_key_exists($key - 1, $arguments))) {
$arguments[$key] = $arguments[$name];
unset($arguments[$name]);
}
}

if ($arguments !== $call[1]) {
ksort($arguments);
ksort($arguments, \SORT_NATURAL);
$calls[$i][1] = $arguments;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1204,4 +1204,19 @@ public function testDecorationWithServiceAndAliasedInterface()
static::assertInstanceOf(DecoratedDecorator::class, $container->get(DecoratorInterface::class));
static::assertInstanceOf(DecoratedDecorator::class, $container->get(DecoratorImpl::class));
}

public function testAutowireWithNamedArgs()
{
$container = new ContainerBuilder();

$container->register('foo', MultipleArgumentsOptionalScalar::class)
->setArguments(['foo' => 'abc'])
->setAutowired(true)
->setPublic(true);
$container->register(A::class, A::class);

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

$this->assertEquals([new TypedReference(A::class, A::class), 'abc'], $container->getDefinition('foo')->getArguments());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,24 @@ public function testBindWithTarget()

$this->assertSame('bar', (string) $container->getDefinition('with_target')->getArgument(0));
}

public function testBindWithNamedArgs()
{
$container = new ContainerBuilder();

$bindings = [
'$apiKey' => new BoundArgument('K'),
];

$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
$definition->setArguments(['c' => 'C', 'hostName' => 'H']);
$definition->setBindings($bindings);

$container->register('foo', CaseSensitiveClass::class);

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

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