ashleydawson/multibundle

在 Symfony2 内核中注册多个包

1.0.2 2016-01-22 10:19 UTC

This package is auto-updated.

Last update: 2024-09-14 03:11:18 UTC


README

Build Status

Symfony2 内核中注册多个、相互依赖的包

需求

 >= PHP 5.3
 >= Symfony Framework 2.3

介绍

当在 Symfony2 中开发使用多包配置的解决方案时 - 这个库提供了一种无侵入的方式来逻辑上分组依赖的包,以便可以一次性注册到 Symfony 内核。

构建此辅助工具的原因是为了管理并行依赖的包。或者您也可以使用 包继承 来解决这个问题 - 但这可能会违反单一职责原则,因为子包可能具有与其父包不同的职责。

安装

您可以通过 Composer 安装多包。要这样做,只需在您的 composer.json 文件中要求该包,如下所示

{
    "require": {
        "ashleydawson/multibundle": "~1.0"
    }
}

运行 composer update 以安装包。

基本用法

第一步是扩展 AbstractMultiBundle 而不是 Symfony2 附带的 Bundle。这个抽象类允许您定义您的分组包,并公开一个 registerInto() 方法用于在 Symfony2 内核中使用。

<?php

namespace Acme\MyBundle;

use AshleyDawson\MultiBundle\AbstractMultiBundle;

class AcmeMyBundle extends AbstractMultiBundle
{
    /**
     * Optional: define a protected constructor to stop instantiation outside of registerInto()
     */
    protected function __construct()
    {

    }

    /**
     * Define bundles that this bundle depends on
     */
    protected static function getBundles()
    {
        return array(
            new Acme\FooBundle\AcmeFooBundle(),
            new Acme\BarBundle\AcmeBarBundle(),
        );
    }
}

第二步是将您的包注册到 Symfony2 内核中,如下所示

// app/AppKernel.php

// ...

class AppKernel extends Kernel
{
    // ...

    public function registerBundles()
    {
        $bundles = array(
            // ...,
            new FOS\UserBundle\FOSUserBundle(),
        );

        // Register my bundle and its dependencies
        \Acme\MyBundle\AcmeMyBundle::registerInto($bundles);

        // ...
    }
}

注意: 现在您不需要像往常一样注册依赖项,因为 registerInto() 方法负责这一点。另外,不用担心您的包尝试注册重复的包,因为去重是内置的。

注意: registerInto() 方法将自动注册父包及其依赖项,因此您不需要在 getBundles() 返回的数组中指定父包。

环境分组

有时有必要按环境分组包。一个例子是,您可能不需要在生产环境中使用开发包。为此,只需在 getBundles() 方法中指定环境组

<?php

namespace Acme\MyBundle;

use AshleyDawson\MultiBundle\AbstractMultiBundle;

class AcmeMyBundle extends AbstractMultiBundle
{
    /**
     * Optional: define a protected constructor to stop instantiation outside of registerInto()
     */
    protected function __construct()
    {

    }

    /**
     * Define bundles that this bundle depends on
     */
    protected static function getBundles()
    {
        return array(
            'prod' => array(
                new Acme\FooBundle\AcmeFooBundle(),
                new Acme\BarBundle\AcmeBarBundle(),
            ),
            'dev' => array(
                new Acme\BazBundle\AcmeBazBundle(),
            ),
        );
    }
}

然后,在内核中,使用 registerInto() 方法的第二个参数过滤包的注册

// app/AppKernel.php

// ...

class AppKernel extends Kernel
{
    // ...

    public function registerBundles()
    {
        $bundles = array(
            // ...,
            new FOS\UserBundle\FOSUserBundle(),
        );

        // Register my bundle and its dependencies for the 'prod' environment
        \Acme\MyBundle\AcmeMyBundle::registerInto($bundles, 'prod');

        if ('dev' == $this->getEnvironment()) {

            // Register my bundle and its dependencies for the 'dev' environment
            \Acme\MyBundle\AcmeMyBundle::registerInto($bundles, 'dev');
        }
    }
}

Symfony2 标准版已经在 app/AppKernel.php 中具有类似的逻辑。