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

53 lines
892 B
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;
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
{
/**
* Interceptor constructor.
* @param string|array $middleware
2020-12-16 15:20:51 +08:00
* @throws
2020-12-16 10:38:03 +08:00
*/
2021-03-03 18:35:04 +08:00
public function __construct(public string|array $middleware)
2020-12-16 10:38:03 +08:00
{
2021-02-23 14:45:50 +08:00
if (is_string($this->middleware)) {
2021-02-23 14:54:47 +08:00
$this->middleware = [$this->middleware];
2021-02-23 14:45:50 +08:00
}
2021-03-04 00:20:21 +08:00
$array = [];
2021-03-03 18:35:04 +08:00
foreach ($this->middleware as $key => $value) {
$sn = Snowflake::createObject($value);
2021-03-05 16:20:49 +08:00
if (!($sn instanceof IMiddleware)) {
2021-03-03 18:35:04 +08:00
continue;
}
2021-03-04 00:20:21 +08:00
$array[] = [$sn, 'onHandler'];
2021-03-03 18:35:04 +08:00
}
2021-03-04 00:20:21 +08:00
$this->middleware = $array;
2021-02-22 17:44:24 +08:00
}
/**
* @param array $handler
2021-02-23 14:18:49 +08:00
* @return Middleware
2021-02-22 17:44:24 +08:00
*/
2021-02-23 14:18:49 +08:00
public function execute(array $handler): static
2021-02-22 17:44:24 +08:00
{
2021-02-23 14:16:08 +08:00
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
}