ashleydawson/multibundle
Group and register multiple dependent Symfony2 bundles as a single logical unit. Extend AbstractMultiBundle to declare required bundles, then call registerInto() in AppKernel to add the bundle and its dependencies in one step.
Installation
composer require ashleydawson/multibundle
Add to config/app.php under providers (Laravel 5.x):
'providers' => [
// ...
Ashleydawson\Multibundle\MultibundleServiceProvider::class,
],
Basic Registration
In a service provider (e.g., AppServiceProvider):
use Ashleydawson\Multibundle\MultibundleManager;
public function boot(MultibundleManager $manager)
{
$manager->register('my_bundle', function () {
return new \MyBundle\MyBundle();
});
}
First Use Case
Load bundles dynamically in bootstrap/app.php (Laravel 8+):
$app->register(\Ashleydawson\Multibundle\MultibundleServiceProvider::class);
$app->make(\Ashleydawson\Multibundle\MultibundleManager::class)->load();
Workflow:
config/multibundle.php:
'bundles' => [
'my_bundle' => [
'class' => \MyBundle\MyBundle::class,
'enabled' => env('MY_BUNDLE_ENABLED', false),
],
],
AppServiceProvider:
public function register()
{
$manager = $this->app->make(MultibundleManager::class);
$manager->load('my_bundle'); // Load single bundle
// OR
$manager->load(); // Load all enabled bundles
}
Override App\Http\Kernel to conditionally register bundles:
protected function registerBundles()
{
$manager = app(MultibundleManager::class);
$manager->load(['my_bundle', 'another_bundle']);
}
Inject MultibundleManager into controllers/services:
public function __construct(MultibundleManager $manager)
{
$this->bundles = $manager->getLoadedBundles();
}
Circular Dependencies Avoid registering bundles that depend on each other. Use lazy loading:
$manager->registerLazy('bundle_a', function () {
return new \BundleA();
});
Namespace Collisions
Ensure bundle namespaces are unique. Prefix with your vendor name (e.g., Vendor\BundleName).
Configuration Overrides If bundles define their own config, merge them manually:
$manager->load('bundle')->getConfig()->merge([
'key' => 'override_value',
]);
Check Loaded Bundles
Dump loaded bundles in tinker:
php artisan tinker
>>> \Ashleydawson\Multibundle\Facades\Multibundle::getLoadedBundles();
Enable Debug Mode
Set debug: true in config/multibundle.php to log bundle registration.
Custom Bundle Resolvers
Extend Ashleydawson\Multibundle\BundleResolverInterface for custom logic (e.g., database-backed bundles).
Event Listeners
Listen for bundle.registered and bundle.loaded events:
$manager->register('bundle')->addListener('bundle.registered', function ($bundle) {
// Post-registration logic
});
Environment-Specific Bundles
Use env() in bundle registration:
$manager->register('bundle', function () {
return env('APP_ENV') === 'local' ? new \LocalBundle() : new \ProdBundle();
});
How can I help you explore Laravel packages today?