Jaeger Dependency Injection Container
, (*1)
A simple dependency injection container for use with Jaeger (or stand alone)., (*2)
Installation
Add jaeger-app/di as a requirement to your composer.json:, (*3)
$ composer require jaeger-app/di
Adding Services
Ideally, like all Jaeger classes, you should extend Jaeger\Bootstrap and initialize the parent services before adding your own like the below:, (*4)
use \JaegerApp\Di;
class MyDi extends Di
{
public function getServices()
{
$this->container = parent::getServices(); //init existing services
//add new service
$this->container['my_service'] = function ($c) {
$settings = new NewService;
return $settings;
};
return $this->container;
}
}
You can also add new Services at run time by using the setService($name, \Closure $function) method., (*5)
use \JaegerApp\Di;
$di = new Di();
$callable = function() {
return 'foo to the who';
};
$di->setService('test_service', $callable);
Calling Services Example
use \JaegerApp\Di;
$di = new Di();
//get all the services
$services = $di->getServices();
//get a specific service
$db = $services['db'];
//or get specific service directly
$db = $di->getService('db');