algoyounes/bindify

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

v1.2.2 2025-04-12 17:07 UTC

This package is not auto-updated.

Last update: 2025-04-18 12:11:18 UTC


README

Bindify Logo
Bindify

Build Status Total Downloads Latest Stable Version License

Bindify is a Laravel package that provides attributes to bind interfaces to their implementations lazily.

Note

This package requires PHP 8.2+

Features ✨

  • Declarative bindings using #[BindWith] attributes
  • Lazy registration (bindings are registered when first resolved)
  • Seamless integration with Laravel's service container
  • Multiple binding types (Singleton, Transient)
  • Tagged bindings

Installation

You can install the package via composer:

composer require algoyounes/bindify

Usage

Basic Binding

Define your interface with the #[BindWith] attribute:

namespace App\Contracts;

use AlgoYounes\Bindify\Attributes\BindWith;
use AlgoYounes\Bindify\Attributes\BindType;
use App\Services\DefaultService;

#[BindWith(DefaultService::class, BindType::Singleton)]
interface ServiceContract
{
    public function execute();
}

Create your implementation:

namespace App\Services;

use App\Contracts\ServiceContract;

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

Binding Types

Type Description
BindType::Singleton Shares the same instance everywhere
BindType::Transient Creates a new instance each time

Advanced Binding

Multiple Implementations

Bind multiple implementations to an interface:

namespace App\Contracts;

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

#[BindWith([DefaultService::class, AlternativeService::class], BindType::Singleton)]
interface ServiceContract
{
    // ...
}

Tagged Bindings

Explicitly tag your bindings:

#[BindWith([DefaultService::class], BindType::Singleton, tag: 'primary')]

Note

When no tag is provided and there are multiple services, will auto-generates a tag by appending <class_name>_tag

Retrieving Bindings

Resolve your bindings as usual through container:

$service = app(ServiceContract::class);

$services = app()->tagged('primary');

Contributing

Thank you for considering contributing to this package! Please check the CONTRIBUTING file for more details.

License

This package is open-sourced software licensed under the MIT license.