From 7781082587de0dcd28e7a28dcbcca6d40cd772b5 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 1 Feb 2017 09:24:16 +0100 Subject: [PATCH] [DI] Deprecate underscore-services in YamlFileLoader --- .../DependencyInjection/Loader/YamlFileLoader.php | 5 ++++- .../Tests/Fixtures/yaml/services_underscore.yml | 3 +++ .../Tests/Loader/YamlFileLoaderTest.php | 11 +++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_underscore.yml diff --git a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php index ba29ca60b50df..9edb951a5dea7 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php @@ -182,7 +182,7 @@ private function parseDefaults(array &$content, $file) throw new InvalidArgumentException(sprintf('Service defaults must be an array, "%s" given in "%s".', gettype($defaults), $file)); } if (isset($defaults['alias']) || isset($defaults['class']) || isset($defaults['factory'])) { - @trigger_error('Giving a service the "_defaults" name is deprecated since Symfony 3.3 and will be forbidden in 4.0. Rename your service.', E_USER_DEPRECATED); + // @deprecated code path, to be removed in 4.0 return array(); } @@ -239,6 +239,9 @@ private function parseDefaults(array &$content, $file) */ 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); + } if (is_string($service) && 0 === strpos($service, '@')) { $public = isset($defaults['public']) ? $defaults['public'] : true; $this->container->setAlias($id, new Alias(substr($service, 1), $public)); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_underscore.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_underscore.yml new file mode 100644 index 0000000000000..0cdbf9032a11c --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_underscore.yml @@ -0,0 +1,3 @@ +services: + _foo: + class: Foo diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php index fedea7084e4f9..7b3f0521779d4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php @@ -436,4 +436,15 @@ public function testInvalidTagsWithDefaults() $loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml')); $loader->load('services31_invalid_tags.yml'); } + + /** + * @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. + */ + public function testUnderscoreServiceId() + { + $container = new ContainerBuilder(); + $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')); + $loader->load('services_underscore.yml'); + } }