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

Service Provider Laravel Package

container-interop/service-provider

Experimental PSR draft to standardize PHP service providers (“bundles/modules”) for PSR-11 containers. Defines interfaces for registering factories and extensions so modules can share container definitions across frameworks. Not stable before 1.0; expect breaking changes.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation

    composer require container-interop/service-provider
    

    Add to composer.json under require (experimental, so ensure your team is aware).

  2. First Use Case: Wrapping a Non-Laravel Container

    • Useful when integrating third-party libraries with PSR-11 containers (e.g., Symfony’s DependencyInjection).
    • Example: Convert a Symfony ContainerInterface to a PSR-11 container for Laravel’s DI:
      use ContainerInterop\ServiceProvider\ServiceProviderInterface;
      use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainer;
      
      $symfonyContainer = new SymfonyContainer();
      $provider = new \ContainerInterop\ServiceProvider\SymfonyServiceProvider($symfonyContainer);
      $psr11Container = new \ContainerInterop\Container($provider);
      
  3. Where to Look First

    • Documentation: PSR-11 and Container Interop.
    • Source: Focus on SymfonyServiceProvider and LaravelServiceProvider in the package’s src/ directory.
    • Laravel Integration: Check Illuminate/Container for compatibility notes (e.g., binding methods).

Implementation Patterns

Workflows

  1. Bridging Containers

    • Use Case: Integrate a legacy Symfony app with Laravel’s DI.
    • Pattern:
      // In a Laravel service provider (e.g., AppServiceProvider)
      public function register()
      {
          $symfonyContainer = new SymfonyContainer();
          $provider = new \ContainerInterop\ServiceProvider\SymfonyServiceProvider($symfonyContainer);
          $this->app->bind('container.interop', function () use ($provider) {
              return new \ContainerInterop\Container($provider);
          });
      }
      
    • Resolution: Access via $app['container.interop']->get(MyService::class).
  2. Extending Laravel’s Container

    • Use Case: Add PSR-11 compatibility to Laravel’s Container.
    • Pattern:
      $laravelProvider = new \ContainerInterop\ServiceProvider\LaravelServiceProvider($this->app);
      $psr11Container = new \ContainerInterop\Container($laravelProvider);
      
    • Note: Useful for libraries expecting PSR-11 but running in Laravel.
  3. Service Provider Chaining

    • Use Case: Combine multiple providers (e.g., Symfony + custom).
    • Pattern:
      $container = new \ContainerInterop\Container();
      $container->addProvider(new SymfonyServiceProvider($symfonyContainer));
      $container->addProvider(new CustomServiceProvider());
      

Integration Tips

  • Laravel Facades: Avoid direct PSR-11 usage in facades; wrap calls in service classes.
  • Testing: Mock ServiceProviderInterface for unit tests:
    $mockProvider = $this->createMock(ServiceProviderInterface::class);
    $mockProvider->method('getService')->willReturn(new MyService());
    $container = new \ContainerInterop\Container($mockProvider);
    
  • Performance: Cache PSR-11 containers if instantiated repeatedly (e.g., in a singleton binding).

Gotchas and Tips

Pitfalls

  1. Experimental Status

    • Risk: API may change; avoid in production-critical paths.
    • Mitigation: Use behind feature flags or in non-core modules.
  2. Laravel-Specific Quirks

    • Contextual Bindings: Laravel’s when()/needs() bindings aren’t directly translatable to PSR-11.
      • Workaround: Use LaravelServiceProvider with caution; prefer explicit bindings.
    • Singleton Scope: PSR-11 containers may not preserve Laravel’s singleton scope by default.
      • Fix: Manually bind singletons in the LaravelServiceProvider.
  3. Circular Dependencies

    • Issue: PSR-11 containers may not handle Laravel’s circular dependency resolution.
    • Solution: Refactor to avoid circular references or use a hybrid approach (e.g., resolve via Laravel’s container when needed).
  4. Namespace Collisions

    • Problem: Third-party PSR-11 services might clash with Laravel’s autoloading.
    • Fix: Use fully qualified class names or aliases in bindings.

Debugging

  • Service Not Found: Verify the provider is registered and the service is bound in the underlying container (e.g., Symfony/Laravel).
    // Debug Laravel bindings
    $this->app->bindings;
    
  • Type Hints: Ensure PSR-11 services implement expected interfaces (e.g., Psr\Container\ContainerInterface).

Extension Points

  1. Custom Providers
    • Implement ServiceProviderInterface for non-Symfony/Laravel containers:
      class MyCustomProvider implements ServiceProviderInterface {
          public function getService($id) {
              return new $id(); // Simplified example
          }
      }
      
  2. Middleware Integration
    • Use PSR-15 middleware with Laravel via PSR-11:
      $container->get(Psr15MiddlewareDispatcher::class)->dispatch(
          $request,
          $response
      );
      
  3. Event Dispatching
    • Bridge Laravel events to PSR-11 containers by binding an event dispatcher:
      $container->set(
          Symfony\Contracts\EventDispatcher\EventDispatcherInterface::class,
          $this->app->make(SymfonyEventDispatcher::class)
      );
      
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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity