Skip to content

Commit

Permalink
Refactor ControllerBlueScreenExceptionListener to leverage Symfony Ke…
Browse files Browse the repository at this point in the history
…rnel
  • Loading branch information
simPod committed Mar 3, 2021
1 parent d9410d2 commit a25eaad
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions src/BlueScreen/ControllerBlueScreenExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Cdn77\TracyBlueScreenBundle\BlueScreen;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Throwable;
use Tracy\BlueScreen;

use function header;
use function headers_sent;
use function strpos;
use function assert;
use function ob_get_clean;
use function ob_start;

final class ControllerBlueScreenExceptionListener
{
Expand All @@ -25,27 +25,20 @@ public function __construct(BlueScreen $blueScreen)

public function onKernelException(ExceptionEvent $event) : void
{
$this->forceExceptionControllerHtml($event->getRequest());
$this->renderBlueScreen($event->getThrowable());
}
$blueScreenResponse = $this->renderBlueScreen($event->getThrowable());

private function forceExceptionControllerHtml(Request $request) : void
{
$request->setRequestFormat('html');
$request->attributes->set('_format', 'html');
$event->setResponse($blueScreenResponse);
}

private function renderBlueScreen(Throwable $exception) : void
private function renderBlueScreen(Throwable $exception) : Response
{
if (! headers_sent()) {
$protocol = $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1';
$code = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== false
? 503
: 500;
header($protocol . ' ' . $code, true, $code);
header('Content-Type: text/html; charset=UTF-8');
}
ob_start();

$this->blueScreen->render($exception);

$contents = ob_get_clean();
assert($contents !== false);

return new Response($contents, Response::HTTP_NOT_FOUND);
}
}

0 comments on commit a25eaad

Please sign in to comment.