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

/modules/debug_toolbar/classes/debugtoolbar.php

https://bitbucket.org/seyar/ari100krat.local
PHP | 362 lines | 249 code | 55 blank | 58 comment | 23 complexity | d5b238207fefbb8bbd71435f49eb20ec MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. class DebugToolbar
  3. {
  4. protected static $_queries = FALSE;
  5. protected static $_benchmarks = FALSE;
  6. public static $benchmark_name = 'debug_toolbar';
  7. /**
  8. * Renders the Debug Toolbar
  9. *
  10. * @param bool print rendered output
  11. * @return string debug toolbar rendered output
  12. */
  13. public static function render($print = false)
  14. {
  15. $token = Profiler::start('custom', self::$benchmark_name);
  16. $template = new View('toolbar');
  17. // Database panel
  18. if (Kohana::config('debug_toolbar.panels.database') === TRUE)
  19. {
  20. $queries = self::get_queries();
  21. $template
  22. ->set('queries', $queries['data'])
  23. ->set('query_count', $queries['count'])
  24. ->set('total_time', $queries['time'])
  25. ->set('total_memory', $queries['memory']);
  26. }
  27. // Files panel
  28. if (Kohana::config('debug_toolbar.panels.files') === TRUE)
  29. {
  30. $template->set('files', self::get_files());
  31. }
  32. // Modules panel
  33. if (Kohana::config('debug_toolbar.panels.modules') === TRUE)
  34. {
  35. $template->set('modules', self::get_modules());
  36. }
  37. // Routes panel
  38. if (Kohana::config('debug_toolbar.panels.routes') === TRUE)
  39. {
  40. $template->set('routes', self::get_routes());
  41. }
  42. // FirePHP
  43. if (Kohana::config('debug_toolbar.firephp_enabled') === TRUE)
  44. {
  45. self::firephp();
  46. }
  47. // Set alignment for toolbar
  48. switch (Kohana::config('debug_toolbar.align'))
  49. {
  50. case 'right':
  51. case 'center':
  52. case 'left':
  53. $template->set('align', Kohana::config('debug_toolbar.align'));
  54. break;
  55. default:
  56. $template->set('align', 'left');
  57. }
  58. // Javascript for toolbar
  59. $template->set('scripts', file_get_contents(Kohana::find_file('views', 'toolbar', 'js')));
  60. // CSS for toolbar
  61. $styles = file_get_contents(Kohana::find_file('views', 'toolbar', 'css'));
  62. Profiler::stop($token);
  63. // Benchmarks panel
  64. if (Kohana::config('debug_toolbar.panels.benchmarks') === TRUE)
  65. {
  66. $template->set('benchmarks', self::get_benchmarks());
  67. }
  68. if ($output = Request::instance()->response and self::is_enabled())
  69. {
  70. // Try to add css just before the </head> tag
  71. if (stripos($output, '</head>') !== FALSE)
  72. {
  73. $output = str_ireplace('</head>', $styles.'</head>', $output);
  74. }
  75. else
  76. {
  77. // No </head> tag found, append styles to output
  78. $template->set('styles', $styles);
  79. }
  80. // Try to add js and HTML just before the </body> tag
  81. if (stripos($output, '</body>') !== FALSE)
  82. {
  83. $output = str_ireplace('</body>', $template->render().'</body>', $output);
  84. }
  85. else
  86. {
  87. // Closing <body> tag not found, just append toolbar to output
  88. $output .= $template->render();
  89. }
  90. Request::instance()->response = $output;
  91. }
  92. else
  93. {
  94. $template->set('styles', $styles);
  95. return ($print ? $template->render() : $template);
  96. }
  97. }
  98. /**
  99. * Retrieves query benchmarks from Database
  100. */
  101. public static function get_queries()
  102. {
  103. if (self::$_queries !== FALSE)
  104. {
  105. return self::$_queries;
  106. }
  107. $result = array();
  108. $count = $time = $memory = 0;
  109. $groups = Profiler::groups();
  110. foreach(Database::$instances as $name => $db)
  111. {
  112. $group_name = 'database (' . strtolower($name) . ')';
  113. $group = arr::get($groups, $group_name, FALSE);
  114. if ($group)
  115. {
  116. $sub_time = $sub_memory = $sub_count = 0;
  117. foreach ($group as $query => $tokens)
  118. {
  119. $sub_count += count($tokens);
  120. foreach ($tokens as $token)
  121. {
  122. $total = Profiler::total($token);
  123. $sub_time += $total[0];
  124. $sub_memory += $total[1];
  125. $result[$name][] = array('name' => $query, 'time' => $total[0], 'memory' => $total[1]);
  126. }
  127. }
  128. $count += $sub_count;
  129. $time += $sub_time;
  130. $memory += $sub_memory;
  131. $result[$name]['total'] = array($sub_count, $sub_time, $sub_memory);
  132. }
  133. }
  134. self::$_queries = array('count' => $count, 'time' => $time, 'memory' => $memory, 'data' => $result);
  135. return self::$_queries;
  136. }
  137. /**
  138. * Creates a formatted array of all Benchmarks
  139. *
  140. * @return array formatted benchmarks
  141. */
  142. public static function get_benchmarks()
  143. {
  144. if (Kohana::$profiling == FALSE)
  145. {
  146. return array();
  147. }
  148. if (self::$_benchmarks !== FALSE)
  149. {
  150. return self::$_benchmarks;
  151. }
  152. $groups = Profiler::groups();
  153. $result = array();
  154. foreach(array_keys($groups) as $group)
  155. {
  156. if (strpos($group, 'database (') === FALSE)
  157. {
  158. foreach($groups[$group] as $name => $marks)
  159. {
  160. $stats = Profiler::stats($marks);
  161. $result[$group][] = array
  162. (
  163. 'name' => $name,
  164. 'count' => count($marks),
  165. 'total_time' => $stats['total']['time'],
  166. 'avg_time' => $stats['average']['time'],
  167. 'total_memory' => $stats['total']['memory'],
  168. 'avg_memory' => $stats['average']['memory'],
  169. );
  170. }
  171. }
  172. }
  173. // add total stats
  174. $total = Profiler::application();
  175. $result['application'] = array
  176. (
  177. 'count' => 1,
  178. 'total_time' => $total['current']['time'],
  179. 'avg_time' => $total['average']['time'],
  180. 'total_memory' => $total['current']['memory'],
  181. 'avg_memory' => $total['average']['memory'],
  182. );
  183. self::$_benchmarks = $result;
  184. return $result;
  185. }
  186. /**
  187. * Get list of included files
  188. *
  189. * @return array file currently included by php
  190. */
  191. public static function get_files()
  192. {
  193. $files = (array)get_included_files();
  194. sort($files);
  195. return $files;
  196. }
  197. /**
  198. *
  199. * @return array module_name => module_path
  200. */
  201. public static function get_modules()
  202. {
  203. return Kohana::modules();
  204. }
  205. public static function get_routes()
  206. {
  207. return Route::all();
  208. }
  209. /**
  210. * Add toolbar data to FirePHP console
  211. *
  212. */
  213. private static function firephp()
  214. {
  215. $firephp = FirePHP::getInstance(TRUE);
  216. $firephp->fb('KOHANA DEBUG TOOLBAR:');
  217. // Globals
  218. $globals = array(
  219. 'Post' => empty($_POST) ? array() : $_POST,
  220. 'Get' => empty($_GET) ? array() : $_GET,
  221. 'Cookie' => empty($_COOKIE) ? array() : $_COOKIE,
  222. 'Session' => empty($_SESSION) ? array() : $_SESSION
  223. );
  224. foreach ($globals as $name => $global)
  225. {
  226. $table = array();
  227. $table[] = array($name,'Value');
  228. foreach((array)$global as $key => $value)
  229. {
  230. if (is_object($value))
  231. {
  232. $value = get_class($value).' [object]';
  233. }
  234. $table[] = array($key, $value);
  235. }
  236. $message = "$name: ".count($global).' variables';
  237. $firephp->fb(array($message, $table), FirePHP::TABLE);
  238. }
  239. // Database
  240. $query_stats = self::get_queries();
  241. //$total_time = $total_rows = 0;
  242. $table = array();
  243. $table[] = array('DB profile', 'SQL Statement','Time','Memory');
  244. foreach ((array)$query_stats['data'] as $db => $queries)
  245. {unset($queries['total']);
  246. foreach ($queries as $query)
  247. {
  248. $table[] = array(
  249. $db,
  250. str_replace("\n",' ',$query['name']),
  251. number_format($query['time']*1000, 3),
  252. number_format($query['memory']/1024, 3),
  253. );
  254. }
  255. }
  256. $message = 'Queries: '.$query_stats['count'].' SQL queries took '.
  257. number_format($query_stats['time'], 3).' seconds and '.$query_stats['memory'].' b';
  258. $firephp->fb(array($message, $table), FirePHP::TABLE);
  259. // Benchmarks
  260. $groups = self::get_benchmarks();
  261. // application benchmarks
  262. $total = array_pop($groups);
  263. $table = array();
  264. $table[] = array('Group', 'Benchmark', 'Count', 'Time', 'Memory');
  265. foreach ((array)$groups as $group => $benchmarks)
  266. {
  267. foreach ((array)$benchmarks as $name => $benchmark)
  268. {
  269. $table[] = array(
  270. ucfirst($group),
  271. ucwords($benchmark['name']),
  272. number_format($benchmark['total_time'], 3). ' s',
  273. text::bytes($benchmark['total_memory']),
  274. );
  275. }
  276. }
  277. $message = 'Application tooks '.number_format($total['total_time'], 3).' seconds and '.text::bytes($total['total_memory']).' memory';
  278. $firephp->fb(array($message, $table), FirePHP::TABLE);
  279. }
  280. /**
  281. * Determines if all the conditions are correct to display the toolbar
  282. * (pretty kludgy, I know)
  283. *
  284. * @returns bool toolbar enabled
  285. */
  286. public static function is_enabled()
  287. {
  288. // Don't auto render toolbar for ajax requests
  289. if (Request::$is_ajax)
  290. return FALSE;
  291. // Don't auto render toolbar if $_GET['debug'] = 'false'
  292. if (isset($_GET['debug']) and strtolower($_GET['debug']) == 'false')
  293. return FALSE;
  294. // Don't auto render if auto_render config is FALSE
  295. if (Kohana::config('debug_toolbar.auto_render') !== TRUE)
  296. return FALSE;
  297. // Auto render if secret key isset
  298. $secret_key = Kohana::config('debug_toolbar.secret_key');
  299. if ($secret_key !== FALSE and isset($_GET[$secret_key]))
  300. return TRUE;
  301. // Don't auto render when IN_PRODUCTION (this can obviously be
  302. // overridden by the above secret key)
  303. if (IN_PRODUCTION)
  304. return FALSE;
  305. return TRUE;
  306. }
  307. }