8000 bug #9489 [DependencyInjection] Add normalization to tag options (Wou… · symfony/symfony@d56cc4b · GitHub
[go: up one dir, main page]

Skip to content

Commit d56cc4b

Browse files
committed
bug #9489 [DependencyInjection] Add normalization to tag options (WouterJ)
This PR was submitted for the 2.3-dev branch but it was merged into the 2.3 branch instead (closes #9489). Discussion ---------- [DependencyInjection] Add normalization to tag options Currently, when using tags in XML, the options aren't normalized. This means that the following code is wrong: <tag name="sonata_admin" manager-type="doctrine_phpcr" ... /> It should be `manager_name` to remove errors, but that's not following the XML rules. The solution is to use the same normalization as the configuration: replacing - with _. To be BC, both options (with and without normalization) are kept | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 06d64d8 Do normalization on tag options
2 parents c05232f + 06985eb commit d56cc4b

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
@@ -198,6 +198,29 @@ public function testLoadServices()
198198
$this->assertFalse($aliases['another_alias_for_foo']->isPublic());
199199
}
200200

201+
public function testParsesTags()
202+
{
203+
$container = new ContainerBuilder();
204+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
205+
$loader->load('services10.xml');
206+
207+
$services = $container->findTaggedServiceIds('foo_tag');
208+
$this->assertCount(1, $services);
209+
210+
foreach ($services as $id => $tagAttributes) {
211+
foreach ($tagAttributes as $attributes) {
212+
$this->assertArrayHasKey('other_option', $attributes);
213+
$this->assertEquals('lorem', $attributes['other_option']);
214+
$this->assertArrayHasKey('other-option', $attributes, 'unnormalized tag attributes should not be removed');
215+
216+
$this->assertEquals('ciz', $attributes['some_option'], 'no overriding should be done when normalizing');
217+
$this->assertEquals('cat', $attributes['some-option']);
218+
219+
$this->assertArrayNotHasKey('an_other_option', $attributes, 'normalization should not be done when an underscore is already found');
220+
}
221+
}
222+
}
223+
201224
public function testConvertDomElementToArray()
202225
{
203226
$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)
0