michael-rubel/
laravel-auto-binder
g4t.io

Laravel Auto Binder: Bind Interfaces to Implementations Automatically

21 views 75 stars by Paul Redmond

This package adds the possibility to bind interfaces to implementations in the Service Container by scanning the specified project folders. This helps avoid manually registering container bindings when the project needs to bind a lot of interfaces to its implementations.

README from michael-rubel/laravel-auto-binder Open on GitHub →

Automated container bindings

Laravel Auto-Binder

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

This package adds the possibility to bind interfaces to concrete classes in the Service Container by scanning the specified project folders. This helps avoid manually registering container bindings when the project needs to bind a lot of interfaces to its implementations.


The package requires PHP 8 or higher and Laravel 9 or higher.

#StandWithUkraine

SWUbanner

Installation

Install the package using composer:

composer require michael-rubel/laravel-auto-binder

Usage

Define in your ServiceProvider:

AutoBinder::from(folder: 'Services')
    ->as('singleton')
    ->bind();

Assuming you have your services in the App\Services and its interfaces in the App\Services\Interfaces, the package will register binding for each pair of class and interface:

$this->app->singleton(AuthServiceInterface::class, AuthService::class);
$this->app->singleton(UserServiceInterface::class, UserService::class);
$this->app->singleton(CompanyServiceInterface::class, CompanyService::class);
...

Customization

If you need to customize the base path or namespace, you can use following methods:

AutoBinder::from(folder: 'Services')
    ->basePath('app/Domain')
    ->classNamespace('App\\Domain')
    ->interfaceNamespace('App\\Domain\\Interfaces')
    ->bind();

This configuration would look for classes in the app/Domain/Services folder, use App\\Domain namespace and apply interfaces from App\\Domain\\Interfaces namespace with ClassNameInterface naming convention.

If you need to change the naming convention of your interfaces, you can specify the namespace and name you prefer:

AutoBinder::from(folder: 'Services')
    ->interfaceNaming('Contract')
    ->bind();

This configuration scans the app/Services folder with App\\Services namespace, App\\Services\\Contracts interface namespace and ClassNameContract interface naming convention.

Excluding subfolders from scan

You might as well exclude subdirectories from the scan of the root directory:

AutoBinder::from(folder: 'Services')
    ->exclude('Traits', 'Components')
    ->bind();

Dependency injection

If you want to inject dependencies to your services while scanning, you can use when method:

AutoBinder::from(folder: 'Services')
    ->when(ExampleServiceInterface::class, function ($app, $service) {
        return new ExampleService($app);
    })
    ->bind();

Passing a concrete class as well as an interface is possible, but keep in mind interfaces have a higher priority when applying dependencies.

Scanning multiple folders at once

If you pass multiple folders, the from method will return an instance of Illuminate/Support/Collection. Assuming that, you can loop over your AutoBinder class instances with access to internal properties.

For example:

AutoBinder::from('Services', 'Models')->each(
    fn ($binder) => $binder->basePath('app')
        ->classNamespace('App\\Domain')
        ->interfaceNamespace("App\\Domain\\$binder->classFolder\\Interfaces")
        ->as('singleton')
        ->bind()
);

Caching

The package will cache your bindings to avoid redundant folder scans during the application bootstrapping process.

If you want cache to be disabled, you may use withoutCaching method on the AutoBinder instance, e.g.:

AutoBinder::from(folder: 'Services')
    ->withoutCaching()
    ->as('singleton')
    ->bind();

You can as well clear the cache for specific folder by using an Artisan command:

php artisan binder:clear Services,Models

Note that for the command to work you should use the folder name you passed to the AutoBinder instance in the from method. Also, the cache will be ignored in the local environment.

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