logifire / nano-container
PSR-11 容器
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
一个 PSR-11 容器实现
用法
基础知识
$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
配置分组 您可以将配置分组为提供者,它需要一个 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);