diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php index 5e30657301d0d..2f1b2a9352410 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php @@ -13,7 +13,6 @@ use Psr\Container\ContainerInterface; use Doctrine\Common\Persistence\ManagerRegistry; -use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; use Symfony\Component\DependencyInjection\ServiceSubscriberInterface; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\HttpFoundation\RequestStack; @@ -53,6 +52,22 @@ public function setContainer(ContainerInterface $container) return $previous; } + /** + * Gets a container parameter by its name. + * + * @return mixed + * + * @final + */ + 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"'); + } + + return $this->container->get('parameter_bag')->get($name); + } + public static function getSubscribedServices() { return array( @@ -68,7 +83,7 @@ public static function getSubscribedServices() 'form.factory' => '?'.FormFactoryInterface::class, 'security.token_storage' => '?'.TokenStorageInterface::class, 'security.csrf.token_manager' => '?'.CsrfTokenManagerInterface::class, - 'parameter_bag' => '?'.ContainerBagInterface::class, + 'parameter_bag' => '?'.ContainerInterface::class, ); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php index 8d275d0b59e49..91f499299a060 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php @@ -377,20 +377,4 @@ protected function isCsrfTokenValid(string $id, string $token): bool return $this->container->get('security.csrf.token_manager')->isTokenValid(new CsrfToken($id, $token)); } - - /** - * Gets a container parameter by its name. - * - * @return mixed - * - * @final - */ - 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"'); - } - - 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 6783ec25c5ab5..77e92a09c0798 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php @@ -13,6 +13,9 @@ use Psr\Container\ContainerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\DependencyInjection\Container; +use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag; +use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; class AbstractControllerTest extends ControllerTraitTest { @@ -20,6 +23,23 @@ protected function createController() { return new TestAbstractController(); } + + public function testGetParameter() + { + $container = new Container(new FrozenParameterBag(array('foo' => 'bar'))); + + $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')); + } } class TestAbstractController extends AbstractController @@ -57,6 +77,11 @@ public function setContainer(ContainerInterface $container) return parent::setContainer($container); } + public function getParameter(string $name) + { + return parent::getParameter($name); + } + public function fooAction() { } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php index 1d056647a2427..a208775cf23d7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php @@ -14,9 +14,6 @@ use Symfony\Bundle\FrameworkBundle\Tests\TestCase; use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait; use Symfony\Component\DependencyInjection\Container; -use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag; -use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; -use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\JsonResponse; @@ -531,23 +528,6 @@ public function testGetDoctrine() $this->assertEquals($doctrine, $controller->getDoctrine()); } - - public function testGetParameter() - { - $container = new Container(new FrozenParameterBag(array('foo' => 'bar'))); - - $controller = $this->createController(); - $controller->setContainer($container); - - if (!interface_exists(ContainerBagInterface::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')); - } } trait TestControllerTrait @@ -572,6 +552,5 @@ trait TestControllerTrait createForm as public; createFormBuilder as public; getDoctrine as public; - getParameter as public; } }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: