diff --git a/HttpServer/Events/OnClose.php b/HttpServer/Events/OnClose.php index 02eab9ab..6f246541 100644 --- a/HttpServer/Events/OnClose.php +++ b/HttpServer/Events/OnClose.php @@ -10,6 +10,7 @@ use HttpServer\Route\Annotation\Tcp; use HttpServer\Route\Annotation\Websocket; use HttpServer\Route\Annotation\Websocket as AWebsocket; use Snowflake\Event; +use Snowflake\Exception\ComponentException; use Snowflake\Snowflake; use Swoole\Coroutine; use Swoole\Server; @@ -32,12 +33,23 @@ class OnClose extends Callback * @throws Exception */ public function onHandler(Server $server, int $fd) + { + Coroutine::defer(function () { + fire(Event::EVENT_AFTER_REQUEST); + }); + $this->execute($server, $fd); + } + + + /** + * @param Server $server + * @param int $fd + * @throws ComponentException + * @throws Exception + */ + private function execute(Server $server, int $fd) { try { - $event = Snowflake::app()->getEvent(); - Coroutine::defer(function () use ($event) { - $event->trigger(Event::EVENT_AFTER_REQUEST); - }); [$manager, $name] = $this->resolve($server, $fd); if (empty($manager) || !$manager->has($name)) { return; diff --git a/HttpServer/Events/OnHandshake.php b/HttpServer/Events/OnHandshake.php index f5439d16..cf14615c 100644 --- a/HttpServer/Events/OnHandshake.php +++ b/HttpServer/Events/OnHandshake.php @@ -76,11 +76,23 @@ class OnHandshake extends Callback * @throws Exception */ public function onHandler(SRequest $request, SResponse $response) + { + Coroutine::defer(function () { + fire(Event::EVENT_AFTER_REQUEST); + }); + $this->execute($request, $response); + } + + + /** + * @param SRequest $request + * @param SResponse $response + * @return mixed|bool|null + * @throws ComponentException + */ + private function execute(SRequest $request, SResponse $response) { try { - Coroutine::defer(function () { - fire(Event::EVENT_AFTER_REQUEST); - }); $this->resolveParse($request, $response); $manager = Snowflake::app()->annotation->websocket; diff --git a/HttpServer/Events/OnRequest.php b/HttpServer/Events/OnRequest.php index 3b203ac2..b2f7f886 100644 --- a/HttpServer/Events/OnRequest.php +++ b/HttpServer/Events/OnRequest.php @@ -14,6 +14,7 @@ use HttpServer\Service\Http; use Snowflake\Abstracts\Config; use Snowflake\Core\JSON; use Snowflake\Event; +use Snowflake\Exception\ComponentException; use Snowflake\Snowflake; use Swoole\Coroutine; use Swoole\Error; @@ -35,9 +36,22 @@ class OnRequest extends Callback * @throws Exception */ public function onHandler(Request $request, Response $response) + { + Coroutine::defer(function () { + fire(Event::EVENT_AFTER_REQUEST); + }); + $this->onRequest($request, $response); + } + + + /** + * @param Request $request + * @param Response $response + * @throws Exception + */ + public function onRequest(Request $request, Response $response) { try { - Coroutine::defer([$this, 'onDefer']); /** @var HRequest $sRequest */ [$sRequest, $sResponse] = [HRequest::create($request), HResponse::create($response)]; if ($sRequest->is('favicon.ico')) { @@ -51,15 +65,6 @@ class OnRequest extends Callback } - /** - * @throws \Snowflake\Exception\ComponentException - */ - public function onDefer() - { - fire(Event::EVENT_AFTER_REQUEST); - } - - /** * @param $response * @throws Exception