This commit is contained in:
2021-02-23 14:45:50 +08:00
parent e789002c9e
commit 27f6c88cbf
6 changed files with 24 additions and 35 deletions
+4
View File
@@ -16,6 +16,7 @@ use Snowflake\Snowflake;
#[\Attribute(\Attribute::TARGET_METHOD)] class After implements IAnnotation
{
use Node;
/**
* Interceptor constructor.
@@ -24,6 +25,9 @@ use Snowflake\Snowflake;
*/
public function __construct(public string|array $after)
{
if (is_string($this->after)) {
$this->after = [];
}
}
+6 -1
View File
@@ -5,6 +5,7 @@ namespace Annotation\Route;
use Annotation\IAnnotation;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
@@ -16,14 +17,18 @@ use Snowflake\Snowflake;
#[\Attribute(\Attribute::TARGET_METHOD)] class Interceptor implements IAnnotation
{
use Node;
/**
* Interceptor constructor.
* @param string|array $interceptor
* @throws
*/
public function __construct(public string|array $interceptor)
#[Pure] public function __construct(public string|array $interceptor)
{
if (is_string($this->interceptor)) {
$this->interceptor = [];
}
}
+4
View File
@@ -16,6 +16,7 @@ use Snowflake\Snowflake;
#[\Attribute(\Attribute::TARGET_METHOD)] class Limits implements IAnnotation
{
use Node;
/**
* Limits constructor.
@@ -24,6 +25,9 @@ use Snowflake\Snowflake;
*/
public function __construct(public string|array $limits)
{
if (is_string($this->limits)) {
$this->limits = [];
}
}
+5
View File
@@ -17,6 +17,8 @@ use Snowflake\Snowflake;
#[\Attribute(\Attribute::TARGET_METHOD)] class Middleware implements IAnnotation
{
use Node;
/**
* Interceptor constructor.
@@ -25,6 +27,9 @@ use Snowflake\Snowflake;
*/
public function __construct(public string|array $middleware)
{
if (is_string($this->middleware)) {
$this->middleware = [];
}
}
+1 -25
View File
@@ -7,7 +7,6 @@ use HttpServer\IInterface\After;
use HttpServer\IInterface\Interceptor;
use HttpServer\IInterface\Limits;
use HttpServer\IInterface\Middleware;
use HttpServer\Route\Node as RNode;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
@@ -16,36 +15,13 @@ trait Node
{
/**
* @param RNode $node
* @return RNode
* @throws
*/
public function add(RNode $node): RNode
{
if (!empty($this->middleware)) {
$node->addMiddleware($this->reflectClass($this->middleware));
}
if (!empty($this->interceptor)) {
$node->addInterceptor($this->reflectClass($this->interceptor));
}
if (!empty($this->limits)) {
$node->addLimits($this->reflectClass($this->limits));
}
if (!empty($this->after)) {
$node->addAfter($this->reflectClass($this->after));
}
return $node;
}
/**
* @param array $classes
* @return array
* @throws ReflectionException
* @throws NotFindClassException
*/
private function reflectClass(array $classes): array
public function reflectClass(array $classes): array
{
$di = Snowflake::getDi();
foreach ($classes as $key => $class) {
+4 -9
View File
@@ -77,22 +77,17 @@ class Middleware
return;
}
foreach ($annotation as $attribute) {
var_dump($attribute);
if ($attribute instanceof Interceptor) {
var_dump($attribute);
$node->addInterceptor($attribute->interceptor);
$node->addInterceptor($attribute->reflectClass($attribute->interceptor));
}
if ($attribute instanceof After) {
var_dump($attribute);
$node->addAfter($attribute->after);
$node->addInterceptor($attribute->reflectClass($attribute->after));
}
if ($attribute instanceof RMiddleware) {
var_dump($attribute);
$node->addMiddleware($attribute->middleware);
$node->addInterceptor($attribute->reflectClass($attribute->middleware));
}
if ($attribute instanceof Limits) {
var_dump($attribute);
$node->addLimits($attribute->limits);
$node->addInterceptor($attribute->reflectClass($attribute->limits));
}
}
}