From 7a48d707e0bf5c80a46a50a77796f1dbee2e0859 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 26 May 2025 18:19:43 +0000 Subject: [PATCH] - Reworked ApplicationStrategy to allows better overridding of it's classes --- meta/definitions/http.php | 3 + src/ApplicationStrategy.php | 56 ++++------------- src/ApplicationStrategy/NotFoundDecorator.php | 62 +++++++++++++++++++ .../NotFoundDecoratorInterface.php | 10 +++ src/ApplicationStrategy/ThrowableHandler.php | 22 +++++++ .../ThrowableHandlerInterface.php | 10 +++ 6 files changed, 118 insertions(+), 45 deletions(-) create mode 100644 src/ApplicationStrategy/NotFoundDecorator.php create mode 100644 src/ApplicationStrategy/NotFoundDecoratorInterface.php create mode 100644 src/ApplicationStrategy/ThrowableHandler.php create mode 100644 src/ApplicationStrategy/ThrowableHandlerInterface.php diff --git a/meta/definitions/http.php b/meta/definitions/http.php index 9c6db73..1ae4f7d 100644 --- a/meta/definitions/http.php +++ b/meta/definitions/http.php @@ -1,5 +1,6 @@ create(SapiEmitter::class), + ThrowableHandlerInterface::class => autowire(ThrowableHandler::class), + NotFoundDecoratorInterface::class => autowire(NotFoundDecorator::class), ]; diff --git a/src/ApplicationStrategy.php b/src/ApplicationStrategy.php index 2d3263f..7c55b3b 100644 --- a/src/ApplicationStrategy.php +++ b/src/ApplicationStrategy.php @@ -3,66 +3,32 @@ namespace Lean; use League\Route\Strategy; - use League\Route\Http\Exception\NotFoundException; +use Lean\ApplicationStrategy\NotFoundDecoratorInterface; +use Lean\ApplicationStrategy\ThrowableHandlerInterface; use Lean\Factory\HttpFactoryInterface; -use Picea\Asset\Asset; use Psr\Container\ContainerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; -use Picea\Picea; use Psr\Http\Server\RequestHandlerInterface; use function DI\get; class ApplicationStrategy extends Strategy\ApplicationStrategy { - public const ASSET_TRIGGER_UPDATE = [ - "js", "mjs", "manifest", "webmanifest", "css", "png", "ico", - "jpg", "jpeg", "gif", "webp", "woff", "woff2", "eot", "svg", - "ttf" - ]; - public function __construct( - protected Picea $picea, - protected ContainerInterface $di, - protected HttpFactoryInterface $httpFactory, - ) {} + ContainerInterface $di, + ) { + $this->setContainer($di); + } public function getNotFoundDecorator(NotFoundException $exception): MiddlewareInterface { - return new class($this->picea, $this->di, $this->httpFactory) implements MiddlewareInterface { + return $this->getContainer()->get(NotFoundDecoratorInterface::class); + } - public function __construct( - protected Picea $picea, - protected ContainerInterface $di, - protected HttpFactoryInterface $httpFactory, - ) { } - - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface - { - if (php_sapi_name() !== 'cli' and ! defined('STDIN')) { - return $this->throw404($request); - } - - return $handler->handle($request);; - } - - public function throw404(ServerRequestInterface $request) : ResponseInterface - { - if ( getenv('DEBUG') && $this->di->has(\Picea\Asset\Asset::class) ) { - $params = $request->getServerParams(); - - $scpName = basename(explode('?', $params['REQUEST_URI'] ?? "", 2)[0]); - list(, $ext) = array_pad(explode('.', $scpName), 2, null); - - if ($ext && in_array($ext, ApplicationStrategy::ASSET_TRIGGER_UPDATE)) { - $this->di->get(\Picea\Asset\Asset::class)->launchInstall(); - } - } - - return $this->httpFactory->createHtmlResponse($this->picea->renderHtml("lean/error/404", [], $this), 404); - } - }; + public function getThrowableHandler(): MiddlewareInterface + { + return $this->getContainer()->get(ThrowableHandlerInterface::class); } } \ No newline at end of file diff --git a/src/ApplicationStrategy/NotFoundDecorator.php b/src/ApplicationStrategy/NotFoundDecorator.php new file mode 100644 index 0000000..706dd70 --- /dev/null +++ b/src/ApplicationStrategy/NotFoundDecorator.php @@ -0,0 +1,62 @@ +throw404($request); + } + + return $handler->handle($request);; + } + + public function throw404(ServerRequestInterface $request) : ResponseInterface + { + return $this->checkAssetTrigger($request) ?: $this->httpFactory->createHtmlResponse($this->picea->renderHtml("lean/error/404", [], $this), 404); + } + + protected function checkAssetTrigger(ServerRequestInterface $request) : false|ResponseInterface + { + if (getenv('DEBUG') && $this->container->has(\Picea\Asset\Asset::class)) { + $params = $request->getServerParams(); + + $scpName = basename(explode('?', $params['REQUEST_URI'] ?? "", 2)[0]); + list(, $ext) = array_pad(explode('.', $scpName), 2, null); + + if ($ext && in_array($ext, static::ASSET_TRIGGER_UPDATE)) { + $this->container->get(\Picea\Asset\Asset::class)->launchInstall(); + + return $this->httpFactory->createTextResponse("Asset updated"); + } + } + + return false; + } +} \ No newline at end of file diff --git a/src/ApplicationStrategy/NotFoundDecoratorInterface.php b/src/ApplicationStrategy/NotFoundDecoratorInterface.php new file mode 100644 index 0000000..530c657 --- /dev/null +++ b/src/ApplicationStrategy/NotFoundDecoratorInterface.php @@ -0,0 +1,10 @@ +handle($request); + } catch (Throwable $e) { + throw $e; + } + } +} \ No newline at end of file diff --git a/src/ApplicationStrategy/ThrowableHandlerInterface.php b/src/ApplicationStrategy/ThrowableHandlerInterface.php new file mode 100644 index 0000000..1ebdc54 --- /dev/null +++ b/src/ApplicationStrategy/ThrowableHandlerInterface.php @@ -0,0 +1,10 @@ +