lean/src/ApplicationStrategy.php

61 lines
2.0 KiB
PHP
Raw Normal View History

<?php
namespace Lean;
use League\Route\Strategy;
use League\Route\Http\Exception\NotFoundException;
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,
) {}
public function getNotFoundDecorator(NotFoundException $exception): MiddlewareInterface
{
return new class($this->picea, $this->di) implements MiddlewareInterface {
public function __construct(
protected Picea $picea,
protected ContainerInterface $di,
) { }
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
return $this->throw404($request);
}
public function throw404(ServerRequestInterface $request) : ResponseInterface
{
if ( getenv('DEBUG') && class_exists(\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 new \Laminas\Diactoros\Response\HtmlResponse($this->picea->renderHtml("lean/error/404", [], $this), 404);
}
};
}
}