This commit is contained in:
2020-09-01 00:32:48 +08:00
parent 34cd277c25
commit e979332f01
3 changed files with 54 additions and 15 deletions
+24 -5
View File
@@ -43,20 +43,39 @@ class Middleware
} }
/** /**
* @param $dispatch * @param Node $node
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function getGenerate($dispatch) public function getGenerate($node)
{ {
$last = function ($passable) use ($dispatch) { $last = function ($passable) use ($node) {
return Dispatch::create($dispatch, $passable)->dispatch(); 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 = []; $this->middleWares = [];
return $data; 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 * @return Closure
*/ */
+21 -5
View File
@@ -62,6 +62,24 @@ class Node extends Application
return $this->newExec(); return $this->newExec();
} }
/**
* @return bool
*/
public function hasInterceptor()
{
return count($this->_interceptors) > 0;
}
/**
* @return array
*/
public function getInterceptor()
{
return $this->_interceptors;
}
/** /**
* @param $request * @param $request
* @return bool * @return bool
@@ -133,10 +151,7 @@ class Node extends Application
/** @var Annotation $annotation */ /** @var Annotation $annotation */
$annotation = Snowflake::createObject(Annotation::class); $annotation = Snowflake::createObject(Annotation::class);
if (!empty($methods)) { if (!empty($methods)) {
$annotations = $annotation->instance($reflect); $this->_interceptors = $annotation->instance($reflect, $action);
if (isset($annotations['Interceptor'])) {
}
} }
return [$reflect->newInstance(), $action]; return [$reflect->newInstance(), $action];
} catch (Exception $exception) { } catch (Exception $exception) {
@@ -146,6 +161,7 @@ class Node extends Application
} }
} }
/** /**
* @return string * @return string
* 错误信息 * 错误信息
@@ -297,7 +313,7 @@ class Node extends Application
if (!empty($this->handler)) { if (!empty($this->handler)) {
$made = new Middleware(); $made = new Middleware();
$made->setMiddleWares($this->middleware); $made->setMiddleWares($this->middleware);
$this->callback = $made->getGenerate($this->handler); $this->callback = $made->getGenerate($this);
} }
return $this; return $this;
} }
+9 -5
View File
@@ -49,11 +49,12 @@ abstract class BaseAnnotation extends Component
/** /**
* @param ReflectionClass $reflect * @param ReflectionClass $reflect
* @param string $method
* @param array $rules * @param array $rules
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
public function instance($reflect, $rules = []) public function instance($reflect, $method = '', $rules = [])
{ {
$annotations = $this->getPrivates($reflect); $annotations = $this->getPrivates($reflect);
@@ -62,11 +63,14 @@ abstract class BaseAnnotation extends Component
throw new Exception('Class ' . $reflect->getName() . ' cannot be instantiated.'); throw new Exception('Class ' . $reflect->getName() . ' cannot be instantiated.');
} }
$object = $reflect->newInstance();
$array = []; $array = [];
foreach ($classMethods as $classMethod) { $object = $reflect->newInstance();
$array = $this->resolveDocComment($classMethod, $object, $annotations, $array); 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; return $array;
} }