oukhennicheabdelkrim/dic
Composer 安装命令:
composer require oukhennicheabdelkrim/dic
包简介
DIC is dependency-injection container allows developers to move object creation logic out of their application logic.
README 文档
README
DIC is a simple dependency-injection container for php, allows developers to move object-creation logic out of their application logic.
This package is compatible with PSR-1, PSR-4 and PSR-11, if you notice compliance oversights, please send me a patch via pull request.
Requirements
The following versions of PHP are supported in this version:
- PHP 7.0
- PHP 7.1
- PHP 7.2
Testing
$ vendor/bin/phpunit
Get started
Install the package via composer:
composer require oukhennicheabdelkrim/dic
Simple usage
To create a container, simply create an instance of the DIC class.
<?php
include 'vendor/autoload.php';
use oukhennicheabdelkrim\DIC\DIC;
$container = new DIC();
Binding
You can bind any object with a key using bind method ,then you can retrieve instances by reffering them with their key using get method.
<?php
$container = new DIC();
//Bind myFoo to the Foo instance
$container->bind('myFoo',function(){
return new Foo();
});
$myFoo = $container->get('myFoo'); // Foo instance
Note : bind method returns the current container.
get method creates a singleton instance by default, this means everytime you request a dependency it returns the same instance.
$container = new DIC();
$container->bind('myFoo',function(){
return new Foo();
});
var_dump( $container->get('myFoo') === $container->get('myFoo')); // true
Within any of your resolve callable, you always have access to the $container property which provides access to the current container:
$container = new DIC();
$container->bind('config.db',function(){
return new DbConfig();
});
$container->bind('dbConnection',function($container){
return new DbConnection($container->get('config.db'));
});
//Get a singleton DbConnection instance
$dbConnection=$container->get('dbConnection');
You can also directly inject instance to bind it:
$container->bind('myFoo',new Foo());
Get a new instance
You can get a new instance using getFactory method
$container = new DIC();
$container->bind('myFoo',function(){
return new Foo();
});
$container->get('myFoo') === $container->getFactory('myFoo') // false
$container->getFactory('myFoo') === $container->getFactory('myFoo') // false
Resolving instance automatically
DIC can resolve any instantiable class without bind method, using the real class:name as an argument in get and getFactory methods
Example 1
/* Foo class */
class Foo{
public $i;
public function __construct($i = 44){
$this->i=$i;
}
}
/* Bar class */
class Bar{
public $foo;
public function __construct(Foo $foo){
$this->foo=$foo;
}
}
$container = new DIC();
$bar=$container->get('Bar');
var_dump($bar->foo->i); // 44
// get method always returns a singleton instance.
var_dump($bar->foo === $container->get('Foo')); // true
//getFactory method always returns a new instance.
var_dump($bar->foo === $container->getFactory('Foo')); // false
Exmaple 2
$container = new DIC();
$container->bind('bar1',function($container){
// resolve Foo using the class name
return new Bar($container->get('Foo'));
});
$bar1 = $container->get('bar1');// a singleton bar1 (Bar instance)
$bar = $container->get('Bar'); // a singleton Bar
var_dump($bar1 === $bar); // false
var_dump($bar1->foo === $bar->foo) ; // true
With DIC you can also bind any variable :
$container = new DIC();
$container->bind('a',5);
var_dump($container->get('a')); // 5
$container->bind('db.config',function(){
$config = new Config(); // We can Also use $container->get('Config')
return $config->getArray('db'); // Array of database configuration
})->bind('dbConnexion',function($container){
return new DbConnexion($container->get('db.config'));
});
var_dump($container->get('db.config')); // Array of database configuration
var_dump($container->get('dbConnexion')); // singleton dbConnexion instance
Note : Since bind method return the current container, you can chain the binding process.
Example:
$myBar = $container->bind('myFoo',new Foo())
->bind('myBar',new Bar($container->get('myFoo'))
->get('myBar');
has method
has method returns true if the container can return an entry for the given identifier, returns false otherwise.
/* A class */
class A{
}
$container = new DIC();
var_dump($container->has('A')) ;// true
var_dump($container->has('b')); // false
var_dump($container->bind('b',new A())->has('b'));// true
oukhennicheabdelkrim/dic 适用场景与选型建议
oukhennicheabdelkrim/dic 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 05 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dependency injection」 「container」 「di」 「PSR-11」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 oukhennicheabdelkrim/dic 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 oukhennicheabdelkrim/dic 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 oukhennicheabdelkrim/dic 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
Dependency injection container for the Monolith framework.
The PSR-11 container bridges
Http Microframework for Hack
PHP Dependency Injection Container
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-26