Deinj - simple dependency injection container
, (*1)
Just another simple dependency injection container with following highlights:, (*2)
- Support of PSR-11 - Container interface
Install
Prefered way to install library is with composer:, (*3)
composer require kambo/deinj
Usage
use Kambo\Deinj\Container;
use Kambo\Examples\Deinj\Transport;
use Kambo\Examples\Deinj\Mailer;
use Kambo\Examples\Deinj\UserManager;
// Create instance of the container.
$container = new Container;
// Set object factory method for identifier 'transport'.
$container->set(
'transport',
// Factory method is plain callback.
function () {
return new Transport();
}
);
// Set object factory method for identifier 'mailer'.
$container->set(
'mailer',
// Factory method is plain callback. An instance of the container must be passed inside
// the closure for allowing of the 'transport' injection.
function ($container) {
return new Mailer($container->get('transport'));
}
);
// Set object factory method for identifier 'userManager'.
$container->set(
'userManager',
// Factory method is plain callback. An instance of the container must be passed inside
// the closure for allowing of the 'mailer' injection.
function ($container) {
return new UserManager($container->get('mailer'));
}
);
// Get instance of the UserManager service.
$userManager = $container->get('userManager');
// Call register method in the UserManager service.
$userManager->register('username', 'password');
License
The MIT License (MIT), https://opensource.org/licenses/MIT, (*4)