diff --git a/Annotation/Route/After.php b/Annotation/Route/After.php index 2587ed5e..7696b0a0 100644 --- a/Annotation/Route/After.php +++ b/Annotation/Route/After.php @@ -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 = []; + } } diff --git a/Annotation/Route/Interceptor.php b/Annotation/Route/Interceptor.php index 00b413b0..80879f95 100644 --- a/Annotation/Route/Interceptor.php +++ b/Annotation/Route/Interceptor.php @@ -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 = []; + } } diff --git a/Annotation/Route/Limits.php b/Annotation/Route/Limits.php index bc893dbf..fc306df3 100644 --- a/Annotation/Route/Limits.php +++ b/Annotation/Route/Limits.php @@ -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 = []; + } } diff --git a/Annotation/Route/Middleware.php b/Annotation/Route/Middleware.php index 9e92c289..0c7da535 100644 --- a/Annotation/Route/Middleware.php +++ b/Annotation/Route/Middleware.php @@ -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 = []; + } } diff --git a/Annotation/Route/Node.php b/Annotation/Route/Node.php index f035a451..f1760b75 100644 --- a/Annotation/Route/Node.php +++ b/Annotation/Route/Node.php @@ -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) { diff --git a/HttpServer/Route/Middleware.php b/HttpServer/Route/Middleware.php index f3a01c39..57c7a6f2 100644 --- a/HttpServer/Route/Middleware.php +++ b/HttpServer/Route/Middleware.php @@ -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)); } } }