fracasula
9/9/2013 - 7:29 AM

Symfony2 Bundle basic parameters configuration tree (config.yml)

Symfony2 Bundle basic parameters configuration tree (config.yml)

# app/config/config.yml

acme_bundlename:
    param1:
        param1_child1: "value"
        param1_child2: "value"
    param2:
        param2_child1: "value"
        param2_child2: "value"
<?php
 
namespace Acme\Bundle\Bundlename\DependencyInjection;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
	public function indexAction()
	{
		$this->container->getParameter('acme_bundlename.param1.param1_child1'); // "value"
	}
}
<?php

namespace Acme\Bundle\Bundlename\DependencyInjection;

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

/**
 * This is the class that validates and merges configuration from your app/config files
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
 * 
 * Adding node definitions to the tree and Node types {@link http://symfony.com/doc/master/components/config/definition.html}
 */
class Configuration implements ConfigurationInterface
{
        /**
         * {@inheritDoc}
         */
        public function getConfigTreeBuilder()
        {
                $treeBuilder = new TreeBuilder();
                $rootNode = $treeBuilder->root('acme_bundlename');

                $rootNode
                        ->children()
                                ->arrayNode('param1')
                                        ->children()
                                                ->scalarNode('param1_child1')->isRequired()->end()
                                                ->scalarNode('param1_child2')->isRequired()->end()
                                        ->end()
                                ->end()
                                ->arrayNode('param2')
                                        ->children()
                                                ->scalarNode('param2_child1')->isRequired()->end()
                                                ->scalarNode('param2_child2')->isRequired()->end()
                                        ->end()
                                ->end()
                        ->end()
                ;

                return $treeBuilder;
        }
}
<?php

namespace Acme\Bundle\Bundlename\DependencyInjection;

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

/**
 * This is the class that loads and manages your bundle configuration
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
 */
class AcmeBundlenameExtension extends Extension
{
        /**
         * {@inheritDoc}
         */
        public function load(array $configs, ContainerBuilder $container)
        {
                $configuration = new Configuration();
                $config = $this->processConfiguration($configuration, $configs);

                foreach ($config as $setting => $configurationValue)
                        foreach ($configurationValue as $k => $v)
                                $container->setParameter('acme_bundlename.' . $setting . '.' . $k, $v);

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