minor #21348 [DependencyInjection] Yaml: check if $tags is an array before using it (dunglas)

This PR was squashed before being merged into the 3.3-dev branch (closes #21348).

Discussion
----------

[DependencyInjection] Yaml: check if $tags is an array before using it

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Commits
-------

1c9b5c9 [DependencyInjection] Yaml: check if  is an array before using it
This commit is contained in:
Nicolas Grekas 2017-01-24 13:43:57 +01:00
commit 29b6e3215e
4 changed files with 39 additions and 26 deletions

View File

@ -352,6 +352,9 @@ class YamlFileLoader extends FileLoader
}
$tags = isset($service['tags']) ? $service['tags'] : array();
if (!is_array($tags)) {
throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
}
if (!isset($defaults['tags'])) {
// no-op
@ -363,34 +366,28 @@ class YamlFileLoader extends FileLoader
$tags = array_merge($tags, $defaults['tags']);
}
if (null !== $tags) {
if (!is_array($tags)) {
throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
foreach ($tags as $tag) {
if (!is_array($tag)) {
$tag = array('name' => $tag);
}
foreach ($tags as $tag) {
if (!is_array($tag)) {
$tag = array('name' => $tag);
}
if (!isset($tag['name'])) {
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
}
$name = $tag['name'];
unset($tag['name']);
if (!is_string($name) || '' === $name) {
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
}
foreach ($tag as $attribute => $value) {
if (!is_scalar($value) && null !== $value) {
throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your YAML syntax.', $id, $name, $attribute, $file));
}
}
$definition->addTag($name, $tag);
if (!isset($tag['name'])) {
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
}
$name = $tag['name'];
unset($tag['name']);
if (!is_string($name) || '' === $name) {
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
}
foreach ($tag as $attribute => $value) {
if (!is_scalar($value) && null !== $value) {
throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your YAML syntax.', $id, $name, $attribute, $file));
}
}
$definition->addTag($name, $tag);
}
if (isset($service['decorates'])) {

View File

@ -0,0 +1,7 @@
services:
_defaults:
tags: ['foo']
Foo\Bar:
tags: invalid
inherit_tags: true

View File

@ -1,5 +1,4 @@
services:
foo_bar:
class: FooBarClass
configurator: foo_bar_configurator:configure

View File

@ -414,4 +414,14 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('bad_decorates.yml');
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Parameter "tags" must be an array for service "Foo\Bar" in services31_invalid_tags.yml. Check your YAML syntax.
*/
public function testInvalidTagsWithDefaults()
{
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('services31_invalid_tags.yml');
}
}