league/container
league/container is a lightweight PSR-11 dependency injection container for PHP. Define entries, factories, and autowiring-friendly services to manage application dependencies cleanly, with modern PHP support and solid tooling for testing and analysis.
register()/boot() methods). This reduces boilerplate compared to manual bindings.Illuminate\Container\Container (PSR-11) can be swapped with League\Container via interface-based dependency injection. Example:
$app->singleton(Psr\Container\ContainerInterface::class, function ($app) {
return new League\Container\Container();
});
app()->makeWith()) unless wrapped in adapters.when()) and tagged bindings are not natively supported but could be implemented via inflectors or custom middleware.League\Container's add() with closures to replicate Laravel’s dynamic bindings.| Risk Area | Mitigation Strategy |
|---|---|
| Laravel-Specific Features | Create a thin adapter layer (e.g., LaravelLeagueContainer) to bridge gaps. |
| Performance Tradeoffs | Benchmark against Laravel’s container; use only in non-web contexts (e.g., CLI). |
| Testing Overhead | Mock Psr\Container\ContainerInterface in tests; ensure no Laravel-specific APIs are used. |
| Dependency Conflicts | Use composer require league/container with --ignore-platform-reqs if needed. |
| Long-Term Maintenance | Monitor Laravel’s container deprecations; align with PSR-11 updates. |
League\Container’s inflectors or delegates replace these?league/container (PSR-11).psr/container (already included via Laravel).league/plates (if using inflectors for dynamic logic).Illuminate\Bus\Dispatcher) if performance is critical.| Phase | Action | Tools/Techniques |
|---|---|---|
| Assessment | Audit dependencies for Laravel-specific container APIs (e.g., app()->makeWith()). |
Static analysis (PHPStan), test coverage reports. |
| Adapter Layer | Create LaravelLeagueContainer to bridge gaps (e.g., contextual bindings). |
Decorator pattern, PSR-11 facade wrappers. |
| Hybrid Mode | Run both containers in parallel; migrate services incrementally. | Service provider hooks, app->resolving() events. |
| Full Switch | Replace Illuminate\Container\Container with League\Container via DI binding. |
config/app.php, AppServiceProvider::register(). |
| Validation | Test all features (facades, middleware, jobs) in staging. | Laravel Pint, Pest, manual QA. |
| Laravel Feature | Compatibility Status | Workaround |
|---|---|---|
| Service Providers | ✅ Full | Use register()/boot() as-is. |
| Facades | ⚠️ Partial | Wrap in PSR-11 facade (e.g., Facade\Ignition). |
| Contextual Bindings | ❌ No | Implement via League\Container\Inflector. |
| Tagged Services | ❌ No | Use custom metadata in bindings or a decorator. |
app()->makeWith() |
❌ No | Replace with League\Container’s add() + closures. |
| Middleware Resolution | ✅ Full | Resolve via PSR-11 (e.g., $container->get(Middleware::class)). |
| Queue Jobs | ✅ Full | No changes needed; resolves via PSR-11. |
| Events/Listeners | ✅ Full | Bind listeners via container (e.g., Event::listen(Foo::class, fn() => $container->get(Listener::class))). |
AppServiceProvider to bind Psr\Container\ContainerInterface to League\Container.makeWith).League\Container’s error messages are more straightforward.League\Container’s docs.telescope, horizon) may assume the native container.League\Container’s API.How can I help you explore Laravel packages today?