jankovacs/injector
Composer 安装命令:
composer require jankovacs/injector
包简介
Lightweight PHP dependency injector
README 文档
README
Injects the class dependencies based on constructor signature. Native types are not supported as dependencies (like int, array, string etc.) as into constructor one should pass just dependencies to other classes, not concrete data, e.g. 'locale' - that could be retrieved from a config class.
Singleton support is kept, but it is not recommended to use singletons.
Simple example for usage:
- You have to specify somewhere in your application a config file for mappings, like this below:
class InjectionMappings {
public function __construct(IInjector $injector)
{
$this->injector = $injector;
}
public function setMappings()
{
//------- will always create a new instance
$this->injector->map( TestModel::class );
//------- will always create a new instance of the mapped type
$this->injector->map( ITestModel::class )->toType( TestModelOne::class );
//------- will return with the mapped object
$this->injector->map( TestModelOne::class )->toObject( new TestModelOne() );
//------- will return with the singleton instance
$this->injector->map( TestModelTwo::class )->asSingleton();
//------- will return with the mapped class as singleton
$this->injector->map( ITestSingleton::class )->toSingleton( TestSingleton::class );
//------- example for unsing provider
//-------
//------- it is for having more complex mapping logic
//------- you can map eg. an Interface to different end classes as different implementations
//------- be carefull, a not proper use can lead to misbehavior of your application
//-------
//------- in this example the TestOne class will be injected to SomeController only
//------- in all other classes, where the ITest is required, the TestTwo will be injected
/** @var \injector\api\IInjectionProvider $providerMappings */
$providerMappings = $this->injector->map( ITest::class )->toProvider( );
$providerMappings->addUnique( 'SomeController' )->toSingleton( TestOne::class );
//$providerMappings->addExceptTo( 'SomeController' )->toSingleton( TestTwo::class );
$providerMappings->addToRest()->toSingleton( TestTwo::class );
}
}
- Then you need to instantiate the injector and the mapper classes and set the mappings ( setMappings() method in the example above, but it could be named to whatever you want )
// create injector
$injector = new \injector\impl\Injector();
// map you injection mapping class
$injector->map(InjectionMappings::class);
// get the intantiated mapper class from injector (it will be injected in this way)
$injectionMappings = $injector->getInstance(InjectionMappings::class);
// set the mappings
$injectionMappings->setMappings();
- This is how you can inject dependencies into your classes
class IndexController
{
public function __construct(ITestModel $testModel, ITestSingleton $testSingletion)
{
}
}
You don't need to rewrite your code by adding some injector specific stuff (like in the initial version of this injector), it is injecting the needed dependencies by the constructor signature.
jankovacs/injector 适用场景与选型建议
jankovacs/injector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jankovacs/injector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jankovacs/injector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-07