2013-07-16 23 views
2

看來我無法通過編譯器傳遞來獲取數據收集器。我試圖保持數據收集器可選以啓用。它在沒有標籤的YAML文件中定義,然後編譯器通過基於參數設置添加標籤。Symfony2在編譯器中添加data_collector標記pass

看起來也許編譯器傳遞已經太遲了添加標籤?

<?php 
    if ($container->getParameter('git_data_collector_enabled')) { 
     $gitDataCollectorDef = $container->getDefinition('git_data_collector'); 

     $gitDataCollectorDef->addTag('data_collector', array(
      'template' => 'Profiler:git_info_layout', 
      'id' => 'git', 
     )); 
    } 
+0

如何初始化編譯器中的整個數據收集器是否傳遞'git_data_collector_enabled'是否爲真? –

回答

0

data_collector標籤在symfony framework bundle profiler pass使用。 symfony框架捆綁中的編譯器pass通常在你的編譯器通過之前運行,因爲在你的bundle(以及bundle按順序加載)之前,你可能已經在應用內核的列表頂部附近註冊了框架bundle。

這意味着不幸的是,使用data_collector標記爲時已晚。但是你仍然可以操縱探查服務被實例化之前和使用addMethodCall方法事件探查器定義添加您git_data_collector到它:

if ($container->getParameter('git_data_collector_enabled')) { 
    //Get the profiler definition 
    $definition = $container->getDefinition('profiler'); 
    //require the definition to run the add method with a reference to your data collector when it is instantiated 
    $definition->addMethodCall('add', array(new Reference('git_data_collector'))); 

    //Add your template to the data_collector templates 
    $templates = $container->getParameter('data_collector.templates'); 
    $container->setParameter('data_collector.templates', array_merge($templates,array('git' => array('git', 'Profiler:git_info_layout')))); 
} 

的想法是從profiler compiler pass爲你基本上是試圖複製它的一些功能。