概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:50:41 | 136 次阅读 | 评论: 0 | 来源: 网络整理

When building reusable and extensible applications, developers are often faced with a choice: either create a single large bundle or multiple smaller bundles. Creating a single bundle has the drawback that it’s impossible for users to choose to remove functionality they are not using. Creating multiple bundles has the drawback that configuration becomes more tedious and settings often need to be repeated for various bundles.

Using the below approach, it is possible to remove the disadvantage of the multiple bundle approach by enabling a single Extension to prepend the settings for any bundle. It can use the settings defined in the app/config/config.yml to prepend settings just as if they would have been written explicitly by the user in the application configuration.

For example, this could be used to configure the entity manager name to use in multiple bundles. Or it can be used to enable an optional feature that depends on another bundle being loaded as well.

To give an Extension the power to do this, it needs to implement PrependExtensionInterface:

// src/Acme/HelloBundle/DependencyInjection/AcmeHelloExtension.php
namespace AcmeHelloBundleDependencyInjection;

use SymfonyComponentHttpKernelDependencyInjectionExtension;
use SymfonyComponentDependencyInjectionExtensionPrependExtensionInterface;
use SymfonyComponentDependencyInjectionContainerBuilder;

class AcmeHelloExtension extends Extension implements PrependExtensionInterface
{
    // ...

    public function prepend(ContainerBuilder $container)
    {
        // ...
    }
}

Inside the prepend() method, developers have full access to the ContainerBuilder instance just before the load() method is called on each of the registered bundle Extensions. In order to prepend settings to a bundle extension developers can use the prependExtensionConfig() method on the ContainerBuilder instance. As this method only prepends settings, any other settings done explicitly inside the app/config/config.yml would override these prepended settings.

The following example illustrates how to prepend a configuration setting in multiple bundles as well as disable a flag in multiple bundles in case a specific other bundle is not registered:

public function prepend(ContainerBuilder $container)
{
    // get all bundles
    $bundles = $container->getParameter('kernel.bundles');
    // determine if AcmeGoodbyeBundle is registered
    if (!isset($bundles['AcmeGoodbyeBundle'])) {
        // disable AcmeGoodbyeBundle in bundles
        $config = array('use_acme_goodbye' => false);
        foreach ($container->getExtensions() as $name => $extension) {
            switch ($name) {
                case 'acme_something':
                case 'acme_other':
                    // set use_acme_goodbye to false in the config of
                    // acme_something and acme_other note that if the user manually
                    // configured use_acme_goodbye to true in the app/config/config.yml
                    // then the setting would in the end be true and not false
                    $container->prependExtensionConfig($name, $config);
                    break;
            }
        }
    }

    // process the configuration of AcmeHelloExtension
    $configs = $container->getExtensionConfig($this->getAlias());
    // use the Configuration class to generate a config array with
    // the settings "acme_hello"
    $config = $this->processConfiguration(new Configuration(), $configs);

    // check if entity_manager_name is set in the "acme_hello" configuration
    if (isset($config['entity_manager_name'])) {
        // prepend the acme_something settings with the entity_manager_name
        $config = array('entity_manager_name' => $config['entity_manager_name']);
        $container->prependExtensionConfig('acme_something', $config);
    }
}

The above would be the equivalent of writing the following into the app/config/config.yml in case AcmeGoodbyeBundle is not registered and the entity_manager_name setting for acme_hello is set to non_default:

  • YAML
    # app/config/config.yml
    acme_something:
        # ...
        use_acme_goodbye: false
        entity_manager_name: non_default
    
    acme_other:
        # ...
        use_acme_goodbye: false
    
  • XML
    <!-- app/config/config.xml -->
    <acme-something:config use-acme-goodbye="false">
        <acme-something:entity-manager-name>non_default</acme-something:entity-manager-name>
    </acme-something:config>
    
    <acme-other:config use-acme-goodbye="false" />
    
  • PHP
    // app/config/config.php
    $container->loadFromExtension('acme_something', array(
        // ...
        'use_acme_goodbye' => false,
        'entity_manager_name' => 'non_default',
    ));
    $container->loadFromExtension('acme_other', array(
        // ...
        'use_acme_goodbye' => false,
    ));
    
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务