kode/di
Composer 安装命令:
composer require kode/di
包简介
高性能 PHP 8.1+ 依赖注入容器,支持属性注入、生命周期管理、协程上下文隔离,兼容 PSR-11
README 文档
README
高性能 PHP 8.1+ 依赖注入容器,支持属性注入、生命周期管理、协程上下文隔离,兼容 PSR-11。
特性
- PSR-11 兼容 - 实现标准容器接口
- 属性注入 - 基于 PHP 8.1+ Attributes 实现声明式注入
- 生命周期管理 - 单例/原型/懒加载/上下文隔离
- 协程安全 - 支持 Fiber/Swoole/Swow 上下文隔离
- 高性能 - 反射缓存 + 定义缓存
- 零全局状态 - 无全局变量污染
- 框架无关 - 可在任何 PHP 8.1+ 项目中使用
安装
composer require kode/di
快速开始
基本使用
use Kode\DI\Container; $container = new Container(); // 绑定单例 $container->singleton(LoggerInterface::class, FileLogger::class); // 绑定原型 $container->prototype(Request::class); // 获取实例 $logger = $container->get(LoggerInterface::class);
属性注入
use Kode\DI\Attributes\Inject; use Kode\DI\Attributes\Singleton; #[Singleton] class UserService { #[Inject] private LoggerInterface $logger; #[Inject(id: 'cache.ttl', required: false)] private int $cacheTtl = 3600; }
生命周期类型
| 类型 | 方法 | 说明 |
|---|---|---|
| 单例 | singleton() |
全局唯一实例 |
| 原型 | prototype() |
每次获取创建新实例 |
| 懒加载 | lazy() |
延迟实例化 |
| 上下文隔离 | contextual() |
协程/Fiber间隔离 |
上下文隔离
use Kode\DI\ContextualContainer; // 在协程环境中自动隔离实例 ContextualContainer::setContainer($container); // 每个协程拥有独立实例 ContextualContainer::resolve(DatabaseConnection::class);
服务提供者
use Kode\DI\ServiceProvider; class DatabaseServiceProvider extends ServiceProvider { public function register(): void { $this->singleton(DatabaseInterface::class, MySQLDatabase::class); } public function boot(): void { // 启动逻辑 } }
上下文绑定
// 当 UserController 需要 LoggerInterface 时,使用专门的实现 $container->when(UserController::class) ->needs(LoggerInterface::class) ->give(UserLogger::class); // 使用闭包 $container->when(OrderController::class) ->needs(LoggerInterface::class) ->give(fn($c) => new OrderLogger('order.log'));
标签
// 给服务打标签 $container->singleton(CacheInterface::class, RedisCache::class)->tag('cache'); $container->singleton(SessionInterface::class, RedisSession::class)->tag('cache'); // 获取所有带标签的服务 $cacheServices = $container->tagged('cache');
API 参考
Container
| 方法 | 说明 |
|---|---|
bind(id, concrete, lifecycle) |
绑定服务 |
singleton(id, concrete) |
绑定单例 |
prototype(id, concrete) |
绑定原型 |
lazy(id, concrete) |
绑定懒加载 |
contextual(id, concrete) |
绑定上下文隔离 |
instance(id, instance) |
绑定实例 |
alias(alias, id) |
设置别名 |
extend(id, callback) |
扩展服务 |
get(id) |
获取服务 |
has(id) |
检查服务是否存在 |
make(id, parameters) |
创建实例 |
call(callback, parameters) |
调用方法 |
resolved(id) |
检查是否已解析 |
forget(id) |
移除绑定 |
flush() |
清空容器 |
Attributes
| 属性 | 目标 | 说明 |
|---|---|---|
#[Inject] |
Property, Parameter | 标记注入点 |
#[Autowire] |
Class, Property, Method | 启用自动装配 |
#[Singleton] |
Class | 标记为单例 |
#[Prototype] |
Class | 标记为原型 |
#[Contextual] |
Class | 标记为上下文隔离 |
与其他 kode 组件集成
use Kode\DI\Container; use Kode\Attributes\Attr; use Kode\Context\Context; // 自动使用 kode/attributes 进行属性读取 // 可选使用 kode/context 进行协程上下文隔离
兼容性
| PHP 版本 | 支持状态 |
|---|---|
| PHP 8.1 | ✅ 完全支持 |
| PHP 8.2 | ✅ 完全支持 |
| PHP 8.3 | ✅ 完全支持 |
| PHP 8.4 | ✅ 完全支持 |
| PHP 8.5 | ✅ 完全支持 |
| 框架 | 兼容性 |
|---|---|
| Laravel | ✅ 完全兼容 |
| Symfony | ✅ 完全兼容 |
| ThinkPHP 8 | ✅ 完全兼容 |
| Webman | ✅ 完全兼容 |
| Hyperf | ✅ 完全兼容 |
| 原生 PHP | ✅ 完全兼容 |
测试
composer test
许可证
kode/di 适用场景与选型建议
kode/di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「container」 「di」 「ioc」 「attribute」 「dependency-injection」 「coroutine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kode/di 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kode/di 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kode/di 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A fast and intuitive dependency injection container.
Common IoC Api Interface
Powerful class discovery system for Laravel with attribute-based scanning, directory traversal, and monorepo support
Show attribute codes in product edit form
Dependency injection container for the Monolith framework.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 42
- 依赖项目数: 0
- 推荐数: 4
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2026-03-23