diff --git a/src/Symfony/Component/DependencyInjection/Compiler/CheckDefinitionValidityPass.php b/src/Symfony/Component/DependencyInjection/Compiler/CheckDefinitionValidityPass.php index a43e0cd78f404..5bbadd6bdf12a 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/CheckDefinitionValidityPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/CheckDefinitionValidityPass.php @@ -53,7 +53,7 @@ public function process(ContainerBuilder $container) 'The definition for "%s" has no class attribute, and appears to reference a ' .'class or interface in the global namespace. Leaving out the "class" attribute ' .'is only allowed for namespaced classes. Please specify the class attribute ' - .'explicitly to get rid of this error.', + .'explicitly or change its service identifier to \\%1$s to get rid of this error.', $id )); } diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ResolveClassPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ResolveClassPass.php index c40d6f5d3d0f1..d380e62441e62 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ResolveClassPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ResolveClassPass.php @@ -31,7 +31,7 @@ public function process(ContainerBuilder $container) if ($definition->isSynthetic() || null !== $definition->getClass()) { continue; } - 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)) { + 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]*+)++|\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)$/', $id)) { if ($definition instanceof ChildDefinition && !class_exists($id)) { throw new InvalidArgumentException(sprintf('Service definition "%s" has a parent but no class, and its name looks like a FQCN. Either the class is missing or you want to inherit it from the parent service. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class.', $id)); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index efe9840e42c40..74245a0026b24 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -1069,18 +1069,6 @@ public function testNoClassFromNonClassId() $container->compile(); } - /** - * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException - * @expectedExceptionMessage The definition for "\foo" has no class. - */ - public function testNoClassFromNsSeparatorId() - { - $container = new ContainerBuilder(); - - $definition = $container->register('\\foo'); - $container->compile(); - } - public function testServiceLocator() { $container = new ContainerBuilder(); @@ -1129,6 +1117,26 @@ public function testPrivateServiceTriggersDeprecation() $container->get('bar'); } + + public function testNamespacedClassServiceWithRootNs() + { + $container = new ContainerBuilder(); + $definition = $container->register($expected = '\\My\\DateTime'); + + $container->compile(); + + $this->assertSame($expected, $definition->getClass()); + } + + public function testClassServiceWithRootNs() + { + $container = new ContainerBuilder(); + $definition = $container->register($expected = '\\DateTime'); + + $container->compile(); + + $this->assertSame($expected, $definition->getClass()); + } } class FooClass