initRequestResponse($request); /** @var Handler $handler */ $handler = HandlerManager::get($request->server['request_uri'], $request->getMethod()); if (is_integer($handler)) { $PsrResponse->withStatus($handler)->withBody(new Stream('Allow Method[' . $request->getMethod() . '].')); } else if (is_null($handler)) { $PsrResponse->withStatus(404)->withBody(new Stream('Page not found.')); } else { $PsrResponse = $this->handler($handler, $PsrRequest); } } catch (\Throwable $throwable) { $PsrResponse= $this->exceptionHandler->emit($throwable, $this->response); } finally { $this->responseEmitter->sender($response, $PsrResponse); } } /** * @param Handler $handler * @param $PsrRequest * @return ResponseInterface * @throws Exception */ protected function handler(Handler $handler, $PsrRequest): \Psr\Http\Message\ResponseInterface { $middlewares = MiddlewareManager::get($handler->callback); $dispatcher = new Dispatcher($handler, $middlewares); return $dispatcher->handle($PsrRequest); } /** * @param Request $request * @return array * @throws Exception */ private function initRequestResponse(Request $request): array { $PsrResponse = Context::setContext(ResponseInterface::class, new \Http\Message\Response()); $PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request)); return [$PsrRequest, $PsrResponse]; } /** * @param Server $server * @param int $fd * @throws Exception */ public function onDisconnect(Server $server, int $fd): void { } /** * @param Server $server * @param int $fd * @throws Exception */ public function onClose(Server $server, int $fd): void { } }