algoyounes/bindify

Laravel Package Bindify is a package that helps you to bind your classes to the Laravel service container

v1.0.0 2024-12-05 17:03 UTC

This package is not auto-updated.

Last update: 2024-12-20 15:38:25 UTC


README

Bindify Logo
Bindify

Build Status Total Downloads Latest Stable Version License

Bindify is a Laravel package that provides a simple way to bind interfaces to their implementations using attributes.

Installation

You can install the package via composer:

composer require algoyounes/bindify

After installation, register the service provider in your config/app.php file:

'providers' => [
    // Other Service Providers
    AlgoYounes\Bindify\BindifyServiceProvider::class,
],

Usage

Supported Bind Types

  • BindType::Singleton: Keeps one instance and shares it everywhere.
  • BindType::Transient: Creates a new instance every time you use it.

Define your interface and implementation

  1. Use the #[BindWith] attribute to bind an interface to its implementation
namespace App\Contracts;

use AlgoYounes\Bindify\Attributes\BindWith;
use AlgoYounes\Bindify\Attributes\BindType;

#[BindWith(DefaultService::class, BindType::Singleton)]
interface ServiceContract
{
    public function execute();
}
  1. Create the implementation of the interface
namespace App\Services;

use App\Contracts\ServiceContract;

class DefaultService implements ServiceContract
{
    public function execute()
    {
        // Your implementation here
    }
}