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

55 lines
1.1 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 05:53:47 +08:00
public function __construct(public string|array $middleware)
2021-07-27 03:30:55 +08:00
{
2021-08-29 05:53:47 +08:00
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'];
}
2021-08-29 06:04:22 +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-08-29 05:53:47 +08:00
public function execute(mixed $class, mixed $method = null): mixed
2021-08-29 04:06:49 +08:00
{
2021-08-29 05:53:47 +08:00
MiddlewareManager::addMiddlewares($class, $method, $this->middleware);
return parent::execute($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
}