diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php b/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php index 9a74fad31b73..bfb142508335 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php @@ -23,27 +23,6 @@ */ class Controller extends ContainerAware { - /** - * Creates a Response instance. - * - * @param string $content The Response body - * @param integer $status The status code - * @param array $headers An array of HTTP headers - * - * @return Response A Response instance - */ - public function createResponse($content = '', $status = 200, array $headers = array()) - { - $response = $this->container->get('response'); - $response->setContent($content); - $response->setStatusCode($status); - foreach ($headers as $name => $value) { - $response->headers->set($name, $value); - } - - return $response; - } - /** * Generates a URL from the given parameters. * @@ -72,18 +51,6 @@ public function forward($controller, array $path = array(), array $query = array return $this->container->get('http_kernel')->forward($controller, $path, $query); } - /** - * Returns an HTTP redirect Response. - * - * @return Response A Response instance - */ - public function redirect($url, $status = 302) - { - $response = $this->container->get('response'); - $response->setRedirect($url, $status); - return $response; - } - /** * Returns a rendered view. * diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php index d4dab53e2ffa..f850806d6929 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php @@ -38,21 +38,13 @@ class RedirectController extends ContainerAware public function redirectAction($route, $permanent = false) { if (!$route) { - $response = $this->container->get('response'); - $response->setStatusCode(410); - - return $response; + return new Response(null, 410); } - $code = $permanent ? 301 : 302; - $attributes = $this->container->get('request')->attributes->all(); unset($attributes['_route'], $attributes['route'], $attributes['permanent'] ); - $response = $this->container->get('response'); - $response->setRedirect($this->container->get('router')->generate($route, $attributes), $code); - - return $response; + return Response::createRedirect($this->container->get('router')->generate($route, $attributes), $permanent ? 301 : 302); } /** @@ -72,17 +64,9 @@ public function redirectAction($route, $permanent = false) public function urlRedirectAction($url, $permanent = false) { if (!$url) { - $response = $this->container->get('response'); - $response->setStatusCode(410); - - return $response; + return new Response(null, 410); } - $code = $permanent ? 301 : 302; - - $response = $this->container->get('response'); - $response->setRedirect($url, $code); - - return $response; + return Response::createRedirect($url, $permanent ? 301 : 302); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml index 8b78ccd90182..7c94262d0b23 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml @@ -7,7 +7,6 @@ Symfony\Bundle\FrameworkBundle\EventDispatcher Symfony\Bundle\FrameworkBundle\HttpKernel - Symfony\Component\HttpFoundation\Response Symfony\Component\HttpKernel\Debug\ErrorHandler null Symfony\Bundle\FrameworkBundle\Util\Filesystem @@ -49,12 +48,6 @@ --> - - - %kernel.charset% - - - diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/DelegatingEngine.php b/src/Symfony/Bundle/FrameworkBundle/Templating/DelegatingEngine.php index 888583446292..cc83b6a655e7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/DelegatingEngine.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/DelegatingEngine.php @@ -84,7 +84,7 @@ protected function getEngine($name) public function renderResponse($view, array $parameters = array(), Response $response = null) { if (null === $response) { - $response = $this->container->get('response'); + $response = new Response(); } $response->setContent($this->render($view, $parameters)); diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/PhpEngine.php b/src/Symfony/Bundle/FrameworkBundle/Templating/PhpEngine.php index 0ff1c6711ff3..90f4bf65354b 100755 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/PhpEngine.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/PhpEngine.php @@ -77,7 +77,7 @@ public function setHelpers(array $helpers) public function renderResponse($view, array $parameters = array(), Response $response = null) { if (null === $response) { - $response = $this->container->get('response'); + $response = new Response(); } $response->setContent($this->render($view, $parameters)); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/RedirectControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/RedirectControllerTest.php index 5f0d3a06a8d1..a7ba26f1e55f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/RedirectControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/RedirectControllerTest.php @@ -20,25 +20,14 @@ use Symfony\Bundle\FrameworkBundle\Tests\Logger; use Symfony\Bundle\FrameworkBundle\Tests\Kernel; - - /** - * * @author Marcin Sikon */ class RedirectControllerTest extends TestCase { public function testEmptyRoute() { - $response = new Response(); - $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); - $container - ->expects($this->once()) - ->method('get') - ->with($this->equalTo('response')) - ->will($this->returnValue($response)) - ; $controller = new RedirectController(); $controller->setContainer($container); @@ -50,21 +39,17 @@ public function testEmptyRoute() $this->assertEquals(410, $returnResponse->getStatusCode()); } - - /** * @dataProvider provider */ public function testRoute($permanent, $expectedCode) { - $response = new Response(); $request = new Request(); $route = 'new-route'; $url = '/redirect-url'; $params = array('additional-parameter' => 'value'); - $request->attributes = new ParameterBag(array('route' => $route, '_route' => 'current-route', 'permanent' => $permanent) + $params); $router = $this->getMock('Symfony\Component\Routing\RouterInterface'); @@ -86,22 +71,14 @@ public function testRoute($permanent, $expectedCode) $container ->expects($this->at(1)) ->method('get') - ->with($this->equalTo('response')) - ->will($this->returnValue($response)); - - $container - ->expects($this->at(2)) - ->method('get') ->with($this->equalTo('router')) ->will($this->returnValue($router)); - $controller = new RedirectController(); $controller->setContainer($container); $returnResponse = $controller->redirectAction($route, $permanent); - $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $returnResponse); $this->assertTrue($returnResponse->isRedirect()); @@ -109,8 +86,6 @@ public function testRoute($permanent, $expectedCode) $this->assertEquals($expectedCode, $returnResponse->getStatusCode()); } - - public function provider() { return array( @@ -118,5 +93,4 @@ public function provider() array(false, 302), ); } - } diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index 251e31487b6a..8bff8774f343 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -29,7 +29,6 @@ - diff --git a/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php b/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php index ae4d1f5beb28..aecc643abe77 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php @@ -25,7 +25,7 @@ public function testEvalutateAddsAppGlobal() { $environment = $this->getTwigEnvironment(); $container = $this->getContainer(); - $engine = new TwigEngine($environment, $container, new TemplateNameParser(), $app = new GlobalVariables($container)); + $engine = new TwigEngine($environment, new TemplateNameParser(), $app = new GlobalVariables($container)); $template = $this->getMock('\Twig_TemplateInterface'); @@ -44,7 +44,7 @@ public function testEvalutateWithoutAvailableRequest() { $environment = $this->getTwigEnvironment(); $container = new Container(); - $engine = new TwigEngine($environment, $container, new TemplateNameParser(), new GlobalVariables($container)); + $engine = new TwigEngine($environment, new TemplateNameParser(), new GlobalVariables($container)); $template = $this->getMock('\Twig_TemplateInterface'); diff --git a/src/Symfony/Bundle/TwigBundle/TwigEngine.php b/src/Symfony/Bundle/TwigBundle/TwigEngine.php index e869b15c00d3..b47a47ea8cf6 100644 --- a/src/Symfony/Bundle/TwigBundle/TwigEngine.php +++ b/src/Symfony/Bundle/TwigBundle/TwigEngine.php @@ -24,21 +24,18 @@ class TwigEngine implements EngineInterface { protected $environment; - protected $container; protected $parser; /** * Constructor. * * @param \Twig_Environment $environment A \Twig_Environment instance - * @param ContainerInterface $container The DI container * @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance * @param GlobalVariables $globals A GlobalVariables instance */ - public function __construct(\Twig_Environment $environment, ContainerInterface $container, TemplateNameParserInterface $parser, GlobalVariables $globals) + public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser, GlobalVariables $globals) { $this->environment = $environment; - $this->container = $container; $this->parser = $parser; $environment->addGlobal('app', $globals); @@ -108,7 +105,7 @@ public function supports($name) public function renderResponse($view, array $parameters = array(), Response $response = null) { if (null === $response) { - $response = $this->container->get('response'); + $response = new Response(); } $response->setContent($this->render($view, $parameters)); diff --git a/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php b/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php index d74d59f0bfcc..26111163d227 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php @@ -70,12 +70,10 @@ public function exportAction($token) throw new NotFoundHttpException(sprintf('Token "%s" does not exist.', $token)); } - $response = $this->container->get('response'); - $response->setContent($profiler->export()); - $response->headers->set('Content-Type', 'text/plain'); - $response->headers->set('Content-Disposition', 'attachment; filename= '.$token.'.txt'); - - return $response; + return new Response($profiler->export(), 200, array( + 'Content-Type' => 'text/plain', + 'Content-Disposition' => 'attachment; filename= '.$token.'.txt', + )); } /** @@ -89,10 +87,7 @@ public function purgeAction() $profiler->disable(); $profiler->purge(); - $response = $this->container->get('response'); - $response->setRedirect($this->container->get('router')->generate('_profiler', array('token' => '-'))); - - return $response; + return Response::createRedirect($this->container->get('router')->generate('_profiler', array('token' => '-'))); } /** @@ -116,10 +111,7 @@ public function importAction() throw new \RuntimeException('Problem uploading the data (token already exists).'); } - $response = $this->container->get('response'); - $response->setRedirect($this->container->get('router')->generate('_profiler', array('token' => $token))); - - return $response; + return Response::createRedirect($this->container->get('router')->generate('_profiler', array('token' => $token))); } /** @@ -133,7 +125,7 @@ public function importAction() public function toolbarAction($token, $position = null) { if (null === $token) { - return $this->container->get('response'); + return new Response(); } $profiler = $this->container->get('profiler'); @@ -142,7 +134,7 @@ public function toolbarAction($token, $position = null) $profiler = $profiler->loadFromToken($token); if ($profiler->isEmpty()) { - return $this->container->get('response'); + return new Response(); } if (null === $position) { @@ -228,10 +220,7 @@ public function searchAction() $request = $this->container->get('request'); if ($token = $request->query->get('token')) { - $response = $this->container->get('response'); - $response->setRedirect($this->container->get('router')->generate('_profiler', array('token' => $token))); - - return $response; + return Response::createRedirect($this->container->get('router')->generate('_profiler', array('token' => $token))); } $session = $request->getSession(); @@ -243,10 +232,7 @@ public function searchAction() $profiler->disable(); $tokens = $profiler->find($ip, $url, $limit); - $response = $this->container->get('response'); - $response->setRedirect($this->container->get('router')->generate('_profiler_search_results', array('token' => $tokens ? $tokens[0]['token'] : ''))); - - return $response; + return Response::createRedirect($this->container->get('router')->generate('_profiler_search_results', array('token' => $tokens ? $tokens[0]['token'] : ''))); } protected function getTemplateNames($profiler) diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php index 6428d82b2b07..14376354ef3a 100644 --- a/src/Symfony/Component/HttpFoundation/Response.php +++ b/src/Symfony/Component/HttpFoundation/Response.php @@ -627,23 +627,27 @@ public function setNotModified() } /** - * Modifies the response so that it conforms to the rules defined for a redirect status code. + * Creates a redirect response so that it conforms to the rules defined for a redirect status code. * * @see http://tools.ietf.org/html/rfc2616#section-10.3.5 */ - public function setRedirect($url, $status = 302) + static public function createRedirect($url, $status = 302) { if (empty($url)) { throw new \InvalidArgumentException('Cannot redirect to an empty URL.'); } - $this->setStatusCode($status); - if (!$this->isRedirect()) { + $response = new static( + sprintf('', htmlspecialchars($url, ENT_QUOTES)), + $status, + array('Location' => $url) + ); + + if (!$response->isRedirect()) { throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status)); } - $this->headers->set('Location', $url); - $this->setContent(sprintf('', htmlspecialchars($url, ENT_QUOTES))); + return $response; } /** diff --git a/src/Symfony/Component/Security/Http/EntryPoint/FormAuthenticationEntryPoint.php b/src/Symfony/Component/Security/Http/EntryPoint/FormAuthenticationEntryPoint.php index 0993fa723124..5809fe8a0844 100644 --- a/src/Symfony/Component/Security/Http/EntryPoint/FormAuthenticationEntryPoint.php +++ b/src/Symfony/Component/Security/Http/EntryPoint/FormAuthenticationEntryPoint.php @@ -49,9 +49,6 @@ public function start(EventInterface $event, Request $request, AuthenticationExc return $event->getSubject()->handle(Request::create($this->loginPath), HttpKernelInterface::SUB_REQUEST); } - $response = new Response(); - $response->setRedirect(0 !== strpos($this->loginPath, 'http') ? $request->getUriForPath($this->loginPath) : $this->loginPath, 302); - - return $response; + return Response::createRedirect(0 !== strpos($this->loginPath, 'http') ? $request->getUriForPath($this->loginPath) : $this->loginPath, 302); } } diff --git a/src/Symfony/Component/Security/Http/EntryPoint/RetryAuthenticationEntryPoint.php b/src/Symfony/Component/Security/Http/EntryPoint/RetryAuthenticationEntryPoint.php index ed1297f45784..444535a66d94 100644 --- a/src/Symfony/Component/Security/Http/EntryPoint/RetryAuthenticationEntryPoint.php +++ b/src/Symfony/Component/Security/Http/EntryPoint/RetryAuthenticationEntryPoint.php @@ -53,9 +53,6 @@ public function start(EventInterface $event, Request $request, AuthenticationExc $url = $scheme.'://'.$request->getHost().$port.$request->getScriptName().$request->getPathInfo().$qs; - $response = new Response(); - $response->setRedirect($url, 301); - - return $response; + return Response::createRedirect($url, 301); } } diff --git a/src/Symfony/Component/Security/Http/Firewall/AbstractAuthenticationListener.php b/src/Symfony/Component/Security/Http/Firewall/AbstractAuthenticationListener.php index 46dbf6df5e14..2e6d36355b93 100644 --- a/src/Symfony/Component/Security/Http/Firewall/AbstractAuthenticationListener.php +++ b/src/Symfony/Component/Security/Http/Firewall/AbstractAuthenticationListener.php @@ -204,10 +204,7 @@ protected function onFailure($event, Request $request, AuthenticationException $ $request->getSession()->set(SecurityContextInterface::AUTHENTICATION_ERROR, $failed); - $response = new Response(); - $response->setRedirect(0 !== strpos($this->options['failure_path'], 'http') ? $request->getUriForPath($this->options['failure_path']) : $this->options['failure_path'], 302); - - return $response; + return Response::createRedirect(0 !== strpos($this->options['failure_path'], 'http') ? $request->getUriForPath($this->options['failure_path']) : $this->options['failure_path'], 302); } } @@ -230,9 +227,8 @@ protected function onSuccess(EventInterface $event, Request $request, TokenInter if (null !== $this->successHandler) { $response = $this->successHandler->onAuthenticationSuccess($event, $request, $token); } else { - $response = new Response(); $path = $this->determineTargetUrl($request); - $response->setRedirect(0 !== strpos($path, 'http') ? $request->getUriForPath($path) : $path, 302); + $response = Response::createRedirect(0 !== strpos($path, 'http') ? $request->getUriForPath($path) : $path, 302); } if (null !== $this->rememberMeServices) { diff --git a/src/Symfony/Component/Security/Http/Firewall/LogoutListener.php b/src/Symfony/Component/Security/Http/Firewall/LogoutListener.php index 4f809b5546fd..b2d1229ca0ce 100644 --- a/src/Symfony/Component/Security/Http/Firewall/LogoutListener.php +++ b/src/Symfony/Component/Security/Http/Firewall/LogoutListener.php @@ -97,8 +97,7 @@ public function handle(EventInterface $event) throw new \RuntimeException('Logout Success Handler did not return a Response.'); } } else { - $response = new Response(); - $response->setRedirect(0 !== strpos($this->targetUrl, 'http') ? $request->getUriForPath($this->targetUrl) : $this->targetUrl, 302); + $response = Response::createRedirect(0 !== strpos($this->targetUrl, 'http') ? $request->getUriForPath($this->targetUrl) : $this->targetUrl, 302); } // handle multiple logout attempts gracefully diff --git a/src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php b/src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php index 65cb428a8e11..098897353832 100644 --- a/src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php +++ b/src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php @@ -109,9 +109,8 @@ public function handle(EventInterface $event) } } - $response = new Response(); $request->server->set('QUERY_STRING', ''); - $response->setRedirect($request->getUri(), 302); + $response = Response::createRedirect($request->getUri(), 302); $event->setProcessed();