This commit is contained in:
as2252258@163.com
2021-07-27 03:30:55 +08:00
parent 22fa0d292c
commit 4e3b41331d
13 changed files with 888 additions and 1199 deletions
-52
View File
@@ -1,52 +0,0 @@
<?php
namespace Annotation\Route;
use Annotation\Attribute;
use JetBrains\PhpStorm\Pure;
use Snowflake\Snowflake;
/**
* Class Interceptor
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Interceptor extends Attribute
{
/**
* Interceptor constructor.
* @param string|array $interceptor
* @throws
*/
public function __construct(public string|array $interceptor)
{
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'];
}
}
/**
* @param mixed $class
* @param mixed|null $method
* @return Interceptor
*/
public function execute(mixed $class, mixed $method = null): static
{
return $this;
}
}
-52
View File
@@ -1,52 +0,0 @@
<?php
namespace Annotation\Route;
use Annotation\Attribute;
use Snowflake\Snowflake;
/**
* Class Limits
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Limits extends Attribute
{
/**
* Limits constructor.
* @param string|array $limits
* @throws
*/
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'];
}
}
/**
* @param mixed $class
* @param mixed|null $method
* @return Limits
*/
public function execute(mixed $class, mixed $method = null): static
{
return $this;
}
}
+31 -28
View File
@@ -5,6 +5,7 @@ namespace Annotation\Route;
use Annotation\Attribute;
use HttpServer\Route\Middlewares;
use Snowflake\Snowflake;
use HttpServer\IInterface\Middleware as IMiddleware;
@@ -16,38 +17,40 @@ use HttpServer\IInterface\Middleware as IMiddleware;
{
/**
* Interceptor constructor.
* @param string|array $middleware
* @throws
*/
public function __construct(public string|array $middleware)
{
if (is_string($this->middleware)) {
$this->middleware = [$this->middleware];
}
/**
* Interceptor constructor.
* @param string|array $middleware
* @throws
*/
public function __construct(public string|array $middleware)
{
if (is_string($this->middleware)) {
$this->middleware = [$this->middleware];
}
$array = [];
foreach ($this->middleware as $key => $value) {
$sn = Snowflake::createObject($value);
if (!($sn instanceof IMiddleware)) {
continue;
}
$array[] = [$sn, 'onHandler'];
}
$this->middleware = $array;
}
$array = [];
foreach ($this->middleware as $value) {
$sn = Snowflake::createObject($value);
if (!($sn instanceof IMiddleware)) {
continue;
}
$array[] = [$sn, 'onHandler'];
}
$this->middleware = $array;
}
/**
* @param mixed $class
* @param mixed|null $method
* @return Middleware
*/
/**
* @param mixed $class
* @param mixed|null $method
* @return Middleware
*/
public function execute(mixed $class, mixed $method = null): static
{
return $this;
}
{
$middleware = Snowflake::getDi()->get(Middlewares::class);
$middleware->addMiddlewares($class, $method, $this->middleware);
return $this;
}
}
+34 -27
View File
@@ -13,36 +13,43 @@ use Snowflake\Snowflake;
#[\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 $version = 'v.1.0'
)
{
}
/**
* Route constructor.
* @param string $uri
* @param string $method
* @param string $version
*/
public function __construct(
public string $uri,
public string $method,
public string $version = 'v.1.0'
)
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return Router
* @throws Exception
*/
/**
* @param mixed $class
* @param mixed|null $method
* @return Router
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): Router
{
// TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter();
$router->addRoute($this->uri, [$class, $method], $this->method);
return $router;
}
{
// TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter();
$node = $router->addRoute($this->uri, [$class, $method], $this->method);
if ($node !== null) {
$attribute = Snowflake::getDi()->getMethodAttribute($class::class, $method);
foreach ($attribute as $item) {
if ($item instanceof Route) {
continue;
}
$item->execute($class, $method);
}
}
return $router;
}
}