From 93efe9976c8c12c213baaac68e711a9e903d3b43 Mon Sep 17 00:00:00 2001 From: Dima Date: Mon, 7 Nov 2016 15:57:26 +0200 Subject: [PATCH] Prepend the content type handler instead of appending it #2 --- src/Whoops.php | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Whoops.php b/src/Whoops.php index 2916536..bdf8f6c 100644 --- a/src/Whoops.php +++ b/src/Whoops.php @@ -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); } }