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-15 23:31:16 +08:00
namespace Kiri\Router;
2023-04-15 23:29:27 +08:00
use Exception;
use Kiri;
use Kiri\Abstracts\AbstractServer;
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-15 23:31:16 +08:00
use Kiri\Router\Constrict\Response as KrcResponse;
use Kiri\Router\Constrict\Uri;
use Kiri\Router\Interface\OnRequestInterface;
use Kiri\Router\Base\ExceptionHandlerDispatcher;
2023-04-15 23:29:27 +08:00
/**
*
*/
class Server extends AbstractServer implements OnRequestInterface
{
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 = [])
{
parent::__construct($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 {
/** @var ServerRequest $PsrRequest */
$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]);
$PsrResponse = $this->exception->emit($throwable, di(Constrict\Response::class));
} 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');
/** @var KrcResponse $PsrResponse */
$PsrResponse = Context::set(ResponseInterface::class, new KrcResponse());
$PsrResponse->withContentType($response->contentType);
2023-04-15 23:29:27 +08:00
2023-04-15 23:31:16 +08:00
$serverRequest = (new ServerRequest())->withHeaders($request->getData())
->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);
/** @var ServerRequest $PsrRequest */
return Context::set(RequestInterface::class, $serverRequest);
}
}