2013-03-10 10 views

回答

4

是的,种:

<?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'); 
    } 
} 

从我可以看到$configs是未合并配置和默认值的阵列中不包括(由配置TreeBuilder作为定义的值)。

herehere

+0

我想这是尽善尽美 – Jens 2013-07-22 16:08:52

+1

据我可以看到这只适用的配置config.yml explicetly设置。什么是默认的配置值?有没有其他方式访问它们?我想这只有在'acme_demo'已经被编译后才有可能。 – acme 2013-09-12 09:34:26

2

我意识到这是一个老帖子,但我一直在寻找同样的信息,并最终发现,这个工程的一个参数:

$cfgVal = $container 
    ->getParameterBag() 
    ->resolveValue($container->getParameter('param_name')); 

当然这是可能在原始帖子后添加了此功能。

+0

是的,我认为这是新的。太糟糕了,我不记得为什么我首先需要这个。 – Jens 2015-07-10 00:09:32

+0

真棒,这真的救了我! – 2015-11-28 23:13:39

4

只是为了完整起见,以@彼得的回答是: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); 
    } 
} 
相关问题