Skip to content

Commit

Permalink
Updated Kernel to load Econumo bundles automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
deeravenger committed Oct 13, 2024
1 parent 7a6e3e7 commit d7d3192
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src/EconumoFamilyBundle

###> symfony/framework-bundle ###
/.env.local
Expand Down
49 changes: 49 additions & 0 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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';
Expand All @@ -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
Expand All @@ -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');
}

Expand All @@ -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;
}
}

0 comments on commit d7d3192

Please sign in to comment.