- Lean's API WIP on forms and ApiRenderer
This commit is contained in:
1 parent
3ce356e1f2
commit
4d7053a9cb
30 files changed
+1003
-56
No files matched your search
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Lean\Api;
|
||||
|
||||
use League\Route\Strategy;
|
||||
|
||||
use League\Route\Http\Exception\NotFoundException;
|
||||
use Lean\Factory\HttpFactoryInterface;
|
||||
use Picea\Asset\Asset;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
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();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user