This commit is contained in:
2021-09-06 19:03:38 +08:00
parent 9581386e84
commit 517cbee900
2 changed files with 12 additions and 9 deletions
+4 -4
View File
@@ -50,14 +50,14 @@ class Request implements RequestInterface
/** /**
* @param \Swoole\Http\Request $request * @param \Swoole\Http\Request $request
* @return Request * @return array<Request, Response>
*/ */
public static function create(\Swoole\Http\Request $request): RequestInterface public static function create(\Swoole\Http\Request $request): array
{ {
Context::setContext(ResponseInterface::class, new Response()); Context::setContext(ResponseInterface::class, $response = new Response());
Context::setContext(RequestMessage::class, RequestMessage::parseRequest($request)); Context::setContext(RequestMessage::class, RequestMessage::parseRequest($request));
return Kiri::getDi()->get(Request::class); return [Kiri::getDi()->get(Request::class), $response];
} }
} }
+8 -5
View File
@@ -42,18 +42,21 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
{ {
// TODO: Implement onRequest() method. // TODO: Implement onRequest() method.
try { try {
$request = \Server\Constrict\Request::create($request); [$request, $psr7Response] = \Server\Constrict\Request::create($request);
$node = $this->router->Branch_search($request); $node = $this->router->Branch_search($request);
if (!($node instanceof Node)) { if (!($node instanceof Node)) {
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404); throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
} }
if (!(($responseData = $node->dispatch($request)) instanceof ResponseInterface)) { if (!(($psr7Response = $node->dispatch($request)) instanceof ResponseInterface)) {
$responseData = $this->transferToResponse($responseData); $psr7Response = $this->transferToResponse($psr7Response);
} }
} catch (Error | \Throwable $exception) { } catch (Error | \Throwable $exception) {
$responseData = $this->exceptionHandler->emit($exception, $this->response); $psr7Response = $this->exceptionHandler->emit($exception, $this->response);
} finally { } finally {
$this->responseEmitter->sender($response, $responseData); if (!isset($psr7Response)) {
return;
}
$this->responseEmitter->sender($response, $psr7Response);
$this->eventDispatch->dispatch(new OnAfterRequest()); $this->eventDispatch->dispatch(new OnAfterRequest());
} }
} }