Skip to content

Commit

Permalink
[FrameworkBundle] removed some more dependencies on the request service
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Dec 29, 2013
1 parent 8bee688 commit 4f3d502
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 26 deletions.
3 changes: 3 additions & 0 deletions UPGRADE-3.0.md
Expand Up @@ -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.

Expand Down
Expand Up @@ -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.
-->
<service id="request" scope="request" synthetic="true" synchronized="true" />

Expand Down
Expand Up @@ -67,12 +67,12 @@

<service id="templating.helper.request" class="%templating.helper.request.class%">
<tag name="templating.helper" alias="request" />
<argument type="service" id="request" strict="false" />
<argument type="service" id="request_stack" />
</service>

<service id="templating.helper.session" class="%templating.helper.session.class%">
<tag name="templating.helper" alias="session" />
<argument type="service" id="request" strict="false" />
<argument type="service" id="request_stack" />
</service>

<service id="templating.helper.router" class="%templating.helper.router.class%">
Expand Down
Expand Up @@ -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.
Expand All @@ -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.');
}
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

/**
Expand Down
Expand Up @@ -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.
Expand All @@ -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.');
}
}

/**
Expand All @@ -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;
}

/**
Expand Down
Expand Up @@ -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'));
Expand All @@ -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());
}
Expand Down

0 comments on commit 4f3d502

Please sign in to comment.