src/Server/MiddlewareStack.php
private function resolve($index)
    {
        return new RequestHandler(function (ServerRequestInterface $request) use ($index) {

            $middleware = isset($this->middlewareStack[$index])
                ? $this->middlewareStack[$index]
                : new CallableMiddleware(function () {
                });

            if ($middleware instanceof \Closure) {
                $middleware = new CallableMiddleware($middleware);
            }

            $response = $middleware->process($request, $this->resolve($index + 1));

            if (!($response instanceof ResponseInterface)) {
                throw new UnexpectedValueException(
                    sprintf('The middleware must return an instance of %s', ResponseInterface::class)
                );
            }

            return $response;
        });
    }
private function resolve($index)
    {
        return new RequestHandler(function (ServerRequestInterface $request) use ($index) {

            $middleware = isset($this->middlewareStack[$index])
                ? $this->middlewareStack[$index]
                : new CallableMiddleware(function () {
                });

            if ($middleware instanceof \Closure) {
                $middleware = new CallableMiddleware($middleware);
            }

            $response = $middleware->process($request, $this->resolve($index + 1));

            if (!($response instanceof ResponseInterface)) {
                throw new UnexpectedValueException(
                    sprintf('The middleware must return an instance of %s', ResponseInterface::class)
                );
            }

            return $response;
        });
    }
Creates a request handler for middleware at the position defined by $index

@param int $index

@return RequestHandler

@throws UnexpectedValueException If the return form a middleware is not a ResponseInterface