diff --git a/HttpServer/Route/Annotation/Annotation.php b/HttpServer/Route/Annotation/Annotation.php index 5502e996..8c42753e 100644 --- a/HttpServer/Route/Annotation/Annotation.php +++ b/HttpServer/Route/Annotation/Annotation.php @@ -3,6 +3,7 @@ namespace HttpServer\Route\Annotation; +use HttpServer\Route\Node; use ReflectionClass; use ReflectionException; use Snowflake\Abstracts\BaseAnnotation; @@ -29,19 +30,28 @@ class Annotation extends \Snowflake\Annotation\Annotation */ private $Limits = 'required|not empty'; + + private $Method = 'post'; + + + private $Middleware = ''; + + +// private $Route = ''; + + protected $_annotations = []; - - /** + * @param Node $node * @param ReflectionClass $reflect * @param $method * @param $annotations * @return mixed|null * @throws ReflectionException */ - public function read($reflect, $method, $annotations) + public function read($node, $reflect, $method, $annotations) { $method = $reflect->getMethod($method); @@ -52,12 +62,80 @@ class Annotation extends \Snowflake\Annotation\Annotation if (!in_array($keyName, $annotations)) { continue; } + + if ($keyName == 'Method') { + $this->bindMethod($node, $annotation); + } else if ($keyName == 'Middleware') { + $this->bindMiddleware($node, $annotation); + } else if ($keyName == 'Interceptors') { + $this->bindInterceptors($node, $annotation); + } + $array[$keyName] = $this->pop($this->getName(...$annotation)); } return $array; } + /** + * @param $node + * @param $annotation + */ + private function bindMethod($node, $annotation) + { + if (!isset($annotation[1][2])) { + return; + } + $explode = explode(',', $annotation[1][2]); + if (in_array('any', $explode)) { + $explode = ['*']; + } + $node->method = $explode; + } + + + /** + * @param Node $node + * @param $annotation + * @throws + */ + private function bindMiddleware($node, $annotation) + { + if (!isset($annotation[1][2])) { + return; + } + + $explode = explode(',', $annotation[1][2]); + foreach ($explode as $middleware) { + $middleware = 'App\Http\Interceptor\\' . $middleware; + if (!class_exists($middleware)) { + continue; + } + $node->addMiddleware($middleware); + } + + } + + + /** + * @param Node $node + * @param $annotation + * @throws + */ + private function bindInterceptors($node, $annotation) + { + if (!isset($annotation[1][2])) { + return; + } + + $explode = explode(',', $annotation[1][2]); + foreach ($explode as $middleware) { + $node->addInterceptor($middleware); + } + + } + + /** * @param $controller * @param $methodName @@ -71,8 +149,6 @@ class Annotation extends \Snowflake\Annotation\Annotation } - - /** * @param $events * @return bool diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index 2b5ec6b9..8aa43592 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -4,6 +4,7 @@ namespace HttpServer\Route; +use Closure; use HttpServer\Http\Request; use Exception; use HttpServer\Application; @@ -19,7 +20,7 @@ class Node extends Application public $path; public $index = 0; - public $method; + public $method = []; /** @var Node[] $childes */ public $childes = []; @@ -46,7 +47,7 @@ class Node extends Application */ public function bindHandler($handler) { - if ($handler instanceof \Closure) { + if ($handler instanceof Closure) { $this->handler = $handler; } else if (is_string($handler) && strpos($handler, '@') !== false) { list($controller, $action) = explode('@', $handler); @@ -151,7 +152,7 @@ class Node extends Application /** @var Annotation $annotation */ $annotation = Snowflake::app()->annotation->get('http'); if (!empty($annotations = $annotation->getAnnotation(Annotation::class))) { - $this->_interceptors = $annotation->read($reflect, $action, $annotations); + $annotation->read($this, $reflect, $action, $annotations); } return [$reflect->newInstance(), $action]; } catch (Exception $exception) { @@ -162,6 +163,17 @@ class Node extends Application } + /** + * @param Closure|array|string $handler + * @throws Exception + */ + public function addInterceptor($handler) + { + $this->_interceptors[] = $handler; + $this->restructure(); + } + + /** * @return string * 错误信息 @@ -292,6 +304,20 @@ class Node extends Application } + /** + * @param string $class + * @throws Exception + */ + public function addMiddleware(string $class) + { +// if (in_array($class, $this->middleware)) { +// return; +// } + $this->middleware[] = $class; + $this->restructure(); + } + + /** * @throws Exception */ @@ -319,10 +345,10 @@ class Node extends Application return $_temp; } foreach ($array as $class) { - if (is_array($class)) { - $_temp = $this->each($class, $_temp); - } else { + if (!is_array($class)) { $_temp[] = Snowflake::createObject($class); + } else { + $_temp = $this->each($class, $_temp); } } return $_temp;