1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
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
16 // ------------------------------------------------------------------------
19 * CodeIgniter Profiler Class
21 * This class enables you to display benchmark, query, and other data
22 * in order to help with debugging and optimization.
24 * Note: At some point it would be good to move all the HTML in this class
25 * into a set of template files in order to allow customization.
27 * @package CodeIgniter
28 * @subpackage Libraries
30 * @author ExpressionEngine Dev Team
31 * @link http://codeigniter.com/user_guide/general/profiling.html
37 protected $_available_sections = array(
49 public function __construct($config = array())
51 $this->CI =& get_instance();
52 $this->CI->load->language('profiler');
54 // default all sections to display
55 foreach ($this->_available_sections as $section)
57 if ( ! isset($config[$section]))
59 $this->_compile_{$section} = TRUE;
63 $this->set_sections($config);
66 // --------------------------------------------------------------------
71 * Sets the private _compile_* properties to enable/disable Profiler sections
76 public function set_sections($config)
78 foreach ($config as $method => $enable)
80 if (in_array($method, $this->_available_sections))
82 $this->_compile_{$method} = ($enable !== FALSE) ? TRUE : FALSE;
87 // --------------------------------------------------------------------
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
99 protected function _compile_benchmarks()
102 foreach ($this->CI->benchmark->marker as $key => $val)
104 // We match the "end" marker so that the list ends
105 // up in the order that it was defined
106 if (preg_match("/(.+?)_end/i", $key, $match))
108 if (isset($this->CI->benchmark->marker[$match[1].'_end']) AND isset($this->CI->benchmark->marker[$match[1].'_start']))
110 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
115 // Build a table containing the profile data.
116 // Note: At some point we should turn this into a template that can
117 // be modified. We also might want to make this data available to be logged
120 $output .= '<fieldset id="ci_profiler_benchmarks" style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
122 $output .= '<legend style="color:#900;"> '.$this->CI->lang->line('profiler_benchmarks').' </legend>';
124 $output .= "\n\n<table style='width:100%'>\n";
126 foreach ($profile as $key => $val)
128 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
129 $output .= "<tr><td style='padding:5px;width:50%;color:#000;font-weight:bold;background-color:#ddd;'>".$key." </td><td style='padding:5px;width:50%;color:#900;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
132 $output .= "</table>\n";
133 $output .= "</fieldset>";
138 // --------------------------------------------------------------------
145 protected function _compile_queries()
149 // Let's determine which databases are currently connected to
150 foreach (get_object_vars($this->CI) as $CI_object)
152 if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB') )
158 if (count($dbs) == 0)
161 $output .= '<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
163 $output .= '<legend style="color:#0000FF;"> '.$this->CI->lang->line('profiler_queries').' </legend>';
165 $output .= "\n\n<table style='border:none; width:100%'>\n";
166 $output .="<tr><td style='width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px'>".$this->CI->lang->line('profiler_no_db')."</td></tr>\n";
167 $output .= "</table>\n";
168 $output .= "</fieldset>";
173 // Load the text helper so we can highlight the SQL
174 $this->CI->load->helper('text');
176 // Key words we want bolded
177 $highlight = array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'AND', 'LEFT JOIN', 'ORDER BY', 'GROUP BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR ', 'HAVING', 'OFFSET', 'NOT IN', 'IN', 'LIKE', 'NOT LIKE', 'COUNT', 'MAX', 'MIN', 'ON', 'AS', 'AVG', 'SUM', '(', ')');
181 foreach ($dbs as $db)
183 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
185 $output .= '<legend style="color:#0000FF;"> '.$this->CI->lang->line('profiler_database').': '.$db->database.' '.$this->CI->lang->line('profiler_queries').': '.count($db->queries).' </legend>';
187 $output .= "\n\n<table style='width:100%;'>\n";
189 if (count($db->queries) == 0)
191 $output .= "<tr><td style='width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;'>".$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
195 foreach ($db->queries as $key => $val)
197 $time = number_format($db->query_times[$key], 4);
199 $val = highlight_code($val, ENT_QUOTES);
201 foreach ($highlight as $bold)
203 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
206 $output .= "<tr><td style='padding:5px; vertical-align: top;width:1%;color:#900;font-weight:normal;background-color:#ddd;'>".$time." </td><td style='padding:5px; color:#000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
210 $output .= "</table>\n";
211 $output .= "</fieldset>";
219 // --------------------------------------------------------------------
226 protected function _compile_get()
229 $output .= '<fieldset id="ci_profiler_get" style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
231 $output .= '<legend style="color:#cd6e00;"> '.$this->CI->lang->line('profiler_get_data').' </legend>';
234 if (count($_GET) == 0)
236 $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_get')."</div>";
240 $output .= "\n\n<table style='width:100%; border:none'>\n";
242 foreach ($_GET as $key => $val)
244 if ( ! is_numeric($key))
249 $output .= "<tr><td style='width:50%;color:#000;background-color:#ddd;padding:5px'>$_GET[".$key."] </td><td style='width:50%;padding:5px;color:#cd6e00;font-weight:normal;background-color:#ddd;'>";
252 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
256 $output .= htmlspecialchars(stripslashes($val));
258 $output .= "</td></tr>\n";
261 $output .= "</table>\n";
263 $output .= "</fieldset>";
268 // --------------------------------------------------------------------
271 * Compile $_POST Data
275 protected function _compile_post()
278 $output .= '<fieldset id="ci_profiler_post" style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
280 $output .= '<legend style="color:#009900;"> '.$this->CI->lang->line('profiler_post_data').' </legend>';
283 if (count($_POST) == 0)
285 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";
289 $output .= "\n\n<table style='width:100%'>\n";
291 foreach ($_POST as $key => $val)
293 if ( ! is_numeric($key))
298 $output .= "<tr><td style='width:50%;padding:5px;color:#000;background-color:#ddd;'>$_POST[".$key."] </td><td style='width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;'>";
301 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, TRUE))) . "</pre>";
305 $output .= htmlspecialchars(stripslashes($val));
307 $output .= "</td></tr>\n";
310 $output .= "</table>\n";
312 $output .= "</fieldset>";
317 // --------------------------------------------------------------------
324 protected function _compile_uri_string()
327 $output .= '<fieldset id="ci_profiler_uri_string" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
329 $output .= '<legend style="color:#000;"> '.$this->CI->lang->line('profiler_uri_string').' </legend>';
332 if ($this->CI->uri->uri_string == '')
334 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_uri')."</div>";
338 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->uri->uri_string."</div>";
341 $output .= "</fieldset>";
346 // --------------------------------------------------------------------
349 * Show the controller and function that were called
353 protected function _compile_controller_info()
356 $output .= '<fieldset id="ci_profiler_controller_info" style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
358 $output .= '<legend style="color:#995300;"> '.$this->CI->lang->line('profiler_controller_info').' </legend>';
361 $output .= "<div style='color:#995300;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->router->fetch_class()."/".$this->CI->router->fetch_method()."</div>";
363 $output .= "</fieldset>";
368 // --------------------------------------------------------------------
371 * Compile memory usage
373 * Display total used memory
377 protected function _compile_memory_usage()
380 $output .= '<fieldset id="ci_profiler_memory_usage" style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
382 $output .= '<legend style="color:#5a0099;"> '.$this->CI->lang->line('profiler_memory_usage').' </legend>';
385 if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
387 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';
391 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_memory_usage')."</div>";
394 $output .= "</fieldset>";
399 // --------------------------------------------------------------------
402 * Compile header information
408 protected function _compile_http_headers()
411 $output .= '<fieldset id="ci_profiler_http_headers" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
413 $output .= '<legend style="color:#000;"> '.$this->CI->lang->line('profiler_headers').' </legend>';
416 $output .= "\n\n<table style='width:100%'>\n";
418 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)
420 $val = (isset($_SERVER[$header])) ? $_SERVER[$header] : '';
421 $output .= "<tr><td style='vertical-align: top;width:50%;padding:5px;color:#900;background-color:#ddd;'>".$header." </td><td style='width:50%;padding:5px;color:#000;background-color:#ddd;'>".$val."</td></tr>\n";
424 $output .= "</table>\n";
425 $output .= "</fieldset>";
430 // --------------------------------------------------------------------
433 * Compile config information
435 * Lists developer config variables
439 protected function _compile_config()
442 $output .= '<fieldset id="ci_profiler_config" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
444 $output .= '<legend style="color:#000;"> '.$this->CI->lang->line('profiler_config').' </legend>';
447 $output .= "\n\n<table style='width:100%'>\n";
449 foreach ($this->CI->config->config as $config=>$val)
453 $val = print_r($val, TRUE);
456 $output .= "<tr><td style='padding:5px; vertical-align: top;color:#900;background-color:#ddd;'>".$config." </td><td style='padding:5px; color:#000;background-color:#ddd;'>".htmlspecialchars($val)."</td></tr>\n";
459 $output .= "</table>\n";
460 $output .= "</fieldset>";
465 // --------------------------------------------------------------------
472 public function run()
474 $output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
475 $fields_displayed = 0;
477 foreach ($this->_available_sections as $section)
479 if ($this->_compile_{$section} !== FALSE)
481 $func = "_compile_{$section}";
482 $output .= $this->{$func}();
487 if ($fields_displayed == 0)
489 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee">'.$this->CI->lang->line('profiler_no_profiles').'</p>';
499 // END CI_Profiler class
501 /* End of file Profiler.php */
502 /* Location: ./system/libraries/Profiler.php */