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