Skip to content

Commit

Permalink
Merge branch '5.4' into 6.0
Browse files Browse the repository at this point in the history
* 5.4:
  [HttpFoundation] Fix bad return type in IpUtils::checkIp4()
  [DependencyInjection] Fix order of arguments when mixing positional and named ones
  [HttpClient] Fix collecting data non-late for the profiler
  [Security/Http] Fix compat of persistent remember-me with legacy tokens
  Bump Symfony version to 5.4.20
  Update VERSION for 5.4.19
  Update CONTRIBUTORS for 5.4.19
  Update CHANGELOG for 5.4.19
  [Security/Http] Remove CSRF tokens from storage on successful login
  [HttpKernel] Remove private headers before storing responses with HttpCache
  • Loading branch information
nicolas-grekas committed Jan 30, 2023
2 parents 34302da + 8185ed0 commit 359806e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Compiler/AutowirePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,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 @@ -338,7 +342,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
16 changes: 15 additions & 1 deletion Compiler/ResolveBindingsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,17 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
}
}

$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(mixed $value, bool $isRoot = false): mixed
}
}

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
15 changes: 15 additions & 0 deletions Tests/Compiler/AutowirePassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1164,4 +1164,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());
}
}
20 changes: 20 additions & 0 deletions Tests/Compiler/ResolveBindingsPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,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 comments on commit 359806e

Please sign in to comment.