Browse Source

replaced Response::createRedirect by a new RedirectResponse class

Fabien Potencier 14 năm trước cách đây
mục cha
commit
353177d1d6

+ 3 - 2
src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php

@@ -13,6 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\Controller;
 
 use Symfony\Component\DependencyInjection\ContainerAware;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\RedirectResponse;
 
 /**
  *
@@ -44,7 +45,7 @@ class RedirectController extends ContainerAware
         $attributes = $this->container->get('request')->attributes->all();
         unset($attributes['_route'], $attributes['route'], $attributes['permanent'] );
 
-        return Response::createRedirect($this->container->get('router')->generate($route, $attributes), $permanent ? 301 : 302);
+        return new RedirectResponse($this->container->get('router')->generate($route, $attributes), $permanent ? 301 : 302);
     }
 
     /**
@@ -67,6 +68,6 @@ class RedirectController extends ContainerAware
             return new Response(null, 410);
         }
 
-        return Response::createRedirect($url, $permanent ? 301 : 302);
+        return new RedirectResponse($url, $permanent ? 301 : 302);
     }
 }

+ 5 - 4
src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php

@@ -13,6 +13,7 @@ namespace Symfony\Bundle\WebProfilerBundle\Controller;
 
 use Symfony\Component\DependencyInjection\ContainerAware;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\RedirectResponse;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 
 /**
@@ -87,7 +88,7 @@ class ProfilerController extends ContainerAware
         $profiler->disable();
         $profiler->purge();
 
-        return Response::createRedirect($this->container->get('router')->generate('_profiler', array('token' => '-')));
+        return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => '-')));
     }
 
     /**
@@ -111,7 +112,7 @@ class ProfilerController extends ContainerAware
             throw new \RuntimeException('Problem uploading the data (token already exists).');
         }
 
-        return Response::createRedirect($this->container->get('router')->generate('_profiler', array('token' => $token)));
+        return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => $token)));
     }
 
     /**
@@ -220,7 +221,7 @@ class ProfilerController extends ContainerAware
         $request = $this->container->get('request');
 
         if ($token = $request->query->get('token')) {
-            return Response::createRedirect($this->container->get('router')->generate('_profiler', array('token' => $token)));
+            return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => $token)));
         }
 
         $session = $request->getSession();
@@ -232,7 +233,7 @@ class ProfilerController extends ContainerAware
         $profiler->disable();
         $tokens = $profiler->find($ip, $url, $limit);
 
-        return Response::createRedirect($this->container->get('router')->generate('_profiler_search_results', array('token' => $tokens ? $tokens[0]['token'] : '')));
+        return new RedirectResponse($this->container->get('router')->generate('_profiler_search_results', array('token' => $tokens ? $tokens[0]['token'] : '')));
     }
 
     protected function getTemplateNames($profiler)

+ 45 - 0
src/Symfony/Component/HttpFoundation/RedirectResponse.php

@@ -0,0 +1,45 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\HttpFoundation;
+
+/**
+ * RedirectResponse represents an HTTP response doing a redirect.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class RedirectResponse extends Response
+{
+    /**
+     * Creates a redirect response so that it conforms to the rules defined for a redirect status code.
+     *
+     * @param string  $url    The URL to redirect to
+     * @param integer $status The status code (302 by default)
+     *
+     * @see http://tools.ietf.org/html/rfc2616#section-10.3.5
+     */
+    public function __construct($url, $status = 302)
+    {
+        if (empty($url)) {
+            throw new \InvalidArgumentException('Cannot redirect to an empty URL.');
+        }
+
+        parent::__construct(
+            sprintf('<html><head><meta http-equiv="refresh" content="1;url=%s"/></head></html>', htmlspecialchars($url, ENT_QUOTES)),
+            $status,
+            array('Location' => $url)
+        );
+
+        if (!$this->isRedirect()) {
+            throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status));
+        }
+    }
+}

+ 0 - 24
src/Symfony/Component/HttpFoundation/Response.php

@@ -626,30 +626,6 @@ class Response
         }
     }
 
-    /**
-     * 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
-     */
-    static public function createRedirect($url, $status = 302)
-    {
-        if (empty($url)) {
-            throw new \InvalidArgumentException('Cannot redirect to an empty URL.');
-        }
-
-        $response = new static(
-            sprintf('<html><head><meta http-equiv="refresh" content="1;url=%s"/></head></html>', 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));
-        }
-
-        return $response;
-    }
-
     /**
      * Returns true if the response includes a Vary header.
      *

+ 2 - 1
src/Symfony/Component/Security/Http/EntryPoint/FormAuthenticationEntryPoint.php

@@ -14,6 +14,7 @@ namespace Symfony\Component\Security\Http\EntryPoint;
 use Symfony\Component\EventDispatcher\EventInterface;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\RedirectResponse;
 use Symfony\Component\Security\Core\Exception\AuthenticationException;
 use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -49,6 +50,6 @@ class FormAuthenticationEntryPoint implements AuthenticationEntryPointInterface
             return $event->getSubject()->handle(Request::create($this->loginPath), HttpKernelInterface::SUB_REQUEST);
         }
 
-        return Response::createRedirect(0 !== strpos($this->loginPath, 'http') ? $request->getUriForPath($this->loginPath) : $this->loginPath, 302);
+        return new RedirectResponse(0 !== strpos($this->loginPath, 'http') ? $request->getUriForPath($this->loginPath) : $this->loginPath, 302);
     }
 }

+ 2 - 1
src/Symfony/Component/Security/Http/EntryPoint/RetryAuthenticationEntryPoint.php

@@ -15,6 +15,7 @@ use Symfony\Component\EventDispatcher\EventInterface;
 use Symfony\Component\Security\Core\Exception\AuthenticationException;
 use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\RedirectResponse;
 use Symfony\Component\HttpFoundation\Request;
 
 /**
@@ -53,6 +54,6 @@ class RetryAuthenticationEntryPoint implements AuthenticationEntryPointInterface
 
         $url = $scheme.'://'.$request->getHost().$port.$request->getScriptName().$request->getPathInfo().$qs;
 
-        return Response::createRedirect($url, 301);
+        return new RedirectResponse($url, 301);
     }
 }

+ 3 - 2
src/Symfony/Component/Security/Http/Firewall/AbstractAuthenticationListener.php

@@ -25,6 +25,7 @@ use Symfony\Component\Security\Core\Exception\AuthenticationException;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\RedirectResponse;
 use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
 
 /**
@@ -204,7 +205,7 @@ abstract class AbstractAuthenticationListener implements ListenerInterface
 
             $request->getSession()->set(SecurityContextInterface::AUTHENTICATION_ERROR, $failed);
 
-            return Response::createRedirect(0 !== strpos($this->options['failure_path'], 'http') ? $request->getUriForPath($this->options['failure_path']) : $this->options['failure_path'], 302);
+            return new RedirectResponse(0 !== strpos($this->options['failure_path'], 'http') ? $request->getUriForPath($this->options['failure_path']) : $this->options['failure_path'], 302);
         }
     }
 
@@ -228,7 +229,7 @@ abstract class AbstractAuthenticationListener implements ListenerInterface
             $response = $this->successHandler->onAuthenticationSuccess($event, $request, $token);
         } else {
             $path = $this->determineTargetUrl($request);
-            $response = Response::createRedirect(0 !== strpos($path, 'http') ? $request->getUriForPath($path) : $path, 302);
+            $response = new RedirectResponse(0 !== strpos($path, 'http') ? $request->getUriForPath($path) : $path, 302);
         }
 
         if (null !== $this->rememberMeServices) {

+ 2 - 1
src/Symfony/Component/Security/Http/Firewall/LogoutListener.php

@@ -18,6 +18,7 @@ use Symfony\Component\Security\Core\SecurityContextInterface;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 use Symfony\Component\EventDispatcher\EventInterface;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\RedirectResponse;
 
 /**
  * LogoutListener logout users.
@@ -97,7 +98,7 @@ class LogoutListener implements ListenerInterface
                 throw new \RuntimeException('Logout Success Handler did not return a Response.');
             }
         } else {
-            $response = Response::createRedirect(0 !== strpos($this->targetUrl, 'http') ? $request->getUriForPath($this->targetUrl) : $this->targetUrl, 302);
+            $response = new RedirectResponse(0 !== strpos($this->targetUrl, 'http') ? $request->getUriForPath($this->targetUrl) : $this->targetUrl, 302);
         }
 
         // handle multiple logout attempts gracefully

+ 2 - 1
src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php

@@ -21,6 +21,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 use Symfony\Component\EventDispatcher\EventInterface;
 use Symfony\Component\Security\Core\Exception\AuthenticationException;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\RedirectResponse;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\Security\Core\Role\SwitchUserRole;
 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
@@ -110,7 +111,7 @@ class SwitchUserListener implements ListenerInterface
         }
 
         $request->server->set('QUERY_STRING', '');
-        $response = Response::createRedirect($request->getUri(), 302);
+        $response = new RedirectResponse($request->getUri(), 302);
 
         $event->setProcessed();