这里为什么要用两次闭包呢,一层不就够了吗?
protected function carry()
{
return function ($stack, $pipe) {
return function ($passable) use ($stack, $pipe) {
if (is_callable($pipe)) {
// If the pipe is an instance of a Closure, we will just call it directly but
// otherwise we'll resolve the pipes out of the container and call it with
// the appropriate method and arguments, returning the results back out.
return $pipe($passable, $stack);
} elseif (! is_object($pipe)) {
.....
//这样不行吗
return function ($stack, $pipe,$passable) {
if (is_callable($pipe)) {
// If the pipe is an instance of a Closure, we will just call it directly but
// otherwise we'll resolve the pipes out of the container and call it with
// the appropriate method and arguments, returning the results back out.
return $pipe($passable, $stack);
} elseif (! is_object($pipe)) {
额,能跑通,就代表应该是另一种方式;不过你这是通过传参方式实现 $stack 的迭代。官方通过挂载静态变量的方式迭代 $stack。静态变量感觉要好一些。因为就定义一遍,多个中间件使用。