This commit is contained in:
2021-09-16 14:40:25 +08:00
parent 9bdb01799b
commit 7e7239be87
2 changed files with 47 additions and 49 deletions
+27 -28
View File
@@ -5,38 +5,37 @@ namespace Annotation\Route;
use Annotation\Attribute; use Annotation\Attribute;
use Http\Route\MiddlewareManager;
use ReflectionException;
use Http\IInterface\MiddlewareInterface; use Http\IInterface\MiddlewareInterface;
use Http\Route\MiddlewareManager;
/** /**
* Class Middleware * Class Middleware
* @package Annotation\Route * @package Annotation\Route
*/ */
#[\Attribute(\Attribute::TARGET_METHOD)] class Middleware extends Attribute #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] class Middleware extends Attribute
{ {
/** /**
* Interceptor constructor. * Interceptor constructor.
* @param string|array $middleware * @param string|array $middleware
* @throws * @throws
*/ */
public function __construct(public string|array $middleware) public function __construct(public string|array $middleware)
{ {
if (is_string($this->middleware)) { if (is_string($this->middleware)) {
$this->middleware = [$this->middleware]; $this->middleware = [$this->middleware];
} }
$array = []; $array = [];
foreach ($this->middleware as $value) { foreach ($this->middleware as $value) {
$sn = di($value); $sn = di($value);
if (!($sn instanceof MiddlewareInterface)) { if (!($sn instanceof MiddlewareInterface)) {
continue; continue;
} }
$array[] = [$sn, 'onHandler']; $array[] = [$sn, 'onHandler'];
} }
$this->middleware = $array; $this->middleware = $array;
} }
/** /**
@@ -44,11 +43,11 @@ use Http\IInterface\MiddlewareInterface;
* @param mixed|null $method * @param mixed|null $method
* @return $this * @return $this
*/ */
public function execute(mixed $class, mixed $method = null): mixed public function execute(mixed $class, mixed $method = null): mixed
{ {
MiddlewareManager::add($class, $method, $this->middleware); MiddlewareManager::add($class, $method, $this->middleware);
return parent::execute($class, $method); return parent::execute($class, $method);
} }
} }
+20 -21
View File
@@ -5,22 +5,21 @@ namespace Annotation\Route;
use Annotation\Attribute; use Annotation\Attribute;
use Exception;
use Http\Route\Router; use Http\Route\Router;
use Kiri\Kiri; use Kiri\Kiri;
#[\Attribute(\Attribute::TARGET_METHOD)] class Route extends Attribute #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] class Route extends Attribute
{ {
/** /**
* Route constructor. * Route constructor.
* @param string $uri * @param string $uri
* @param string $method * @param string $method
* @param string $version * @param string $version
*/ */
public function __construct(public string $uri,public string $method,public string $version = 'v.1.0') public function __construct(public string $uri, public string $method, public string $version = 'v.1.0')
{ {
} }
/** /**
@@ -28,16 +27,16 @@ use Kiri\Kiri;
* @param mixed|null $method * @param mixed|null $method
* @return Router * @return Router
*/ */
public function execute(mixed $class, mixed $method = null): Router public function execute(mixed $class, mixed $method = null): Router
{ {
// TODO: Implement setHandler() method. // TODO: Implement setHandler() method.
$router = Kiri::getDi()->get(Router::class); $router = Kiri::getDi()->get(Router::class);
if (is_string($class)) { if (is_string($class)) {
$class = di($class); $class = di($class);
} }
$router->addRoute($this->uri, [$class, $method], strtoupper($this->method)); $router->addRoute($this->uri, [$class, $method], strtoupper($this->method));
return $router; return $router;
} }
} }