简体   繁体   English

Symfony:如何从文件加载自定义捆绑包配置?

[英]Symfony: How to load custom bundle config from file?

I am using Symfony 3.4 . 我正在使用Symfony 3.4 How can I add a custom config file to a custom bundle which should hold a list of names and regular expressions? 如何将自定义配置文件添加到应包含名称和正则表达式列表的自定义包中? For example: 例如:

// Config file content = list of names and regular expressions
NameA
NameB
Name[cC]+
Some\w+Other(Name|Pattern)
...


// Symfony controller
$patterns = $this->getPatternConfigFromFileSomehow(...);

In in the Symfony docs I found information on adding custom config to the .yml files in the app/config or MyBundle/Resources/config dirs. 在Symfony文档中,我找到了有关在app/configMyBundle/Resources/config目录.yml自定义配置添加到.yml文件的信息。 However, this is not about a small set of well defined parameters (eg mailaddress: xyz ) but list of any number of entries. 但是,这不是一mailaddress: xyz定义良好的参数(例如mailaddress: xyz ),而是任何数量的条目的列表。

Additionally the config should only be loaded if its actually used and not every time the kernel or a services is created. 另外,仅当配置实际使用时才应加载配置,而不是每次创建内核或服务时才加载。

Of course I could simply use file_get_contents or any similar PHP method to load any file, however this seems to be quite hacky and not the "Symfony way" ... 当然,我可以简单地使用file_get_contents或任何类似的PHP方法来加载任何文件,但是这似乎很hacky,而不是“ Symfony方式” ...

So, what is the "correct" way of doing this? 那么,这样做的“正确”方法是什么?

EDIT: 编辑:

yml files are great to configure parameters , but this is not about parameters (= key + value) but a list of file names and regular expressions. yml文件非常适合配置参数 ,但这与参数(=键+值) 无关 ,而是文件名和正则表达式的列表。 This list does not have a defined length and the entries do not have defined names. 这份名单没有确定的长度和条目没有定义的名称。 Thus .yml is not the right solution here, is it? 因此,.yml在这里不是正确的解决方案,对吗?

Additionally config files which are added using the ConfigTreeBuilder are loaded when the kernel is loaded, aren't they? 另外,使用ConfigTreeBuilder添加的配置文件是在加载内核时加载的,不是吗? I am looking for a lazy solution which loads the list only when needed. 我正在寻找一种仅在需要时才加载列表的惰性解决方案。

Put your yml file in any config directory (app/config,config,Resources/config) that is loaded. 将您的yml文件放入已加载的任何配置目录(app / config,config,Resources / config)中。 How you name the file does not 'really' matter. 您如何命名文件并不重要。 What matter is that your root node in this yml is the root node you define in /your-bundle/src/DependencyInjection/Configuration.php 重要的是,此yml中的根节点是您在/your-bundle/src/DependencyInjection/Configuration.php中定义的根节点

if you defined it like this : 如果您这样定义它:

public function getConfigTreeBuilder()
{
    $treeBuilder = new TreeBuilder('your_root_node' );
    $treeBuilder->getRootNode()... 

    // with use of array prototypes... 

    $treeBuilder = new TreeBuilder('your_root_node');
    $rootNode = $treeBuilder->getRootNode();

    $rootNode
        ->children()
            ->arrayNode('fields')
                ->arrayPrototype()
                    ->children()
                        ->enumNode('type')
                            ->values(['iris','iri','entity','entities','datetime','string','integer','boolean','custom_class'])
                            ->defaultNull()
                        ->end()
                        ->enumNode('type_out')
                            ->values(['iris','iri','entity','entities','datetime','string','integer','boolean','custom_class'])
                            ->defaultNull()
                        ->end()
                        ->scalarNode('entity')->defaultNull()->end()
                        ->scalarNode('customClass')->defaultNull()->end()
                        ->arrayNode('parameters')
                            ->useAttributeAsKey('name')
                            ->scalarPrototype()->end()
                        ->end()
                        ->booleanNode('nullable')->defaultFalse()->end()
                    ->end()
                ->end()
            ->end()
        ->end()
    ;

    ...
}

Then you should have a yml file starting like this: 然后,您应该有一个像这样开始的yml文件:

your_root_node:
    my_parameter:
    my_other_parameter:
        and_so_on:

Also, config file with definition don't have to be loaded with kernel and can be loaded 'lazily' in a service like this: 同样,具有定义的配置文件也不必加载内核,并且可以“懒惰”地加载到这样的服务中:

    use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Processor;
class SchemaConfiguration implements ConfigurationInterface
{
    /**
     * {@inheritdoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('schema');
        $rootNode
            ... // Tree definition
            ->end()
        ;
        return $treeBuilder;
    }
    /**
     * @param $schemaConfig
     * @return mixed
     */
    public function getSchemaFromYaml($schemaConfig){
        $configuration = new SchemaConfiguration();
        $processor = new Processor();
        $processed = $processor->processConfiguration($configuration, $schemaConfig);
        $schema = $processed['fields'];
        return $schema ;
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM