Files
zhamao-framework/src/ZM/Middleware/MiddlewareHandler.php

184 lines
6.7 KiB
PHP
Raw Normal View History

2022-08-13 17:00:29 +08:00
<?php
declare(strict_types=1);
namespace ZM\Middleware;
use OneBot\Util\Singleton;
use ZM\Exception\InvalidArgumentException;
class MiddlewareHandler
{
use Singleton;
/**
* @var array 存储中间件的
*/
protected array $middlewares = [];
2022-08-13 17:00:29 +08:00
/**
* @var array 存储注册中间件的类和方法
*/
protected array $reg_map = [];
2022-08-13 17:00:29 +08:00
/**
* @var array 用于将中间件名称压栈
*/
protected array $stack = [];
2022-08-13 17:00:29 +08:00
/**
* @var array 用于将正在运行的中间件压栈
*/
protected array $callable_stack = [];
2022-08-13 17:00:29 +08:00
public function registerBefore(string $name, callable $callback)
{
$this->middlewares[$name]['before'] = $callback;
}
public function registerAfter(string $name, callable $callback)
{
if (
is_array($callback) // 如果是数组类型callback
&& is_object($callback[0]) // 且为动态调用
&& isset($this->middlewares[$name]['before']) // 且存在before
&& is_array($this->middlewares[$name]['before']) // 且before也是数组类型callback
&& is_object($this->middlewares[$name]['before'][0]) // 且before类型也为动态调用
&& $this->middlewares[$name]['before'][0]::class === $callback[0]::class // 且before和after在一个类
2022-08-13 17:00:29 +08:00
) {
// 那么就把after的对象替换为和before同一个
$callback[0] = $this->middlewares[$name]['before'][0];
}
$this->middlewares[$name]['after'] = $callback;
}
public function registerException(string $name, string $exception_class, callable $callback)
{
$this->middlewares[$name]['exception'][$exception_class] = $callback;
}
/**
* @throws InvalidArgumentException
*/
public function bindMiddleware(callable $callback, string $name, array $params = [])
{
$stack_id = $this->getStackId($callback);
// TODO: 对中间件是否存在进行检查
if (class_exists($name)) {
$obj = resolve($name);
}
$this->reg_map[$stack_id][] = [$name, $params];
}
2022-08-13 22:01:03 +08:00
public function getPipeClosure(callable $callback, $stack_id)
{
$pipe_func = function (array $mid_list, ...$args) use ($callback, $stack_id, &$pipe_func) {
$return = true;
try {
while (($item = array_shift($mid_list)) !== null) {
$this->stack[$stack_id][] = $item;
// 如果是 pipeline 形式的中间件,则使用闭包回去
if (class_exists($item[0]) && is_a($item[0], PipelineInterface::class, true)) {
return resolve($item[0])->handle(function (...$args) use ($mid_list, &$pipe_func) {
return $pipe_func($mid_list, ...$args);
}, ...$args);
} elseif (isset($this->middlewares[$item[0]]['before'])) {
$return = container()->call($this->middlewares[$item[0]]['before'], $args);
// before 没执行完,直接跳出,不执行本体
if ($return === false) {
array_pop($this->callable_stack);
$mid_list = [];
break;
}
}
}
if ($return !== false) {
$result = container()->call($callback, $args);
}
while (isset($this->stack[$stack_id]) && ($item = array_pop($this->stack[$stack_id])) !== null) {
// 如果是 pipeline 形式的中间件,则使用闭包回去
if (class_exists($item[0]) && is_a($item[0], PipelineInterface::class, true)) {
continue;
}
if (isset($this->middlewares[$item[0]]['after'])) {
$after_result = container()->call($this->middlewares[$item[0]]['after'], $args);
}
}
} catch (\Throwable $e) {
2022-08-13 22:01:03 +08:00
while (isset($this->stack[$stack_id]) && ($item = array_pop($this->stack[$stack_id])) !== null) {
// 如果是 pipeline 形式的中间件,则使用闭包回去
if (class_exists($item[0]) && is_a($item[0], PipelineInterface::class, true)) {
throw $e;
}
foreach (($this->middlewares[$item[0]]['exception'] ?? []) as $k => $v) {
if (is_a($e, $k)) {
$exception_result = $v($e);
unset($this->stack[$stack_id]);
break 2;
}
}
}
if (!isset($exception_result)) {
throw $e;
}
}
return $result ?? $after_result ?? $exception_result ?? null;
};
unset($this->stack[$stack_id]);
2022-08-13 22:01:03 +08:00
return $pipe_func;
}
2022-08-13 17:00:29 +08:00
/**
* @throws InvalidArgumentException
* @throws \Throwable
2022-08-13 17:00:29 +08:00
*/
2022-08-13 22:04:05 +08:00
public function process(callable $callback, ...$args)
2022-08-13 17:00:29 +08:00
{
$stack_id = $this->getStackId($callback);
unset($this->stack[$stack_id]);
2022-08-13 22:01:03 +08:00
2022-08-13 17:00:29 +08:00
$this->callable_stack[] = $callback;
2022-08-13 22:01:03 +08:00
2022-08-13 17:00:29 +08:00
// 遍历执行before并压栈并在遇到返回false后停止
try {
2022-08-13 22:01:03 +08:00
$mid_list = ($this->reg_map[$stack_id] ?? []);
2022-08-13 22:04:05 +08:00
$final_result = ($this->getPipeClosure($callback, $stack_id))($mid_list, ...$args);
2022-08-13 17:00:29 +08:00
} finally {
array_pop($this->callable_stack);
}
2022-08-13 22:01:03 +08:00
return $final_result ?? null;
2022-08-13 17:00:29 +08:00
}
/**
* 获取正在运行的回调调用对象可能是Closure、array、string
*
* @return false|mixed
*/
public function getCurrentCallable()
{
return end($this->callable_stack);
}
/**
* @param callable $callback 可执行的方法
* @throws InvalidArgumentException
*/
2022-08-13 22:01:03 +08:00
public function getStackId(callable $callback): string
2022-08-13 17:00:29 +08:00
{
if ($callback instanceof \Closure) {
2022-08-13 17:00:29 +08:00
// 闭包情况下直接根据闭包的ID号来找stack
return strval(spl_object_id($callback));
}
if (is_array($callback) && count($callback) === 2) {
// 活性调用,根据组合名称来判断
return (is_object($callback[0]) ? $callback[0]::class : $callback[0]) . '::' . $callback[1];
2022-08-13 17:00:29 +08:00
}
if (is_string($callback)) {
return $callback;
}
throw new InvalidArgumentException('传入的 callable 有误!');
}
}