8000 [DependencyInjection] Yaml: check if is an array before using it · symfony/symfony@1c9b5c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1c9b5c9

Browse files
dunglasnicolas-grekas
authored andcommitted
[DependencyInjection] Yaml: check if is an array before using it
1 parent 4491eb6 commit 1c9b5c9

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

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

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ private function parseDefinition($id, $service, $file, array $defaults)
332332
}
333333

334334
$tags = isset($service['tags']) ? $service['tags'] : array();
335+
if (!is_array($tags)) {
336+
throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
337+
}
335338

336339
if (!isset($defaults['tags'])) {
337340
// no-op
@@ -343,34 +346,28 @@ private function parseDefinition($id, $service, $file, array $defaults)
343346
$tags = array_merge($tags, $defaults['tags']);
344347
}
345348

346-
if (null !== $tags) {
347-
if (!is_array($tags)) {
348-
throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
349+
foreach ($tags as $tag) {
350+
if (!is_array($tag)) {
351+
$tag = array('name' => $tag);
349352
}
350353

351-
foreach ($tags as $tag) {
352-
if (!is_array($tag)) {
353-
$tag = array('name' => $tag);
354-
}
355-
356-
if (!isset($tag['name'])) {
357-
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
358-
}
359-
$name = $tag['name'];
360-
unset($tag['name']);
354+
if (!isset($tag['name'])) {
355+
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
356+
}
357+
$name = $tag['name'];
358+
unset($tag['name']);
361359

362-
if (!is_string($name) || '' === $name) {
363-
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
364-
}
360+
if (!is_string($name) || '' === $name) {
361+
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
362+
}
365363

366-
foreach ($tag as $attribute => $value) {
367-
if (!is_scalar($value) && null !== $value) {
368-
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));
369-
}
364+
foreach ($tag as $attribute => $value) {
365+
if (!is_scalar($value) && null !== $value) {
366+
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));
370367
}
371-
372-
$definition->addTag($name, $tag);
373368
}
369+
370+
$definition->addTag($name, $tag);
374371
}
375372

376373
if (isset($service['decorates'])) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
_defaults:
3+
tags: ['foo']
4+
5+
Foo\Bar:
6+
tags: invalid
7+
inherit_tags: true

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_configurator_short_syntax.yml

Lines changed: 0 additions & 1 deletion
Diff line change
Original file line numberDiff line number
@@ -1,5 +1,4 @@
11
services:
2-
32
foo_bar:
43
class: FooBarClass
54
configurator: foo_bar_configurator:configure

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,14 @@ public function testDecoratedServicesWithWrongSyntaxThrowsException()
408408
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
409409
$loader->load('bad_decorates.yml');
410410
}
411+
412+
/**
413+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
414+
* @expectedExceptionMessage Parameter "tags" must be an array for service "Foo\Bar" in services31_invalid_tags.yml. Check your YAML syntax.
415+
*/
416+
public function testInvalidTagsWithDefaults()
417+
{
418+
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
419+
$loader->load('services31_invalid_tags.yml');
420+
}
411421
}

0 commit comments

Comments
 (0)
0