algoyounes/bindify
最新稳定版本:v1.2.3
Composer 安装命令:
composer require algoyounes/bindify
包简介
Laravel Package Bindify is a package that helps you to bind your classes to the Laravel service container
README 文档
README
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.
统计信息
- 总下载量: 345
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-05
