改名
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
namespace HttpServer\Route;
|
||||
|
||||
|
||||
use Closure;
|
||||
use HttpServer\IInterface\Middleware;
|
||||
use Snowflake\Abstracts\BaseObject;
|
||||
|
||||
@@ -15,72 +16,91 @@ use Snowflake\Abstracts\BaseObject;
|
||||
class MiddlewareManager extends BaseObject
|
||||
{
|
||||
|
||||
private static array $_middlewares = [];
|
||||
private static array $_middlewares = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @param array|string $middlewares
|
||||
*/
|
||||
public function addMiddlewares($class, $method, array|string $middlewares)
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = $class::class;
|
||||
}
|
||||
if (!isset(static::$_middlewares[$class . '::' . $method])) {
|
||||
static::$_middlewares[$class . '::' . $method] = [];
|
||||
}
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @param array|string $middlewares
|
||||
*/
|
||||
public function addMiddlewares($class, $method, array|string $middlewares)
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = $class::class;
|
||||
}
|
||||
if (!isset(static::$_middlewares[$class . '::' . $method])) {
|
||||
static::$_middlewares[$class . '::' . $method] = [];
|
||||
}
|
||||
|
||||
if (is_string($middlewares) && !in_array($middlewares, static::$_middlewares[$class . '::' . $method])) {
|
||||
static::$_middlewares[$class . '::' . $method][] = $middlewares;
|
||||
return;
|
||||
}
|
||||
foreach ($middlewares as $middleware) {
|
||||
if (in_array($middlewares, static::$_middlewares[$class . '::' . $method])) {
|
||||
continue;
|
||||
}
|
||||
static::$_middlewares[$class . '::' . $method][] = $middleware;
|
||||
}
|
||||
}
|
||||
if (is_string($middlewares) && !in_array($middlewares, static::$_middlewares[$class . '::' . $method])) {
|
||||
static::$_middlewares[$class . '::' . $method][] = $middlewares;
|
||||
return;
|
||||
}
|
||||
foreach ($middlewares as $middleware) {
|
||||
if (in_array($middlewares, static::$_middlewares[$class . '::' . $method])) {
|
||||
continue;
|
||||
}
|
||||
static::$_middlewares[$class . '::' . $method][] = $middleware;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMiddleware($class, $method)
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = $class::class;
|
||||
}
|
||||
return isset(static::$_middlewares[$class . '::' . $method]);
|
||||
}
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMiddleware($class, $method): bool
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = $class::class;
|
||||
}
|
||||
return isset(static::$_middlewares[$class . '::' . $method]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @param $caller
|
||||
*/
|
||||
public function callerMiddlewares($class, $method, $caller)
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = $class::class;
|
||||
}
|
||||
$middlewares = static::$_middlewares[$class . '::' . $method] ?? [];
|
||||
if (empty($middlewares)) {
|
||||
return $caller;
|
||||
}
|
||||
return array_reduce(array_reverse($middlewares), function ($stack, $pipe) {
|
||||
return function ($passable) use ($stack, $pipe) {
|
||||
if ($pipe instanceof Middleware) {
|
||||
return $pipe->onHandler($passable, $stack);
|
||||
}
|
||||
return call_user_func($pipe, $passable, $stack);
|
||||
};
|
||||
}, $caller);
|
||||
}
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @param $caller
|
||||
* @return mixed
|
||||
*/
|
||||
public function callerMiddlewares($class, $method, $caller): mixed
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = $class::class;
|
||||
}
|
||||
$middlewares = static::$_middlewares[$class . '::' . $method] ?? [];
|
||||
if (empty($middlewares)) {
|
||||
return $caller;
|
||||
}
|
||||
return array_reduce(array_reverse($middlewares), function ($stack, $pipe) {
|
||||
return function ($passable) use ($stack, $pipe) {
|
||||
if ($pipe instanceof Middleware) {
|
||||
return $pipe->onHandler($passable, $stack);
|
||||
}
|
||||
return call_user_func($pipe, $passable, $stack);
|
||||
};
|
||||
}, $caller);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $middlewares
|
||||
* @param Closure $caller
|
||||
* @return Closure
|
||||
*/
|
||||
public function closureMiddlewares($middlewares, Closure $caller): Closure
|
||||
{
|
||||
return array_reduce(array_reverse($middlewares), function ($stack, $pipe) {
|
||||
return function ($passable) use ($stack, $pipe) {
|
||||
if ($pipe instanceof Middleware) {
|
||||
return $pipe->onHandler($passable, $stack);
|
||||
}
|
||||
return call_user_func($pipe, $passable, $stack);
|
||||
};
|
||||
}, $caller);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -94,8 +94,26 @@ class Node extends HttpService
|
||||
} else {
|
||||
$this->handler = $handler;
|
||||
}
|
||||
if (!empty($this->handler) && is_array($this->handler)) {
|
||||
$this->callback = di(MiddlewareManager::class)->callerMiddlewares(
|
||||
return $this->injectMiddleware();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function injectMiddleware(): static
|
||||
{
|
||||
$manager = di(MiddlewareManager::class);
|
||||
if ($this->handler instanceof Closure) {
|
||||
if (!empty($this->middleware)) {
|
||||
$this->callback = $manager->closureMiddlewares($this->middleware, $this->createDispatch());
|
||||
} else {
|
||||
$this->callback = $this->createDispatch();
|
||||
}
|
||||
} else {
|
||||
$manager->addMiddlewares($this->handler[0], $this->handler[1], $this->middleware);
|
||||
$this->callback = $manager->callerMiddlewares(
|
||||
$this->handler[0], $this->handler[1], $this->createDispatch()
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user