blob: 9cbd69bb2a79583b5471a9502717dd6bd98b47e3 [file] [log] [blame]
Andrey Andreev195d3112011-12-25 03:59:13 +02001<?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
Andrey Andreev195d3112011-12-25 03:59:13 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev195d3112011-12-25 03:59:13 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Andrey Andreev195d3112011-12-25 03:59:13 +0200129 if (preg_match('/(.+?)_end/i', $key, $match)
130 AND isset($this->CI->benchmark->marker[$match[1].'_end'], $this->CI->benchmark->marker[$match[1].'_start']))
Barry Mienydd671972010-10-04 16:33:58 +0200131 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200132 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
Barry Mienydd671972010-10-04 16:33:58 +0200133 }
134 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000135
136 // Build a table containing the profile data.
137 // Note: At some point we should turn this into a template that can
Derek Jones4b9c6292011-07-01 17:40:48 -0500138 // be modified. We also might want to make this data available to be logged
Barry Mienydd671972010-10-04 16:33:58 +0200139
Andrey Andreev195d3112011-12-25 03:59:13 +0200140 $output = "\n\n"
141 . '<fieldset id="ci_profiler_benchmarks" style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'
142 . "\n"
143 . '<legend style="color:#900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks').'&nbsp;&nbsp;</legend>'
144 . "\n\n\n<table style='width:100%'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 foreach ($profile as $key => $val)
147 {
148 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
Greg Aker3a563982010-11-18 18:43:03 -0600149 $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 +0000150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Andrey Andreev195d3112011-12-25 03:59:13 +0200152 return $output."</table>\n</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200153 }
154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 // --------------------------------------------------------------------
156
157 /**
158 * Compile Queries
159 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200161 */
Greg Aker5ac55942010-11-10 14:59:47 -0600162 protected function _compile_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
164 $dbs = array();
Derek Jonesf0a9b332009-07-29 14:19:18 +0000165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 // Let's determine which databases are currently connected to
167 foreach (get_object_vars($this->CI) as $CI_object)
168 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000169 if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB') )
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 $dbs[] = $CI_object;
172 }
173 }
Barry Mienydd671972010-10-04 16:33:58 +0200174
Andrey Andreev195d3112011-12-25 03:59:13 +0200175 if (count($dbs) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200177 return "\n\n"
178 . '<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
179 . "\n"
180 . '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>'
181 . "\n\n\n<table style='border:none; width:100%;'>\n"
182 . '<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
183 . $this->CI->lang->line('profiler_no_db')
184 . "</td></tr>\n</table>\n</fieldset>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 // Load the text helper so we can highlight the SQL
188 $this->CI->load->helper('text');
189
190 // Key words we want bolded
Greg Aker3424bf72010-09-14 17:53:10 -0500191 $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 +0000192
Derek Jones4b9c6292011-07-01 17:40:48 -0500193 $output = "\n\n";
Razicanc24f49b2011-04-25 13:43:57 +0200194
Greg Akere6e6e642011-04-18 15:54:13 -0500195 $count = 0;
Razicanc24f49b2011-04-25 13:43:57 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 foreach ($dbs as $db)
198 {
Greg Akere6e6e642011-04-18 15:54:13 -0500199 $hide_queries = (count($db->queries) > $this->_query_toggle_count) ? ' display:none' : '';
Razicanc24f49b2011-04-25 13:43:57 +0200200
Greg Akere6e6e642011-04-18 15:54:13 -0500201 $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 +0200202
Greg Akere6e6e642011-04-18 15:54:13 -0500203 if ($hide_queries != '')
204 {
205 $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>)';
206 }
Razicanc24f49b2011-04-25 13:43:57 +0200207
Andrey Andreev195d3112011-12-25 03:59:13 +0200208 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'
209 . "\n"
210 . '<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>'
211 . "\n\n\n<table style='width:100%;{$hide_queries}' id='ci_profiler_queries_db_{$count}'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200212
Andrey Andreev195d3112011-12-25 03:59:13 +0200213 if (count($db->queries) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
Greg Aker3a563982010-11-18 18:43:03 -0600215 $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 +0000216 }
217 else
Barry Mienydd671972010-10-04 16:33:58 +0200218 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 foreach ($db->queries as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200220 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 $time = number_format($db->query_times[$key], 4);
222
223 $val = highlight_code($val, ENT_QUOTES);
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 foreach ($highlight as $bold)
226 {
Barry Mienydd671972010-10-04 16:33:58 +0200227 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
Greg Aker3a563982010-11-18 18:43:03 -0600230 $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 +0000231 }
232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Andrey Andreev195d3112011-12-25 03:59:13 +0200234 $output .= "</table>\n</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 }
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 return $output;
239 }
240
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 // --------------------------------------------------------------------
243
244 /**
245 * Compile $_GET Data
246 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200248 */
Greg Aker5ac55942010-11-10 14:59:47 -0600249 protected function _compile_get()
Barry Mienydd671972010-10-04 16:33:58 +0200250 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200251 $output = "\n\n"
252 . '<fieldset id="ci_profiler_get" style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'
253 . "\n"
254 . '<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data').'&nbsp;&nbsp;</legend>'
255 . "\n";
Barry Mienydd671972010-10-04 16:33:58 +0200256
Andrey Andreev195d3112011-12-25 03:59:13 +0200257 if (count($_GET) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
259 $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_get')."</div>";
260 }
261 else
262 {
Greg Aker3a563982010-11-18 18:43:03 -0600263 $output .= "\n\n<table style='width:100%; border:none'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 foreach ($_GET as $key => $val)
266 {
267 if ( ! is_numeric($key))
268 {
269 $key = "'".$key."'";
270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Andrey Andreev195d3112011-12-25 03:59:13 +0200272 $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;'>"
273 . (is_array($val) ? "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>" : htmlspecialchars(stripslashes($val)))
274 . "</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 }
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 $output .= "</table>\n";
278 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000279
Andrey Andreev195d3112011-12-25 03:59:13 +0200280 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 /**
286 * Compile $_POST Data
287 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200289 */
Greg Aker5ac55942010-11-10 14:59:47 -0600290 protected function _compile_post()
Barry Mienydd671972010-10-04 16:33:58 +0200291 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200292 $output = "\n\n"
293 . '<fieldset id="ci_profiler_post" style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'
294 . "\n"
295 . '<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data').'&nbsp;&nbsp;</legend>'
296 . "\n";
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 if (count($_POST) == 0)
299 {
300 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";
301 }
302 else
303 {
Greg Aker3a563982010-11-18 18:43:03 -0600304 $output .= "\n\n<table style='width:100%'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 foreach ($_POST as $key => $val)
307 {
308 if ( ! is_numeric($key))
309 {
310 $key = "'".$key."'";
311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Greg Aker3a563982010-11-18 18:43:03 -0600313 $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 +0000314 if (is_array($val))
315 {
Derek Jones511e3d72010-05-13 09:03:30 -0500316 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, TRUE))) . "</pre>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
318 else
319 {
320 $output .= htmlspecialchars(stripslashes($val));
321 }
322 $output .= "</td></tr>\n";
323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 $output .= "</table>\n";
326 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000327
Andrey Andreev195d3112011-12-25 03:59:13 +0200328 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 /**
334 * Show query string
335 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200337 */
Greg Aker5ac55942010-11-10 14:59:47 -0600338 protected function _compile_uri_string()
Barry Mienydd671972010-10-04 16:33:58 +0200339 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200340 return "\n\n"
341 . '<fieldset id="ci_profiler_uri_string" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'
342 . "\n"
343 . '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string').'&nbsp;&nbsp;</legend>'
344 . "\n<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>"
345 . ($this->CI->uri->uri_string == '' ? $this->CI->lang->line('profiler_no_uri') : $this->CI->uri->uri_string)
346 . '</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
348
349 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 /**
352 * Show the controller and function that were called
353 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200355 */
Greg Aker5ac55942010-11-10 14:59:47 -0600356 protected function _compile_controller_info()
Barry Mienydd671972010-10-04 16:33:58 +0200357 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200358 return "\n\n"
359 . '<fieldset id="ci_profiler_controller_info" style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'
360 . "\n"
361 . '<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info').'&nbsp;&nbsp;</legend>'
362 . "\n<div style='color:#995300;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->router->fetch_class().'/'.$this->CI->router->fetch_method()
363 . '</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
Derek Allard76af4092010-01-16 19:20:49 +0000365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 /**
369 * Compile memory usage
370 *
371 * Display total used memory
372 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 * @return string
374 */
Greg Aker5ac55942010-11-10 14:59:47 -0600375 protected function _compile_memory_usage()
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200377 return "\n\n"
378 . '<fieldset id="ci_profiler_memory_usage" style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'
379 . "\n"
380 . '<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage').'&nbsp;&nbsp;</legend>'
381 . "\n<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>"
382 . ((function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '') ? number_format($usage).' bytes' : $this->CI->lang->line('profiler_no_memory'))
383 . '</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 }
385
386 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 /**
Derek Allard76af4092010-01-16 19:20:49 +0000389 * Compile header information
390 *
391 * Lists HTTP headers
392 *
Derek Allard76af4092010-01-16 19:20:49 +0000393 * @return string
394 */
Greg Aker5ac55942010-11-10 14:59:47 -0600395 protected function _compile_http_headers()
Derek Allard76af4092010-01-16 19:20:49 +0000396 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200397 $output = "\n\n"
398 . '<fieldset id="ci_profiler_http_headers" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'
399 . "\n"
400 . '<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>'
401 . "\n\n\n<table style='width:100%;display:none' id='ci_profiler_httpheaders_table'>\n";
Derek Allard76af4092010-01-16 19:20:49 +0000402
Pascal Kriete14287f32011-02-14 13:39:34 -0500403 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 +0000404 {
405 $val = (isset($_SERVER[$header])) ? $_SERVER[$header] : '';
Greg Aker3a563982010-11-18 18:43:03 -0600406 $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 +0000407 }
408
Andrey Andreev195d3112011-12-25 03:59:13 +0200409 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000410 }
411
412 // --------------------------------------------------------------------
413
414 /**
415 * Compile config information
416 *
417 * Lists developer config variables
418 *
Derek Allard76af4092010-01-16 19:20:49 +0000419 * @return string
420 */
Greg Aker5ac55942010-11-10 14:59:47 -0600421 protected function _compile_config()
Derek Allard76af4092010-01-16 19:20:49 +0000422 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200423 $output = "\n\n"
424 . '<fieldset id="ci_profiler_config" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'
425 . "\n"
426 . '<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>'
427 . "\n\n\n<table style='width:100%; display:none' id='ci_profiler_config_table'>\n";
Derek Allard76af4092010-01-16 19:20:49 +0000428
Pascal Kriete14287f32011-02-14 13:39:34 -0500429 foreach ($this->CI->config->config as $config=>$val)
Derek Allard76af4092010-01-16 19:20:49 +0000430 {
Greg Akere6026832010-04-29 13:41:39 -0500431 if (is_array($val))
432 {
433 $val = print_r($val, TRUE);
434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Greg Aker3a563982010-11-18 18:43:03 -0600436 $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 +0000437 }
438
Andrey Andreev195d3112011-12-25 03:59:13 +0200439 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000440 }
441
442 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard76af4092010-01-16 19:20:49 +0000444 /**
Greg Aker62df1312011-04-18 11:18:02 -0500445 * Compile session userdata
446 *
447 * @return string
448 */
yterajima8310c9a2011-08-22 07:26:54 +0900449 protected function _compile_session_data()
Greg Aker62df1312011-04-18 11:18:02 -0500450 {
451 if ( ! isset($this->CI->session))
452 {
453 return;
454 }
455
Andrey Andreev195d3112011-12-25 03:59:13 +0200456 $output = '<fieldset id="ci_profiler_csession" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'
457 . '<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>'
458 . "<table style='width:100%;display:none' id='ci_profiler_session_data'>";
Greg Aker62df1312011-04-18 11:18:02 -0500459
460 foreach ($this->CI->session->all_userdata() as $key => $val)
461 {
Pedro Junior 7fe625a2011-09-05 09:47:09 -0300462 if (is_array($val) || is_object($val))
Greg Aker62df1312011-04-18 11:18:02 -0500463 {
464 $val = print_r($val, TRUE);
465 }
466
467 $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";
468 }
469
Andrey Andreev195d3112011-12-25 03:59:13 +0200470 return $output."</table>\n</fieldset>";
Greg Aker62df1312011-04-18 11:18:02 -0500471 }
472
473 // --------------------------------------------------------------------
474
475 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 * Run the Profiler
477 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200479 */
Greg Aker5ac55942010-11-10 14:59:47 -0600480 public function run()
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
482 $output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
Derek Jonesee71c802010-03-10 10:05:05 -0600483 $fields_displayed = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Jonesee71c802010-03-10 10:05:05 -0600485 foreach ($this->_available_sections as $section)
486 {
487 if ($this->_compile_{$section} !== FALSE)
488 {
489 $func = "_compile_{$section}";
490 $output .= $this->{$func}();
491 $fields_displayed++;
492 }
493 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000494
Andrey Andreev195d3112011-12-25 03:59:13 +0200495 if ($fields_displayed === 0)
Derek Jonesee71c802010-03-10 10:05:05 -0600496 {
497 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee">'.$this->CI->lang->line('profiler_no_profiles').'</p>';
498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Andrey Andreev195d3112011-12-25 03:59:13 +0200500 return $output.'</div>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000502}
503
504// END CI_Profiler class
505
506/* End of file Profiler.php */
yterajima8310c9a2011-08-22 07:26:54 +0900507/* Location: ./system/libraries/Profiler.php */