1

Under Defining and Processing Configuration Values in the documentation for the Symfony Config component, it's implied that the root node name you pass to the TreeBuilder constructor will be seen as a node at the root of the configurations the processor is passed, rather than the root of the objects themselves.

It certainly seems like things are handled this way when loading files into a container, but the following code follows what I understand the documentation to be describing, yet throws an InvalidConfigurationException claiming Unrecognized option "foo" under "foo". Available option is "bar". on all currently supported versions of Symfony.

I'm baffled; can anyone, please, help me understand what's going on here?

class Config implements \Symfony\Component\Config\Definition\ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $builder = new \Symfony\Component\Config\Definition\Builder\TreeBuilder('foo');
        $builder->getRootNode()
            ->children()
                ->scalarNode('bar')->end()
            ->end();

        return $builder;
    }
}

$config    = new Config();
$processor = new \Symfony\Component\Config\Definition\Processor();
$a         = ['foo' => ['bar' => 'a']];
$b         = ['foo' => ['bar' => 'b']];
$c         = $processor->processConfiguration($config, [$a, $b]);
Andrew Dinmore
  • 804
  • 7
  • 14
  • 1
    The [example in the docs](https://symfony.com/doc/current/components/config/definition.html#processing-configuration-values) is either incorrect or misleading. The processor assumes the top level array key is already stripped off. At least that is what the Extension::load method receives. So use: $processor->processConfiguration($config, [['bar' => 'a'],['bar' => 'b']]); – Cerad Sep 15 '21 at 02:22
  • Thanks, glad I'm not going crazy. It begs the question whether the documentation or component functionality is at fault though, especially given the root node name being a required argument. Guess that's more of a discussion for an issue thread than here. – Andrew Dinmore Sep 15 '21 at 09:54
  • Yep. As far as I can tell the TreeBuilder name is not actually used anywhere. At least in simple cases. The framework uses the bundle's alias to find relevant configuration information. I took one of my bundle's Configuration objects, renamed the root TreeBuilder and it all still seems to work. I think it might be used when building nested trees. – Cerad Sep 15 '21 at 12:57

0 Answers0