emitter = di(HttpResponseEmitter::class); $exception = $this->request->exception; if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) { $exception = ExceptionHandlerDispatcher::class; } $this->exception = di($exception); $this->router = di(DataGrip::class)->get(ROUTER_TYPE_HTTP); } /** * @param Request $request * @param Response $response * @throws Exception */ public function onRequest(Request $request, Response $response): void { try { /** @var ConstrictRequest $PsrRequest */ $PsrRequest = $this->initRequestAndResponse($request); $dispatcher = $this->router->query($request->server['request_uri'], $request->getMethod()); $middleware = $this->request->middlewares; if (!($dispatcher instanceof Kiri\Router\Base\NotFoundController)) { $middlewareManager = \Kiri::getDi()->get(MiddlewareManager::class); $middleware = $middlewareManager->get($dispatcher->getClass(), $dispatcher->getMethod()); } $PsrResponse = (new HttpRequestHandler($middleware, $dispatcher))->handle($PsrRequest); } catch (\Throwable $throwable) { error($throwable); $PsrResponse = $this->exception->emit($throwable, di(ConstrictResponse::class)); } finally { $this->emitter->sender($PsrResponse, $response); } } /** * @param Request $request * @return RequestInterface * @throws Exception */ private function initRequestAndResponse(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(Uri::parse($request)) ->withProtocolVersion($request->server['server_protocol']) ->withCookieParams($request->cookie ?? []) ->withQueryParams($request->get ?? []) ->withUploadedFiles($request->files ?? []) ->withMethod($request->getMethod()) ->withParsedBody($request->post ?? []); /** @var ConstrictRequest $PsrRequest */ return Context::set(RequestInterface::class, $serverRequest); } }