From 261e045e1c3822f4fa86a56d10d179279fac0b24 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Fri, 1 Dec 2017 21:48:23 +0100 Subject: [PATCH] [DI] Improve service not found in service subscribers --- .../Tests/Controller/AbstractControllerTest.php | 17 +++++++++++++++++ .../Exception/ServiceNotFoundException.php | 13 ++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php index 6783ec25c5ab5..6059c42268605 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php @@ -13,9 +13,21 @@ use Psr\Container\ContainerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\DependencyInjection\Container; class AbstractControllerTest extends ControllerTraitTest { + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException + * @expectedExceptionMessage You have requested a non-existent service "unknown". Did you forget to add it to "Symfony\Bundle\FrameworkBundle\Tests\Controller\TestAbstractController::getSubscribedServices()"? + */ + public function testServiceNotFound() + { + $controller = $this->createController(); + $controller->setContainer(new Container()); + $controller->serviceNotFoundAction(); + } + protected function createController() { return new TestAbstractController(); @@ -60,4 +72,9 @@ public function setContainer(ContainerInterface $container) public function fooAction() { } + + public function serviceNotFoundAction() + { + $this->get('unknown'); + } } diff --git a/src/Symfony/Component/DependencyInjection/Exception/ServiceNotFoundException.php b/src/Symfony/Component/DependencyInjection/Exception/ServiceNotFoundException.php index dabd9da4a41e4..4cae310dc12b0 100644 --- a/src/Symfony/Component/DependencyInjection/Exception/ServiceNotFoundException.php +++ b/src/Symfony/Component/DependencyInjection/Exception/ServiceNotFoundException.php @@ -12,6 +12,7 @@ namespace Symfony\Component\DependencyInjection\Exception; use Psr\Container\NotFoundExceptionInterface; +use Symfony\Component\DependencyInjection\ServiceSubscriberInterface; /** * This exception is thrown when a non-existent service is requested. @@ -34,7 +35,17 @@ public function __construct(string $id, string $sourceId = null, \Exception $pre $msg = sprintf('The service "%s" has a dependency on a non-existent service "%s".', $sourceId, $id); } - if ($alternatives) { + $subscriber = null; + foreach (array_reverse(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)) as $trace) { + if (isset($trace['class']) && in_array(ServiceSubscriberInterface::class, class_implements($trace['class']))) { + $subscriber = $trace['class']; + break; + } + } + + if (null !== $subscriber) { + $msg .= sprintf(' Did you forget to add it to "%s::getSubscribedServices()"?', $subscriber); + } elseif ($alternatives) { if (1 == count($alternatives)) { $msg .= ' Did you mean this: "'; } else {