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

61 lines
1.3 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-07-27 04:16:42 +08:00
use HttpServer\Route\MiddlewareManager;
2021-08-06 10:50:07 +08:00
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
2020-12-16 15:20:51 +08:00
use Snowflake\Snowflake;
2021-03-05 16:20:49 +08:00
use HttpServer\IInterface\Middleware as IMiddleware;
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
*/
public function __construct(public string|array $middleware)
{
if (is_string($this->middleware)) {
$this->middleware = [$this->middleware];
}
$array = [];
foreach ($this->middleware as $value) {
2021-08-06 10:50:07 +08:00
$sn = di($value);
2021-07-27 03:30:55 +08:00
if (!($sn instanceof IMiddleware)) {
continue;
}
$array[] = [$sn, 'onHandler'];
}
$this->middleware = $array;
}
2021-08-06 10:50:07 +08:00
/**
* @param mixed $class
* @param mixed|null $method
* @return $this
* @throws ReflectionException
* @throws NotFindClassException
*/
2021-05-03 03:48:52 +08:00
public function execute(mixed $class, mixed $method = null): static
2021-07-27 03:30:55 +08:00
{
2021-07-27 04:16:42 +08:00
$middleware = Snowflake::getDi()->get(MiddlewareManager::class);
2021-07-27 03:30:55 +08:00
$middleware->addMiddlewares($class, $method, $this->middleware);
return $this;
}
2020-12-16 10:38:03 +08:00
2021-02-22 17:44:24 +08:00
2020-12-16 10:38:03 +08:00
}