Skip to content

Commit

Permalink
removed as many usage of the request service as possible without brea…
Browse files Browse the repository at this point in the history
…king BC
  • Loading branch information
fabpot committed Feb 28, 2014
1 parent 7baeaa2 commit d638369
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 50 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function generateUrl($route, $parameters = array(), $referenceType = UrlG
public function forward($controller, array $path = array(), array $query = array()) public function forward($controller, array $path = array(), array $query = array())
{ {
$path['_controller'] = $controller; $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); return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bundle\FrameworkBundle\Tests\TestCase; use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;


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


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

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


$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); $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)); $container->expects($this->at(1))->method('get')->will($this->returnValue($kernel));


$controller = new Controller(); $controller = new Controller();
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@


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


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


class SessionController extends 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(); $session = $request->getSession();


// new session case // 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)); 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.'); return new Response('Session cleared.');
} }


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


return new RedirectResponse($this->container->get('router')->generate('session_showflash')); 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(); $session = $request->getSession();


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


use Symfony\Bundle\FrameworkBundle\Translation\Translator; use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Translation\MessageCatalogue; use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Translation\MessageSelector; 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)); $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'); $requestStack = new RequestStack();

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

$requestStack->push($request);
}


$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); $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 $container
->expects($this->once()) ->expects($this->once())
->method('get') ->method('get')
->with('request') ->with('request_stack')
->will($this->returnValue($request)) ->will($this->returnValue($requestStack))
; ;


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


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

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


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


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


if ('/' === $logoutPath[0]) { 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; $url = UrlGeneratorInterface::ABSOLUTE_URL === $referenceType ? $request->getUriForPath($logoutPath) : $request->getBasePath().$logoutPath;


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller; namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;


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


class LocalizedController extends ContainerAware class LocalizedController extends ContainerAware
{ {
public function loginAction() public function loginAction(Request $request)
{ {
// get the login error if there is one // get the login error if there is one
if ($this->container->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) { if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $this->container->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR); $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else { } 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( return $this->container->get('templating')->renderResponse('FormLoginBundle:Localized:login.html.twig', array(
// last username entered by the user // 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, 'error' => $error,
)); ));
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller; namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;


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


class LoginController extends ContainerAware class LoginController extends ContainerAware
{ {
public function loginAction() public function loginAction(Request $request)
{ {
// get the login error if there is one // get the login error if there is one
if ($this->container->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) { if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $this->container->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR); $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else { } 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( return $this->container->get('templating')->renderResponse('FormLoginBundle:Login:login.html.twig', array(
// last username entered by the user // 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, 'error' => $error,
)); ));
} }
Expand Down

0 comments on commit d638369

Please sign in to comment.