From 13aeefd2d3ec44ae6e9a483765b00ef64ca635b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 2 Sep 2020 15:45:52 +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 --- http-server/Events/OnRequest.php | 33 ++++++++++++++++++++-------- system/Abstracts/BaseApplication.php | 10 +++++++++ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/http-server/Events/OnRequest.php b/http-server/Events/OnRequest.php index 33bfd0b1..dbf97242 100644 --- a/http-server/Events/OnRequest.php +++ b/http-server/Events/OnRequest.php @@ -11,6 +11,7 @@ use HttpServer\Http\Request as HRequest; use HttpServer\Http\Response as HResponse; use HttpServer\Service\Http; use Snowflake\Core\JSON; +use Snowflake\Event; use Snowflake\Snowflake; use Swoole\Error; use Swoole\Http\Request; @@ -24,7 +25,6 @@ class OnRequest extends Callback { - /** * @param Request $request * @param Response $response @@ -36,15 +36,30 @@ class OnRequest extends Callback [$sRequest, $sResponse] = static::setContext($request, $response); $sResponse->send(Snowflake::get()->router->dispatch(), 200); } catch (Error | \Throwable $exception) { - if (!isset($sResponse)) { - $response->status(200); - $response->end($exception->getMessage()); - } else { - $sResponse->send($this->format($exception), 200); - } + $this->sendErrorMessage($sResponse ?? null, $exception, $response); } finally { - $dividing_line = str_pad('', 100, '-'); - $this->debug($dividing_line, 'app'); + $events = Snowflake::get()->getEvent(); + if (!$events->exists(Event::EVENT_AFTER_REQUEST)) { + return; + } + $events->trigger(Event::EVENT_AFTER_REQUEST, [$request]); + } + } + + + /** + * @param $sResponse + * @param $exception + * @param $response + * @throws Exception + */ + protected function sendErrorMessage($sResponse, $exception, $response) + { + if (empty($sResponse)) { + $response->status(200); + $response->end($exception->getMessage()); + } else { + $sResponse->send($this->format($exception), 200); } } diff --git a/system/Abstracts/BaseApplication.php b/system/Abstracts/BaseApplication.php index 5f6a1d36..3327677a 100644 --- a/system/Abstracts/BaseApplication.php +++ b/system/Abstracts/BaseApplication.php @@ -317,6 +317,16 @@ abstract class BaseApplication extends Service } + /** + * @return Event + * @throws ComponentException + */ + public function getEvent() + { + return $this->get('event'); + } + + /** * @return Jwt * @throws ComponentException