kambo/deinj

依赖注入容器

v0.0.1 2017-12-27 19:27 UTC

This package is auto-updated.

Last update: 2024-09-19 16:26:41 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Software License

这只是另一个具有以下特点的简单依赖注入容器:

  • 支持 PSR-11 - 容器接口

安装

安装库的首选方式是使用 composer

composer require kambo/deinj

用法

use Kambo\Deinj\Container;

use Kambo\Examples\Deinj\Transport;
use Kambo\Examples\Deinj\Mailer;
use Kambo\Examples\Deinj\UserManager;

// Create instance of the container.
$container = new Container;

// Set object factory method for identifier 'transport'.
$container->set(
    'transport',
    // Factory method is plain callback.
    function () {
        return new Transport();
    }
);

// Set object factory method for identifier 'mailer'.
$container->set(
    'mailer',
    // Factory method is plain callback. An instance of the container must be passed inside
    // the closure for allowing of the 'transport' injection.
    function ($container) {
        return new Mailer($container->get('transport'));
    }
);

// Set object factory method for identifier 'userManager'.
$container->set(
    'userManager',
    // Factory method is plain callback. An instance of the container must be passed inside
    // the closure for allowing of the 'mailer' injection.
    function ($container) {
        return new UserManager($container->get('mailer'));
    }
);

// Get instance of the UserManager service.
$userManager = $container->get('userManager');

// Call register method in the UserManager service.
$userManager->register('username', 'password');

许可协议

MIT 许可协议 (MIT), https://open-source.org.cn/licenses/MIT