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

@ -1,4 +1,5 @@
PRODUCTION=false PRODUCTION=false
USE_SSL=false
DISCORD_CLIENT_ID="" DISCORD_CLIENT_ID=""
DISCORD_CLIENT_SECRET="" DISCORD_CLIENT_SECRET=""

View file

@ -5,6 +5,7 @@ namespace GamesShop;
use League\Container\Container; use League\Container\Container;
use League\Container\ReflectionContainer; use League\Container\ReflectionContainer;
use Whoops\Handler\HandlerInterface;
final class ContainerHandler final class ContainerHandler
{ {
@ -35,5 +36,6 @@ final class ContainerHandler
self::$instance = new Container(); self::$instance = new Container();
$reflectionContainer = new ReflectionContainer(true); $reflectionContainer = new ReflectionContainer(true);
self::$instance->delegate($reflectionContainer); 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); declare(strict_types=1);
use GamesShop\ContainerHandler; use GamesShop\ContainerHandler;
use GamesShop\CrashHandler;
use GamesShop\DoctrineManager; use GamesShop\DoctrineManager;
use GamesShop\Environment\EnvironmentHandler; use GamesShop\Environment\EnvironmentHandler;
use GamesShop\Routing\Router; use GamesShop\Routing\Router;
@ -12,12 +13,9 @@ use Whoops\Run;
require_once __DIR__ . '/../src/php/bootstrap.php'; require_once __DIR__ . '/../src/php/bootstrap.php';
$whoops = new Run(); ContainerHandler::get(CrashHandler::class)->register();
$prettyPageHandler = new PrettyPageHandler();
$whoops->pushHandler($prettyPageHandler);
$whoops->register();
ContainerHandler::getInstance()->addShared(HandlerInterface::class, $prettyPageHandler); throw new Exception();
$router = ContainerHandler::getInstance()->get(Router::class); $router = ContainerHandler::getInstance()->get(Router::class);
$result = $router->route(); $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);

View file

@ -10,9 +10,9 @@ const PUBLIC_FOLDER = Path.resolve(__dirname, 'public'),
CSS_FOLDER = Path.resolve(SOURCE_FOLDER, 'css'), CSS_FOLDER = Path.resolve(SOURCE_FOLDER, 'css'),
PHP_FOLDER = Path.resolve(SOURCE_FOLDER, 'php'); PHP_FOLDER = Path.resolve(SOURCE_FOLDER, 'php');
const PROD = false; const PROD = process.env.PROD ?? false;
const INDEX_PATH = Path.resolve(PHP_FOLDER, PROD ? 'index.prod.php' : 'index.dev.php'); const INDEX_PATH = Path.resolve(PHP_FOLDER, 'index.php');
module.exports = { module.exports = {
plugins: [ plugins: [