Skip to content

Commit 7dcae05

Browse files
bug #48449 [DependencyInjection] Fix bug when tag name is a text node (BrandonlinU)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [DependencyInjection] Fix bug when tag name is a text node | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #48445 | License | MIT Following the discussion in the ticket, I added a test for a tag without name attribute and content inside the tag for the name. I use `$tag->childElementCount !== 0` instead of `$tag->hasChildNode()` to detect if a `<attribute>` tag is present in the tag definition, or is only the name of the tag. Commits ------- b9337f1768 [DependencyInjection] Fix bug when tag name is a text node
2 parents 96b1ef4 + a8b127b commit 7dcae05

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

Loader/XmlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ private function parseDefinition(\DOMElement $service, string $file, Definition
336336
$tags = $this->getChildren($service, 'tag');
337337

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

Tests/Fixtures/xml/services10.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
other-option="lorem"
1212
an_other-option="ipsum"
1313
/>
14+
<tag some-option="cat"
15+
some_option="ciz"
16+
other-option="lorem"
17+
an_other-option="ipsum">bar_tag</tag>
1418
</service>
1519
</services>
1620
</container>

Tests/Loader/XmlFileLoaderTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,17 @@ public function testParsesIteratorArgument()
369369
$this->assertEquals([new IteratorArgument(['k1' => new Reference('foo.baz'), 'k2' => new Reference('service_container')]), new IteratorArgument([])], $lazyDefinition->getArguments(), '->load() parses lazy arguments');
370370
}
371371

372-
public function testParsesTags()
372+
/**
373+
* @testWith ["foo_tag"]
374+
* ["bar_tag"]
375+
*/
376+
public function testParsesTags(string $tag)
373377
{
374378
$container = new ContainerBuilder();
375379
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
376380
$loader->load('services10.xml');
377381

378-
$services = $container->findTaggedServiceIds('foo_tag');
382+
$services = $container->findTaggedServiceIds($tag);
379383
$this->assertCount(1, $services);
380384

381385
foreach ($services as $id => $tagAttributes) {

0 commit comments

Comments
 (0)