Skip to content

Commit

Permalink
Prepend the content type handler instead of appending it #2
Browse files Browse the repository at this point in the history
  • Loading branch information
kerneldemon committed Nov 7, 2016
1 parent 0fd6b51 commit 93efe99
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/Whoops.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,35 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
*/
protected function pushHandlerByContentType($contentType)
{
$contentTypeBasedHandler = null;
switch ($contentType) {
case 'application/json':
$this->whoops->pushHandler(new JsonResponseHandler());
$contentTypeBasedHandler = new JsonResponseHandler();
break;
case 'text/xml':
case 'application/xml':
$this->whoops->pushHandler(new XmlResponseHandler());
$contentTypeBasedHandler = new XmlResponseHandler();
break;
case 'text/html':
$this->whoops->pushHandler(new PrettyPageHandler());
$contentTypeBasedHandler = new PrettyPageHandler();
break;
default:
return;
}

$this->prependHandler($contentTypeBasedHandler);
}

/**
* @param Callable|HandlerInterface $handler
*/
private function prependHandler($handler)
{
$existingHandlers = array_merge([$handler], $this->whoops->getHandlers());
$this->whoops->clearHandlers();

foreach ($existingHandlers as $existingHandler) {
$this->whoops->pushHandler($existingHandler);
}
}

Expand Down

0 comments on commit 93efe99

Please sign in to comment.