baobd / laravel-auto-binder

Bind your interfaces to implementations automatically.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel Auto Binder

Laravel Auto Binder

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

This package automatically binds interfaces to implementations in the Service Container, 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 without any additional dependencies.

One requirement you should follow to use this package:

  • Folder with interfaces always should be the child of the folder where it belongs.

For example: App\Services\YourService => App\Services\Interfaces\YourServiceInterface

You can customize folders to scan, type of bindings, and the naming convention of your interfaces in the config.

The package requires PHP ^8.x and Laravel ^8.67.

PHP Version Laravel Version Laravel Octane Compatible

Installation

Install the package using composer:

composer require michael-rubel/laravel-auto-binder

Then publish and customize the configuration:

php artisan vendor:publish --tag="auto-binder-config"

Usage

Edit the config and put your classes and interfaces to the proper folders with proper class naming. That's all.

So, this kind of bindings:

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

Or provider's array:

public array $singletons = [
    AuthServiceInterface::class    => AuthService::class,
    UserServiceInterface::class    => UserService::class,
    CompanyServiceInterface::class => CompanyService::class,
    ...
];

Will be registered automatically for you.

Testing

composer test

About

Bind your interfaces to implementations automatically.

License:MIT License


Languages

Language:PHP 100.0%