All Projects → michael-rubel → laravel-auto-binder

michael-rubel / laravel-auto-binder

Licence: MIT license
Bind interfaces to concrete classes automatically

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-auto-binder

Reflex
Minimal dependency injection framework for Unity
Stars: ✭ 263 (+292.54%)
Mutual labels:  container, ioc-container
Container
A lightweight yet powerful IoC container for Go projects
Stars: ✭ 160 (+138.81%)
Mutual labels:  container, ioc-container
di
🐑 A flexible dependency injection container; It is an implementation of PSR-11
Stars: ✭ 20 (-70.15%)
Mutual labels:  container, ioc-container
PainlessDocker
Painless Docker book git repository.
Stars: ✭ 17 (-74.63%)
Mutual labels:  container
docker-cassandra-k8s
Cassandra Docker optimized for Kubernetes
Stars: ✭ 13 (-80.6%)
Mutual labels:  container
shorty
URL shortener available as library, microservice (even containerized), aws lambda, and azure function
Stars: ✭ 31 (-53.73%)
Mutual labels:  container
docker-routeros
Mikrotik RouterOS inside Docker container
Stars: ✭ 225 (+235.82%)
Mutual labels:  container
drupal-container
Drupal container for running Drupal sites with Docker, built with Ansible.
Stars: ✭ 37 (-44.78%)
Mutual labels:  container
search-api
Search API for GOV.UK
Stars: ✭ 21 (-68.66%)
Mutual labels:  container
DrawContainerDemo
作业盒子批注控件反编译
Stars: ✭ 17 (-74.63%)
Mutual labels:  container
undock
Extract contents of a container image in a local folder
Stars: ✭ 119 (+77.61%)
Mutual labels:  container
docker-nfs4
Simple containerized NFS v4 server running on Alpine Linux
Stars: ✭ 19 (-71.64%)
Mutual labels:  container
microcore
.NET Core framework for inter-service communication
Stars: ✭ 24 (-64.18%)
Mutual labels:  container
tableau-server-docker
Dockerfile for Tableau Server on Linux - Single Node
Stars: ✭ 111 (+65.67%)
Mutual labels:  container
yoda
Simple tool to dockerize and manage deployment of your project
Stars: ✭ 69 (+2.99%)
Mutual labels:  container
p3y
A single binary reverse proxy written in go. It was developed for use in Kubernetes, to wrap services like Prometheus with simple BasicAuth and TLS encryption.
Stars: ✭ 15 (-77.61%)
Mutual labels:  container
spacetech-kubesat
IBM Space Tech - Cognitive Autonomous Framework
Stars: ✭ 75 (+11.94%)
Mutual labels:  container
solr-container
Ansible Container project that manages the lifecycle of Apache Solr on Docker.
Stars: ✭ 17 (-74.63%)
Mutual labels:  container
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (-67.16%)
Mutual labels:  ioc-container
Container
🚀 PHP Service Container with fast and cachable dependency injection.
Stars: ✭ 28 (-58.21%)
Mutual labels:  container

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.x and Laravel 9.x

#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

Note that for the command to work you should use the folder name you passed to the AutoBinder instance in the from method. The cache will be ignored on local environment starting from v5.0.2

Testing

composer test

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].