symfony/dependency-injection
Symfony DependencyInjection component standardizes and centralizes object construction via a service container. Define services, parameters, and wiring, support autowiring and compilation, and manage dependencies consistently across applications and libraries.
Illuminate\Container). This package is a direct architectural fit for standardizing and centralizing object construction, aligning with Laravel’s service container philosophy.[Inject], [Singleton]), which this package fully supports, enabling type-safe DI without manual binding.Illuminate\Container is a thin wrapper around Symfony DI, so no major refactoring is needed. Existing app() container usage remains compatible.bind(), singleton(), and tag() methods are already DI-agnostic and map directly to Symfony’s API.php artisan container:dump), which Laravel’s bootstrap/cache/services.php already mimics. No runtime overhead.app()->bind()) may need to adopt attribute-based DI (e.g., [Autowire]) for advanced use cases.illuminate/container may introduce subtle differences. Test thoroughly with:
#[Inject] vs. Symfony’s #[Autowire] could cause confusion if both are used. Recommendation: Standardize on one (prefer Symfony’s for consistency).app()->bind() usage.config/services.php (Laravel style) or Symfony’s resources/config/?container:dump) be used, or rely on runtime resolution?ContainerInterface is compatible, but Laravel’s MockApplication may need updates.Illuminate\Container, Illuminate\Foundation\Application, and Illuminate\Contracts\Container\Container.ServiceProvider::register() using Symfony’s CompilerPass).Testing\TestCase and Mockery for dependency substitution.php artisan di:compile for container dumps).| Phase | Action | Tools/Commands |
|---|---|---|
| Assessment | Audit existing app()->bind() calls and identify candidates for DI. |
php artisan container:inspect (custom) |
| Pilot | Replace manual bindings with attributes (e.g., [Autowire]) in a single module. |
php artisan make:service (custom) |
| Hybrid | Use Symfony DI for new services while keeping legacy bindings. | config/di.php (custom loader) |
| Full Adoption | Migrate all services to Symfony DI; replace app() with container(). |
php artisan di:migrate (custom) |
Illuminate\Contracts\Container\Container → Symfony\Component\DependencyInjection\ContainerInterface (drop-in).ServiceProvider → Extend Symfony\Component\DependencyInjection\ContainerAwareServiceProvider.app()->bind() will work unchanged.app()->makeWith()) may need wrappers.config/services.php schema must be adapted to Symfony’s parameters/services structure.[Autowire], [Singleton], etc., in new classes.symfony/dependency-injection compiler pass.CompilerPass to ServiceProvider for runtime DI logic (e.g., dynamic service registration).bindIf() with Symfony’s when().config/services.php to Symfony’s config/packages/di.yaml.Illuminate\Container with a Symfony DI wrapper.bind() calls for simple services.config/di.yaml).debug:container CLI command for runtime inspection.ParameterNotFoundException).di:dump (compile container).di:inspect (analyze service graph).di:validate (check for circular dependencies).Definition, CompilerPass, TaggedIterator).app()->bind(); Symfony DI docs assume Symfony Framework knowledge.LazyService for on-demand initialization (useful for heavy services).stateful: true in Symfony DI (maps to Laravel’s app()->when()).PhpDumper generates optimized PHP code.| Risk | Mitigation Strategy | Detection Tooling |
|---|---|---|
| Circular Dependencies | Use debug:container --show-circular |
Symfony’s CircularReferenceException |
| Missing Services | Validate configs with di:validate |
ParameterNotFoundException |
| Configuration Errors | Schema validation for YAML/XML | InvalidArgumentException |
| Runtime Type Mismatches | Enable CheckTypeDeclarationsPass |
TypeError in compiled container |
How can I help you explore Laravel packages today?