Skip to content

Commit 359806e

Browse files
Merge branch '5.4' into 6.0
* 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
2 parents 34302da + 8185ed0 commit 359806e

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

Compiler/AutowirePass.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
237237
foreach ($parameters as $index => $parameter) {
238238
$this->defaultArgument->names[$index] = $parameter->name;
239239

240+
if (\array_key_exists($parameter->name, $arguments)) {
241+
$arguments[$index] = $arguments[$parameter->name];
242+
unset($arguments[$parameter->name]);
243+
}
240244
if (\array_key_exists($index, $arguments) && '' !== $arguments[$index]) {
241245
continue;
242246
}
@@ -338,7 +342,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
338342

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

343347
return $arguments;
344348
}

Compiler/ResolveBindingsPass.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,17 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
177177
}
178178
}
179179

180+
$names = [];
181+
180182
foreach ($reflectionMethod->getParameters() as $key => $parameter) {
183+
$names[$key] = $parameter->name;
184+
181185
if (\array_key_exists($key, $arguments) && '' !== $arguments[$key]) {
182186
continue;
183187
}
188+
if (\array_key_exists($parameter->name, $arguments) && '' !== $arguments[$parameter->name]) {
189+
continue;
190+
}
184191

185192
$typeHint = ProxyHelper::getTypeHint($reflectionMethod, $parameter);
186193
$name = Target::parseName($parameter);
@@ -210,8 +217,15 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
210217
}
211218
}
212219

220+
foreach ($names as $key => $name) {
221+
if (\array_key_exists($name, $arguments) && (0 === $key || \array_key_exists($key - 1, $arguments))) {
222+
$arguments[$key] = $arguments[$name];
223+
unset($arguments[$name]);
224+
}
225+
}
226+
213227
if ($arguments !== $call[1]) {
214-
ksort($arguments);
228+
ksort($arguments, \SORT_NATURAL);
215229
$calls[$i][1] = $arguments;
216230
}
217231
}

Tests/Compiler/AutowirePassTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,4 +1164,19 @@ public function testDecorationWithServiceAndAliasedInterface()
11641164
static::assertInstanceOf(DecoratedDecorator::class, $container->get(DecoratorInterface::class));
11651165
static::assertInstanceOf(DecoratedDecorator::class, $container->get(DecoratorImpl::class));
11661166
}
1167+
1168+
public function testAutowireWithNamedArgs()
1169+
{
1170+
$container = new ContainerBuilder();
1171+
1172+
$container->register('foo', MultipleArgumentsOptionalScalar::class)
1173+
->setArguments(['foo' => 'abc'])
1174+
->setAutowired(true)
1175+
->setPublic(true);
1176+
$container->register(A::class, A::class);
1177+
1178+
(new AutowirePass())->process($container);
1179+
1180+
$this->assertEquals([new TypedReference(A::class, A::class), 'abc'], $container->getDefinition('foo')->getArguments());
1181+
}
11671182
}

Tests/Compiler/ResolveBindingsPassTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,24 @@ public function testBindWithTarget()
246246

247247
$this->assertSame('bar', (string) $container->getDefinition('with_target')->getArgument(0));
248248
}
249+
250+
public function testBindWithNamedArgs()
251+
{
252+
$container = new ContainerBuilder();
253+
254+
$bindings = [
255+
'$apiKey' => new BoundArgument('K'),
256+
];
257+
258+
$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
259+
$definition->setArguments(['c' => 'C', 'hostName' => 'H']);
260+
$definition->setBindings($bindings);
261+
262+
$container->register('foo', CaseSensitiveClass::class);
263+
264+
$pass = new ResolveBindingsPass();
265+
$pass->process($container);
266+
267+
$this->assertEquals(['C', 'K', 'H'], $definition->getArguments());
268+
}
249269
}

0 commit comments

Comments
 (0)