private function scanBundle(Bundle $bundle, BundleService $bundleService) { $bundleService->addBundle($bundle); if ($bundle->hasBundles()) { $rootNamespace = $bundle->getNamespace(); $rootDir = $bundle->getBundlesDir(); $bundles = Chain::create(scandir($bundle->getBundlesDir()))->filter(function (string $dir) use($rootDir) { return $dir != '.' && $dir != '..' && is_dir($rootDir . '/' . $dir); })->map(function (string $dir) use($rootNamespace) { $bundleClassName = "{$rootNamespace}\\{$dir}\\{$dir}Bundle"; if (!class_exists($bundleClassName)) { $bundleClassName = "{$rootNamespace}\\Bundles\\{$dir}\\{$dir}Bundle"; if (!class_exists($bundleClassName)) { throw new \Exception(sprintf('No Bundle available for bundle `%s`', $bundleClassName)); } } if (!is_subclass_of($bundleClassName, Bundle::class)) { throw new \Exception(sprintf('Bundle `%s` should implements interface %s', $bundleClassName, Bundle::class)); } return new $bundleClassName(); })->array; foreach ($bundles as $bundle) { $this->scanBundle($bundle, $bundleService); } } }