From e84e23e668d4fcc816a86962d652406ec0835676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 16 Mar 2021 10:53: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 | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/HttpServer/Events/OnRequest.php b/HttpServer/Events/OnRequest.php index d7cdeabf..2ad2adcb 100644 --- a/HttpServer/Events/OnRequest.php +++ b/HttpServer/Events/OnRequest.php @@ -11,6 +11,7 @@ use HttpServer\Http\Request as HRequest; use HttpServer\Http\Response as HResponse; use ReflectionException; use Snowflake\Core\Json; +use Snowflake\Error\Logger; use Snowflake\Event; use Snowflake\Exception\ComponentException; use Snowflake\Exception\NotFindClassException; @@ -28,6 +29,24 @@ class OnRequest extends Callback { + public Event $event; + public Logger $logger; + + + /** + * @throws Exception + */ + public function init() + { + $this->event = Snowflake::app()->getEvent(); + $this->logger = Snowflake::app()->getLogger(); + } + + + const BEFORE_REQUEST = 'beforeRequest'; + const AFTER_REQUEST = 'afterRequest'; + + /** * @param Request $request * @param Response $response @@ -36,21 +55,25 @@ class OnRequest extends Callback */ public function onHandler(Request $request, Response $response): mixed { - Coroutine::defer(function () use ($request) { - fire(Event::SYSTEM_RESOURCE_RELEASES); - logger()->insert(); - }); try { + $this->event->trigger(self::BEFORE_REQUEST, [$request]); + [$request, $response] = static::create($request, $response); if (!$request->is('favicon.ico')) { return \router()->dispatch(); } + return \send(null); } catch (ExitException | Error | \Throwable $exception) { if ($exception instanceof ExitException) { return \send(Json::to($exception->getCode(), $exception->getMessage())); } return $this->sendErrorMessage($request, $response, $exception); + } finally { + $this->event->trigger(Event::SYSTEM_RESOURCE_RELEASES); + $this->event->trigger(self::AFTER_REQUEST, [$request]); + + $this->logger->insert(); } }