From d0e9f834d4136efdd86cf9ae76bd379d3db2a285 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sat, 18 Sep 2021 01:37:49 +0800 Subject: [PATCH] 111 --- http-handler/TestRequest.php | 105 +++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 41 deletions(-) diff --git a/http-handler/TestRequest.php b/http-handler/TestRequest.php index 68d944b7..05d806b7 100644 --- a/http-handler/TestRequest.php +++ b/http-handler/TestRequest.php @@ -19,52 +19,75 @@ class TestRequest { - private ?ResponseEmitter $response = null; + private ?ResponseEmitter $response = null; - /** - * @param Request $request - * @param Response $response - * @throws Exception - */ - public function onRequest(Request $request, Response $response): void - { - try { - [$PsrRequest, $PsrResponse] = $this->initRequestResponse($request); - - /** @var Handler $handler */ - $handler = HandlerManager::get($request->server['request_uri'], $request->getMethod()); - if (is_null($handler)) { - $PsrResponse->withStatus(404)->withBody(null); - } else if (is_integer($handler)) { - $PsrResponse->withStatus($handler)->withBody(null); - } else { - $middlewares = MiddlewareManager::get($handler->callback[0]::class, $handler->callback[1]); - - $stream = new Stream((new Dispatcher($handler, $middlewares))->handle($PsrRequest)); - - $PsrResponse->withStatus(200)->withBody($stream); - } - } catch (\Throwable $throwable) { - - } finally { - $this->response->sender($response, $PsrResponse); - } - } + /** + * @param Request $request + * @param Response $response + * @throws Exception + */ + public function onRequest(Request $request, Response $response): void + { + try { + [$PsrRequest, $PsrResponse] = $this->initRequestResponse($request); + /** @var Handler $handler */ + $handler = HandlerManager::get($request->server['request_uri'], $request->getMethod()); + if (is_integer($handler)) { + $PsrResponse->withStatus($handler)->withBody(null); + } else if (is_null($handler)) { + $PsrResponse->withStatus(404)->withBody(null); + } else { + $PsrResponse = $this->handler($handler, $PsrRequest); + } + } catch (\Throwable $throwable) { + $PsrResponse = $this->throwable($throwable); + } finally { + $this->response->sender($response, $PsrResponse); + } + } - /** - * @param Request $request - * @return array - * @throws Exception - */ - private function initRequestResponse(Request $request): array - { - $PsrResponse = Context::setContext(\Server\Constrict\ResponseInterface::class, new \Http\Message\Response()); + /** + * @param \Throwable $throwable + * @return \Psr\Http\Message\ResponseInterface + */ + protected function throwable(\Throwable $throwable): ResponseInterface + { + return \response()->withStatus($throwable->getCode()) + ->withContentType(\Http\Message\Response::CONTENT_TYPE_HTML) + ->withBody(new Stream(jTraceEx($throwable))); + } - $PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request)); - return [$PsrRequest, $PsrResponse]; - } + /** + * @param \Http\Handler\Handler $handler + * @param $PsrRequest + * @return \Psr\Http\Message\ResponseInterface + * @throws \Exception + */ + protected function handler(Handler $handler, $PsrRequest): 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]; + } }