blob: d1828b9840c8957b89a33440079e32baf6fc3b19 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Profiler Class
20 *
21 * This class enables you to display benchmark, query, and other data
22 * in order to help with debugging and optimization.
23 *
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.
26 *
27 * @package CodeIgniter
28 * @subpackage Libraries
29 * @category Libraries
30 * @author ExpressionEngine Dev Team
31 * @link http://codeigniter.com/user_guide/general/profiling.html
32 */
33class CI_Profiler {
34
Greg Aker62df1312011-04-18 11:18:02 -050035 private $CI;
Barry Mienydd671972010-10-04 16:33:58 +020036
Greg Aker5ac55942010-11-10 14:59:47 -060037 protected $_available_sections = array(
Derek Jonesee71c802010-03-10 10:05:05 -060038 'benchmarks',
Derek Jonesee71c802010-03-10 10:05:05 -060039 'get',
Derek Jonesee71c802010-03-10 10:05:05 -060040 'memory_usage',
41 'post',
Greg Akere6026832010-04-29 13:41:39 -050042 'uri_string',
43 'controller_info',
Derek Jonesee71c802010-03-10 10:05:05 -060044 'queries',
Greg Akere6026832010-04-29 13:41:39 -050045 'http_headers',
Greg Aker62df1312011-04-18 11:18:02 -050046 'session_data',
Greg Akere6026832010-04-29 13:41:39 -050047 'config'
Derek Jonesee71c802010-03-10 10:05:05 -060048 );
49
Greg Aker5ac55942010-11-10 14:59:47 -060050 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +020051 {
52 $this->CI =& get_instance();
53 $this->CI->load->language('profiler');
54
Derek Jonesee71c802010-03-10 10:05:05 -060055 // default all sections to display
56 foreach ($this->_available_sections as $section)
57 {
58 if ( ! isset($config[$section]))
59 {
60 $this->_compile_{$section} = TRUE;
61 }
62 }
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Jonesee71c802010-03-10 10:05:05 -060064 $this->set_sections($config);
Barry Mienydd671972010-10-04 16:33:58 +020065 }
66
Derek Allard2067d1a2008-11-13 22:59:24 +000067 // --------------------------------------------------------------------
68
69 /**
Derek Jonesee71c802010-03-10 10:05:05 -060070 * Set Sections
71 *
72 * Sets the private _compile_* properties to enable/disable Profiler sections
73 *
Derek Jonesee71c802010-03-10 10:05:05 -060074 * @param mixed
75 * @return void
76 */
Greg Aker5ac55942010-11-10 14:59:47 -060077 public function set_sections($config)
Derek Jonesee71c802010-03-10 10:05:05 -060078 {
79 foreach ($config as $method => $enable)
80 {
81 if (in_array($method, $this->_available_sections))
82 {
Barry Mienydd671972010-10-04 16:33:58 +020083 $this->_compile_{$method} = ($enable !== FALSE) ? TRUE : FALSE;
Derek Jonesee71c802010-03-10 10:05:05 -060084 }
85 }
86 }
87
88 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Jonesee71c802010-03-10 10:05:05 -060090 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000091 * Auto Profiler
92 *
93 * This function cycles through the entire array of mark points and
94 * matches any two points that are named identically (ending in "_start"
95 * and "_end" respectively). It then compiles the execution times for
96 * all points and returns it as an array
Greg Aker5ac55942010-11-10 14:59:47 -060097 *
Derek Allard2067d1a2008-11-13 22:59:24 +000098 * @return array
99 */
Greg Aker5ac55942010-11-10 14:59:47 -0600100 protected function _compile_benchmarks()
Barry Mienydd671972010-10-04 16:33:58 +0200101 {
102 $profile = 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 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000115
116 // Build a table containing the profile data.
117 // Note: At some point we should turn this into a template that can
118 // be modified. We also might want to make this data available to be logged
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600121 $output .= '<fieldset id="ci_profiler_benchmarks" style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 $output .= "\n";
Derek Allard76af4092010-01-16 19:20:49 +0000123 $output .= '<legend style="color:#900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks').'&nbsp;&nbsp;</legend>';
Barry Mienydd671972010-10-04 16:33:58 +0200124 $output .= "\n";
Greg Aker3a563982010-11-18 18:43:03 -0600125 $output .= "\n\n<table style='width:100%'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 foreach ($profile as $key => $val)
128 {
129 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
Greg Aker3a563982010-11-18 18:43:03 -0600130 $output .= "<tr><td style='padding:5px;width:50%;color:#000;font-weight:bold;background-color:#ddd;'>".$key."&nbsp;&nbsp;</td><td style='padding:5px;width:50%;color:#900;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 $output .= "</table>\n";
134 $output .= "</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200135
136 return $output;
137 }
138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 // --------------------------------------------------------------------
140
141 /**
142 * Compile Queries
143 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200145 */
Greg Aker5ac55942010-11-10 14:59:47 -0600146 protected function _compile_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
148 $dbs = array();
Derek Jonesf0a9b332009-07-29 14:19:18 +0000149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 // Let's determine which databases are currently connected to
151 foreach (get_object_vars($this->CI) as $CI_object)
152 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000153 if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB') )
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
155 $dbs[] = $CI_object;
156 }
157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 if (count($dbs) == 0)
160 {
161 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600162 $output .= '<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 $output .= "\n";
164 $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>';
Barry Mienydd671972010-10-04 16:33:58 +0200165 $output .= "\n";
Greg Aker3a563982010-11-18 18:43:03 -0600166 $output .= "\n\n<table style='border:none; width:100%'>\n";
167 $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";
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 $output .= "</table>\n";
169 $output .= "</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 return $output;
172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 // Load the text helper so we can highlight the SQL
175 $this->CI->load->helper('text');
176
177 // Key words we want bolded
Greg Aker3424bf72010-09-14 17:53:10 -0500178 $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', '(', ')');
Derek Allard2067d1a2008-11-13 22:59:24 +0000179
180 $output = "\n\n";
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 foreach ($dbs as $db)
183 {
184 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
185 $output .= "\n";
Greg Akerccbfbf62010-12-21 13:49:33 -0600186 $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_database').':&nbsp; '.$db->database.'&nbsp;&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').': '.count($db->queries).'&nbsp;&nbsp;&nbsp;</legend>';
Barry Mienydd671972010-10-04 16:33:58 +0200187 $output .= "\n";
Greg Aker3a563982010-11-18 18:43:03 -0600188 $output .= "\n\n<table style='width:100%;'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 if (count($db->queries) == 0)
191 {
Greg Aker3a563982010-11-18 18:43:03 -0600192 $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";
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 }
194 else
Barry Mienydd671972010-10-04 16:33:58 +0200195 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 foreach ($db->queries as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200197 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 $time = number_format($db->query_times[$key], 4);
199
200 $val = highlight_code($val, ENT_QUOTES);
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 foreach ($highlight as $bold)
203 {
Barry Mienydd671972010-10-04 16:33:58 +0200204 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
Barry Mienydd671972010-10-04 16:33:58 +0200206
Greg Aker3a563982010-11-18 18:43:03 -0600207 $output .= "<tr><td style='padding:5px; vertical-align: top;width:1%;color:#900;font-weight:normal;background-color:#ddd;'>".$time."&nbsp;&nbsp;</td><td style='padding:5px; color:#000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 $output .= "</table>\n";
212 $output .= "</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 return $output;
217 }
218
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 // --------------------------------------------------------------------
221
222 /**
223 * Compile $_GET Data
224 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200226 */
Greg Aker5ac55942010-11-10 14:59:47 -0600227 protected function _compile_get()
Barry Mienydd671972010-10-04 16:33:58 +0200228 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600230 $output .= '<fieldset id="ci_profiler_get" style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 $output .= "\n";
232 $output .= '<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data').'&nbsp;&nbsp;</legend>';
233 $output .= "\n";
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 if (count($_GET) == 0)
236 {
237 $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_get')."</div>";
238 }
239 else
240 {
Greg Aker3a563982010-11-18 18:43:03 -0600241 $output .= "\n\n<table style='width:100%; border:none'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 foreach ($_GET as $key => $val)
244 {
245 if ( ! is_numeric($key))
246 {
247 $key = "'".$key."'";
248 }
Barry Mienydd671972010-10-04 16:33:58 +0200249
Greg Aker3a563982010-11-18 18:43:03 -0600250 $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;'>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 if (is_array($val))
252 {
253 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
254 }
255 else
256 {
257 $output .= htmlspecialchars(stripslashes($val));
258 }
259 $output .= "</td></tr>\n";
260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 $output .= "</table>\n";
263 }
264 $output .= "</fieldset>";
265
Barry Mienydd671972010-10-04 16:33:58 +0200266 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 /**
272 * Compile $_POST Data
273 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200275 */
Greg Aker5ac55942010-11-10 14:59:47 -0600276 protected function _compile_post()
Barry Mienydd671972010-10-04 16:33:58 +0200277 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600279 $output .= '<fieldset id="ci_profiler_post" style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 $output .= "\n";
281 $output .= '<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data').'&nbsp;&nbsp;</legend>';
282 $output .= "\n";
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 if (count($_POST) == 0)
285 {
286 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";
287 }
288 else
289 {
Greg Aker3a563982010-11-18 18:43:03 -0600290 $output .= "\n\n<table style='width:100%'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 foreach ($_POST as $key => $val)
293 {
294 if ( ! is_numeric($key))
295 {
296 $key = "'".$key."'";
297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Greg Aker3a563982010-11-18 18:43:03 -0600299 $output .= "<tr><td style='width:50%;padding:5px;color:#000;background-color:#ddd;'>&#36;_POST[".$key."]&nbsp;&nbsp; </td><td style='width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;'>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 if (is_array($val))
301 {
Derek Jones511e3d72010-05-13 09:03:30 -0500302 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, TRUE))) . "</pre>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
304 else
305 {
306 $output .= htmlspecialchars(stripslashes($val));
307 }
308 $output .= "</td></tr>\n";
309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 $output .= "</table>\n";
312 }
313 $output .= "</fieldset>";
314
Barry Mienydd671972010-10-04 16:33:58 +0200315 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 /**
321 * Show query string
322 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200324 */
Greg Aker5ac55942010-11-10 14:59:47 -0600325 protected function _compile_uri_string()
Barry Mienydd671972010-10-04 16:33:58 +0200326 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600328 $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">';
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 $output .= "\n";
330 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string').'&nbsp;&nbsp;</legend>';
331 $output .= "\n";
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 if ($this->CI->uri->uri_string == '')
334 {
335 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_uri')."</div>";
336 }
337 else
338 {
Barry Mienydd671972010-10-04 16:33:58 +0200339 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->uri->uri_string."</div>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 $output .= "</fieldset>";
343
Barry Mienydd671972010-10-04 16:33:58 +0200344 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
346
347 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 /**
350 * Show the controller and function that were called
351 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200353 */
Greg Aker5ac55942010-11-10 14:59:47 -0600354 protected function _compile_controller_info()
Barry Mienydd671972010-10-04 16:33:58 +0200355 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600357 $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">';
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 $output .= "\n";
359 $output .= '<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info').'&nbsp;&nbsp;</legend>';
360 $output .= "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000361
Barry Mienydd671972010-10-04 16:33:58 +0200362 $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
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 $output .= "</fieldset>";
365
Barry Mienydd671972010-10-04 16:33:58 +0200366 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
Derek Allard76af4092010-01-16 19:20:49 +0000368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 /**
372 * Compile memory usage
373 *
374 * Display total used memory
375 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * @return string
377 */
Greg Aker5ac55942010-11-10 14:59:47 -0600378 protected function _compile_memory_usage()
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
380 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600381 $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">';
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 $output .= "\n";
383 $output .= '<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage').'&nbsp;&nbsp;</legend>';
384 $output .= "\n";
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
387 {
388 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';
389 }
390 else
391 {
Barry Mienydd671972010-10-04 16:33:58 +0200392 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_memory_usage')."</div>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 $output .= "</fieldset>";
396
397 return $output;
398 }
399
400 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 /**
Derek Allard76af4092010-01-16 19:20:49 +0000403 * Compile header information
404 *
405 * Lists HTTP headers
406 *
Derek Allard76af4092010-01-16 19:20:49 +0000407 * @return string
408 */
Greg Aker5ac55942010-11-10 14:59:47 -0600409 protected function _compile_http_headers()
Derek Allard76af4092010-01-16 19:20:49 +0000410 {
411 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600412 $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">';
Derek Allard76af4092010-01-16 19:20:49 +0000413 $output .= "\n";
Greg Aker62df1312011-04-18 11:18:02 -0500414 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_headers').'&nbsp;&nbsp;(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_httpheaders_table\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI->lang->line('profiler_section_show').'\'?\''.$this->CI->lang->line('profiler_section_hide').'\':\''.$this->CI->lang->line('profiler_section_show').'\';">'.$this->CI->lang->line('profiler_section_show').'</span>)</legend>';
Derek Allard76af4092010-01-16 19:20:49 +0000415 $output .= "\n";
416
Greg Aker62df1312011-04-18 11:18:02 -0500417 $output .= "\n\n<table style='width:100%;display:none' id='ci_profiler_httpheaders_table'>\n";
Derek Allard76af4092010-01-16 19:20:49 +0000418
Pascal Kriete14287f32011-02-14 13:39:34 -0500419 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)
Derek Allard76af4092010-01-16 19:20:49 +0000420 {
421 $val = (isset($_SERVER[$header])) ? $_SERVER[$header] : '';
Greg Aker3a563982010-11-18 18:43:03 -0600422 $output .= "<tr><td style='vertical-align: top;width:50%;padding:5px;color:#900;background-color:#ddd;'>".$header."&nbsp;&nbsp;</td><td style='width:50%;padding:5px;color:#000;background-color:#ddd;'>".$val."</td></tr>\n";
Derek Allard76af4092010-01-16 19:20:49 +0000423 }
424
425 $output .= "</table>\n";
426 $output .= "</fieldset>";
427
Derek Allard76af4092010-01-16 19:20:49 +0000428 return $output;
429 }
430
431 // --------------------------------------------------------------------
432
433 /**
434 * Compile config information
435 *
436 * Lists developer config variables
437 *
Derek Allard76af4092010-01-16 19:20:49 +0000438 * @return string
439 */
Greg Aker5ac55942010-11-10 14:59:47 -0600440 protected function _compile_config()
Derek Allard76af4092010-01-16 19:20:49 +0000441 {
442 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600443 $output .= '<fieldset id="ci_profiler_config" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
Derek Allard76af4092010-01-16 19:20:49 +0000444 $output .= "\n";
Greg Aker62df1312011-04-18 11:18:02 -0500445 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_config').'&nbsp;&nbsp;(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_config_table\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI->lang->line('profiler_section_show').'\'?\''.$this->CI->lang->line('profiler_section_hide').'\':\''.$this->CI->lang->line('profiler_section_show').'\';">'.$this->CI->lang->line('profiler_section_show').'</span>)</legend>';
Derek Allard76af4092010-01-16 19:20:49 +0000446 $output .= "\n";
447
Greg Aker62df1312011-04-18 11:18:02 -0500448 $output .= "\n\n<table style='width:100%; display:none' id='ci_profiler_config_table'>\n";
Derek Allard76af4092010-01-16 19:20:49 +0000449
Pascal Kriete14287f32011-02-14 13:39:34 -0500450 foreach ($this->CI->config->config as $config=>$val)
Derek Allard76af4092010-01-16 19:20:49 +0000451 {
Greg Akere6026832010-04-29 13:41:39 -0500452 if (is_array($val))
453 {
454 $val = print_r($val, TRUE);
455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Greg Aker3a563982010-11-18 18:43:03 -0600457 $output .= "<tr><td style='padding:5px; vertical-align: top;color:#900;background-color:#ddd;'>".$config."&nbsp;&nbsp;</td><td style='padding:5px; color:#000;background-color:#ddd;'>".htmlspecialchars($val)."</td></tr>\n";
Derek Allard76af4092010-01-16 19:20:49 +0000458 }
459
460 $output .= "</table>\n";
461 $output .= "</fieldset>";
462
Derek Allard76af4092010-01-16 19:20:49 +0000463 return $output;
464 }
465
466 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard76af4092010-01-16 19:20:49 +0000468 /**
Greg Aker62df1312011-04-18 11:18:02 -0500469 * Compile session userdata
470 *
471 * @return string
472 */
473 private function _compile_session_data()
474 {
475 if ( ! isset($this->CI->session))
476 {
477 return;
478 }
479
480 $output = '<fieldset id="ci_profiler_csession" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
481 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_session_data').'&nbsp;&nbsp;(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_session_data\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI->lang->line('profiler_section_show').'\'?\''.$this->CI->lang->line('profiler_section_hide').'\':\''.$this->CI->lang->line('profiler_section_show').'\';">'.$this->CI->lang->line('profiler_section_show').'</span>)</legend>';
482 $output .= "<table style='width:100%;display:none' id='ci_profiler_session_data'>";
483
484 foreach ($this->CI->session->all_userdata() as $key => $val)
485 {
486 if (is_array($val))
487 {
488 $val = print_r($val, TRUE);
489 }
490
491 $output .= "<tr><td style='padding:5px; vertical-align: top;color:#900;background-color:#ddd;'>".$key."&nbsp;&nbsp;</td><td style='padding:5px; color:#000;background-color:#ddd;'>".htmlspecialchars($val)."</td></tr>\n";
492 }
493
494 $output .= '</table>';
495 $output .= "</fieldset>";
496 return $output;
497 }
498
499 // --------------------------------------------------------------------
500
501 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 * Run the Profiler
503 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200505 */
Greg Aker5ac55942010-11-10 14:59:47 -0600506 public function run()
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
508 $output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
Derek Jonesee71c802010-03-10 10:05:05 -0600509 $fields_displayed = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Jonesee71c802010-03-10 10:05:05 -0600511 foreach ($this->_available_sections as $section)
512 {
513 if ($this->_compile_{$section} !== FALSE)
514 {
515 $func = "_compile_{$section}";
516 $output .= $this->{$func}();
517 $fields_displayed++;
518 }
519 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000520
Derek Jonesee71c802010-03-10 10:05:05 -0600521 if ($fields_displayed == 0)
522 {
523 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee">'.$this->CI->lang->line('profiler_no_profiles').'</p>';
524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 $output .= '</div>';
527
528 return $output;
529 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000530}
531
532// END CI_Profiler class
533
534/* End of file Profiler.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000535/* Location: ./system/libraries/Profiler.php */