Skip to content

Commit 36401d5

Browse files
Merge branch '3.4' into 4.0
* 3.4: [HttpKernel] Fixed invalid REMOTE_ADDR in inline subrequest when configuring trusted proxy with subnet [FrameworkBundle] fixed guard event names for transitions [DI] Improve class named servics error message [HttpFoundation] fixed using _method parameter with invalid type [Intl] Replace svn with git in the icu data update script [HttpFoundation] Fix Cookie::isCleared
2 parents bda259d + 1c0e679 commit 36401d5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Compiler/CheckDefinitionValidityPass.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ public function process(ContainerBuilder $container)
4848
throw new RuntimeException(sprintf('Please add the class to service "%s" even if it is constructed by a factory since we might need to add method calls based on compile-time checks.', $id));
4949
}
5050
if (class_exists($id) || interface_exists($id, false)) {
51+
if (0 === strpos($id, '\\') && 1 < substr_count($id, '\\')) {
52+
throw new RuntimeException(sprintf(
53+
'The definition for "%s" has no class attribute, and appears to reference a class or interface. '
54+
.'Please specify the class attribute explicitly or remove the leading backslash by renaming '
55+
.'the service to "%s" to get rid of this error.',
56+
$id, substr($id, 1)
57+
));
58+
}
59+
5160
throw new RuntimeException(sprintf(
5261
'The definition for "%s" has no class attribute, and appears to reference a '
5362
.'class or interface in the global namespace. Leaving out the "class" attribute '

Tests/ContainerBuilderTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,30 @@ public function testNoClassFromGlobalNamespaceClassId()
12381238
$container->compile();
12391239
}
12401240

1241+
/**
1242+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
1243+
* @expectedExceptionMessage The definition for "\DateTime" has no class attribute, and appears to reference a class or interface in the global namespace.
1244+
*/
1245+
public function testNoClassFromGlobalNamespaceClassIdWithLeadingSlash()
1246+
{
1247+
$container = new ContainerBuilder();
1248+
1249+
$container->register('\\'.\DateTime::class);
1250+
$container->compile();
1251+
}
1252+
1253+
/**
1254+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
1255+
* @expectedExceptionMessage The definition for "\Symfony\Component\DependencyInjection\Tests\FooClass" 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 "Symfony\Component\DependencyInjection\Tests\FooClass" to get rid of this error.
1256+
*/
1257+
public function testNoClassFromNamespaceClassIdWithLeadingSlash()
1258+
{
1259+
$container = new ContainerBuilder();
1260+
1261+
$container->register('\\'.FooClass::class);
1262+
$container->compile();
1263+
}
1264+
12411265
/**
12421266
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
12431267
* @expectedExceptionMessage The definition for "123_abc" has no class.

0 commit comments

Comments
 (0)