blob: 8a2568d8e98aa9d29d30301886d1c18fee584f54 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Profiler Class
32 *
33 * This class enables you to display benchmark, query, and other data
34 * in order to help with debugging and optimization.
35 *
36 * Note: At some point it would be good to move all the HTML in this class
37 * into a set of template files in order to allow customization.
38 *
39 * @package CodeIgniter
40 * @subpackage Libraries
41 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050042 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000043 * @link http://codeigniter.com/user_guide/general/profiling.html
44 */
45class CI_Profiler {
46
Greg Aker5ac55942010-11-10 14:59:47 -060047 protected $_available_sections = array(
Derek Jonesee71c802010-03-10 10:05:05 -060048 'benchmarks',
Derek Jonesee71c802010-03-10 10:05:05 -060049 'get',
Derek Jonesee71c802010-03-10 10:05:05 -060050 'memory_usage',
51 'post',
Greg Akere6026832010-04-29 13:41:39 -050052 'uri_string',
53 'controller_info',
Derek Jonesee71c802010-03-10 10:05:05 -060054 'queries',
Greg Akere6026832010-04-29 13:41:39 -050055 'http_headers',
Greg Aker62df1312011-04-18 11:18:02 -050056 'session_data',
Greg Akere6026832010-04-29 13:41:39 -050057 'config'
Derek Jonesee71c802010-03-10 10:05:05 -060058 );
Razicanc24f49b2011-04-25 13:43:57 +020059
Greg Akere6e6e642011-04-18 15:54:13 -050060 protected $_query_toggle_count = 25;
Razicanc24f49b2011-04-25 13:43:57 +020061
62 protected $CI;
Derek Jonesee71c802010-03-10 10:05:05 -060063
Greg Akere6e6e642011-04-18 15:54:13 -050064 // --------------------------------------------------------------------
Razicanc24f49b2011-04-25 13:43:57 +020065
Greg Aker5ac55942010-11-10 14:59:47 -060066 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +020067 {
68 $this->CI =& get_instance();
69 $this->CI->load->language('profiler');
70
Greg Akere6e6e642011-04-18 15:54:13 -050071 if (isset($config['query_toggle_count']))
72 {
73 $this->_query_toggle_count = (int) $config['query_toggle_count'];
74 unset($config['query_toggle_count']);
75 }
76
Derek Jonesee71c802010-03-10 10:05:05 -060077 // default all sections to display
78 foreach ($this->_available_sections as $section)
79 {
80 if ( ! isset($config[$section]))
81 {
82 $this->_compile_{$section} = TRUE;
83 }
84 }
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Jonesee71c802010-03-10 10:05:05 -060086 $this->set_sections($config);
Barry Mienydd671972010-10-04 16:33:58 +020087 }
88
Derek Allard2067d1a2008-11-13 22:59:24 +000089 // --------------------------------------------------------------------
90
91 /**
Derek Jonesee71c802010-03-10 10:05:05 -060092 * Set Sections
93 *
94 * Sets the private _compile_* properties to enable/disable Profiler sections
95 *
Derek Jonesee71c802010-03-10 10:05:05 -060096 * @param mixed
97 * @return void
98 */
Greg Aker5ac55942010-11-10 14:59:47 -060099 public function set_sections($config)
Derek Jonesee71c802010-03-10 10:05:05 -0600100 {
101 foreach ($config as $method => $enable)
102 {
103 if (in_array($method, $this->_available_sections))
104 {
Barry Mienydd671972010-10-04 16:33:58 +0200105 $this->_compile_{$method} = ($enable !== FALSE) ? TRUE : FALSE;
Derek Jonesee71c802010-03-10 10:05:05 -0600106 }
107 }
108 }
109
110 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200111
Derek Jonesee71c802010-03-10 10:05:05 -0600112 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 * Auto Profiler
114 *
115 * This function cycles through the entire array of mark points and
116 * matches any two points that are named identically (ending in "_start"
Derek Jones4b9c6292011-07-01 17:40:48 -0500117 * and "_end" respectively). It then compiles the execution times for
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 * all points and returns it as an array
Greg Aker5ac55942010-11-10 14:59:47 -0600119 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 * @return array
121 */
Greg Aker5ac55942010-11-10 14:59:47 -0600122 protected function _compile_benchmarks()
Barry Mienydd671972010-10-04 16:33:58 +0200123 {
124 $profile = array();
125 foreach ($this->CI->benchmark->marker as $key => $val)
126 {
127 // We match the "end" marker so that the list ends
128 // up in the order that it was defined
129 if (preg_match("/(.+?)_end/i", $key, $match))
130 {
131 if (isset($this->CI->benchmark->marker[$match[1].'_end']) AND isset($this->CI->benchmark->marker[$match[1].'_start']))
132 {
133 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
134 }
135 }
136 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000137
138 // Build a table containing the profile data.
139 // Note: At some point we should turn this into a template that can
Derek Jones4b9c6292011-07-01 17:40:48 -0500140 // be modified. We also might want to make this data available to be logged
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Jones4b9c6292011-07-01 17:40:48 -0500142 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600143 $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 +0000144 $output .= "\n";
Derek Allard76af4092010-01-16 19:20:49 +0000145 $output .= '<legend style="color:#900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks').'&nbsp;&nbsp;</legend>';
Barry Mienydd671972010-10-04 16:33:58 +0200146 $output .= "\n";
Greg Aker3a563982010-11-18 18:43:03 -0600147 $output .= "\n\n<table style='width:100%'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 foreach ($profile as $key => $val)
150 {
151 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
Greg Aker3a563982010-11-18 18:43:03 -0600152 $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 +0000153 }
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 $output .= "</table>\n";
156 $output .= "</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200157
158 return $output;
159 }
160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 // --------------------------------------------------------------------
162
163 /**
164 * Compile Queries
165 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200167 */
Greg Aker5ac55942010-11-10 14:59:47 -0600168 protected function _compile_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 {
170 $dbs = array();
Derek Jonesf0a9b332009-07-29 14:19:18 +0000171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 // Let's determine which databases are currently connected to
173 foreach (get_object_vars($this->CI) as $CI_object)
174 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000175 if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB') )
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
177 $dbs[] = $CI_object;
178 }
179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 if (count($dbs) == 0)
182 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500183 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600184 $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 +0000185 $output .= "\n";
186 $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>';
Barry Mienydd671972010-10-04 16:33:58 +0200187 $output .= "\n";
Greg Akere6e6e642011-04-18 15:54:13 -0500188 $output .= "\n\n<table style='border:none; width:100%;'>\n";
Greg Aker3a563982010-11-18 18:43:03 -0600189 $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 +0000190 $output .= "</table>\n";
191 $output .= "</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 return $output;
194 }
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 // Load the text helper so we can highlight the SQL
197 $this->CI->load->helper('text');
198
199 // Key words we want bolded
Greg Aker3424bf72010-09-14 17:53:10 -0500200 $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 +0000201
Derek Jones4b9c6292011-07-01 17:40:48 -0500202 $output = "\n\n";
Razicanc24f49b2011-04-25 13:43:57 +0200203
Greg Akere6e6e642011-04-18 15:54:13 -0500204 $count = 0;
Razicanc24f49b2011-04-25 13:43:57 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 foreach ($dbs as $db)
207 {
Greg Akere6e6e642011-04-18 15:54:13 -0500208 $count++;
Razicanc24f49b2011-04-25 13:43:57 +0200209
Greg Akere6e6e642011-04-18 15:54:13 -0500210 $hide_queries = (count($db->queries) > $this->_query_toggle_count) ? ' display:none' : '';
Razicanc24f49b2011-04-25 13:43:57 +0200211
Greg Akere6e6e642011-04-18 15:54:13 -0500212 $show_hide_js = '(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_queries_db_'.$count.'\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI->lang->line('profiler_section_hide').'\'?\''.$this->CI->lang->line('profiler_section_show').'\':\''.$this->CI->lang->line('profiler_section_hide').'\';">'.$this->CI->lang->line('profiler_section_hide').'</span>)';
Razicanc24f49b2011-04-25 13:43:57 +0200213
Greg Akere6e6e642011-04-18 15:54:13 -0500214 if ($hide_queries != '')
215 {
216 $show_hide_js = '(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_queries_db_'.$count.'\').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>)';
217 }
Razicanc24f49b2011-04-25 13:43:57 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
220 $output .= "\n";
Greg Akere6e6e642011-04-18 15:54:13 -0500221 $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;'.$show_hide_js.'</legend>';
Barry Mienydd671972010-10-04 16:33:58 +0200222 $output .= "\n";
Greg Akere6e6e642011-04-18 15:54:13 -0500223 $output .= "\n\n<table style='width:100%;{$hide_queries}' id='ci_profiler_queries_db_{$count}'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 if (count($db->queries) == 0)
226 {
Greg Aker3a563982010-11-18 18:43:03 -0600227 $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 +0000228 }
229 else
Barry Mienydd671972010-10-04 16:33:58 +0200230 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 foreach ($db->queries as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200232 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 $time = number_format($db->query_times[$key], 4);
234
235 $val = highlight_code($val, ENT_QUOTES);
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 foreach ($highlight as $bold)
238 {
Barry Mienydd671972010-10-04 16:33:58 +0200239 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Greg Aker3a563982010-11-18 18:43:03 -0600242 $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 +0000243 }
244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 $output .= "</table>\n";
247 $output .= "</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 return $output;
252 }
253
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 // --------------------------------------------------------------------
256
257 /**
258 * Compile $_GET Data
259 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200261 */
Greg Aker5ac55942010-11-10 14:59:47 -0600262 protected function _compile_get()
Barry Mienydd671972010-10-04 16:33:58 +0200263 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500264 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600265 $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 +0000266 $output .= "\n";
267 $output .= '<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data').'&nbsp;&nbsp;</legend>';
268 $output .= "\n";
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 if (count($_GET) == 0)
271 {
272 $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_get')."</div>";
273 }
274 else
275 {
Greg Aker3a563982010-11-18 18:43:03 -0600276 $output .= "\n\n<table style='width:100%; border:none'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 foreach ($_GET as $key => $val)
279 {
280 if ( ! is_numeric($key))
281 {
282 $key = "'".$key."'";
283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Greg Aker3a563982010-11-18 18:43:03 -0600285 $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 +0000286 if (is_array($val))
287 {
288 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
289 }
290 else
291 {
292 $output .= htmlspecialchars(stripslashes($val));
293 }
294 $output .= "</td></tr>\n";
295 }
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 $output .= "</table>\n";
298 }
299 $output .= "</fieldset>";
300
Barry Mienydd671972010-10-04 16:33:58 +0200301 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 /**
307 * Compile $_POST Data
308 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200310 */
Greg Aker5ac55942010-11-10 14:59:47 -0600311 protected function _compile_post()
Barry Mienydd671972010-10-04 16:33:58 +0200312 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500313 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600314 $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 +0000315 $output .= "\n";
316 $output .= '<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data').'&nbsp;&nbsp;</legend>';
317 $output .= "\n";
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 if (count($_POST) == 0)
320 {
321 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";
322 }
323 else
324 {
Greg Aker3a563982010-11-18 18:43:03 -0600325 $output .= "\n\n<table style='width:100%'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 foreach ($_POST as $key => $val)
328 {
329 if ( ! is_numeric($key))
330 {
331 $key = "'".$key."'";
332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Greg Aker3a563982010-11-18 18:43:03 -0600334 $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 +0000335 if (is_array($val))
336 {
Derek Jones511e3d72010-05-13 09:03:30 -0500337 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, TRUE))) . "</pre>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
339 else
340 {
341 $output .= htmlspecialchars(stripslashes($val));
342 }
343 $output .= "</td></tr>\n";
344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 $output .= "</table>\n";
347 }
348 $output .= "</fieldset>";
349
Barry Mienydd671972010-10-04 16:33:58 +0200350 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 /**
356 * Show query string
357 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200359 */
Greg Aker5ac55942010-11-10 14:59:47 -0600360 protected function _compile_uri_string()
Barry Mienydd671972010-10-04 16:33:58 +0200361 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500362 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600363 $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 +0000364 $output .= "\n";
365 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string').'&nbsp;&nbsp;</legend>';
366 $output .= "\n";
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 if ($this->CI->uri->uri_string == '')
369 {
370 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_uri')."</div>";
371 }
372 else
373 {
Barry Mienydd671972010-10-04 16:33:58 +0200374 $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 +0000375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 $output .= "</fieldset>";
378
Barry Mienydd671972010-10-04 16:33:58 +0200379 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 }
381
382 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 /**
385 * Show the controller and function that were called
386 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200388 */
Greg Aker5ac55942010-11-10 14:59:47 -0600389 protected function _compile_controller_info()
Barry Mienydd671972010-10-04 16:33:58 +0200390 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500391 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600392 $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 +0000393 $output .= "\n";
394 $output .= '<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info').'&nbsp;&nbsp;</legend>';
395 $output .= "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000396
Barry Mienydd671972010-10-04 16:33:58 +0200397 $output .= "<div style='color:#995300;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->router->fetch_class()."/".$this->CI->router->fetch_method()."</div>";
398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 $output .= "</fieldset>";
400
Barry Mienydd671972010-10-04 16:33:58 +0200401 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
Derek Allard76af4092010-01-16 19:20:49 +0000403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 /**
407 * Compile memory usage
408 *
409 * Display total used memory
410 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @return string
412 */
Greg Aker5ac55942010-11-10 14:59:47 -0600413 protected function _compile_memory_usage()
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500415 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600416 $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 +0000417 $output .= "\n";
418 $output .= '<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage').'&nbsp;&nbsp;</legend>';
419 $output .= "\n";
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
422 {
423 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';
424 }
425 else
426 {
Razicanc24f49b2011-04-25 13:43:57 +0200427 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_memory')."</div>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 $output .= "</fieldset>";
431
432 return $output;
433 }
434
435 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 /**
Derek Allard76af4092010-01-16 19:20:49 +0000438 * Compile header information
439 *
440 * Lists HTTP headers
441 *
Derek Allard76af4092010-01-16 19:20:49 +0000442 * @return string
443 */
Greg Aker5ac55942010-11-10 14:59:47 -0600444 protected function _compile_http_headers()
Derek Allard76af4092010-01-16 19:20:49 +0000445 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500446 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600447 $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 +0000448 $output .= "\n";
Greg Aker62df1312011-04-18 11:18:02 -0500449 $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 +0000450 $output .= "\n";
451
Greg Aker62df1312011-04-18 11:18:02 -0500452 $output .= "\n\n<table style='width:100%;display:none' id='ci_profiler_httpheaders_table'>\n";
Derek Allard76af4092010-01-16 19:20:49 +0000453
Pascal Kriete14287f32011-02-14 13:39:34 -0500454 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 +0000455 {
456 $val = (isset($_SERVER[$header])) ? $_SERVER[$header] : '';
Greg Aker3a563982010-11-18 18:43:03 -0600457 $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 +0000458 }
459
460 $output .= "</table>\n";
461 $output .= "</fieldset>";
462
Derek Allard76af4092010-01-16 19:20:49 +0000463 return $output;
464 }
465
466 // --------------------------------------------------------------------
467
468 /**
469 * Compile config information
470 *
471 * Lists developer config variables
472 *
Derek Allard76af4092010-01-16 19:20:49 +0000473 * @return string
474 */
Greg Aker5ac55942010-11-10 14:59:47 -0600475 protected function _compile_config()
Derek Allard76af4092010-01-16 19:20:49 +0000476 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500477 $output = "\n\n";
Greg Aker3a563982010-11-18 18:43:03 -0600478 $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 +0000479 $output .= "\n";
Greg Aker62df1312011-04-18 11:18:02 -0500480 $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 +0000481 $output .= "\n";
482
Greg Aker62df1312011-04-18 11:18:02 -0500483 $output .= "\n\n<table style='width:100%; display:none' id='ci_profiler_config_table'>\n";
Derek Allard76af4092010-01-16 19:20:49 +0000484
Pascal Kriete14287f32011-02-14 13:39:34 -0500485 foreach ($this->CI->config->config as $config=>$val)
Derek Allard76af4092010-01-16 19:20:49 +0000486 {
Greg Akere6026832010-04-29 13:41:39 -0500487 if (is_array($val))
488 {
489 $val = print_r($val, TRUE);
490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Greg Aker3a563982010-11-18 18:43:03 -0600492 $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 +0000493 }
494
495 $output .= "</table>\n";
496 $output .= "</fieldset>";
497
Derek Allard76af4092010-01-16 19:20:49 +0000498 return $output;
499 }
500
501 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard76af4092010-01-16 19:20:49 +0000503 /**
Greg Aker62df1312011-04-18 11:18:02 -0500504 * Compile session userdata
505 *
506 * @return string
507 */
yterajima8310c9a2011-08-22 07:26:54 +0900508 protected function _compile_session_data()
Greg Aker62df1312011-04-18 11:18:02 -0500509 {
510 if ( ! isset($this->CI->session))
511 {
512 return;
513 }
514
515 $output = '<fieldset id="ci_profiler_csession" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
516 $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>';
517 $output .= "<table style='width:100%;display:none' id='ci_profiler_session_data'>";
518
519 foreach ($this->CI->session->all_userdata() as $key => $val)
520 {
Pedro Junior 7fe625a2011-09-05 09:47:09 -0300521 if (is_array($val) || is_object($val))
Greg Aker62df1312011-04-18 11:18:02 -0500522 {
523 $val = print_r($val, TRUE);
524 }
525
526 $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";
527 }
528
529 $output .= '</table>';
530 $output .= "</fieldset>";
531 return $output;
532 }
533
534 // --------------------------------------------------------------------
535
536 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 * Run the Profiler
538 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200540 */
Greg Aker5ac55942010-11-10 14:59:47 -0600541 public function run()
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 {
543 $output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
Derek Jonesee71c802010-03-10 10:05:05 -0600544 $fields_displayed = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200545
Derek Jonesee71c802010-03-10 10:05:05 -0600546 foreach ($this->_available_sections as $section)
547 {
548 if ($this->_compile_{$section} !== FALSE)
549 {
550 $func = "_compile_{$section}";
551 $output .= $this->{$func}();
552 $fields_displayed++;
553 }
554 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000555
Derek Jonesee71c802010-03-10 10:05:05 -0600556 if ($fields_displayed == 0)
557 {
558 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee">'.$this->CI->lang->line('profiler_no_profiles').'</p>';
559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 $output .= '</div>';
562
563 return $output;
564 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000565}
566
567// END CI_Profiler class
568
569/* End of file Profiler.php */
yterajima8310c9a2011-08-22 07:26:54 +0900570/* Location: ./system/libraries/Profiler.php */