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 Implementations Laravel Package

psr-discovery/container-implementations

Auto-discovers an available PSR-11 container at runtime by checking for well-known implementations and returning the first match. Ideal for SDKs/libraries that want PSR-11 support without hard dependencies or user configuration.

View on GitHub
Deep Wiki
Context7

Getting Started

This package provides PSR-11 container implementations (e.g., PsrContainer) for Laravel applications, enabling dependency injection and service container interoperability. To get started:

  1. Installation: Require the package via Composer:
    composer require psr-discovery/container-implementations
    
  2. Leverage in Laravel: Use the PsrContainer facade or bind it to Laravel's container:
    use Psr\Container\ContainerInterface;
    use PsrDiscovery\ContainerImplementations\PsrContainer;
    
    // Bind the PSR-11 container to Laravel's container
    $this->app->bind(ContainerInterface::class, function ($app) {
        return new PsrContainer($app);
    });
    
  3. First Use Case: Resolve PSR-11 services or integrate with libraries requiring ContainerInterface:
    $container = app(ContainerInterface::class);
    $service = $container->get(MyService::class);
    

Implementation Patterns

Workflows

  1. Service Resolution:

    • Use the PSR-11 container to resolve services in a standardized way, especially when working with libraries that require ContainerInterface.
    • Example: Resolve a service bound to Laravel's container via the PSR-11 facade:
      $service = PsrContainer::get(MyService::class);
      
  2. Integration with PSR-15 Middleware:

    • Leverage the PSR-11 container to resolve PSR-15 middleware (e.g., Psr\Http\Message\ServerRequestHandlerInterface) in Laravel applications.
  3. Testing:

    • Mock or replace Laravel's container with a PSR-11 container in unit tests for better isolation:
      $container = new PsrContainer();
      $container->set(MyService::class, $mockService);
      

Tips for Laravel Integration

  • Leverage Laravel's Bindings: The PSR-11 container can resolve Laravel-bound services seamlessly. Bind services to Laravel's container first, then resolve them via the PSR-11 facade.
  • Dependency Injection: Use the PSR-11 container in constructors or methods requiring ContainerInterface:
    public function __construct(private ContainerInterface $container) {}
    
  • Service Providers: Bind the PSR-11 container in a service provider for global access:
    $this->app->singleton(ContainerInterface::class, function ($app) {
        return new PsrContainer($app);
    });
    

Gotchas and Tips

Breaking Changes (1.2.0)

  • PHP Version Requirement: This release bumps the minimum PHP version to 8.2. Ensure your project meets this requirement before upgrading.
  • Laravel Framework Version: Now supports Laravel ^11.0. If you're on Laravel 10.x, verify compatibility or pin the package version to ^1.1.1.

Debugging

  • Service Not Found: If resolving a service fails, ensure it is bound to both Laravel's container and the PSR-11 container. Use app()->bound(MyService::class) to check bindings.
  • Circular Dependencies: The PSR-11 container may throw exceptions for circular dependencies. Refactor bindings or use Laravel's built-in container for such cases.

Extension Points

  • Custom Container: Extend PsrContainer to add custom logic (e.g., automatic service registration):
    class CustomPsrContainer extends PsrContainer {
        public function __construct($laravelContainer) {
            parent::__construct($laravelContainer);
            $this->registerAutomaticBindings();
        }
    }
    
  • PSR-11 Middleware: Combine with psr/http-server-middleware to resolve middleware via the PSR-11 container.

Configuration Quirks

  • Laravel 11+: If upgrading to Laravel 11, test the PSR-11 container integration early, as Laravel 11 may introduce container-related changes.
  • Package Version Pinning: If you cannot upgrade PHP or Laravel, pin the package to ^1.1.1 in composer.json:
    "psr-discovery/container-implementations": "^1.1.1"
    
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.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
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