From 15b52941937ee2bccf1c75a7b430af699d6e63e0 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Sun, 11 Aug 2019 10:31:51 +0200 Subject: [PATCH] [DI] Validate class-like service IDs --- .../Compiler/CheckDefinitionValidityPass.php | 8 ++++++++ .../DependencyInjection/Tests/ContainerBuilderTest.php | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/CheckDefinitionValidityPass.php b/src/Symfony/Component/DependencyInjection/Compiler/CheckDefinitionValidityPass.php index 0e9005415a30b..1568b61d189d6 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/CheckDefinitionValidityPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/CheckDefinitionValidityPass.php @@ -65,6 +65,14 @@ public function process(ContainerBuilder $container) $id )); } + if (preg_match('/^\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)++$/', $id)) { + throw new RuntimeException(sprintf( + 'The definition for "%s" has no class attribute, and appears to reference a class or interface. ' + .'Please specify the class attribute explicitly or remove the leading backslash by renaming ' + .'the service to "%s" to get rid of this error.', + $id, substr($id, 1) + )); + } throw new RuntimeException(sprintf('The definition for "%s" has no class. If you intend to inject this service dynamically at runtime, please mark it as synthetic=true. If this is an abstract definition solely used by child definitions, please add abstract=true, otherwise specify a class to get rid of this error.', $id)); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index 199179c9b4998..860deb2580929 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -1289,6 +1289,16 @@ public function testNoClassFromNamespaceClassIdWithLeadingSlash() $container->compile(); } + public function testNoClassFromNamespaceDummyClassIdWithLeadingSlash() + { + $this->expectException('Symfony\Component\DependencyInjection\Exception\RuntimeException'); + $this->expectExceptionMessage('The definition for "\\App\\Foo" has no class attribute, and appears to reference a class or interface. Please specify the class attribute explicitly or remove the leading backslash by renaming the service to "App\\Foo" to get rid of this error.'); + $container = new ContainerBuilder(); + + $container->register('\\App\\Foo'); + $container->compile(); + } + public function testNoClassFromNonClassId() { $this->expectException('Symfony\Component\DependencyInjection\Exception\RuntimeException');