- Reworked ApplicationStrategy to allows better overridding of it's classes

This commit is contained in:
Dave Mc Nicoll 2025-05-26 18:19:43 +00:00
parent 156defddc9
commit 7a48d707e0
6 changed files with 118 additions and 45 deletions

View File

@ -1,5 +1,6 @@
<?php
use Lean\ApplicationStrategy\{ ThrowableHandler, ThrowableHandlerInterface, NotFoundDecoratorInterface, NotFoundDecorator };
use function DI\autowire, DI\create, DI\get;
use Psr\Http\Message\ServerRequestInterface;
@ -14,4 +15,6 @@ return [
},
EmitterInterface::class => create(SapiEmitter::class),
ThrowableHandlerInterface::class => autowire(ThrowableHandler::class),
NotFoundDecoratorInterface::class => autowire(NotFoundDecorator::class),
];

View File

@ -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);
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace Lean\ApplicationStrategy;
use DI\Attribute\Inject;
use Lean\ApplicationStrategy;
use Lean\Factory\HttpFactoryInterface;
use Picea\Picea;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
class NotFoundDecorator implements NotFoundDecoratorInterface
{
#[Inject]
protected Picea $picea;
#[Inject]
protected ContainerInterface $container;
#[Inject]
protected HttpFactoryInterface $httpFactory;
public const ASSET_TRIGGER_UPDATE = [
"js", "mjs", "manifest", "webmanifest", "css", "png", "ico",
"jpg", "jpeg", "gif", "webp", "woff", "woff2", "eot", "svg",
"ttf"
];
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
{
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;
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace Lean\ApplicationStrategy;
use Psr\Http\Server\MiddlewareInterface;
interface NotFoundDecoratorInterface extends MiddlewareInterface
{
}

View File

@ -0,0 +1,22 @@
<?php
namespace Lean\ApplicationStrategy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class ThrowableHandler implements ThrowableHandlerInterface
{
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
try {
return $handler->handle($request);
} catch (Throwable $e) {
throw $e;
}
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace Lean\ApplicationStrategy;
use Psr\Http\Server\MiddlewareInterface;
interface ThrowableHandlerInterface extends MiddlewareInterface
{
}