diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 139041aba81a..fde20aaaba6e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG ----- * Allowed configuring taggable cache pools via a new `framework.cache.pools.tags` option (bool|service-id) + * Deprecated auto-injection of the container in AbstractController instances, register them as service subscribers instead 4.1.0 ----- diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php index 27e714acc86e..d8f2cd8e1af8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php @@ -51,16 +51,22 @@ protected function createController($controller) */ protected function instantiateController($class) { - return $this->configureController(parent::instantiateController($class)); + return $this->configureController(parent::instantiateController($class), $class); } - private function configureController($controller) + private function configureController($controller, string $class) { if ($controller instanceof ContainerAwareInterface) { $controller->setContainer($this->container); } - if ($controller instanceof AbstractController && null !== $previousContainer = $controller->setContainer($this->container)) { - $controller->setContainer($previousContainer); + if ($controller instanceof AbstractController) { + if (null === $previousContainer = $controller->setContainer($this->container)) { + @trigger_error(sprintf('Auto-injection of the container for "%s" is deprecated since Symfony 4.2. Configure it as a service instead.', $class), E_USER_DEPRECATED); + // To be uncommented on Symfony 5: + //throw new \LogicException(sprintf('"%s" has no container set, did you forget to define it as a service subscriber?', $class)); + } else { + $controller->setContainer($previousContainer); + } } return $controller; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerResolverTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerResolverTest.php index a4529a657c7f..d5d35cf4e285 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerResolverTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerResolverTest.php @@ -92,6 +92,10 @@ class_exists(AbstractControllerTest::class); $this->assertSame($container, $controller->getContainer()); } + /** + * @group legacy + * @expectedDeprecation Auto-injection of the container for "Symfony\Bundle\FrameworkBundle\Tests\Controller\TestAbstractController" is deprecated since Symfony 4.2. Configure it as a service instead. + */ public function testAbstractControllerGetsContainerWhenNotSet() { class_exists(AbstractControllerTest::class); @@ -110,6 +114,10 @@ class_exists(AbstractControllerTest::class); $this->assertSame($container, $controller->setContainer($container)); } + /** + * @group legacy + * @expectedDeprecation Auto-injection of the container for "Symfony\Bundle\FrameworkBundle\Tests\Controller\DummyController" is deprecated since Symfony 4.2. Configure it as a service instead. + */ public function testAbstractControllerServiceWithFcqnIdGetsContainerWhenNotSet() { class_exists(AbstractControllerTest::class);