护士 / 迪
依赖注入库
v3.0
2018-03-30 17:01 UTC
Requires
- php: >=7.0
- container-interop/container-interop: ^1.2
- psr/container: ^1.0
Requires (Dev)
- phpunit/phpunit: ~6.0
- satooshi/php-coveralls: ~1.0.0
This package is not auto-updated.
Last update: 2024-09-14 16:36:31 UTC
README
非常简单的PHP依赖注入容器
代码信息
包信息
安装
通过Composer安装
将lib添加到composer.json中的require键。
{ // composer.json // [..] require: { // append this line to your requirements "nurse/di": "dev-master" } }
为PHP 5.3和5.4支持使用版本0.9.2。
替代安装
问题/功能建议
这里是问题跟踪器。
用法
定义依赖
$container = new Nurse\Container; // Defining a dependency $container->set('connection', function ($container) { $params = $container->get('connection_params'); return new Connection($params); }) ->set('connection_params', function () { return array( 'schema' => 'someschema', 'username' => 'root', 'password' => 's3cr3t', ); }); // Retrieving the dependency (lazy loading) $connection = $container->get('connection'); // alternatively you can use the singleton instance of the container Nurse\Di::set('connection', function ($container) { $params = $container->get('connection_params'); return new Connection($params); }) ->set('connection_params', function () { return array( 'schema' => 'someschema', 'username' => 'root', 'password' => 's3cr3t', ); }); $connection = Nurse\Di::get('connection');
您也可以创建工厂
<?php namespace App; use Nurse\Factory\FactoryInterface; use Psr\Container\ContainerInterface; class ConnectionFactory implements FactoryInterface { public function createService(ContainerInterface $container) { $params = $container->get('connection_params'); return new Connection($params); } public function getKey() { return 'connection'; } }
然后
$factory = new \Dummy\MyDummyFactory(); $actual = $container->addFactory($factory);
贡献
请参阅贡献指南。
许可证
作者
- Marcelo Jacobus
- Maicon Pinto
- Anthonny Machado
- Emerson Engroff
- Daniel Caña
- Elisandro Nabienger