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

Skip to content

Commit bb5f118

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

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

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

Lines changed: 46 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, $file);
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,24 @@ 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+
* @param string $file The loaded YAML file
468+
*/
469+
private static function checkDefinition($id, array $definition, $file)
470+
{
471+
foreach ($definition as $key => $value) {
472+
if (!isset(static::$keywords[$key])) {
473+
@trigger_error(sprintf('The configuration key "%s" is unsupported for service definition "%s" in "%s". Allowed configuration keys are "%s". The YamlFileLoader object will raise an exception instead in Symfony 4.0 when detecting an unsupported service configuration key.', $key, $id, $file, implode('", "', static::$keywords)), E_USER_DEPRECATED);
474+
// @deprecated Uncomment the following statement in Symfony 4.0
475+
// and also update the corresponding unit test to make it expect
476+
// an InvalidArgumentException exception.
477+
//throw new InvalidArgumentException(sprintf('The configuration key "%s" is unsupported for service definition "%s" in "%s". Allowed configuration keys are "%s".', $key, $id, $file, implode('", "', static::$keywords)));
478+
}
479+
}
480+
}
435481
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
# This definition is valid and should not raise any deprecation notice
3+
foo:
4+
class: stdClass
5+
arguments: [ 'foo', 'bar' ]
6+
7+
# This definition is invalid and must raise a deprecation notice
8+
bar:
9+
class: stdClass
10+
private: true # the "private" keyword is invalid

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

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

294294
$this->assertTrue($container->getDefinition('bar_service')->isAutowired());
295295
}
296+
297+
/**
298+
* @group legacy
299+
*/
300+
public function testServiceDefinitionContainsUnsupportedKeywords()
301+
{
302+
$container = new ContainerBuilder();
303+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
304+
$loader->load('legacy_invalid_definition.yml');
305+
}
296306
}

0 commit comments

Comments
 (0)