PHP Classes

How to Implement Laravel Bind Singleton to Simplify the Creation of Objects that Implement Interfaces Using the Package Laravel Rapid Bind: Create singletons to implement interfaces

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-09-25 (2 days ago) RSS 2.0 feedNot enough user ratingsTotal: 6 This week: 6All time: 11,435 This week: 7Up
Version License PHP version Categories
laravel-rapid-bind 1.0The PHP License8.1Libraries, Language, Design Patterns, P...
Description 

Author

This package can create singletons to implement interfaces.

Applications can define interfaces and add annotation comments to determine the class to implement the respective interfaces.

The package provides a rapid binding class to create a singleton class that implements each interface using the respective binding class.

Picture of DeGraciaMathieu
  Performance   Level  
Name: DeGraciaMathieu <contact>
Classes: 20 packages by
Country: France France
Innovation award
Innovation award
Nominee: 14x

Winner: 2x

Documentation

<p align="center">

<img style='height: 350px' src='https://github.com/DeGraciaMathieu/laravel-rapid-bind/assets/11473997/17e2a556-e081-4889-8ae4-30f074e41686'>

</p>

laravel-rapid-bind

testing

In an architecture based on ports and adapters, we create many bindings between interfaces and implementations.

Often, these bindings are simple and require no additional configuration.

This package facilitates the creation of these bindings, helping you avoid cluttering your AppServiceProvider.

Installation

Requires >= PHP 8.1

composer require degraciamathieu/laravel-rapid-bind

Usage

Provide information on the folders containing the interfaces :

<?php

namespace App\Providers;

class AppServiceProvider extends ServiceProvider
{
    /
     * Register any application services.
     */
    public function register(): void
    {
        RapidBind::bind([
            '../app/Domain/Ports/Repositories',
        ]);
    }

    /
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        //
    }
}

Now, add the Bind annotation on the port with the value of the FQCN of the associated adapter :

<?php

namespace App\Domain\Ports\Repositories;

use DeGraciaMathieu\RapidBind\Bind;
use App\Infrastructure\Repositories\UserRepositoryAdapter;

#[Bind(UserRepositoryAdapter::class)]
interface UserRepository
{
    //
}
<?php

namespace App\Infrastructure\Repositories;

use App\Domain\Ports\Repositories\UserRepository;

class UserRepositoryAdapter implements UserRepository
{
    //
}

From now on, a singleton has been automatically created between the port and the adapter !

$userRepository = app(UserRepository::class);

dd($userRepository); // App\Infrastructure\Repositories\UserRepositoryAdapter


  Files folder image Files (14)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (2 files, 1 directory)
Files folder imagetests (1 directory)
Accessible without login Plain text file .phpunit.result.cache Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file testbench.yaml Data Auxiliary data

  Files folder image Files (14)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (14)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file testing.yml Data Auxiliary data

  Files folder image Files (14)  /  src  
File Role Description
Files folder imageFacades (1 file)
  Plain text file Bind.php Class Class source
  Plain text file RapidBind.php Class Class source

  Files folder image Files (14)  /  src  /  Facades  
File Role Description
  Plain text file RapidBind.php Class Class source

  Files folder image Files (14)  /  tests  
File Role Description
Files folder imageUnit (1 file, 1 directory)

  Files folder image Files (14)  /  tests  /  Unit  
File Role Description
Files folder imageStubs (3 directories)
  Plain text file RapidBindTest.php Class Class source

  Files folder image Files (14)  /  tests  /  Unit  /  Stubs  
File Role Description
Files folder imageAdapters (1 file)
Files folder imagePorts (1 file)
Files folder imageServices (1 file)

  Files folder image Files (14)  /  tests  /  Unit  /  Stubs  /  Adapters  
File Role Description
  Plain text file FooAdapter.php Class Class source

  Files folder image Files (14)  /  tests  /  Unit  /  Stubs  /  Ports  
File Role Description
  Plain text file Foo.php Class Class source

  Files folder image Files (14)  /  tests  /  Unit  /  Stubs  /  Services  
File Role Description
  Plain text file Foo.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:6
This week:6
All time:11,435
This week:7Up