62 lines
2.0 KiB
PHP
62 lines
2.0 KiB
PHP
<?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;
|
|
}
|
|
} |