From 4f3d502d6e2b15624abefff6f2eefde803f14193 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 9 Oct 2013 08:28:55 +0200 Subject: [PATCH] [FrameworkBundle] removed some more dependencies on the request service --- UPGRADE-3.0.md | 3 ++ .../Resources/config/services.xml | 2 + .../Resources/config/templating_php.xml | 4 +- .../Templating/Helper/RequestHelper.php | 33 +++++++++++++--- .../Templating/Helper/SessionHelper.php | 38 +++++++++++++++---- .../Templating/Helper/RequestHelperTest.php | 20 +++++----- 6 files changed, 74 insertions(+), 26 deletions(-) diff --git a/UPGRADE-3.0.md b/UPGRADE-3.0.md index dffbf8ca95fc7..be70db255a00a 100644 --- a/UPGRADE-3.0.md +++ b/UPGRADE-3.0.md @@ -178,6 +178,9 @@ UPGRADE FROM 2.x to 3.0 ### FrameworkBundle + * The `request` service was removed. You must inject the `request_stack` + service instead. + * The `enctype` method of the `form` helper was removed. You should use the new method `start` instead. diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml index 608bf42d5253f..4457dbb27669c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml @@ -43,6 +43,8 @@ YourRequestClass to the Kernel. This service definition only defines the scope of the request. It is used to check references scope. + + This service is deprecated, you should use the request_stack service instead. --> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml index 2f9992aa4a7b0..ffa66a74c6b9d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml @@ -67,12 +67,12 @@ - + - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/RequestHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/RequestHelper.php index 4533bf172bc6c..d071a586c02e8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/RequestHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/RequestHelper.php @@ -13,6 +13,7 @@ use Symfony\Component\Templating\Helper\Helper; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; /** * RequestHelper provides access to the current request parameters. @@ -22,15 +23,24 @@ class RequestHelper extends Helper { protected $request; + protected $requestStack; /** * Constructor. * - * @param Request $request A Request instance + * @param Request|RequestStack $requestStack A RequestStack instance or a Request instance + * + * @deprecated since 2.5, passing a Request instance is deprecated and support for it will be removed in 3.0 */ - public function __construct(Request $request) + public function __construct($requestStack) { - $this->request = $request; + if ($requestStack instanceof Request) { + $this->request = $requestStack; + } elseif ($requestStack instanceof RequestStack) { + $this->requestStack = $requestStack; + } else { + throw new \InvalidArgumentException('RequestHelper only accepts a Request or a RequestStack instance.'); + } } /** @@ -45,7 +55,7 @@ public function __construct(Request $request) */ public function getParameter($key, $default = null) { - return $this->request->get($key, $default); + return $this->getRequest()->get($key, $default); } /** @@ -55,7 +65,20 @@ public function getParameter($key, $default = null) */ public function getLocale() { - return $this->request->getLocale(); + return $this->getRequest()->getLocale(); + } + + private function getRequest() + { + if ($this->requestStack) { + if (!$this->requestStack->getCurrentRequest()) { + throw new \LogicException('A Request must be available.'); + } + + return $this->requestStack->getCurrentRequest(); + } + + return $this->request; } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php index aac3f6dd7b1ee..d0bf4aa492a0b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php @@ -12,7 +12,7 @@ namespace Symfony\Bundle\FrameworkBundle\Templating\Helper; use Symfony\Component\Templating\Helper\Helper; -use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; /** * SessionHelper provides read-only access to the session attributes. @@ -22,15 +22,24 @@ class SessionHelper extends Helper { protected $session; + protected $requestStack; /** * Constructor. * - * @param Request $request A Request instance + * @param Request|RequestStack $requestStack A RequestStack instance or a Request instance + * + * @deprecated since 2.5, passing a Request instance is deprecated and support for it will be removed in 3.0 */ - public function __construct(Request $request) + public function __construct($requestStack) { - $this->session = $request->getSession(); + if ($requestStack instanceof Request) { + $this->session = $requestStack->getSession(); + } elseif ($requestStack instanceof RequestStack) { + $this->requestStack = $requestStack; + } else { + throw new \InvalidArgumentException('RequestHelper only accepts a Request or a RequestStack instance.'); + } } /** @@ -43,22 +52,35 @@ public function __construct(Request $request) */ public function get($name, $default = null) { - return $this->session->get($name, $default); + return $this->getSession()->get($name, $default); } public function getFlash($name, array $default = array()) { - return $this->session->getFlashBag()->get($name, $default); + return $this->getSession()->getFlashBag()->get($name, $default); } public function getFlashes() { - return $this->session->getFlashBag()->all(); + return $this->getSession()->getFlashBag()->all(); } public function hasFlash($name) { - return $this->session->getFlashBag()->has($name); + return $this->getSession()->getFlashBag()->has($name); + } + + private function getSession() + { + if (null === $this->session) { + if (!$this->requestStack->getMasterRequest()) { + throw new \LogicException('A Request must be available.'); + } + + $this->session = $this->requestStack->getMasterRequest()->getSession(); + } + + return $this->session; } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/RequestHelperTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/RequestHelperTest.php index d3d4f22385c24..d626e63e41cfa 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/RequestHelperTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/RequestHelperTest.php @@ -12,26 +12,24 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Bundle\FrameworkBundle\Templating\Helper\RequestHelper; class RequestHelperTest extends \PHPUnit_Framework_TestCase { - protected $request; + protected $requestStack; protected function setUp() { - $this->request = new Request(); - $this->request->initialize(array('foobar' => 'bar')); - } - - protected function tearDown() - { - $this->request = null; + $this->requestStack = new RequestStack(); + $request = new Request(); + $request->initialize(array('foobar' => 'bar')); + $this->requestStack->push($request); } public function testGetParameter() { - $helper = new RequestHelper($this->request); + $helper = new RequestHelper($this->requestStack); $this->assertEquals('bar', $helper->getParameter('foobar')); $this->assertEquals('foo', $helper->getParameter('bar', 'foo')); @@ -41,14 +39,14 @@ public function testGetParameter() public function testGetLocale() { - $helper = new RequestHelper($this->request); + $helper = new RequestHelper($this->requestStack); $this->assertEquals('en', $helper->getLocale()); } public function testGetName() { - $helper = new RequestHelper($this->request); + $helper = new RequestHelper($this->requestStack); $this->assertEquals('request', $helper->getName()); } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy