2014-10-18 40 views
5

我想獲得更多的時間信息到Symfony Profiler時間軸中,但我無法獲取任何內容。根據我讀過的文檔,它應該和下面的例子一樣簡單,但是這不會在時間線上出現任何額外的信息。Symfony秒錶事件沒有出現在Profiler時間線

我需要以某種方式讓探查器知道我開始和停止的事件嗎?

use Symfony\Component\Stopwatch\Stopwatch; 

class DefaultController extends Controller 
{ 
    public function testAction() 
    { 
     $stopwatch = new Stopwatch(); 
     $stopwatch->start('testController'); 

     usleep(1000000); 

     $response = new Response(
      '<body>Hi</body>', 
      Response::HTTP_OK, 
      array('content-type' => 'text/html') 
     ); 

     $event = $stopwatch->stop('testController'); 

     return $response; 
    } 
} 

回答

18

Symfony的分析器無法掃描到所有秒錶實例的代碼並將其放入時間軸。你必須使用由簡而不是提供預配置的秒錶:

public function testAction() 
{ 
    $stopwatch = $this->get('debug.stopwatch'); 
    $stopwatch->start('testController'); 

    // ... 

    $event = $stopwatch->stop('testController'); 

    return $response; 
} 

然而,你的控制器已經在時間軸上...

+1

這個答案應該被接受 – 2017-03-18 07:33:25

+3

他們應該在官方文檔添加此, Stopwatch頁面上沒有提及它。 – Waddles 2017-03-23 05:15:58

+0

@Waddles https://symfony.com/doc/current/components/stopwatch.html – 2017-10-20 11:46:12