qqq
This commit is contained in:
+92
-91
@@ -30,123 +30,124 @@ use const Kiri\Router\ROUTER_TYPE_HTTP;
|
|||||||
class OnRequest implements OnRequestInterface
|
class OnRequest implements OnRequestInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var RouterCollector
|
* @var RouterCollector
|
||||||
*/
|
*/
|
||||||
public RouterCollector $router;
|
public RouterCollector $router;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ExceptionHandlerInterface
|
* @var ExceptionHandlerInterface
|
||||||
*/
|
*/
|
||||||
public ExceptionHandlerInterface $exception;
|
public ExceptionHandlerInterface $exception;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ResponseEmitterInterface
|
* @var ResponseEmitterInterface
|
||||||
*/
|
*/
|
||||||
public ResponseEmitterInterface $emitter;
|
public ResponseEmitterInterface $emitter;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Kiri\Router\Request
|
* @var Kiri\Router\Request
|
||||||
*/
|
*/
|
||||||
#[Container(RequestInterface::class)]
|
#[Container(RequestInterface::class)]
|
||||||
public RequestInterface $request;
|
public RequestInterface $request;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ResponseInterface
|
* @var ResponseInterface
|
||||||
*/
|
*/
|
||||||
#[Container(ResponseInterface::class)]
|
#[Container(ResponseInterface::class)]
|
||||||
public ResponseInterface $response;
|
public ResponseInterface $response;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function init(): void
|
public function init(): void
|
||||||
{
|
{
|
||||||
$container = Kiri::getDi();
|
$container = Kiri::getDi();
|
||||||
$exception = $this->request->exception;
|
$exception = $this->request->exception;
|
||||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) {
|
if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) {
|
||||||
$exception = ExceptionHandlerDispatcher::class;
|
$exception = ExceptionHandlerDispatcher::class;
|
||||||
}
|
}
|
||||||
$this->exception = $container->get($exception);
|
$this->exception = $container->get($exception);
|
||||||
$this->router = $container->get(DataGrip::class)->get(ROUTER_TYPE_HTTP);
|
$this->router = $container->get(DataGrip::class)->get(ROUTER_TYPE_HTTP);
|
||||||
$this->emitter = $this->response->emmit;
|
$this->emitter = $this->response->emmit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param Response $response
|
* @param Response $response
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onRequest(Request $request, Response $response): void
|
public function onRequest(Request $request, Response $response): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
/** @var ConstrictRequest $PsrRequest */
|
/** @var ConstrictRequest $PsrRequest */
|
||||||
$PsrRequest = $this->initPsr7RequestAndPsr7Response($request);
|
$PsrRequest = $this->initPsr7RequestAndPsr7Response($request);
|
||||||
|
|
||||||
$dispatcher = $this->router->query($PsrRequest->getUri()->getPath(), $PsrRequest->getMethod());
|
$method = $PsrRequest->getUri()->getPath() == 'OPTIONS' ? '/*' : $PsrRequest->getUri()->getPath();
|
||||||
|
$dispatcher = $this->router->query($method, $PsrRequest->getMethod());
|
||||||
|
|
||||||
$middlewareManager = \Kiri::getDi()->get(MiddlewareManager::class);
|
$middlewareManager = \Kiri::getDi()->get(MiddlewareManager::class);
|
||||||
$middleware = $middlewareManager->get($dispatcher->getClass(), $dispatcher->getMethod());
|
$middleware = $middlewareManager->get($dispatcher->getClass(), $dispatcher->getMethod());
|
||||||
|
|
||||||
$PsrResponse = (new HttpRequestHandler($middleware, $dispatcher))->handle($PsrRequest);
|
$PsrResponse = (new HttpRequestHandler($middleware, $dispatcher))->handle($PsrRequest);
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
$PsrResponse = $this->exception->emit($throwable, di(ConstrictResponse::class));
|
$PsrResponse = $this->exception->emit($throwable, di(ConstrictResponse::class));
|
||||||
} finally {
|
} finally {
|
||||||
$this->emitter->sender($PsrResponse, $response);
|
$this->emitter->sender($PsrResponse, $response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return RequestInterface
|
* @return RequestInterface
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function initPsr7RequestAndPsr7Response(Request $request): RequestInterface
|
private function initPsr7RequestAndPsr7Response(Request $request): RequestInterface
|
||||||
{
|
{
|
||||||
/** @var ConstrictResponse $PsrResponse */
|
/** @var ConstrictResponse $PsrResponse */
|
||||||
$PsrResponse = Context::set(ResponseInterface::class, new ConstrictResponse());
|
$PsrResponse = Context::set(ResponseInterface::class, new ConstrictResponse());
|
||||||
$PsrResponse->withContentType($this->response->contentType);
|
$PsrResponse->withContentType($this->response->contentType);
|
||||||
|
|
||||||
$serverRequest = (new ConstrictRequest())->withDataHeaders($request->getData())
|
$serverRequest = (new ConstrictRequest())->withDataHeaders($request->getData())
|
||||||
->withUri(static::parse($request))
|
->withUri(static::parse($request))
|
||||||
->withProtocolVersion($request->server['server_protocol'])
|
->withProtocolVersion($request->server['server_protocol'])
|
||||||
->withCookieParams($request->cookie ?? [])
|
->withCookieParams($request->cookie ?? [])
|
||||||
->withServerParams($request->server)
|
->withServerParams($request->server)
|
||||||
->withQueryParams($request->get ?? [])
|
->withQueryParams($request->get ?? [])
|
||||||
->withUploadedFiles($request->files ?? [])
|
->withUploadedFiles($request->files ?? [])
|
||||||
->withMethod($request->getMethod())
|
->withMethod($request->getMethod())
|
||||||
->withParsedBody($request->post ?? []);
|
->withParsedBody($request->post ?? []);
|
||||||
|
|
||||||
/** @var ConstrictRequest $PsrRequest */
|
/** @var ConstrictRequest $PsrRequest */
|
||||||
return Context::set(RequestInterface::class, $serverRequest);
|
return Context::set(RequestInterface::class, $serverRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return UriInterface
|
* @return UriInterface
|
||||||
*/
|
*/
|
||||||
public static function parse(Request $request): UriInterface
|
public static function parse(Request $request): UriInterface
|
||||||
{
|
{
|
||||||
$uri = new Uri();
|
$uri = new Uri();
|
||||||
$uri->withQuery($request->server['query_string'] ?? '')
|
$uri->withQuery($request->server['query_string'] ?? '')
|
||||||
->withPath($request->server['path_info'])
|
->withPath($request->server['path_info'])
|
||||||
->withHost($request->header['host'] ?? '127.0.0.1')
|
->withHost($request->header['host'] ?? '127.0.0.1')
|
||||||
->withPort($request->server['server_port']);
|
->withPort($request->server['server_port']);
|
||||||
if (isset($request->server['https']) && $request->server['https'] !== 'off') {
|
if (isset($request->server['https']) && $request->server['https'] !== 'off') {
|
||||||
$uri->withScheme('https');
|
$uri->withScheme('https');
|
||||||
} else {
|
} else {
|
||||||
$uri->withScheme('http');
|
$uri->withScheme('http');
|
||||||
}
|
}
|
||||||
return $uri;
|
return $uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user