|
| 1 | +.. index:: |
| 2 | + single: Using Parameters within a Dependency Injection Class |
| 3 | + |
| 4 | +Using Parameters within a Dependency Injection Class |
| 5 | +---------------------------------------------------- |
| 6 | + |
| 7 | +You have seen how to use configuration parameters within |
| 8 | +:ref:`Symfony service containers <book-service-container-parameters>`. |
| 9 | +There are special cases such as when you want, for instance, to use the |
| 10 | +``%kernel.debug%`` parameter to make the services in your bundle enter |
| 11 | +debug mode. For this case there is more work to do in order |
| 12 | +to make the system understand the parameter value. By default |
| 13 | +your parameter ``%kernel.debug%`` will be treated as a |
| 14 | +simple string. Consider this example with the AcmeDemoBundle:: |
| 15 | + |
| 16 | + // Inside Configuration class |
| 17 | + $rootNode |
| 18 | + ->children() |
| 19 | + ->booleanNode('logging')->defaultValue('%kernel.debug%')->end() |
| 20 | + // ... |
| 21 | + ->end() |
| 22 | + ; |
| 23 | + |
| 24 | + // Inside the Extension class |
| 25 | + $config = $this->processConfiguration($configuration, $configs); |
| 26 | + var_dump($config['logging']); |
| 27 | + |
| 28 | +Now, examine the results to see this closely: |
| 29 | + |
| 30 | +.. configuration-block:: |
| 31 | + |
| 32 | + .. code-block:: yaml |
| 33 | +
|
| 34 | + my_bundle: |
| 35 | + logging: true |
| 36 | + # true, as expected |
| 37 | +
|
| 38 | + my_bundle: |
| 39 | + logging: %kernel.debug% |
| 40 | + # true/false (depends on 2nd parameter of AppKernel), |
| 41 | + # as expected, because %kernel.debug% inside configuration |
| 42 | + # gets evaluated before being passed to the extension |
| 43 | +
|
| 44 | + my_bundle: ~ |
| 45 | + # passes the string "%kernel.debug%". |
| 46 | + # Which is always considered as true. |
| 47 | + # The Configurator does not know anything about |
| 48 | + # "%kernel.debug%" being a parameter. |
| 49 | +
|
| 50 | + .. code-block:: xml |
| 51 | +
|
| 52 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 53 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 54 | + xmlns:my-bundle="http://example.org/schema/dic/my_bundle"> |
| 55 | +
|
| 56 | + <my-bundle:config logging="true" /> |
| 57 | + <!-- true, as expected --> |
| 58 | +
|
| 59 | + <my-bundle:config logging="%kernel.debug%" /> |
| 60 | + <!-- true/false (depends on 2nd parameter of AppKernel), |
| 61 | + as expected, because %kernel.debug% inside configuration |
| 62 | + gets evaluated before being passed to the extension --> |
| 63 | +
|
| 64 | + <my-bundle:config /> |
| 65 | + <!-- passes the string "%kernel.debug%". |
| 66 | + Which is always considered as true. |
| 67 | + The Configurator does not know anything about |
| 68 | + "%kernel.debug%" being a parameter. --> |
| 69 | + </container> |
| 70 | +
|
| 71 | + .. code-block:: php |
| 72 | +
|
| 73 | + $container->loadFromExtension('my_bundle', array( |
| 74 | + 'logging' => true, |
| 75 | + // true, as expected |
| 76 | + ) |
| 77 | + ); |
| 78 | +
|
| 79 | + $container->loadFromExtension('my_bundle', array( |
| 80 | + 'logging' => "%kernel.debug%", |
| 81 | + // true/false (depends on 2nd parameter of AppKernel), |
| 82 | + // as expected, because %kernel.debug% inside configuration |
| 83 | + // gets evaluated before being passed to the extension |
| 84 | + ) |
| 85 | + ); |
| 86 | +
|
| 87 | + $container->loadFromExtension('my_bundle'); |
| 88 | + // passes the string "%kernel.debug%". |
| 89 | + // Which is always considered as true. |
| 90 | + // The Configurator does not know anything about |
| 91 | + // "%kernel.debug%" being a parameter. |
| 92 | +
|
| 93 | +In order to support this use case, the ``Configuration`` class has to |
| 94 | +be injected with this parameter via the extension as follows:: |
| 95 | + |
| 96 | + namespace Acme\DemoBundle\DependencyInjection; |
| 97 | + |
| 98 | + use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
| 99 | + use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
| 100 | + use Symfony\Component\Config\Definition\ConfigurationInterface; |
| 101 | + |
| 102 | + class Configuration implements ConfigurationInterface |
| 103 | + { |
| 104 | + private $debug; |
| 105 | + |
| 106 | + public function __construct($debug) |
| 107 | + { |
| 108 | + $this->debug = (Boolean) $debug; |
| 109 | + } |
| 110 | + |
| 111 | + public function getConfigTreeBuilder() |
| 112 | + { |
| 113 | + $treeBuilder = new TreeBuilder(); |
| 114 | + $rootNode = $treeBuilder->root('acme_demo'); |
| 115 | + |
| 116 | + $rootNode |
| 117 | + ->children() |
| 118 | + // ... |
| 119 | + ->booleanNode('logging')->defaultValue($this->debug)->end() |
| 120 | + // ... |
| 121 | + ->end() |
| 122 | + ; |
| 123 | + |
| 124 | + return $treeBuilder; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | +And set it in the constructor of ``Configuration`` via the ``Extension`` class:: |
| 129 | + |
| 130 | + namespace Acme\DemoBundle\DependencyInjection; |
| 131 | + |
| 132 | + use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 133 | + use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
| 134 | + use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
| 135 | + use Symfony\Component\Config\FileLocator; |
| 136 | + |
| 137 | + class AcmeDemoExtension extends Extension |
| 138 | + { |
| 139 | + // ... |
| 140 | + |
| 141 | + public function getConfiguration(array $config, ContainerBuilder $container) |
| 142 | + { |
| 143 | + return new Configuration($container->getParameter('kernel.debug')); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | +.. sidebar:: Setting the Default in the Extension |
| 148 | + |
| 149 | + There are some instances of ``%kernel.debug%`` usage within a ``Configurator`` |
| 150 | + class in TwigBundle and AsseticBundle, however this is because the default |
| 151 | + parameter value is set by the Extension class. For example in AsseticBundle, |
| 152 | + you can find:: |
| 153 | + |
| 154 | + $container->setParameter('assetic.debug', $config['debug']); |
| 155 | + |
| 156 | + The string ``%kernel.debug%`` passed here as an argument handles the |
| 157 | + interpreting job to the container which in turn does the evaluation. |
| 158 | + Both ways accomplish similar goals. AsseticBundle will not use |
| 159 | + ``%kernel.debug%`` but rather the new ``%assetic.debug%`` parameter. |
0 commit comments