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
Requires
- php: ^8.2
Requires (Dev)
- laravel/framework: ^11
- laravel/pint: ^1.13.7
- orchestra/testbench: ^9.0
- pestphp/pest: ^2.28.1
- phpstan/phpstan: 1.10.56
- rector/rector: 0.19.5
Suggests
- illuminate/contracts: for the Laravel integration
- illuminate/support: for the Laravel integration
This package is not auto-updated.
Last update: 2024-12-20 15:38:25 UTC
README
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
- 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(); }
- Create the implementation of the interface
namespace App\Services; use App\Contracts\ServiceContract; class DefaultService implements ServiceContract { public function execute() { // Your implementation here } }