This commit is contained in:
2020-12-16 15:20:51 +08:00
parent 991c3f9145
commit c2b3f02bfc
7 changed files with 158 additions and 34 deletions
+73 -21
View File
@@ -22,6 +22,9 @@ class Annotation extends Component
private array $_annotations = []; private array $_annotations = [];
private array $_classes = [];
private array $_targets = []; private array $_targets = [];
@@ -100,21 +103,44 @@ class Annotation extends Component
*/ */
private function getReflect(string $class, string $alias): array private function getReflect(string $class, string $alias): array
{ {
$reflect = Snowflake::getDi()->getReflect($class); $reflect = $this->reflectClass($class);
if (!$reflect->isInstantiable()) { if ($reflect->isInstantiable()) {
$object = $reflect->newInstance();
foreach ($reflect->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
$tmp = $this->resolveAnnotations($method, $alias, $object);
$this->_classes[$reflect->getName()][$method->getName()] = $tmp;
}
}
return $this->targets($reflect);
}
/**
* @param string $class
* @return ReflectionClass
* @throws ReflectionException
*/
private function reflectClass(string $class): ReflectionClass
{
return Snowflake::getDi()->getReflect($class);
}
/**
* @param $method
* @param $alias
* @param $object
* @return array
*/
private function resolveAnnotations($method, $alias, $object): array
{
$attributes = $method->getAttributes();
if (count($attributes) < 1) {
return []; return [];
} }
$this->targets($reflect);
$annotations = [];
$object = $reflect->newInstance();
foreach ($reflect->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
$names = []; $names = [];
$attributes = $method->getAttributes();
if (count($attributes) < 1) {
continue;
}
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
$names[$attribute->getName()] = $this->instance($attribute); $names[$attribute->getName()] = $this->instance($attribute);
} }
@@ -123,28 +149,54 @@ class Annotation extends Component
$tmp['attributes'] = $names; $tmp['attributes'] = $names;
$this->_annotations[$alias][] = $tmp; $this->_annotations[$alias][] = $tmp;
return $tmp;
} }
return $annotations;
/**
* @param $className
* @param string $method
* @return array
*/
public function getByClass($className, $method = ''): array
{
if (!isset($this->_classes[$className])) {
return [];
} }
if (empty($method)) {
return $this->_classes[$className];
}
foreach ($this->_classes[$className] as $_method => $class) {
if ($method == $_method) {
return [$class];
}
}
return [];
}
/** /**
* @param ReflectionClass $reflect * @param ReflectionClass $reflect
* @return array
*/ */
private function targets(ReflectionClass $reflect) private function targets(ReflectionClass $reflect): array
{ {
$name = $reflect->getName();
if (!isset($this->_classes[$name])) {
$this->_classes[$name] = [];
}
$attributes = $reflect->getAttributes(); $attributes = $reflect->getAttributes();
if (count($attributes) < 1) { if (count($attributes) > 0) {
return; if (!isset($this->_targets[$name])) {
$this->_targets[$name] = [];
} }
if (!isset($this->_targets[$reflect->getName()])) {
$this->_targets[$reflect->getName()] = [];
}
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
$this->_targets[$reflect->getName()][] = $this->instance($attribute); $this->_targets[$name][] = $this->instance($attribute);
} }
} }
return [];
}
/** /**
+4
View File
@@ -4,6 +4,9 @@
namespace Annotation; namespace Annotation;
use Exception;
use validator\Validator;
/** /**
* Class RequestValidator * Class RequestValidator
* @package Annotation * @package Annotation
@@ -14,6 +17,7 @@ namespace Annotation;
/** /**
* RequestValidator constructor. * RequestValidator constructor.
* @param array $validators * @param array $validators
* @throws Exception
*/ */
public function __construct(public array $validators) public function __construct(public array $validators)
{ {
+9
View File
@@ -4,6 +4,8 @@
namespace Annotation\Route; namespace Annotation\Route;
use Snowflake\Snowflake;
/** /**
* Class Interceptor * Class Interceptor
* @package Annotation\Route * @package Annotation\Route
@@ -15,9 +17,16 @@ namespace Annotation\Route;
/** /**
* Interceptor constructor. * Interceptor constructor.
* @param \HttpServer\IInterface\After|\HttpServer\IInterface\After[] $after * @param \HttpServer\IInterface\After|\HttpServer\IInterface\After[] $after
* @throws
*/ */
public function __construct(public string|array $after) public function __construct(public string|array $after)
{ {
if (is_string($this->after)) {
$this->after = [$this->after];
}
foreach ($this->after as $key => $item) {
$this->after[$key] = Snowflake::createObject($item);
}
} }
} }
+9
View File
@@ -4,6 +4,8 @@
namespace Annotation\Route; namespace Annotation\Route;
use Snowflake\Snowflake;
/** /**
* Class Interceptor * Class Interceptor
* @package Annotation\Route * @package Annotation\Route
@@ -15,9 +17,16 @@ namespace Annotation\Route;
/** /**
* Interceptor constructor. * Interceptor constructor.
* @param string|array $interceptor * @param string|array $interceptor
* @throws
*/ */
public function __construct(public string|array $interceptor) public function __construct(public string|array $interceptor)
{ {
if (is_string($this->interceptor)) {
$this->interceptor = [$this->interceptor];
}
foreach ($this->interceptor as $key => $item) {
$this->interceptor[$key] = Snowflake::createObject($item);
}
} }
} }
+9
View File
@@ -4,6 +4,8 @@
namespace Annotation\Route; namespace Annotation\Route;
use Snowflake\Snowflake;
/** /**
* Class Limits * Class Limits
* @package Annotation\Route * @package Annotation\Route
@@ -15,9 +17,16 @@ namespace Annotation\Route;
/** /**
* Limits constructor. * Limits constructor.
* @param string|array $limits * @param string|array $limits
* @throws
*/ */
public function __construct(public string|array $limits) public function __construct(public string|array $limits)
{ {
if (is_string($this->limits)) {
$this->limits = [$this->limits];
}
foreach ($this->limits as $key => $item) {
$this->limits[$key] = Snowflake::createObject($item);
}
} }
+9
View File
@@ -4,6 +4,8 @@
namespace Annotation\Route; namespace Annotation\Route;
use Snowflake\Snowflake;
/** /**
* Class Middleware * Class Middleware
* @package Annotation\Route * @package Annotation\Route
@@ -15,9 +17,16 @@ namespace Annotation\Route;
/** /**
* Interceptor constructor. * Interceptor constructor.
* @param string|array $middleware * @param string|array $middleware
* @throws
*/ */
public function __construct(public string|array $middleware) public function __construct(public string|array $middleware)
{ {
if (is_string($this->middleware)) {
$this->middleware = [$this->middleware];
}
foreach ($this->middleware as $key => $item) {
$this->middleware[$key] = Snowflake::createObject($item);
}
} }
} }
+34 -2
View File
@@ -7,12 +7,15 @@
*/ */
declare(strict_types=1); declare(strict_types=1);
// declare(strict_types=1);
namespace HttpServer\Route; namespace HttpServer\Route;
use Annotation\Route\After;
use Annotation\Route\Interceptor;
use Annotation\Route\Middleware as RMiddleware;
use Exception; use Exception;
use Annotation\Route\Limits;
use HttpServer\Route\Dispatch\Dispatch; use HttpServer\Route\Dispatch\Dispatch;
use Snowflake\Snowflake;
/** /**
* Class Middleware * Class Middleware
@@ -51,12 +54,41 @@ class Middleware
*/ */
public function getGenerate(Node $node): mixed public function getGenerate(Node $node): mixed
{ {
if (is_array($node->handler) && is_object($node->handler[0])) {
$this->set_attributes($node);
}
return $node->callback = Reduce::reduce(function () use ($node) { return $node->callback = Reduce::reduce(function () use ($node) {
return Dispatch::create($node->handler, func_get_args())->dispatch(); return Dispatch::create($node->handler, func_get_args())->dispatch();
}, $this->annotation($node)); }, $this->annotation($node));
} }
/**
* @param $node
* @throws Exception
*/
private function set_attributes(Node $node)
{
[$controller, $action] = $node->handler;
$attributes = Snowflake::app()->getAttributes();
$annotation = $attributes->getByClass(get_class($controller), $action);
foreach ($annotation as $item) {
if ($item instanceof Interceptor) {
$node->addInterceptor($item->interceptor);
}
if ($item instanceof After) {
$node->addAfter($item->after);
}
if ($item instanceof RMiddleware) {
$node->addMiddleware($item->middleware);
}
if ($item instanceof Limits) {
$node->addLimits($item->limits);
}
}
}
/** /**
* @param Node $node * @param Node $node
* @return array * @return array