Very simple Dependency Injection Container for PHP
Code information:
Package information:
Append the lib to your requirements key in your composer.json.
{
// composer.json
// [..]
require: {
// append this line to your requirements
"nurse/di": "dev-master"
}
}
For PHP 5.3 and 5.4 support use version 0.9.2.
- Learn composer. You should not be looking for an alternative install. It is worth the time. Trust me ;-)
- Follow this set of instructions
Here is the issue tracker.
Defining a dependency:
$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');
You can also create factories:
<?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';
}
}
And then:
$factory = new \Dummy\MyDummyFactory();
$actual = $container->addFactory($factory);
Please refer to the contribuiting guide.
- Marcelo Jacobus
- Maicon Pinto
- Anthonny Machado
- Emerson Engroff
- Daniel Caña
- Elisandro Nabienger