From 1e6333a18eff628ec952db561cb281d57f4510f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 21 Jul 2021 18:50:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpServer/Events/OnRequest.php | 10 +++++----- HttpServer/Http/Request.php | 6 ++---- HttpServer/Route/Router.php | 22 +++++++++++++++------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/HttpServer/Events/OnRequest.php b/HttpServer/Events/OnRequest.php index 89e78b66..a1b02323 100644 --- a/HttpServer/Events/OnRequest.php +++ b/HttpServer/Events/OnRequest.php @@ -16,7 +16,6 @@ use Snowflake\Snowflake; use Swoole\Error; use Swoole\Http\Request; use Swoole\Http\Response; -use Swoole\Http\Status; use Throwable; /** @@ -49,7 +48,7 @@ class OnRequest extends Callback * @return void * @throws Exception */ - public function onHandler(Request $request, Response $response): mixed + public function onHandler(Request $request, Response $response): void { try { defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES)); @@ -58,12 +57,13 @@ class OnRequest extends Callback [$request, $response] = OnRequest::createContext($request, $response); if ($request->is('favicon.ico')) { - return $response->close(404); + $response->close(404); + } else { + $this->router->dispatch(); } - return $this->router->dispatch(); } catch (ExitException | Error | Throwable $exception) { $this->addError($exception, 'throwable'); - return $this->sendErrorMessage($request, $response, $exception); + $this->sendErrorMessage($request, $response, $exception); } } diff --git a/HttpServer/Http/Request.php b/HttpServer/Http/Request.php index 7c296cb6..e0c80f3f 100644 --- a/HttpServer/Http/Request.php +++ b/HttpServer/Http/Request.php @@ -82,7 +82,6 @@ class Request extends HttpService $this->fd = $fd; } - /** * @return array|null * @throws Exception @@ -243,12 +242,11 @@ class Request extends HttpService * @return mixed * @throws Exception */ - public function adapter(): mixed + public function adapter(): void { if (!$this->isHead()) { - return router()->dispatch(); + router()->dispatch(); } - return ''; } diff --git a/HttpServer/Route/Router.php b/HttpServer/Route/Router.php index 3864db1e..3fe27a7d 100644 --- a/HttpServer/Route/Router.php +++ b/HttpServer/Route/Router.php @@ -8,6 +8,7 @@ use Exception; use HttpServer\Abstracts\HttpService; use HttpServer\Controller; use HttpServer\Http\Request; +use HttpServer\Http\Response; use HttpServer\IInterface\Middleware; use HttpServer\IInterface\RouterInterface; use JetBrains\PhpStorm\Pure; @@ -43,6 +44,9 @@ class Router extends HttpService implements RouterInterface public int $useTree = ROUTER_TREE; + public ?Response $response = null; + + /** * @param Closure $middleware */ @@ -54,11 +58,14 @@ class Router extends HttpService implements RouterInterface /** * @throws ConfigException + * @throws Exception * 初始化函数路径 */ public function init() { $this->dir = Config::get('http.namespace', $this->dir); + + $this->response = Snowflake::app()->get('response'); } @@ -513,17 +520,18 @@ class Router extends HttpService implements RouterInterface * @return mixed * @throws */ - public function dispatch(): mixed + public function dispatch(): void { $node = $this->find_path(\request()); if (!($node instanceof Node)) { - return send(\request()->getUri() . ' -> ' . self::NOT_FOUND); + $this->response->setFormat(Response::HTML); + $this->response->send('

404

'); + } else { + $this->response->send(($response = $node->dispatch()), 200); + if ($node->hasAfter()) { + $node->afterDispatch($response); + } } - send(($response = $node->dispatch()), 200); - if (!$node->hasAfter()) { - return null; - } - return $node->afterDispatch($response); }