Skip to content

Commit 1f81bd0

Browse files
committed
bug #20267 [DependencyInjection] A decorated service should not keep the autowiring types (chalasr)
This PR was merged into the 2.8 branch. Discussion ---------- [DependencyInjection] A decorated service should not keep the autowiring types | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony/symfony#20260 (comment) | License | MIT | Doc PR | n/a When decorating a service which is not abstract and has `autowiring_types`, the decorator should be the one used for autowiring methods of autowired services, so we should explicitly empty them on the decorated definition after merged them into the child. See symfony/symfony#20260 (comment) for a use case where we are forced to manually empty the decorated service's `autowiring_types`. Commits ------- 5951378 A decorated service should not keep the autowiring types
2 parents 4adae54 + 4318481 commit 1f81bd0

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

Compiler/DecoratorServicePass.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ public function process(ContainerBuilder $container)
5454
} else {
5555
$decoratedDefinition = $container->getDefinition($inner);
5656
$definition->setTags($decoratedDefinition->getTags(), $definition->getTags());
57+
$definition->setAutowiringTypes(array_merge($decoratedDefinition->getAutowiringTypes(), $definition->getAutowiringTypes()));
5758
$public = $decoratedDefinition->isPublic();
5859
$decoratedDefinition->setPublic(false);
5960
$decoratedDefinition->setTags(array());
61+
$decoratedDefinition->setAutowiringTypes(array());
6062
$container->setDefinition($renamedId, $decoratedDefinition);
6163
}
6264

Tests/Compiler/DecoratorServicePassTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,27 @@ public function testProcessMovesTagsFromDecoratedDefinitionToDecoratingDefinitio
142142
$this->assertEquals(array('name' => 'bar'), $container->getDefinition('baz')->getTags());
143143
}
144144

145+
public function testProcessMergesAutowiringTypesInDecoratingDefinitionAndRemoveThemFromDecoratedDefinition()
146+
{
147+
$container = new ContainerBuilder();
148+
149+
$container
150+
->register('parent')
151+
->addAutowiringType('Bar')
152+
;
153+
154+
$container
155+
->register('child')
156+
->setDecoratedService('parent')
157+
->addAutowiringType('Foo')
158+
;
159+
160+
$this->process($container);
161+
162+
$this->assertEquals(array('Bar', 'Foo'), $container->getDefinition('child')->getAutowiringTypes());
163+
$this->assertEmpty($container->getDefinition('child.inner')->getAutowiringTypes());
164+
}
165+
145166
protected function process(ContainerBuilder $container)
146167
{
147168
$repeatedPass = new DecoratorServicePass();

Tests/Compiler/ResolveDefinitionTemplatesPassTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,11 @@ public function testProcessMergeAutowiringTypes()
360360

361361
$this->process($container);
362362

363-
$def = $container->getDefinition('child');
364-
$this->assertEquals(array('Foo', 'Bar'), $def->getAutowiringTypes());
363+
$childDef = $container->getDefinition('child');
364+
$this->assertEquals(array('Foo', 'Bar'), $childDef->getAutowiringTypes());
365+
366+
$parentDef = $container->getDefinition('parent');
367+
$this->assertSame(array('Foo'), $parentDef->getAutowiringTypes());
365368
}
366369

367370
protected function process(ContainerBuilder $container)

0 commit comments

Comments
 (0)