Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Container Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • PSR-11 Compliance: The package fully adheres to the PSR-11 Container Interface, making it a seamless drop-in replacement for Laravel’s native container (which also implements PSR-11). This ensures compatibility with Laravel’s existing dependency injection (DI) ecosystem, including service providers, bindings, and context binding.
  • Laravel Integration: Laravel’s Illuminate\Container\Container (a PSR-11 implementation) can be swapped out for league/container with minimal changes, as both implement the same interface. This is particularly useful for:
    • Performance optimization: league/container is lightweight (~10KB) and optimized for speed.
    • Feature parity: Supports events, attribute-based resolution (#[Inject], #[Resolve]), and service providers, which align with Laravel’s DI patterns.
    • Extensibility: The event system (added in v5.2) allows intercepting container lifecycle events (e.g., BeforeResolveEvent), enabling custom logic without subclassing Laravel’s container.
  • Modern PHP Support: Requires PHP 8.3+, which aligns with Laravel’s latest LTS (Laravel 11+). Backward compatibility with older PHP versions is not a concern for new projects.

Integration Feasibility

  • Zero-Breaking Changes: Since both Laravel’s container and league/container implement PSR-11, swapping them requires no code changes in most cases. Existing bindings (e.g., app()->bind()) will work identically.
  • Service Provider Compatibility: Laravel’s Service Providers (e.g., register() and boot() methods) are compatible with league/container’s ServiceProviderInterface, though some Laravel-specific features (e.g., app()->singleton()) may need manual adjustments.
  • Context Binding: Laravel’s context binding (e.g., app()->when(X::class)->needs(Y::class)->give(Z::class)) is not natively supported in league/container. A custom event listener or delegate container would be needed to replicate this behavior.
  • Facade Integration: Laravel’s facades (e.g., Route::get()) rely on the container’s resolution logic. Since league/container uses the same PSR-11 interface, facades will continue to work, but custom resolution logic (e.g., afterResolve()) may require adjustments.

Technical Risk

Risk Area Assessment Mitigation
Context Binding Laravel’s context binding is not directly supported. Custom logic would be needed to replicate this functionality. Use Container::afterResolve() or event listeners to intercept and modify resolutions dynamically.
Service Provider Differences Laravel’s AppServiceProvider and BootServiceProvider may need adjustments for league/container’s stricter typing or missing methods (e.g., provides() is required in v5+). Abstract service provider logic into a base class or use traits to ensure compatibility.
Performance Overhead While league/container is lightweight, event listeners or custom resolution logic could introduce overhead. Profile performance and disable unused events (e.g., OnDefineEvent if not needed).
Attribute Resolution Laravel’s autowiring (via #[Inject]) is similar but not identical to league/container’s attribute resolution. Some edge cases (e.g., union types) may behave differently. Test thoroughly with complex dependency graphs and adjust autowiring configurations if needed.
Migration Complexity Swapping containers in a large codebase may require testing all resolved dependencies. Use feature flags or environment-based container switching to test incrementally.

Key Questions for TPM

  1. Is Laravel’s context binding critical to the application?
    • If yes, assess whether a custom event listener or delegate container can replicate the behavior.
  2. Are there existing service providers that rely on Laravel-specific container methods (e.g., app()->makeWith())?
    • If yes, evaluate the effort to refactor them for league/container.
  3. Does the application use advanced features like afterResolve() or attribute-based resolution?
    • If yes, ensure league/container’s implementation meets the same requirements.
  4. What is the PHP version and Laravel version in use?
    • league/container requires PHP 8.3+, so ensure compatibility with the target Laravel version (e.g., Laravel 11+).
  5. Are there performance bottlenecks in the current container?
    • If yes, benchmark league/container to justify the migration.

Integration Approach

Stack Fit

  • Laravel Compatibility: league/container is a drop-in replacement for Laravel’s container, as both implement PSR-11. This means:
    • Facades (e.g., Route, Auth) will continue to work without changes.
    • Service providers (e.g., AppServiceProvider, third-party packages) will work if they adhere to PSR-11.
    • Bindings (e.g., app()->bind(), app()->singleton()) are functionally identical.
  • Modern PHP Features: Supports PHP 8.3+, including:
    • Attribute-based resolution (#[Inject], #[Resolve]).
    • Union types (with explicit opt-out in Laravel’s autowiring).
    • Enums and named arguments.
  • Event-Driven Architecture: The event system (v5.2+) allows for:
    • Intercepting resolutions (BeforeResolveEvent).
    • Modifying resolved instances (afterResolve()).
    • Tag-based filtering (e.g., forTag('queue.worker')).

Migration Path

Step Action Tools/Notes
1. Assessment Audit dependencies for Laravel-specific container usage (e.g., app()->makeWith(), context binding). Use phpstan or psalm to detect non-PSR-11 container calls.
2. Dependency Update Update composer.json to replace illuminate/container with league/container. json { "require": { "league/container": "^5.2" } }
3. Service Provider Adjustments Refactor service providers to use PSR-11 methods (e.g., get() instead of make()). Ensure provides() is implemented if using league/container v5+. Laravel’s register() method can remain unchanged, but boot() may need adjustments if using container-specific logic.
4. Context Binding Workaround Implement a custom event listener or delegate container to replicate Laravel’s context binding. Example: Use Container::listen() to intercept BeforeResolveEvent and apply conditional logic.
5. Testing Test all dependency resolutions, facades, and service providers. Pay special attention to:
  • Autowiring (ensure #[Inject] works as expected).
  • Singleton/scoped bindings.
  • Custom resolvers. | Use Laravel’s phpunit with RefreshDatabase and WithFaker traits to test edge cases. | | 6. Performance Benchmarking | Compare memory usage and resolution speed between Laravel’s container and league/container. | Use blackfire.io or xdebug to profile critical paths. | | 7. Rollback Plan | Maintain a feature flag to switch between containers dynamically (e.g., via environment variable). | Example: php if (app()->bound('config')) { return app('config'); } with a fallback to the original container. |

Compatibility

Feature Laravel Container league/container Compatibility Notes
PSR-11 Compliance ✅ Yes ✅ Yes Fully interchangeable.
Service Providers ✅ Yes ✅ Yes Laravel’s register() and boot() work, but provides() may be required in v5+.
Context Binding ✅ Yes ❌ No Requires custom event listener or delegate container
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope