PageRenderTime 22ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/core/classes/debug.php

https://github.com/ngonchan/fuel
PHP | 324 lines | 198 code | 35 blank | 91 comment | 12 complexity | 1d503846a6b2d31def24a51168e45197 MD5 | raw file
  1. <?php
  2. /**
  3. * Fuel
  4. *
  5. * Fuel is a fast, lightweight, community driven PHP5 framework.
  6. *
  7. * @package Fuel
  8. * @version 1.0
  9. * @author Fuel Development Team
  10. * @license MIT License
  11. * @copyright 2010 Dan Horrigan
  12. * @link http://fuelphp.com
  13. */
  14. namespace Fuel;
  15. use Fuel\Application as App;
  16. class Debug {
  17. protected static $js_displayed = false;
  18. protected static $files = array();
  19. /**
  20. * Quick and nice way to output a mixed variable to the browser
  21. *
  22. * @author Phil Sturgeon <http://philsturgeon.co.uk/>
  23. * @static
  24. * @access public
  25. * @return string
  26. */
  27. public static function dump()
  28. {
  29. $backtrace = debug_backtrace();
  30. // If being called from within, show the file above in the backtrack
  31. if (strpos($backtrace[0]['file'], 'core/classes/debug.php') !== FALSE)
  32. {
  33. $callee = $backtrace[1];
  34. $label = App\Inflector::humanize($backtrace[1]['function']);
  35. }
  36. else
  37. {
  38. $callee = $backtrace[0];
  39. $label = 'Debug';
  40. }
  41. $arguments = func_get_args();
  42. $total_arguments = count($arguments);
  43. $callee['file'] = Fuel::clean_path($callee['file']);
  44. if ( ! static::$js_displayed)
  45. {
  46. echo <<<JS
  47. <script type="text/javascript">function fuel_debug_toggle(a){if(document.getElementById){if(document.getElementById(a).style.display=="none"){document.getElementById(a).style.display="block"}else{document.getElementById(a).style.display="none"}}else{if(document.layers){if(document.id.display=="none"){document.id.display="block"}else{document.id.display="none"}}else{if(document.all.id.style.display=="none"){document.all.id.style.display="block"}else{document.all.id.style.display="none"}}}};</script>
  48. JS;
  49. static::$js_displayed = true;
  50. }
  51. echo '<div style="font-size: 13px;background: #EEE !important; border:1px solid #666; padding:10px;">';
  52. echo '<h1 style="border-bottom: 1px solid #CCC; padding: 0 0 5px 0; margin: 0 0 5px 0; font: bold 120% sans-serif;">'.$callee['file'].' @ line: '.$callee['line'].'</h1>';
  53. echo '<pre style="overflow:auto;font-size:100%;">';
  54. $i = 0;
  55. foreach ($arguments as $argument)
  56. {
  57. echo '<strong>'.$label.' #'.(++$i).' of '.$total_arguments.'</strong>:<br />';
  58. echo static::format('...', $argument);
  59. echo '<br />';
  60. }
  61. echo "</pre>";
  62. echo "</div>";
  63. }
  64. /**
  65. * Formats the given $var's output in a nice looking, Foldable interface.
  66. *
  67. * @param string $name the name of the var
  68. * @param mixed $var the variable
  69. * @param int $level the indentation level
  70. * @param string $indent_char the indentation character
  71. * @return string the formatted string.
  72. */
  73. public static function format($name, $var, $level = 0, $indent_char = '&nbsp;&nbsp;&nbsp;&nbsp;')
  74. {
  75. $return = str_repeat($indent_char, $level);
  76. if (is_array($var))
  77. {
  78. $id = 'fuel_debug_'.mt_rand();
  79. if (count($var) > 0)
  80. {
  81. $return .= "<a href=\"javascript:fuel_debug_toggle('$id');\"><strong>{$name}</strong></a>";
  82. }
  83. else
  84. {
  85. $return .= "<strong>{$name}</strong>";
  86. }
  87. $return .= " (Array, ".count($var)." elements)\n";
  88. $sub_return = '';
  89. foreach ($var as $key => $val)
  90. {
  91. $sub_return .= static::format($key, $val, $level + 1);
  92. }
  93. if (count($var) > 0)
  94. {
  95. $return .= "<span id=\"$id\" style=\"display: none;\">$sub_return</span>";
  96. }
  97. else
  98. {
  99. $return .= $sub_return;
  100. }
  101. }
  102. elseif (is_string($var))
  103. {
  104. $return .= "<strong>{$name}</strong> (String, ".strlen($var)." characters): \"{$var}\"\n";
  105. }
  106. elseif (is_float($var))
  107. {
  108. $return .= "<strong>{$name}</strong> (Float): {$var}\n";
  109. }
  110. elseif (is_long($var))
  111. {
  112. $return .= "<strong>{$name}</strong> (Integer): {$var}\n";
  113. }
  114. elseif (is_null($var))
  115. {
  116. $return .= "<strong>{$name}</strong> (Null): null\n";
  117. }
  118. elseif (is_bool($var))
  119. {
  120. $return .= "<strong>{$name}</strong> (Boolean): ".($var ? 'true' : 'false')."\n";
  121. }
  122. elseif (is_double($var))
  123. {
  124. $return .= "<strong>{$name}</strong> (Double): {$var}\n";
  125. }
  126. elseif (is_object($var))
  127. {
  128. $id = 'fuel_debug_'.mt_rand();
  129. $vars = get_object_vars($var);
  130. if (count($vars) > 0)
  131. {
  132. $return .= "<a href=\"javascript:fuel_debug_toggle('$id');\"><strong>{$name}</strong></a>";
  133. }
  134. else
  135. {
  136. $return .= "<strong>{$name}</strong>";
  137. }
  138. $return .= " (Object): ".get_class($var)."\n";
  139. $sub_return = '';
  140. foreach ($vars as $key => $val)
  141. {
  142. $sub_return .= static::format($key, $val, $level + 1);
  143. }
  144. if (count($vars) > 0)
  145. {
  146. $return .= "<span id=\"$id\" style=\"display: none;\">$sub_return</span>";
  147. }
  148. else
  149. {
  150. $return .= $sub_return;
  151. }
  152. }
  153. else
  154. {
  155. $return .= "<strong>{$name}</strong>: {$var}\n";
  156. }
  157. return $return;
  158. }
  159. /**
  160. * Returns the debug lines from the specified file
  161. *
  162. * @access protected
  163. * @param string the file path
  164. * @param int the line number
  165. * @param bool whether to use syntax highlighting or not
  166. * @param int the amount of line padding
  167. * @return array
  168. */
  169. public static function file_lines($filepath, $line_num, $highlight = true, $padding = 5)
  170. {
  171. // We cache the entire file to reduce disk IO for multiple errors
  172. if ( ! isset(static::$files[$filepath]))
  173. {
  174. static::$files[$filepath] = file($filepath, FILE_IGNORE_NEW_LINES);
  175. array_unshift(static::$files[$filepath], '');
  176. }
  177. $start = $line_num - $padding;
  178. if ($start < 0)
  179. {
  180. $start = 0;
  181. }
  182. $length = ($line_num - $start) + $padding + 1;
  183. if (($start + $length) > count(static::$files[$filepath]) - 1)
  184. {
  185. $length = NULL;
  186. }
  187. $debug_lines = array_slice(static::$files[$filepath], $start, $length, TRUE);
  188. if ($highlight)
  189. {
  190. $to_replace = array('<code>', '</code>', '<span style="color: #0000BB">&lt;?php&nbsp;', "\n");
  191. $replace_with = array('', '', '<span style="color: #0000BB">', '');
  192. foreach ($debug_lines as & $line)
  193. {
  194. $line = str_replace($to_replace, $replace_with, highlight_string('<?php ' . $line, TRUE));
  195. }
  196. }
  197. return $debug_lines;
  198. }
  199. public static function backtrace()
  200. {
  201. return static::dump(debug_backtrace());
  202. }
  203. /**
  204. * Prints a list of all currently declared classes.
  205. *
  206. * @access public
  207. * @static
  208. */
  209. public static function classes()
  210. {
  211. return static::dump(get_declared_classes());
  212. }
  213. /**
  214. * Prints a list of all currently declared interfaces (PHP5 only).
  215. *
  216. * @access public
  217. * @static
  218. */
  219. public static function interfaces()
  220. {
  221. return static::dump(get_declared_interfaces());
  222. }
  223. /**
  224. * Prints a list of all currently included (or required) files.
  225. *
  226. * @access public
  227. * @static
  228. */
  229. public static function includes()
  230. {
  231. return static::dump(get_included_files());
  232. }
  233. /**
  234. * Prints a list of all currently declared functions.
  235. *
  236. * @access public
  237. * @static
  238. */
  239. public static function functions()
  240. {
  241. return static::dump(get_defined_functions());
  242. }
  243. /**
  244. * Prints a list of all currently declared constants.
  245. *
  246. * @access public
  247. * @static
  248. */
  249. public static function constants()
  250. {
  251. return static::dump(get_defined_constants());
  252. }
  253. /**
  254. * Prints a list of all currently loaded PHP extensions.
  255. *
  256. * @access public
  257. * @static
  258. */
  259. public static function extensions()
  260. {
  261. return static::dump(get_loaded_extensions());
  262. }
  263. /**
  264. * Prints a list of all HTTP request headers.
  265. *
  266. * @access public
  267. * @static
  268. */
  269. public static function headers()
  270. {
  271. return static::dump(getAllHeaders());
  272. }
  273. /**
  274. * Prints a list of the configuration settings read from <i>php.ini</i>
  275. *
  276. * @access public
  277. * @static
  278. */
  279. public static function phpini()
  280. {
  281. if ( ! is_readable(get_cfg_var('cfg_file_path')))
  282. {
  283. return false;
  284. }
  285. // render it
  286. return static::dump(parse_ini_file(get_cfg_var('cfg_file_path'), true));
  287. }
  288. }
  289. /* End of file input.php */