Skip to content

Commit 33d8c6b

Browse files
committed
removed as many usage of the request service as possible without breaking BC
1 parent 05ed6e5 commit 33d8c6b

File tree

5 files changed

+45
-39
lines changed

5 files changed

+45
-39
lines changed

Controller/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function generateUrl($route, $parameters = array(), $referenceType = UrlG
6262
public function forward($controller, array $path = array(), array $query = array())
6363
{
6464
$path['_controller'] = $controller;
65-
$subRequest = $this->container->get('request')->duplicate($query, null, $path);
65+
$subRequest = $this->container->get('request_stack')->getCurrentRequest()->duplicate($query, null, $path);
6666

6767
return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
6868
}

Tests/Controller/ControllerTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1515
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
1616
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpFoundation\RequestStack;
1718
use Symfony\Component\HttpFoundation\Response;
1819

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

28+
$requestStack = new RequestStack();
29+
$requestStack->push($request);
30+
2731
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
2832
$kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
2933
return new Response($request->getRequestFormat().'--'.$request->getLocale());
3034
}));
3135

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

3640
$controller = new Controller();

Tests/Functional/Bundle/TestBundle/Controller/SessionController.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

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

14+
use Symfony\Component\HttpFoundation\Request;
1415
use Symfony\Component\HttpFoundation\Response;
1516
use Symfony\Component\HttpFoundation\RedirectResponse;
1617
use Symfony\Component\DependencyInjection\ContainerAware;
1718

1819
class SessionController extends ContainerAware
1920
{
20-
public function welcomeAction($name=null)
21+
public function welcomeAction(Request $request, $name=null)
2122
{
22-
$request = $this->container->get('request');
2323
$session = $request->getSession();
2424

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

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

4747
return new Response('Session cleared.');
4848
}
4949

50-
public function setFlashAction($message)
50+
public function setFlashAction(Request $request, $message)
5151
{
52-
$request = $this->container->get('request');
5352
$session = $request->getSession();
5453
$session->getFlashBag()->set('notice', $message);
5554

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

59-
public function showFlashAction()
58+
public function showFlashAction(Request $request)
6059
{
61-
$request = $this->container->get('request');
6260
$session = $request->getSession();
6361

6462
if ($session->getFlashBag()->has('notice')) {

Tests/Translation/TranslatorTest.php

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Translation;
1313

1414
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
15+
use Symfony\Component\HttpFoundation\RequestStack;
1516
use Symfony\Component\Translation\MessageCatalogue;
1617
use Symfony\Component\Filesystem\Filesystem;
1718
use Symfony\Component\Translation\MessageSelector;
@@ -86,43 +87,46 @@ public function testTransWithCaching()
8687
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
8788
}
8889

89-
public function testGetLocale()
90+
/**
91+
* @dataProvider getGetLocaleData
92+
*/
93+
public function testGetLocale($inRequestScope)
9094
{
91-
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
92-
93-
$request
94-
->expects($this->once())
95-
->method('getLocale')
96-
->will($this->returnValue('en'))
97-
;
95+
$requestStack = new RequestStack();
96+
if ($inRequestScope) {
97+
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
98+
$request
99+
->expects($this->once())
100+
->method('getLocale')
101+
->will($this->returnValue('en'))
102+
;
103+
104+
$requestStack->push($request);
105+
}
98106

99107
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
100-
101-
$container
102-
->expects($this->exactly(2))
103-
->method('isScopeActive')
104-
->with('request')
105-
->will($this->onConsecutiveCalls(false, true))
106-
;
107-
108-
$container
109-
->expects($this->once())
110-
->method('has')
111-
->with('request')
112-
->will($this->returnValue(true))
113-
;
114-
115108
$container
116109
->expects($this->once())
117110
->method('get')
118-
->with('request')
119-
->will($this->returnValue($request))
111+
->with('request_stack')
112+
->will($this->returnValue($requestStack))
120113
;
121114

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

124-
$this->assertNull($translator->getLocale());
125-
$this->assertSame('en', $translator->getLocale());
117+
if ($inRequestScope) {
118+
$this->assertSame('en', $translator->getLocale());
119+
} else {
120+
$this->assertNull($translator->getLocale());
121+
}
122+
}
123+
124+
public function getGetLocaleData()
125+
{
126+
return array(
127+
array(false),
128+
array(true),
129+
);
126130
}
127131

128132
protected function getCatalogue($locale, $messages)

Translation/Translator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
6565
*/
6666
public function getLocale()
6767
{
68-
if (null === $this->locale && $this->container->isScopeActive('request') && $this->container->has('request')) {
69-
$this->locale = $this->container->get('request')->getLocale();
68+
if (null === $this->locale && $request = $this->container->get('request_stack')->getCurrentRequest()) {
69+
$this->locale = $request->getLocale();
7070
}
7171

7272
return $this->locale;

0 commit comments

Comments
 (0)