This commit is contained in:
2021-08-24 18:24:46 +08:00
parent 575f4b8520
commit 24c93edf2b
110 changed files with 5 additions and 6 deletions
+58
View File
@@ -0,0 +1,58 @@
<?php
namespace Annotation\Route;
use Annotation\Attribute;
use Http\Route\MiddlewareManager;
use ReflectionException;
use Kiri\Kiri;
use Http\IInterface\MiddlewareInterface ;
/**
* Class Middleware
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Middleware extends Attribute
{
/**
* 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) {
$sn = di($value);
if (!($sn instanceof MiddlewareInterface)) {
continue;
}
$array[] = [$sn, 'onHandler'];
}
$this->middleware = $array;
}
/**
* @param mixed $class
* @param mixed|null $method
* @return $this
* @throws ReflectionException
*/
public function execute(mixed $class, mixed $method = null): static
{
$middleware = Kiri::getDi()->get(MiddlewareManager::class);
$middleware->addMiddlewares($class, $method, $this->middleware);
return $this;
}
}