Skip to content

Commit

Permalink
bug #50595 [DependencyInjection] Don't ignore attributes on the actua…
Browse files Browse the repository at this point in the history
…l decorator (HypeMC)

This PR was merged into the 5.4 branch.

Discussion
----------

[DependencyInjection] Don't ignore attributes on the actual decorator

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #49931
| License       | MIT
| Doc PR        | -

An attempt to fix #49931 without reintroducing the problem reported in #30391 and fixed by #30417. The idea here is that if the attribute is declared on the decorator itself, it shouldn't be ignored as it was explicitly added.

Commits
-------

fdd2ec251e [DependencyInjection] Don't ignore attributes on the actual decorator
  • Loading branch information
nicolas-grekas committed Jul 7, 2023
2 parents 8149143 + c65b611 commit cc80120
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 4 additions & 3 deletions Compiler/ResolveInstanceofConditionalsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function processDefinition(ContainerBuilder $container, string $id, Defi
$instanceofDef->setAbstract(true)->setParent($parent ?: '.abstract.instanceof.'.$id);
$parent = '.instanceof.'.$interface.'.'.$key.'.'.$id;
$container->setDefinition($parent, $instanceofDef);
$instanceofTags[] = $instanceofDef->getTags();
$instanceofTags[] = [$interface, $instanceofDef->getTags()];
$instanceofBindings = $instanceofDef->getBindings() + $instanceofBindings;

foreach ($instanceofDef->getMethodCalls() as $methodCall) {
Expand Down Expand Up @@ -126,8 +126,9 @@ private function processDefinition(ContainerBuilder $container, string $id, Defi
// Don't add tags to service decorators
$i = \count($instanceofTags);
while (0 <= --$i) {
foreach ($instanceofTags[$i] as $k => $v) {
if (null === $definition->getDecoratedService() || \in_array($k, $tagsToKeep, true)) {
[$interface, $tags] = $instanceofTags[$i];
foreach ($tags as $k => $v) {
if (null === $definition->getDecoratedService() || $interface === $definition->getClass() || \in_array($k, $tagsToKeep, true)) {
foreach ($v as $v) {
if ($definition->hasTag($k) && \in_array($v, $definition->getTag($k))) {
continue;
Expand Down
10 changes: 9 additions & 1 deletion Tests/Compiler/ResolveInstanceofConditionalsPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,18 +318,26 @@ public function testDecoratorsAreNotAutomaticallyTagged()
$decorator->setDecoratedService('decorated');
$decorator->setInstanceofConditionals([
parent::class => (new ChildDefinition(''))->addTag('tag'),
self::class => (new ChildDefinition(''))->addTag('other-tag'),
]);
$decorator->setAutoconfigured(true);
$decorator->addTag('manual');

$container->registerForAutoconfiguration(parent::class)
->addTag('tag')
;
$container->registerForAutoconfiguration(self::class)
->addTag('last-tag')
;

(new ResolveInstanceofConditionalsPass())->process($container);
(new ResolveChildDefinitionsPass())->process($container);

$this->assertSame(['manual' => [[]]], $container->getDefinition('decorator')->getTags());
$this->assertSame([
'manual' => [[]],
'other-tag' => [[]],
'last-tag' => [[]],
], $container->getDefinition('decorator')->getTags());
}

public function testDecoratorsKeepBehaviorDescribingTags()
Expand Down

0 comments on commit cc80120

Please sign in to comment.