php - Exactly how does the defaultValue work in Symfony 2.0 NodeDefinition?

715

I'm trying to expose the semantic configuration within a bundle in Symfony 2.0 but I'm having trouble getting the defaultValue to work in the NodeDefinition class. I generated an empty bundle and created the necessary files for the Configuration to work and I am able to get the config value, but I want to set a defaultValue to configuration item. I use the defaultValue() method and remove the configuration item from my config.yml and then it shows up an empty array? Can anyone explain how the defaultValue() actually works, am I missing something?

<?php
// ./DependencyInjection/Configuration.php

namespace Test\Bundle\TestBundle\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('test_bundle');

        $rootNode
            ->children()
                ->scalarNode('foo')->defaultValue('bar')->end()
            ->end();

        return $treeBuilder;
    }
}

-

<?php
// ./DependencyInjection/TestBundleExtension.php

namespace Test\Bundle\TestBundle\DependencyInjection;

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

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

        var_dump($configs); // empty array

        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.xml');
    }
}

So from the above Configuration class surely when the config item 'test_bundle.foo' is missing, it's value will be set to 'bar'... yes? Well that's what I thought but it doesn't.

212

Answer

Solution:

Your root node is an array node. By default, if you dont set a key, default value is not applied. 2 examples on how to get your default value working:

  1. Set the value as null:

    # in the config.yml
    test_bundle:
        foo: ~
    
  2. Tell your root node to use default values if not set:

    // in your Configuration.php
    $rootNode
        ->addDefaultsIfNotSet()
        ->children()
            ->scalarNode('foo')->defaultValue('bar')->end()
        ->end();
    
    # in the config.yml
    test_bundle: ~
    

People are also looking for solutions to the problem: regex - How to make PHP BB Codes with RegExp?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.