yokai/dependency-injection

提供用于操作 Symfony 依赖注入的实用类

v1.5 2024-09-05 08:42 UTC

README

安装

$ php composer.phar require yokai/dependency-injection

编译器通过

<?php

namespace AppBundle;

use Yokai\DependencyInjection\CompilerPass\ArgumentRegisterTaggedServicesCompilerPass;
use Yokai\DependencyInjection\CompilerPass\AdderRegisterTaggedServicesCompilerPass;
use Yokai\DependencyInjection\CompilerPass\SetterRegisterTaggedServicesCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class AppBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        $container
            ->addCompilerPass(
                new ArgumentRegisterTaggedServicesCompilerPass(
                    'some_service_id',
                    'some_tag_name',
                    'An\Optional\Interface\To\Check',
                    0
                )
            )
            ->addCompilerPass(
                new AdderRegisterTaggedServicesCompilerPass(
                    'some_service_id',
                    'some_tag_name',
                    'An\Optional\Interface\To\Check',
                    'addDependency'
                )
            )
            ->addCompilerPass(
                new SetterRegisterTaggedServicesCompilerPass(
                    'some_service_id',
                    'some_tag_name',
                    'An\Optional\Interface\To\Check',
                    'setDependencies'
                )
            )
        ;
    }
}

ArgumentRegisterTaggedServicesCompilerPass

此编译器通过将

  • 检查服务(第一个参数)是否存在(如果不存在,则抛出 LogicException
  • 查找带有标签的服务(第二个参数)
  • 如果提供,则检查每个服务是否与接口(第三个参数)匹配(如果不符合,则抛出 LogicException
  • 根据 priority 属性对这些引用进行排序
  • 将您的服务定义中的一个参数(第四个参数)替换为排序后的引用

AdderRegisterTaggedServicesCompilerPass

此编译器通过将

  • 检查服务(第一个参数)是否存在(如果不存在,则抛出 LogicException
  • 查找带有标签的服务(第二个参数)
  • 如果提供,则检查每个服务是否与接口(第三个参数)匹配(如果不符合,则抛出 LogicException
  • 根据 priority 属性对这些引用进行排序
  • 对每个排序后的引用调用一个方法(第四个参数)

SetterRegisterTaggedServicesCompilerPass

此编译器通过将

  • 检查服务(第一个参数)是否存在(如果不存在,则抛出 LogicException
  • 查找带有标签的服务(第二个参数)
  • 如果提供,则检查每个服务是否与接口(第三个参数)匹配(如果不符合,则抛出 LogicException
  • 根据 priority 属性对这些引用进行排序
  • 使用所有排序后的引用调用一个方法(第四个参数)

MIT 许可证

许可证可以在 这里 找到。

作者

此捆绑包最初由 Yann Eugoné 创建。查看 贡献者 列表。