Skip to content

Commit

Permalink
bug #22358 [DI] Fix named args overridding (nicolas-grekas)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Apr 10, 2017
2 parents 6d8c53e + 0c030d9 commit aada1a1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,11 @@ public static function mergeDefinition(Definition $def, ChildDefinition $definit
foreach ($definition->getArguments() as $k => $v) {
if (is_numeric($k)) {
$def->addArgument($v);
continue;
}

if (0 === strpos($k, 'index_')) {
$index = (int) substr($k, strlen('index_'));
} elseif (0 !== strpos($k, '$')) {
throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k));
} elseif (0 === strpos($k, 'index_')) {
$def->replaceArgument((int) substr($k, strlen('index_')), $v);
} else {
$def->setArgument($k, $v);
}

$def->replaceArgument($index, $v);
}

// merge properties
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/DependencyInjection/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ public function replaceArgument($index, $argument)
return $this;
}

public function setArgument($key, $value)
{
$this->arguments[$key] = $value;

return $this;
}

/**
* Gets the arguments to pass to the service constructor/factory method.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,23 @@ public function testProcessResolvesAliases()
$this->assertSame('ParentClass', $def->getClass());
}

public function testProcessSetsArguments()
{
$container = new ContainerBuilder();

$container->register('parent', 'ParentClass')->setArguments(array(0));
$container->setDefinition('child', (new ChildDefinition('parent'))->setArguments(array(
1,
'index_0' => 2,
'foo' => 3,
)));

$this->process($container);

$def = $container->getDefinition('child');
$this->assertSame(array(2, 1, 'foo' => 3), $def->getArguments());
}

protected function process(ContainerBuilder $container)
{
$pass = new ResolveDefinitionTemplatesPass();
Expand Down

0 comments on commit aada1a1

Please sign in to comment.