diff --git a/http-server/Route/Middleware.php b/http-server/Route/Middleware.php index 8cae7325..9f745cb3 100644 --- a/http-server/Route/Middleware.php +++ b/http-server/Route/Middleware.php @@ -43,20 +43,39 @@ class Middleware } /** - * @param $dispatch + * @param Node $node * @return mixed * @throws Exception */ - public function getGenerate($dispatch) + public function getGenerate($node) { - $last = function ($passable) use ($dispatch) { - return Dispatch::create($dispatch, $passable)->dispatch(); + $last = function ($passable) use ($node) { + return Dispatch::create($node->handler, $passable)->dispatch(); }; - $data = array_reduce(array_reverse($this->middleWares), $this->core(), $last); + $middleWares = $this->annotation($node); + $data = array_reduce(array_reverse($middleWares), $this->core(), $last); $this->middleWares = []; return $data; } + + /** + * @param Node $node + * @return array + */ + public function annotation($node) + { + $middleWares = $this->middleWares; + if (!$node->hasInterceptor()) { + return $middleWares; + } + foreach ($node->getInterceptor() as $item) { + $middleWares[] = $item; + } + return $middleWares; + } + + /** * @return Closure */ diff --git a/http-server/Route/Node.php b/http-server/Route/Node.php index 4e5ef828..ae61889f 100644 --- a/http-server/Route/Node.php +++ b/http-server/Route/Node.php @@ -62,6 +62,24 @@ class Node extends Application return $this->newExec(); } + + /** + * @return bool + */ + public function hasInterceptor() + { + return count($this->_interceptors) > 0; + } + + + /** + * @return array + */ + public function getInterceptor() + { + return $this->_interceptors; + } + /** * @param $request * @return bool @@ -133,10 +151,7 @@ class Node extends Application /** @var Annotation $annotation */ $annotation = Snowflake::createObject(Annotation::class); if (!empty($methods)) { - $annotations = $annotation->instance($reflect); - if (isset($annotations['Interceptor'])) { - } - + $this->_interceptors = $annotation->instance($reflect, $action); } return [$reflect->newInstance(), $action]; } catch (Exception $exception) { @@ -146,6 +161,7 @@ class Node extends Application } } + /** * @return string * 错误信息 @@ -297,7 +313,7 @@ class Node extends Application if (!empty($this->handler)) { $made = new Middleware(); $made->setMiddleWares($this->middleware); - $this->callback = $made->getGenerate($this->handler); + $this->callback = $made->getGenerate($this); } return $this; } diff --git a/system/Abstracts/BaseAnnotation.php b/system/Abstracts/BaseAnnotation.php index 53ea762a..49d95b0e 100644 --- a/system/Abstracts/BaseAnnotation.php +++ b/system/Abstracts/BaseAnnotation.php @@ -49,11 +49,12 @@ abstract class BaseAnnotation extends Component /** * @param ReflectionClass $reflect + * @param string $method * @param array $rules * @return array * @throws Exception */ - public function instance($reflect, $rules = []) + public function instance($reflect, $method = '', $rules = []) { $annotations = $this->getPrivates($reflect); @@ -62,11 +63,14 @@ abstract class BaseAnnotation extends Component throw new Exception('Class ' . $reflect->getName() . ' cannot be instantiated.'); } - $object = $reflect->newInstance(); - $array = []; - foreach ($classMethods as $classMethod) { - $array = $this->resolveDocComment($classMethod, $object, $annotations, $array); + $object = $reflect->newInstance(); + if (!empty($method)) { + $array = $this->resolveDocComment($reflect->getMethod($method), $object, $annotations, $array); + } else { + foreach ($classMethods as $classMethod) { + $array = $this->resolveDocComment($classMethod, $object, $annotations, $array); + } } return $array; }