Skip to content

Commit d94acd8

Browse files
committed
remove response as a service
The Response is not available in the DIC anymore. When you need to create a response, create an instance of Symfony\Component\HttpFoundation\Response instead. As a side effect, the Controller::createResponse() and Controller::redirect() methods have been removed and can easily be replaced as follows: return $this->createResponse('content', 200, array('foo' => 'bar')); return new Response('content', 200, array('foo' => 'bar')); return $this->redirect($url); return Response::createRedirect($url);
1 parent bf20238 commit d94acd8

File tree

16 files changed

+36
-144
lines changed

16 files changed

+36
-144
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,6 @@
2323
*/
2424
class Controller extends ContainerAware
2525
{
26-
/**
27-
* Creates a Response instance.
28-
*
29-
* @param string $content The Response body
30-
* @param integer $status The status code
31-
* @param array $headers An array of HTTP headers
32-
*
33-
* @return Response A Response instance
34-
*/
35-
public function createResponse($content = '', $status = 200, array $headers = array())
36-
{
37-
$response = $this->container->get('response');
38-
$response->setContent($content);
39-
$response->setStatusCode($status);
40-
foreach ($headers as $name => $value) {
41-
$response->headers->set($name, $value);
42-
}
43-
44-
return $response;
45-
}
46-
4726
/**
4827
* Generates a URL from the given parameters.
4928
*
@@ -72,18 +51,6 @@ public function forward($controller, array $path = array(), array $query = array
7251
return $this->container->get('http_kernel')->forward($controller, $path, $query);
7352
}
7453

75-
/**
76-
* Returns an HTTP redirect Response.
77-
*
78-
* @return Response A Response instance
79-
*/
80-
public function redirect($url, $status = 302)
81-
{
82-
$response = $this->container->get('response');
83-
$response->setRedirect($url, $status);
84-
return $response;
85-
}
86-
8754
/**
8855
* Returns a rendered view.
8956
*

src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,13 @@ class RedirectController extends ContainerAware
3838
public function redirectAction($route, $permanent = false)
3939
{
4040
if (!$route) {
41-
$response = $this->container->get('response');
42-
$response->setStatusCode(410);
43-
44-
return $response;
41+
return new Response(null, 410);
4542
}
4643

47-
$code = $permanent ? 301 : 302;
48-
4944
$attributes = $this->container->get('request')->attributes->all();
5045
unset($attributes['_route'], $attributes['route'], $attributes['permanent'] );
5146

52-
$response = $this->container->get('response');
53-
$response->setRedirect($this->container->get('router')->generate($route, $attributes), $code);
54-
55-
return $response;
47+
return Response::createRedirect($this->container->get('router')->generate($route, $attributes), $permanent ? 301 : 302);
5648
}
5749

5850
/**
@@ -72,17 +64,9 @@ public function redirectAction($route, $permanent = false)
7264
public function urlRedirectAction($url, $permanent = false)
7365
{
7466
if (!$url) {
75-
$response = $this->container->get('response');
76-
$response->setStatusCode(410);
77-
78-
return $response;
67+
return new Response(null, 410);
7968
}
8069

81-
$code = $permanent ? 301 : 302;
82-
83-
$response = $this->container->get('response');
84-
$response->setRedirect($url, $code);
85-
86-
return $response;
70+
return Response::createRedirect($url, $permanent ? 301 : 302);
8771
}
8872
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<parameters>
88
<parameter key="event_dispatcher.class">Symfony\Bundle\FrameworkBundle\EventDispatcher</parameter>
99
<parameter key="http_kernel.class">Symfony\Bundle\FrameworkBundle\HttpKernel</parameter>
10-
<parameter key="response.class">Symfony\Component\HttpFoundation\Response</parameter>
1110
<parameter key="error_handler.class">Symfony\Component\HttpKernel\Debug\ErrorHandler</parameter>
1211
<parameter key="error_handler.level">null</parameter>
1312
<parameter key="filesystem.class">Symfony\Bundle\FrameworkBundle\Util\Filesystem</parameter>
@@ -49,12 +48,6 @@
4948
-->
5049
<service id="request" scope="request" synthetic="true" />
5150

52-
<service id="response" class="%response.class%" scope="prototype">
53-
<call method="setCharset">
54-
<argument>%kernel.charset%</argument>
55-
</call>
56-
</service>
57-
5851
<service id="filesystem" class="%filesystem.class%"></service>
5952

6053
<service id="file_locator" class="%file_locator.class%">

src/Symfony/Bundle/FrameworkBundle/Templating/DelegatingEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected function getEngine($name)
8484
public function renderResponse($view, array $parameters = array(), Response $response = null)
8585
{
8686
if (null === $response) {
87-
$response = $this->container->get('response');
87+
$response = new Response();
8888
}
8989

9090
$response->setContent($this->render($view, $parameters));

src/Symfony/Bundle/FrameworkBundle/Templating/PhpEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function setHelpers(array $helpers)
7777
public function renderResponse($view, array $parameters = array(), Response $response = null)
7878
{
7979
if (null === $response) {
80-
$response = $this->container->get('response');
80+
$response = new Response();
8181
}
8282

8383
$response->setContent($this->render($view, $parameters));

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/RedirectControllerTest.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,14 @@
2020
use Symfony\Bundle\FrameworkBundle\Tests\Logger;
2121
use Symfony\Bundle\FrameworkBundle\Tests\Kernel;
2222

23-
24-
2523
/**
26-
*
2724
* @author Marcin Sikon<marcin.sikon@gmail.com>
2825
*/
2926
class RedirectControllerTest extends TestCase
3027
{
3128
public function testEmptyRoute()
3229
{
33-
$response = new Response();
34-
3530
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
36-
$container
37-
->expects($this->once())
38-
->method('get')
39-
->with($this->equalTo('response'))
40-
->will($this->returnValue($response))
41-
;
4231

4332
$controller = new RedirectController();
4433
$controller->setContainer($container);
@@ -50,21 +39,17 @@ public function testEmptyRoute()
5039
$this->assertEquals(410, $returnResponse->getStatusCode());
5140
}
5241

53-
54-
5542
/**
5643
* @dataProvider provider
5744
*/
5845
public function testRoute($permanent, $expectedCode)
5946
{
60-
$response = new Response();
6147
$request = new Request();
6248

6349
$route = 'new-route';
6450
$url = '/redirect-url';
6551
$params = array('additional-parameter' => 'value');
6652

67-
6853
$request->attributes = new ParameterBag(array('route' => $route, '_route' => 'current-route', 'permanent' => $permanent) + $params);
6954

7055
$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
@@ -86,37 +71,26 @@ public function testRoute($permanent, $expectedCode)
8671
$container
8772
->expects($this->at(1))
8873
->method('get')
89-
->with($this->equalTo('response'))
90-
->will($this->returnValue($response));
91-
92-
$container
93-
->expects($this->at(2))
94-
->method('get')
9574
->with($this->equalTo('router'))
9675
->will($this->returnValue($router));
9776

98-
9977
$controller = new RedirectController();
10078
$controller->setContainer($container);
10179

10280
$returnResponse = $controller->redirectAction($route, $permanent);
10381

104-
10582
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $returnResponse);
10683

10784
$this->assertTrue($returnResponse->isRedirect());
10885
$this->assertTrue($returnResponse->isRedirected($url));
10986
$this->assertEquals($expectedCode, $returnResponse->getStatusCode());
11087
}
11188

112-
113-
11489
public function provider()
11590
{
11691
return array(
11792
array(true, 301),
11893
array(false, 302),
11994
);
12095
}
121-
12296
}

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
<service id="templating.engine.twig" class="%templating.engine.twig.class%" public="false">
3131
<argument type="service" id="twig" />
32-
<argument type="service" id="service_container" />
3332
<argument type="service" id="templating.name_parser" />
3433
<argument type="service" id="twig.globals" />
3534
</service>

src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testEvalutateAddsAppGlobal()
2525
{
2626
$environment = $this->getTwigEnvironment();
2727
$container = $this->getContainer();
28-
$engine = new TwigEngine($environment, $container, new TemplateNameParser(), $app = new GlobalVariables($container));
28+
$engine = new TwigEngine($environment, new TemplateNameParser(), $app = new GlobalVariables($container));
2929

3030
$template = $this->getMock('\Twig_TemplateInterface');
3131

@@ -44,7 +44,7 @@ public function testEvalutateWithoutAvailableRequest()
4444
{
4545
$environment = $this->getTwigEnvironment();
4646
$container = new Container();
47-
$engine = new TwigEngine($environment, $container, new TemplateNameParser(), new GlobalVariables($container));
47+
$engine = new TwigEngine($environment, new TemplateNameParser(), new GlobalVariables($container));
4848

4949
$template = $this->getMock('\Twig_TemplateInterface');
5050

src/Symfony/Bundle/TwigBundle/TwigEngine.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,18 @@
2424
class TwigEngine implements EngineInterface
2525
{
2626
protected $environment;
27-
protected $container;
2827
protected $parser;
2928

3029
/**
3130
* Constructor.
3231
*
3332
* @param \Twig_Environment $environment A \Twig_Environment instance
34-
* @param ContainerInterface $container The DI container
3533
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
3634
* @param GlobalVariables $globals A GlobalVariables instance
3735
*/
38-
public function __construct(\Twig_Environment $environment, ContainerInterface $container, TemplateNameParserInterface $parser, GlobalVariables $globals)
36+
public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser, GlobalVariables $globals)
3937
{
4038
$this->environment = $environment;
41-
$this->container = $container;
4239
$this->parser = $parser;
4340

4441
$environment->addGlobal('app', $globals);
@@ -108,7 +105,7 @@ public function supports($name)
108105
public function renderResponse($view, array $parameters = array(), Response $response = null)
109106
{
110107
if (null === $response) {
111-
$response = $this->container->get('response');
108+
$response = new Response();
112109
}
113110

114111
$response->setContent($this->render($view, $parameters));

src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,10 @@ public function exportAction($token)
7070
throw new NotFoundHttpException(sprintf('Token "%s" does not exist.', $token));
7171
}
7272

73-
$response = $this->container->get('response');
74-
$response->setContent($profiler->export());
75-
$response->headers->set('Content-Type', 'text/plain');
76-
$response->headers->set('Content-Disposition', 'attachment; filename= '.$token.'.txt');
77-
78-
return $response;
73+
return new Response($profiler->export(), 200, array(
74+
'Content-Type' => 'text/plain',
75+
'Content-Disposition' => 'attachment; filename= '.$token.'.txt',
76+
));
7977
}
8078

8179
/**
@@ -89,10 +87,7 @@ public function purgeAction()
8987
$profiler->disable();
9088
$profiler->purge();
9189

92-
$response = $this->container->get('response');
93-
$response->setRedirect($this->container->get('router')->generate('_profiler', array('token' => '-')));
94-
95-
return $response;
90+
return Response::createRedirect($this->container->get('router')->generate('_profiler', array('token' => '-')));
9691
}
9792

9893
/**
@@ -116,10 +111,7 @@ public function importAction()
116111
throw new \RuntimeException('Problem uploading the data (token already exists).');
117112
}
118113

119-
$response = $this->container->get('response');
120-
$response->setRedirect($this->container->get('router')->generate('_profiler', array('token' => $token)));
121-
122-
return $response;
114+
return Response::createRedirect($this->container->get('router')->generate('_profiler', array('token' => $token)));
123115
}
124116

125117
/**
@@ -133,7 +125,7 @@ public function importAction()
133125
public function toolbarAction($token, $position = null)
134126
{
135127
if (null === $token) {
136-
return $this->container->get('response');
128+
return new Response();
137129
}
138130

139131
$profiler = $this->container->get('profiler');
@@ -142,7 +134,7 @@ public function toolbarAction($token, $position = null)
142134
$profiler = $profiler->loadFromToken($token);
143135

144136
if ($profiler->isEmpty()) {
145-
return $this->container->get('response');
137+
return new Response();
146138
}
147139

148140
if (null === $position) {
@@ -228,10 +220,7 @@ public function searchAction()
228220
$request = $this->container->get('request');
229221

230222
if ($token = $request->query->get('token')) {
231-
$response = $this->container->get('response');
232-
$response->setRedirect($this->container->get('router')->generate('_profiler', array('token' => $token)));
233-
234-
return $response;
223+
return Response::createRedirect($this->container->get('router')->generate('_profiler', array('token' => $token)));
235224
}
236225

237226
$session = $request->getSession();
@@ -243,10 +232,7 @@ public function searchAction()
243232
$profiler->disable();
244233
$tokens = $profiler->find($ip, $url, $limit);
245234

246-
$response = $this->container->get('response');
247-
$response->setRedirect($this->container->get('router')->generate('_profiler_search_results', array('token' => $tokens ? $tokens[0]['token'] : '')));
248-
249-
return $response;
235+
return Response::createRedirect($this->container->get('router')->generate('_profiler_search_results', array('token' => $tokens ? $tokens[0]['token'] : '')));
250236
}
251237

252238
protected function getTemplateNames($profiler)

0 commit comments

Comments
 (0)