chilldev/dependency-injection-extra

提供了一些常用的Symfony2 DI模式的实现。

这个包的官方仓库似乎已经消失,因此该包已被冻结。

0.0.1 2013-09-12 18:44 UTC

This package is not auto-updated.

Last update: 2019-03-08 14:40:41 UTC


README

ChillDevDependencyInjectionExtra 是一个库,它实现了在 Symfony2 DI 中常用的一些模式。

Build Status Scrutinizer Quality Score Coverage Status Dependency Status SensioLabsInsight

安装

这个库以 Composer 包 的形式提供。要安装它,只需将以下依赖定义添加到您的 composer.json 文件中

"chilldev/dependency-injection-extra": "dev-master"

如果您想使用特定版本,可以将 dev-master 替换为不同的约束条件。

注意: 这个库需要 PHP 5.4

注意: 这不是一个捆绑包;您不需要修改您的内核或项目的任何其他部分来使用它 - 直接在代码中使用即可。

使用方法

您可以使用 ChillDev\DependencyInjection\Compiler\TagGrabbingPass来自动将所有带有标签的服务抓取到容器中。考虑以下DI服务

services:
    repository:
        class: "YourAdaptersRepository"
    first:
        class: "YourFirstAdapter"
        tags:
            - { name: "your.adapter.tag", adapter: "first" }
    second:
        class: "YourSecondAdapter"
        tags:
            - { name: "your.adapter.tag", adapter: "second" }
    third:
        class: "YourThirdAdapter"
        tags:
            - { name: "your.adapter.tag", adapter: "third" }

假设您的仓库类如下所示

class YourAdaptersRepository extends ArrayObject
{
    public function registerAdapter($key, $adapter)
    {
        $this[$key] = $adapter;
    }
}

这是一个非常常见的设置 - 现在您想将所有带有 your.adapter.tag 标签的服务添加到 repository 服务中。您不需要编写自己的自定义DI编译器传递,可以使用现成的实现来做到这一点。就像这样简单

use ChillDev\DependencyInjection\Compiler\TagGrabbingPass;

use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class YourBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        $container->addCompilerPass(
            new TagGrabbingPass(
                // this is tag name for which you want to look
                'your.adapter.tag',
                // this is ID of container service
                'repository',
                // this is name of method for registering new adapters
                'registerAdapter',
                // this is tag attribute name to be used as key identifier
                'adapter'
            ),
            PassConfig::TYPE_OPTIMIZE
        );
    }
}

有关更高级的方面,请参阅高级使用文档或甚至内部描述

资源

贡献

您想帮助改进这个项目吗?只需 分支 它并提交一个pull request。您可以自己完成所有事情,不需要询问是否可以,只需做您想做的所有酷炫的事情!

此项目在MIT许可证下发布。

作者

ChillDevDependencyInjectionExtraChillout Development 提供。

贡献者列表