fastero/php-di

简单且快速的php依赖注入器

v1.0.0 2017-12-14 12:04 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:15:32 UTC


README

简单快速的依赖注入器。支持自动装配和自动加载。

需要php 7.0以上

使用composer轻松安装

$ composer require fastero/php-di

使用方法

$di = new \Fastero\DependencyInjector\DependencyInjector();

$user = $di->get(User::class);

像这样简单,无需配置,只需为构造函数提供正确的类型提示。

当然,根据依赖注入器的非常定义,你通常不会经常调用$di->get(),只有在对应用程序核心非常关键的地方才会调用,然后让所有依赖项进行注入。

更多完整的示例:让我们创建一些要工作的类

class Company{

}
class User{
    public $company;
    public $language;
    //typehint Company says that first parameter expected to be of 
    //type Company and DependencyInjetor can understand this
    public function __construct(Company $userCompany, $language = null)
    {
        $this->company = $userCompany;
        $this->language = $language;
    }
}

class UserFactory{
    public static function create(){
        return new User(new Company(), 'en');
    }
}

默认情况下无需配置,但如果你必须,这里是如何使用工具类ServiceConfiguration来添加一些配置

$configuration = [
        "services" =>[
            Company::class => ServiceConfiguration::setupClass(Company::class) //service that will create object of Company
                ->get(),//return complete configuration, should be very last call for every definition
            User::class => ServiceConfiguration::setupClass(User::class) //service that will create object of User
                ->addParameterService(Company::class) //first parameter in the constructor will service with name Company::class
                ->addParameterValue('fr') //second parameter will be value "fr"
                ->get(),//return complete configuration
            'forty-two' => ServiceConfiguration::setupClosure(function ($di, $serviceName){//closure will be called and returned value is a service
                        return 42;
                    })
                ->get(),
        ]
    ];

推荐使用ServiceConfiguration来创建服务的配置,尽管实际的配置是一个简单的数组,可以手动创建,但这个工具类有助于避免错误。使用这个类不会创建大量的对象,因此几乎没有开销。缺点是必须在每个定义的末尾调用 ->get()。

$configuration = [
        "services" =>[
            Company::class => ServiceConfiguration::setupClass(Company::class) //service that will create object of Company
                ->get(),//return complete configuration, should be very last call for every definition
            User::class => ServiceConfiguration::setupClass(User::class) //service that will create object of User
                ->addParameterService(Company::class) //first parameter in the constructor will service with name Company::class
                ->addParameterValue('fr') //second parameter will be value "fr"
                ->get(),//return complete configuration
            'forty-two' => ServiceConfiguration::setupClosure(function ($di, $serviceName){//closure will be called and returned value is a service
                        return 42;
                    })
                ->get(),
        ]
    ];
    

因此,创建配置必须从->setup*(..)方法之一开始,并以->get()方法结束,该方法实际上返回配置数组。

$di = new Fastero\DependencyInjector\DependencyInjector($configuration);

//if you need to define some service after configuration is set there is a way
//here we create service type factory but sure thing it can be any supported type
$UserEngServiceConfiguration =
    ServiceConfiguration::setupFactory([UserFactory::class, 'create'])// call_user_function_array([UserFactory::class, 'create'], $params) will be called and return value is a service
        ->get();

$di->setServiceConfiguration('UserEng', $UserEngServiceConfiguration);
$service = $di->get(User::class );

var_dump($service instanceof User);
var_dump($service->language);

$service = $di->get("UserEng" );
var_dump($service instanceof User);
var_dump($service->language);

$service = $di->get("forty-two");
var_dump($service);

结果

bool(true)
string(2) "fr"
bool(true)
string(2) "en"
int(42)

MIT许可,https://open-source.org.cn/licenses/MIT