Skip to content

Commit

Permalink
bug #27271 [DI] Allow defining bindings on ChildDefinition (nicolas-g…
Browse files Browse the repository at this point in the history
…rekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Allow defining bindings on ChildDefinition

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Spotted by @stof while trying to put symfony/monolog-bundle#254 into practice.
Binding log channels doesn't work because we put this artificial restriction in place.
Let's allow ChildDefinition to have bindings (but only at the DI extension level, loaders still forbid defining them at their level because of the parent vs _defaults ambiguity.)

Commits
-------

1c3b105 [DI] Allow defining bindings on ChildDefinition
  • Loading branch information
fabpot committed May 17, 2018
2 parents c18813d + 1c3b105 commit c280f8a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
8 changes: 0 additions & 8 deletions src/Symfony/Component/DependencyInjection/ChildDefinition.php
Expand Up @@ -121,14 +121,6 @@ public function setInstanceofConditionals(array $instanceof)
{
throw new BadMethodCallException('A ChildDefinition cannot have instanceof conditionals set on it.');
}

/**
* @internal
*/
public function setBindings(array $bindings)
{
throw new BadMethodCallException('A ChildDefinition cannot have bindings set on it.');
}
}

class_alias(ChildDefinition::class, DefinitionDecorator::class);
Expand Up @@ -103,7 +103,7 @@ private function doResolveDefinition(ChildDefinition $definition)
$def->setAutowired($parentDef->isAutowired());
$def->setChanges($parentDef->getChanges());

$def->setBindings($parentDef->getBindings());
$def->setBindings($definition->getBindings() + $parentDef->getBindings());

// overwrite with values specified in the decorator
$changes = $definition->getChanges();
Expand Down
Expand Up @@ -382,6 +382,27 @@ public function testProcessSetsArguments()
$this->assertSame(array(2, 1, 'foo' => 3), $def->getArguments());
}

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

$container->register('parent', 'stdClass')
->setBindings(array('a' => '1', 'b' => '2'))
;

$child = $container->setDefinition('child', new ChildDefinition('parent'))
->setBindings(array('b' => 'B', 'c' => 'C'))
;

$this->process($container);

$bindings = array();
foreach ($container->getDefinition('child')->getBindings() as $k => $v) {
$bindings[$k] = $v->getValues()[0];
}
$this->assertEquals(array('b' => 'B', 'c' => 'C', 'a' => '1'), $bindings);
}

public function testSetAutoconfiguredOnServiceIsParent()
{
$container = new ContainerBuilder();
Expand Down

0 comments on commit c280f8a

Please sign in to comment.