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; $this->constrictResponse = $container->get(ConstrictResponse::class); $this->middlewareManager = $container->get(MiddlewareManager::class); } /** * @param Request $request * @param Response $response * @throws Exception */ public function onRequest(Request $request, Response $response): void { try { /** @var ConstrictRequest $PsrRequest */ $PsrRequest = Context::set(RequestInterface::class, $this->constrictRequest($request)); /** @var ConstrictResponse $PsrResponse */ $PsrResponse = Context::set(ResponseInterface::class, new ConstrictResponse()); $PsrResponse->withContentType($this->response->contentType); /** @var $dispatcher */ $dispatcher = $this->router->query($request->server['path_info'], $request->getMethod()); /** @var $PsrResponse */ $PsrResponse = $dispatcher->run($PsrRequest); } catch (Throwable $throwable) { $PsrResponse = $this->exception->emit($throwable, $this->constrictResponse); } finally { $this->emitter->sender($PsrResponse, $response, $PsrRequest); } } /** * @param Request $request * @return ConstrictRequest */ protected function constrictRequest(Request $request): ConstrictRequest { return (new ConstrictRequest())->withHeaders($request->header ?? []) ->withUri(new Uri($request)) ->withProtocolVersion($request->server['server_protocol']) ->withCookieParams($request->cookie ?? []) ->withServerParams($request->server) ->withQueryParams($request->get ?? []) ->withParsedBody(function () use ($request) { $contentType = $request->header['content-type'] ?? 'application/json'; if (str_contains($contentType, 'json')) { return Json::decode($request->getContent()); } else if (str_contains($contentType, 'xml')) { return Xml::toArray($request->getContent()); } else { return $request->post ?? []; } }) ->withUploadedFiles($request->files ?? []) ->withMethod($request->getMethod()); } }