michael-rubel/
laravel-enhanced-container
g4t.io

Advanced Container Package for Laravel

20 views 88 stars by Paul Redmond

This package provides DX tweaks for Service Container in Laravel.

README from michael-rubel/laravel-enhanced-container Open on GitHub →

Method binding with Service Container   Call Proxy

Laravel Enhanced Container

Latest Version on Packagist Total Downloads Code Quality Code Coverage GitHub Tests Action Status PHPStan

This package provides additional features for Laravel's Service Container.

#StandWithUkraine

SWUbanner

Contents

Installation

Install the package via composer (Laravel 11):

composer require michael-rubel/laravel-enhanced-container

If you need the package for older versions of Laravel, check changelog.

Usage

Resolve contextual binding outside of constructor

call(ServiceInterface::class, context: static::class);

// The `call` method automatically resolves the implementation from the interface you passed.
// If you pass the context, the proxy tries to resolve contextual binding instead of global binding first.

🔝 back to contents

Method binding

This feature makes it possible to override the behavior of methods accessed using the call.

Assuming that is your function in the service class:

class Service
{
    public function yourMethod(int $count): int
    {
        return $count;
    }
}

Bind the service to an interface:

$this->app->bind(ServiceInterface::class, Service::class);

Call your service method through container:

call(ServiceInterface::class)->yourMethod(100);

For example in feature tests:

$this->app->bind(ApiGatewayContract::class, InternalApiGateway::class);

bind(ApiGatewayContract::class)->method('performRequest', function ($service, $app, $params) {
    // Note: you can access `$params` passed to the method call.

    return true;
});

$apiGateway = call(ApiGatewayContract::class);
$request = $apiGateway->performRequest();
$this->assertTrue($request);

Note: if you rely on interfaces, the proxy will automatically resolve bound implementation for you.

Note for package creators

If you want to use method binding in your own package, you need to make sure the LecServiceProvider registered before you use this feature.

$this->app->register(LecServiceProvider::class);

🔝 back to contents

Method forwarding

This feature automatically forwards the method if it doesn't exist in your class to the second one defined in the forwarding configuration.

You can define forwarding in your ServiceProvider:

use MichaelRubel\EnhancedContainer\Core\Forwarding;

Forwarding::enable()
    ->from(Service::class)
    ->to(Repository::class);

You can as well use chained forwarding:

Forwarding::enable()
    ->from(Service::class)
    ->to(Repository::class)
    ->from(Repository::class)
    ->to(Model::class);

Important notes

  • Pay attention to which internal instance you're now working on in CallProxy when using forwarding. The instance may change without your awareness. If you interact with the same methods/properties on a different instance, the InstanceInteractionException will be thrown.
  • If you use PHPStan/Larastan you'll need to add the @method docblock to the service to make it static-analyzable, otherwise it will return an error that the method doesn't exist in the class.

🔝 back to contents

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.

More from the ecosystem

durable-workflow/
workflow
g4t.io
9 1,292

Durable Workflow: Long-Running Workflows for Laravel

Embedded durable workflow runtime and orchestration engine for Laravel applications.

durable-workflow/workflow via Laravel News
packstub/
session-replay
g4t.io
14 52

Session Replay for Laravel: Self-Hosted Browser Recordings

Session replay for Laravel: record what a person did in the browser with rrweb, keep the recording on your own disk and database, and watch it behind a gate you define — masked inputs by default, errors, failed Livewire requests and web vitals marked on the timeline, a link to the replay in every log line. packstub/filament-session-replay adds the sessions to a Filament panel.

packstub/session-replay via Laravel News
cyberwizard-dev/
schedule-catchup
g4t.io
17

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Schedule Catchup replays missed Laravel scheduled tasks after downtime, with opt-in recovery, bounded retries, and access to the original scheduled date.

cyberwizard-dev/schedule-catchup via Laravel News