【问题标题】:Symfony: How to load custom bundle config from file?Symfony:如何从文件中加载自定义捆绑配置?
【发布时间】:2019-10-26 19:03:49
【问题描述】:

我正在使用Symfony 3.4。如何将自定义配置文件添加到应包含名称和正则表达式列表的自定义包中?例如:

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


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

在 Symfony 文档中,我找到了有关将自定义配置添加到 app/configMyBundle/Resources/config 目录中的 .yml 文件的信息。然而,这不是关于一小组定义良好的参数(例如mailaddress: xyz),而是任意数量的条目列表。

此外,配置应该仅在实际使用时才加载,而不是在每次创建内核或服务时加载。

当然,我可以简单地使用file_get_contents 或任何类似的 PHP 方法来加载任何文件,但是这似乎很 hacky 而不是 “Symfony 方式”...

那么,“正确”的做法是什么?

编辑:

yml 文件非常适合配置参数,但这不是关于参数(= key + value),而是文件名和正则表达式的列表。该列表没有有定义的长度,条目没有有定义的名称。因此 .yml 在这里不是正确的解决方案,是吗?

另外,使用 ConfigTreeBuilder 添加的配置文件会在内核加载时加载,不是吗?我正在寻找一种仅在需要时才加载列表的惰性解决方案。

【问题讨论】:

  • 我不确定是否完全理解您的问题,但您不能使用 yaml imports 吗?例如:imports: - { resource: '%kernel.project_dir%/YourBundle/Resources/config.yaml' }
  • 这适用于key: value 样式参数,但此处并非如此。请看我的编辑。
  • file_get_contents 可能是您想要满足您的特定要求的内容。标题中的“配置”一词可能不是最好的词。好像您正在加载数据。

标签: php symfony


【解决方案1】:

将您的 yml 文件放在任何已加载的配置目录(app/config、config、Resources/config)中。您如何命名文件并不“真正”重要。重要的是您在此 yml 中的根节点是您在其中定义的根节点 /your-bundle/src/DependencyInjection/Configuration.php

如果你是这样定义的:

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()
    ;

    ...
}

那么你应该有一个像这样开头的 yml 文件:

your_root_node:
    my_parameter:
    my_other_parameter:
        and_so_on:

此外,具有定义的配置文件不必与内核一起加载,并且可以在这样的服务中“延迟”加载:

    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 ;
    }


}

【讨论】:

  • 这适用于key: value 样式参数,这里不是这种情况。请看我的编辑。
  • 我编辑了我的答案,添加了使用数组原型定义值或键/值列表的示例
  • 来吧,我想这就是你要找的东西
猜你喜欢
  • 2014-05-03
  • 1970-01-01
  • 2013-09-10
  • 2010-10-05
  • 2012-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多