2
0

Firebug.php 4.8 KB

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