matthiasnoback/lazy-services-bundle

标记服务和参数为“懒加载”的Bundle

v0.2.0 2013-07-11 07:53 UTC

This package is auto-updated.

Last update: 2024-08-29 03:57:43 UTC


README

由Matthias Noback编写

Build Status

安装

运行

php composer.phar require matthiasnoback/lazy-services-bundle 0.2.*

然后在 /app/AppKernel.php 中注册Bundle

<?php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Matthias\LazyServicesBundle\MatthiasLazyServicesBundle,
        );
    }
}

使用方法

懒参数

此Bundle允许您通过向引用它们的 参数 添加标签来将服务标记为懒加载

<service id="some_service" class="...">
    <argument type="service" id="mailer" key="mailer" />
    <tag name="lazy_argument" key="mailer" />
</service>

参数键可以是 字符串(如上例所示),或一个 数字,表示参数的索引(从0开始)

<service id="some_service" class="...">
    <argument type="service" id="event_dispatcher" /><!-- key is 0 -->
    <argument type="service" id="mailer" /><!-- key is 1 -->
    <tag name="lazy_argument" key="1" />
</service>

两个示例都将有效地将 mailer 服务转换为懒加载服务。

当引用的服务不存在时,它将静默跳过。

通过配置标记懒服务

为了方便起见,此Bundle还允许您通过将服务ID添加到应用程序配置中懒服务的列表中来将服务标记为懒加载

# in config.yml:

matthias_lazy_services:
    lazy_service_ids:
        - mailer
        - ...

当引用的服务不存在时,它将静默跳过。