This commit is contained in:
2023-07-25 10:09:06 +08:00
parent e832a2721c
commit 00cd571ffe
7 changed files with 24 additions and 50 deletions
-26
View File
@@ -3,35 +3,9 @@ declare(strict_types=1);
namespace Kiri\Router\Annotate; namespace Kiri\Router\Annotate;
use Kiri\Router\Base\Middleware as MiddlewareManager;
use ReflectionException;
abstract class AbstractRequestMethod abstract class AbstractRequestMethod
{ {
/**
* @param object $class
* @param string $method
* @return void
* @throws ReflectionException
* @throws \Exception
*/
public function registerMiddleware(object $class, string $method): void
{
$reflectionMethod = \Kiri::getDi()->getMethod($class::class, $method);
$middleware = $reflectionMethod->getAttributes(Middleware::class);
$middlewareManager = \Kiri::getDi()->get(MiddlewareManager::class);
foreach ($middleware as $value) {
if (!class_exists($value->getName())) {
continue;
}
/** @var Middleware $instance */
$instance = $value->newInstance();
$middlewareManager->set($class::class, $method, $instance->middleware);
}
}
} }
+3 -1
View File
@@ -4,12 +4,14 @@ declare(strict_types=1);
namespace Kiri\Router\Annotate; namespace Kiri\Router\Annotate;
use ReflectionException;
#[\Attribute(\Attribute::TARGET_CLASS)] #[\Attribute(\Attribute::TARGET_CLASS)]
class AutoController class AutoController
{ {
/** /**
* @throws \ReflectionException * @throws ReflectionException
*/ */
public function dispatch(object $object): void public function dispatch(object $object): void
{ {
+4 -4
View File
@@ -14,10 +14,10 @@ class Delete extends AbstractRequestMethod implements InjectRouteInterface
{ {
/**
/** * @param string $path
* @param string $path * @param string $version
*/ */
public function __construct(readonly public string $path, readonly public string $version = 'v1') public function __construct(readonly public string $path, readonly public string $version = 'v1')
{ {
} }
+4 -5
View File
@@ -14,11 +14,10 @@ class Head extends AbstractRequestMethod implements InjectRouteInterface
{ {
/**
* @param string $path
/** * @param string $version
* @param string $path */
*/
public function __construct(readonly public string $path, readonly public string $version = 'v1') public function __construct(readonly public string $path, readonly public string $version = 'v1')
{ {
} }
+4 -5
View File
@@ -14,11 +14,10 @@ class Options extends AbstractRequestMethod implements InjectRouteInterface
{ {
/**
* @param string $path
/** * @param string $version
* @param string $path */
*/
public function __construct(readonly public string $path, readonly public string $version = 'v1') public function __construct(readonly public string $path, readonly public string $version = 'v1')
{ {
} }
+4 -5
View File
@@ -14,11 +14,10 @@ class Put extends AbstractRequestMethod implements InjectRouteInterface
{ {
/**
* @param string $path
/** * @param string $version
* @param string $path */
*/
public function __construct(readonly public string $path, readonly public string $version = 'v1') public function __construct(readonly public string $path, readonly public string $version = 'v1')
{ {
} }
+5 -4
View File
@@ -12,10 +12,11 @@ class Route extends AbstractRequestMethod implements InjectRouteInterface
{ {
/** /**
* @param string $path * @param string $path
* @param RequestMethod $method * @param RequestMethod $method
*/ * @param string $version
*/
public function __construct(readonly public string $path, readonly public RequestMethod $method, readonly public string $version = 'v1') public function __construct(readonly public string $path, readonly public RequestMethod $method, readonly public string $version = 'v1')
{ {
} }