Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 505089b

Browse files
author
Hugo Hamon
committed
[DependencyInjection] The YamlFileLoader raises an InvalidArgumentException is a service definition contains unsupported keywords.
1 parent 03c4276 commit 505089b

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@
3232
*/
3333
class YamlFileLoader extends FileLoader
3434
{
35+
private static $keywords = array(
36+
'alias', 'parent', 'class', 'shared',
37+
'synthetic', 'lazy', 'public', 'abstract',
38+
'deprecated', 'factory', 'file', 'arguments',
39+
'properties', 'configurator', 'calls', 'tags',
40+
'decorates', 'decoration_inner_name', 'decoration_priority',
41+
'autowire', 'autowiring_types',
42+
);
43+
3544
private $yamlParser;
3645

3746
/**
@@ -147,6 +156,8 @@ private function parseDefinition($id, $service, $file)
147156
throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" but %s found for service "%s" in %s. Check your YAML syntax.', gettype($service), $id, $file));
148157
}
149158

159+
static::checkDefinition($id, $service);
160+
150161
if (isset($service['alias'])) {
151162
$public = !array_key_exists('public', $service) || (bool) $service['public'];
152163
$this->container->setAlias($id, new Alias($service['alias'], $public));
@@ -432,4 +443,26 @@ private function loadFromExtensions($content)
432443
$this->container->loadFromExtension($namespace, $values);
433444
}
434445
}
446+
447+
/**
448+
* Checks the keywords used to define a service.
449+
*
450+
* @param string $id The service name
451+
* @param array $definition The service definition to check
452+
*
453+
* @throws InvalidArgumentException When an invalid keyword is used
454+
*/
455+
private static function checkDefinition($id, array $definition)
456+
{
457+
foreach ($definition as $key => $value) {
458+
if (!in_array($key, static::$keywords)) {
459+
throw new InvalidArgumentException(sprintf(
460+
'The "%s" key in "%s" service definition is not valid. It must be one of "%s".',
461+
(string) $key,
462+
$id,
463+
implode('", "', static::$keywords)
464+
));
465+
}
466+
}
467+
}
435468
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
foo:
3+
class: stdClass
4+
arguments: [ 'foo', 'bar' ]
5+
6+
# This definition is invalid and must raise an InvalidArgumentException
7+
bar:
8+
class: stdClass
9+
private: true # the "private" keyword is invalid

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,15 @@ public function testAutowire()
293293

294294
$this->assertTrue($container->getDefinition('bar_service')->isAutowired());
295295
}
296+
297+
/**
298+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
299+
* @expectedExceptionMessageRegExp #^The "private" key in "bar" service definition is not valid\.#
300+
*/
301+
public function testInvalidDefinitionIsRejected()
302+
{
303+
$container = new ContainerBuilder();
304+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
305+
$loader->load('invalid_definition.yml');
306+
}
296307
}

0 commit comments

Comments
 (0)