改名
This commit is contained in:
@@ -5,7 +5,6 @@ namespace Http\Route;
|
||||
|
||||
|
||||
use Closure;
|
||||
use Http\IInterface\MiddlewareInterface;
|
||||
use Kiri\Abstracts\BaseObject;
|
||||
|
||||
|
||||
@@ -16,112 +15,65 @@ use Kiri\Abstracts\BaseObject;
|
||||
class MiddlewareManager extends BaseObject
|
||||
{
|
||||
|
||||
private static array $_middlewares = [];
|
||||
private static array $_middlewares = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @param array|string $middlewares
|
||||
*/
|
||||
public static 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($middleware, static::$_middlewares[$class . '::' . $method])) {
|
||||
continue;
|
||||
}
|
||||
static::$_middlewares[$class . '::' . $method][] = $middleware;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @param array|string $middlewares
|
||||
* @return bool
|
||||
*/
|
||||
public static function add($class, $method, array|string $middlewares): bool
|
||||
{
|
||||
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;
|
||||
} else {
|
||||
foreach ($middlewares as $middleware) {
|
||||
if (in_array($middleware, static::$_middlewares[$class . '::' . $method])) {
|
||||
continue;
|
||||
}
|
||||
static::$_middlewares[$class . '::' . $method][] = $middleware;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $handler
|
||||
* @return mixed|null
|
||||
*/
|
||||
public static function get($handler)
|
||||
{
|
||||
if ($handler instanceof Closure) {
|
||||
return null;
|
||||
}
|
||||
[$class, $method] = [$handler[0]::class, $handler[1]];
|
||||
if (!static::hasMiddleware($class, $method)) {
|
||||
return null;
|
||||
}
|
||||
return static::$_middlewares[$class . '::' . $method];
|
||||
}
|
||||
/**
|
||||
* @param $handler
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get($handler): mixed
|
||||
{
|
||||
if ($handler instanceof Closure) {
|
||||
return null;
|
||||
}
|
||||
[$class, $method] = [$handler[0]::class, $handler[1]];
|
||||
if (!static::hasMiddleware($class, $method)) {
|
||||
return null;
|
||||
}
|
||||
return static::$_middlewares[$class . '::' . $method];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasMiddleware($class, $method): bool
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = $class::class;
|
||||
}
|
||||
return isset(static::$_middlewares[$class . '::' . $method]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @param $caller
|
||||
* @return mixed
|
||||
*/
|
||||
public static 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 MiddlewareInterface) {
|
||||
$pipe = [$pipe, 'onHandler'];
|
||||
}
|
||||
return call_user_func($pipe, $passable, $stack);
|
||||
};
|
||||
}, $caller);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $middlewares
|
||||
* @param Closure $caller
|
||||
* @return Closure
|
||||
*/
|
||||
public static function closureMiddlewares($middlewares, Closure $caller): Closure
|
||||
{
|
||||
if (empty($middlewares)) {
|
||||
return $caller;
|
||||
}
|
||||
return array_reduce(array_reverse($middlewares), function ($stack, $pipe) {
|
||||
return function ($passable) use ($stack, $pipe) {
|
||||
if ($pipe instanceof MiddlewareInterface) {
|
||||
$pipe = [$pipe, 'onHandler'];
|
||||
}
|
||||
return call_user_func($pipe, $passable, $stack);
|
||||
};
|
||||
}, $caller);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasMiddleware($class, $method): bool
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = $class::class;
|
||||
}
|
||||
return isset(static::$_middlewares[$class . '::' . $method]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -52,6 +52,8 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
|
||||
} catch (Error | \Throwable $exception) {
|
||||
$psr7Response = $this->exceptionHandler->emit($exception, $this->response);
|
||||
} finally {
|
||||
$request_time_float = $request->server['request_time_float'] - $request->server['request_time'];
|
||||
$psr7Response->withHeader('Run-Time', microtime(true) - $request_time_float);
|
||||
$this->responseEmitter->sender($response, $psr7Response);
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ use Http\IInterface\MiddlewareInterface;
|
||||
*/
|
||||
public function execute(mixed $class, mixed $method = null): mixed
|
||||
{
|
||||
MiddlewareManager::addMiddlewares($class, $method, $this->middleware);
|
||||
MiddlewareManager::add($class, $method, $this->middleware);
|
||||
return parent::execute($class, $method);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user