Преглед на файлове

removed the status message from HttpException, changed the signature so that most useful arguments are first, fixed many small problems introduced with previous HTTP exception refactoring

Quote from HTTP (bis) spec (Part 2 - 5.1.1):

The Reason Phrase exists for the
sole purpose of providing a textual description associated with the
numeric status code, out of deference to earlier Internet application
protocols that were more frequently used with interactive text
clients.  A client SHOULD ignore the content of the Reason Phrase.
Fabien Potencier преди 14 години
родител
ревизия
662a4b3740

+ 5 - 7
src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionController.php

@@ -29,13 +29,10 @@ class ExceptionController extends ContainerAware
      * @param FlattenException     $exception A FlattenException instance
      * @param DebugLoggerInterface $logger    A DebugLoggerInterface instance
      * @param string               $format    The format to use for rendering (html, xml, ...)
-     * @param integer              $code      An HTTP response code
-     * @param string               $message   An HTTP response status message
-     * @param array                $headers   HTTP response headers
      *
      * @throws \InvalidArgumentException When the exception template does not exist
      */
-    public function showAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html', $code = 500, $message = null, array $headers = array())
+    public function showAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html')
     {
         $this->container->get('request')->setRequestFormat($format);
 
@@ -60,19 +57,20 @@ class ExceptionController extends ContainerAware
             $template = 'FrameworkBundle:Exception:'.$name.'.html.twig';
         }
 
+        $code = $exception->getStatusCode();
         $response = $templating->renderResponse(
             $template,
             array(
                 'status_code'    => $code,
-                'status_text'    => $message ?: Response::$statusTexts[$code],
+                'status_text'    => Response::$statusTexts[$code],
                 'exception'      => $exception,
                 'logger'         => $logger,
                 'currentContent' => $currentContent,
             )
         );
 
-        $response->setStatusCode($code, $message);
-        $response->headers->replace($headers);
+        $response->setStatusCode($code);
+        $response->headers->replace($exception->getHeaders());
 
         return $response;
     }

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/RequestListener.php

@@ -104,7 +104,7 @@ class RequestListener
             if (null !== $this->logger) {
                 $this->logger->err($message);
             }
-            throw new NotFoundHttpException('Not Found', $message, 0, $e);
+            throw new NotFoundHttpException($message, $e);
         } catch (MethodNotAllowedException $e) {
             $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getPathInfo(), strtoupper(implode(', ', $e->getAllowedMethods())));
             if (null !== $this->logger) {

+ 1 - 1
src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php

@@ -30,7 +30,7 @@ class ExceptionController extends BaseExceptionController
     public function showAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html')
     {
         $template = $this->container->get('kernel')->isDebug() ? 'exception' : 'error';
-        $code = $this->getStatusCode($exception);
+        $code = $exception->getStatusCode();
 
         return $this->container->get('templating')->renderResponse(
             'FrameworkBundle:Exception:'.$template.'.html.twig',

+ 17 - 1
src/Symfony/Component/HttpKernel/DataCollector/ExceptionDataCollector.php

@@ -14,6 +14,7 @@ namespace Symfony\Component\HttpKernel\DataCollector;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\Exception\FlattenException;
+use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
 
 /**
  * ExceptionDataCollector.
@@ -28,8 +29,13 @@ class ExceptionDataCollector extends DataCollector
     public function collect(Request $request, Response $response, \Exception $exception = null)
     {
         if (null !== $exception) {
+            $flattenException = FlattenException::create($exception);
+            if ($exception instanceof HttpExceptionInterface) {
+                $flattenException->setStatusCode($exception->getStatusCode());
+            }
+
             $this->data = array(
-                'exception' => FlattenException::create($exception),
+                'exception' => $flattenException,
             );
         }
     }
@@ -74,6 +80,16 @@ class ExceptionDataCollector extends DataCollector
         return $this->data['exception']->getCode();
     }
 
+    /**
+     * Gets the status code.
+     *
+     * @return integer The status code
+     */
+    public function getStatusCode()
+    {
+        return $this->data['exception']->getStatusCode();
+    }
+
     /**
      * Gets the exception trace.
      *

+ 7 - 11
src/Symfony/Component/HttpKernel/Debug/ExceptionListener.php

@@ -56,24 +56,20 @@ class ExceptionListener
 
         $logger = null !== $this->logger ? $this->logger->getDebugLogger() : null;
 
+        $flattenException = FlattenException::create($exception);
+        if ($exception instanceof HttpExceptionInterface) {
+            $flattenException->setStatusCode($exception->getStatusCode());
+            $flattenException->setHeaders($exception->getHeaders());
+        }
+
         $attributes = array(
             '_controller' => $this->controller,
-            'exception'   => FlattenException::create($exception),
+            'exception'   => $flattenException,
             'logger'      => $logger,
             // when using CLI, we force the format to be TXT
             'format'      => 0 === strncasecmp(PHP_SAPI, 'cli', 3) ? 'txt' : $request->getRequestFormat(),
         );
 
-        $attributes += $exception instanceof HttpExceptionInterface ? array(
-            'code'    => $exception->getStatusCode(),
-            'message' => $exception->getStatusMessage(),
-            'headers' => $exception->getHeaders(),
-        ) : array(
-            'code'    => 500,
-            'message' => 'Internal Server Error',
-            'headers' => array(),
-        );
-
         $request = $request->duplicate(null, null, $attributes);
 
         try {

+ 34 - 10
src/Symfony/Component/HttpKernel/Exception/FlattenException.php

@@ -20,17 +20,21 @@ namespace Symfony\Component\HttpKernel\Exception;
  */
 class FlattenException
 {
-    protected $message;
-    protected $code;
-    protected $previous;
-    protected $trace;
-    protected $class;
-
-    static public function create(\Exception $exception)
+    private $message;
+    private $code;
+    private $previous;
+    private $trace;
+    private $class;
+    private $statusCode;
+    private $headers;
+
+    static public function create(\Exception $exception, $statusCode = 500, array $headers = array())
     {
         $e = new static();
         $e->setMessage($exception->getMessage());
         $e->setCode($exception->getCode());
+        $e->setStatusCode($statusCode);
+        $e->setHeaders($headers);
         $e->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine());
         $e->setClass(get_class($exception));
         if ($exception->getPrevious()) {
@@ -45,15 +49,35 @@ class FlattenException
         $exceptions = array();
         foreach (array_merge(array($this), $this->getPreviouses()) as $exception) {
             $exceptions[] = array(
-                'message'  => $exception->getMessage(),
-                'class'    => $exception->getClass(),
-                'trace'    => $exception->getTrace(),
+                'message' => $exception->getMessage(),
+                'class'   => $exception->getClass(),
+                'trace'   => $exception->getTrace(),
             );
         }
 
         return $exceptions;
     }
 
+    public function getStatusCode()
+    {
+        return $this->statusCode;
+    }
+
+    public function setStatusCode($code)
+    {
+        $this->statusCode = $code;
+    }
+
+    public function getHeaders()
+    {
+        return $this->headers;
+    }
+
+    public function setHeaders(array $headers)
+    {
+        $this->headers = $headers;
+    }
+
     public function getClass()
     {
         return $this->class;

+ 1 - 8
src/Symfony/Component/HttpKernel/Exception/HttpException.php

@@ -19,13 +19,11 @@ namespace Symfony\Component\HttpKernel\Exception;
 class HttpException extends \RuntimeException implements HttpExceptionInterface
 {
     protected $statusCode;
-    protected $statusMessage;
     protected $headers;
 
-    public function __construct($statusCode, $statusMessage, array $headers = array(), $message = null, $code = 0, \Exception $previous = null)
+    public function __construct($statusCode, $message = null, \Exception $previous = null, array $headers = array(), $code = 0)
     {
         $this->statusCode = $statusCode;
-        $this->statusMessage = $statusMessage;
         $this->headers = $headers;
 
         parent::__construct($message, 0, $previous);
@@ -36,11 +34,6 @@ class HttpException extends \RuntimeException implements HttpExceptionInterface
         return $this->statusCode;
     }
 
-    public function getStatusMessage()
-    {
-        return $this->statusMessage;
-    }
-
     public function getHeaders()
     {
         return $this->headers;

+ 0 - 7
src/Symfony/Component/HttpKernel/Exception/HttpExceptionInterface.php

@@ -25,13 +25,6 @@ interface HttpExceptionInterface
      */
     function getStatusCode();
 
-    /**
-     * Return the status message.
-     *
-     * @return string An HTTP response status message
-     */
-    function getStatusMessage();
-
     /**
      * Returns response headers.
      *

+ 3 - 7
src/Symfony/Component/HttpKernel/Exception/MethodNotAllowedHttpException.php

@@ -21,19 +21,15 @@ class MethodNotAllowedHttpException extends HttpException
     /**
      * Constructor.
      *
-     * WARNING: The status message will be sent as a response header
-     * regardless of debug mode.
-     *
      * @param array     $allow         An array of allowed methods
-     * @param string    $statusMessage The HTTP response status message
      * @param string    $message       The internal exception message
-     * @param integer   $code          The internal exception code
      * @param Exception $previous      The previous exception
+     * @param integer   $code          The internal exception code
      */
-    public function __construct(array $allow, $statusMessage = 'Method Not Allowed', $message = null, $code = 0, \Exception $previous = null)
+    public function __construct(array $allow, $message = null, \Exception $previous = null, $code = 0)
     {
         $headers = array('Allow' => strtoupper(implode(', ', $allow)));
 
-        parent::__construct(405, $statusMessage, $headers, $message ?: $statusMessage, $code, $previous);
+        parent::__construct(405, $message, $previous, $headers, $code);
     }
 }

+ 5 - 9
src/Symfony/Component/HttpKernel/Exception/NotFoundHttpException.php

@@ -21,16 +21,12 @@ class NotFoundHttpException extends HttpException
     /**
      * Constructor.
      *
-     * WARNING: The status message will be sent as a response header
-     * regardless of debug mode.
-     *
-     * @param string    $statusMessage The HTTP response status message
-     * @param string    $message       The internal exception message
-     * @param integer   $code          The internal exception code
-     * @param Exception $previous      The previous exception
+     * @param string    $message  The internal exception message
+     * @param Exception $previous The previous exception
+     * @param integer   $code     The internal exception code
      */
-    public function __construct($statusMessage = 'Not Found', $message = null, $code = 0, \Exception $previous = null)
+    public function __construct($message = null, \Exception $previous = null, $code = 0)
     {
-        parent::__construct(404, $statusMessage, array(), $message ?: $statusMessage, $code, $previous);
+        parent::__construct(404, $message, $previous, array(), $code);
     }
 }