add data from work folder
This commit is contained in:
parent
97579d6d91
commit
48c9c9467d
218 changed files with 16123 additions and 1233 deletions
BIN
app/public/favicon.ico
Normal file
BIN
app/public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
0
app/src/.gitkeep
Normal file
0
app/src/.gitkeep
Normal file
17
app/src/Http/AddRoute.php
Normal file
17
app/src/Http/AddRoute.php
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Http;
|
||||
|
||||
interface AddRoute
|
||||
{
|
||||
/**
|
||||
* @param array<class-string, string>|class-string|callable $handler
|
||||
*/
|
||||
public function addRoute(
|
||||
string $method,
|
||||
string $path,
|
||||
array|string|callable $handler,
|
||||
): self;
|
||||
}
|
97
app/src/Http/RouteDecorationMiddleware.php
Normal file
97
app/src/Http/RouteDecorationMiddleware.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Http;
|
||||
|
||||
use FastRoute\Dispatcher;
|
||||
use FastRoute\RouteCollector;
|
||||
use Lubian\NoFramework\Exception\InternalServerError;
|
||||
use Lubian\NoFramework\Exception\MethodNotAllowed;
|
||||
use Lubian\NoFramework\Exception\NotFound;
|
||||
use Psr\Http\Message\ResponseFactoryInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Throwable;
|
||||
use function FastRoute\simpleDispatcher;
|
||||
|
||||
final class RouteDecorationMiddleware implements MiddlewareInterface, AddRoute
|
||||
{
|
||||
/**
|
||||
* @param array<int, array{string, string, array{class-string, string}|class-string|callable}> $routes
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly ResponseFactoryInterface $responseFactory,
|
||||
private readonly string $routeAttributeName = '__route_handler',
|
||||
private array $routes = [],
|
||||
private Dispatcher|null $dispatcher = null,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws MethodNotAllowed
|
||||
* @throws NotFound
|
||||
*/
|
||||
private function decorateRequest(ServerRequestInterface $request): ServerRequestInterface
|
||||
{
|
||||
$this->dispatcher ??= $this->createDispatcher();
|
||||
$routeInfo = $this->dispatcher->dispatch(
|
||||
$request->getMethod(),
|
||||
$request->getUri()->getPath()
|
||||
);
|
||||
|
||||
if ($routeInfo[0] === Dispatcher::NOT_FOUND) {
|
||||
throw new NotFound;
|
||||
}
|
||||
|
||||
if ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
|
||||
throw new MethodNotAllowed;
|
||||
}
|
||||
|
||||
foreach ($routeInfo[2] as $attributeName => $attributeValue) {
|
||||
$request = $request->withAttribute($attributeName, $attributeValue);
|
||||
}
|
||||
|
||||
return $request->withAttribute($this->routeAttributeName, $routeInfo[1]);
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
try {
|
||||
$request = $this->decorateRequest($request);
|
||||
} catch (NotFound) {
|
||||
$response = $this->responseFactory->createResponse(404);
|
||||
$response->getBody()->write('Not Found');
|
||||
return $response;
|
||||
} catch (MethodNotAllowed) {
|
||||
return $this->responseFactory->createResponse(405);
|
||||
} catch (Throwable $t) {
|
||||
throw new InternalServerError($t->getMessage(), $t->getCode(), $t);
|
||||
}
|
||||
|
||||
if ($handler instanceof RoutedRequestHandler) {
|
||||
$handler->setRouteAttributeName($this->routeAttributeName);
|
||||
}
|
||||
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
private function createDispatcher(): Dispatcher
|
||||
{
|
||||
return simpleDispatcher(function (RouteCollector $r) {
|
||||
foreach ($this->routes as $route) {
|
||||
$r->addRoute($route[0], $route[1], $route[2]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function addRoute(string $method, string $path, array|string|callable $handler,): AddRoute
|
||||
{
|
||||
$this->routes[] = [$method, $path, $handler];
|
||||
return $this;
|
||||
}
|
||||
}
|
63
app/src/Kernel.php
Normal file
63
app/src/Kernel.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework;
|
||||
|
||||
use Lubian\NoFramework\Exception\InternalServerError;
|
||||
use Lubian\NoFramework\Http\AddRoute;
|
||||
use Lubian\NoFramework\Http\Emitter;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Throwable;
|
||||
|
||||
use function assert;
|
||||
|
||||
final class Kernel implements RequestHandlerInterface, AddRoute
|
||||
{
|
||||
public function __construct(
|
||||
private ContainerInterface $container,
|
||||
private Emitter $emitter,
|
||||
private MiddlewareInterface&AddRoute $routeMiddleware,
|
||||
private RequestHandlerInterface $handler,
|
||||
) {
|
||||
}
|
||||
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
return $this->routeMiddleware->process($request, $this->handler);
|
||||
}
|
||||
|
||||
public function run(ServerRequestInterface |null $request = null): void
|
||||
{
|
||||
$request ??= $this->createRequest();
|
||||
$response = $this->handle($request);
|
||||
$this->emitter->emit($response);
|
||||
}
|
||||
|
||||
private function createRequest(): ServerRequestInterface
|
||||
{
|
||||
try {
|
||||
$request = $this->container->get(ServerRequestInterface::class);
|
||||
assert($request instanceof ServerRequestInterface);
|
||||
return $request;
|
||||
} catch (Throwable $t) {
|
||||
throw new InternalServerError(
|
||||
'could not get Request from container, please configure the container ' .
|
||||
'in order to use run() wihtout a request',
|
||||
$t->getCode(),
|
||||
$t,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function addRoute(
|
||||
string $method,
|
||||
string $path,
|
||||
array|string|callable $handler): AddRoute
|
||||
{
|
||||
$this->routeMiddleware->addRoute($method, $path, $handler);
|
||||
return $this;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue