A dependency injection container.

0.1.1 2016-07-05 02:26 UTC

This package is not auto-updated.

Last update: 2020-06-26 19:20:25 UTC


README

Build Status Scrutinizer Code Quality Author GitHub license

A simple dependency injection container for use with Jaeger (or stand alone).

Installation

Add jaeger-app/di as a requirement to your composer.json:

$ 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:

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.

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');