diff --git a/HttpServer/Route/Annotation/Annotation.php b/HttpServer/Route/Annotation/Annotation.php index e70c464d..9fb1cc76 100644 --- a/HttpServer/Route/Annotation/Annotation.php +++ b/HttpServer/Route/Annotation/Annotation.php @@ -82,6 +82,9 @@ class Annotation extends \Snowflake\Annotation\Annotation case 'Middleware': $this->bindMiddleware($node, $annotation); break; + case 'Limits': + $this->bindLimits($node, $annotation); + break; } } @@ -144,6 +147,27 @@ class Annotation extends \Snowflake\Annotation\Annotation } } + + /** + * @param Node $node + * @param $annotation + * @throws + */ + private function bindLimits($node, $annotation) + { + if (!isset($annotation[1][2])) { + return; + } + + $explode = explode(',', $annotation[1][2]); + + [$keyName, $matchs] = $annotation; + foreach ($explode as $middleware) { + $params = [$keyName, [$matchs[0], $matchs[1], $middleware]]; + $node->addLimits($this->pop($this->getName(...$params))); + } + } + /** * @param $controller * @param $methodName diff --git a/HttpServer/Route/Middleware.php b/HttpServer/Route/Middleware.php index 22ce5fa2..99f28dba 100644 --- a/HttpServer/Route/Middleware.php +++ b/HttpServer/Route/Middleware.php @@ -63,15 +63,32 @@ class Middleware * @param Node $node * @return array */ - public function annotation($node) + protected function annotation($node) { $middleWares = $this->middleWares; if (!$node->hasInterceptor()) { - return $middleWares; + return $this->annotation_limit($node, $middleWares); } foreach ($node->getInterceptor() as $item) { $middleWares[] = $item[0]; } + return $this->annotation_limit($node, $middleWares); + } + + + /** + * @param Node $node + * @param $middleWares + * @return array + */ + protected function annotation_limit($node, $middleWares) + { + if (!$node->hasLimits()) { + return $middleWares; + } + foreach ($node->getLimits() as $item) { + $middleWares[] = $item[0]; + } return $middleWares; } diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index 0c29b044..12bb7231 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -39,6 +39,7 @@ class Node extends Application public $callback = []; private $_interceptors = []; + private $_limits = []; /** * @param $handler @@ -73,6 +74,15 @@ class Node extends Application } + /** + * @return bool + */ + public function hasLimits() + { + return count($this->_limits) > 0; + } + + /** * @return array */ @@ -81,6 +91,16 @@ class Node extends Application return $this->_interceptors; } + + + /** + * @return array + */ + public function getLimits() + { + return $this->_limits; + } + /** * @param $request * @return bool @@ -174,6 +194,17 @@ class Node extends Application } + /** + * @param Closure|array|string $handler + * @throws Exception + */ + public function addLimits($handler) + { + $this->_limits[] = $handler; + $this->restructure(); + } + + /** * @return string * 错误信息