diff --git a/HttpServer/Events/OnRequest.php b/HttpServer/Events/OnRequest.php index 1479486a..c5215547 100644 --- a/HttpServer/Events/OnRequest.php +++ b/HttpServer/Events/OnRequest.php @@ -40,9 +40,6 @@ class OnRequest extends Callback Coroutine::defer(function () { fire(Event::EVENT_AFTER_REQUEST, [$sRequest ?? null]); }); - if (Config::get('debug.enable', false, false)) { - function_exists('trackerHookMalloc') && trackerHookMalloc(); - } /** @var HRequest $sRequest */ [$sRequest, $sResponse] = [HRequest::create($request), HResponse::create($response)]; if ($sRequest->is('favicon.ico')) { diff --git a/HttpServer/Http/Formatter/JsonFormatter.php b/HttpServer/Http/Formatter/JsonFormatter.php index 285c7988..1aea101f 100644 --- a/HttpServer/Http/Formatter/JsonFormatter.php +++ b/HttpServer/Http/Formatter/JsonFormatter.php @@ -20,9 +20,9 @@ class JsonFormatter extends Application implements IFormatter { public $data; - public $status = 200; + public int $status = 200; - public $header = []; + public array $header = []; /** * @param $data diff --git a/HttpServer/Route/Middleware.php b/HttpServer/Route/Middleware.php index 4dfa1945..3df08425 100644 --- a/HttpServer/Route/Middleware.php +++ b/HttpServer/Route/Middleware.php @@ -51,10 +51,9 @@ class Middleware */ public function getGenerate(Node $node) { - $last = function ($passable) use ($node) { + return $node->callback = Reduce::reduce(function ($passable) use ($node) { return Dispatch::create($node->handler, $passable)->dispatch(); - }; - return $node->callback = Reduce::reduce($last, $this->annotation($node)); + }, $this->annotation($node)); } diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index 8e3677be..598c8448 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -10,8 +10,10 @@ use HttpServer\Http\Request; use Exception; use HttpServer\Application; use HttpServer\Route\Annotation\Http; +use ReflectionException; use Snowflake\Core\JSON; use Snowflake\Event; +use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; use Swoole\Coroutine; @@ -36,14 +38,14 @@ class Node extends Application public array $rules = []; /** @var ?Closure|?array */ - public $handler; + public Closure|array|null $handler; public string $htmlSuffix = '.html'; public bool $enableHtmlSuffix = false; public array $namespace = []; public array $middleware = []; - /** @var array|Closure */ - public $callback = []; + /** @var array|Closure */ + public Closure|array $callback = []; private string $_alias = ''; @@ -71,7 +73,7 @@ class Node extends Application } else { $this->handler = $handler; } - return $this; + return $this->restructure(); } @@ -225,10 +227,10 @@ class Node extends Application * @param Closure|array|string $handler * @throws Exception */ - public function addInterceptor($handler) + public function addInterceptor(Closure|string|array $handler) { $this->_interceptors[] = $handler; - + $this->restructure(); } @@ -236,9 +238,10 @@ class Node extends Application * @param Closure|array|string $handler * @throws Exception */ - public function addAfter($handler) + public function addAfter(Closure|string|array $handler) { $this->_after[] = $handler; + $this->restructure(); } @@ -246,10 +249,10 @@ class Node extends Application * @param Closure|array|string $handler * @throws Exception */ - public function addLimits($handler) + public function addLimits(Closure|string|array $handler) { $this->_limits[] = $handler; - + $this->restructure(); } @@ -356,7 +359,7 @@ class Node extends Application { $limits = Snowflake::app()->getLimits(); $limits->addLimits($this->path, $limit, $duration, $isBindConsumer); - return $this; + return $this->restructure(); } @@ -371,14 +374,17 @@ class Node extends Application return; } $this->middleware = $this->each($middles, $_tmp); + $this->restructure(); } /** - * @param string|\Closure $class + * @param array|Closure|string $class + * @throws ReflectionException + * @throws NotFindClassException * @throws Exception */ - public function addMiddleware($class) + public function addMiddleware(Closure|string|array $class) { if (!is_callable($class, true)) { return; @@ -391,6 +397,7 @@ class Node extends Application $class = [$class, 'handler']; } $this->middleware[] = $class; + $this->restructure(); } @@ -415,7 +422,6 @@ class Node extends Application */ public function dispatch() { - $node = $this->restructure(); if (empty($node->callback)) { return JSON::to(404, $node->_error ?? 'Page not found.'); }