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

Skip to content

Commit 898e9b8

Browse files
author
Hugo Hamon
committed
[DependencyInjection] make YamlFileLoader raise a deprecation notice if a service definition contains unsupported keywords.
1 parent 586c58a commit 898e9b8

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@
3232
*/
3333
class YamlFileLoader extends FileLoader
3434
{
35+
private static $keywords = array(
36+
'alias' => 'alias',
37+
'parent' => 'parent',
38+
'class' => 'class',
39+
'shared' => 'shared',
40+
'synthetic' => 'synthetic',
41+
'lazy' => 'lazy',
42+
'public' => 'public',
43+
'abstract' => 'abstract',
44+
'deprecated' => 'deprecated',
45+
'factory' => 'factory',
46+
'file' => 'file',
47+
'arguments' => 'arguments',
48+
'properties' => 'properties',
49+
'configurator' => 'configurator',
50+
'calls' => 'calls',
51+
'tags' => 'tags',
52+
'decorates' => 'decorates',
53+
'decoration_inner_name' => 'decoration_inner_name',
54+
'decoration_priority' => 'decoration_priority',
55+
'autowire' => 'autowire',
56+
'autowiring_types' => 'autowiring_types',
57+
);
58+
3559
private $yamlParser;
3660

3761
/**
@@ -147,6 +171,8 @@ private function parseDefinition($id, $service, $file)
147171
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));
148172
}
149173

174+
static::checkDefinition($id, $service);
175+
150176
if (isset($service['alias'])) {
151177
$public = !array_key_exists('public', $service) || (bool) $service['public'];
152178
$this->container->setAlias($id, new Alias($service['alias'], $public));
@@ -432,4 +458,21 @@ private function loadFromExtensions($content)
432458
$this->container->loadFromExtension($namespace, $values);
433459
}
434460
}
461+
462+
/**
463+
* Checks the keywords used to define a service.
464+
*
465+
* @param string $id The service name
466+
* @param array $definition The service definition to check
467+
*
468+
* @throws InvalidArgumentException When an invalid keyword is used
469+
*/
470+
private static function checkDefinition($id, array $definition)
471+
{
472+
foreach ($definition as $key => $value) {
473+
if (!isset(static::$keywords[$key])) {
474+
@trigger_error(sprintf('The "%s" key in "%s" service definition is not supported. It must be one of "%s".', $key, $id, implode('", "', static::$keywords)), E_USER_DEPRECATED);
475+
}
476+
}
477+
}
435478
}
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,11 @@ public function testAutowire()
293293

294294
$this->assertTrue($container->getDefinition('bar_service')->isAutowired());
295295
}
296+
297+
public function testLegacyInvalidDefinitionIsRejected()
298+
{
299+
$container = new ContainerBuilder();
300+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
301+
$loader->load('legacy_invalid_definition.yml');
302+
}
296303
}

0 commit comments

Comments
 (0)