This commit is contained in:
as2252258@163.com
2021-07-27 03:30:55 +08:00
parent 22fa0d292c
commit 4e3b41331d
13 changed files with 888 additions and 1199 deletions
+5 -41
View File
@@ -19,52 +19,16 @@ class Reduce
* @return mixed
*/
public static function reduce($last, $middleWares): mixed
{
return array_reduce(array_reverse($middleWares), static::core(), $last);
}
/**
* @param $middleWares
* @return mixed
*/
public static function after($middleWares): mixed
{
return array_reduce(array_reverse($middleWares), function ($stack, $pipe) {
return function ($request, $passable) use ($stack, $pipe) {
if (!($pipe instanceof After)) {
return call_user_func($pipe, $request, $passable, $stack);
return function ($passable) use ($stack, $pipe) {
if ($pipe instanceof Middleware) {
return $pipe->onHandler($passable, $stack);
}
return $pipe->onHandler($request, $passable);
return call_user_func($pipe, $passable, $stack);
};
});
}, $last);
}
/**
* @return Closure
*/
private static function core(): Closure
{
return function ($stack, $pipe) {
return static::passable($stack, $pipe);
};
}
/**
* @param $stack
* @param $pipe
* @return Closure
*/
public static function passable($stack, $pipe): Closure
{
return function ($passable) use ($stack, $pipe) {
if ($pipe instanceof Middleware) {
return $pipe->onHandler($passable, $stack);
}
return call_user_func($pipe, $passable, $stack);
};
}
}