gamesshop/src/php/CrashHandler.php

42 lines
No EOL
1.1 KiB
PHP

<?php declare(strict_types=1);
namespace GamesShop;
use GamesShop\Environment\EnvironmentHandler;
use GamesShop\Templates\TemplateEngine;
use League\Container\Container;
use Whoops\Handler\CallbackHandler;
use Whoops\Handler\HandlerInterface;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;
final class CrashHandler
{
public function __construct(
private readonly Container $container,
private readonly EnvironmentHandler $env
) { }
public function register() : void {
$handler = $this->getHandler();
$this->container->addShared(HandlerInterface::class, $handler);
$whoops = new Run();
$whoops->pushHandler($handler);
$whoops->register();
}
private function getHandler(): HandlerInterface
{
if (!$this->env->isProduction()) {
return new PrettyPageHandler();
}
return new CallbackHandler(
function ($exception, $inspector, $run) {
http_response_code(500);
echo ContainerHandler::get(TemplateEngine::class)
->renderErrorPage(500);
}
);
}
}