dotkernel/dot-dependency-injection
Composer 安装命令:
composer require dotkernel/dot-dependency-injection
包简介
Dotkernel dependency injection component using class attributes.
README 文档
README
dot-dependency-injection is Dotkernel's dependency injection service.
By providing reusable factories for service and repository injection, it reduces code complexity in projects.
Documentation
Documentation is available at: https://docs.dotkernel.org/dot-dependency-injection/.
Badges
Installation
Install dot-dependency-injection by running the following command in your project directory:
composer require dotkernel/dot-dependency-injection
After installing, register dot-dependency-injection in your project by adding the below line to your configuration aggregate (usually: config/config.php):
Dot\DependencyInjection\ConfigProvider::class,
Usage
Using the AttributedServiceFactory
You can register services in the service manager using AttributedServiceFactory as seen in the below example:
return [ 'factories' => [ ServiceClass::class => AttributedServiceFactory::class, ], ];
You can use only the fully qualified class name as the service key
The next step is to add the #[Inject] attribute to the service constructor with the service FQCNs to inject:
#[\Dot\DependencyInjection\Attribute\Inject( App\Srevice\Dependency1::class, App\Srevice\Dependency2::class, "config", )] public function __construct( protected App\Srevice\Dependency1 $dep1, protected App\Srevice\Dependency2 $dep2, protected array $config ) { }
The #[Inject] attribute is telling AttributedServiceFactory to inject the services specified as parameters.
Valid service names should be provided, as registered in the service manager.
To inject an array value from the service manager, you can use dot notation as below
#[\Dot\DependencyInjection\Attribute\Inject( "config.debug", )]
which will inject $container->get('config')['debug'];.
Even if using dot notation,
AttributedServiceFactorywill check first if a service name exists with that name.
Using the AttributedRepositoryFactory
You can register doctrine repositories and inject them using the AttributedRepositoryFactory as below:
return [ 'factories' => [ ExampleRepository::class => AttributedRepositoryFactory::class, ], ];
The next step is to add the #[Entity] attribute in the repository class.
The name field has to be the fully qualified class name.
Every repository should extend Doctrine\ORM\EntityRepository.
use Api\App\Entity\Example; use Doctrine\ORM\EntityRepository; use Dot\DependencyInjection\Attribute\Entity; #[Entity(name: Example::class)] class ExampleRepository extends EntityRepository { }
Dependencies injected via the
#[Entity]/#[Inject]attributes are not cached
Injecting dependencies into property setters is not supported
dotkernel/dot-dependency-injection 适用场景与选型建议
dotkernel/dot-dependency-injection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 39.79k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 06 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「container」 「dependency」 「di」 「service」 「attribute」 「factory」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dotkernel/dot-dependency-injection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dotkernel/dot-dependency-injection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dotkernel/dot-dependency-injection 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
Registry service.
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
Service auto install for Nette Framework
Structure for the Laravel Service-Repository Pattern
Dependency injection container for the Monolith framework.
统计信息
- 总下载量: 39.79k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 24
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-06-07