src/Middleware/MiddlewareBuilder.php
public function buildMiddleware($middlewares): MiddlewareInterface
    {
        if ($middlewares instanceof MiddlewareInterface) {
            return $middlewares;
        }

        if (is_callable($middlewares) && true === $this->isAdmissibleMiddlewares($middlewares)) {
            return new CallableMiddlewareDecorator($middlewares);
        }

        if (is_string($middlewares)) {
            if (!class_exists($middlewares)) {
                throw new InvalidArgumentException(
                    sprintf('Unable to create middleware "%s"; not a valid class or service name', $middlewares)
                );
            }

            $loader = new LazyLoading($this->container);
            $instance = $loader->load($middlewares);

            if ($instance instanceof MiddlewareInterface) {
                return $instance;
            }

            if (true === $this->isAdmissibleMiddlewares($instance)) {
                return new CallableMiddlewareDecorator($instance);
            }
        }

        if (is_array($middlewares)) {
            $queue = new \SplQueue();
            $middlewares = array_reverse($middlewares);

            foreach ($middlewares as $middleware) {
                $queue->enqueue(new Route(
                    '*',
                    $this->buildMiddleware($middleware)
                ));
            }

            return new MiddlewareCollection($queue);
        }

        throw new InvalidArgumentException('Argument middleware isn\'t callable.');
    }
public function buildMiddleware($middlewares): MiddlewareInterface
    {
        if ($middlewares instanceof MiddlewareInterface) {
            return $middlewares;
        }

        if (is_callable($middlewares) && true === $this->isAdmissibleMiddlewares($middlewares)) {
            return new CallableMiddlewareDecorator($middlewares);
        }

        if (is_string($middlewares)) {
            if (!class_exists($middlewares)) {
                throw new InvalidArgumentException(
                    sprintf('Unable to create middleware "%s"; not a valid class or service name', $middlewares)
                );
            }

            $loader = new LazyLoading($this->container);
            $instance = $loader->load($middlewares);

            if ($instance instanceof MiddlewareInterface) {
                return $instance;
            }

            if (true === $this->isAdmissibleMiddlewares($instance)) {
                return new CallableMiddlewareDecorator($instance);
            }
        }

        if (is_array($middlewares)) {
            $queue = new \SplQueue();
            $middlewares = array_reverse($middlewares);

            foreach ($middlewares as $middleware) {
                $queue->enqueue(new Route(
                    '*',
                    $this->buildMiddleware($middleware)
                ));
            }

            return new MiddlewareCollection($queue);
        }

        throw new InvalidArgumentException('Argument middleware isn\'t callable.');
    }
@param array|callable|MiddlewareInterface|string $middlewares

@return MiddlewareInterface