Using Parameters within a Dependency Injection Class

Using Parameters within a Dependency Injection Class

You have seen how to use configuration parameters within Symfony service containers. There are special cases such as when you want, for instance, to use the %kernel.debug% parameter to make the services in your bundle enter debug mode. For this case there is more work to do in order to make the system understand the parameter value. By default, your parameter %kernel.debug% will be treated as a string. Consider the following example:

  1. // inside Configuration class
  2. $rootNode
  3. ->children()
  4. ->booleanNode('logging')->defaultValue('%kernel.debug%')->end()
  5. // ...
  6. ->end()
  7. ;
  8. // inside the Extension class
  9. $config = $this->processConfiguration($configuration, $configs);
  10. var_dump($config['logging']);

Now, examine the results to see this closely:

  • YAML

    1. my_bundle:
    2. logging: true
    3. # true, as expected
    4. my_bundle:
    5. logging: '%kernel.debug%'
    6. # true/false (depends on 2nd argument of the Kernel class),
    7. # as expected, because %kernel.debug% inside configuration
    8. # gets evaluated before being passed to the extension
    9. my_bundle: ~
    10. # passes the string "%kernel.debug%".
    11. # Which is always considered as true.
    12. # The Configurator does not know anything about
    13. # "%kernel.debug%" being a parameter.
  • XML

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <container xmlns="http://symfony.com/schema/dic/services"
    3. xmlns:my-bundle="http://example.org/schema/dic/my_bundle"
    4. xsi:schemaLocation="http://symfony.com/schema/dic/services
    5. https://symfony.com/schema/dic/services/services-1.0.xsd
    6. http://example.org/schema/dic/my_bundle
    7. https://example.org/schema/dic/my_bundle/my_bundle-1.0.xsd">
    8. <my-bundle:config logging="true"/>
    9. <!-- true, as expected -->
    10. <my-bundle:config logging="%kernel.debug%"/>
    11. <!-- true/false (depends on 2nd parameter of Kernel),
    12. as expected, because %kernel.debug% inside configuration
    13. gets evaluated before being passed to the extension -->
    14. <my-bundle:config/>
    15. <!-- passes the string "%kernel.debug%".
    16. Which is always considered as true.
    17. The Configurator does not know anything about
    18. "%kernel.debug%" being a parameter. -->
    19. </container>
  • PHP

    1. $container->loadFromExtension('my_bundle', [
    2. 'logging' => true,
    3. // true, as expected
    4. ]
    5. );
    6. $container->loadFromExtension('my_bundle', [
    7. 'logging' => "%kernel.debug%",
    8. // true/false (depends on 2nd parameter of Kernel),
    9. // as expected, because %kernel.debug% inside configuration
    10. // gets evaluated before being passed to the extension
    11. ]
    12. );
    13. $container->loadFromExtension('my_bundle');
    14. // passes the string "%kernel.debug%".
    15. // Which is always considered as true.
    16. // The Configurator does not know anything about
    17. // "%kernel.debug%" being a parameter.

In order to support this use case, the Configuration class has to be injected with this parameter via the extension as follows:

  1. namespace App\DependencyInjection;
  2. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  3. use Symfony\Component\Config\Definition\ConfigurationInterface;
  4. class Configuration implements ConfigurationInterface
  5. {
  6. private $debug;
  7. public function __construct($debug)
  8. {
  9. $this->debug = (bool) $debug;
  10. }
  11. public function getConfigTreeBuilder()
  12. {
  13. $treeBuilder = new TreeBuilder('my_bundle');
  14. $treeBuilder->getRootNode()
  15. ->children()
  16. // ...
  17. ->booleanNode('logging')->defaultValue($this->debug)->end()
  18. // ...
  19. ->end()
  20. ;
  21. return $treeBuilder;
  22. }
  23. }

And set it in the constructor of Configuration via the Extension class:

  1. namespace App\DependencyInjection;
  2. use Symfony\Component\DependencyInjection\ContainerBuilder;
  3. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  4. class AppExtension extends Extension
  5. {
  6. // ...
  7. public function getConfiguration(array $config, ContainerBuilder $container)
  8. {
  9. return new Configuration($container->getParameter('kernel.debug'));
  10. }
  11. }

Tip

There are some instances of %kernel.debug% usage within a Configurator class for example in TwigBundle. However, this is because the default parameter value is set by the Extension class.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.