Skip to content

Commit 657d1dc

Browse files
committed
[#17724] Fixing autowiring bug where if some args are set, new ones are put in the wrong spot
1 parent 6225176 commit 657d1dc

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

Compiler/AutowirePass.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,13 @@ private function completeDefinition($id, Definition $definition)
110110
$value = $parameter->getDefaultValue();
111111
}
112112

113-
if ($argumentExists) {
114-
$definition->replaceArgument($index, $value);
115-
} else {
116-
$definition->addArgument($value);
117-
}
113+
$arguments[$index] = $value;
118114
}
115+
116+
// it's possible index 1 was set, then index 0, then 2, etc
117+
// make sure that we re-order so they're injected as expected
118+
ksort($arguments);
119+
$definition->setArguments($arguments);
119120
}
120121

121122
/**

Tests/Compiler/AutowirePassTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,36 @@ public function testDontUseAbstractServices()
282282
$arguments = $container->getDefinition('bar')->getArguments();
283283
$this->assertSame('foo', (string) $arguments[0]);
284284
}
285+
286+
public function testSomeSpecificArgumentsAreSet()
287+
{
288+
$container = new ContainerBuilder();
289+
290+
$container->register('foo', __NAMESPACE__.'\Foo');
291+
$container->register('a', __NAMESPACE__.'\A');
292+
$container->register('dunglas', __NAMESPACE__.'\Dunglas');
293+
$container->register('multiple', __NAMESPACE__.'\MultipleArguments')
294+
->setAutowired(true)
295+
// set the 2nd (index 1) argument only: autowire the first and third
296+
// args are: A, Foo, Dunglas
297+
->setArguments(array(
298+
1 => new Reference('foo'),
299+
));
300+
301+
$pass = new AutowirePass();
302+
$pass->process($container);
303+
304+
$definition = $container->getDefinition('multiple');
305+
// takes advantage of Reference's __toString
306+
$this->assertEquals(
307+
array(
308+
new Reference('a'),
309+
new Reference('foo'),
310+
new Reference('dunglas'),
311+
),
312+
$definition->getArguments()
313+
);
314+
}
285315
}
286316

287317
class Foo
@@ -406,3 +436,9 @@ public function __construct(A $k)
406436
{
407437
}
408438
}
439+
class MultipleArguments
440+
{
441+
public function __construct(A $k, $foo, Dunglas $dunglas)
442+
{
443+
}
444+
}

0 commit comments

Comments
 (0)