10000 bug #50088 [DependencyInjection] Do not ignore tags `name` attribute … · symfony/symfony@6f77fa0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f77fa0

Browse files
bug #50088 [DependencyInjection] Do not ignore tags name attribute when it does not define their name (MatTheCat)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [DependencyInjection] Do not ignore tags `name` attribute when it does not define their name | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #50081 | License | MIT | Doc PR | N/A Tags `name` attribute is ignored using XML if the tag name is its node content. That means `<tag name="name_attribute">tag_name</tag>` will return a `tag_name` tag without any attribute. This seems to be a regression from #36586. Commits ------- 4c9c688 [DependencyInjection] Do not ignore tags `name` attribute when it does not define their name
2 parents ec43ae8 + 4c9c688 commit 6f77fa0

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,14 @@ private function parseDefinition(\DOMElement $service, string $file, Definition
336336
$tags = $this->getChildren($service, 'tag');
337337

338338
foreach ($tags as $tag) {
339-
if ('' === $tagName = $tag->childElementCount || '' === $tag->nodeValue ? $tag->getAttribute('name') : $tag->nodeValue) {
339+
$tagNameComesFromAttribute = $tag->childElementCount || '' === $tag->nodeValue;
340+
if ('' === $tagName = $tagNameComesFromAttribute ? $tag->getAttribute('name') : $tag->nodeValue) {
340341
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in "%s" must be a non-empty string.', (string) $service->getAttribute('id'), $file));
341342
}
342343

343344
$parameters = $this->getTagAttributes($tag, sprintf('The attribute name of tag "%s" for service "%s" in %s must be a non-empty string.', $tagName, (string) $service->getAttribute('id'), $file));
344345
foreach ($tag->attributes as $name => $node) {
345-
if ('name' === $name) {
346+
if ($tagNameComesFromAttribute && 'name' === $name) {
346347
continue;
347348
}
348349

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service id="foo" class="BarClass">
8+
<tag name="name_attribute">tag_name</tag>
9+
</service>
10+
</services>
11+
</container>

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,4 +1194,14 @@ public static function dataForBindingsAndInnerCollections()
11941194
['bar8', new IteratorArgument(['item.1', 'item.2', ['item.3.1', 'item.3.2']])],
11951195
];
11961196
}
1197+
1198+
public function testTagNameAttribute()
1199+
{
1200+
$container = new ContainerBuilder();
1201+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
1202+
$loader->load('tag_with_name_attribute.xml');
1203+
1204+
$definition = $container->getDefinition('foo');
1205+
$this->assertSame([['name' => 'name_attribute']], $definition->getTag('tag_name'));
1206+
}
11971207
}

0 commit comments

Comments
 (0)
0