blob: 7d7069b95b9ec3bd28afae86523d0a63114e0996 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Profiler Class
31 *
32 * This class enables you to display benchmark, query, and other data
33 * in order to help with debugging and optimization.
34 *
35 * Note: At some point it would be good to move all the HTML in this class
36 * into a set of template files in order to allow customization.
37 *
38 * @package CodeIgniter
39 * @subpackage Libraries
40 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050041 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000042 * @link http://codeigniter.com/user_guide/general/profiling.html
43 */
44class CI_Profiler {
45
Timothy Warren0688ac92012-04-20 10:25:04 -040046 /**
47 * List of profiler sections available to show
48 *
49 * @var array
50 */
Greg Aker5ac55942010-11-10 14:59:47 -060051 protected $_available_sections = array(
Timothy Warren0688ac92012-04-20 10:25:04 -040052 'benchmarks',
53 'get',
54 'memory_usage',
55 'post',
56 'uri_string',
57 'controller_info',
58 'queries',
59 'http_headers',
60 'session_data',
61 'config'
62 );
Razicanc24f49b2011-04-25 13:43:57 +020063
Timothy Warren0688ac92012-04-20 10:25:04 -040064 /**
65 * Number of queries to show before making the additional queries togglable
66 *
67 * @var int
68 */
Greg Akere6e6e642011-04-18 15:54:13 -050069 protected $_query_toggle_count = 25;
Razicanc24f49b2011-04-25 13:43:57 +020070
Timothy Warren0688ac92012-04-20 10:25:04 -040071 /**
72 * Reference to the CodeIgniter singleton
73 *
74 * @var object
75 */
Razicanc24f49b2011-04-25 13:43:57 +020076 protected $CI;
Derek Jonesee71c802010-03-10 10:05:05 -060077
Timothy Warren0688ac92012-04-20 10:25:04 -040078 /**
79 * Constructor
80 *
81 * Initialize Profiler
82 *
83 * @param array $config
84 */
Greg Aker5ac55942010-11-10 14:59:47 -060085 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +020086 {
87 $this->CI =& get_instance();
88 $this->CI->load->language('profiler');
89
Greg Akere6e6e642011-04-18 15:54:13 -050090 if (isset($config['query_toggle_count']))
91 {
92 $this->_query_toggle_count = (int) $config['query_toggle_count'];
93 unset($config['query_toggle_count']);
94 }
95
Derek Jonesee71c802010-03-10 10:05:05 -060096 // default all sections to display
97 foreach ($this->_available_sections as $section)
98 {
99 if ( ! isset($config[$section]))
100 {
101 $this->_compile_{$section} = TRUE;
102 }
103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Jonesee71c802010-03-10 10:05:05 -0600105 $this->set_sections($config);
Barry Mienydd671972010-10-04 16:33:58 +0200106 }
107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 // --------------------------------------------------------------------
109
110 /**
Derek Jonesee71c802010-03-10 10:05:05 -0600111 * Set Sections
112 *
113 * Sets the private _compile_* properties to enable/disable Profiler sections
114 *
Derek Jonesee71c802010-03-10 10:05:05 -0600115 * @param mixed
116 * @return void
117 */
Greg Aker5ac55942010-11-10 14:59:47 -0600118 public function set_sections($config)
Derek Jonesee71c802010-03-10 10:05:05 -0600119 {
Andrey Andreev0140ddd2012-06-16 01:12:56 +0300120 if (isset($config['query_toggle_count']))
121 {
122 $this->_query_toggle_count = (int) $config['query_toggle_count'];
123 unset($config['query_toggle_count']);
124 }
125
Derek Jonesee71c802010-03-10 10:05:05 -0600126 foreach ($config as $method => $enable)
127 {
128 if (in_array($method, $this->_available_sections))
129 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300130 $this->_compile_{$method} = ($enable !== FALSE);
Derek Jonesee71c802010-03-10 10:05:05 -0600131 }
132 }
133 }
134
135 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Jonesee71c802010-03-10 10:05:05 -0600137 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 * Auto Profiler
139 *
140 * This function cycles through the entire array of mark points and
141 * matches any two points that are named identically (ending in "_start"
Derek Jones4b9c6292011-07-01 17:40:48 -0500142 * and "_end" respectively). It then compiles the execution times for
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 * all points and returns it as an array
Greg Aker5ac55942010-11-10 14:59:47 -0600144 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 * @return array
146 */
Greg Aker5ac55942010-11-10 14:59:47 -0600147 protected function _compile_benchmarks()
Barry Mienydd671972010-10-04 16:33:58 +0200148 {
149 $profile = array();
150 foreach ($this->CI->benchmark->marker as $key => $val)
151 {
152 // We match the "end" marker so that the list ends
153 // up in the order that it was defined
Andrey Andreev195d3112011-12-25 03:59:13 +0200154 if (preg_match('/(.+?)_end/i', $key, $match)
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300155 && isset($this->CI->benchmark->marker[$match[1].'_end'], $this->CI->benchmark->marker[$match[1].'_start']))
Barry Mienydd671972010-10-04 16:33:58 +0200156 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200157 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
Barry Mienydd671972010-10-04 16:33:58 +0200158 }
159 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000160
161 // Build a table containing the profile data.
162 // Note: At some point we should turn this into a template that can
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300163 // be modified. We also might want to make this data available to be logged
Barry Mienydd671972010-10-04 16:33:58 +0200164
Andrey Andreev195d3112011-12-25 03:59:13 +0200165 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300166 .'<fieldset id="ci_profiler_benchmarks" style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
167 ."\n"
168 .'<legend style="color:#900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks')."&nbsp;&nbsp;</legend>"
169 ."\n\n\n<table style=\"width:100%;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 foreach ($profile as $key => $val)
172 {
173 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300174 $output .= '<tr><td style="padding:5px;width:50%;color:#000;font-weight:bold;background-color:#ddd;">'
175 .$key.'&nbsp;&nbsp;</td><td style="padding:5px;width:50%;color:#900;font-weight:normal;background-color:#ddd;">'
176 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 }
Barry Mienydd671972010-10-04 16:33:58 +0200178
Andrey Andreev195d3112011-12-25 03:59:13 +0200179 return $output."</table>\n</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200180 }
181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 // --------------------------------------------------------------------
183
184 /**
185 * Compile Queries
186 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200188 */
Greg Aker5ac55942010-11-10 14:59:47 -0600189 protected function _compile_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
191 $dbs = array();
Derek Jonesf0a9b332009-07-29 14:19:18 +0000192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 // Let's determine which databases are currently connected to
194 foreach (get_object_vars($this->CI) as $CI_object)
195 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300196 if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
198 $dbs[] = $CI_object;
199 }
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Andrey Andreev195d3112011-12-25 03:59:13 +0200202 if (count($dbs) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200204 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300205 .'<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
206 ."\n"
207 .'<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>'
208 ."\n\n\n<table style=\"border:none; width:100%;\">\n"
209 .'<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
210 .$this->CI->lang->line('profiler_no_db')
211 ."</td></tr>\n</table>\n</fieldset>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 // Load the text helper so we can highlight the SQL
215 $this->CI->load->helper('text');
216
217 // Key words we want bolded
Greg Aker3424bf72010-09-14 17:53:10 -0500218 $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 +0000219
Derek Jones4b9c6292011-07-01 17:40:48 -0500220 $output = "\n\n";
Greg Akere6e6e642011-04-18 15:54:13 -0500221 $count = 0;
Razicanc24f49b2011-04-25 13:43:57 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 foreach ($dbs as $db)
224 {
Greg Akere6e6e642011-04-18 15:54:13 -0500225 $hide_queries = (count($db->queries) > $this->_query_toggle_count) ? ' display:none' : '';
Razicanc24f49b2011-04-25 13:43:57 +0200226
Greg Akere6e6e642011-04-18 15:54:13 -0500227 $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 +0200228
Alex Bilbied261b1e2012-06-02 11:12:16 +0100229 if ($hide_queries !== '')
Greg Akere6e6e642011-04-18 15:54:13 -0500230 {
231 $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>)';
232 }
Razicanc24f49b2011-04-25 13:43:57 +0200233
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300234 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
235 ."\n"
236 .'<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_database')
237 .':&nbsp; '.$db->database.'&nbsp;&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries')
238 .': '.count($db->queries).'&nbsp;&nbsp;'.$show_hide_js."</legend>\n\n\n"
239 .'<table style="width:100%;'.$hide_queries.'" id="ci_profiler_queries_db_'.$count."\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200240
Andrey Andreev195d3112011-12-25 03:59:13 +0200241 if (count($db->queries) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300243 $output .= '<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
244 .$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
246 else
Barry Mienydd671972010-10-04 16:33:58 +0200247 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 foreach ($db->queries as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200249 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 $time = number_format($db->query_times[$key], 4);
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 $val = highlight_code($val, ENT_QUOTES);
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 foreach ($highlight as $bold)
254 {
Barry Mienydd671972010-10-04 16:33:58 +0200255 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300258 $output .= '<tr><td style="padding:5px;vertical-align:top;width:1%;color:#900;font-weight:normal;background-color:#ddd;">'
259 .$time.'&nbsp;&nbsp;</td><td style="padding:5px;color:#000;font-weight:normal;background-color:#ddd;">'
260 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 }
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Andrey Andreev195d3112011-12-25 03:59:13 +0200264 $output .= "</table>\n</fieldset>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 return $output;
268 }
269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // --------------------------------------------------------------------
271
272 /**
273 * Compile $_GET Data
274 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200276 */
Greg Aker5ac55942010-11-10 14:59:47 -0600277 protected function _compile_get()
Barry Mienydd671972010-10-04 16:33:58 +0200278 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300279 $output = "\n\n"
280 .'<fieldset id="ci_profiler_get" style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
281 ."\n"
282 .'<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data')."&nbsp;&nbsp;</legend>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200283
Andrey Andreev195d3112011-12-25 03:59:13 +0200284 if (count($_GET) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300286 $output .= '<div style="color:#cd6e00;font-weight:normal;padding:4px 0 4px 0;">'.$this->CI->lang->line('profiler_no_get').'</div>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 }
288 else
289 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300290 $output .= "\n\n<table style=\"width:100%;border:none;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 foreach ($_GET as $key => $val)
293 {
294 if ( ! is_numeric($key))
295 {
296 $key = "'".$key."'";
297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300299 $output .= '<tr><td style="width:50%;color:#000;background-color:#ddd;padding:5px;">&#36;_GET['
300 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#cd6e00;font-weight:normal;background-color:#ddd;">'
301 .((is_array($val) OR is_object($val)) ? '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>' : htmlspecialchars(stripslashes($val)))
302 ."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 $output .= "</table>\n";
306 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000307
Andrey Andreev195d3112011-12-25 03:59:13 +0200308 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 /**
314 * Compile $_POST Data
315 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200317 */
Greg Aker5ac55942010-11-10 14:59:47 -0600318 protected function _compile_post()
Barry Mienydd671972010-10-04 16:33:58 +0200319 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200320 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300321 .'<fieldset id="ci_profiler_post" style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
322 ."\n"
323 .'<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data')."&nbsp;&nbsp;</legend>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200324
Alex Bilbied261b1e2012-06-02 11:12:16 +0100325 if (count($_POST) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300327 $output .= '<div style="color:#009900;font-weight:normal;padding:4px 0 4px 0;">'.$this->CI->lang->line('profiler_no_post').'</div>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
329 else
330 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300331 $output .= "\n\n<table style=\"width:100%;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 foreach ($_POST as $key => $val)
334 {
335 if ( ! is_numeric($key))
336 {
337 $key = "'".$key."'";
338 }
Barry Mienydd671972010-10-04 16:33:58 +0200339
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300340 $output .= '<tr><td style="width:50%;padding:5px;color:#000;background-color:#ddd;">&#36;_POST['
341 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;">';
342
Andrey Andreev963386b2012-03-02 01:52:01 +0200343 if (is_array($val) OR is_object($val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300345 $output .= '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
347 else
348 {
349 $output .= htmlspecialchars(stripslashes($val));
350 }
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 $output .= "</td></tr>\n";
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 $output .= "</table>\n";
356 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000357
Andrey Andreev195d3112011-12-25 03:59:13 +0200358 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 /**
364 * Show query string
365 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200367 */
Greg Aker5ac55942010-11-10 14:59:47 -0600368 protected function _compile_uri_string()
Barry Mienydd671972010-10-04 16:33:58 +0200369 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200370 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300371 .'<fieldset id="ci_profiler_uri_string" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
372 ."\n"
373 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string')."&nbsp;&nbsp;</legend>\n"
374 .'<div style="color:#000;font-weight:normal;padding:4px 0 4px 0;">'
Alex Bilbied261b1e2012-06-02 11:12:16 +0100375 .($this->CI->uri->uri_string === '' ? $this->CI->lang->line('profiler_no_uri') : $this->CI->uri->uri_string)
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300376 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
378
379 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 /**
382 * Show the controller and function that were called
383 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200385 */
Greg Aker5ac55942010-11-10 14:59:47 -0600386 protected function _compile_controller_info()
Barry Mienydd671972010-10-04 16:33:58 +0200387 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200388 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300389 .'<fieldset id="ci_profiler_controller_info" style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
390 ."\n"
391 .'<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info')."&nbsp;&nbsp;</legend>\n"
392 .'<div style="color:#995300;font-weight:normal;padding:4px 0 4px 0;">'.$this->CI->router->fetch_class().'/'.$this->CI->router->fetch_method()
393 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 }
Derek Allard76af4092010-01-16 19:20:49 +0000395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 /**
399 * Compile memory usage
400 *
401 * Display total used memory
402 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * @return string
404 */
Greg Aker5ac55942010-11-10 14:59:47 -0600405 protected function _compile_memory_usage()
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200407 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300408 .'<fieldset id="ci_profiler_memory_usage" style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
409 ."\n"
410 .'<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage')."&nbsp;&nbsp;</legend>\n"
411 .'<div style="color:#5a0099;font-weight:normal;padding:4px 0 4px 0;">'
Andrey Andreevc839d282012-06-07 14:35:27 +0300412 .(($usage = memory_get_usage()) != '' ? number_format($usage).' bytes' : $this->CI->lang->line('profiler_no_memory'))
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300413 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
415
416 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 /**
Derek Allard76af4092010-01-16 19:20:49 +0000419 * Compile header information
420 *
421 * Lists HTTP headers
422 *
Derek Allard76af4092010-01-16 19:20:49 +0000423 * @return string
424 */
Greg Aker5ac55942010-11-10 14:59:47 -0600425 protected function _compile_http_headers()
Derek Allard76af4092010-01-16 19:20:49 +0000426 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200427 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300428 .'<fieldset id="ci_profiler_http_headers" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
429 ."\n"
430 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_headers')
431 .'&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>\n\n\n"
432 .'<table style="width:100%;display:none;" id="ci_profiler_httpheaders_table">'."\n";
Derek Allard76af4092010-01-16 19:20:49 +0000433
Pascal Kriete14287f32011-02-14 13:39:34 -0500434 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 +0000435 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300436 $val = isset($_SERVER[$header]) ? $_SERVER[$header] : '';
437 $output .= '<tr><td style="vertical-align:top;width:50%;padding:5px;color:#900;background-color:#ddd;">'
438 .$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 +0000439 }
440
Andrey Andreev195d3112011-12-25 03:59:13 +0200441 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000442 }
443
444 // --------------------------------------------------------------------
445
446 /**
447 * Compile config information
448 *
449 * Lists developer config variables
450 *
Derek Allard76af4092010-01-16 19:20:49 +0000451 * @return string
452 */
Greg Aker5ac55942010-11-10 14:59:47 -0600453 protected function _compile_config()
Derek Allard76af4092010-01-16 19:20:49 +0000454 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200455 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300456 .'<fieldset id="ci_profiler_config" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
457 ."\n"
458 .'<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>\n\n\n"
459 .'<table style="width:100%;display:none;" id="ci_profiler_config_table">'."\n";
Derek Allard76af4092010-01-16 19:20:49 +0000460
Andrey Andreev963386b2012-03-02 01:52:01 +0200461 foreach ($this->CI->config->config as $config => $val)
Derek Allard76af4092010-01-16 19:20:49 +0000462 {
Andrey Andreev963386b2012-03-02 01:52:01 +0200463 if (is_array($val) OR is_object($val))
Greg Akere6026832010-04-29 13:41:39 -0500464 {
465 $val = print_r($val, TRUE);
466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300468 $output .= '<tr><td style="padding:5px;vertical-align:top;color:#900;background-color:#ddd;">'
469 .$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 +0000470 }
471
Andrey Andreev195d3112011-12-25 03:59:13 +0200472 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000473 }
474
475 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard76af4092010-01-16 19:20:49 +0000477 /**
Greg Aker62df1312011-04-18 11:18:02 -0500478 * Compile session userdata
479 *
480 * @return string
481 */
yterajima8310c9a2011-08-22 07:26:54 +0900482 protected function _compile_session_data()
Greg Aker62df1312011-04-18 11:18:02 -0500483 {
484 if ( ! isset($this->CI->session))
485 {
486 return;
487 }
488
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300489 $output = '<fieldset id="ci_profiler_csession" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
490 .'<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>'
491 .'<table style="width:100%;display:none;" id="ci_profiler_session_data">';
Greg Aker62df1312011-04-18 11:18:02 -0500492
493 foreach ($this->CI->session->all_userdata() as $key => $val)
494 {
Andrey Andreev963386b2012-03-02 01:52:01 +0200495 if (is_array($val) OR is_object($val))
Greg Aker62df1312011-04-18 11:18:02 -0500496 {
497 $val = print_r($val, TRUE);
498 }
499
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300500 $output .= '<tr><td style="padding:5px;vertical-align:top;color:#900;background-color:#ddd;">'
501 .$key.'&nbsp;&nbsp;</td><td style="padding:5px;color:#000;background-color:#ddd;">'.htmlspecialchars($val)."</td></tr>\n";
Greg Aker62df1312011-04-18 11:18:02 -0500502 }
503
Andrey Andreev195d3112011-12-25 03:59:13 +0200504 return $output."</table>\n</fieldset>";
Greg Aker62df1312011-04-18 11:18:02 -0500505 }
506
507 // --------------------------------------------------------------------
508
509 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * Run the Profiler
511 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200513 */
Greg Aker5ac55942010-11-10 14:59:47 -0600514 public function run()
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300516 $output = '<div id="codeigniter_profiler" style="clear:both;background-color:#fff;padding:10px;">';
Derek Jonesee71c802010-03-10 10:05:05 -0600517 $fields_displayed = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Jonesee71c802010-03-10 10:05:05 -0600519 foreach ($this->_available_sections as $section)
520 {
521 if ($this->_compile_{$section} !== FALSE)
522 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300523 $func = '_compile_'.$section;
Derek Jonesee71c802010-03-10 10:05:05 -0600524 $output .= $this->{$func}();
525 $fields_displayed++;
526 }
527 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000528
Andrey Andreev195d3112011-12-25 03:59:13 +0200529 if ($fields_displayed === 0)
Derek Jonesee71c802010-03-10 10:05:05 -0600530 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300531 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee;">'
532 .$this->CI->lang->line('profiler_no_profiles').'</p>';
Derek Jonesee71c802010-03-10 10:05:05 -0600533 }
Barry Mienydd671972010-10-04 16:33:58 +0200534
Andrey Andreev195d3112011-12-25 03:59:13 +0200535 return $output.'</div>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 }
Andrey Andreev56454792012-05-17 14:32:19 +0300537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538}
539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540/* End of file Profiler.php */
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300541/* Location: ./system/libraries/Profiler.php */