private function resolveBundleDependencies(
        KernelInterface $kernel,
        array &$bundleStack,
        array &$visitedBundles,
        array $bundles
    ) {
        $bundles = array_reverse($bundles);

        foreach ($bundles as $bundle) {
            /*
             * Each visited node is prioritized and placed at the beginning.
             */
            $this
                ->prioritizeBundle(
                    $bundleStack,
                    $bundle
                );
        }

        foreach ($bundles as $bundle) {
            $bundleNamespace = $this->getBundleDefinitionNamespace($bundle);
            /*
             * If have already visited this bundle, continue. One bundle can be
             * processed once.
             */
            if (isset($visitedBundles[$bundleNamespace])) {
                continue;
            }

            $visitedBundles[$bundleNamespace] = true;
            $bundleNamespaceObj = new \ReflectionClass($bundleNamespace);
            if ($bundleNamespaceObj->implementsInterface(DependentBundleInterface::class)) {
                /**
                 * @var DependentBundleInterface
                 */
                $bundleDependencies = $bundleNamespace::getBundleDependencies($kernel);

                $this->resolveBundleDependencies(
                    $kernel,
                    $bundleStack,
                    $visitedBundles,
                    $bundleDependencies
                );
            }
        }
    }