2020-12-16 10:38:03 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Annotation\Route;
|
|
|
|
|
|
|
|
|
|
|
2021-02-22 17:44:24 +08:00
|
|
|
use Annotation\IAnnotation;
|
|
|
|
|
use JetBrains\PhpStorm\Pure;
|
|
|
|
|
use ReflectionException;
|
|
|
|
|
use Snowflake\Exception\NotFindClassException;
|
2020-12-16 15:20:51 +08:00
|
|
|
use Snowflake\Snowflake;
|
|
|
|
|
|
2020-12-16 10:38:03 +08:00
|
|
|
/**
|
|
|
|
|
* Class Middleware
|
|
|
|
|
* @package Annotation\Route
|
|
|
|
|
*/
|
2021-02-22 17:44:24 +08:00
|
|
|
#[\Attribute(\Attribute::TARGET_METHOD)] class Middleware implements IAnnotation
|
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
|
|
|
*/
|
|
|
|
|
public function __construct(public string|array $middleware)
|
|
|
|
|
{
|
2021-02-22 17:44:24 +08:00
|
|
|
if (!is_string($this->middleware)) {
|
|
|
|
|
return;
|
2020-12-16 15:20:51 +08:00
|
|
|
}
|
2021-02-22 17:44:24 +08:00
|
|
|
$this->middleware = [$this->middleware];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $handler
|
|
|
|
|
* @return array|string
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws NotFindClassException
|
|
|
|
|
*/
|
|
|
|
|
public function execute(array $handler): array|string
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement execute() method.
|
2020-12-16 15:20:51 +08:00
|
|
|
foreach ($this->middleware as $key => $item) {
|
2020-12-16 15:37:23 +08:00
|
|
|
$this->middleware[$key] = [Snowflake::createObject($item), 'onHandler'];
|
2020-12-16 15:20:51 +08:00
|
|
|
}
|
2021-02-22 17:44:24 +08:00
|
|
|
return $this->middleware;
|
2020-12-16 10:38:03 +08:00
|
|
|
}
|
|
|
|
|
|
2021-02-22 17:44:24 +08:00
|
|
|
|
2020-12-16 10:38:03 +08:00
|
|
|
}
|