guzzle 中间件原理
<?php #闭包链式调用 $handle = function ($method) { echo $method, PHP_EOL; return 'done'; }; $stack = []; array_push($stack, [function ($callable, $msg) use($handle) { return function () use ($callable, $msg) { echo '闭包 1', PHP_EOL; return $callable($msg); }; }, 'one']); array_push($stack, [function ($callable, $msg) use($handle) { return function () use ($callable, $msg) { echo '闭包 2', PHP_EOL; return $callable($msg); }; }, 'two']); $prev = $handle; foreach (array_reverse($stack) as $k => $fn) { $prev = $fn[0]($prev, $fn[1]); } echo $prev(); echo '----------------', PHP_EOL; $prev = $handle; foreach ($stack as $k => $fn) { $prev = $fn[0]($prev, $fn[1]); } echo $prev();