LaravelPackages.net
Acme Inc.
Toggle sidebar
degraciamathieu/laravel-rapid-bind

Quickly and easily create singletons in the Laravel container with annotations

77
5
1.0.0
About degraciamathieu/laravel-rapid-bind

degraciamathieu/laravel-rapid-bind is a Laravel package for quickly and easily create singletons in the laravel container with annotations. It currently has 5 GitHub stars and 77 downloads on Packagist (latest version 1.0.0). Install it with composer require degraciamathieu/laravel-rapid-bind. Discover more Laravel packages by degraciamathieu or browse all Laravel packages to compare alternatives.

Last updated

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

Star History Chart