Symfony覆盖app / config.yml中的配置

时间:2013-06-27 08:39:51

标签: symfony configuration doctrine

我有app/config.yml这个:

# Doctrine Configuration
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true

现在我创建了我的包,我希望它覆盖一些配置:

doctrine:
    orm:
        resolve_target_entities:
              Acme\UserBundle\Interfaces\User: Acme\MyBundle\Entity\User

我想在不更改app/config.yml

的情况下添加此配置

2 个答案:

答案 0 :(得分:3)

我通过添加编译器传递找到了解决方案:

class OrmResolverCompilerPass implements CompilerPassInterface
{

    public function process(ContainerBuilder $container)
    {
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config'));
        $loader->load('orm.yml');

        $def = $container->findDefinition('doctrine.orm.listeners.resolve_target_entity');
        foreach ($container->getParameter('resolve_target_entities') as $name => $implementation) {
            $def->addMethodCall('addResolveTargetEntity', array( $name, $implementation, array() ));
        }
        $def->addTag('doctrine.event_listener', array('event' => 'loadClassMetadata'));
    }

}

我们还需要在Bundle内的orm.yml目录中创建Resources/config

最后在controllerBundle类中注册编译器传递:

class MyAcmeBundle extends Bundle
{

    public function build(ContainerBuilder $container)
    {
        parent::build($container);
        $container->addCompilerPass(new OrmResolverCompilerPass());
    }

}

答案 1 :(得分:2)

您现在可以使用PrependExtensionInterface

class ThirisCartLogicCatalogExtension extends Extension implements PrependExtensionInterface
{    
    public function prepend(ContainerBuilder $container)
    {
        $config = Yaml::parse(file_get_contents(__DIR__.'/../Resources/config/config.yml'));

        foreach ($config as $key => $configuration) {
            $container->prependExtensionConfig($key, $configuration);
        }
    }
}

使用此代码,您的捆绑包中的config.yml将无条件地合并到全局配置中。

请注意,您以这种方式加载is not validated for syntax的配置文件。