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

53 lines
1.0 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-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
* @package Annotation\Route
*/
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-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
}