- Fixed a bug where Assets would not load properly

This commit is contained in:
Dave Mc Nicoll 2025-05-26 18:20:22 +00:00
parent d181c6d3a7
commit 2dbcfbcb2e
5 changed files with 65 additions and 54 deletions

View File

@ -52,7 +52,8 @@ return [
return $adapter; return $adapter;
}, },
\Lean\Api\Factory\MessageFactoryInterface::class => DI\autowire(\Lean\Api\Lib\Message::class), Lean\ApplicationStrategy\NotFoundDecoratorInterface::class => DI\autowire(Lean\Api\ApplicationStrategy\NotFoundDecorator::class),
\League\Route\Strategy\ApplicationStrategy::class => DI\autowire(\Lean\Api\ApplicationStrategy::class), Lean\Api\Factory\MessageFactoryInterface::class => DI\autowire(Lean\Api\Lib\Message::class),
\Lean\Api\Factory\DebugFormFactoryInterface::class => DI\autowire(\Lean\Api\Factory\DebugFormFactory::class), Lean\Api\Factory\DebugFormFactoryInterface::class => DI\autowire(Lean\Api\Factory\DebugFormFactory::class),
# League\Route\Strategy\ApplicationStrategy::class => DI\autowire(Lean\Api\ApplicationStrategy::class),
]; ];

View File

@ -5,6 +5,8 @@ namespace Lean\Api;
use League\Route\Strategy; use League\Route\Strategy;
use League\Route\Http\Exception\NotFoundException; use League\Route\Http\Exception\NotFoundException;
use Lean\ApplicationStrategy\NotFoundDecoratorInterface;
use Lean\ApplicationStrategy\ThrowableHandler;
use Lean\Factory\HttpFactoryInterface; use Lean\Factory\HttpFactoryInterface;
use Picea\Asset\Asset; use Picea\Asset\Asset;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
@ -15,58 +17,10 @@ use Picea\Picea;
use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
use function DI\get; use function DI\get;
class ApplicationStrategy extends Strategy\ApplicationStrategy { class ApplicationStrategy extends \Lean\ApplicationStrategy {
public function __construct(
protected HttpFactoryInterface $httpFactory,
ContainerInterface $di,
) {
$this->container = $di;
}
public function getThrowableHandler(): MiddlewareInterface
{
return new class implements MiddlewareInterface
{
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
try {
return $handler->handle($request);
} catch (\Throwable $ex) {
echo sprintf("%s in <strong>%s</strong> <pre>%s</pre>", $ex->getMessage(), $ex->getFile() . ":" . $ex->getLine(), $ex->getTraceAsString());
exit();
}
}
};
}
public function getNotFoundDecorator(NotFoundException $exception): MiddlewareInterface public function getNotFoundDecorator(NotFoundException $exception): MiddlewareInterface
{ {
return new class($this->httpFactory) implements MiddlewareInterface { return $this->getContainer()->get(NotFoundDecoratorInterface::class);
public function __construct(
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
{
return $this->httpFactory->createJsonResponse([
'status' => 'error',
'message' => "Not Found",
'ts' => time(),
], 404);
}
};
} }
} }

View File

@ -0,0 +1,33 @@
<?php
namespace Lean\Api\ApplicationStrategy;
use DI\Attribute\Inject;
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\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class NotFoundDecorator extends \Lean\ApplicationStrategy\NotFoundDecorator
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (php_sapi_name() !== 'cli' and ! defined('STDIN')) {
return $this->throw404($request);
}
return parent::process($request, $handler);
}
public function throw404(ServerRequestInterface $request) : ResponseInterface
{
return $this->checkAssetTrigger($request) ?: $this->httpFactory->createJsonResponse([
'status' => 'error',
'message' => "Not Found",
'ts' => time(),
], 404);
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace Lean\Api\ApplicationStrategy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class ThrowableHandler extends \Lean\ApplicationStrategy\ThrowableHandler
{
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
try {
return $handler->handle($request);
} catch (\Throwable $ex) {
echo sprintf("%s in <strong>%s</strong> <pre>%s</pre>", $ex->getMessage(), $ex->getFile() . ":" . $ex->getLine(), $ex->getTraceAsString());
exit();
}
}

View File

@ -24,7 +24,7 @@ class ApiRenderer implements MiddlewareInterface {
$response = $handler->handle($request); $response = $handler->handle($request);
} }
catch(\Throwable $ex) { catch(\Throwable $ex) {
$errorId = $this->saveError($request, $ex)->id; # $errorId = $this->saveError($request, $ex)->id;
if ($this->awaitingJson($request)) { if ($this->awaitingJson($request)) {
return HttpFactory::createJsonResponse([ return HttpFactory::createJsonResponse([