Skip to content

Commit ed9024b

Browse files
[DI] Allow defining bindings on ChildDefinition
1 parent 1f22dc4 commit ed9024b

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

ChildDefinition.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,6 @@ public function setInstanceofConditionals(array $instanceof)
121121
{
122122
throw new BadMethodCallException('A ChildDefinition cannot have instanceof conditionals set on it.');
123123
}
124-
125-
/**
126-
* @internal
127-
*/
128-
public function setBindings(array $bindings)
129-
{
130-
throw new BadMethodCallException('A ChildDefinition cannot have bindings set on it.');
131-
}
132124
}
133125

134126
class_alias(ChildDefinition::class, DefinitionDecorator::class);

Compiler/ResolveChildDefinitionsPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private function doResolveDefinition(ChildDefinition $definition)
103103
$def->setAutowired($parentDef->isAutowired());
104104
$def->setChanges($parentDef->getChanges());
105105

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

108108
// overwrite with values specified in the decorator
109109
$changes = $definition->getChanges();

Tests/Compiler/ResolveChildDefinitionsPassTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,27 @@ public function testProcessSetsArguments()
382382
$this->assertSame(array(2, 1, 'foo' => 3), $def->getArguments());
383383
}
384384

385+
public function testBindings()
386+
{
387+
$container = new ContainerBuilder();
388+
389+
$container->register('parent', 'stdClass')
390+
->setBindings(array('a' => '1', 'b' => '2'))
391+
;
392+
393+
$child = $container->setDefinition('child', new ChildDefinition('parent'))
394+
->setBindings(array('b' => 'B', 'c' => 'C'))
395+
;
396+
397+
$this->process($container);
398+
399+
$bindings = array();
400+
foreach ($container->getDefinition('child')->getBindings() as $k => $v) {
401+
$bindings[$k] = $v->getValues()[0];
402+
}
403+
$this->assertEquals(array('b' => 'B', 'c' => 'C', 'a' => '1'), $bindings);
404+
}
405+
385406
public function testSetAutoconfiguredOnServiceIsParent()
386407
{
387408
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)