bug #28820 [DependencyInjection] Fix tags on multiple decorated service (Soner Sayakci)
This PR was merged into the 3.4 branch.
Discussion
----------
[DependencyInjection] Fix tags on multiple decorated service
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #28682
| License | MIT
| Doc PR |
After the first run in the loop with the same decorated service it goes only in the hasAlias condition. The tags will be not set here, so the `ResolveTaggedIteratorArgumentPass` will handle only the first decoration.
Commits
-------
90f8df2830
[DependencyInjection] Fix tags on multiple decorated service
This commit is contained in:
commit
5d1120593c
@ -34,6 +34,7 @@ class DecoratorServicePass implements CompilerPassInterface
|
|||||||
}
|
}
|
||||||
$definitions->insert(array($id, $definition), array($decorated[2], --$order));
|
$definitions->insert(array($id, $definition), array($decorated[2], --$order));
|
||||||
}
|
}
|
||||||
|
$decoratingDefinitions = array();
|
||||||
|
|
||||||
foreach ($definitions as list($id, $definition)) {
|
foreach ($definitions as list($id, $definition)) {
|
||||||
list($inner, $renamedId) = $definition->getDecoratedService();
|
list($inner, $renamedId) = $definition->getDecoratedService();
|
||||||
@ -53,18 +54,25 @@ class DecoratorServicePass implements CompilerPassInterface
|
|||||||
$container->setAlias($renamedId, new Alias($container->normalizeId($alias), false));
|
$container->setAlias($renamedId, new Alias($container->normalizeId($alias), false));
|
||||||
} else {
|
} else {
|
||||||
$decoratedDefinition = $container->getDefinition($inner);
|
$decoratedDefinition = $container->getDefinition($inner);
|
||||||
$definition->setTags(array_merge($decoratedDefinition->getTags(), $definition->getTags()));
|
|
||||||
if ($types = array_merge($decoratedDefinition->getAutowiringTypes(false), $definition->getAutowiringTypes(false))) {
|
|
||||||
$definition->setAutowiringTypes($types);
|
|
||||||
}
|
|
||||||
$public = $decoratedDefinition->isPublic();
|
$public = $decoratedDefinition->isPublic();
|
||||||
$private = $decoratedDefinition->isPrivate();
|
$private = $decoratedDefinition->isPrivate();
|
||||||
$decoratedDefinition->setPublic(false);
|
$decoratedDefinition->setPublic(false);
|
||||||
$decoratedDefinition->setTags(array());
|
|
||||||
if ($decoratedDefinition->getAutowiringTypes(false)) {
|
|
||||||
$decoratedDefinition->setAutowiringTypes(array());
|
|
||||||
}
|
|
||||||
$container->setDefinition($renamedId, $decoratedDefinition);
|
$container->setDefinition($renamedId, $decoratedDefinition);
|
||||||
|
$decoratingDefinitions[$inner] = $decoratedDefinition;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($decoratingDefinitions[$inner])) {
|
||||||
|
$decoratingDefinition = $decoratingDefinitions[$inner];
|
||||||
|
$definition->setTags(array_merge($decoratingDefinition->getTags(), $definition->getTags()));
|
||||||
|
$autowiringTypes = $decoratingDefinition->getAutowiringTypes(false);
|
||||||
|
if ($types = array_merge($autowiringTypes, $definition->getAutowiringTypes(false))) {
|
||||||
|
$definition->setAutowiringTypes($types);
|
||||||
|
}
|
||||||
|
$decoratingDefinition->setTags(array());
|
||||||
|
if ($autowiringTypes) {
|
||||||
|
$decoratingDefinition->setAutowiringTypes(array());
|
||||||
|
}
|
||||||
|
$decoratingDefinitions[$inner] = $definition;
|
||||||
}
|
}
|
||||||
|
|
||||||
$container->setAlias($inner, $id)->setPublic($public)->setPrivate($private);
|
$container->setAlias($inner, $id)->setPublic($public)->setPrivate($private);
|
||||||
|
@ -168,6 +168,29 @@ class DecoratorServicePassTest extends TestCase
|
|||||||
$this->assertEmpty($container->getDefinition('child.inner')->getAutowiringTypes());
|
$this->assertEmpty($container->getDefinition('child.inner')->getAutowiringTypes());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testProcessMovesTagsFromDecoratedDefinitionToDecoratingDefinitionMultipleTimes()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
$container
|
||||||
|
->register('foo')
|
||||||
|
->setPublic(true)
|
||||||
|
->setTags(array('bar' => array('attr' => 'baz')))
|
||||||
|
;
|
||||||
|
$container
|
||||||
|
->register('deco1')
|
||||||
|
->setDecoratedService('foo', null, 50)
|
||||||
|
;
|
||||||
|
$container
|
||||||
|
->register('deco2')
|
||||||
|
->setDecoratedService('foo', null, 2)
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$this->assertEmpty($container->getDefinition('deco1')->getTags());
|
||||||
|
$this->assertEquals(array('bar' => array('attr' => 'baz')), $container->getDefinition('deco2')->getTags());
|
||||||
|
}
|
||||||
|
|
||||||
protected function process(ContainerBuilder $container)
|
protected function process(ContainerBuilder $container)
|
||||||
{
|
{
|
||||||
$repeatedPass = new DecoratorServicePass();
|
$repeatedPass = new DecoratorServicePass();
|
||||||
|
Reference in New Issue
Block a user