Skip to content

Commit 4b1d87f

Browse files
committed
[DependencyInjection] Fixed decoration of service for service with parent
1 parent d244c4e commit 4b1d87f

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

Compiler/ResolveDefinitionTemplatesPass.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ private function resolveDefinition($id, DefinitionDecorator $definition)
119119
if (isset($changes['lazy'])) {
120120
$def->setLazy($definition->isLazy());
121121
}
122+
if (isset($changes['decorated_service'])) {
123+
$decoratedService = $definition->getDecoratedService();
124+
if (null === $decoratedService) {
125+
$def->setDecoratedService($decoratedService);
126+
} else {
127+
$def->setDecoratedService($decoratedService[0], $decoratedService[1]);
128+
}
129+
}
122130

123131
// merge arguments
124132
foreach ($definition->getArguments() as $k => $v) {

DefinitionDecorator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ public function setLazy($boolean)
170170
return parent::setLazy($boolean);
171171
}
172172

173+
/**
174+
* {@inheritdoc}
175+
*/
176+
public function setDecoratedService($id, $renamedId = null)
177+
{
178+
$this->changes['decorated_service'] = true;
179+
180+
return parent::setDecoratedService($id, $renamedId);
181+
}
182+
173183
/**
174184
* Gets an argument to pass to the service constructor/factory method.
175185
*

Tests/Compiler/ResolveDefinitionTemplatesPassTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ public function testProcessDoesNotCopyTags()
117117
$this->assertEquals(array(), $def->getTags());
118118
}
119119

120+
public function testProcessDoesNotCopyDecoratedService()
121+
{
122+
$container = new ContainerBuilder();
123+
124+
$container
125+
->register('parent')
126+
->setDecoratedService('foo')
127+
;
128+
129+
$container
130+
->setDefinition('child', new DefinitionDecorator('parent'))
131+
;
132+
133+
$this->process($container);
134+
135+
$def = $container->getDefinition('child');
136+
$this->assertNull($def->getDecoratedService());
137+
}
138+
120139
public function testProcessHandlesMultipleInheritance()
121140
{
122141
$container = new ContainerBuilder();
@@ -173,6 +192,21 @@ public function testSetLazyOnServiceIsParent()
173192
$this->assertTrue($container->getDefinition('child1')->isLazy());
174193
}
175194

195+
public function testSetDecoratedServiceOnServiceHasParent()
196+
{
197+
$container = new ContainerBuilder();
198+
199+
$container->register('parent', 'stdClass');
200+
201+
$container->setDefinition('child1', new DefinitionDecorator('parent'))
202+
->setDecoratedService('foo', 'foo_inner')
203+
;
204+
205+
$this->process($container);
206+
207+
$this->assertEquals(array('foo', 'foo_inner'), $container->getDefinition('child1')->getDecoratedService());
208+
}
209+
176210
protected function process(ContainerBuilder $container)
177211
{
178212
$pass = new ResolveDefinitionTemplatesPass();

0 commit comments

Comments
 (0)