yolo-fx/di
最新稳定版本:v2.0.0
Composer 安装命令:
composer require yolo-fx/di
包简介
A PHP dependency injection package.
README 文档
README
YOLO DI is a lightweight PHP dependency injection (DI) library designed to simplify the process of managing dependencies, resolving classes, and injecting properties. It provides a static facade for interacting with the DI container, making it easy to bind, resolve, and manage object lifecycles.
Features
- Dependency Resolution: Automatically resolve and instantiate classes with their dependencies.
- Class Binding: Bind interfaces or abstract classes to concrete implementations.
- Instance Management: Manually set or remove specific instances for classes.
- Alias Management: Create and manage aliases for classes.
- Custom Property Injection: Use attributes to inject custom dependencies into class properties.
Installation
To install YOLO DI, use Composer:
composer require yolo-fx/di
Usage
Basic Example
use Yolo\Di\DI; DI::bind(LoggerInterface::class, ConsoleLogger::class); $logger = DI::use(LoggerInterface::class); $logger->log("Hello, World!");
Using Aliases
DI::alias(TestRunner::class, 'runner'); $runner = DI::use('runner'); $runner->sayHello();
Custom Property Injection
Define a custom attribute:
#[Attribute] class Cache implements \Yolo\Di\PropertyAttributeInterface { public function __construct(private string $driver = 'default') {} public function inject(): mixed { return match ($this->driver) { 'file' => DI::use(FileLogger::class), default => DI::use(ConsoleLogger::class), }; } }
Use the attribute in a class:
class TestRunner { public function __construct( #[Cache('file')] private LoggerInterface $logger ) {} public function sayHello(): void { $this->logger->log("Hello, World!"); } }
Requirements
- PHP 8.0 or higher
License
This project is licensed under the MIT License. See the LICENSE file for details.
Author
- LUSHAOMING - lushaoming6@gmail.com
统计信息
- 总下载量: 55
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-15