Jen*_*ens 5 configuration dependency-injection symfony
我有一个bundle的语义配置,需要在编译器传递期间为同一个bundle解释.
是否可以在不将其存储在中间容器变量中的情况下访问它?
是的,有点:
<?php
namespace Acme\DemoBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class CompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$configs = $container->getExtensionConfig('acme_demo');
}
}
Run Code Online (Sandbox Code Playgroud)
从我可以看到的$configs是一组未合并的配置和默认值不包括(由配置TreeBuilder定义的值).
只是为了完整性@ Peter的答案:getExtensionConfig返回一个数组数组,应该使用相应的处理Configuration来访问默认值.
<?php
namespace Acme\DemoBundle\DependencyInjection;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
class CompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$configs = $container->getExtensionConfig('acme_demo');
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
/// You can safely work with $config now
}
private function processConfiguration(ConfigurationInterface $configuration, array $configs)
{
$processor = new Processor();
return $processor->processConfiguration($configuration, $configs);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2234 次 |
| 最近记录: |