Merge pull request 'develop' (#5) from develop into master
All checks were successful
/ build (push) Successful in 37s

Reviewed-on: #5
This commit is contained in:
neintonine 2024-11-07 20:42:57 +00:00
commit aec0aa19d1
8 changed files with 63 additions and 36 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

@ -37,4 +37,9 @@ final class EnvironmentHandler
public function isProduction(): bool { public function isProduction(): bool {
return $_SERVER['PRODUCTION'] === 'true'; return $_SERVER['PRODUCTION'] === 'true';
} }
public function useSSL(): bool
{
return $_SERVER['USE_SSL'] === 'true';
}
} }

View file

@ -7,12 +7,14 @@ use Doctrine\ORM\EntityManager;
use GamesShop\Api\DiscordAPI; use GamesShop\Api\DiscordAPI;
use GamesShop\ContainerHandler; use GamesShop\ContainerHandler;
use GamesShop\Entities\Account\User; use GamesShop\Entities\Account\User;
use GamesShop\Environment\EnvironmentHandler;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
final class DiscordLoginProvider implements LoginProvider final class DiscordLoginProvider implements LoginProvider
{ {
public function __construct( public function __construct(
private readonly EntityManager $entityManager private readonly EntityManager $entityManager,
private readonly EnvironmentHandler $env
) )
{ {
} }
@ -20,7 +22,12 @@ final class DiscordLoginProvider implements LoginProvider
public function getUser(ServerRequestInterface $request): User public function getUser(ServerRequestInterface $request): User
{ {
$discordApiHandler = ContainerHandler::get(DiscordAPI::class); $discordApiHandler = ContainerHandler::get(DiscordAPI::class);
$result = $discordApiHandler->getUserFromCode($request->getQueryParams()['code'], (string)$request->getUri()->withQuery('')); $result = $discordApiHandler->getUserFromCode(
$request->getQueryParams()['code'],
(string)$request->getUri()
->withScheme($this->env->useSSL() ? 'https' : 'http')
->withQuery('')
);
$repo = $this->entityManager->getRepository(User::class); $repo = $this->entityManager->getRepository(User::class);
$users = $repo->findBy(['loginMethod' => LoginMethod::DISCORD, 'foreignLoginId' => $result['id']]); $users = $repo->findBy(['loginMethod' => LoginMethod::DISCORD, 'foreignLoginId' => $result['id']]);

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,7 @@ 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);
$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: [