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

wp-starter/container

Lightweight dependency injection container for PHP. Provides simple service binding and resolution to help structure applications and manage dependencies cleanly, suitable for small projects and framework-agnostic setups.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require wp-starter/container
    

    Add the service provider to config/app.php under providers:

    WpStarter\Container\ContainerServiceProvider::class,
    
  2. Basic Usage Register a container binding in a service provider (e.g., AppServiceProvider):

    use WpStarter\Container\Container;
    
    public function register()
    {
        Container::bind('my.service', function () {
            return new MyService();
        });
    }
    
  3. First Use Case Resolve the binding in a controller or anywhere else:

    $service = Container::resolve('my.service');
    

Implementation Patterns

Dependency Injection Workflow

  • Explicit Binding: Prefer explicit bindings for clarity:
    Container::bind('App\Contracts\MyInterface', 'App\Services\MyService');
    
  • Singleton vs. Transient:
    Container::singleton('cache', Cache::class); // Singleton
    Container::transient('logger', Logger::class); // New instance per resolve
    

Integration with Laravel

  • Replace Laravel’s Container (Advanced) Override Laravel’s container in AppServiceProvider:

    $this->app->singleton('container', function () {
        return new WpStarter\Container\Container();
    });
    

    (Use cautiously—test thoroughly!)

  • Contextual Bindings Bind services dynamically based on context (e.g., request):

    Container::bindWhen('user.repository', function ($app) {
        return new UserRepository($app['request']->user());
    }, function ($needs) {
        return isset($needs['user']);
    });
    

WP Integration (If Applicable)

  • WordPress Hooks as Bindings Register WP services as container bindings:
    Container::bind('wp.db', function () {
        global $wpdb;
        return $wpdb;
    });
    

Gotchas and Tips

Pitfalls

  • Circular Dependencies The container throws RuntimeException on circular dependencies. Refactor to break cycles.
  • Missing Bindings Container::resolve('unbound.service') throws BindingResolutionException. Use Container::bound() to check:
    if (Container::bound('service')) { ... }
    

Debugging

  • Inspect Bindings Dump all bindings for debugging:
    dd(Container::getBindings());
    
  • Override Bindings Carefully Use Container::rebind() instead of bind() to replace existing bindings:
    Container::rebind('service', NewService::class);
    

Extension Points

  • Custom Resolvers Extend the container’s resolver logic by implementing WpStarter\Container\Contracts\Resolver:
    Container::extend('custom.resolver', function ($concrete) {
        return new CustomResolver($concrete);
    });
    
  • Contextual Binding Logic Use bindIf() to conditionally register bindings:
    Container::bindIf('debug.logger', DebugLogger::class, function () {
        return app()->environment('local');
    });
    

Configuration Quirks

  • No Built-in Config The package is lightweight—configure bindings explicitly in providers.
  • Thread Safety The container is not thread-safe by default. Use Container::make() in queues with caution.
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