symfony/dependency-injection
Symfony DependencyInjection standardizes and centralizes object construction with a powerful service container. Define services and parameters, manage autowiring and configuration, and optimize performance through compilation for cleaner, decoupled apps.
symfony/dependency-injection component is a foundational part of Symfony’s ecosystem but is not natively integrated into Laravel. Laravel uses its own Service Container (PSR-11 compliant) with autowiring and bindings, which is inspired by Symfony’s DI but diverges in implementation (e.g., Laravel’s Illuminate\Container\Container vs. Symfony’s DependencyInjection\ContainerInterface).#[AutowireCallable]) for modern dependency injection.CompilerPass system for runtime container modifications).#[Inject]).ContainerBuilder (breaking change).CompilerPass system.Illuminate\Container vs. Symfony\Component\DependencyInjection).symfony/dependency-injection to pre-compile service definitions and inject them into Laravel’s container.PhpDumper to generate a static container for Laravel.Definition, Reference, CompilerPass). Documentation and training would be required.| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Breaking Changes | High | Phase integration in a feature flag or new microservice. |
| Performance Overhead | Medium | Benchmark Symfony’s DI vs. Laravel’s container. |
| Developer Adoption | High | Provide migration guides, code samples, and training. |
| Dependency Bloat | Low | Symfony’s DI is lightweight (~1MB), but adds complexity. |
| Long-Term Maintenance | Medium | Align with Laravel’s release cycle (Symfony DI is more frequent). |
| Testing Overhead | High | Rewrite container-related tests for Symfony’s DI. |
Why Symfony’s DI?
Scope of Integration
CompilerPass?Team Readiness
container()->extend(), custom BootstrapServiceProvider)?Performance Impact
PhpDumper) justify the integration?Long-Term Viability
Illuminate\Container\Container).app/Providers/AppServiceProvider.php).#[AutowireCallable]).PhpDumper.contextual binding, tagged service helpers (Laravel’s app()->tag()).Symfony\Component\DependencyInjection\ContainerBuilder as the new container.Illuminate\Container\Container to delegate to Symfony’s DI.register() methods to Symfony’s loadFromExtension() or CompilerPass.AutowireLocator.PhpDumper to generate a static container for Laravel.// In a Laravel service provider
$containerBuilder = new ContainerBuilder();
$containerBuilder->register('app.service', AppService::class);
$dumper = new PhpDumper($containerBuilder);
file_put_contents(base_path('bootstrap/cache/services.php'), $dumper->dump());
CompilerPass to modify the container before Laravel loads it.$containerBuilder->addCompilerPass(new class implements CompilerPassInterface {
public function process(ContainerBuilder $container) {
$definition = $container->findDefinition('app.service');
$definition->addTag('monolog.logger');
}
});
symfony/dependency-injection only for tagged services while keeping the rest of Laravel’s container intact.use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/config'));
$loader->load('services.yaml');
// Manually merge with Laravel’s container
| Laravel Feature | Symfony DI Compatibility | Workaround |
|---|---|---|
| Autowiring | Partial (use AutowireLocator) |
Manual configuration or hybrid approach. |
| Service Providers | Low (use CompilerPass) |
Rewrite providers as extensions. |
| Contextual Binding | No | Implement custom logic. |
Tagged Services (app()->tag()) |
Yes (native support) | Use Symfony’s addTag(). |
PHP Attributes (#[Inject]) |
Yes | Requires Symfony 6.0+. |
app()->bind()/app()->singleton() |
Yes (via set()) |
Direct mapping. |
app()->when() (contextual) |
No | Custom CompilerPass or fallback. |
PhpDumper).How can I help you explore Laravel packages today?