private function sortEnvs(MutableBag $envs): MutableBag
    {
        $resolver = DependencyResolver::fromMap($envs, function (MutableBag $env, $key) {
            // Default is the base case and cannot depend upon anything.
            if ($key === 'default') {
                return [];
            }

            $parent = $env->get('_extends', 'default');

            return [$parent];
        });

        try {
            return $resolver->sort()->mutable();
        } catch (CyclicDependencyException $e) {
            $e->setItemName('environments');
            throw $e;
        } catch (UnknownDependencyException $e) {
            $message = sprintf(
                "Config environment '%s' cannot extend '%s' environment because it does not exist.",
                $e->getItem(),
                $e->getDependency()
            );
            throw new ConfigException($message, 0, $e);
        }
    }