Skip to content

Commit 986a633

Browse files
committed
bug #23218 [DI] Dedup tags when using instanceof/autoconfigure (ogizanagi)
This PR was merged into the 3.3 branch. Discussion ---------- [DI] Dedup tags when using instanceof/autoconfigure | Q | A | ------------- | --- | Branch? | 3.3 <!-- see comment below --> | Bug fix? | yes | New feature? | no <!-- don't forget updating src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes, failures unrelated (symfony/symfony@8dc00bb) | Fixed tickets | N/A <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | N/A This fixes uselessly duplicated tags shown when using the `debug:container` command, or in the dumped container in xml format: <img width="554" alt="screenshot 2017-06-17 a 20 41 27" src="https://user-images.githubusercontent.com/2211145/27255375-de79fc4e-539d-11e7-98d9-c10074ddcffb.PNG"> <img width="494" alt="screenshot 2017-06-17 a 20 41 54" src="https://user-images.githubusercontent.com/2211145/27255376-de7ff5b8-539d-11e7-97ae-ccd31b1d5254.PNG"> <img width="1371" alt="screenshot 2017-06-17 a 20 42 33" src="https://user-images.githubusercontent.com/2211145/27255377-de869ba2-539d-11e7-8cd7-6005f8a499d6.PNG"> (duplicates here are explained by the twig namespaced and unnamespaced versions, and the controllers being tagged explicitly in https://github.com/symfony/symfony-demo/blob/master/app/config/services.yml#L25, while also being tagged by autoconfiguration ([which is expected](symfony/symfony-docs#7921 (comment)))) Commits ------- 9f877efb39 [DI] Dedup tags when using instanceof/autoconfigure
2 parents a96d1c2 + baffd45 commit 986a633

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Compiler/ResolveInstanceofConditionalsPass.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
107107
while (0 <= --$i) {
108108
foreach ($instanceofTags[$i] as $k => $v) {
109109
foreach ($v as $v) {
110+
if ($definition->hasTag($k) && in_array($v, $definition->getTag($k))) {
111+
continue;
112+
}
110113
$definition->addTag($k, $v);
111114
}
112115
}

Tests/Compiler/ResolveInstanceofConditionalsPassTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,29 @@ public function testProcessUsesAutoconfiguredInstanceof()
130130
$this->assertSame(array('local_instanceof_tag' => array(array()), 'autoconfigured_tag' => array(array())), $def->getTags());
131131
}
132132

133+
public function testAutoconfigureInstanceofDoesNotDuplicateTags()
134+
{
135+
$container = new ContainerBuilder();
136+
$def = $container->register('normal_service', self::class);
137+
$def
138+
->addTag('duplicated_tag')
139+
->addTag('duplicated_tag', array('and_attributes' => 1))
140+
;
141+
$def->setInstanceofConditionals(array(
142+
parent::class => (new ChildDefinition(''))->addTag('duplicated_tag'),
143+
));
144+
$def->setAutoconfigured(true);
145+
$container->registerForAutoconfiguration(parent::class)
146+
->addTag('duplicated_tag', array('and_attributes' => 1))
147+
;
148+
149+
(new ResolveInstanceofConditionalsPass())->process($container);
150+
(new ResolveDefinitionTemplatesPass())->process($container);
151+
152+
$def = $container->getDefinition('normal_service');
153+
$this->assertSame(array('duplicated_tag' => array(array(), array('and_attributes' => 1))), $def->getTags());
154+
}
155+
133156
public function testProcessDoesNotUseAutoconfiguredInstanceofIfNotEnabled()
134157
{
135158
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)