Container is a lightweight dependency injection container. It's compatible with the PSR-11 container interface, so you can use it with lots of different projects out of the box.
The container is really simple; It's basically an array of identifier => callable mappings, and the callable is invoked to get the resulting object.
New to dependency injection and containers? I wrote a blog post explaining dependency injection and how this container works.
This package is compliant with PSR-1, PSR-2, PSR-4, and PSR-11.
Via Composer
$ composer require yuloh/container
Adding an entry to the container is really simple. Just specify the identifier as the first argument, and a callable as the second argument.
use Yuloh\Container\Container;
$container = new Container();
$container->set(Psr\Log\LoggerInterface::class, function () {
$logger = new Monolog\Logger();
$logger->pushHandler(new StreamHandler('error.log'));
return $logger;
});
The closure will receive the container as it's only argument, so you can use the container to resolve the dependencies of your entry.
$container->set('db', function ($container) {
$db = new Database();
$logger = $container->get(Psr\Log\LoggerInterface::class);
$db->setLogger($logger);
return $db;
});
All entries are shared (singletons), which means an entry will be resolved once and reused for subsequent calls.
To check if an entry exists, use has
. To get an entry, use get
. If you are just retrieving entries you can typehint Psr\Container\ContainerInterface
instead of the actual Container.
if ($container->has('db')) {
$db = $container->get('db');
}
There are a lot of containers out there. I was working on a project and wanted a lightweight default container and couldn't find what I wanted. This container:
- Implements container-interop.
- Supports PHP 5.4+
- Supports adding entries at runtime.
- Is incredibly lightweight, with the bare minimum of code to support the first 3 goals.
$ composer test
$ composer cs