logifire/nano-container

PSR-11 container

2.0.3 2021-10-05 17:11 UTC

This package is auto-updated.

Last update: 2024-11-06 00:02:26 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);