minor #28057 [DI] Improve class named servics error message (ro0NL)
This PR was squashed before being merged into the 3.4 branch (closes #28057).
Discussion
----------
[DI] Improve class named servics error message
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | no
| New feature? | no
| BC breaks? | no <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass? | yes <!-- please add some, will be required by reviewers -->
| Fixed tickets | #28006
| License | MIT
| Doc PR | symfony/symfony-docs#... <!-- required for new features -->
Commits
-------
61de0601a1
[DI] Improve class named servics error message
This commit is contained in:
commit
f4951682bf
|
@ -48,6 +48,15 @@ class CheckDefinitionValidityPass implements CompilerPassInterface
|
|||
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));
|
||||
}
|
||||
if (class_exists($id) || interface_exists($id, false)) {
|
||||
if (0 === strpos($id, '\\') && 1 < substr_count($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 attribute, and appears to reference a '
|
||||
.'class or interface in the global namespace. Leaving out the "class" attribute '
|
||||
|
|
|
@ -1264,6 +1264,30 @@ class ContainerBuilderTest extends TestCase
|
|||
$container->compile();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
|
||||
* @expectedExceptionMessage The definition for "\DateTime" has no class attribute, and appears to reference a class or interface in the global namespace.
|
||||
*/
|
||||
public function testNoClassFromGlobalNamespaceClassIdWithLeadingSlash()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
|
||||
$container->register('\\'.\DateTime::class);
|
||||
$container->compile();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
|
||||
* @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.
|
||||
*/
|
||||
public function testNoClassFromNamespaceClassIdWithLeadingSlash()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
|
||||
$container->register('\\'.FooClass::class);
|
||||
$container->compile();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
|
||||
* @expectedExceptionMessage The definition for "123_abc" has no class.
|
||||
|
|
Reference in New Issue