feature #17133 [DependencyInjection] Make YamlFileLoader raise a deprecation notice if a service definition contains unsupported keywords. (hhamon)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[DependencyInjection] Make YamlFileLoader raise a deprecation notice if a service definition contains unsupported keywords.

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | ~
| License       | MIT
| Doc PR        | ~

Merry Christmas Symfony!!!

Commits
-------

bb5f118 [DependencyInjection] make YamlFileLoader raise a deprecation notice if a service definition contains unsupported keywords.
This commit is contained in:
Fabien Potencier 2016-01-07 14:30:25 +01:00
commit 960a0f4e4b
3 changed files with 66 additions and 0 deletions

View File

@ -32,6 +32,30 @@ use Symfony\Component\ExpressionLanguage\Expression;
*/ */
class YamlFileLoader extends FileLoader class YamlFileLoader extends FileLoader
{ {
private static $keywords = array(
'alias' => 'alias',
'parent' => 'parent',
'class' => 'class',
'shared' => 'shared',
'synthetic' => 'synthetic',
'lazy' => 'lazy',
'public' => 'public',
'abstract' => 'abstract',
'deprecated' => 'deprecated',
'factory' => 'factory',
'file' => 'file',
'arguments' => 'arguments',
'properties' => 'properties',
'configurator' => 'configurator',
'calls' => 'calls',
'tags' => 'tags',
'decorates' => 'decorates',
'decoration_inner_name' => 'decoration_inner_name',
'decoration_priority' => 'decoration_priority',
'autowire' => 'autowire',
'autowiring_types' => 'autowiring_types',
);
private $yamlParser; private $yamlParser;
/** /**
@ -147,6 +171,8 @@ class YamlFileLoader extends FileLoader
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)); 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));
} }
static::checkDefinition($id, $service, $file);
if (isset($service['alias'])) { if (isset($service['alias'])) {
$public = !array_key_exists('public', $service) || (bool) $service['public']; $public = !array_key_exists('public', $service) || (bool) $service['public'];
$this->container->setAlias($id, new Alias($service['alias'], $public)); $this->container->setAlias($id, new Alias($service['alias'], $public));
@ -432,4 +458,24 @@ class YamlFileLoader extends FileLoader
$this->container->loadFromExtension($namespace, $values); $this->container->loadFromExtension($namespace, $values);
} }
} }
/**
* Checks the keywords used to define a service.
*
* @param string $id The service name
* @param array $definition The service definition to check
* @param string $file The loaded YAML file
*/
private static function checkDefinition($id, array $definition, $file)
{
foreach ($definition as $key => $value) {
if (!isset(static::$keywords[$key])) {
@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);
// @deprecated Uncomment the following statement in Symfony 4.0
// and also update the corresponding unit test to make it expect
// an InvalidArgumentException exception.
//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)));
}
}
}
} }

View File

@ -0,0 +1,10 @@
services:
# This definition is valid and should not raise any deprecation notice
foo:
class: stdClass
arguments: [ 'foo', 'bar' ]
# This definition is invalid and must raise a deprecation notice
bar:
class: stdClass
private: true # the "private" keyword is invalid

View File

@ -293,4 +293,14 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($container->getDefinition('bar_service')->isAutowired()); $this->assertTrue($container->getDefinition('bar_service')->isAutowired());
} }
/**
* @group legacy
*/
public function testServiceDefinitionContainsUnsupportedKeywords()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('legacy_invalid_definition.yml');
}
} }