Adds proper safety for prod and added option for SSL

This commit is contained in:
Michel 2024-11-07 21:38:48 +01:00
parent 0962b609c4
commit d67c4063e4
6 changed files with 50 additions and 33 deletions

View file

@ -5,6 +5,7 @@ namespace GamesShop;
use League\Container\Container;
use League\Container\ReflectionContainer;
use Whoops\Handler\HandlerInterface;
final class ContainerHandler
{
@ -35,5 +36,6 @@ final class ContainerHandler
self::$instance = new Container();
$reflectionContainer = new ReflectionContainer(true);
self::$instance->delegate($reflectionContainer);
self::$instance->addShared(Container::class, self::$instance);
}
}

42
src/php/CrashHandler.php Normal file
View file

@ -0,0 +1,42 @@
<?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);
}
);
}
}

View file

@ -2,6 +2,7 @@
declare(strict_types=1);
use GamesShop\ContainerHandler;
use GamesShop\CrashHandler;
use GamesShop\DoctrineManager;
use GamesShop\Environment\EnvironmentHandler;
use GamesShop\Routing\Router;
@ -12,12 +13,9 @@ use Whoops\Run;
require_once __DIR__ . '/../src/php/bootstrap.php';
$whoops = new Run();
$prettyPageHandler = new PrettyPageHandler();
$whoops->pushHandler($prettyPageHandler);
$whoops->register();
ContainerHandler::get(CrashHandler::class)->register();
ContainerHandler::getInstance()->addShared(HandlerInterface::class, $prettyPageHandler);
throw new Exception();
$router = ContainerHandler::getInstance()->get(Router::class);
$result = $router->route();

View file

@ -1,26 +0,0 @@
<?php
declare(strict_types=1);
use GamesShop\ContainerHandler;
use GamesShop\DoctrineManager;
use GamesShop\Environment\EnvironmentHandler;
use GamesShop\Routing\Router;
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Whoops\Handler\HandlerInterface;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;
require_once __DIR__ . '/../src/php/bootstrap.php';
$whoops = new Run();
$prettyPageHandler = new PrettyPageHandler();
$whoops->pushHandler($prettyPageHandler);
$whoops->register();
ContainerHandler::getInstance()->addShared(HandlerInterface::class, $prettyPageHandler);
$router = ContainerHandler::getInstance()->get(Router::class);
$result = $router->route();
(new SapiEmitter)->emit($result);