> Symfony中文手册 > 在依赖注入类内部使用参数

在依赖注入类内部使用参数

你已经看到了如何在Symfony服务容器内部使用配置参数。比如你可能想在这里出现一个特殊的情况,在你Bundle的服务中使用%kernel.debug%参数来进入debug模式。对于这种情况,有很多的工作要做,以便使系统了解参数值。默认情况下,你的参数%kernel.debug%将被视为是一个简单的字符串。看看下面这个例子:

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

现在,进一步看看来检查这个结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
my_bundle:
    logging: true
    # true, as expected
    # ture,正如预期

my_bundle:
    logging: '%kernel.debug%'
    # true/false (depends on 2nd parameter of AppKernel),
    # true/false (取决于AppKerenl的第二个参数),
    # as expected, because %kernel.debug% inside configuration
    # gets evaLuated before being passed to the extension
    # 正如预期,因为在被传入到扩展之前,%kernel.debug%在configuration内部已经被评估

my_bundle: ~
# passes the string "%kernel.debug%".
# 传入这个字符串%kernel.debug%。
# Which is always considered as true.
# 总是被看作是true。
# The Configurator does not know anything about
# "%kernel.debug%" being a parameter.
# 这个配置器要做的就是不知道关于"%kernel.debug%"参数的任何事。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="Http://symfony.com/schema/dic/services"
    xmlns:my-bundle="http://example.org/schema/dic/my_bundle">
 
    <my-bundle:config logging="true" />
    <!-- true, as expected -->
 
     <my-bundle:config logging="%kernel.debug%" />
     <!-- true/false (depends on 2nd parameter of AppKernel),
          as expected, because %kernel.debug% inside configuration
          gets evaluated before being passed to the extension -->
 
    <my-bundle:config />
    <!-- passes the string "%kernel.debug%".
         Which is always considered as true.
         The Configurator does not know anything about
         "%kernel.debug%" being a parameter. -->
</container>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$container->loadFromExtension('my_bundle', array(
        'logging' => true,
        // true, as expected
    )
);
 
$container->loadFromExtension('my_bundle', array(
        'logging' => "%kernel.debug%",
        // true/false (depends on 2nd parameter of AppKernel),
        // as expected, because %kernel.debug% inside configuration
        // gets evaluated before being passed to the extension
    )
);
 
$container->loadFromExtension('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.

为了支持这个用例(译注:让my_bundle: ~模式下不解析成字符串),Configuration 类必须通过以下的扩展来注入这个参数:

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
30
namespace AppBundle\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();
        $rootNode = $treeBuilder->root('my_bundle');
 
        $rootNode
            ->children()
                // ...
                ->booleanNode('logging')->defaultValue($this->debug)->end()
                // ...
            ->end()
        ;
 
        return $treeBuilder;
    }
}

并且通过 Extension 类来把它设置到 Configuration 的构造器中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace AppBundle\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'));
    }
}

设置扩展(Extension)中的默认值

在 TwigBundle 和 AsseticBundle 里,有一些%kernel.debug%的用法,被使用在Configurator类内。然而,这是因为这个默认参数值是由 扩展(Extension) 类设置的。例如在AsseticBundle中,你可以找到:

1
$container->setParameter('assetic.debug', $config['debug']);

作为一个参数而传到此处的%kernel.debug%字符串,处理的是到这个容器的解析工作,接下来容器要负责评估。TwigBundle 和 AsseticBundle 里的两种方法都实现了类似的目标。AsseticBundle 不使用%kernel.debug%,而是新的%assetic.debug%参数。