From 946bfb218023c999c3f9239d3dae6d30765e08a1 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 21 Mar 2013 08:03:05 +0100 Subject: [PATCH] [Debug] made the exception handler independant of HttpFoundation --- .../Component/Debug/ExceptionHandler.php | 37 ++++++++++++++++++- src/Symfony/Component/Debug/README.md | 3 ++ src/Symfony/Component/Debug/composer.json | 8 +++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Debug/ExceptionHandler.php b/src/Symfony/Component/Debug/ExceptionHandler.php index 1b4ba2ea33..903ae686ca 100644 --- a/src/Symfony/Component/Debug/ExceptionHandler.php +++ b/src/Symfony/Component/Debug/ExceptionHandler.php @@ -57,13 +57,46 @@ class ExceptionHandler } /** - * Sends a Response for the given Exception. + * Sends a response for the given Exception. + * + * If you have the Symfony HttpFoundation component installed, + * this method will use it to create and send the response. If not, + * it will fallback to plain PHP functions. * * @param \Exception $exception An \Exception instance + * + * @see sendPhpResponse + * @see createResponse */ public function handle(\Exception $exception) { - $this->createResponse($exception)->send(); + if (class_exists('Symfony\Component\HttpFoundation\Response')) { + $this->createResponse($exception)->send(); + } else { + $this->sendPhpResponse($exception); + } + } + + /** + * Sends the error associated with the given Exception as a plain PHP response. + * + * This method uses plain PHP functions like header() and echo to output + * the response. + * + * @param \Exception|FlattenException $exception An \Exception instance + */ + public function sendPhpResponse($exception) + { + if (!$exception instanceof FlattenException) { + $exception = FlattenException::create($exception); + } + + header(sprintf('HTTP/1.0 %s', $exception->getStatusCode())); + foreach ($exception->getHeaders() as $name => $value) { + header($name.': '.$value, false); + } + + echo $this->decorate($this->getContent($exception), $this->getStylesheet($exception)); } /** diff --git a/src/Symfony/Component/Debug/README.md b/src/Symfony/Component/Debug/README.md index 7b0f6af722..c230d8e7b3 100644 --- a/src/Symfony/Component/Debug/README.md +++ b/src/Symfony/Component/Debug/README.md @@ -27,6 +27,9 @@ You can also use the tools individually: Not that the `Debug::enable()` call also registers the debug class loader from the Symfony ClassLoader component when available. +This component can optionally take advantage of the features of the HttpKernel +and HttpFoundation components. + Resources --------- diff --git a/src/Symfony/Component/Debug/composer.json b/src/Symfony/Component/Debug/composer.json index 3c10e8514a..91d704009a 100644 --- a/src/Symfony/Component/Debug/composer.json +++ b/src/Symfony/Component/Debug/composer.json @@ -18,9 +18,15 @@ "require": { "php": ">=5.3.3" }, + "require-dev": { + "symfony/http-kernel": "2.2.*", + "symfony/http-foundation": "2.2.*" + }, "suggest": { + "symfony/http-foundation": "2.2.*", + "symfony/http-kernel": "2.2.*", "symfony/class-loader": "2.2.*" - } + }, "autoload": { "psr-0": { "Symfony\\Component\\Debug\\": "" } },