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