mmoreram/symfony-bundle-dependencies
Symfony bundle to manage inter-bundle dependencies and load order. Define requirements between bundles and let the resolver ensure needed bundles are enabled and initialized first, helping avoid missing services and configuration issues in complex apps.
Installation:
composer require mmoreram/symfony-bundle-dependencies
Add the bundle to your config/bundles.php (Symfony) or config/app.php (Laravel via Symfony bridge):
return [
// ...
Mmoreram\BundleDependenciesBundle\MmoreramBundleDependenciesBundle::class => ['all' => true],
];
First Use Case:
Define a dependency between two bundles in config/packages/mmoreram_bundle_dependencies.yaml:
dependencies:
Acme\Bundle1: [Acme\Bundle2]
This ensures Bundle2 loads before Bundle1.
Verify:
Run php bin/console debug:container (Symfony) or check Laravel service container for bundle order.
Bundle Ordering:
Use config/packages/mmoreram_bundle_dependencies.yaml to enforce load order:
dependencies:
App\Bundle\Admin: [App\Bundle\Auth, App\Bundle\Security]
Auth and Security bundles are initialized before Admin.Conditional Dependencies: Leverage Symfony’s environment-aware configuration:
dependencies:
App\Bundle\Stripe: [App\Bundle\Payment]
_env: [dev, staging] # Only enforce in dev/staging
Dynamic Dependencies (PHP): Override the dependency resolver in a custom compiler pass (Symfony):
use Mmoreram\BundleDependenciesBundle\Dependency\DependencyResolverInterface;
public function process(ContainerBuilder $container)
{
$resolver = $container->getDefinition('mmoreram_bundle_dependencies.resolver');
$resolver->addMethodCall('addDynamicDependency', [
'App\Bundle\Dynamic', ['App\Bundle\Config']
]);
}
Laravel Integration: Use the Symfony bridge to integrate with Laravel’s service provider ordering:
// In a Laravel service provider
$this->app->register(Mmoreram\BundleDependenciesBundle\MmoreramBundleDependenciesBundle::class);
{
"scripts": {
"post-autoload-cmd": [
"@php bin/console mmoreram:bundle-dependencies:validate"
]
}
}
php bin/console mmoreram:bundle-dependencies:validate --fail
Circular Dependencies: The package detects and throws exceptions for circular dependencies. Example:
dependencies:
App\Bundle\A: [App\Bundle\B]
App\Bundle\B: [App\Bundle\A] # Throws: Circular dependency detected!
Lazy-Loaded Bundles:
If bundles are lazy-loaded (e.g., via loadFromDirectory), dependencies may not be enforced until the bundle is explicitly loaded.
prepend: true in config/bundles.php for critical bundles.Namespace Conflicts:
Ensure bundle namespaces in dependencies match the actual class names (e.g., Acme\Bundle1\Bundle vs. Acme\Bundle1).
Symfony Flex Autoconfigure:
If using Symfony Flex, the package may not auto-register. Manually add it to config/bundles.php.
Validate Dependencies:
php bin/console mmoreram:bundle-dependencies:validate --verbose
Outputs a graph of dependencies and potential issues.
Check Load Order:
php bin/console debug:container --parameter=kernel.bundles
Verify the order matches expectations.
Visualize Dependencies: Use Graphviz to visualize bundle relationships:
php bin/console mmoreram:bundle-dependencies:graph > dependencies.dot
dot -Tpng dependencies.dot -o dependencies.png
Environment-Specific Rules: Override dependencies per environment:
# config/packages/dev/mmoreram_bundle_dependencies.yaml
dependencies:
App\Bundle\DebugToolbar: [App\Bundle\Profiler]
Extend the Resolver: Create a custom resolver for complex logic:
namespace App\Dependency;
use Mmoreram\BundleDependenciesBundle\Dependency\DependencyResolverInterface;
class CustomResolver implements DependencyResolverInterface
{
public function resolve(array $dependencies): array
{
// Custom logic (e.g., skip optional bundles)
return $dependencies;
}
}
Register it in config/packages/mmoreram_bundle_dependencies.yaml:
resolver_class: App\Dependency\CustomResolver
Laravel Service Providers:
For Laravel, treat bundles as service providers and order them in AppServiceProvider:
public function register()
{
$this->app->register(\App\Providers\AuthServiceProvider::class);
$this->app->register(\App\Providers\AdminServiceProvider::class);
// Dependencies enforced via provider order
}
Performance: Cache the dependency graph in production:
# config/packages/prod/mmoreram_bundle_dependencies.yaml
cache_graph: true
How can I help you explore Laravel packages today?