/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/debug/sfWebDebugPanelPropel.class.php

https://bitbucket.org/sebastian_jaurena/todo · PHP · 146 lines · 84 code · 16 blank · 46 comment · 10 complexity · 0433ff84c583e15ddfc6904d633d028f MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfWebDebugPanelPropel adds a panel to the web debug toolbar with Propel information.
  11. *
  12. * @package symfony
  13. * @subpackage debug
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWebDebugPanelPropel.class.php 27284 2010-01-28 18:34:57Z Kris.Wallsmith $
  16. */
  17. class sfWebDebugPanelPropel extends sfWebDebugPanel
  18. {
  19. /**
  20. * Get the title/icon for the panel
  21. *
  22. * @return string $html
  23. */
  24. public function getTitle()
  25. {
  26. if ($sqlLogs = $this->getSqlLogs())
  27. {
  28. return '<img src="'.$this->webDebug->getOption('image_root_path').'/database.png" alt="SQL queries" /> '.count($sqlLogs);
  29. }
  30. }
  31. /**
  32. * Get the verbal title of the panel
  33. *
  34. * @return string $title
  35. */
  36. public function getPanelTitle()
  37. {
  38. return 'SQL queries';
  39. }
  40. /**
  41. * Get the html content of the panel
  42. *
  43. * @return string $html
  44. */
  45. public function getPanelContent()
  46. {
  47. return '
  48. <div id="sfWebDebugDatabaseLogs">
  49. <h3>Propel Version: '.Propel::VERSION.'</h3>
  50. <ol>'.implode("\n", $this->getSqlLogs()).'</ol>
  51. </div>
  52. ';
  53. }
  54. /**
  55. * Listens to debug.web.load_panels and adds this panel.
  56. */
  57. static public function listenToAddPanelEvent(sfEvent $event)
  58. {
  59. $event->getSubject()->setPanel('db', new self($event->getSubject()));
  60. }
  61. /**
  62. * Builds the sql logs and returns them as an array.
  63. *
  64. * @return array
  65. */
  66. protected function getSqlLogs()
  67. {
  68. $config = $this->getPropelConfiguration();
  69. $outerGlue = $config->getParameter('debugpdo.logging.outerglue', ' | ');
  70. $innerGlue = $config->getParameter('debugpdo.logging.innerglue', ': ');
  71. $flagSlow = $config->getParameter('debugpdo.logging.details.slow.enabled', false);
  72. $threshold = $config->getParameter('debugpdo.logging.details.slow.threshold', DebugPDO::DEFAULT_SLOW_THRESHOLD);
  73. $html = array();
  74. foreach ($this->webDebug->getLogger()->getLogs() as $log)
  75. {
  76. if ('sfPropelLogger' != $log['type'])
  77. {
  78. continue;
  79. }
  80. $details = array();
  81. $slowQuery = false;
  82. $parts = explode($outerGlue, $log['message']);
  83. foreach ($parts as $i => $part)
  84. {
  85. // is this a key-glue-value fragment ?
  86. if (preg_match('/^(\w+)'.preg_quote($innerGlue, '/').'(.*)/', $part, $match))
  87. {
  88. $details[] = $part;
  89. unset($parts[$i]);
  90. // check for slow query
  91. if ('time' == $match[1])
  92. {
  93. if ($flagSlow && (float) $match[2] > $threshold)
  94. {
  95. $slowQuery = true;
  96. if ($this->getStatus() > sfLogger::NOTICE)
  97. {
  98. $this->setStatus(sfLogger::NOTICE);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. // all stuff that has not been eaten by the loop should be the query string
  105. $query = join($outerGlue, $parts);
  106. $query = $this->formatSql(htmlspecialchars($query, ENT_QUOTES, sfConfig::get('sf_charset')));
  107. $backtrace = isset($log['debug_backtrace']) && count($log['debug_backtrace']) ? '&nbsp;'.$this->getToggleableDebugStack($log['debug_backtrace']) : '';
  108. $html[] = sprintf('
  109. <li%s>
  110. <p class="sfWebDebugDatabaseQuery">%s</p>
  111. <div class="sfWebDebugDatabaseLogInfo">%s%s</div>
  112. </li>',
  113. $slowQuery ? ' class="sfWebDebugWarning"' : '',
  114. $query,
  115. implode(', ', $details),
  116. $backtrace
  117. );
  118. }
  119. return $html;
  120. }
  121. /**
  122. * Returns the current PropelConfiguration.
  123. *
  124. * @return PropelConfiguration
  125. */
  126. protected function getPropelConfiguration()
  127. {
  128. return Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT);
  129. }
  130. }