Skip to content

Commit

Permalink
bug #23218 [DI] Dedup tags when using instanceof/autoconfigure (ogiza…
Browse files Browse the repository at this point in the history
…nagi)

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 (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
-------

9f877ef [DI] Dedup tags when using instanceof/autoconfigure
  • Loading branch information
fabpot committed Jun 20, 2017
2 parents 9a276aa + 9f877ef commit 035d526
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Expand Up @@ -107,6 +107,9 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
while (0 <= --$i) {
foreach ($instanceofTags[$i] as $k => $v) {
foreach ($v as $v) {
if ($definition->hasTag($k) && in_array($v, $definition->getTag($k))) {
continue;
}
$definition->addTag($k, $v);
}
}
Expand Down
Expand Up @@ -130,6 +130,29 @@ public function testProcessUsesAutoconfiguredInstanceof()
$this->assertSame(array('local_instanceof_tag' => array(array()), 'autoconfigured_tag' => array(array())), $def->getTags());
}

public function testAutoconfigureInstanceofDoesNotDuplicateTags()
{
$container = new ContainerBuilder();
$def = $container->register('normal_service', self::class);
$def
->addTag('duplicated_tag')
->addTag('duplicated_tag', array('and_attributes' => 1))
;
$def->setInstanceofConditionals(array(
parent::class => (new ChildDefinition(''))->addTag('duplicated_tag'),
));
$def->setAutoconfigured(true);
$container->registerForAutoconfiguration(parent::class)
->addTag('duplicated_tag', array('and_attributes' => 1))
;

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

$def = $container->getDefinition('normal_service');
$this->assertSame(array('duplicated_tag' => array(array(), array('and_attributes' => 1))), $def->getTags());
}

public function testProcessDoesNotUseAutoconfiguredInstanceofIfNotEnabled()
{
$container = new ContainerBuilder();
Expand Down

0 comments on commit 035d526

Please sign in to comment.