[Debug] made the exception handler independant of HttpFoundation

This commit is contained in:
Fabien Potencier 2013-03-21 08:03:05 +01:00
parent 2b305c21d8
commit 946bfb2180
3 changed files with 45 additions and 3 deletions

View File

@ -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));
}
/**

View File

@ -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
---------

View File

@ -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\\": "" }
},