rehmat/service-locator
Composer 安装命令:
composer require rehmat/service-locator
包简介
A simple dependency injection library for php
README 文档
README
A simple and light weight Dependency Injection library for PHP projects.
##Requires composer autoloading
#How to use?
In your php code, make sure to require composer's autoload
require 'vendor/autoload.php';
You have to import the Rehmat\ServiceLocator\ServiceLocator namespace
use Rehmat\ServiceLocator\ServiceLocator;
//instantiate ServiceLocator
$serviceLocator = ServiceLocator::getInstance();
//add configuration through which ServiceLocator can locate the factory to create your desired classes
// the ::class is required for retrieving the Fully Qualified Class Name (FQCN)
$serviceLocator->register(MyClassInterface::class, MyClassFactory::class, 'prod' );
The first parameter is the class that we want to register, the second parameter is the factory that we want to use when locating our class, the third parameter is used as a mode, while retrieving our class, if same class is registered twice but under different mode, the mode we specify takes precedence.
$myClassObj = $serviceLocator->create(MyClassInterface::class, 'prod');
Or, you can use our config reader and do it in a much quicker way.
//change this line to use ConfigReader too
use Rehmat\ServiceLocator\{ServiceLocator, ConfigReader};
$configs = [
'prod' => [
MyClassInterface::class => MyClassFactory::class
]
];
(new ConfigReader())->fromArray($configs);
统计信息
- 总下载量: 73
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2018-03-29