This commit is contained in:
2020-09-04 17:51:10 +08:00
parent f86ba7bc86
commit 1c3da9d835
2 changed files with 113 additions and 11 deletions
+81 -5
View File
@@ -3,6 +3,7 @@
namespace HttpServer\Route\Annotation; namespace HttpServer\Route\Annotation;
use HttpServer\Route\Node;
use ReflectionClass; use ReflectionClass;
use ReflectionException; use ReflectionException;
use Snowflake\Abstracts\BaseAnnotation; use Snowflake\Abstracts\BaseAnnotation;
@@ -29,19 +30,28 @@ class Annotation extends \Snowflake\Annotation\Annotation
*/ */
private $Limits = 'required|not empty'; private $Limits = 'required|not empty';
private $Method = 'post';
private $Middleware = '';
// private $Route = '';
protected $_annotations = []; protected $_annotations = [];
/** /**
* @param Node $node
* @param ReflectionClass $reflect * @param ReflectionClass $reflect
* @param $method * @param $method
* @param $annotations * @param $annotations
* @return mixed|null * @return mixed|null
* @throws ReflectionException * @throws ReflectionException
*/ */
public function read($reflect, $method, $annotations) public function read($node, $reflect, $method, $annotations)
{ {
$method = $reflect->getMethod($method); $method = $reflect->getMethod($method);
@@ -52,12 +62,80 @@ class Annotation extends \Snowflake\Annotation\Annotation
if (!in_array($keyName, $annotations)) { if (!in_array($keyName, $annotations)) {
continue; 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)); $array[$keyName] = $this->pop($this->getName(...$annotation));
} }
return $array; 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 $controller
* @param $methodName * @param $methodName
@@ -71,8 +149,6 @@ class Annotation extends \Snowflake\Annotation\Annotation
} }
/** /**
* @param $events * @param $events
* @return bool * @return bool
+32 -6
View File
@@ -4,6 +4,7 @@
namespace HttpServer\Route; namespace HttpServer\Route;
use Closure;
use HttpServer\Http\Request; use HttpServer\Http\Request;
use Exception; use Exception;
use HttpServer\Application; use HttpServer\Application;
@@ -19,7 +20,7 @@ class Node extends Application
public $path; public $path;
public $index = 0; public $index = 0;
public $method; public $method = [];
/** @var Node[] $childes */ /** @var Node[] $childes */
public $childes = []; public $childes = [];
@@ -46,7 +47,7 @@ class Node extends Application
*/ */
public function bindHandler($handler) public function bindHandler($handler)
{ {
if ($handler instanceof \Closure) { if ($handler instanceof Closure) {
$this->handler = $handler; $this->handler = $handler;
} else if (is_string($handler) && strpos($handler, '@') !== false) { } else if (is_string($handler) && strpos($handler, '@') !== false) {
list($controller, $action) = explode('@', $handler); list($controller, $action) = explode('@', $handler);
@@ -151,7 +152,7 @@ class Node extends Application
/** @var Annotation $annotation */ /** @var Annotation $annotation */
$annotation = Snowflake::app()->annotation->get('http'); $annotation = Snowflake::app()->annotation->get('http');
if (!empty($annotations = $annotation->getAnnotation(Annotation::class))) { if (!empty($annotations = $annotation->getAnnotation(Annotation::class))) {
$this->_interceptors = $annotation->read($reflect, $action, $annotations); $annotation->read($this, $reflect, $action, $annotations);
} }
return [$reflect->newInstance(), $action]; return [$reflect->newInstance(), $action];
} catch (Exception $exception) { } 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 * @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 * @throws Exception
*/ */
@@ -319,10 +345,10 @@ class Node extends Application
return $_temp; return $_temp;
} }
foreach ($array as $class) { foreach ($array as $class) {
if (is_array($class)) { if (!is_array($class)) {
$_temp = $this->each($class, $_temp);
} else {
$_temp[] = Snowflake::createObject($class); $_temp[] = Snowflake::createObject($class);
} else {
$_temp = $this->each($class, $_temp);
} }
} }
return $_temp; return $_temp;