2013-05-13 52 views
3

我想用我自己的自定義數據擴展Symfony2調試工具欄。如何用自定義數據擴展Symfony2調試工具欄?

我有我想要的記錄特定的方法調用,然後調試工具欄顯示它們的服務。

我讀了cookbook article,但它不是很有幫助。

我創建了自己DataCollector類:

class PermissionDataCollector extends DataCollector 
{ 
    private $permissionCalls = array(); 

    private $permissionExtension; 

    public function __construct(PermissionExtension $permissionExtension) 
    { 
     $this->permissionExtension = $permissionExtension; 
    } 

    /** 
    * Collects data for the given Request and Response. 
    * 
    * @param Request $request A Request instance 
    * @param Response $response A Response instance 
    * @param \Exception $exception An Exception instance 
    * 
    * @api 
    */ 
    public function collect(Request $request, Response $response, \Exception $exception = null) 
    { 
     $this->permissionCalls = $this->permissionExtension->getPermissionCalls(); 

     $this->data = array(
      'calls' => $this->permissionCalls 
     ); 
    } 
    public function getPermissionCallsCount() 
    { 
     return count($this->permissionCalls); 
    } 

    public function getFailedPermissionCallsCount() 
    { 
     return count(array_filter($this->permissionCalls, array(&$this, "filterForFailedPermissionCalls"))); 
    } 

    private function filterForFailedPermissionCalls($var) 
    { 
     return $var['success']; 
    } 

    /** 
    * Returns the name of the collector. 
    * 
    * @return string The collector name 
    * 
    * @api 
    */ 
    public function getName() 
    { 
     return 'permission'; 
    } 
} 

PermissionExtension記錄所有來電,然後我想在 PermissionDataCollector檢索該數組調用。

和模板只是輸出{{ collector.permissionCallsCount }}

該部分顯示在工具欄中,但只顯示0這是錯誤的。

我不知道如果我連這樣做的權利,因爲該文檔缺乏這一部分。我使用的Symfony 2.1

有沒有人擴展了自定義數據的工具欄?

+2

我不知道如何從內存中做到這一點,但我知道FOQElasticaBundle會顯示ElasticSearch查詢的日誌,這將是一個很好的開始。 – Steve 2013-05-14 22:19:19

+0

啊太棒了!有用。我基本上需要始終引用'$ this-> data'。非常感謝捆綁參考! – 2013-05-15 09:31:11

+0

哈,拍攝和分數:)很高興它幫助! – Steve 2013-05-18 13:19:08

回答

1

啊太棒了!有用。我基本上需要始終引用$ this->數據。

這樣的原因 - >數據被Symfony\Component\HttpKernel\DataCollector\DataCollector和序列化使用(請參閱DataCollector :: serialize)。

這在以後存儲(不知怎麼的,我不知道在哪裏,但後來反序列化)。如果你使用自己的屬性DataCollector::unserialize只是修剪你的數據。

https://symfony.com/doc/current/profiler/data_collector.html#creating-a-custom-data-collector

由於探查串行數據採集器的情況下,你不應該存儲不能序列(如PDO對象)的對象,或者您需要提供自己的序列化()方法。

只需使用$ this->數據,或者實現您自己的\Serializable序列化。