This commit is contained in:
2021-04-20 12:07:46 +08:00
parent 78d89f8a71
commit 84f4bf14e0
2 changed files with 22 additions and 19 deletions
+16 -18
View File
@@ -113,9 +113,7 @@ class Node extends HttpService
$this->annotationInject(get_class($controller), $action); $this->annotationInject(get_class($controller), $action);
} }
if (!empty($this->handler)) { if (!empty($this->handler)) {
$this->callback = Reduce::reduce(function () { $this->callback = Reduce::reduce($this->createDispatch(), $this->annotation());
return call_user_func($this->handler, func_get_args());
}, $this->annotation());
} }
return $this; return $this;
} }
@@ -127,7 +125,7 @@ class Node extends HttpService
public function createDispatch(): Closure public function createDispatch(): Closure
{ {
return function () { return function () {
return call_user_func($this->handler, func_get_args()); return call_user_func($this->handler, ...func_get_args());
}; };
} }
@@ -534,10 +532,17 @@ class Node extends HttpService
$dispatchParams = [\request()]; $dispatchParams = [\request()];
} }
if ($this->handler instanceof Closure) { if ($this->handler instanceof Closure) {
return call_user_func($this->handler, $dispatchParams); return call_user_func($this->handler, ...$dispatchParams);
} }
return $this->runWith($this->callback, $dispatchParams);
return $this->runFilter(...$dispatchParams); $validator = $this->getValidator();
if (!($validator instanceof Validator)) {
return call_user_func($this->callback, ...$dispatchParams);
}
if ($validator->validation()) {
return call_user_func($this->callback, ...$dispatchParams);
}
return Json::to(5005, $validator->getError());
} catch (Throwable $throwable) { } catch (Throwable $throwable) {
$this->addError($throwable, 'throwable'); $this->addError($throwable, 'throwable');
@@ -549,21 +554,14 @@ class Node extends HttpService
/** /**
* @return mixed * @return Validator
* @throws \Exception * @throws Exception
*/ */
private function runFilter(): mixed private function getValidator(): Validator
{ {
/** @var HttpFilter $filter */ /** @var HttpFilter $filter */
$filter = Snowflake::app()->get('filter'); $filter = Snowflake::app()->get('filter');
$validator = $filter->check(get_class($this->handler[0]), $this->handler[1]); return $filter->check(get_class($this->handler[0]), $this->handler[1]);
if (!($validator instanceof Validator)) {
return $this->runWith($this->callback, func_get_args());
}
if (!$validator->validation()) {
return Json::to(5005, $validator->getError());
}
return $this->runWith($this->callback, func_get_args());
} }
+5
View File
@@ -6,6 +6,7 @@ namespace HttpServer\Route;
use Closure; use Closure;
use HttpServer\IInterface\After; use HttpServer\IInterface\After;
use HttpServer\IInterface\Middleware;
use Snowflake\Core\Json; use Snowflake\Core\Json;
class Reduce class Reduce
@@ -52,7 +53,11 @@ class Reduce
{ {
return function ($stack, $pipe) { return function ($stack, $pipe) {
return function ($passable) use ($stack, $pipe) { return function ($passable) use ($stack, $pipe) {
if ($pipe instanceof Middleware) {
return $pipe->onHandler($passable, $stack);
} else {
return call_user_func($pipe, $passable, $stack); return call_user_func($pipe, $passable, $stack);
}
}; };
}; };
} }