Files
kiri-core/note/Route/Middleware.php
T

57 lines
1.2 KiB
PHP
Raw Normal View History

2020-12-16 10:38:03 +08:00
<?php
namespace Annotation\Route;
2021-03-03 18:35:04 +08:00
use Annotation\Attribute;
2021-08-17 16:43:50 +08:00
use Http\Route\MiddlewareManager;
2021-08-06 10:50:07 +08:00
use ReflectionException;
2021-08-29 04:06:49 +08:00
use Http\IInterface\MiddlewareInterface;
2020-12-16 15:20:51 +08:00
2020-12-16 10:38:03 +08:00
/**
* Class Middleware
* @package Annotation\Route
*/
2021-03-03 18:35:04 +08:00
#[\Attribute(\Attribute::TARGET_METHOD)] class Middleware extends Attribute
2020-12-16 10:38:03 +08:00
{
2021-07-27 03:30:55 +08:00
/**
* Interceptor constructor.
* @param string|array $middleware
* @throws
*/
2021-08-29 04:06:49 +08:00
public function __construct(string|array $middleware)
2021-07-27 03:30:55 +08:00
{
2021-08-29 04:06:49 +08:00
}
2021-07-27 03:30:55 +08:00
2021-08-29 04:06:49 +08:00
/**
2021-08-29 04:31:14 +08:00
* @param static $params
2021-08-29 04:06:49 +08:00
* @param mixed $class
* @param mixed|null $method
* @return $this
* @throws ReflectionException
*/
public static function execute(mixed $params, mixed $class, mixed $method = null): mixed
{
if (is_string($params->middleware)) {
$params->middleware = [$params->middleware];
}
2021-07-27 03:30:55 +08:00
$array = [];
2021-08-29 04:06:49 +08:00
foreach ($params->middleware as $value) {
2021-08-06 10:50:07 +08:00
$sn = di($value);
2021-08-19 11:00:04 +08:00
if (!($sn instanceof MiddlewareInterface)) {
2021-07-27 03:30:55 +08:00
continue;
}
$array[] = [$sn, 'onHandler'];
}
2021-08-29 04:06:49 +08:00
MiddlewareManager::addMiddlewares($class, $method, $array);
2021-07-27 03:30:55 +08:00
2021-08-29 04:06:49 +08:00
return parent::execute($params, $class, $method);
2021-07-27 03:30:55 +08:00
}
2020-12-16 10:38:03 +08:00
2021-02-22 17:44:24 +08:00
2020-12-16 10:38:03 +08:00
}