Modify bundle configuration from another bundle

家住魔仙堡 提交于 2019-12-18 13:20:04

问题


I was wondering whether there's a possible to modify a bundles configuration from another bundle. Let's say, for example, I'm using the FOSUserBundle with the following configuration:

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: Acme\UserBundle\Entity\User

And now, I want to change the user class when loading a specific extension (the AcmeFoobarExtension). Is it possible to change the configuration when loading the AcmeFoobarExtension? For example:

<?php
namespace Acme\FoobarBundle\DependencyInjection;

// use statements for dependency injection

class FoobarExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $container->setConfiguration(
            'fos_user.user_class',
            'Acme\FoobarBundle\Entity\User'
        );
    }
}

Is something like this possible? Or defeats it the purpose of dependency injection?

EDIT: Apparently there's a pull request for Symfony 2.2 which addresses this idea/issue.

EDIT 2: This is now available in Symfony 2.2 (with an article in the cookbook).


回答1:


It should be noted that this is now an official feature of Symfony:

http://symfony.com/doc/current/cookbook/bundles/prepend_extension.html

A bundle can prepend configuration values to the global config parameters. To do that, the bundle extension must implement PrependExtensionInterface. The prepend() method can then add global configuration values.

Note, however, that the values may be overwritten by other bundles and in the config file itself.




回答2:


After asking the same question on the Symfony user mailing list (here's the question), I've found out that there's an open pull request for this idea.

It is for version 2.2 and not yet merged into master, but the last activity is from 3 days ago, so let's hope for the best!



来源:https://stackoverflow.com/questions/12114932/modify-bundle-configuration-from-another-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!