logifire / nano-container
PSR-11 container
2.0.3
2021-10-05 17:11 UTC
Requires
- php: >=7.0.0
- psr/container: ^2.0.1
Requires (Dev)
- phpunit/phpunit: ^9.5.10
Provides
- psr/container-implementation: ^2.0.1
README
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);