|
| 1 | +.. index:: |
| 2 | +single: Configuration |
| 3 | + |
| 4 | +Configuration Usages |
| 5 | +==================== |
| 6 | + |
| 7 | +Parameters are common in configuration such as ``%kernel.root_dir%`` |
| 8 | +(which is used for passing a path) or ``%kernel.debug%`` (which is used for |
| 9 | +passing a boolean indicating if mode is debug). The syntax wrapping an |
| 10 | +alias string with ``%`` conveys that this is a value defined as a parameter |
| 11 | +and will be evaluated somewhere down the line and used. Within Symfony |
| 12 | +we can have parameters defined in a bundle and used only in that bundle and |
| 13 | +also parameters defined in a bundle and used in another. Usually they would |
| 14 | +have a namespace like ``demo_bundle.service_doer.local_parameter`` making |
| 15 | +explicit where this parameter comes from. If the parameter is global then |
| 16 | +it may not have a namespace, e.g. ``%some_global_option_here%``. |
| 17 | + |
| 18 | +We can have parameters being used inside ``parameters.yml``: |
| 19 | + |
| 20 | +.. configuration-block:: |
| 21 | + |
| 22 | + .. code-block:: yaml |
| 23 | +
|
| 24 | + # app/config/parameters.yml |
| 25 | + parameters: |
| 26 | + payment_test_mode: %kernel.root_dir% |
| 27 | +
|
| 28 | +Inside ``config.yml`` and other configuration files building larger |
| 29 | +strings: |
| 30 | + |
| 31 | +.. configuration-block:: |
| 32 | + |
| 33 | + .. code-block:: yaml |
| 34 | +
|
| 35 | +
|
| 36 | + # app/config/config.yml |
| 37 | + my_bundle: |
| 38 | + local: |
| 39 | + directory: %kernel.root_dir%/../web/media/image |
| 40 | +
|
| 41 | +We can also use parameters in service configuration files: |
| 42 | + |
| 43 | +.. configuration-block:: |
| 44 | + |
| 45 | + .. code-block:: xml |
| 46 | +
|
| 47 | + <parameters> |
| 48 | + <parameter id="some_service.class" class="\Acme\DemoBundle\Service\Doer"> |
| 49 | + </parameters> |
| 50 | +
|
| 51 | + <services> |
| 52 | + <service id="%some_service%" class="%some_service.class%" /> |
| 53 | + </services> |
| 54 | +
|
| 55 | +For more information on how parameters are used in Symfony please see |
| 56 | +:ref:`parameters <book-service-container-parameters>`. |
| 57 | + |
| 58 | +Besides these usages above we can use this syntax in routing files and handle |
| 59 | +parameters in special cases as discussed below. |
| 60 | + |
| 61 | +If for instance, there is a use case in which we want to use the |
| 62 | +``%kernel.debug%`` debug mode parameter to make our bundle adapt its |
| 63 | +configuration depending on this. For this case we cannot use |
| 64 | +the syntax directly and expect this to work. The configuration handling |
| 65 | +will just tread this ``%kernel.debug%`` as a string. Let's consider |
| 66 | +this example with ``AcmeDemoBundle``: |
| 67 | + |
| 68 | +.. code-block:: php |
| 69 | +
|
| 70 | + // Inside Configuration class |
| 71 | + ->booleanNode('logging')->defaultValue('%kernel.debug%')->end() |
| 72 | +
|
| 73 | + // Inside the Extension class |
| 74 | + $config = $this->processConfiguration($configuration, $configs); |
| 75 | + var_dump($config['logging']); |
| 76 | +
|
| 77 | +Now let's examine the results to see this closely: |
| 78 | + |
| 79 | +.. configuration-block:: |
| 80 | + |
| 81 | + .. code-block:: xml |
| 82 | +
|
| 83 | + my_bundle: |
| 84 | + logging: true |
| 85 | + # true, as expected |
| 86 | +
|
| 87 | + my_bundle: |
| 88 | + logging: %kernel.debug% |
| 89 | + # true/false (depends on 2nd parameter of AppKernel), |
| 90 | + # as expected, because %kernel.debug% inside configuration |
| 91 | + # gets evaluated before being passed to the extension |
| 92 | +
|
| 93 | + my_bundle: ~ |
| 94 | + # passes the string ``%kernel.debug%``. |
| 95 | + # Which is always considered as true. |
| 96 | + # Configurator class does not know anything about the default |
| 97 | + # string ``%kernel.debug%``. |
| 98 | +
|
| 99 | +In order to support this use case ``Configuration`` class has to |
| 100 | +be injected with this parameter via the extension as follows: |
| 101 | + |
| 102 | +.. code-block:: php |
| 103 | +
|
| 104 | + <?php |
| 105 | +
|
| 106 | + namespace Acme\DemoBundle\DependencyInjection; |
| 107 | +
|
| 108 | + use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
| 109 | + use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
| 110 | + use Symfony\Component\Config\Definition\ConfigurationInterface; |
| 111 | +
|
| 112 | + class Configuration implements ConfigurationInterface |
| 113 | + { |
| 114 | + private $debug; |
| 115 | +
|
| 116 | + /** |
| 117 | + * Constructor |
| 118 | + * |
| 119 | + * @param Boolean $debug Whether to use the debug mode |
| 120 | + */ |
| 121 | + public function __construct($debug) |
| 122 | + { |
| 123 | + $this->debug = (Boolean) $debug; |
| 124 | + } |
| 125 | +
|
| 126 | + /** |
| 127 | + * {@inheritDoc} |
| 128 | + */ |
| 129 | + public function getConfigTreeBuilder() |
| 130 | + { |
| 131 | + $treeBuilder = new TreeBuilder(); |
| 132 | + $rootNode = $treeBuilder->root('acme_demo'); |
| 133 | +
|
| 134 | + $rootNode |
| 135 | + // ... |
| 136 | + ->booleanNode('logging')->defaultValue($this->debug)->end() |
| 137 | + // ... |
| 138 | + ; |
| 139 | +
|
| 140 | + return $treeBuilder; |
| 141 | + } |
| 142 | + } |
| 143 | +
|
| 144 | +And set it in the constructor of ``Configuration`` via the Extension class: |
| 145 | + |
| 146 | +.. code-block:: php |
| 147 | +
|
| 148 | + <?php |
| 149 | +
|
| 150 | + namespace Acme\DemoBundle\DependencyInjection; |
| 151 | +
|
| 152 | + use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
| 153 | + use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 154 | + use Symfony\Component\DependencyInjection\Definition; |
| 155 | + use Symfony\Component\Config\FileLocator; |
| 156 | +
|
| 157 | + class AcmeDemoExtension extends AbstractDoctrineExtension |
| 158 | + { |
| 159 | + // ... |
| 160 | +
|
| 161 | + /** |
| 162 | + * {@inheritDoc} |
| 163 | + */ |
| 164 | + public function getConfiguration(array $config, ContainerBuilder $container) |
| 165 | + { |
| 166 | + return new Configuration($container->getParameter('kernel.debug')); |
| 167 | + } |
| 168 | + } |
| 169 | +
|
| 170 | +There are some instances of ``%kernel.debug%`` usage within a ``Configurator`` |
| 171 | +class in ``TwigBundle`` and ``AsseticBundle``, however this is because they are |
| 172 | +setting the parameter value in the container via the Extension class. For |
| 173 | +example in ``AsseticBundle`` we have: |
| 174 | + |
| 175 | +.. code-block:: php |
| 176 | +
|
| 177 | + $container->setParameter('assetic.debug', $config['debug']); |
| 178 | +
|
| 179 | +The string ``%kernel.debug%`` passed here as an argument handles the |
| 180 | +interpreting job to the container which in turn does the evaluation. |
| 181 | +Both ways accomplish similar goals. ``AsseticBundle`` will not use |
| 182 | +anymore ``%kernel.debug%`` but rather the new ``%assetic.debug%`` parameter. |
0 commit comments