Files
kiri-router/src/Server.php
T

131 lines
3.3 KiB
PHP
Raw Normal View History

2023-04-15 23:29:27 +08:00
<?php
2023-04-16 01:24:30 +08:00
declare(strict_types=1);
2023-04-15 23:29:27 +08:00
2023-04-15 23:31:16 +08:00
namespace Kiri\Router;
2023-04-15 23:29:27 +08:00
use Exception;
use Kiri;
use Kiri\Di\ContainerInterface;
use Kiri\Di\Context;
use Kiri\Events\EventProvider;
2023-04-15 23:31:16 +08:00
use Kiri\Router\Interface\ExceptionHandlerInterface;
2023-04-15 23:29:27 +08:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2023-04-15 23:31:16 +08:00
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
2023-04-15 23:29:27 +08:00
use Swoole\Http\Request;
use Swoole\Http\Response;
2023-04-16 00:06:10 +08:00
use Kiri\Router\Constrict\ConstrictRequest;
use Kiri\Router\Constrict\ConstrictResponse;
2023-04-15 23:31:16 +08:00
use Kiri\Router\Constrict\Uri;
use Kiri\Router\Interface\OnRequestInterface;
use Kiri\Router\Base\ExceptionHandlerDispatcher;
2023-04-15 23:29:27 +08:00
/**
*
*/
2023-04-16 01:45:33 +08:00
class Server implements OnRequestInterface
2023-04-15 23:29:27 +08:00
{
public RouterCollector $router;
/**
* @var ExceptionHandlerInterface
*/
public ExceptionHandlerInterface $exception;
2023-04-15 23:31:16 +08:00
/**
* @var HttpResponseEmitter
*/
public HttpResponseEmitter $emitter;
2023-04-15 23:29:27 +08:00
/**
* @param ContainerInterface $container
* @param EventProvider $provider
* @param array $config
* @throws Exception
*/
public function __construct(
public ContainerInterface $container,
public EventProvider $provider,
array $config = [])
{
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function init()
{
2023-04-15 23:31:16 +08:00
$this->emitter = $this->container->get(HttpResponseEmitter::class);
2023-04-15 23:29:27 +08:00
2023-04-15 23:31:16 +08:00
$exception = Kiri::service()->get('request')->exception;
2023-04-15 23:29:27 +08:00
if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) {
$exception = ExceptionHandlerDispatcher::class;
}
$this->exception = $this->container->get($exception);
2023-04-15 23:31:16 +08:00
$this->router = $this->container->get(DataGrip::class)->get(ROUTER_TYPE_HTTP);
2023-04-15 23:29:27 +08:00
}
/**
* @param Request $request
* @param Response $response
* @throws Exception
*/
public function onRequest(Request $request, Response $response): void
{
try {
2023-04-16 00:07:36 +08:00
/** @var ConstrictRequest $PsrRequest */
2023-04-15 23:29:27 +08:00
$PsrRequest = $this->initRequestAndResponse($request);
$dispatcher = $this->router->query($request->server['request_uri'], $request->getMethod());
2023-04-15 23:31:16 +08:00
$PsrResponse = (new HttpRequestHandler([], $dispatcher))->handle($PsrRequest);
2023-04-15 23:29:27 +08:00
} catch (\Throwable $throwable) {
$this->logger->error($throwable->getMessage(), [$throwable]);
2023-04-16 00:06:10 +08:00
$PsrResponse = $this->exception->emit($throwable, di(ConstrictResponse::class));
2023-04-15 23:29:27 +08:00
} finally {
2023-04-15 23:31:16 +08:00
$this->emitter->sender($PsrResponse, $response);
2023-04-15 23:29:27 +08:00
}
}
2023-04-15 23:31:16 +08:00
2023-04-15 23:29:27 +08:00
/**
* @param Request $request
* @return RequestInterface
* @throws Exception
*/
2023-04-15 23:31:16 +08:00
private function initRequestAndResponse(Request $request): RequestInterface
2023-04-15 23:29:27 +08:00
{
2023-04-15 23:31:16 +08:00
/** @var \Kiri\Router\Response $response */
$response = Kiri::service()->get('response');
2023-04-16 00:06:10 +08:00
/** @var ConstrictResponse $PsrResponse */
$PsrResponse = Context::set(ResponseInterface::class, new ConstrictResponse());
2023-04-15 23:31:16 +08:00
$PsrResponse->withContentType($response->contentType);
2023-04-15 23:29:27 +08:00
2023-04-16 00:06:10 +08:00
$serverRequest = (new ConstrictRequest())->withDataHeaders($request->getData())
2023-04-15 23:31:16 +08:00
->withUri(Uri::parse($request))
->withProtocolVersion($request->server['server_protocol'])
2023-04-15 23:29:27 +08:00
->withCookieParams($request->cookie ?? [])
->withQueryParams($request->get)
->withUploadedFiles($request->files)
->withMethod($request->getMethod())
->withParsedBody($request->post);
2023-04-16 00:07:36 +08:00
/** @var ConstrictRequest $PsrRequest */
2023-04-15 23:29:27 +08:00
return Context::set(RequestInterface::class, $serverRequest);
}
}