logifire/nano-container

PSR-11 容器

2.0.3 2021-10-05 17:11 UTC

This package is auto-updated.

Last update: 2024-09-05 23:38:40 UTC


README

Scrutinizer Code Quality Build Status

一个 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);