logifire/nano-container

PSR-11 container

2.0.2 2020-11-10 16:13 UTC

This package is auto-updated.

Last update: 2021-04-17 20:40:46 UTC


README

Scrutinizer Code Quality Build Status

A PSR-11 container implementation

Usage

Basics

$factory = new ContainerFactory();

$factory->set('my_value', 42);

$factory->register('my_service', function(Container $c) {
    return 100 + $c->get('my_value');
});

$container = $factory->createContainer();

echo $container->get('my_value'); // 42

echo $container->get('my_service'); // 142

Grouping configurations You can group configurations into providers, which takes a ContainerFactory

class ControllerProvider implements FactoryProvider {
    public function register(ContainerFactory $factory) {
        $factory->register(LoginController::class, function(Container $c) {
            return new LoginController();
        });
    }
}

$factory = new ContainerFactory();
$factory->addProvider(new ControllerProvider());
$container = $factory->createContainer();
$controller = $container->get(LoginController::class);