Skip to content

Commit

Permalink
feature #10354 removed as many usage of the request service as possib…
Browse files Browse the repository at this point in the history
…le without breaking BC (fabpot)

This PR was merged into the 2.5-dev branch.

Discussion
----------

removed as many usage of the request service as possible without breaking BC

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Commits
-------

d638369 removed as many usage of the request service as possible without breaking BC
  • Loading branch information
fabpot committed Feb 28, 2014
2 parents 681f14b + d638369 commit eede330
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 50 deletions.
Expand Up @@ -62,7 +62,7 @@ public function generateUrl($route, $parameters = array(), $referenceType = UrlG
public function forward($controller, array $path = array(), array $query = array())
{
$path['_controller'] = $controller;
$subRequest = $this->container->get('request')->duplicate($query, null, $path);
$subRequest = $this->container->get('request_stack')->getCurrentRequest()->duplicate($query, null, $path);

return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
}
Expand Down
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

class ControllerTest extends TestCase
Expand All @@ -24,13 +25,16 @@ public function testForward()
$request->setLocale('fr');
$request->setRequestFormat('xml');

$requestStack = new RequestStack();
$requestStack->push($request);

$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
return new Response($request->getRequestFormat().'--'.$request->getLocale());
}));

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container->expects($this->at(0))->method('get')->will($this->returnValue($request));
$container->expects($this->at(0))->method('get')->will($this->returnValue($requestStack));
$container->expects($this->at(1))->method('get')->will($this->returnValue($kernel));

$controller = new Controller();
Expand Down
Expand Up @@ -11,15 +11,15 @@

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\DependencyInjection\ContainerAware;

class SessionController extends ContainerAware
{
public function welcomeAction($name=null)
public function welcomeAction(Request $request, $name=null)
{
$request = $this->container->get('request');
$session = $request->getSession();

// new session case
Expand All @@ -40,25 +40,23 @@ public function welcomeAction($name=null)
return new Response(sprintf('Welcome back %s, nice to meet you.', $name));
}

public function logoutAction()
public function logoutAction(Request $request)
{
$request = $this->container->get('request')->getSession('session')->invalidate();
$request->getSession('session')->invalidate();

return new Response('Session cleared.');
}

public function setFlashAction($message)
public function setFlashAction(Request $request, $message)
{
$request = $this->container->get('request');
$session = $request->getSession();
$session->getFlashBag()->set('notice', $message);

return new RedirectResponse($this->container->get('router')->generate('session_showflash'));
}

public function showFlashAction()
public function showFlashAction(Request $request)
{
$request = $this->container->get('request');
$session = $request->getSession();

if ($session->getFlashBag()->has('notice')) {
Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Translation;

use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Translation\MessageSelector;
Expand Down Expand Up @@ -86,43 +87,46 @@ public function testTransWithCaching()
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
}

public function testGetLocale()
/**
* @dataProvider getGetLocaleData
*/
public function testGetLocale($inRequestScope)
{
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');

$request
->expects($this->once())
->method('getLocale')
->will($this->returnValue('en'))
;
$requestStack = new RequestStack();
if ($inRequestScope) {
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
$request
->expects($this->once())
->method('getLocale')
->will($this->returnValue('en'))
;

$requestStack->push($request);
}

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');

$container
->expects($this->exactly(2))
->method('isScopeActive')
->with('request')
->will($this->onConsecutiveCalls(false, true))
;

$container
->expects($this->once())
->method('has')
->with('request')
->will($this->returnValue(true))
;

$container
->expects($this->once())
->method('get')
->with('request')
->will($this->returnValue($request))
->with('request_stack')
->will($this->returnValue($requestStack))
;

$translator = new Translator($container, new MessageSelector());

$this->assertNull($translator->getLocale());
$this->assertSame('en', $translator->getLocale());
if ($inRequestScope) {
$this->assertSame('en', $translator->getLocale());
} else {
$this->assertNull($translator->getLocale());
}
}

public function getGetLocaleData()
{
return array(
array(false),
array(true),
);
}

protected function getCatalogue($locale, $messages)
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
Expand Up @@ -65,8 +65,8 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
*/
public function getLocale()
{
if (null === $this->locale && $this->container->isScopeActive('request') && $this->container->has('request')) {
$this->locale = $this->container->get('request')->getLocale();
if (null === $this->locale && $request = $this->container->get('request_stack')->getCurrentRequest()) {
$this->locale = $request->getLocale();
}

return $this->locale;
Expand Down
Expand Up @@ -106,7 +106,7 @@ private function generateLogoutUrl($key, $referenceType)
$parameters = null !== $csrfTokenManager ? array($csrfParameter => (string) $csrfTokenManager->getToken($csrfTokenId)) : array();

if ('/' === $logoutPath[0]) {
$request = $this->container->get('request');
$request = $this->container->get('request_stack')->getCurrentRequest();

$url = UrlGeneratorInterface::ABSOLUTE_URL === $referenceType ? $request->getUriForPath($logoutPath) : $request->getBasePath().$logoutPath;

Expand Down
Expand Up @@ -12,23 +12,24 @@
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;

use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerAware;

class LocalizedController extends ContainerAware
{
public function loginAction()
public function loginAction(Request $request)
{
// get the login error if there is one
if ($this->container->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $this->container->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $this->container->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
$error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
}

return $this->container->get('templating')->renderResponse('FormLoginBundle:Localized:login.html.twig', array(
// last username entered by the user
'last_username' => $this->container->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
Expand Down
Expand Up @@ -12,24 +12,25 @@
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;

use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\DependencyInjection\ContainerAware;

class LoginController extends ContainerAware
{
public function loginAction()
public function loginAction(Request $request)
{
// get the login error if there is one
if ($this->container->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $this->container->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $this->container->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
$error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
}

return $this->container->get('templating')->renderResponse('FormLoginBundle:Login:login.html.twig', array(
// last username entered by the user
'last_username' => $this->container->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
Expand Down

0 comments on commit eede330

Please sign in to comment.