// 定义事先的中间件
$middlewares = [
function ($request1, $next) {
echo "Middleware 1 start\n";
$request = $next($request1 );
echo "Middleware 1 end\n";
return $request;
},
function ($request2, $next) {
echo "Middleware 2 start\n";
$request = $next($request2 . " c ");
echo "Middleware 2 end\n";
return $request;
},
];
//
$middlewares = array_reverse($middlewares );
//
$callback = array_reduce($middlewares, function ($carry, $pipe) {
return function ($request) use ($carry, $pipe) {
return $pipe($request, $carry);
};
}, function ($request) {
echo "Processing request: $request\n";
return $request;
});
//
$callback("my request");
不错