Bind your interfaces to implementations automatically.

Overview

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
You might also like...
Automatically retry non-atomic upsert operation when unique key constraints are violated.

Laravel Retry on Duplicate Key Automatically retry non-atomic upsert operation when unique constraints are violated. e.g. firstOrCreate() updateOrCrea

Automatically delete old SiteTree page versions from Silverstripe

Version truncator for Silverstripe An extension for Silverstripe to automatically delete old versioned DataObject records from your database when a re

Automatically load the next page of products in Magento. Easy to install and configure, this module works 100% out of the box with vanilla Magento 1.9.x and earlier.

Automatically load the next page of products in Magento. Easy to install and configure, this module works 100% out of the box with vanilla Magento 1.9.x and earlier.

Magento 2 module to automatically flush the cache whenever you save something in the System Configuration

Yireo AutoFlushCache Magento 2 module to automatically flush the cache whenever you save something in the System Configuration. Do NOT use this in pro

Composer Plugin for automatically including files for easing function usage in php.

Php Inc Php inc is a composer plugin for automatically including certain files into composer's autoload and autoload-dev files config. Given a set of

A tool to automatically fix PHP Coding Standards issues
A tool to automatically fix PHP Coding Standards issues

PHP Coding Standards Fixer The PHP Coding Standards Fixer (PHP CS Fixer) tool fixes your code to follow standards; whether you want to follow PHP codi

PHP package to make your objects strict and throw exception when you try to access or set some undefined property in your objects.

📢 Yell PHP package to make your objects strict and throw exception when you try to access or set some undefined property in your objects. Requirement

Import your Foursquare/Swarm checkins to your Micropub-enabled website

Swarm Checkins Import Import your Foursquare/Swarm checkins to your Micropub-enabled website Installation You'll need PHP and Composer to install this

Comments
  • Fluent configuration

    Fluent configuration

    By using objects in the ServiceProvider, we will be able to define automatic bindings for folders using fluent methods rather than using a messy configuration file.

    We can now do this:

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

    We are no longer constrained to subfolders of implementation classes, and you will be able to place your interfaces wherever you like.

    AutoBinder::from(folder: 'Services')
        ->basePath('app')
        ->classNamespace('App')
        ->interfaceNamespace('Interfaces')
        ->as('singleton')
        ->bind();
    

    If the interface namespace is not defined explicitly, the package will guess the interface folder based on the class folder and class namespace. For example:

    AutoBinder::from(folder: 'Services')
        ->basePath('app/Domain')
        ->classNamespace('App\\Domain\\Services')
        ->interfaceNamespace('App\\Domain\\Services\\Interfaces')
        ->as('singleton')
        ->bind();
    
    opened by michael-rubel 4
  • Allow Binder usage in provider's `register` method

    Allow Binder usage in provider's `register` method

    About

    This PR adds a possibility of AutoBinder usage in the provider's registering process (register method), removing the requirement to use it only in the boot method. We make sure Laravel's cache binding is available during the package's bootstrapping process.

    public function register(): void
    {
        AutoBinder::from(folder: 'Services')
            ->as('singleton')
            ->bind();
    }
    
    enhancement 
    opened by michael-rubel 0
  • Caching

    Caching

    About

    To avoid scanning folders for each application boot, we now use caching. The cache driver is abstract and would be determined by your application's configuration. Use can use a filesystem, redis, memcached, or any other driver you would like.

    CACHE_DRIVER=redis
    

    The binder will cache your bindings by default, but if in some cases you would prefer to disable the caching, you can use:

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

    You as well able to clear cached bindings:

    php artisan binder:clear Services
    
    enhancement 
    opened by michael-rubel 0
  • Use `Laravel Pint` instead of `PHP_CodeSniffer`

    Use `Laravel Pint` instead of `PHP_CodeSniffer`

    Logo Laravel Pint

    Introduction

    Laravel Pint is an opinionated PHP code style fixer for minimalists. Pint is built on top of PHP-CS-Fixer and makes it simple to ensure that your code style stays clean and consistent.

    enhancement 
    opened by michael-rubel 0
Releases(5.1.3)
  • 5.1.3(Dec 10, 2022)

    What's Changed

    • PHP 8.2 build 🐘 by @michael-rubel in https://github.com/michael-rubel/laravel-auto-binder/pull/11

    Full Changelog: https://github.com/michael-rubel/laravel-auto-binder/compare/5.1.2...5.1.3

    Source code(tar.gz)
    Source code(zip)
  • 5.1.2(Nov 17, 2022)

    What's Changed

    • Replace string cast with toString by @michael-rubel in https://github.com/michael-rubel/laravel-auto-binder/pull/8
    • Introduce mutation testing by @michael-rubel in https://github.com/michael-rubel/laravel-auto-binder/pull/9
    • Add providesCache 🔨 by @michael-rubel in https://github.com/michael-rubel/laravel-auto-binder/pull/10

    Full Changelog: https://github.com/michael-rubel/laravel-auto-binder/compare/5.1.1...5.1.2

    Source code(tar.gz)
    Source code(zip)
  • 5.1.1(Aug 9, 2022)

    What's Changed

    • Fix Redis issue when using register ServiceProvider's method by @michael-rubel in https://github.com/michael-rubel/laravel-auto-binder/pull/7

    Full Changelog: https://github.com/michael-rubel/laravel-auto-binder/compare/5.1.0...5.1.1

    Source code(tar.gz)
    Source code(zip)
  • 5.1.0(Aug 6, 2022)

    What's Changed

    • Allow Binder usage in provider's register method by @michael-rubel in https://github.com/michael-rubel/laravel-auto-binder/pull/6

    Full Changelog: https://github.com/michael-rubel/laravel-auto-binder/compare/5.0.2...5.1.0

    Source code(tar.gz)
    Source code(zip)
  • 5.0.2(Aug 1, 2022)

    What's Changed

    • Bump Laravel Pint version by @michael-rubel in https://github.com/michael-rubel/laravel-auto-binder/pull/5
    • Ignore cache when env is local by @michael-rubel in https://github.com/michael-rubel/laravel-auto-binder/pull/4

    Full Changelog: https://github.com/michael-rubel/laravel-auto-binder/compare/5.0.1...5.0.2

    Source code(tar.gz)
    Source code(zip)
  • 5.0.1(Jul 11, 2022)

  • 5.0.0(Jul 11, 2022)

    What's Changed

    • Use Laravel Pint instead of PHP_CodeSniffer by @michael-rubel in https://github.com/michael-rubel/laravel-auto-binder/pull/2
    • Caching by @michael-rubel in https://github.com/michael-rubel/laravel-auto-binder/pull/3

    Full Changelog: https://github.com/michael-rubel/laravel-auto-binder/compare/4.1.0...5.0.0

    Source code(tar.gz)
    Source code(zip)
  • 4.1.0(Jun 24, 2022)

    Fixed

    • Major issue with scanning subdirectories.

    Full Changelog: https://github.com/michael-rubel/laravel-auto-binder/compare/4.0.0...4.1.0

    Source code(tar.gz)
    Source code(zip)
  • 4.0.0(Jun 23, 2022)

    What's Changed

    • [4.x] Fluent configuration by @michael-rubel in https://github.com/michael-rubel/laravel-auto-binder/pull/1

    New way of AutoBinder configuration

    • No limits on the project structure
    • Dependency injection on the fly while scanning

    Example:

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

    Full Changelog: https://github.com/michael-rubel/laravel-auto-binder/compare/v3.0.0...4.0.0

    Source code(tar.gz)
    Source code(zip)
Owner
Michael Rubel
Laravel Certified Engineer
Michael Rubel
The SensioLabs DeprecationDetector runs a static code analysis against your project's source code to find usages of deprecated methods, classes and interfaces

SensioLabs DeprecationDetector CAUTION: This package is abandoned and will no longer receive any updates. The SensioLabs DeprecationDetector runs a st

QOSSMIC GmbH 389 Nov 24, 2022
Demonstration of OOP concepts and usage of Abstract class & Interfaces

Learn OOP Demonstration of OOP concepts and usage of Abstract class & Interfaces Usage clone this repo run composer install run php index.php Code str

M N Islam Shihan 3 Sep 14, 2021
bin/magento command to display configured preferences for classes or interfaces

bin/magento command to display configured preferences for classes or interfaces A bin/magento command that will show you the configured preferences fo

David Manners 14 Jul 18, 2022
This library can be used, among other things, to retrieve the classes, interfaces, traits, enums, functions and constants declared in a file

marijnvanwezel/reflection-file Library that allows reflection of files. This library can be used, among other things, to retrieve the classes, interfa

Marijn van Wezel 5 Apr 17, 2022
Provides simple interfaces to implement a webhook-based tweeting system

webhook-tweeter This package aims to provide simple interfaces to implement a webhook-based tweeting system. This can, for example, be used to tweet a

Ricardo Boss 2 May 7, 2022
PHPStan extension for sealed classes and interfaces.

Sealed classes with PHPStan This extension adds support for sealed classes and interfaces to PHPStan. Installation To use this extension, require it v

Jiří Pudil 14 Nov 28, 2022
Um repositório com classes, interfaces para padronizar os projetos de PHP da empresa

php-utils PHP Utilities for Laravel/Lumen Installation cd /path/to/your/project composer require logcomex/php-utils Utilities Packages Contracts Excep

LogComex 1 Oct 10, 2022
YL MVC Structure (PHP MVC) is a pattern made in PHP used to implement user interfaces, data, and controlling logic.

YL MVC Structure (PHP MVC) is a pattern made in PHP used to implement user interfaces, data, and controlling logic. It is built based on the combination of ideas from the Yii framework and Laravel framework (yl).

Tan Nguyen 3 Jan 3, 2023
A tool to automatically fix Twig Coding Standards issues

Twig CS Fixer Installation This standard can be installed with the Composer dependency manager. Add the coding standard as a dependency of your projec

Vincent Langlet 50 Jan 6, 2023
Helper to automatically load various Kirby extensions in a plugin

Autoloader for Kirby Helper to automatically load various Kirby extensions in a plugin Commerical Usage This package is free but if you use it in a co

Bruno Meilick 13 Nov 9, 2022