如何动态地将部分附加到 Symfony 2 配置? [英] How to dynamically append sections to Symfony 2 configuration?

查看:23
本文介绍了如何动态地将部分附加到 Symfony 2 配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

my_bundle:
    algorithm: blowfish # One of 'md5', 'blowfish', 'sha256', 'sha512'

这个配置是由这个配置树完成的:

This configuration is done by this configuration tree:

// Algorithms and constants to check
$algorithms = array(
    'md5'      => 'CRYPT_MD5',
    'blowfish' => 'CRYPT_BLOWFISH',
    'sha256'   => 'CRYPT_SHA256',
    'sha512'   => 'CRYPT_SHA512',
);

$rootNode
    ->children()
        ->scalarNode('algorithm')
            ->isRequired()
            ->beforeNormalization()
                ->ifString()
                ->then(function($v) { return strtolower($v); })
            ->end()
            ->validate()
                ->ifNotInArray(array_keys($algorithms))
                ->thenInvalid('invalid algorithm.')
            ->end()
            ->validate()
                ->ifTrue(function($v) use($algorithms) {
                    return 1 != @constant($algorithms[$v]);
                })
                ->thenInvalid('algorithm %s is not supported by this system.')
            ->end()
        ->end()
    ->end();

由于每个算法需要不同的参数,如何根据所选算法将它们动态添加为根节点的子节点?

Since each algorithm requires different parameters, how can I dynamically add them as children of the root node, base on the selected algorithm?

例如,如果算法是blowfish",那么应该有一个名为cost"的标量节点,而如果sha512"是一个标量节点rounds",每个节点都有不同的验证规则.

For example, if algorithm is "blowfish" there should be a scalar node named "cost", while if "sha512" a scalar node "rounds", each with different validation rules.

编辑:我真正需要的是找出当前的算法(如何使用 $rootNode?)然后调用:

EDIT: what I really need is figure out the current algorithm (how to do with $rootNode?) and than call on of:

$rootNode->append($this->getBlowfishParamsNode());
$rootNode->append($this->getSha256ParamsNode());
$rootNode->append($this->getSha512ParamsNode());

编辑:我想完成的可能配置:

EDIT: possible configurations I'd like to accomplish:

my_bundle:
    algorithm: blowfish
    cost: 15

另一个:

my_bundle:
    algorithm: sha512
    rounds: 50000

还有一个:

my_bundle:
    algorithm: md5

推荐答案

您可以检查(使用 ifTrue())algorithm 的值是否为 md5.如果是这种情况,请取消设置包含原始配置值的数组中的 blowfishsha256sha513 键.

You could check (using ifTrue()) if the value of algorithm is md5. If this is the case, you unset the blowfish, sha256, sha513 keys in the array containing the raw config values.

如果 algorithmblowfishsha256sha513,您可以使用类似的逻辑.

You can then use a similar logic if algorithm is blowfish, sha256 or sha513.

$rootNode->
    ->beforeNormalization()
        //...
        ->ifTrue(function($v) {
            // $v contains the raw configuration values
            return 'md5' === $v['algorithm'];
        })
        ->then(function($v) {
            unset($v['blowfish']);
            unset($v['sha256']);
            unset($v['sha512']);
            return $v;
        })
        ->end()
        // ...do same logic for the others
    ->end();

你必须使用这样的东西:

You'd have to use something like this:

my_bundle:
    algorithm: blowfish
    md5: #your params
    blowfish: #will be unset if algorithm is md5 for example
    sha256: #will be unset if algorithm is md5 for example
    sha512: #will be unset if algorithm is md5 for example

正如您所提到的,您可以附加所有这些内容:

As you mention, you can then append all of them:

$rootNode->append($this->getMd5ParamsNode());
$rootNode->append($this->getBlowfishParamsNode());
$rootNode->append($this->getSha256ParamsNode());
$rootNode->append($this->getSha512ParamsNode());

编辑

还有一个 thenUnset() 函数.

Doctrine 的 这样做的方式 可能在这里很有趣.

Doctrine's way of doing it might be of interest here.

这篇关于如何动态地将部分附加到 Symfony 2 配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆