Created
March 10, 2011 16:35
-
-
Save fabn/864419 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Plugin_Profiler extends Zend_Controller_Plugin_Abstract | |
{ | |
private $profiler; | |
/** | |
* Constructor | |
* | |
* @param string $label OPTIONAL Label for the profiling info. | |
* @return void | |
*/ | |
public function __construct($label = null) | |
{ | |
$this->_label = $label; | |
if (!$this->_label) { | |
$this->_label = 'Zend_Db_Profiler_Firebug'; | |
} | |
$conn = Doctrine_Manager::connection(); | |
$conn->setListener($this->profiler = new Doctrine_Connection_Profiler()); | |
// setup firebug | |
if (!$this->_message) { | |
$this->_message = new Zend_Wildfire_Plugin_FirePhp_TableMessage($this->_label); | |
$this->_message->setBuffered(true); | |
$this->_message->setHeader(array('Time', 'Event', 'Parameters')); | |
$this->_message->setDestroy(true); | |
$this->_message->setOption('includeLineNumbers', false); | |
Zend_Wildfire_Plugin_FirePhp::getInstance()->send($this->_message); | |
} | |
} | |
public function dispatchLoopShutdown() | |
{ | |
foreach ($this->profiler as $event) { | |
/** @var $event Doctrine_Event */ | |
$this->queryEnd($event); | |
} | |
} | |
/** | |
* The original label for this profiler. | |
* @var string | |
*/ | |
protected $_label = null; | |
/** | |
* The label template for this profiler | |
* @var string | |
*/ | |
protected $_label_template = '%label% (%totalCount% @ %totalDuration% sec)'; | |
/** | |
* The message envelope holding the profiling summary | |
* @var Zend_Wildfire_Plugin_FirePhp_TableMessage | |
*/ | |
protected $_message = null; | |
/** | |
* The total time taken for all profiled queries. | |
* @var float | |
*/ | |
protected $_totalElapsedTime = 0; | |
/** | |
* Number of executed queries | |
* @var int | |
*/ | |
public $_totalNumQueries = 0; | |
/** | |
* Intercept the query end and log the profiling data. | |
* | |
* @param Doctrine_Event $event | |
* @throws Zend_Db_Profiler_Exception | |
* @return void | |
*/ | |
public function queryEnd($event) | |
{ | |
$this->_message->setDestroy(false); | |
$this->_totalElapsedTime += $event->getElapsedSecs(); | |
$this->_message->addRow(array((string)round($event->getElapsedSecs(), 5), | |
$event->getQuery(), | |
($params = $event->getParams()) ? $params : null)); | |
$this->_totalNumQueries++; | |
// update label | |
$this->updateMessageLabel(); | |
} | |
/** | |
* Update the label of the message holding the profile info. | |
* | |
* @return void | |
*/ | |
protected function updateMessageLabel() | |
{ | |
if (!$this->_message) { | |
return; | |
} | |
$this->_message->setLabel(str_replace(array('%label%', | |
'%totalCount%', | |
'%totalDuration%'), | |
array($this->_label, | |
$this->_totalNumQueries, | |
(string)round($this->_totalElapsedTime, 5)), | |
$this->_label_template)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment