[DI] Allow defining bindings on ChildDefinition

This commit is contained in:
Nicolas Grekas 2018-05-15 09:06:31 +02:00
parent 4c38b4dfa6
commit 1c3b1055df
3 changed files with 22 additions and 9 deletions

View File

@ -121,14 +121,6 @@ class ChildDefinition extends Definition
{
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);

View File

@ -103,7 +103,7 @@ class ResolveChildDefinitionsPass extends AbstractRecursivePass
$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();

View File

@ -382,6 +382,27 @@ class ResolveChildDefinitionsPassTest extends TestCase
$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();