PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jelix/plugins/htmlresponse/debugbar/debugbar.htmlresponse.php

http://github.com/jelix/jelix
PHP | 290 lines | 160 code | 34 blank | 96 comment | 19 complexity | b37a69a3b143f42e138cc560392aa7f8 MD5 | raw file
Possible License(s): BSD-3-Clause, JSON, GPL-3.0, LGPL-3.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * @package jelix
  4. * @subpackage responsehtml_plugin
  5. * @author Laurent Jouanneau
  6. * @contributor Julien Issler
  7. * @copyright 2010-2012 Laurent Jouanneau
  8. * @copyright 2011 Julien Issler
  9. * @link http://jelix.org
  10. * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  11. */
  12. /**
  13. * interface for plugins for the debugbar
  14. * @since 1.3
  15. */
  16. interface jIDebugbarPlugin {
  17. /**
  18. * @return string CSS styles
  19. */
  20. function getCss();
  21. /**
  22. * @return string Javascript code lines
  23. */
  24. function getJavascript();
  25. /**
  26. * it should adds content or set some properties on the debugbar
  27. * to displays some contents.
  28. * @param debugbarHTMLResponsePlugin $debugbar the debugbar
  29. */
  30. function show($debugbar);
  31. }
  32. #if ENABLE_OPTIMIZED_SOURCE
  33. #includephp errors.debugbar.php
  34. #else
  35. require_once(dirname(__FILE__).'/errors.debugbar.php');
  36. #endif
  37. /**
  38. * information for a component a debug bar
  39. * @since 1.3
  40. */
  41. class debugbarItemInfo {
  42. /**
  43. * an id. required
  44. */
  45. public $id ='';
  46. /**
  47. * a simple text label
  48. */
  49. public $label='';
  50. /**
  51. * the HTML label to display in the debug bar
  52. */
  53. public $htmlLabel = '';
  54. /**
  55. * the HTML content of the popup if the information needs a popup
  56. */
  57. public $popupContent ='';
  58. /**
  59. * indicate if the popup should be opened or not at the startup
  60. */
  61. public $popupOpened = false;
  62. /**
  63. * @param string $id an id
  64. * @param string $label a simple text label
  65. * @param string $htmlLabel the HTML label to display in the debug bar
  66. * @param string $popupContent the HTML content of the popup if the information needs a popup
  67. * @param boolean $isOpened indicate if the popup should be opened or not at the startup
  68. */
  69. function __construct($id, $label, $htmlLabel='', $popupContent='', $isOpened= false) {
  70. $this->id = $id;
  71. $this->label = $label;
  72. $this->htmlLabel = $htmlLabel;
  73. $this->popupContent = $popupContent;
  74. $this->popupOpened = $isOpened;
  75. }
  76. }
  77. #includerawinto LOGOBULLETPLUS icons/bullet_toggle_plus.png | base64
  78. #includerawinto LOGOBULLETMINUS icons/bullet_toggle_minus.png | base64
  79. /**
  80. * plugin for jResponseHTML, it displays a debugbar
  81. * @since 1.3
  82. */
  83. class debugbarHTMLResponsePlugin implements jIHTMLResponsePlugin {
  84. protected $response = null;
  85. protected $plugins = array();
  86. protected $tabs = array();
  87. // ------------- implementation of the jIHTMLResponsePlugin interface
  88. public function __construct(jResponse $c) {
  89. $this->response = $c;
  90. $this->plugins['errors'] = new errorsDebugbarPlugin();
  91. }
  92. /**
  93. * called just before the jResponseBasicHtml::doAfterActions() call
  94. */
  95. public function afterAction() {
  96. }
  97. /**
  98. * called just before the final output. This is the opportunity
  99. * to make changes before the head and body output. At this step
  100. * the main content (if any) is already generated.
  101. */
  102. public function beforeOutput() {
  103. $plugins = jApp::config()->debugbar['plugins'];
  104. $css = "
  105. #expand ul.jxdb-list li h5 a {background-image: url('data:image/png;base64,__LOGOBULLETPLUS__');}
  106. #expand ul.jxdb-list li.jxdb-opened h5 a {background-image: url('data:image/png;base64,__LOGOBULLETMINUS__');}
  107. ";
  108. $js = '';
  109. if ($plugins) {
  110. $plugins = preg_split('/ *, */', $plugins);
  111. foreach ($plugins as $name) {
  112. $plugin = jApp::loadPlugin($name, 'debugbar', '.debugbar.php', $name.'DebugbarPlugin', $this);
  113. if ($plugin) {
  114. $this->plugins[$name] = $plugin;
  115. }
  116. /*else
  117. throw new jException('');*/
  118. }
  119. }
  120. foreach($this->plugins as $name => $plugin) {
  121. $css .= $plugin->getCSS();
  122. $js .= $plugin->getJavascript();
  123. }
  124. $this->response->addHeadContent('
  125. <style type="text/css">
  126. #includeraw debugbar.css
  127. '.$css.'
  128. </style>
  129. <script type="text/javascript">//<![CDATA[
  130. #includeraw debugbar.js|jspacker|escquote
  131. '.$js.' //]]>
  132. </script>
  133. ');
  134. }
  135. /**
  136. * called when the content is generated, and potentially sent, except
  137. * the body end tag and the html end tags. This method can output
  138. * directly some contents.
  139. */
  140. public function atBottom() {
  141. foreach($this->plugins as $plugin) {
  142. $plugin->show($this);
  143. }
  144. #includerawinto LOGOJELIX jelix-dbg.png | base64
  145. #includerawinto LOGOSTOP icons/cancel.png | base64
  146. ?>
  147. <div id="jxdb">
  148. <div id="jxdb-header">
  149. #expand <a href="javascript:jxdb.selectTab('jxdb-panel-jelix');"><img src="data:image/png;base64,__LOGOJELIX__" alt="Jelix debug toolbar"/></a>
  150. <?php foreach ($this->tabs as $item) {
  151. $label = ($item->htmlLabel ? $item->htmlLabel: htmlspecialchars($item->label));
  152. if ($item->popupContent) {
  153. echo '<span><a href="javascript:jxdb.selectTab(\'jxdb-panel-'.$item->id.'\');">'.$label.'</a></span>';
  154. }
  155. else
  156. echo '<span>'.$label.'</span>';
  157. }
  158. ?>
  159. #expand <a href="javascript:jxdb.close();"><img src="data:image/png;base64,__LOGOSTOP__" alt="close" title="click to close the debug toolbar"/></a>
  160. </div>
  161. <div id="jxdb-tabpanels">
  162. <div id="jxdb-panel-jelix" class="jxdb-tabpanel" style="display:none">
  163. <ul>
  164. <li>Jelix version: <?php echo JELIX_VERSION?></li>
  165. <li>Move the debug bar <a id="jxdb-pjlx-a-right" href="javascript:jxdb.moveTo('r')">to right</a>
  166. <a href="javascript:jxdb.moveTo('l')" id="jxdb-pjlx-a-left">to left</a></li>
  167. </ul>
  168. </div>
  169. <?php
  170. $alreadyOpen = false;
  171. foreach ($this->tabs as $item) {
  172. if (!$item->popupContent)
  173. continue;
  174. echo '<div id="jxdb-panel-'.$item->id.'" class="jxdb-tabpanel"';
  175. if ($item->popupOpened && !$alreadyOpen) {
  176. $alreadyOpen = true;
  177. echo ' style="display:block"';
  178. }
  179. else
  180. echo ' style="display:none"';
  181. echo '>', $item->popupContent;
  182. echo '</div>';
  183. }?>
  184. </div>
  185. </div>
  186. <?php
  187. }
  188. /**
  189. * called just before the output of an error page
  190. */
  191. public function beforeOutputError() {
  192. $this->beforeOutput();
  193. ob_start();
  194. $this->atBottom();
  195. $this->response->addContent(ob_get_clean(),true);
  196. }
  197. // ------------- methods that plugins for the debugbar can call
  198. /**
  199. * add an information in the debug bar
  200. * @param debugbarItemInfo $info informations
  201. */
  202. function addInfo($info) {
  203. $this->tabs[] = $info;
  204. }
  205. /**
  206. * returns html formated stack trace
  207. * @param array $trace
  208. * @return string
  209. */
  210. function formatTrace($trace) {
  211. $html = '<table>';
  212. foreach($trace as $k=>$t) {
  213. if (isset($t['file'])) {
  214. $file = $t['file'];
  215. $path = '';
  216. $shortcut = '';
  217. if (strpos($file, LIB_PATH) === 0) {
  218. $path = LIB_PATH;
  219. $shortcut = 'lib:';
  220. }
  221. elseif (strpos($file, jApp::tempPath()) === 0) {
  222. $path = jApp::tempPath();
  223. $shortcut = 'temp:';
  224. }
  225. elseif (strpos($file, jApp::appPath()) === 0) {
  226. $path = jApp::appPath();
  227. $shortcut = 'app:';
  228. }
  229. else {
  230. $path = dirname(jApp::appPath());
  231. $shortcut = 'app:';
  232. while ($path != '.' && $path != '') {
  233. $shortcut .= '../';
  234. if (strpos($file, $path) === 0) {
  235. break;
  236. }
  237. $path = dirname($path);
  238. }
  239. if ($path =='.')
  240. $path = '';
  241. }
  242. if ($path != '') {
  243. $cut = ($path[0] == '/'?0:1);
  244. $file = '<i>'.$shortcut.'</i>'.substr($file, strlen($path)+$cut);
  245. }
  246. }
  247. else {
  248. $file = '[php]';
  249. }
  250. $html .='<tr><td>'.$k.'</td><td>'.(isset($t['class'])?$t['class'].$t['type']:'').$t['function'].'()</td>';
  251. $html .='<td>'.($file).'</td><td>'.(isset($t['line'])?$t['line']:'').'</td></tr>';
  252. }
  253. $html .='</table>';
  254. return $html;
  255. }
  256. }