diff --git a/meta/definitions/software.php b/meta/definitions/software.php index 497042d..570e6cd 100644 --- a/meta/definitions/software.php +++ b/meta/definitions/software.php @@ -52,7 +52,8 @@ return [ return $adapter; }, - \Lean\Api\Factory\MessageFactoryInterface::class => DI\autowire(\Lean\Api\Lib\Message::class), - \League\Route\Strategy\ApplicationStrategy::class => DI\autowire(\Lean\Api\ApplicationStrategy::class), - \Lean\Api\Factory\DebugFormFactoryInterface::class => DI\autowire(\Lean\Api\Factory\DebugFormFactory::class), + Lean\ApplicationStrategy\NotFoundDecoratorInterface::class => DI\autowire(Lean\Api\ApplicationStrategy\NotFoundDecorator::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), + # League\Route\Strategy\ApplicationStrategy::class => DI\autowire(Lean\Api\ApplicationStrategy::class), ]; \ No newline at end of file diff --git a/src/ApplicationStrategy.php b/src/ApplicationStrategy.php index 33aeca1..9def940 100644 --- a/src/ApplicationStrategy.php +++ b/src/ApplicationStrategy.php @@ -5,6 +5,8 @@ namespace Lean\Api; use League\Route\Strategy; use League\Route\Http\Exception\NotFoundException; +use Lean\ApplicationStrategy\NotFoundDecoratorInterface; +use Lean\ApplicationStrategy\ThrowableHandler; use Lean\Factory\HttpFactoryInterface; use Picea\Asset\Asset; use Psr\Container\ContainerInterface; @@ -15,58 +17,10 @@ use Picea\Picea; use Psr\Http\Server\RequestHandlerInterface; use function DI\get; -class ApplicationStrategy extends Strategy\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 %s
%s
", $ex->getMessage(), $ex->getFile() . ":" . $ex->getLine(), $ex->getTraceAsString()); - exit(); - } - } - }; - } +class ApplicationStrategy extends \Lean\ApplicationStrategy { public function getNotFoundDecorator(NotFoundException $exception): MiddlewareInterface { - return new class($this->httpFactory) implements MiddlewareInterface { - - 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); - } - }; + return $this->getContainer()->get(NotFoundDecoratorInterface::class); } } \ No newline at end of file diff --git a/src/ApplicationStrategy/NotFoundDecorator.php b/src/ApplicationStrategy/NotFoundDecorator.php new file mode 100644 index 0000000..61f74f4 --- /dev/null +++ b/src/ApplicationStrategy/NotFoundDecorator.php @@ -0,0 +1,33 @@ +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); + } +} \ No newline at end of file diff --git a/src/ApplicationStrategy/ThrowableHandler.php b/src/ApplicationStrategy/ThrowableHandler.php new file mode 100644 index 0000000..0ec9214 --- /dev/null +++ b/src/ApplicationStrategy/ThrowableHandler.php @@ -0,0 +1,23 @@ +handle($request); + } catch (\Throwable $ex) { + echo sprintf("%s in %s
%s
", $ex->getMessage(), $ex->getFile() . ":" . $ex->getLine(), $ex->getTraceAsString()); + exit(); + } + +} \ No newline at end of file diff --git a/src/Middleware/ApiRenderer.php b/src/Middleware/ApiRenderer.php index 0e74b5d..47a09ab 100644 --- a/src/Middleware/ApiRenderer.php +++ b/src/Middleware/ApiRenderer.php @@ -24,7 +24,7 @@ class ApiRenderer implements MiddlewareInterface { $response = $handler->handle($request); } catch(\Throwable $ex) { - $errorId = $this->saveError($request, $ex)->id; + # $errorId = $this->saveError($request, $ex)->id; if ($this->awaitingJson($request)) { return HttpFactory::createJsonResponse([