eee
This commit is contained in:
+28
-14
@@ -8,6 +8,7 @@ namespace Kiri\Router;
|
||||
use Closure;
|
||||
use Kiri\Router\Base\NotFoundController;
|
||||
use Kiri\Router\Constrict\RequestMethod;
|
||||
use ReflectionException;
|
||||
use Throwable;
|
||||
use Traversable;
|
||||
use Kiri\Router\Base\Middleware;
|
||||
@@ -204,47 +205,60 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
|
||||
public function register(string $path, string $method, Handler $handler): void
|
||||
{
|
||||
$this->methods[$method . '_' . $path] = $handler;
|
||||
$this->registerMiddleware($handler->getClass(), $handler->getMethod());
|
||||
$middlewares = $this->registerMiddleware($handler->getClass(), $handler->getMethod());
|
||||
if (count($middlewares) > 0) {
|
||||
$handler->setMiddlewares($middlewares);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $method
|
||||
* @return void
|
||||
* @throws
|
||||
* @return array
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function registerMiddleware(string $class, string $method): void
|
||||
public function registerMiddleware(string $class, string $method): array
|
||||
{
|
||||
$response = [];
|
||||
$middlewares = \request()->middlewares;
|
||||
var_dump($class . '::' . $method . '('.json_encode($middlewares,JSON_UNESCAPED_UNICODE).')');
|
||||
if (count($middlewares) > 0) {
|
||||
$this->appendMiddleware($middlewares, $class, $method);
|
||||
$response = $this->appendMiddleware($response, $middlewares);
|
||||
}
|
||||
$middlewares = array_column($this->groupTack, 'middleware');
|
||||
if (count($middlewares) > 0) {
|
||||
$this->appendMiddleware($middlewares, $class, $method);
|
||||
$response = $this->appendMiddleware($response, $middlewares);
|
||||
|
||||
$reflect = \Kiri::getDi()->getReflectionClass($class);
|
||||
$attributes = $reflect->getMethod($method)->getAttributes(Annotate\Middleware::class);
|
||||
|
||||
foreach ($attributes as $attribute) {
|
||||
if (!in_array($attribute->getName(), $response)) {
|
||||
$response[] = $attribute->getName();
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $response
|
||||
* @param array $middlewares
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @return void
|
||||
* @throws
|
||||
* @return array
|
||||
*/
|
||||
private function appendMiddleware(array $middlewares, $class, $method): void
|
||||
private function appendMiddleware(array $response, array $middlewares): array
|
||||
{
|
||||
foreach ($middlewares as $middleware) {
|
||||
if (is_string($middleware)) {
|
||||
$middleware = [$middleware];
|
||||
}
|
||||
foreach ($middleware as $value) {
|
||||
Middleware::set($class, $method, $value);
|
||||
if (!in_array($value, $response)) {
|
||||
$response[] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user