- Fixed a bug where Assets would not load properly
This commit is contained in:
		
							parent
							
								
									d181c6d3a7
								
							
						
					
					
						commit
						2dbcfbcb2e
					
				@ -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),
 | 
			
		||||
];
 | 
			
		||||
@ -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 <strong>%s</strong> <pre>%s</pre>", $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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										33
									
								
								src/ApplicationStrategy/NotFoundDecorator.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/ApplicationStrategy/NotFoundDecorator.php
									
									
									
									
									
										Normal 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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										23
									
								
								src/ApplicationStrategy/ThrowableHandler.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/ApplicationStrategy/ThrowableHandler.php
									
									
									
									
									
										Normal 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();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -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([
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user