PageRenderTime 27ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/daholicofneki/IDC-Payroll
PHP | 507 lines | 284 code | 86 blank | 137 comment | 40 complexity | 67058558dd514234080e338ef1f0211d 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. // Key words we want bolded
  149. $highlight = array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'AND', 'LEFT&nbsp;JOIN', 'ORDER&nbsp;BY', 'GROUP&nbsp;BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR&nbsp;', 'HAVING', 'OFFSET', 'NOT&nbsp;IN', 'IN', 'LIKE', 'NOT&nbsp;LIKE', 'COUNT', 'MAX', 'MIN', 'ON', 'AS', 'AVG', 'SUM', '(', ')');
  150. foreach ($dbs as $db)
  151. {
  152. if (count($db->queries) == 0)
  153. {
  154. $output = $this->CI->lang->line('profiler_no_queries');
  155. }
  156. else
  157. {
  158. foreach ($db->queries as $key => $val)
  159. {
  160. $time = number_format($db->query_times[$key], 4);
  161. /*
  162. $val = highlight_code($val, ENT_QUOTES);
  163. foreach ($highlight as $bold)
  164. {
  165. $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
  166. }
  167. */
  168. $output[$time] = $val;
  169. }
  170. }
  171. }
  172. return $output;
  173. }
  174. // --------------------------------------------------------------------
  175. /**
  176. * Compile $_GET Data
  177. *
  178. * @return string
  179. */
  180. protected function _compile_get()
  181. {
  182. $output = array();
  183. if (count($_GET) == 0)
  184. {
  185. $output = $this->CI->lang->line('profiler_no_get');
  186. }
  187. else
  188. {
  189. foreach ($_GET as $key => $val)
  190. {
  191. if ( ! is_numeric($key))
  192. {
  193. $key = "'".$key."'";
  194. }
  195. $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;'>";
  196. if (is_array($val))
  197. {
  198. $output['&#36;_GET['. $key .']'] = "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
  199. }
  200. else
  201. {
  202. $output['&#36;_GET['. $key .']'] = htmlspecialchars(stripslashes($val));
  203. }
  204. }
  205. }
  206. return $output;
  207. }
  208. // --------------------------------------------------------------------
  209. /**
  210. * Compile $_POST Data
  211. *
  212. * @return string
  213. */
  214. protected function _compile_post()
  215. {
  216. $output = array();
  217. if (count($_POST) == 0)
  218. {
  219. $output = $this->CI->lang->line('profiler_no_post');
  220. }
  221. else
  222. {
  223. foreach ($_POST as $key => $val)
  224. {
  225. if ( ! is_numeric($key))
  226. {
  227. $key = "'".$key."'";
  228. }
  229. if (is_array($val))
  230. {
  231. $output['&#36;_POST['. $key .']'] = '<pre>'. htmlspecialchars(stripslashes(print_r($val, TRUE))) . '</pre>';
  232. }
  233. else
  234. {
  235. $output['&#36;_POST['. $key .']'] = htmlspecialchars(stripslashes($val));
  236. }
  237. }
  238. }
  239. return $output;
  240. }
  241. // --------------------------------------------------------------------
  242. /**
  243. * Show query string
  244. *
  245. * @return string
  246. */
  247. protected function _compile_uri_string()
  248. {
  249. if ($this->CI->uri->uri_string == '')
  250. {
  251. $output = $this->CI->lang->line('profiler_no_uri');
  252. }
  253. else
  254. {
  255. $output = $this->CI->uri->uri_string;
  256. }
  257. return $output;
  258. }
  259. // --------------------------------------------------------------------
  260. /**
  261. * Show the controller and function that were called
  262. *
  263. * @return string
  264. */
  265. protected function _compile_controller_info()
  266. {
  267. $output = $this->CI->router->fetch_class()."/".$this->CI->router->fetch_method();
  268. return $output;
  269. }
  270. // --------------------------------------------------------------------
  271. /**
  272. * Compile memory usage
  273. *
  274. * Display total used memory
  275. *
  276. * @return string
  277. */
  278. protected function _compile_memory_usage()
  279. {
  280. if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
  281. {
  282. $output = number_format($usage) .' bytes';
  283. }
  284. else
  285. {
  286. $output = $this->CI->lang->line('profiler_no_memory_usage');
  287. }
  288. return $output;
  289. }
  290. // --------------------------------------------------------------------
  291. /**
  292. * Compile header information
  293. *
  294. * Lists HTTP headers
  295. *
  296. * @return string
  297. */
  298. protected function _compile_http_headers()
  299. {
  300. $output = array();
  301. 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)
  302. {
  303. $val = (isset($_SERVER[$header])) ? $_SERVER[$header] : '';
  304. $output[$header] = $val;
  305. }
  306. return $output;
  307. }
  308. // --------------------------------------------------------------------
  309. /**
  310. * Compile config information
  311. *
  312. * Lists developer config variables
  313. *
  314. * @return string
  315. */
  316. protected function _compile_config()
  317. {
  318. $output = array();
  319. foreach ($this->CI->config->config as $config=>$val)
  320. {
  321. if (is_array($val))
  322. {
  323. $val = print_r($val, TRUE);
  324. }
  325. $output[$config] = htmlspecialchars($val);
  326. }
  327. return $output;
  328. }
  329. // --------------------------------------------------------------------
  330. public function _compile_files()
  331. {
  332. $files = get_included_files();
  333. sort($files);
  334. return $files;
  335. }
  336. //--------------------------------------------------------------------
  337. public function _compile_console()
  338. {
  339. $logs = Console::get_logs();
  340. if ($logs['console'])
  341. {
  342. foreach ($logs['console'] as $key => $log)
  343. {
  344. if ($log['type'] == 'log')
  345. {
  346. $logs['console'][$key]['data'] = print_r($log['data'], true);
  347. }
  348. elseif ($log['type'] == 'memory')
  349. {
  350. $logs['console'][$key]['data'] = $this->get_file_size($log['data']);
  351. }
  352. }
  353. }
  354. //echo '<pre>'; print_r($logs); echo '</pre>';
  355. return $logs;
  356. }
  357. //--------------------------------------------------------------------
  358. function _compile_userdata()
  359. {
  360. $output = array();
  361. $compiled_userdata = $this->CI->session->all_userdata();
  362. if (count($compiled_userdata))
  363. {
  364. foreach ($compiled_userdata as $key => $val)
  365. {
  366. if (is_numeric($key))
  367. {
  368. $output[$key] = "'$val'";
  369. }
  370. if (is_array($val))
  371. {
  372. $output[$key] = htmlspecialchars(stripslashes(print_r($val, true)));
  373. }
  374. else
  375. {
  376. $output[$key] = htmlspecialchars(stripslashes($val));
  377. }
  378. }
  379. }
  380. return $output;
  381. }
  382. //--------------------------------------------------------------------
  383. public static function get_file_size($size, $retstring = null) {
  384. // adapted from code at http://aidanlister.com/repos/v/function.size_readable.php
  385. $sizes = array('bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  386. if ($retstring === null) { $retstring = '%01.2f %s'; }
  387. $lastsizestring = end($sizes);
  388. foreach ($sizes as $sizestring) {
  389. if ($size < 1024) { break; }
  390. if ($sizestring != $lastsizestring) { $size /= 1024; }
  391. }
  392. if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't normally fractional
  393. return sprintf($retstring, $size, $sizestring);
  394. }
  395. //--------------------------------------------------------------------
  396. /**
  397. * Run the Profiler
  398. *
  399. * @return string
  400. */
  401. public function run()
  402. {
  403. $this->CI->load->helper('language');
  404. $fields_displayed = 0;
  405. foreach ($this->_available_sections as $section)
  406. {
  407. if ($this->_compile_{$section} !== FALSE)
  408. {
  409. $func = "_compile_{$section}";
  410. $this->_sections[$section] = $this->{$func}();
  411. $fields_displayed++;
  412. }
  413. }
  414. return $this->CI->load->view('profiler_template', array('sections' => $this->_sections), true);
  415. }
  416. }
  417. // END CI_Profiler class
  418. //--------------------------------------------------------------------
  419. /* End of file Profiler.php */
  420. /* Location: ./system/libraries/Profiler.php */