djuricmilos/container

v1.1.0 2020-09-08 22:03 UTC

This package is auto-updated.

Last update: 2024-09-09 07:14:05 UTC


README

Build Status Code Quality Code Coverage Latest Version PDS Skeleton

安装

推荐的安装方法是使用 Composer。运行以下命令安装最新版本的包并将其添加到项目的 composer.json 文件中:

composer require djuricmilos/container

使用

简单的容器

创建容器最快的方法是实例化 Laganica\Di\Container 类。

use Laganica\Di\Container;

$container = new Container();

默认情况下,自动装配启用,注解禁用。

配置容器

use Laganica\Di\ContainerBuilder;

$builder = new ContainerBuilder();
$builder->useAutowiring(false);
$builder->useAnnotations(false);

$container = $builder->build();

定义

如果无法使用自动装配创建服务类的对象,我们必须为该服务创建一个定义。定义告诉容器如何实例化服务类。

接口到类绑定

容器将使用传递给绑定方法的类名来创建该类的实例。

use Laganica\Di\ContainerBuilder;
use function Laganica\Di\bind;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => bind(Service::class)
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);

类名

与绑定相同,但更短。

use Laganica\Di\ContainerBuilder;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => Service::class
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);

闭包

容器将调用闭包来创建服务实例。注意,$container 作为闭包参数可用。

use Laganica\Di\ContainerBuilder;
use Psr\Container\ContainerInterface;
use function Laganica\Di\closure;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => closure(static function (ContainerInterface $container) {
        return new Service($container->get(Dependency::class));
    })
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);

内联工厂

与闭包相同,但更短。

use Laganica\Di\ContainerBuilder;
use Psr\Container\ContainerInterface;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => static function (ContainerInterface $container) {
        return new Service($container->get(Dependency::class));
    }
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);

工厂

容器将调用工厂类的对象来创建服务实例。

use Laganica\Di\ContainerBuilder;
use function Laganica\Di\factory;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => factory(ServiceFactory::class)
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);

服务工厂是实现了 Laganica\Di\FactoryInterface 接口并使用其 __invoke 方法来定义如何创建服务的类。

use Laganica\Di\FactoryInterface;
use Psr\Container\ContainerInterface;

class ServiceFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container)
    {
        return new Service($container->get(Dependency::class));
    }
}

别名

容器将使用传递给别名方法的条目名称来查找其他条目并使用它来创建服务实例。

use Laganica\Di\ContainerBuilder;
use function Laganica\Di\alias;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => Service::class,
    'service-alias' => alias(ServiceInterface::class)
]);

$container = $builder->build();
$service = $container->get('service-alias');

容器将返回传递给值方法的值。

use Laganica\Di\ContainerBuilder;
use function Laganica\Di\value;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    'count' => value(100)
]);

$container = $builder->build();
$count = $container->get('count');

制作

有时我们不想从容器中共享服务。为此,我们可以使用 make() 方法。

use Laganica\Di\ContainerBuilder;
use function Laganica\Di\bind;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => bind(Service::class)
]);

$container = $builder->build();
$service = $container->make(ServiceInterface::class);

注解

容器将使用 @Inject 注解在 Service 类中的 $dependency 属性来注入 Dependency。由于默认启用自动装配,它将用于创建 Dependency 类的实例。

use Laganica\Di\ContainerBuilder;

class Service
{
    /**
     * @Inject
     * @var Dependency
     */
    private $dependency;
}

$builder = new ContainerBuilder();
$builder->useAnnotations(true);

$container = $builder->build();
$service = $container->get(Service::class);

致谢

许可

在 MIT 许可证下发布 - 详细信息请参阅 许可证文件