corviz/di-container 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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');

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固