1. sfWebDebugPanel.class.php
  2. /** * sfWebDebugPanel represents a web debug panel. * * @package symfony * @subpackage debug * @author Fabien Potencier * @version SVN: $Id: sfWebDebugPanel.class.php 27284 2010-01-28 18:34:57Z Kris.Wallsmith $ */
  3. abstract class sfWebDebugPanel
  4. {
  5. protected
  6. $webDebug = null,
  7. $status = sfLogger::INFO;
  8. /**
  9. * Constructor.
  10. *
  11. * @param sfWebDebug $webDebug The web debug toolbar instance
  12. */
  13. public function __construct(sfWebDebug $webDebug)
  14. {
  15. $this->webDebug = $webDebug;
  16. }
  17. /**
  18. * Gets the link URL for the link.
  19. *
  20. * @return string The URL link
  21. */
  22. public function getTitleUrl()
  23. {
  24. }
  25. /**
  26. * Gets the text for the link.
  27. *
  28. * @return string The link text
  29. */
  30. abstract public function getTitle();
  31. /**
  32. * Gets the title of the panel.
  33. *
  34. * @return string The panel title
  35. */
  36. abstract public function getPanelTitle();
  37. /**
  38. * Gets the panel HTML content.
  39. *
  40. * @return string The panel HTML content
  41. */
  42. abstract public function getPanelContent();
  43. /**
  44. * Returns the current status.
  45. *
  46. * @return integer A {@link sfLogger} priority constant
  47. */
  48. public function getStatus()
  49. {
  50. return $this->status;
  51. }
  52. /**
  53. * Sets the current panel's status.
  54. *
  55. * @param integer $status A {@link sfLogger} priority constant
  56. */
  57. public function setStatus($status)
  58. {
  59. $this->status = $status;
  60. }
  61. /**
  62. * Returns a toggler element.
  63. *
  64. * @param string $element The value of an element's DOM id attribute
  65. * @param string $title A title attribute
  66. *
  67. * @return string
  68. */
  69. public function getToggler($element, $title = 'Toggle details')
  70. {
  71. return '<a href="#" onclick="sfWebDebugToggle(\''.$element.'\'); return false;" title="'.$title.'"><img src="'.$this->webDebug->getOption('image_root_path').'/toggle.gif" alt="'.$title.'"/></a>';
  72. }
  73. /**
  74. * Returns a toggleable presentation of a debug stack.
  75. *
  76. * @param array $debugStack
  77. *
  78. * @return string
  79. */
  80. public function getToggleableDebugStack($debugStack)
  81. {
  82. static $i = 1;
  83. if (!$debugStack)
  84. {
  85. return '';
  86. }
  87. $element = get_class($this).'Debug'.$i++;
  88. $keys = array_reverse(array_keys($debugStack));
  89. $html = $this->getToggler($element, 'Toggle debug stack');
  90. $html .= '<div class="sfWebDebugDebugInfo" id="'.$element.'" style="display:none">';
  91. foreach ($debugStack as $j => $trace)
  92. {
  93. $file = isset($trace['file']) ? $trace['file'] : null;
  94. $line = isset($trace['line']) ? $trace['line'] : null;
  95. $isProjectFile = $file && 0 === strpos($file, sfConfig::get('sf_root_dir')) && !preg_match('/(cache|plugins|vendor)/', $file);
  96. $html .= sprintf('<span%s>#%s &raquo; ', $isProjectFile ? ' class="sfWebDebugHighlight"' : '', $keys[$j] + 1);
  97. if (isset($trace['function']))
  98. {
  99. $html .= sprintf('in <span class="sfWebDebugLogInfo">%s%s%s()</span> ',
  100. isset($trace['class']) ? $trace['class'] : '',
  101. isset($trace['type']) ? $trace['type'] : '',
  102. $trace['function']
  103. );
  104. }
  105. $html .= sprintf('from %s line %s', $this->formatFileLink($file, $line), $line);
  106. $html .= '</span><br/>';
  107. }
  108. $html .= "</div>\n";
  109. return $html;
  110. }
  111. /**
  112. * Formats a file link.
  113. *
  114. * @param string $file A file path or class name
  115. * @param integer $line
  116. * @param string $text Text to use for the link
  117. *
  118. * @return string
  119. */
  120. public function formatFileLink($file, $line = null, $text = null)
  121. {
  122. // this method is called a lot so we avoid calling class_exists()
  123. if ($file && !sfToolkit::isPathAbsolute($file))
  124. {
  125. if (null === $text)
  126. {
  127. $text = $file;
  128. }
  129. // translate class to file name
  130. $r = new ReflectionClass($file);
  131. $file = $r->getFileName();
  132. }
  133. $shortFile = sfDebug::shortenFilePath($file);
  134. if ($linkFormat = sfConfig::get('sf_file_link_format', ini_get('xdebug.file_link_format')))
  135. {
  136. // return a link
  137. return sprintf(
  138. '<a href="%s" class="sfWebDebugFileLink" title="%s">%s</a>',
  139. htmlspecialchars(strtr($linkFormat, array('%f' => $file, '%l' => $line)), ENT_QUOTES, sfConfig::get('sf_charset')),
  140. htmlspecialchars($shortFile, ENT_QUOTES, sfConfig::get('sf_charset')),
  141. null === $text ? $shortFile : $text);
  142. }
  143. else if (null === $text)
  144. {
  145. // return the shortened file path
  146. return $shortFile;
  147. }
  148. else
  149. {
  150. // return the provided text with the shortened file path as a tooltip
  151. return sprintf('<span title="%s">%s</span>', $shortFile, $text);
  152. }
  153. }
  154. /**
  155. * Format a SQL string with some colors on SQL keywords to make it more readable.
  156. *
  157. * @param string $sql SQL string to format
  158. *
  159. * @return string $newSql The new formatted SQL string
  160. */
  161. public function formatSql($sql)
  162. {
  163. return preg_replace('/\b(UPDATE|SET|SELECT|FROM|AS|LIMIT|ASC|COUNT|DESC|WHERE|LEFT JOIN|INNER JOIN|RIGHT JOIN|ORDER BY|GROUP BY|IN|LIKE|DISTINCT|DELETE|INSERT|INTO|VALUES)\b/', '<span class="sfWebDebugLogInfo">\\1</span>', $sql);
  164. }
  165. }

Debug toolbar