From 4830ada35dec476cd8fe6e7e2c5c6f7c9a2828a8 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Mon, 20 Sep 2021 17:10:52 +0800 Subject: [PATCH] 111 --- http-handler/Abstracts/Handler.php | 190 +++++++++++++++-------------- 1 file changed, 100 insertions(+), 90 deletions(-) diff --git a/http-handler/Abstracts/Handler.php b/http-handler/Abstracts/Handler.php index 73c181fc..e076add9 100644 --- a/http-handler/Abstracts/Handler.php +++ b/http-handler/Abstracts/Handler.php @@ -18,115 +18,125 @@ abstract class Handler implements RequestHandlerInterface { - private int $offset = 0; + private int $offset = 0; - protected CHl $handler; + protected CHl $handler; #[Inject(AspectProxy::class)] - protected AspectProxy $aspectProxy; + protected AspectProxy $aspectProxy; - protected ?array $middlewares; - - /** - * @param CHl $handler - * @return $this - */ - public function setHandler(CHl $handler): static - { - $this->handler = $handler; - return $this; - } + protected ?array $middlewares; - /** - * @param array|null $middlewares - * @return $this - */ - public function setMiddlewares(?array $middlewares): static - { - $this->middlewares = $middlewares; - return $this; - } + /** + * @param \Kiri\Proxy\AspectProxy $aspectProxy + */ + public function setAspectProxy(AspectProxy $aspectProxy) + { + $this->aspectProxy = $aspectProxy; + } - /** - * @param ServerRequestInterface $request - * @return ResponseInterface - * @throws \Exception - */ - protected function execute(ServerRequestInterface $request): ResponseInterface - { - if (empty($this->middlewares) || !isset($this->middlewares[$this->offset])) { - return $this->dispatcher($request); - } - - $middleware = $this->middlewares[$this->offset]; - if (!($middleware instanceof MiddlewareInterface)) { - throw new \Exception('get_implements_class($middleware) not found method process.'); - } - - ++$this->offset; - - return $middleware->process($request, $this); - } + /** + * @param CHl $handler + * @return $this + */ + public function setHandler(CHl $handler): static + { + $this->handler = $handler; + return $this; + } - /** - * @param ServerRequestInterface $request - * @return mixed - * @throws \Exception - */ - protected function dispatcher(ServerRequestInterface $request): mixed - { + /** + * @param array|null $middlewares + * @return $this + */ + public function setMiddlewares(?array $middlewares): static + { + $this->middlewares = $middlewares; + return $this; + } + + + /** + * @param ServerRequestInterface $request + * @return ResponseInterface + * @throws \Exception + */ + protected function execute(ServerRequestInterface $request): ResponseInterface + { + if (empty($this->middlewares) || !isset($this->middlewares[$this->offset])) { + return $this->dispatcher($request); + } + + $middleware = $this->middlewares[$this->offset]; + if (!($middleware instanceof MiddlewareInterface)) { + throw new \Exception('get_implements_class($middleware) not found method process.'); + } + + ++$this->offset; + + return $middleware->process($request, $this); + } + + + /** + * @param ServerRequestInterface $request + * @return mixed + * @throws \Exception + */ + protected function dispatcher(ServerRequestInterface $request): mixed + { $response = $this->aspectProxy->proxy($this->handler); - if (!($response instanceof ResponseInterface)) { - $response = $this->transferToResponse($response); - } - $response->withHeader('Run-Time', $this->_runTime($request)); - return $response; - } + if (!($response instanceof ResponseInterface)) { + $response = $this->transferToResponse($response); + } + $response->withHeader('Run-Time', $this->_runTime($request)); + return $response; + } - /** - * @param ServerRequest $request - * @return float - */ - private function _runTime(ServerRequestInterface $request): float - { - $float = microtime(true) - time(); + /** + * @param ServerRequest $request + * @return float + */ + private function _runTime(ServerRequestInterface $request): float + { + $float = microtime(true) - time(); - $serverParams = $request->getServerParams(); + $serverParams = $request->getServerParams(); - $rTime = $serverParams['request_time_float'] - $serverParams['request_time']; + $rTime = $serverParams['request_time_float'] - $serverParams['request_time']; - return round($float - $rTime, 6); - } + return round($float - $rTime, 6); + } - /** - * @param mixed $responseData - * @return \Server\Constrict\ResponseInterface - * @throws \Exception - */ - private function transferToResponse(mixed $responseData): ResponseInterface - { - $interface = response()->withStatus(200); - if (!$interface->hasContentType()) { - $interface->withContentType('application/json;charset=utf-8'); - } - if (is_object($responseData)) { - $responseData = get_object_vars($responseData); - } - if ($interface->getContentType() == 'application/xml;charset=utf-8') { - $interface->getBody()->write(Help::toXml($responseData)); - } else if (is_array($responseData)) { - $interface->getBody()->write(json_encode($responseData)); - } else { - $interface->getBody()->write((string)$responseData); - } - return $interface; - } + /** + * @param mixed $responseData + * @return \Server\Constrict\ResponseInterface + * @throws \Exception + */ + private function transferToResponse(mixed $responseData): ResponseInterface + { + $interface = response()->withStatus(200); + if (!$interface->hasContentType()) { + $interface->withContentType('application/json;charset=utf-8'); + } + if (is_object($responseData)) { + $responseData = get_object_vars($responseData); + } + if ($interface->getContentType() == 'application/xml;charset=utf-8') { + $interface->getBody()->write(Help::toXml($responseData)); + } else if (is_array($responseData)) { + $interface->getBody()->write(json_encode($responseData)); + } else { + $interface->getBody()->write((string)$responseData); + } + return $interface; + } }