8000 bug #22358 [DI] Fix named args overridding (nicolas-grekas) · symfony/symfony@aada1a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit aada1a1

Browse files
committed
bug #22358 [DI] Fix named args overridding (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Fix named args overridding | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22196 | License | MIT | Doc PR | - (The exception check in ResolveDefinitionTemplatesPass is not done in ResolveNamedArgumentsPass.) Commits ------- 0c030d9 [DI] Fix named args overridding
2 parents 6d8c53e + 0c030d9 commit aada1a1

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,11 @@ public static function mergeDefinition(Definition $def, ChildDefinition $definit
161161
foreach ($definition->getArguments() as $k => $v) {
162162
if (is_numeric($k)) {
163163
$def->addArgument($v);
164-
continue;
165-
}
166-
167-
if (0 === strpos($k, 'index_')) {
168-
$index = (int) substr($k, strlen('index_'));
169-
} elseif (0 !== strpos($k, '$')) {
170-
throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k));
164+
} elseif (0 === strpos($k, 'index_')) {
165+
$def->replaceArgument((int) substr($k, strlen('index_')), $v);
166+
} else {
167+
$def->setArgument($k, $v);
171168
}
172-
173-
$def->replaceArgument($index, $v);
174169
}
175170

176171
// merge properties

src/Symfony/Component/DependencyInjection/Definition.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ public function replaceArgument($index, $argument)
216216
return $this;
217217
}
218218

219+
public function setArgument($key, $value)
220+
{
221+
$this->arguments[$key] = $value;
222+
223+
return $this;
224+
}
225+
219226
/**
220227
* Gets the arguments to pass to the service constructor/factory method.
221228
*

src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveDefinitionTemplatesPassTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,23 @@ public function testProcessResolvesAliases()
364364
$this->assertSame('ParentClass', $def->getClass());
365365
}
366366

367+
public function testProcessSetsArguments()
368+
{
369+
$container = new ContainerBuilder();
370+
371+
$container->register('parent', 'ParentClass')->setArguments(array(0));
372+
$container->setDefinition('child', (new ChildDefinition('parent'))->setArguments(array(
373+
1,
374+
'index_0' => 2,
375+
'foo' => 3,
376+
)));
377+
378+
$this->process($container);
379+
380+
$def = $container->getDefinition('child');
381+
$this->assertSame(array(2, 1, 'foo' => 3), $def->getArguments());
382+
}
383+
367384
protected function process(ContainerBuilder $container)
368385
{
369386
$pass = new ResolveDefinitionTemplatesPass();

0 commit comments

Comments
 (0)
0