babacooll/imbrix
最新稳定版本:v0.0.3
Composer 安装命令:
composer require babacooll/imbrix
包简介
Dependency Manager for PHP projects
README 文档
README
Imbrix is a fully unit tested dependency manager suited for small PHP projects.
Still in progress
- Wrapper for DependencyManager to get static access to it (singleton way ?)
Installation
Imbrix is available through composer :
$ php composer.phar require babacooll/imbrix ~0.0.3
How to use
All you need is to instanciate a new DependencyManager :
<?php use Imbrix\DependencyManager; $depManager = new DependencyManager();
This DependencyManager will contains both your services and parameters.
Services
You can add your services easily with the addService method, first parameter being your service name and second a closure returning your service :
<?php $depManager->addService('myService', function () { return new MyService(); });
You can retrieve your service with the get method :
<?php $depManager->get('myService');
Parameters
Same method exists for parameters, first parameter being your parameter name and second is string value :
<?php $depManager->addParameter('myParameter', 'value');
You can retrieve your parameter with the get method :
<?php $depManager->get('myParameter');
Injection
You can inject parameters into services and services into services (as many times you need it). All you need is the name of the service/parameter you want to inject :
<?php // Injection of a parameter into a service $depManager = new DependencyManager(); $depManager->addParameter('myParameter', 'value'); $depManager->addService('myService', function ($myParameter) { return new MyService($myParameter); });
The order of the parameters/service definition does not matter as your service will be instanciate after the whole definition when you do call the get method (not before !).
A more complex example as following :
<?php $depManager = new DependencyManager(); $depManager->addParameter('myParameter', 'value'); $depManager->addService('myService', function ($myParameter) { return new MyService($myParameter); }); $depManager->addService('mySecondService', function ($myService, $myParameter) { return new MySecondService($myService, $myParameter); }); // We suppose MySecondService has both a getMyService() and a getParameter() method and the Service a getParameter() echo $depManager->get('mySecondService')->getMyService()->getParameter(); echo $depManager->get('mySecondService')->getParameter(); // Both will return "value"
Feel free to add feedbacks !
统计信息
- 总下载量: 41
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-04-01