PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Db/Profiler/Firebug.php

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