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

54 lines
1.1 KiB
PHP
Raw Normal View History

2020-12-16 10:38:03 +08:00
<?php
2021-11-30 15:10:01 +08:00
namespace Note\Route;
2020-12-16 10:38:03 +08:00
2021-11-30 15:10:01 +08:00
use Note\Attribute;
2021-09-18 16:54:39 +08:00
use Http\Handler\Abstracts\MiddlewareManager;
use Psr\Http\Server\MiddlewareInterface;
2020-12-16 15:20:51 +08:00
2020-12-16 10:38:03 +08:00
/**
* Class Middleware
2021-11-30 15:10:01 +08:00
* @package Note\Route
2020-12-16 10:38:03 +08:00
*/
2021-09-16 14:40:25 +08:00
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] class Middleware extends Attribute
2020-12-16 10:38:03 +08:00
{
2021-09-16 14:40:25 +08:00
/**
* 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) {
2021-09-18 16:54:39 +08:00
if (!in_array(MiddlewareInterface::class, class_implements($value))) {
throw new \Exception('The middleware');
2021-09-16 14:40:25 +08:00
}
2021-09-24 18:06:49 +08:00
$array[] = $value;
2021-09-16 14:40:25 +08:00
}
$this->middleware = $array;
}
2021-08-29 04:06:49 +08:00
2021-07-27 03:30:55 +08:00
2021-08-30 18:53:26 +08:00
/**
* @param mixed $class
* @param mixed|null $method
* @return $this
2021-11-29 14:35:09 +08:00
* @throws \ReflectionException
2021-08-30 18:53:26 +08:00
*/
2021-09-16 14:40:25 +08:00
public function execute(mixed $class, mixed $method = null): mixed
{
MiddlewareManager::add($class, $method, $this->middleware);
return parent::execute($class, $method);
}
2020-12-16 10:38:03 +08:00
2021-02-22 17:44:24 +08:00
2020-12-16 10:38:03 +08:00
}