Firebug.php
134 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * Zend Framework |
| 6 | * |
| 7 | * LICENSE |
| 8 | * |
| 9 | * This source file is subject to the new BSD license that is bundled |
| 10 | * with this package in the file LICENSE.txt. |
| 11 | * It is also available through the world-wide-web at this URL: |
| 12 | * http://framework.zend.com/license/new-bsd |
| 13 | * If you did not receive a copy of the license and are unable to |
| 14 | * obtain it through the world-wide-web, please send an email |
| 15 | * to license@zend.com so we can send you a copy immediately. |
| 16 | * |
| 17 | * @category Zend |
| 18 | * @package Zend_Db |
| 19 | * @subpackage Profiler |
| 20 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 21 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 22 | * @version $Id: Firebug.php 23775 2011-03-01 17:25:24Z ralph $ |
| 23 | */ |
| 24 | /** Zend_Db_Profiler */ |
| 25 | // require_once 'Zend/Db/Profiler.php'; |
| 26 | /** Zend_Wildfire_Plugin_FirePhp */ |
| 27 | // require_once 'Zend/Wildfire/Plugin/FirePhp.php'; |
| 28 | /** Zend_Wildfire_Plugin_FirePhp_TableMessage */ |
| 29 | // require_once 'Zend/Wildfire/Plugin/FirePhp/TableMessage.php'; |
| 30 | /** |
| 31 | * Writes DB events as log messages to the Firebug Console via FirePHP. |
| 32 | * |
| 33 | * @category Zend |
| 34 | * @package Zend_Db |
| 35 | * @subpackage Profiler |
| 36 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 37 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 38 | */ |
| 39 | class Zend_Db_Profiler_Firebug extends \Zend_Db_Profiler |
| 40 | { |
| 41 | /** |
| 42 | * The original label for this profiler. |
| 43 | * @var string |
| 44 | */ |
| 45 | protected $_label = null; |
| 46 | /** |
| 47 | * The label template for this profiler |
| 48 | * @var string |
| 49 | */ |
| 50 | protected $_label_template = '%label% (%totalCount% @ %totalDuration% sec)'; |
| 51 | /** |
| 52 | * The message envelope holding the profiling summary |
| 53 | * @var Zend_Wildfire_Plugin_FirePhp_TableMessage |
| 54 | */ |
| 55 | protected $_message = null; |
| 56 | /** |
| 57 | * The total time taken for all profiled queries. |
| 58 | * @var float |
| 59 | */ |
| 60 | protected $_totalElapsedTime = 0; |
| 61 | /** |
| 62 | * Constructor |
| 63 | * |
| 64 | * @param string $label OPTIONAL Label for the profiling info. |
| 65 | * @return void |
| 66 | */ |
| 67 | public function __construct($label = null) |
| 68 | { |
| 69 | $this->_label = $label; |
| 70 | if (!$this->_label) { |
| 71 | $this->_label = 'Zend_Db_Profiler_Firebug'; |
| 72 | } |
| 73 | } |
| 74 | /** |
| 75 | * Enable or disable the profiler. If $enable is false, the profiler |
| 76 | * is disabled and will not log any queries sent to it. |
| 77 | * |
| 78 | * @param boolean $enable |
| 79 | * @return Zend_Db_Profiler Provides a fluent interface |
| 80 | */ |
| 81 | public function setEnabled($enable) |
| 82 | { |
| 83 | parent::setEnabled($enable); |
| 84 | if ($this->getEnabled()) { |
| 85 | if (!$this->_message) { |
| 86 | $this->_message = new \Zend_Wildfire_Plugin_FirePhp_TableMessage($this->_label); |
| 87 | $this->_message->setBuffered(\true); |
| 88 | $this->_message->setHeader(array('Time', 'Event', 'Parameters')); |
| 89 | $this->_message->setDestroy(\true); |
| 90 | $this->_message->setOption('includeLineNumbers', \false); |
| 91 | \Zend_Wildfire_Plugin_FirePhp::getInstance()->send($this->_message); |
| 92 | } |
| 93 | } else { |
| 94 | if ($this->_message) { |
| 95 | $this->_message->setDestroy(\true); |
| 96 | $this->_message = null; |
| 97 | } |
| 98 | } |
| 99 | return $this; |
| 100 | } |
| 101 | /** |
| 102 | * Intercept the query end and log the profiling data. |
| 103 | * |
| 104 | * @param integer $queryId |
| 105 | * @throws Zend_Db_Profiler_Exception |
| 106 | * @return void |
| 107 | */ |
| 108 | public function queryEnd($queryId) |
| 109 | { |
| 110 | $state = parent::queryEnd($queryId); |
| 111 | if (!$this->getEnabled() || $state == self::IGNORED) { |
| 112 | return; |
| 113 | } |
| 114 | $this->_message->setDestroy(\false); |
| 115 | $profile = $this->getQueryProfile($queryId); |
| 116 | $this->_totalElapsedTime += $profile->getElapsedSecs(); |
| 117 | $this->_message->addRow(array((string) \round($profile->getElapsedSecs(), 5), $profile->getQuery(), ($params = $profile->getQueryParams()) ? $params : null)); |
| 118 | $this->updateMessageLabel(); |
| 119 | } |
| 120 | /** |
| 121 | * Update the label of the message holding the profile info. |
| 122 | * |
| 123 | * @return void |
| 124 | */ |
| 125 | protected function updateMessageLabel() |
| 126 | { |
| 127 | if (!$this->_message) { |
| 128 | return; |
| 129 | } |
| 130 | $this->_message->setLabel(\str_replace(array('%label%', '%totalCount%', '%totalDuration%'), array($this->_label, $this->getTotalNumQueries(), (string) \round($this->_totalElapsedTime, 5)), $this->_label_template)); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 |