Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load, process and use custom parameters from Yaml configuration files in DI Extension class?

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:

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

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

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

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

<?php

namespace App\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

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

And src/DependencyInjection/Configuration.php

<?php

namespace App\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

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?

like image 773
ArGh Avatar asked Dec 21 '17 20:12

ArGh


People also ask

How do I open a config YAML file?

Click the folder icon in the top left of the file editor window to open the file browser sidebar. Click the configuration.yaml file (in the /config/ folder) to load it into the main file editor window.

How do I read a YAML config file in Python?

Reading a key from YAML config file We can read the data using yaml. load() method which takes file pointer and Loader as parameters. FullLoader handles the conversion from YAML scalar values to the Python dictionary. The index [0] is used to select the tag we want to read.

What is configuration file in YAML?

YAML is a digestible data serialization language often used to create configuration files with any programming language. Designed for human interaction, YAML is a strict superset of JSON, another data serialization language. But because it's a strict superset, it can do everything that JSON can and more.

Why is YAML used for configuration?

YAML is popular because it is optimized for data serialization, formatted dumping, configuration files, log files, and internet messaging and filtering. Since YAML works in concurrence with any programming language, it is often used to write configuration files.


1 Answers

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.

Hope this helps.

like image 157
gp_sflover Avatar answered Sep 28 '22 01:09

gp_sflover