2013-03-10 11 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); 
    } 
} 
相關問題