feature #10354 removed as many usage of the request service as possible 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
This commit is contained in:
Fabien Potencier 2014-02-28 16:50:01 +01:00
commit eede3303f3
8 changed files with 57 additions and 49 deletions

View File

@ -62,7 +62,7 @@ class Controller extends ContainerAware
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);
}

View File

@ -14,6 +14,7 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Controller;
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
@ -24,13 +25,16 @@ class ControllerTest extends TestCase
$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();

View File

@ -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
@ -40,25 +40,23 @@ class SessionController extends ContainerAware
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')) {

View File

@ -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;
@ -86,43 +87,46 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
$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 = $this->getMock('Symfony\Component\HttpFoundation\Request');
$request
->expects($this->once())
->method('getLocale')
->will($this->returnValue('en'))
;
$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)

View File

@ -65,8 +65,8 @@ class Translator extends BaseTranslator
*/
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;

View File

@ -106,7 +106,7 @@ class LogoutUrlHelper extends Helper
$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;

View File

@ -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,
));
}

View File

@ -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,
));
}