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:

Symfony サービス コンテナー内で構成パラメーターを使用する方法を見てきました。たとえば、%kernel.debug% パラメーターを使用してバンドル内のサービスをデバッグ モードにする場合など、特別なケースがあります。この場合、システムにパラメータ値を理解させるために、さらに作業が必要です。デフォルトでは、パラメータ %kernel.debug% は文字列として扱われます。次の例を検討してください。
1
2
3
4
5
6
7
8
9
10
11
// inside Configuration class
$rootNode
    ->children()
        ->booleanNode('logging')->defaultValue('%kernel.debug%')->end()
        // ...
    ->end()
;

// inside the Extension class
$config = $this->processConfiguration($configuration, $configs);
var_dump($config['logging']);

Now, examine the results to see this closely:

次に、結果を調べて、これを詳しく確認します。
  • YAML
    YAML
  • XML
    XML
  • PHP
    PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
my_bundle:
    logging: true
    # true, as expected

my_bundle:
    logging: '%kernel.debug%'
    # true/false (depends on 2nd argument of the Kernel class),
    # as expected, because %kernel.debug% inside configuration
    # gets evaluated before being passed to the extension

my_bundle: ~
# passes the string "%kernel.debug%".
# Which is always considered as true.
# The Configurator does not know anything about
# "%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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace App\DependencyInjection;

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

class Configuration implements ConfigurationInterface
{
    private $debug;

    public function __construct($debug)
    {
        $this->debug = (bool) $debug;
    }

    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder('my_bundle');

        $treeBuilder->getRootNode()
            ->children()
                // ...
                ->booleanNode('logging')->defaultValue($this->debug)->end()
                // ...
            ->end()
        ;

        return $treeBuilder;
    }
}

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

そして、Extension クラスを介して Configuration のコンストラクターに設定します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace App\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class AppExtension extends Extension
{
    // ...

    public function getConfiguration(array $config, ContainerBuilder $container)
    {
        return new Configuration($container->getParameter('kernel.debug'));
    }
}

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.

TwigBundle など、Configurator クラス内で %kernel.debug% を使用するインスタンスがいくつかあります。ただし、これは、既定のパラメーター値が Extension クラスによって設定されるためです。