diff --git a/src/OnRequest.php b/src/OnRequest.php new file mode 100644 index 0000000..0a838f9 --- /dev/null +++ b/src/OnRequest.php @@ -0,0 +1,103 @@ +responseEmitter = $this->response->emmit; + $exception = \config('exception.http'); + if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) { + $exception = ExceptionHandlerDispatcher::class; + } + $this->exception = \Kiri::getDi()->get($exception); + $this->router = $dataGrip->get(ROUTER_TYPE_HTTP); + } + + + /** + * @param Request $request + * @param Response $response + * @throws + */ + public function onRequest(Request $request, Response $response): void + { + /** @var CQ $PsrRequest */ + try { + $PsrRequest = $this->initRequestAndResponse($request); + + $PsrResponse = $this->router->query($request->server['path_info'], $request->getMethod()) + ->run($PsrRequest); + } catch (Throwable $throwable) { + $PsrResponse = $this->exception->emit($throwable, $this->constrictResponse); + } finally { + $this->responseEmitter->response($PsrResponse, $response, $PsrRequest); + } + } + + + /** + * @param Request $request + * @return ServerRequestInterface + */ + public function initRequestAndResponse(Request $request): ServerRequestInterface + { + $response = new ConstrictResponse($this->response->contentType); + + Context::set(ResponseInterface::class, $response); + + return Context::set(RequestInterface::class, CQ::builder($request)); + } + +}