Skip to content

Commit 13bf53d

Browse files
committed
bug #27271 [DI] Allow defining bindings on ChildDefinition (nicolas-grekas)
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 ------- 1c3b1055df [DI] Allow defining bindings on ChildDefinition
2 parents 54086a1 + ed9024b commit 13bf53d

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)