diff --git a/HttpServer/Events/OnRequest.php b/HttpServer/Events/OnRequest.php index 7b7d3e19..d361bb28 100644 --- a/HttpServer/Events/OnRequest.php +++ b/HttpServer/Events/OnRequest.php @@ -14,6 +14,7 @@ use HttpServer\Service\Http; use Snowflake\Core\JSON; use Snowflake\Event; use Snowflake\Snowflake; +use Swoole\Coroutine; use Swoole\Error; use Swoole\Http\Request; use Swoole\Http\Response; @@ -40,7 +41,7 @@ class OnRequest extends Callback if ($sRequest->is('favicon.ico')) { return $sResponse->send($sRequest->isNotFound(), 200); } - $sResponse->send(Snowflake::app()->router->dispatch(), 200); + return Snowflake::app()->getRouter()->dispatch(); } catch (Error | \Throwable $exception) { $this->sendErrorMessage($sResponse ?? null, $exception, $response); } finally { diff --git a/HttpServer/Route/Annotation/Annotation.php b/HttpServer/Route/Annotation/Annotation.php index f3ba7ea4..a7928899 100644 --- a/HttpServer/Route/Annotation/Annotation.php +++ b/HttpServer/Route/Annotation/Annotation.php @@ -142,10 +142,7 @@ class Annotation extends \Snowflake\Annotation\Annotation if (!isset($annotation[1][2])) { return; } - $explode = explode(',', $annotation[1][2]); - - [$keyName, $matchs] = $annotation; foreach ($explode as $middleware) { $middleware = 'App\Http\Interceptor\\' . $middleware; if (!class_exists($middleware)) { @@ -156,10 +153,6 @@ class Annotation extends \Snowflake\Annotation\Annotation continue; } $node->addInterceptor([$middleware, 'Interceptor']); - continue; - - $params = [$keyName, [$matchs[0], $matchs[1], $middleware]]; - $node->addInterceptor($this->pop($this->getName(...$params))); } } @@ -176,8 +169,6 @@ class Annotation extends \Snowflake\Annotation\Annotation } $explode = explode(',', $annotation[1][2]); - - [$keyName, $matchs] = $annotation; foreach ($explode as $middleware) { $middleware = 'App\Http\After\\' . $middleware; if (!class_exists($middleware)) { @@ -204,8 +195,6 @@ class Annotation extends \Snowflake\Annotation\Annotation } $explode = explode(',', $annotation[1][2]); - - [$keyName, $matchs] = $annotation; foreach ($explode as $middleware) { $middleware = 'App\Http\Limits\\' . $middleware; if (!class_exists($middleware)) { @@ -216,10 +205,6 @@ class Annotation extends \Snowflake\Annotation\Annotation continue; } $node->addLimits([$middleware, 'next']); - continue; - - $params = [$keyName, [$matchs[0], $matchs[1], $middleware]]; - $node->addLimits($this->pop($this->getName(...$params))); } } diff --git a/HttpServer/Route/Middleware.php b/HttpServer/Route/Middleware.php index 9ef0f63f..39db32ab 100644 --- a/HttpServer/Route/Middleware.php +++ b/HttpServer/Route/Middleware.php @@ -52,14 +52,7 @@ class Middleware public function getGenerate($node) { $last = function ($passable) use ($node) { - $responseData = Dispatch::create($node->handler, $passable)->dispatch(); - Coroutine::defer(function () use ($node, $responseData) { - if ($node->hasAfter()) { - $node->afterDispatch($responseData); - } - }); - return $responseData; -// response()->send($responseData, 200); + return Dispatch::create($node->handler, $passable)->dispatch(); }; return $node->callback = Reduce::reduce($last, $this->annotation($node)); } diff --git a/HttpServer/Route/Router.php b/HttpServer/Route/Router.php index f5e63a52..9bf47f48 100644 --- a/HttpServer/Route/Router.php +++ b/HttpServer/Route/Router.php @@ -14,6 +14,7 @@ use Snowflake\Abstracts\Config; use Snowflake\Core\JSON; use Snowflake\Exception\ConfigException; use Snowflake\Snowflake; +use Swoole\Coroutine; /** * Class Router @@ -26,6 +27,8 @@ class Router extends Application implements RouterInterface public $groupTacks = []; public $dir = 'App\\Http\\Controllers'; + const NOT_FOUND = 'Page not found or method not allowed.'; + /** @var string[] */ public $methods = ['get', 'post', 'options', 'put', 'delete', 'receive']; @@ -407,17 +410,36 @@ class Router extends Application implements RouterInterface return $newPath; } + /** - * @return mixed + * @return mixed|void * @throws */ public function dispatch() { $request = Context::getContext('request'); if (!($node = $this->find_path($request))) { - return JSON::to(404, 'Page not found or method not allowed.'); + $response = JSON::to(404, self::NOT_FOUND); + response()->send($response, 200); + } else { + response()->send($response = $node->dispatch(), 200); } - return $node->dispatch(); + $this->onAfter($response, $node); + } + + + /** + * @param $data + * @param null|Node $node + */ + private function onAfter($data, $node = null) + { + Coroutine::defer(function () use ($node, $data) { + if (!$node->hasAfter()) { + return; + } + $node->afterDispatch($data); + }); }