Skip to content

Commit 115e50a

Browse files
Merge branch '6.0' into 6.1
* 6.0: 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 f90551b + c7492c0 commit 115e50a

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

Compiler/ResolveNamedArgumentsPass.php

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

4849
foreach ($arguments as $key => $argument) {
@@ -51,6 +52,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
5152
}
5253

5354
if (\is_int($key)) {
55+
$resolvedKeys[$key] = $key;
5456
$resolvedArguments[$key] = $argument;
5557
continue;
5658
}
@@ -71,9 +73,11 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
7173
if ($key === '$'.$p->name) {
7274
if ($p->isVariadic() && \is_array($argument)) {
7375
foreach ($argument as $variadicArgument) {
76+
$resolvedKeys[$j] = $j;
7477
$resolvedArguments[$j++] = $variadicArgument;
7578
}
7679
} else {
80+
$resolvedKeys[$j] = $p->name;
7781
$resolvedArguments[$j] = $argument;
7882
}
7983

@@ -91,6 +95,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
9195
$typeFound = false;
9296
foreach ($parameters as $j => $p) {
9397
if (!\array_key_exists($j, $resolvedArguments) && ProxyHelper::getTypeHint($r, $p, true) === $key) {
98+
$resolvedKeys[$j] = $p->name;
9499
$resolvedArguments[$j] = $argument;
95100
$typeFound = true;
96101
}
@@ -103,6 +108,12 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
103108

104109
if ($resolvedArguments !== $call[1]) {
105110
ksort($resolvedArguments);
111+
112+
if (!$value->isAutowired() && !array_is_list($resolvedArguments)) {
113+
ksort($resolvedKeys);
114+
$resolvedArguments = array_combine($resolvedKeys, $resolvedArguments);
115+
}
116+
106117
$calls[$i][1] = $resolvedArguments;
107118
}
108119
}

ContainerBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ private function createService(Definition $definition, array &$inlineServices, b
10421042
} else {
10431043
$r = new \ReflectionClass($parameterBag->resolveValue($definition->getClass()));
10441044

1045-
$service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs(array_values($arguments));
1045+
$service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments);
10461046

10471047
if (!$definition->isDeprecated() && 0 < strpos($r->getDocComment(), "\n * @deprecated ")) {
10481048
trigger_deprecation('', '', 'The "%s" service relies on the deprecated "%s" class. It should either be deprecated or its implementation upgraded.', $id, $r->name);

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
@@ -1762,6 +1762,21 @@ public function testFindTags()
17621762

17631763
$this->assertSame(['tag1', 'tag2', 'tag3'], $container->findTags());
17641764
}
1765+
1766+
public function testNamedArgument()
1767+
{
1768+
$container = new ContainerBuilder();
1769+
$container->register(E::class)
1770+
->setPublic(true)
1771+
->setArguments(['$second' => 2]);
1772+
1773+
$container->compile();
1774+
1775+
$e = $container->get(E::class);
1776+
1777+
$this->assertSame('', $e->first);
1778+
$this->assertSame(2, $e->second);
1779+
}
17651780
}
17661781

17671782
class FooClass
@@ -1790,3 +1805,15 @@ class C implements X
17901805
class D implements X
17911806
{
17921807
}
1808+
1809+
class E
1810+
{
1811+
public $first;
1812+
public $second;
1813+
1814+
public function __construct($first = '', $second = '')
1815+
{
1816+
$this->first = $first;
1817+
$this->second = $second;
1818+
}
1819+
}

0 commit comments

Comments
 (0)