This commit is contained in:
2021-03-03 18:35:04 +08:00
parent 1c6cde4133
commit c337280d4e
25 changed files with 533 additions and 499 deletions
+10 -8
View File
@@ -4,31 +4,33 @@
namespace Annotation\Route;
use Annotation\IAnnotation;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
use Annotation\Attribute;
use Snowflake\Snowflake;
/**
* Class Interceptor
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class After implements IAnnotation
#[\Attribute(\Attribute::TARGET_METHOD)] class After extends Attribute
{
use Node;
/**
* Interceptor constructor.
* @param \HttpServer\IInterface\After|\HttpServer\IInterface\After[] $after
* @throws
*/
#[Pure] 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 => $value) {
$sn = Snowflake::createObject($value);
if (!($sn instanceof \HttpServer\IInterface\After)) {
continue;
}
$this->after[$key] = [$sn, 'onHandler'];
}
}
+2 -2
View File
@@ -4,13 +4,13 @@
namespace Annotation\Route;
use Annotation\IAnnotation;
use Annotation\Attribute;
/**
* Class Document
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Document implements IAnnotation
#[\Attribute(\Attribute::TARGET_METHOD)] class Document extends Attribute
{
const INTEGER = 'int';
+14 -1
View File
@@ -4,11 +4,13 @@
namespace Annotation\Route;
use Annotation\Attribute;
/**
* Class Filter
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Filter
#[\Attribute(\Attribute::TARGET_METHOD)] class Filter extends Attribute
{
/**
@@ -20,4 +22,15 @@ namespace Annotation\Route;
}
/**
* @param array $handler
* @return array
*/
public function execute(array $handler): array
{
// TODO: Implement execute() method.
return $handler;
}
}
+12 -5
View File
@@ -4,20 +4,17 @@
namespace Annotation\Route;
use Annotation\IAnnotation;
use Annotation\Attribute;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
* Class Interceptor
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Interceptor implements IAnnotation
#[\Attribute(\Attribute::TARGET_METHOD)] class Interceptor extends Attribute
{
use Node;
/**
* Interceptor constructor.
@@ -29,6 +26,16 @@ use Snowflake\Snowflake;
if (is_string($this->interceptor)) {
$this->interceptor = [$this->interceptor];
}
foreach ($this->interceptor as $key => $value) {
$sn = Snowflake::createObject($value);
if (!($sn instanceof \HttpServer\IInterface\Interceptor)) {
continue;
}
$this->interceptor[$key] = [$sn, 'Interceptor'];
}
}
+13 -7
View File
@@ -4,31 +4,37 @@
namespace Annotation\Route;
use Annotation\IAnnotation;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
use Annotation\Attribute;
use Snowflake\Snowflake;
/**
* Class Limits
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Limits implements IAnnotation
#[\Attribute(\Attribute::TARGET_METHOD)] class Limits extends Attribute
{
use Node;
/**
* Limits constructor.
* @param string|array $limits
* @throws
*/
#[Pure] 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 => $value) {
$sn = Snowflake::createObject($value);
if (!($sn instanceof \HttpServer\IInterface\Limits)) {
continue;
}
$this->limits[$key] = [$sn, 'next'];
}
}
+11 -8
View File
@@ -4,32 +4,35 @@
namespace Annotation\Route;
use Annotation\IAnnotation;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
use Annotation\Attribute;
use Snowflake\Snowflake;
/**
* Class Middleware
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Middleware implements IAnnotation
#[\Attribute(\Attribute::TARGET_METHOD)] class Middleware extends Attribute
{
use Node;
/**
* Interceptor constructor.
* @param string|array $middleware
* @throws
*/
#[Pure] 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 => $value) {
$sn = Snowflake::createObject($value);
if (!($sn instanceof \HttpServer\IInterface\Middleware)) {
continue;
}
$this->middleware[$key] = [$sn, 'onHandler'];
}
}
-45
View File
@@ -1,45 +0,0 @@
<?php
namespace Annotation\Route;
use HttpServer\IInterface\After;
use HttpServer\IInterface\Interceptor;
use HttpServer\IInterface\Limits;
use HttpServer\IInterface\Middleware;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
trait Node
{
/**
* @param array $classes
* @return array
* @throws ReflectionException
* @throws NotFindClassException
*/
public function reflectClass(array $classes): array
{
$array = [];
foreach ($classes as $class) {
$object = Snowflake::getDi()->get($class);
if ($object instanceof Interceptor) {
$array[] = [$object, 'Interceptor'];
}
if ($object instanceof Limits) {
$array[] = [$object, 'next'];
}
if ($object instanceof After) {
$array[] = [$object, 'onHandler'];
}
if ($object instanceof Middleware) {
$array[] = [$object, 'onHandler'];
}
}
return $array;
}
}
+10 -7
View File
@@ -4,26 +4,27 @@
namespace Annotation\Route;
use Closure;
use Exception;
use HttpServer\Route\Node;
use Annotation\Attribute;
use HttpServer\Route\Router;
use ReflectionException;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use Annotation\IAnnotation;
#[\Attribute(\Attribute::TARGET_METHOD)] class Route implements IAnnotation
#[\Attribute(\Attribute::TARGET_METHOD)] class Route extends Attribute
{
/**
* Route constructor.
* @param string $uri
* @param string $method
* @param string $version
*/
public function __construct(
public string $uri,
public string $method
public string $method,
public string $version = 'v.1.0'
)
{
}
@@ -34,13 +35,15 @@ use Annotation\IAnnotation;
* @return Router
* @throws ComponentException
* @throws ConfigException
* @throws ReflectionException
* @throws NotFindClassException
*/
public function execute(array $handler): Router
{
// TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter();
$router->addRoute($this->uri, $handler, $this->method);
$router->addRoute($this->uri . $this->version, $handler, $this->method);
return $router;
}
+8 -7
View File
@@ -4,10 +4,7 @@
namespace Annotation\Route;
use Annotation\IAnnotation;
use Closure;
use Exception;
use HttpServer\Route\Node;
use Annotation\Attribute;
use HttpServer\Route\Router;
use ReflectionException;
use Snowflake\Exception\ComponentException;
@@ -19,7 +16,7 @@ use Snowflake\Snowflake;
* Class Socket
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Socket implements IAnnotation
#[\Attribute(\Attribute::TARGET_METHOD)] class Socket extends Attribute
{
const CLOSE = 'CLOSE';
@@ -30,10 +27,12 @@ use Snowflake\Snowflake;
* Socket constructor.
* @param string $event
* @param string|null $uri
* @param string $version
*/
public function __construct(
public string $event,
public ?string $uri = null
public ?string $uri = null,
public string $version = 'v.1.0'
)
{
}
@@ -44,6 +43,8 @@ use Snowflake\Snowflake;
* @return Router
* @throws ComponentException
* @throws ConfigException
* @throws ReflectionException
* @throws NotFindClassException
*/
public function execute(array $handler): Router
{
@@ -52,7 +53,7 @@ use Snowflake\Snowflake;
$method = $this->event . '::' . (is_null($this->uri) ? 'event' : $this->uri);
$router->addRoute($method, $handler, 'sw::socket');
$router->addRoute($method . $this->version, $handler, 'sw::socket');
return $router;
}