Configuration

When creating a bundle, it is useful to configure it, which will be used as a library (and not only).

Of course, you can put everything in parameters, thus avoiding configuration, but that would not be kosher. =)

Proper practice is to describe the configuration in config.yml, and to extract some necessary parameters into parameters.yml:

# app/config/parameters.yml
parameters:
    ...
    redis_host: localhost
    redis_port: localhost
    ...
# app/config/config.yml
you:
    ...
    redis_connection:
        host: %redis_host%
        port: %redis_port%

We will see how to read such configuration.

By the way, if you describe the configuration of the bundle and do not read it, it will cause an error.

To read, in our bundle we need to describe two classes in the DependencyInjection directory: Configuration and OurBundleExtension.

In Configuration, validation takes place, and in YourBundleExtension, the loaded configuration will be available and additional processing can be performed. This issue will be covered in a separate note.

More about validation:

<?php
# src/YourVendor/YourBundle/DependensyInjection/Config.php

class Configuration implements ConfigurationInterface { public function getConfigTreeBuilder() { $treeBuilder = new TreeBuilder(); // the root method receives the key, by the name of the bundle (without the word bundle itself) $rootNode = $treeBuilder->root('your'); $rootNode ->children() ->arrayNode('redis') ->isRequired() ->children() ->scalarNode('host')->defaultValue('127.0.0.1')->end() ->integerNode('port')->defaultValue(6379)->end() ->end() ->end() ->end(); } }

Using TreeBuilder , we read and validate the configuration. Typically, there are no problems with trivial configurations. If the structure is specified, all keys, etc. - it is all simple.

If you need to specify an unlimited number of strings, it's a little different.

Let's imagine such a configuration (for example):

# app/config/config.yml
you:
    ...
    redis_connection1:
        host: %redis_host%
        port: %redis_port%
    redis_connection2:
        host: %redis_host1%
        port: %redis_port2%
    ....
    redis_connectionN:
    ...

(This example is quite spherical, but also not without meaning.) In this case, just specify prototype('array')

<?php
# src/YourVendor/YourBundle/DependensyInjection/Config.php

class Configuration implements ConfigurationInterface { public function getConfigTreeBuilder() { $treeBuilder = new TreeBuilder(); // the root method receives the key, by the name of the bundle (without the word bundle itself) $rootNode = $treeBuilder->root('your'); $rootNode ->children() ->arrayNode('redis') ->isRequired() ->prototype('array') ->children() ->scalarNode('host')->defaultValue('127.0.0.1')->end() ->integerNode('port')->defaultValue(6379)->end() ->end() -end() ->end(); } }

As already mentioned, this is just validation. A useful feature is the use of conditions:

....
$rootNode = $treeBuilder->root('your');
$rootNode
    ->children()
        ->arrayNode('redis')
            ->beforeNormalization()
                ->ifTrue(function ($v) { return is_array($v) })
                ->then(function ($v) {
                   // Addition handle here
                   return $v;
                })
            ->end()
        ->end()
...

In this way, additional processing of data can be performed, even changing the structure of the configuration, making them valid. Possible node types:

  • scalar
  • boolean
  • integer
  • float
  • enum
  • array
  • variable

The last type should be used when everything is really bad and the data cannot be validated in ordinary ways - the variable node type is not validated. And don't forget to read the documentation =) http://symfony.com/doc/current/components/config/definition.html