middleware = \array_values($middleware); } /** * @param ServerRequestInterface $request * @return ResponseInterface|PromiseInterface * @throws \Exception */ public function __invoke(ServerRequestInterface $request) { if (empty($this->middleware)) { throw new \RuntimeException('No middleware to run'); } return $this->call($request, 0); } /** @internal */ public function call(ServerRequestInterface $request, $position) { // final request handler will be invoked without a next handler if (!isset($this->middleware[$position + 1])) { $handler = $this->middleware[$position]; return $handler($request); } $that = $this; $next = function (ServerRequestInterface $request) use ($that, $position) { return $that->call($request, $position + 1); }; // invoke middleware request handler with next handler $handler = $this->middleware[$position]; return $handler($request, $next); } }