From f4893843cd1238b1f022f240cf72ec841f079018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 16 Dec 2020 17:48:01 +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 | 3 +++ HttpServer/Route/Router.php | 22 +++++++++------------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/HttpServer/Events/OnRequest.php b/HttpServer/Events/OnRequest.php index f8ac210b..200034ab 100644 --- a/HttpServer/Events/OnRequest.php +++ b/HttpServer/Events/OnRequest.php @@ -6,6 +6,7 @@ namespace HttpServer\Events; use Exception; use HttpServer\Abstracts\Callback; +use HttpServer\Exception\ExitException; use HttpServer\Http\Context; use HttpServer\Http\Request as HRequest; use HttpServer\Http\Response as HResponse; @@ -60,6 +61,8 @@ class OnRequest extends Callback return $sResponse->send($sRequest->isNotFound(), 200); } return Snowflake::app()->getRouter()->dispatch(); + } catch (ExitException $exception) { + return send($exception->getMessage(), $exception->getCode()); } catch (Error | \Throwable $exception) { return $this->sendErrorMessage($exception); } finally { diff --git a/HttpServer/Route/Router.php b/HttpServer/Route/Router.php index b7667000..5d2a9c4d 100644 --- a/HttpServer/Route/Router.php +++ b/HttpServer/Route/Router.php @@ -471,23 +471,19 @@ class Router extends Application implements RouterInterface /** - * @return mixed|void + * @return mixed * @throws */ - public function dispatch() + public function dispatch(): mixed { - try { - if (!($node = $this->find_path(\request()))) { - return send(self::NOT_FOUND, 404); + if (!($node = $this->find_path(\request()))) { + return send(self::NOT_FOUND, 404); + } else { + send($response = $node->dispatch(), 200); + if (!$node->hasAfter()) { + return null; } - send($node->dispatch(), 200); - if ($node->hasAfter()) { - $node->afterDispatch(\request()); - } - } catch (ExitException $exception) { - send($exception->getMessage(), $exception->getCode()); - } catch (\Throwable $exception) { - send($this->exception($exception), 200); + return $node->afterDispatch($response); } }