Skip to content

Commit

Permalink
[DependencyInjection] fix XmlDumper when a tag contains also a 'name'…
Browse files Browse the repository at this point in the history
… property
  • Loading branch information
lyrixx committed Mar 13, 2024
1 parent a0721fd commit 0d38842
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php
Expand Up @@ -134,16 +134,17 @@ private function addService(Definition $definition, ?string $id, \DOMElement $pa
foreach ($tags as $name => $tags) {
foreach ($tags as $attributes) {
$tag = $this->document->createElement('tag');
if (!\array_key_exists('name', $attributes)) {
$tag->setAttribute('name', $name);
} else {
$tag->appendChild($this->document->createTextNode($name));
}

// Check if we have recursive attributes
if (array_filter($attributes, \is_array(...))) {
$tag->setAttribute('name', $name);
$this->addTagRecursiveAttributes($tag, $attributes);
} else {
if (!\array_key_exists('name', $attributes)) {
$tag->setAttribute('name', $name);
} else {
$tag->appendChild($this->document->createTextNode($name));
}
foreach ($attributes as $key => $value) {
$tag->setAttribute($key, $value ?? '');
}
Expand Down
Expand Up @@ -10,6 +10,7 @@
$container
->register('foo', FooClass::class)
->addTag('foo_tag', [
'name' => 'attributeName',
'foo' => 'bar',
'bar' => [
'foo' => 'bar',
Expand Down
Expand Up @@ -4,6 +4,7 @@
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="true" synthetic="true"/>
<service id="foo" class="Bar\FooClass">
<tag name="foo_tag">
<attribute name="name">attributeName</attribute>
<attribute name="foo">bar</attribute>
<attribute name="bar">
<attribute name="foo">bar</attribute>
Expand Down
Expand Up @@ -7,4 +7,4 @@ services:
foo:
class: Bar\FooClass
tags:
- foo_tag: { foo: bar, bar: { foo: bar, bar: foo } }
- foo_tag: { name: attributeName, foo: bar, bar: { foo: bar, bar: foo } }
Expand Up @@ -460,7 +460,7 @@ public function testParseServiceTagsWithArrayAttributes()
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services_with_array_tags.xml');

$this->assertEquals(['foo_tag' => [['foo' => 'bar', 'bar' => ['foo' => 'bar', 'bar' => 'foo']]]], $container->getDefinition('foo')->getTags());
$this->assertEquals(['foo_tag' => [['name' => 'attributeName', 'foo' => 'bar', 'bar' => ['foo' => 'bar', 'bar' => 'foo']]]], $container->getDefinition('foo')->getTags());
}

public function testParseTagsWithoutNameThrowsException()
Expand Down

0 comments on commit 0d38842

Please sign in to comment.