diff --git a/.gitignore b/.gitignore index 7c7975ef..ea188d7b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +src/EconumoFamilyBundle ###> symfony/framework-bundle ### /.env.local diff --git a/src/Kernel.php b/src/Kernel.php index 43ae7935..08bb22ab 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -7,6 +7,7 @@ use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\HttpKernel\Kernel as BaseKernel; use Symfony\Component\Routing\RouteCollectionBuilder; @@ -19,6 +20,11 @@ class Kernel extends BaseKernel */ private const CONFIG_EXTS = '.{php,xml,yaml,yml}'; + /** + * @var Bundle[] + */ + private array $econumoBundles = []; + public function registerBundles(): iterable { $contents = require $this->getProjectDir().'/config/bundles.php'; @@ -27,6 +33,13 @@ public function registerBundles(): iterable yield new $class(); } } + foreach ($this->getEconumoBundles() as $className) { + $bundle = new $className(); + if (method_exists($bundle, 'isActive') && $bundle->isActive()) { + $this->econumoBundles[$className] = $bundle; + yield $bundle; + } + } } public function getProjectDir(): string @@ -47,6 +60,19 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa $loader->load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob'); $loader->load($confDir.'/{services}_'.$this->environment.self::CONFIG_EXTS, 'glob'); + foreach ($this->econumoBundles as $bundle) { + $loader->load($bundle->getPath() . '/Resources/config/{packages}/*' . self::CONFIG_EXTS, 'glob'); + $loader->load( + $bundle->getPath() . '/Resources/config/{packages}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, + 'glob' + ); + $loader->load($bundle->getPath() . '/Resources/config/{services}' . self::CONFIG_EXTS, 'glob'); + $loader->load( + $bundle->getPath() . '/Resources/config/{services}_' . $this->environment . self::CONFIG_EXTS, + 'glob' + ); + } + $container->registerForAutoconfiguration(EventHandlerInterface::class)->addTag('messenger.message_handler'); } @@ -57,5 +83,28 @@ protected function configureRoutes(RouteCollectionBuilder $routes): void $routes->import($confDir.'/{routes}/'.$this->environment.'/*'.self::CONFIG_EXTS, '/', 'glob'); $routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob'); $routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob'); + + foreach ($this->econumoBundles as $bundle) { + $routes->import( + $bundle->getPath() . '/Resources/config/{routes}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, + '/', + 'glob' + ); + $routes->import($bundle->getPath() . '/Resources/config/{routes}/*' . self::CONFIG_EXTS, '/', 'glob'); + $routes->import($bundle->getPath() . '/Resources/config/{routes}' . self::CONFIG_EXTS, '/', 'glob'); + } + } + + private function getEconumoBundles(): array + { + $bundlesPattern = $this->getProjectDir().'/src/*Bundle/*Bundle.php'; + $files = glob($bundlesPattern); + $bundles = []; + foreach ($files as $file) { + $filename = pathinfo($file, PATHINFO_FILENAME); + $bundles[] = sprintf('App\%s\%s', $filename, $filename); + } + + return $bundles; } }