Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b5624cc

Browse filesBrowse files
[DependencyInjection] Fix support for named arguments on non-autowired services
1 parent 2f62c4a commit b5624cc
Copy full SHA for b5624cc

File tree

4 files changed

+40
-2
lines changed
Filter options

4 files changed

+40
-2
lines changed

‎src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ protected function processValue($value, bool $isRoot = false)
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($value, bool $isRoot = false)
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($value, bool $isRoot = false)
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($value, bool $isRoot = false)
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($value, bool $isRoot = false)
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
}

‎src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ContainerBuilder.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ private function createService(Definition $definition, array &$inlineServices, b
11021102
} else {
11031103
$r = new \ReflectionClass($parameterBag->resolveValue($definition->getClass()));
11041104

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

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

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveNamedArgumentsPassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveNamedArgumentsPassTest.php
+1-1Lines 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()

‎src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,21 @@ public function testFindTags()
17731773

17741774
$this->assertSame(['tag1', 'tag2', 'tag3'], $container->findTags());
17751775
}
1776+
1777+
public function testNamedArgument()
1778+
{
1779+
$container = new ContainerBuilder();
1780+
$container->register(E::class)
1781+
->setPublic(true)
1782+
->setArguments(['$second' => 2]);
1783+
1784+
$container->compile();
1785+
1786+
$e = $container->get(E::class);
1787+
1788+
$this->assertSame('', $e->first);
1789+
$this->assertSame(2, $e->second);
1790+
}
17761791
}
17771792

17781793
class FooClass
@@ -1801,3 +1816,15 @@ class C implements X
18011816
class D implements X
18021817
{
18031818
}
1819+
1820+
class E
1821+
{
1822+
public $first;
1823+
public $second;
1824+
1825+
public function __construct($first = '', $second = '')
1826+
{
1827+
$this->first = $first;
1828+
$this->second = $second;
1829+
}
1830+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.