getDispatchStack($dispatchable); } if (is_callable($dispatchable)) { return $dispatchable($request, $response, $next); } elseif ($dispatchable instanceof MiddlewareInterface) { $delegate = new DispatcherDelegate($response, $next); return $dispatchable->process($request, $delegate); } elseif ($dispatchable instanceof ResponseInterface) { return $dispatchable; } else { throw new DispatchException("Unable to dispatch middleware."); } } protected function getDispatchStack($dispatchables) { $stack = new DispatchStack($this); foreach ($dispatchables as $dispatchable) { $stack->add($dispatchable); } return $stack; } }