2023-04-24 10:54:56 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Server\Handler;
|
|
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
use Kiri;
|
2023-05-26 10:16:20 +08:00
|
|
|
use Kiri\Di\Inject\Container;
|
2023-04-24 10:54:56 +08:00
|
|
|
use Kiri\Di\Context;
|
2023-04-24 11:50:58 +08:00
|
|
|
use Kiri\Di\Interface\ResponseEmitterInterface;
|
|
|
|
|
use Kiri\Router\Base\ExceptionHandlerDispatcher;
|
|
|
|
|
use Kiri\Router\Base\Middleware as MiddlewareManager;
|
2023-04-24 10:54:56 +08:00
|
|
|
use Kiri\Router\Constrict\ConstrictRequest;
|
|
|
|
|
use Kiri\Router\Constrict\ConstrictResponse;
|
|
|
|
|
use Kiri\Router\Constrict\Uri;
|
2023-04-24 11:50:58 +08:00
|
|
|
use Kiri\Router\DataGrip;
|
|
|
|
|
use Kiri\Router\HttpRequestHandler;
|
|
|
|
|
use Kiri\Router\Interface\ExceptionHandlerInterface;
|
2023-04-24 10:54:56 +08:00
|
|
|
use Kiri\Router\Interface\OnRequestInterface;
|
2023-04-24 11:50:58 +08:00
|
|
|
use Kiri\Router\RouterCollector;
|
2023-04-24 10:54:56 +08:00
|
|
|
use Psr\Http\Message\RequestInterface;
|
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
use Psr\Http\Message\UriInterface;
|
|
|
|
|
use Swoole\Http\Request;
|
|
|
|
|
use Swoole\Http\Response;
|
2023-04-24 11:50:58 +08:00
|
|
|
use const Kiri\Router\ROUTER_TYPE_HTTP;
|
2023-04-24 10:54:56 +08:00
|
|
|
|
2023-05-26 11:25:43 +08:00
|
|
|
/**
|
2023-05-26 16:05:21 +08:00
|
|
|
* OnRequest
|
2023-05-26 11:25:43 +08:00
|
|
|
*/
|
2023-04-24 10:54:56 +08:00
|
|
|
class OnRequest implements OnRequestInterface
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-24 11:50:58 +08:00
|
|
|
* @var RouterCollector
|
2023-04-24 10:54:56 +08:00
|
|
|
*/
|
2023-04-24 11:50:58 +08:00
|
|
|
public RouterCollector $router;
|
2023-04-24 10:54:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-24 11:50:58 +08:00
|
|
|
* @var ExceptionHandlerInterface
|
|
|
|
|
*/
|
|
|
|
|
public ExceptionHandlerInterface $exception;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var ResponseEmitterInterface
|
|
|
|
|
*/
|
|
|
|
|
public ResponseEmitterInterface $emitter;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var Kiri\Router\Request
|
|
|
|
|
*/
|
2023-05-26 10:16:20 +08:00
|
|
|
#[Container(RequestInterface::class)]
|
2023-04-24 11:50:58 +08:00
|
|
|
public RequestInterface $request;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var ResponseInterface
|
2023-04-24 10:54:56 +08:00
|
|
|
*/
|
2023-05-26 10:16:20 +08:00
|
|
|
#[Container(ResponseInterface::class)]
|
2023-04-24 11:50:58 +08:00
|
|
|
public ResponseInterface $response;
|
2023-04-24 10:54:56 +08:00
|
|
|
|
|
|
|
|
|
2023-04-24 11:50:58 +08:00
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function init(): void
|
|
|
|
|
{
|
|
|
|
|
$container = Kiri::getDi();
|
|
|
|
|
$exception = $this->request->exception;
|
|
|
|
|
if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) {
|
|
|
|
|
$exception = ExceptionHandlerDispatcher::class;
|
|
|
|
|
}
|
|
|
|
|
$this->exception = $container->get($exception);
|
|
|
|
|
$this->router = $container->get(DataGrip::class)->get(ROUTER_TYPE_HTTP);
|
2023-04-24 11:51:45 +08:00
|
|
|
$this->emitter = $this->response->emmit;
|
2023-04-24 11:50:58 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-24 13:35:16 +08:00
|
|
|
|
2023-04-24 10:54:56 +08:00
|
|
|
/**
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @param Response $response
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function onRequest(Request $request, Response $response): void
|
|
|
|
|
{
|
2023-04-24 11:50:58 +08:00
|
|
|
try {
|
2023-05-26 16:02:33 +08:00
|
|
|
/** @var ConstrictRequest $PsrRequest */
|
2023-04-24 11:50:58 +08:00
|
|
|
$PsrRequest = $this->initPsr7RequestAndPsr7Response($request);
|
|
|
|
|
|
2023-05-26 11:23:21 +08:00
|
|
|
$dispatcher = $this->router->query($PsrRequest->getUri()->getPath(), $PsrRequest->getMethod());
|
2023-04-24 11:50:58 +08:00
|
|
|
|
|
|
|
|
$middleware = [];
|
|
|
|
|
if (!($dispatcher instanceof Kiri\Router\Base\NotFoundController)) {
|
|
|
|
|
$middlewareManager = \Kiri::getDi()->get(MiddlewareManager::class);
|
|
|
|
|
|
|
|
|
|
$middleware = $middlewareManager->get($dispatcher->getClass(), $dispatcher->getMethod());
|
|
|
|
|
}
|
2023-04-24 10:54:56 +08:00
|
|
|
|
2023-04-24 11:50:58 +08:00
|
|
|
$PsrResponse = (new HttpRequestHandler($middleware, $dispatcher))->handle($PsrRequest);
|
|
|
|
|
} catch (\Throwable $throwable) {
|
|
|
|
|
$PsrResponse = $this->exception->emit($throwable, di(ConstrictResponse::class));
|
|
|
|
|
} finally {
|
|
|
|
|
$this->emitter->sender($PsrResponse, $response);
|
|
|
|
|
}
|
2023-04-24 10:54:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @return RequestInterface
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function initPsr7RequestAndPsr7Response(Request $request): RequestInterface
|
|
|
|
|
{
|
|
|
|
|
/** @var ConstrictResponse $PsrResponse */
|
|
|
|
|
$PsrResponse = Context::set(ResponseInterface::class, new ConstrictResponse());
|
|
|
|
|
$PsrResponse->withContentType($this->response->contentType);
|
|
|
|
|
|
|
|
|
|
$serverRequest = (new ConstrictRequest())->withDataHeaders($request->getData())
|
|
|
|
|
->withUri(static::parse($request))
|
|
|
|
|
->withProtocolVersion($request->server['server_protocol'])
|
|
|
|
|
->withCookieParams($request->cookie ?? [])
|
2023-05-26 16:02:33 +08:00
|
|
|
->withServerParams($request->server)
|
2023-04-24 10:54:56 +08:00
|
|
|
->withQueryParams($request->get ?? [])
|
|
|
|
|
->withUploadedFiles($request->files ?? [])
|
|
|
|
|
->withMethod($request->getMethod())
|
|
|
|
|
->withParsedBody($request->post ?? []);
|
|
|
|
|
|
|
|
|
|
/** @var ConstrictRequest $PsrRequest */
|
|
|
|
|
return Context::set(RequestInterface::class, $serverRequest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @return UriInterface
|
|
|
|
|
*/
|
|
|
|
|
public static function parse(Request $request): UriInterface
|
|
|
|
|
{
|
|
|
|
|
$uri = new Uri();
|
|
|
|
|
$uri->withQuery($request->server['query_string'] ?? '')
|
|
|
|
|
->withPath($request->server['path_info'])
|
2023-04-24 11:42:43 +08:00
|
|
|
->withHost($request->header['host'] ?? '127.0.0.1')
|
2023-04-24 10:54:56 +08:00
|
|
|
->withPort($request->server['server_port']);
|
|
|
|
|
if (isset($request->server['https']) && $request->server['https'] !== 'off') {
|
|
|
|
|
$uri->withScheme('https');
|
|
|
|
|
} else {
|
|
|
|
|
$uri->withScheme('http');
|
|
|
|
|
}
|
|
|
|
|
return $uri;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|