PageRenderTime 33ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/sparks/Debug-Toolbar/1.0.7/libraries/Profiler.php

https://bitbucket.org/daholicofneki/ams
PHP | 497 lines | 284 code | 85 blank | 128 comment | 40 complexity | b38f64c6bde2521176ec4550642181bd MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * CodeIgniter Profiler Class
  18. *
  19. * This class enables you to display benchmark, query, and other data
  20. * in order to help with debugging and optimization.
  21. *
  22. * Note: At some point it would be good to move all the HTML in this class
  23. * into a set of template files in order to allow customization.
  24. *
  25. * @package CodeIgniter
  26. * @subpackage Libraries
  27. * @category Libraries
  28. * @author ExpressionEngine Dev Team
  29. * @link http://codeigniter.com/user_guide/general/profiling.html
  30. */
  31. class CI_Profiler extends CI_Loader {
  32. var $CI;
  33. protected $_available_sections = array(
  34. 'benchmarks',
  35. 'get',
  36. 'memory_usage',
  37. 'post',
  38. 'uri_string',
  39. 'controller_info',
  40. 'queries',
  41. 'http_headers',
  42. 'config',
  43. 'files',
  44. 'console',
  45. 'userdata'
  46. );
  47. protected $_sections = array(); // Stores _compile_x() results
  48. // --------------------------------------------------------------------
  49. public function __construct($config = array())
  50. {
  51. $this->CI =& get_instance();
  52. $this->CI->load->language('profiler');
  53. $this->CI->load->library('session');
  54. // default all sections to display
  55. foreach ($this->_available_sections as $section)
  56. {
  57. if ( ! isset($config[$section]))
  58. {
  59. $this->_compile_{$section} = TRUE;
  60. }
  61. }
  62. // Make sure the Console is loaded.
  63. if (!class_exists('Console'))
  64. {
  65. $this->CI->load->library('Console');
  66. }
  67. $this->set_sections($config);
  68. }
  69. // --------------------------------------------------------------------
  70. /**
  71. * Set Sections
  72. *
  73. * Sets the private _compile_* properties to enable/disable Profiler sections
  74. *
  75. * @param mixed
  76. * @return void
  77. */
  78. public function set_sections($config)
  79. {
  80. foreach ($config as $method => $enable)
  81. {
  82. if (in_array($method, $this->_available_sections))
  83. {
  84. $this->_compile_{$method} = ($enable !== FALSE) ? TRUE : FALSE;
  85. }
  86. }
  87. }
  88. // --------------------------------------------------------------------
  89. /**
  90. * Auto Profiler
  91. *
  92. * This function cycles through the entire array of mark points and
  93. * matches any two points that are named identically (ending in "_start"
  94. * and "_end" respectively). It then compiles the execution times for
  95. * all points and returns it as an array
  96. *
  97. * @return array
  98. */
  99. protected function _compile_benchmarks()
  100. {
  101. $profile = array();
  102. $output = array();
  103. foreach ($this->CI->benchmark->marker as $key => $val)
  104. {
  105. // We match the "end" marker so that the list ends
  106. // up in the order that it was defined
  107. if (preg_match("/(.+?)_end/i", $key, $match))
  108. {
  109. if (isset($this->CI->benchmark->marker[$match[1].'_end']) AND isset($this->CI->benchmark->marker[$match[1].'_start']))
  110. {
  111. $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
  112. }
  113. }
  114. }
  115. // Build a table containing the profile data.
  116. // Note: At some point we might want to make this data available to be logged.
  117. foreach ($profile as $key => $val)
  118. {
  119. $key = ucwords(str_replace(array('_', '-'), ' ', $key));
  120. $output[$key] = $val;
  121. }
  122. return $output;
  123. }
  124. // --------------------------------------------------------------------
  125. /**
  126. * Compile Queries
  127. *
  128. * @return string
  129. */
  130. protected function _compile_queries()
  131. {
  132. $dbs = array();
  133. $output = array();
  134. // Let's determine which databases are currently connected to
  135. foreach (get_object_vars($this->CI) as $CI_object)
  136. {
  137. if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB') )
  138. {
  139. $dbs[] = $CI_object;
  140. }
  141. }
  142. if (count($dbs) == 0)
  143. {
  144. return $this->CI->lang->line('profiler_no_db');
  145. }
  146. // Load the text helper so we can highlight the SQL
  147. $this->CI->load->helper('text');
  148. foreach ($dbs as $db)
  149. {
  150. if (count($db->queries) == 0)
  151. {
  152. $output = $this->CI->lang->line('profiler_no_queries');
  153. }
  154. else
  155. {
  156. foreach ($db->queries as $key => $val)
  157. {
  158. $time = number_format($db->query_times[$key], 4);
  159. $val = highlight_code($val, ENT_QUOTES);
  160. $output[] = array($time, $val);
  161. }
  162. }
  163. }
  164. return $output;
  165. }
  166. // --------------------------------------------------------------------
  167. /**
  168. * Compile $_GET Data
  169. *
  170. * @return string
  171. */
  172. protected function _compile_get()
  173. {
  174. $output = array();
  175. if (count($_GET) == 0)
  176. {
  177. $output = $this->CI->lang->line('profiler_no_get');
  178. }
  179. else
  180. {
  181. foreach ($_GET as $key => $val)
  182. {
  183. if ( ! is_numeric($key))
  184. {
  185. $key = "'".$key."'";
  186. }
  187. $output .= "<tr><td style='width:50%;color:#000;background-color:#ddd;padding:5px'>&#36;_GET[".$key."]&nbsp;&nbsp; </td><td style='width:50%;padding:5px;color:#cd6e00;font-weight:normal;background-color:#ddd;'>";
  188. if (is_array($val))
  189. {
  190. $output['&#36;_GET['. $key .']'] = "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
  191. }
  192. else
  193. {
  194. $output['&#36;_GET['. $key .']'] = htmlspecialchars(stripslashes($val));
  195. }
  196. }
  197. }
  198. return $output;
  199. }
  200. // --------------------------------------------------------------------
  201. /**
  202. * Compile $_POST Data
  203. *
  204. * @return string
  205. */
  206. protected function _compile_post()
  207. {
  208. $output = array();
  209. if (count($_POST) == 0)
  210. {
  211. $output = $this->CI->lang->line('profiler_no_post');
  212. }
  213. else
  214. {
  215. foreach ($_POST as $key => $val)
  216. {
  217. if ( ! is_numeric($key))
  218. {
  219. $key = "'".$key."'";
  220. }
  221. if (is_array($val))
  222. {
  223. $output['&#36;_POST['. $key .']'] = '<pre>'. htmlspecialchars(stripslashes(print_r($val, TRUE))) . '</pre>';
  224. }
  225. else
  226. {
  227. $output['&#36;_POST['. $key .']'] = htmlspecialchars(stripslashes($val));
  228. }
  229. }
  230. }
  231. return $output;
  232. }
  233. // --------------------------------------------------------------------
  234. /**
  235. * Show query string
  236. *
  237. * @return string
  238. */
  239. protected function _compile_uri_string()
  240. {
  241. if ($this->CI->uri->uri_string == '')
  242. {
  243. $output = $this->CI->lang->line('profiler_no_uri');
  244. }
  245. else
  246. {
  247. $output = $this->CI->uri->uri_string;
  248. }
  249. return $output;
  250. }
  251. // --------------------------------------------------------------------
  252. /**
  253. * Show the controller and function that were called
  254. *
  255. * @return string
  256. */
  257. protected function _compile_controller_info()
  258. {
  259. $output = $this->CI->router->fetch_class()."/".$this->CI->router->fetch_method();
  260. return $output;
  261. }
  262. // --------------------------------------------------------------------
  263. /**
  264. * Compile memory usage
  265. *
  266. * Display total used memory
  267. *
  268. * @return string
  269. */
  270. protected function _compile_memory_usage()
  271. {
  272. if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
  273. {
  274. $output = number_format($usage) .' bytes';
  275. }
  276. else
  277. {
  278. $output = $this->CI->lang->line('profiler_no_memory_usage');
  279. }
  280. return $output;
  281. }
  282. // --------------------------------------------------------------------
  283. /**
  284. * Compile header information
  285. *
  286. * Lists HTTP headers
  287. *
  288. * @return string
  289. */
  290. protected function _compile_http_headers()
  291. {
  292. $output = array();
  293. foreach (array('HTTP_ACCEPT', 'HTTP_USER_AGENT', 'HTTP_CONNECTION', 'SERVER_PORT', 'SERVER_NAME', 'REMOTE_ADDR', 'SERVER_SOFTWARE', 'HTTP_ACCEPT_LANGUAGE', 'SCRIPT_NAME', 'REQUEST_METHOD',' HTTP_HOST', 'REMOTE_HOST', 'CONTENT_TYPE', 'SERVER_PROTOCOL', 'QUERY_STRING', 'HTTP_ACCEPT_ENCODING', 'HTTP_X_FORWARDED_FOR') as $header)
  294. {
  295. $val = (isset($_SERVER[$header])) ? $_SERVER[$header] : '';
  296. $output[$header] = $val;
  297. }
  298. return $output;
  299. }
  300. // --------------------------------------------------------------------
  301. /**
  302. * Compile config information
  303. *
  304. * Lists developer config variables
  305. *
  306. * @return string
  307. */
  308. protected function _compile_config()
  309. {
  310. $output = array();
  311. foreach ($this->CI->config->config as $config=>$val)
  312. {
  313. if (is_array($val))
  314. {
  315. $val = print_r($val, TRUE);
  316. }
  317. $output[$config] = htmlspecialchars($val);
  318. }
  319. return $output;
  320. }
  321. // --------------------------------------------------------------------
  322. public function _compile_files()
  323. {
  324. $files = get_included_files();
  325. sort($files);
  326. return $files;
  327. }
  328. //--------------------------------------------------------------------
  329. public function _compile_console()
  330. {
  331. $logs = Console::get_logs();
  332. if ($logs['console'])
  333. {
  334. foreach ($logs['console'] as $key => $log)
  335. {
  336. if ($log['type'] == 'log')
  337. {
  338. $logs['console'][$key]['data'] = print_r($log['data'], true);
  339. }
  340. elseif ($log['type'] == 'memory')
  341. {
  342. $logs['console'][$key]['data'] = $this->get_file_size($log['data']);
  343. }
  344. }
  345. }
  346. //echo '<pre>'; print_r($logs); echo '</pre>';
  347. return $logs;
  348. }
  349. //--------------------------------------------------------------------
  350. function _compile_userdata()
  351. {
  352. $output = array();
  353. $compiled_userdata = $this->CI->session->all_userdata();
  354. if (count($compiled_userdata))
  355. {
  356. foreach ($compiled_userdata as $key => $val)
  357. {
  358. if (is_numeric($key))
  359. {
  360. $output[$key] = "'$val'";
  361. }
  362. if (is_array($val))
  363. {
  364. $output[$key] = htmlspecialchars(stripslashes(print_r($val, true)));
  365. }
  366. else
  367. {
  368. $output[$key] = htmlspecialchars(stripslashes($val));
  369. }
  370. }
  371. }
  372. return $output;
  373. }
  374. //--------------------------------------------------------------------
  375. public static function get_file_size($size, $retstring = null) {
  376. // adapted from code at http://aidanlister.com/repos/v/function.size_readable.php
  377. $sizes = array('bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  378. if ($retstring === null) { $retstring = '%01.2f %s'; }
  379. $lastsizestring = end($sizes);
  380. foreach ($sizes as $sizestring) {
  381. if ($size < 1024) { break; }
  382. if ($sizestring != $lastsizestring) { $size /= 1024; }
  383. }
  384. if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't normally fractional
  385. return sprintf($retstring, $size, $sizestring);
  386. }
  387. //--------------------------------------------------------------------
  388. /**
  389. * Run the Profiler
  390. *
  391. * @return string
  392. */
  393. public function run()
  394. {
  395. $this->CI->load->helper('language');
  396. $fields_displayed = 0;
  397. foreach ($this->_available_sections as $section)
  398. {
  399. if ($this->_compile_{$section} !== FALSE)
  400. {
  401. $func = "_compile_{$section}";
  402. $this->_sections[$section] = $this->{$func}();
  403. $fields_displayed++;
  404. }
  405. }
  406. return $this->CI->load->view('profiler_template', array('sections' => $this->_sections), true);
  407. }
  408. }
  409. // END CI_Profiler class
  410. //--------------------------------------------------------------------
  411. /* End of file Profiler.php */
  412. /* Location: ./system/libraries/Profiler.php */