corviz/di-container
Composer 安装命令:
composer require corviz/di-container
包简介
A simple yet powerful container with auto wiring
README 文档
README
A simple yet powerful container with auto wiring
Installation
composer require corviz/di-container
Usage
Here are some common scenarios you will meet when using container + dependency injection
For these examples, we will assume the following classes:
class A { public funtion doSomething(){ /* ... */ } public function __construct() { echo "new instance of A"; } } class B { public function __construct(A $a) { echo "new instance of B"; } }
Retrieve a new instance from container
Note that no previous declaration was required!
use Corviz\DI\Container; $container = new Container(); $a = $container->get(A::class); $a->doSomething(); //It works! :)
Auto-wiring and parameter types
What if we need a new instance of B?
$b = $container->get(B::class); //Outputs: //new instance of A //new instance of B
Because we declared a parameter with type "A" in the constructor, and it is a class, our container will try to create a new instance automatically. This is done though Reflection.
If we needed to receive a primitive type as parameter, we have to declare a default value otherwise the container will throw a ContainerException, since it can't guess how to fill it.
Manual setup
You may want to set some objects manually in some particular cases. To do so, simply use 'set' method.
//Whenever this interface is met, the container will declare a class instance instead: $container->set(AInterface::class, A::class); //Declaring a constructor. This function will be called whenever 'B' class is met. //Note: a closure is REQUIRED, in this case $container->set(B::class, function (){ $param1 = new A(); return new B($param); }); //Defining an instance. Whenever this interface is met, the same object will be accessed $container->set(AInterface::class, new A());
Aliases
If we want to alias something, all we have to do is set it in our container as usual
$container->set('s3', new S3Client(/* ... */)); //... $s3 = $container->get('s3');
Singletons
When we want to access the same object through the entire execution, all we have to do is use 'setSingleton'. It accepts the same definition variants as 'set'. In other words, you may use the class name, a constructor closure or an object instance.
$container->setSingleton(Queue::class, function(){ $queue = new Queue(); //setup... return $queue; }); $queue = $container->get(Queue::class); $queue->add(/* ... */); echo $queue->count(); //1 //Somewhere else: $queue = $container->get(Queue::class); $queue->add(/* ... */); echo $queue->count(); //2
Invoking methods
Let's assume the following class:
class Person { public function sayHello(string $name, string $surname = '') { echo "Hello $name $surname!"; } public function sendMailMessage(PHPMailer $mail) { //send routine... } } $person = new Person(); // Inform only required parameters $container->invoke($person, 'sayHello', [ 'name' => 'John' ]); //Hello John ! // Inform required and optional parameters $container->invoke($person, 'sayHello', [ 'name' => 'John', 'surname' => 'Doe', ]); //Hello John Doe! // Mailer class automatically bound (auto wire) $container->invoke($person, 'sendMailMessage');
corviz/di-container 适用场景与选型建议
corviz/di-container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 06 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 corviz/di-container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 corviz/di-container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-28