symfony服务:只覆盖一个参数

时间:2013-10-03 09:46:48

标签: symfony

我有一个从packagist.org导入的软件包,位于供应商目录中 此捆绑包具有带参数

的services.yml配置文件
parameters:
    myservice.class: ...
    myservice.options:
        MERCHANT_ID: '11223344556677'
        ACCESS_KEY: YourAccesKey
        ...

我想在 app / config / parameters.yml 中覆盖一个参数,但它会删除所有myservice.options数组

parameters:
    myservice.options:
        MERCHANT_ID: '99999999999999'

如何只覆盖一个参数?

1 个答案:

答案 0 :(得分:4)

您应该公开您的捆绑配置,以及在此处执行此操作的文档:

http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class

以下代码基础。应该对数组的每个参数进行

// ACME/YourBundle/DependencyInjection/Configuration.php
class Configuration implements ConfigurationInterface
{

    public function getConfigTreeBuilder()
    {

        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('your_bundle');

        $rootNode
                ->children()
                ->arrayNode('parameters')
                        ->prototype('array')
                            ->treatNullLike(array())
                            ->children()
                                ->scalarNode('MERCHANT_ID')
                                    ->isRequired()
                                    ->cannotBeEmpty()
                                ->end()
                                    [...] // Another node
                            ->end()
                        ->end()
                    ->end()
               ->end()
            ->end()
    ;

    return $treeBuilder;
}

然后他的config.yml文件中的每个用户都将定义他的私有参数:

your_bundle
    parameters:
        MERCHANT_ID: XXXXXX