2023-10-16 17:58:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Lean;
|
|
|
|
|
|
|
|
use League\Route\Strategy;
|
|
|
|
|
|
|
|
use League\Route\Http\Exception\NotFoundException;
|
2023-10-18 00:10:42 +00:00
|
|
|
use Picea\Asset\Asset;
|
|
|
|
use Psr\Container\ContainerInterface;
|
2023-10-16 17:58:51 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
|
|
use Picea\Picea;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2023-10-18 00:10:42 +00:00
|
|
|
use function DI\get;
|
2023-10-16 17:58:51 +00:00
|
|
|
|
|
|
|
class ApplicationStrategy extends Strategy\ApplicationStrategy {
|
|
|
|
|
2023-10-18 00:10:42 +00:00
|
|
|
public const ASSET_TRIGGER_UPDATE = [
|
2023-10-16 17:58:51 +00:00
|
|
|
"js", "mjs", "manifest", "webmanifest", "css", "png", "ico",
|
|
|
|
"jpg", "jpeg", "gif", "webp", "woff", "woff2", "eot", "svg",
|
|
|
|
"ttf"
|
|
|
|
];
|
|
|
|
|
|
|
|
public function __construct(
|
2023-10-18 00:10:42 +00:00
|
|
|
protected Picea $picea,
|
|
|
|
protected ContainerInterface $di,
|
2023-10-16 17:58:51 +00:00
|
|
|
) {}
|
|
|
|
|
|
|
|
public function getNotFoundDecorator(NotFoundException $exception): MiddlewareInterface
|
|
|
|
{
|
2023-10-18 00:10:42 +00:00
|
|
|
return new class($this->picea, $this->di) implements MiddlewareInterface {
|
2023-10-16 17:58:51 +00:00
|
|
|
|
2023-10-18 00:10:42 +00:00
|
|
|
public function __construct(
|
|
|
|
protected Picea $picea,
|
|
|
|
protected ContainerInterface $di,
|
|
|
|
) { }
|
2023-10-16 17:58:51 +00:00
|
|
|
|
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
|
|
{
|
|
|
|
return $this->throw404($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function throw404(ServerRequestInterface $request) : ResponseInterface
|
|
|
|
{
|
2023-11-01 15:28:22 +00:00
|
|
|
if ( getenv('DEBUG') && $this->di->has(\Picea\Asset\Asset::class) ) {
|
2023-10-18 00:10:42 +00:00
|
|
|
$params = $request->getServerParams();
|
|
|
|
|
2023-10-31 14:18:28 +00:00
|
|
|
$scpName = basename(explode('?', $params['REQUEST_URI'] ?? "", 2)[0]);
|
2023-10-18 00:10:42 +00:00
|
|
|
list(, $ext) = array_pad(explode('.', $scpName), 2, null);
|
2023-10-16 17:58:51 +00:00
|
|
|
|
2023-10-18 00:10:42 +00:00
|
|
|
if ($ext && in_array($ext, ApplicationStrategy::ASSET_TRIGGER_UPDATE)) {
|
|
|
|
$this->di->get(\Picea\Asset\Asset::class)->launchInstall();
|
|
|
|
}
|
2023-10-16 17:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new \Laminas\Diactoros\Response\HtmlResponse($this->picea->renderHtml("lean/error/404", [], $this), 404);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|