From 7e7239be875547570be4c3f84f840b7fc08e27be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 16 Sep 2021 14:40:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- note/Route/Middleware.php | 55 +++++++++++++++++++-------------------- note/Route/Route.php | 41 ++++++++++++++--------------- 2 files changed, 47 insertions(+), 49 deletions(-) diff --git a/note/Route/Middleware.php b/note/Route/Middleware.php index 3f563564..0dfa6c09 100644 --- a/note/Route/Middleware.php +++ b/note/Route/Middleware.php @@ -5,38 +5,37 @@ namespace Annotation\Route; use Annotation\Attribute; -use Http\Route\MiddlewareManager; -use ReflectionException; use Http\IInterface\MiddlewareInterface; +use Http\Route\MiddlewareManager; /** * Class Middleware * @package Annotation\Route */ -#[\Attribute(\Attribute::TARGET_METHOD)] class Middleware extends Attribute +#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] class Middleware extends Attribute { - /** - * 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 $value) { - $sn = di($value); - if (!($sn instanceof MiddlewareInterface)) { - continue; - } - $array[] = [$sn, 'onHandler']; - } - $this->middleware = $array; - } + /** + * 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 $value) { + $sn = di($value); + if (!($sn instanceof MiddlewareInterface)) { + continue; + } + $array[] = [$sn, 'onHandler']; + } + $this->middleware = $array; + } /** @@ -44,11 +43,11 @@ use Http\IInterface\MiddlewareInterface; * @param mixed|null $method * @return $this */ - public function execute(mixed $class, mixed $method = null): mixed - { - MiddlewareManager::add($class, $method, $this->middleware); - return parent::execute($class, $method); - } + public function execute(mixed $class, mixed $method = null): mixed + { + MiddlewareManager::add($class, $method, $this->middleware); + return parent::execute($class, $method); + } } diff --git a/note/Route/Route.php b/note/Route/Route.php index 2a349f15..bca9e8a5 100644 --- a/note/Route/Route.php +++ b/note/Route/Route.php @@ -5,22 +5,21 @@ namespace Annotation\Route; use Annotation\Attribute; -use Exception; use Http\Route\Router; use Kiri\Kiri; -#[\Attribute(\Attribute::TARGET_METHOD)] class Route extends Attribute +#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] 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') + { + } /** @@ -28,16 +27,16 @@ use Kiri\Kiri; * @param mixed|null $method * @return Router */ - public function execute(mixed $class, mixed $method = null): Router - { - // TODO: Implement setHandler() method. - $router = Kiri::getDi()->get(Router::class); - if (is_string($class)) { - $class = di($class); - } - $router->addRoute($this->uri, [$class, $method], strtoupper($this->method)); - return $router; - } + public function execute(mixed $class, mixed $method = null): Router + { + // TODO: Implement setHandler() method. + $router = Kiri::getDi()->get(Router::class); + if (is_string($class)) { + $class = di($class); + } + $router->addRoute($this->uri, [$class, $method], strtoupper($this->method)); + return $router; + } }