From a39bed8e60669efc6d50a5b3b2f152b2dde1f570 Mon Sep 17 00:00:00 2001 From: Niels Keurentjes Date: Thu, 31 May 2018 12:02:59 +0200 Subject: [PATCH 1/6] Improve exception message when AbstractController::getParameter fails Fixes #27436 --- .../Controller/AbstractController.php | 5 +++- .../Controller/AbstractControllerTest.php | 26 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php index b317786c68583..bacbfb1b8a673 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php @@ -13,6 +13,7 @@ use Psr\Container\ContainerInterface; use Doctrine\Common\Persistence\ManagerRegistry; +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; use Symfony\Component\DependencyInjection\ServiceSubscriberInterface; use Symfony\Component\Form\FormFactoryInterface; @@ -64,7 +65,9 @@ public function setContainer(ContainerInterface $container) protected function getParameter(string $name) { if (!$this->container->has('parameter_bag')) { - throw new \LogicException('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"'); + throw new ServiceNotFoundException('parameter_bag', null, null, array(), + 'The "parameter_bag" service could not be located. Ensure that symfony/dependency-injection 4.1.0 or higher '. + 'is installed and that the controller is either set to be autoconfigured or wired manually to inject it'); } return $this->container->get('parameter_bag')->get($name); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php index 0ae42c14ce317..496cd842f31b3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php @@ -52,20 +52,32 @@ public function testSubscribedServices() public function testGetParameter() { + if (!class_exists(ContainerBag::class)) { + $this->markTestSkipped('ContainerBag class does not exist'); + } + $container = new Container(new FrozenParameterBag(array('foo' => 'bar'))); + $container->set('parameter_bag', new ContainerBag($container)); $controller = $this->createController(); $controller->setContainer($container); - if (!class_exists(ContainerBag::class)) { - $this->expectException(\LogicException::class); - $this->expectExceptionMessage('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"'); - } else { - $container->set('parameter_bag', new ContainerBag($container)); - } - $this->assertSame('bar', $controller->getParameter('foo')); } + + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException + * @expectedExceptionMessage The "parameter_bag" service could not be located + */ + public function testMissingParameterBag() + { + $container = new Container(); + + $controller = $this->createController(); + $controller->setContainer($container); + + $controller->getParameter('foo'); + } } class TestAbstractController extends AbstractController From f3e01e0fdde015c32fb042806e6deb3cbbe06936 Mon Sep 17 00:00:00 2001 From: Niels Keurentjes Date: Thu, 31 May 2018 14:14:31 +0200 Subject: [PATCH 2/6] Finetune exception message --- .../FrameworkBundle/Controller/AbstractController.php | 11 ++++++++--- .../Tests/Controller/AbstractControllerTest.php | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php index bacbfb1b8a673..ef59564e898b9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php @@ -65,9 +65,14 @@ public function setContainer(ContainerInterface $container) protected function getParameter(string $name) { if (!$this->container->has('parameter_bag')) { - throw new ServiceNotFoundException('parameter_bag', null, null, array(), - 'The "parameter_bag" service could not be located. Ensure that symfony/dependency-injection 4.1.0 or higher '. - 'is installed and that the controller is either set to be autoconfigured or wired manually to inject it'); + $message = 'The "parameter_bag" service could not be located. '; + if (!interface_exists(ContainerBagInterface::class)) { + $message .= 'Upgrade symfony/dependency-injection to at least 4.1.0 to have it injected automatically.'; + } else { + $message .= 'Ensure that the controller is either set to be autoconfigured or wired manually to inject it.'; + } + + throw new ServiceNotFoundException('parameter_bag', null, null, array(), $message); } return $this->container->get('parameter_bag')->get($name); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php index 496cd842f31b3..c83fa886c38ca 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php @@ -67,7 +67,7 @@ public function testGetParameter() /** * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException - * @expectedExceptionMessage The "parameter_bag" service could not be located + * @expectedExceptionMessage The "parameter_bag" service could not be located. */ public function testMissingParameterBag() { From 3dec31d959de2659d9f6dfcd18178aa35e766a2d Mon Sep 17 00:00:00 2001 From: Niels Keurentjes Date: Thu, 31 May 2018 17:42:07 +0200 Subject: [PATCH 3/6] Remove reference to upgrading DI as FrameworkBundle already required ^4.1 --- .../FrameworkBundle/Controller/AbstractController.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php index ef59564e898b9..277bc1f2b3d40 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php @@ -65,14 +65,9 @@ public function setContainer(ContainerInterface $container) protected function getParameter(string $name) { if (!$this->container->has('parameter_bag')) { - $message = 'The "parameter_bag" service could not be located. '; - if (!interface_exists(ContainerBagInterface::class)) { - $message .= 'Upgrade symfony/dependency-injection to at least 4.1.0 to have it injected automatically.'; - } else { - $message .= 'Ensure that the controller is either set to be autoconfigured or wired manually to inject it.'; - } - - throw new ServiceNotFoundException('parameter_bag', null, null, array(), $message); + throw new ServiceNotFoundException('parameter_bag', null, null, array(), + 'The "parameter_bag" service could not be located. Ensure that the controller is either set ' . + 'to be autoconfigured or wired manually to inject it.'); } return $this->container->get('parameter_bag')->get($name); From 90222dbc22ab15f121922aff0ea84b7d452e6af3 Mon Sep 17 00:00:00 2001 From: Niels Keurentjes Date: Thu, 31 May 2018 17:43:00 +0200 Subject: [PATCH 4/6] And Fabbot disagrees again with my spaces between all operators --- .../Bundle/FrameworkBundle/Controller/AbstractController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php index 277bc1f2b3d40..97d8f4fc006c2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php @@ -66,7 +66,7 @@ protected function getParameter(string $name) { if (!$this->container->has('parameter_bag')) { throw new ServiceNotFoundException('parameter_bag', null, null, array(), - 'The "parameter_bag" service could not be located. Ensure that the controller is either set ' . + 'The "parameter_bag" service could not be located. Ensure that the controller is either set '. 'to be autoconfigured or wired manually to inject it.'); } From b34f72dec57540855a234c3a237e17ed9e782fc2 Mon Sep 17 00:00:00 2001 From: Niels Keurentjes Date: Sat, 2 Jun 2018 00:24:10 +0200 Subject: [PATCH 5/6] Change exception message once again --- .../Bundle/FrameworkBundle/Controller/AbstractController.php | 3 +-- .../Tests/Controller/AbstractControllerTest.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php index 97d8f4fc006c2..5917ef095bfe7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php @@ -66,8 +66,7 @@ protected function getParameter(string $name) { if (!$this->container->has('parameter_bag')) { throw new ServiceNotFoundException('parameter_bag', null, null, array(), - 'The "parameter_bag" service could not be located. Ensure that the controller is either set '. - 'to be autoconfigured or wired manually to inject it.'); + sprintf('The "getParameter()" method of the controller can\'t get the value of the "%s" parameter because the "parameter_bag" service could not be located. Either enable autoconfigure for the controller service or inject a ServiceLocator referencing a "parameter_bag" service manually in the controller.', $name)); } return $this->container->get('parameter_bag')->get($name); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php index c83fa886c38ca..9ea912af58cdd 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php @@ -67,7 +67,7 @@ public function testGetParameter() /** * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException - * @expectedExceptionMessage The "parameter_bag" service could not be located. + * @expectedExceptionMessage The "getParameter()" method of the controller can't get the value of the "foo" parameter */ public function testMissingParameterBag() { From 1f400f63f9c7a55ee92e78fcefb4969d59315197 Mon Sep 17 00:00:00 2001 From: Niels Keurentjes Date: Sun, 3 Jun 2018 01:01:05 +0200 Subject: [PATCH 6/6] More message finetuning --- .../Bundle/FrameworkBundle/Controller/AbstractController.php | 3 +-- .../Tests/Controller/AbstractControllerTest.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php index 5917ef095bfe7..7286162c2e5ac 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php @@ -65,8 +65,7 @@ public function setContainer(ContainerInterface $container) protected function getParameter(string $name) { if (!$this->container->has('parameter_bag')) { - throw new ServiceNotFoundException('parameter_bag', null, null, array(), - sprintf('The "getParameter()" method of the controller can\'t get the value of the "%s" parameter because the "parameter_bag" service could not be located. Either enable autoconfigure for the controller service or inject a ServiceLocator referencing a "parameter_bag" service manually in the controller.', $name)); + throw new ServiceNotFoundException('parameter_bag', null, null, array(), sprintf('The "%s::getParameter()" method is missing a parameter bag to work properly. Did you forget to register your controller as a service subscriber? This can be fixed either by using autoconfiguration or by manually wiring a "parameter_bag" in the service locator passed to the controller.', get_class($this))); } return $this->container->get('parameter_bag')->get($name); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php index 9ea912af58cdd..03b451528d48f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php @@ -67,7 +67,7 @@ public function testGetParameter() /** * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException - * @expectedExceptionMessage The "getParameter()" method of the controller can't get the value of the "foo" parameter + * @expectedExceptionMessage TestAbstractController::getParameter()" method is missing a parameter bag */ public function testMissingParameterBag() {