Skip to content

Commit be8072b

Browse files
Merge branch '6.1' into 6.2
* 6.1: Revert "minor #47721 [Notifier] Use local copy of stella-maris/clock when testing (nicolas-grekas)" [FrameworkBundle] Fix deprecation when accessing a "container.private" service from the test container [DependencyInjection] Fix support for named arguments on non-autowired services
2 parents c09a269 + 115e50a commit be8072b

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

Compiler/ResolveNamedArgumentsPass.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
4040
foreach ($calls as $i => $call) {
4141
[$method, $arguments] = $call;
4242
$parameters = null;
43+
$resolvedKeys = [];
4344
$resolvedArguments = [];
4445

4546
foreach ($arguments as $key => $argument) {
@@ -48,6 +49,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
4849
}
4950

5051
if (\is_int($key)) {
52+
$resolvedKeys[$key] = $key;
5153
$resolvedArguments[$key] = $argument;
5254
continue;
5355
}
@@ -68,9 +70,11 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
6870
if ($key === '$'.$p->name) {
6971
if ($p->isVariadic() && \is_array($argument)) {
7072
foreach ($argument as $variadicArgument) {
73+
$resolvedKeys[$j] = $j;
7174
$resolvedArguments[$j++] = $variadicArgument;
7275
}
7376
} else {
77+
$resolvedKeys[$j] = $p->name;
7478
$resolvedArguments[$j] = $argument;
7579
}
7680

@@ -88,6 +92,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
8892
$typeFound = false;
8993
foreach ($parameters as $j => $p) {
9094
if (!\array_key_exists($j, $resolvedArguments) && ProxyHelper::exportType($p, true) === $key) {
95+
$resolvedKeys[$j] = $p->name;
9196
$resolvedArguments[$j] = $argument;
9297
$typeFound = true;
9398
}
@@ -100,6 +105,12 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
100105

101106
if ($resolvedArguments !== $call[1]) {
102107
ksort($resolvedArguments);
108+
109+
if (!$value->isAutowired() && !array_is_list($resolvedArguments)) {
110+
ksort($resolvedKeys);
111+
$resolvedArguments = array_combine($resolvedKeys, $resolvedArguments);
112+
}
113+
103114
$calls[$i][1] = $resolvedArguments;
104115
}
105116
}

ContainerBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,12 +1048,12 @@ private function createService(Definition $definition, array &$inlineServices, b
10481048

10491049
if (\is_object($tryProxy)) {
10501050
if ($r->getConstructor()) {
1051-
$tryProxy->__construct(...array_values($arguments));
1051+
$tryProxy->__construct(...$arguments);
10521052
}
10531053

10541054
$service = $tryProxy;
10551055
} else {
1056-
$service = $r->getConstructor() ? $r->newInstanceArgs(array_values($arguments)) : $r->newInstance();
1056+
$service = $r->getConstructor() ? $r->newInstanceArgs($arguments) : $r->newInstance();
10571057
}
10581058

10591059
if (!$definition->isDeprecated() && 0 < strpos($r->getDocComment(), "\n * @deprecated ")) {

Tests/Compiler/ResolveNamedArgumentsPassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function testInterfaceTypedArgument()
165165
$pass = new ResolveNamedArgumentsPass();
166166
$pass->process($container);
167167

168-
$this->assertSame($expected, $definition->getArgument(3));
168+
$this->assertSame($expected, $definition->getArgument('container'));
169169
}
170170

171171
public function testResolvesMultipleArgumentsOfTheSameType()

Tests/ContainerBuilderTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,21 @@ public function testFindTags()
18291829

18301830
$this->assertSame(['tag1', 'tag2', 'tag3'], $container->findTags());
18311831
}
1832+
1833+
public function testNamedArgument()
1834+
{
1835+
$container = new ContainerBuilder();
1836+
$container->register(E::class)
1837+
->setPublic(true)
1838+
->setArguments(['$second' => 2]);
1839+
1840+
$container->compile();
1841+
1842+
$e = $container->get(E::class);
1843+
1844+
$this->assertSame('', $e->first);
1845+
$this->assertSame(2, $e->second);
1846+
}
18321847
}
18331848

18341849
class FooClass
@@ -1857,3 +1872,15 @@ class C implements X
18571872
class D implements X
18581873
{
18591874
}
1875+
1876+
class E
1877+
{
1878+
public $first;
1879+
public $second;
1880+
1881+
public function __construct($first = '', $second = '')
1882+
{
1883+
$this->first = $first;
1884+
$this->second = $second;
1885+
}
1886+
}

0 commit comments

Comments
 (0)