Skip to content

Commit 06d64d8

Browse files
committed
Do normalization on tag options
1 parent b76ac2f commit 06d64d8

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ private function parseDefinition($id, $service, $file)
187187
continue;
188188
}
189189

190+
if (false !== strpos($name, '-') && false === strpos($name, '_') && !array_key_exists($normalizedName = str_replace('-', '_', $name), $parameters)) {
191+
$parameters[$normalizedName] = SimpleXMLElement::phpize($value);
192+
}
193+
// keep not normalized key for BC too
190194
$parameters[$name] = SimpleXMLElement::phpize($value);
191195
}
192196

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
<services>
7+
<service id="foo" class="BarClass">
8+
<tag name="foo_tag"
9+
some-option="cat"
10+
some_option="ciz"
11+
other-option="lorem"
12+
an_other-option="ipsum"
13+
/>
14+
</service>
15+
</services>
16+
</container>

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,29 @@ public function testLoadServices()
231231
$this->assertFalse($aliases['another_alias_for_foo']->isPublic());
232232
}
233233

234+
public function testParsesTags()
235+
{
236+
$container = new ContainerBuilder();
237+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
238+
$loader->load('services10.xml');
239+
240+
$services = $container->findTaggedServiceIds('foo_tag');
241+
$this->assertCount(1, $services);
242+
243+
foreach ($services as $id => $tagAttributes) {
244+
foreach ($tagAttributes as $attributes) {
245+
$this->assertArrayHasKey('other_option', $attributes);
246+
$this->assertEquals('lorem', $attributes['other_option']);
247+
$this->assertArrayHasKey('other-option', $attributes, 'unnormalized tag attributes should not be removed');
248+
249+
$this->assertEquals('ciz', $attributes['some_option'], 'no overriding should be done when normalizing');
250+
$this->assertEquals('cat', $attributes['some-option']);
251+
252+
$this->assertArrayNotHasKey('an_other_option', $attributes, 'normalization should not be done when an underscore is already found');
253+
}
254+
}
255+
}
256+
234257
public function testConvertDomElementToArray()
235258
{
236259
$doc = new \DOMDocument("1.0");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
<services>
7+
<service id="foo" class="BarClass" />
8+
</services>
9+
</container>

0 commit comments

Comments
 (0)