From 854efce7cacfa299a1f09a9878c2d845b02703cb Mon Sep 17 00:00:00 2001 From: Hugo Hamon Date: Thu, 18 May 2017 10:14:39 +0200 Subject: [PATCH] [DependencyInjection] remove deprecated YAML configuration features and syntax --- .../Loader/YamlFileLoader.php | 18 ++++--------- .../Tests/Fixtures/yaml/bad_alias.yml | 11 ++++++++ ...invalid_definition.yml => bad_keyword.yml} | 0 .../Tests/Loader/YamlFileLoaderTest.php | 26 +++++++++++++++++-- 4 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/bad_alias.yml rename src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/{legacy_invalid_definition.yml => bad_keyword.yml} (100%) diff --git a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php index c3e697c1b5..19d919f2ed 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php @@ -315,8 +315,9 @@ class YamlFileLoader extends FileLoader private function parseDefinition($id, $service, $file, array $defaults) { if (preg_match('/^_[a-zA-Z0-9_]*$/', $id)) { - @trigger_error(sprintf('Service names that start with an underscore are deprecated since Symfony 3.3 and will be reserved in 4.0. Rename the "%s" service or define it in XML instead.', $id), E_USER_DEPRECATED); + throw new InvalidArgumentException(sprintf('Service names that start with an underscore are reserved. Rename the "%s" service or define it in XML instead.', $id)); } + if (is_string($service) && 0 === strpos($service, '@')) { $public = isset($defaults['public']) ? $defaults['public'] : true; $this->container->setAlias($id, new Alias(substr($service, 1), $public)); @@ -344,7 +345,7 @@ class YamlFileLoader extends FileLoader foreach ($service as $key => $value) { if (!in_array($key, array('alias', 'public'))) { - @trigger_error(sprintf('The configuration key "%s" is unsupported for the service "%s" which is defined as an alias in "%s". Allowed configuration keys for service aliases are "alias" and "public". The YamlFileLoader will raise an exception in Symfony 4.0, instead of silently ignoring unsupported attributes.', $key, $id, $file), E_USER_DEPRECATED); + throw new InvalidArgumentException(sprintf('The configuration key "%s" is unsupported for the service "%s" which is defined as an alias in "%s". Allowed configuration keys for service aliases are "alias" and "public".', $key, $id, $file)); } } @@ -740,11 +741,6 @@ class YamlFileLoader extends FileLoader $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; } - if ('=' === substr($value, -1)) { - @trigger_error(sprintf('The "=" suffix that used to disable strict references in Symfony 2.x is deprecated since 3.3 and will be unsupported in 4.0. Remove it in "%s".', $value), E_USER_DEPRECATED); - $value = substr($value, 0, -1); - } - if (null !== $invalidBehavior) { $value = new Reference($value, $invalidBehavior); } @@ -782,7 +778,7 @@ class YamlFileLoader extends FileLoader */ private function checkDefinition($id, array $definition, $file) { - if ($throw = $this->isLoadingInstanceof) { + if ($this->isLoadingInstanceof) { $keywords = self::$instanceofKeywords; } elseif ($throw = isset($definition['resource'])) { $keywords = self::$prototypeKeywords; @@ -792,11 +788,7 @@ class YamlFileLoader extends FileLoader foreach ($definition as $key => $value) { if (!isset($keywords[$key])) { - if ($throw) { - throw new InvalidArgumentException(sprintf('The configuration key "%s" is unsupported for definition "%s" in "%s". Allowed configuration keys are "%s".', $key, $id, $file, implode('", "', $keywords))); - } - - @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('", "', $keywords)), E_USER_DEPRECATED); + throw new InvalidArgumentException(sprintf('The configuration key "%s" is unsupported for definition "%s" in "%s". Allowed configuration keys are "%s".', $key, $id, $file, implode('", "', $keywords))); } } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/bad_alias.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/bad_alias.yml new file mode 100644 index 0000000000..78975e5092 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/bad_alias.yml @@ -0,0 +1,11 @@ +services: + foo: + class: stdClass + public: false + + bar: + alias: foo + public: true + # keys other than "alias" and "public" are invalid when defining an alias. + calls: + - [doSomething] diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/legacy_invalid_definition.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/bad_keyword.yml similarity index 100% rename from src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/legacy_invalid_definition.yml rename to src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/bad_keyword.yml diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php index 8de64d55f4..037af21615 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php @@ -510,8 +510,8 @@ class YamlFileLoaderTest extends TestCase } /** - * @group legacy - * @expectedDeprecation Service names that start with an underscore are deprecated since Symfony 3.3 and will be reserved in 4.0. Rename the "_foo" service or define it in XML instead. + * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException + * @expectedExceptionMessage Service names that start with an underscore are reserved. Rename the "_foo" service or define it in XML instead. */ public function testUnderscoreServiceId() { @@ -633,6 +633,28 @@ class YamlFileLoaderTest extends TestCase $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')); $loader->load('bad_empty_instanceof.yml'); } + + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException + * @expectedExceptionMessageRegExp /^The configuration key "private" is unsupported for definition "bar"/ + */ + public function testUnsupportedKeywordThrowsException() + { + $container = new ContainerBuilder(); + $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')); + $loader->load('bad_keyword.yml'); + } + + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException + * @expectedExceptionMessageRegExp /^The configuration key "calls" is unsupported for the service "bar" which is defined as an alias/ + */ + public function testUnsupportedKeywordInServiceAliasThrowsException() + { + $container = new ContainerBuilder(); + $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')); + $loader->load('bad_alias.yml'); + } } interface FooInterface