如何在 DI 扩展类中加载、处理和使用 Yaml 配置文件中的自定义参数? [英] How to load, process and use custom parameters from Yaml configuration files in DI Extension class?

查看:14
本文介绍了如何在 DI 扩展类中加载、处理和使用 Yaml 配置文件中的自定义参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照此处提供的文档在我的应用程序中导入 yaml 配置文件 http://symfony.com/doc/current/bundles/extension.html 但我总是收到错误消息:

I'm trying to import a yaml configuration file in my App following the documentation provided here http://symfony.com/doc/current/bundles/extension.html but I always have the error message:

没有扩展能够加载app"的配置

There is no extension able to load the configuration for "app"

我的文件位于:config/packages/app.yaml,结构如下:

My file is located here : config/packages/app.yaml and has the following structure :

app:  
    list:  
        model1:  
            prop1: value1
            prop2: value2  
        model2:
            ...

由于这是一个简单的应用程序,所有文件都在 src/ 中.所以我有 src/DependencyInjection/AppExtension.php

As this is a simple App, all the files are in src/. So I have src/DependencyInjection/AppExtension.php

<?php

namespace AppDependencyInjection;

use SymfonyComponentDependencyInjectionContainerBuilder;
use SymfonyComponentConfigFileLocator;
use SymfonyComponentHttpKernelDependencyInjectionExtension;
use SymfonyComponentDependencyInjectionLoader;

class AppExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);
    }
}

还有src/DependencyInjection/Configuration.php

<?php

namespace AppDependencyInjection;

use SymfonyComponentConfigDefinitionBuilderTreeBuilder;
use SymfonyComponentConfigDefinitionConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('app');

        // Node definition
        $rootNode
            ->children()
                ->arrayNode('list')
                    ->useAttributeAsKey('name')
                    ->requiresAtLeastOneElement()
                    ->prototype('array')
                        ->children()
                            ->requiresAtLeastOneElement()
                            ->prototype('scalar')
                            ->end()
                        ->end()
                    ->end()
                ->end()
            ->end();

        return $treeBuilder;
    }
}

我无法访问我的参数:(
有什么想法吗?

I'm not able to access my parameters :(
Any idea?

推荐答案

如果您想加载自定义配置文件以使用扩展类处理其参数(类似于 Symfony 捆绑扩展但不创建捆绑),要最终创建"并将其中的一个或多个添加到容器"中(在编译之前),您可以在 configureContainer<中手动注册扩展类/code> 方法包含在 Kernel.php 文件中:

If you want to load a custom configuration file to process it's parameters using an Extension class (like in Symfony bundle extension but without to create a bundle), to eventually "create" and add one or more of it to the "container" (before it will be compiled) you can register your Extension class manually in the configureContainer method contained in the Kernel.php file:

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
{
    // to avoid the same error you need to put this line at the top
    // if your file is stored under "$this->getProjectDir().'/config'" directory
    $container->registerExtension(new YourAppExtensionClass());

    // ----- rest of the code
}

然后您可以像往常一样使用您的参数注册编译器通行证.

Then you can use your params as usual registering a Compiler Pass.

希望这会有所帮助.

这篇关于如何在 DI 扩展类中加载、处理和使用 Yaml 配置文件中的自定义参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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