Skip to content

Commit b3cc037

Browse files
committed
Merge branch '2.3' into 2.7
* 2.3: fix container cache key generation [Translation] Add resources from fallback locale [DependencyInjection] enforce tags to have a name [YAML] Refine the return value of Yaml::parse()
2 parents ac507a2 + adcea6f commit b3cc037

File tree

9 files changed

+85
-1
lines changed

9 files changed

+85
-1
lines changed

Loader/XmlFileLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ private function parseDefinition(\DOMElement $service, $file)
231231
$parameters[$name] = XmlUtils::phpize($node->nodeValue);
232232
}
233233

234+
if ('' === $tag->getAttribute('name')) {
235+
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', (string) $service->getAttribute('id'), $file));
236+
}
237+
234238
$definition->addTag($tag->getAttribute('name'), $parameters);
235239
}
236240

Loader/YamlFileLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ private function parseDefinition($id, $service, $file)
270270
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
271271
}
272272

273+
if (!is_string($tag['name']) || '' === $tag['name']) {
274+
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
275+
}
276+
273277
$name = $tag['name'];
274278
unset($tag['name']);
275279

Loader/schema/dic/services/services-1.0.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
</xsd:complexType>
104104

105105
<xsd:complexType name="tag">
106-
<xsd:attribute name="name" type="xsd:string" />
106+
<xsd:attribute name="name" type="xsd:string" use="required" />
107107
<xsd:anyAttribute namespace="##any" processContents="lax" />
108108
</xsd:complexType>
109109

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 http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service id="foo" class="BarClass">
8+
<tag name="" foo="bar" />
9+
</service>
10+
</services>
11+
</container>
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 http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service id="foo" class="BarClass">
8+
<tag foo="bar" />
9+
</service>
10+
</services>
11+
</container>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
foo_service:
3+
class: FooClass
4+
tags:
5+
# tag name is an empty string
6+
- { name: '', foo: bar }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
foo_service:
3+
class: FooClass
4+
tags:
5+
# tag name is not a string
6+
- { name: [], foo: bar }

Tests/Loader/XmlFileLoaderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerInterface;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1617
use Symfony\Component\DependencyInjection\Reference;
1718
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1819
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
@@ -268,6 +269,27 @@ public function testParsesTags()
268269
}
269270
}
270271

272+
/**
273+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
274+
*/
275+
public function testParseTagsWithoutNameThrowsException()
276+
{
277+
$container = new ContainerBuilder();
278+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
279+
$loader->load('tag_without_name.xml');
280+
}
281+
282+
/**
283+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
284+
* @expectedExceptionMessageRegExp /The tag name for service ".+" in .* must be a non-empty string/
285+
*/
286+
public function testParseTagWithEmptyNameThrowsException()
287+
{
288+
$container = new ContainerBuilder();
289+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
290+
$loader->load('tag_with_empty_name.xml');
291+
}
292+
271293
public function testConvertDomElementToArray()
272294
{
273295
$doc = new \DOMDocument('1.0');

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,24 @@ public function testLoadYamlOnlyWithKeys()
277277
$this->assertEquals(array(true), $definition->getArguments());
278278
$this->assertEquals(array('manager' => array(array('alias' => 'user'))), $definition->getTags());
279279
}
280+
281+
/**
282+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
283+
* @expectedExceptionMessageRegExp /The tag name for service ".+" in .+ must be a non-empty string/
284+
*/
285+
public function testTagWithEmptyNameThrowsException()
286+
{
287+
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
288+
$loader->load('tag_name_empty_string.yml');
289+
}
290+
291+
/**
292+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
293+
* @expectedExceptionMessageREgExp /The tag name for service "\.+" must be a non-empty string/
294+
*/
295+
public function testTagWithNonStringNameThrowsException()
296+
{
297+
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
298+
$loader->load('tag_name_no_string.yml');
299+
}
280300
}

0 commit comments

Comments
 (0)