Skip to content

Commit

Permalink
[DependencyInjection] Throw helpful error when shortcutting global cl…
Browse files Browse the repository at this point in the history
…asses

As discussed in #22146 the error message received when trying to use a class in the global
namespace as a service without defined class is confusing. Helpful information was added
pointing out this current limitation.
  • Loading branch information
curry684 committed Mar 24, 2017
1 parent 2dfd136 commit c57b685
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Expand Up @@ -48,6 +48,14 @@ public function process(ContainerBuilder $container)
if ($definition->getFactory()) {
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)) {
throw new RuntimeException(sprintf(
'Service "%s" appears to reference a class in the global namespace, and does '
.'not have a class defined. Leaving out the class property is only allowed '
.'for namespaced classes. Specify the class explicitly to get rid of this error.',
$id
));
}

throw new RuntimeException(sprintf(
'The definition for "%s" has no class. If you intend to inject '
Expand Down
Expand Up @@ -1106,7 +1106,7 @@ public function testClassFromId()

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The definition for "DateTime" has no class.
* @expectedExceptionMessage Service "DateTime" appears to reference a class in the global namespace, and does not have a class defined.
*/
public function testNoClassFromGlobalNamespaceClassId()
{
Expand Down

0 comments on commit c57b685

Please sign in to comment.