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
* @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
*/
+21 -5
View File
@@ -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;
}
+9 -5
View File
@@ -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;
}