blob: 1e961f6dfec1f43fa1fc005f825599ee8d04dd2f [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 *
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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Profiler Class
30 *
31 * This class enables you to display benchmark, query, and other data
32 * in order to help with debugging and optimization.
33 *
34 * Note: At some point it would be good to move all the HTML in this class
35 * into a set of template files in order to allow customization.
36 *
37 * @package CodeIgniter
38 * @subpackage Libraries
39 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/general/profiling.html
42 */
43class CI_Profiler {
44
Timothy Warren0688ac92012-04-20 10:25:04 -040045 /**
46 * List of profiler sections available to show
47 *
48 * @var array
49 */
Greg Aker5ac55942010-11-10 14:59:47 -060050 protected $_available_sections = array(
Timothy Warren0688ac92012-04-20 10:25:04 -040051 'benchmarks',
52 'get',
53 'memory_usage',
54 'post',
55 'uri_string',
56 'controller_info',
57 'queries',
58 'http_headers',
59 'session_data',
60 'config'
61 );
Razicanc24f49b2011-04-25 13:43:57 +020062
Timothy Warren0688ac92012-04-20 10:25:04 -040063 /**
64 * Number of queries to show before making the additional queries togglable
65 *
66 * @var int
67 */
Greg Akere6e6e642011-04-18 15:54:13 -050068 protected $_query_toggle_count = 25;
Razicanc24f49b2011-04-25 13:43:57 +020069
Timothy Warren0688ac92012-04-20 10:25:04 -040070 /**
71 * Reference to the CodeIgniter singleton
72 *
73 * @var object
74 */
Razicanc24f49b2011-04-25 13:43:57 +020075 protected $CI;
Derek Jonesee71c802010-03-10 10:05:05 -060076
Timothy Warren0688ac92012-04-20 10:25:04 -040077 /**
78 * Constructor
79 *
80 * Initialize Profiler
81 *
82 * @param array $config
83 */
Greg Aker5ac55942010-11-10 14:59:47 -060084 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +020085 {
86 $this->CI =& get_instance();
87 $this->CI->load->language('profiler');
88
Greg Akere6e6e642011-04-18 15:54:13 -050089 if (isset($config['query_toggle_count']))
90 {
91 $this->_query_toggle_count = (int) $config['query_toggle_count'];
92 unset($config['query_toggle_count']);
93 }
94
Derek Jonesee71c802010-03-10 10:05:05 -060095 // default all sections to display
96 foreach ($this->_available_sections as $section)
97 {
98 if ( ! isset($config[$section]))
99 {
100 $this->_compile_{$section} = TRUE;
101 }
102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Jonesee71c802010-03-10 10:05:05 -0600104 $this->set_sections($config);
Barry Mienydd671972010-10-04 16:33:58 +0200105 }
106
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 // --------------------------------------------------------------------
108
109 /**
Derek Jonesee71c802010-03-10 10:05:05 -0600110 * Set Sections
111 *
112 * Sets the private _compile_* properties to enable/disable Profiler sections
113 *
Derek Jonesee71c802010-03-10 10:05:05 -0600114 * @param mixed
115 * @return void
116 */
Greg Aker5ac55942010-11-10 14:59:47 -0600117 public function set_sections($config)
Derek Jonesee71c802010-03-10 10:05:05 -0600118 {
Andrey Andreev0140ddd2012-06-16 01:12:56 +0300119 if (isset($config['query_toggle_count']))
120 {
121 $this->_query_toggle_count = (int) $config['query_toggle_count'];
122 unset($config['query_toggle_count']);
123 }
124
Derek Jonesee71c802010-03-10 10:05:05 -0600125 foreach ($config as $method => $enable)
126 {
127 if (in_array($method, $this->_available_sections))
128 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300129 $this->_compile_{$method} = ($enable !== FALSE);
Derek Jonesee71c802010-03-10 10:05:05 -0600130 }
131 }
132 }
133
134 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Jonesee71c802010-03-10 10:05:05 -0600136 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 * Auto Profiler
138 *
139 * This function cycles through the entire array of mark points and
140 * matches any two points that are named identically (ending in "_start"
Derek Jones4b9c6292011-07-01 17:40:48 -0500141 * and "_end" respectively). It then compiles the execution times for
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 * all points and returns it as an array
Greg Aker5ac55942010-11-10 14:59:47 -0600143 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * @return array
145 */
Greg Aker5ac55942010-11-10 14:59:47 -0600146 protected function _compile_benchmarks()
Barry Mienydd671972010-10-04 16:33:58 +0200147 {
148 $profile = array();
149 foreach ($this->CI->benchmark->marker as $key => $val)
150 {
151 // We match the "end" marker so that the list ends
152 // up in the order that it was defined
Andrey Andreev195d3112011-12-25 03:59:13 +0200153 if (preg_match('/(.+?)_end/i', $key, $match)
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300154 && isset($this->CI->benchmark->marker[$match[1].'_end'], $this->CI->benchmark->marker[$match[1].'_start']))
Barry Mienydd671972010-10-04 16:33:58 +0200155 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200156 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
Barry Mienydd671972010-10-04 16:33:58 +0200157 }
158 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000159
160 // Build a table containing the profile data.
161 // Note: At some point we should turn this into a template that can
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300162 // be modified. We also might want to make this data available to be logged
Barry Mienydd671972010-10-04 16:33:58 +0200163
Andrey Andreev195d3112011-12-25 03:59:13 +0200164 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300165 .'<fieldset id="ci_profiler_benchmarks" style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
166 ."\n"
167 .'<legend style="color:#900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks')."&nbsp;&nbsp;</legend>"
168 ."\n\n\n<table style=\"width:100%;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 foreach ($profile as $key => $val)
171 {
172 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300173 $output .= '<tr><td style="padding:5px;width:50%;color:#000;font-weight:bold;background-color:#ddd;">'
174 .$key.'&nbsp;&nbsp;</td><td style="padding:5px;width:50%;color:#900;font-weight:normal;background-color:#ddd;">'
175 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
Barry Mienydd671972010-10-04 16:33:58 +0200177
Andrey Andreev195d3112011-12-25 03:59:13 +0200178 return $output."</table>\n</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200179 }
180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 // --------------------------------------------------------------------
182
183 /**
184 * Compile Queries
185 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200187 */
Greg Aker5ac55942010-11-10 14:59:47 -0600188 protected function _compile_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
190 $dbs = array();
Derek Jonesf0a9b332009-07-29 14:19:18 +0000191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 // Let's determine which databases are currently connected to
193 foreach (get_object_vars($this->CI) as $CI_object)
194 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300195 if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
197 $dbs[] = $CI_object;
198 }
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Andrey Andreev195d3112011-12-25 03:59:13 +0200201 if (count($dbs) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200203 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300204 .'<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
205 ."\n"
206 .'<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>'
207 ."\n\n\n<table style=\"border:none; width:100%;\">\n"
208 .'<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
209 .$this->CI->lang->line('profiler_no_db')
210 ."</td></tr>\n</table>\n</fieldset>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 // Load the text helper so we can highlight the SQL
214 $this->CI->load->helper('text');
215
216 // Key words we want bolded
Greg Aker3424bf72010-09-14 17:53:10 -0500217 $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 +0000218
Derek Jones4b9c6292011-07-01 17:40:48 -0500219 $output = "\n\n";
Greg Akere6e6e642011-04-18 15:54:13 -0500220 $count = 0;
Razicanc24f49b2011-04-25 13:43:57 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 foreach ($dbs as $db)
223 {
Greg Akere6e6e642011-04-18 15:54:13 -0500224 $hide_queries = (count($db->queries) > $this->_query_toggle_count) ? ' display:none' : '';
Razicanc24f49b2011-04-25 13:43:57 +0200225
Greg Akere6e6e642011-04-18 15:54:13 -0500226 $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 +0200227
Alex Bilbied261b1e2012-06-02 11:12:16 +0100228 if ($hide_queries !== '')
Greg Akere6e6e642011-04-18 15:54:13 -0500229 {
230 $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>)';
231 }
Razicanc24f49b2011-04-25 13:43:57 +0200232
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300233 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
234 ."\n"
235 .'<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_database')
236 .':&nbsp; '.$db->database.'&nbsp;&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries')
237 .': '.count($db->queries).'&nbsp;&nbsp;'.$show_hide_js."</legend>\n\n\n"
238 .'<table style="width:100%;'.$hide_queries.'" id="ci_profiler_queries_db_'.$count."\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200239
Andrey Andreev195d3112011-12-25 03:59:13 +0200240 if (count($db->queries) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300242 $output .= '<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
243 .$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 }
245 else
Barry Mienydd671972010-10-04 16:33:58 +0200246 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 foreach ($db->queries as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200248 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 $time = number_format($db->query_times[$key], 4);
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 $val = highlight_code($val, ENT_QUOTES);
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 foreach ($highlight as $bold)
253 {
Barry Mienydd671972010-10-04 16:33:58 +0200254 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300257 $output .= '<tr><td style="padding:5px;vertical-align:top;width:1%;color:#900;font-weight:normal;background-color:#ddd;">'
258 .$time.'&nbsp;&nbsp;</td><td style="padding:5px;color:#000;font-weight:normal;background-color:#ddd;">'
259 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Andrey Andreev195d3112011-12-25 03:59:13 +0200263 $output .= "</table>\n</fieldset>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 return $output;
267 }
268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // --------------------------------------------------------------------
270
271 /**
272 * Compile $_GET Data
273 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200275 */
Greg Aker5ac55942010-11-10 14:59:47 -0600276 protected function _compile_get()
Barry Mienydd671972010-10-04 16:33:58 +0200277 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300278 $output = "\n\n"
279 .'<fieldset id="ci_profiler_get" style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
280 ."\n"
281 .'<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data')."&nbsp;&nbsp;</legend>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200282
Andrey Andreev195d3112011-12-25 03:59:13 +0200283 if (count($_GET) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300285 $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 +0000286 }
287 else
288 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300289 $output .= "\n\n<table style=\"width:100%;border:none;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 foreach ($_GET as $key => $val)
292 {
293 if ( ! is_numeric($key))
294 {
295 $key = "'".$key."'";
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300298 $output .= '<tr><td style="width:50%;color:#000;background-color:#ddd;padding:5px;">&#36;_GET['
299 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#cd6e00;font-weight:normal;background-color:#ddd;">'
300 .((is_array($val) OR is_object($val)) ? '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>' : htmlspecialchars(stripslashes($val)))
301 ."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 $output .= "</table>\n";
305 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000306
Andrey Andreev195d3112011-12-25 03:59:13 +0200307 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 /**
313 * Compile $_POST Data
314 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200316 */
Greg Aker5ac55942010-11-10 14:59:47 -0600317 protected function _compile_post()
Barry Mienydd671972010-10-04 16:33:58 +0200318 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200319 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300320 .'<fieldset id="ci_profiler_post" style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
321 ."\n"
322 .'<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data')."&nbsp;&nbsp;</legend>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200323
Alex Bilbied261b1e2012-06-02 11:12:16 +0100324 if (count($_POST) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300326 $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 +0000327 }
328 else
329 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300330 $output .= "\n\n<table style=\"width:100%;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 foreach ($_POST as $key => $val)
333 {
334 if ( ! is_numeric($key))
335 {
336 $key = "'".$key."'";
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300339 $output .= '<tr><td style="width:50%;padding:5px;color:#000;background-color:#ddd;">&#36;_POST['
340 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;">';
341
Andrey Andreev963386b2012-03-02 01:52:01 +0200342 if (is_array($val) OR is_object($val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300344 $output .= '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
346 else
347 {
348 $output .= htmlspecialchars(stripslashes($val));
349 }
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 $output .= "</td></tr>\n";
352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 $output .= "</table>\n";
355 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000356
Andrey Andreev195d3112011-12-25 03:59:13 +0200357 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 /**
363 * Show query string
364 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200366 */
Greg Aker5ac55942010-11-10 14:59:47 -0600367 protected function _compile_uri_string()
Barry Mienydd671972010-10-04 16:33:58 +0200368 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200369 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300370 .'<fieldset id="ci_profiler_uri_string" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
371 ."\n"
372 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string')."&nbsp;&nbsp;</legend>\n"
373 .'<div style="color:#000;font-weight:normal;padding:4px 0 4px 0;">'
Alex Bilbied261b1e2012-06-02 11:12:16 +0100374 .($this->CI->uri->uri_string === '' ? $this->CI->lang->line('profiler_no_uri') : $this->CI->uri->uri_string)
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300375 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 }
377
378 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 /**
381 * Show the controller and function that were called
382 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200384 */
Greg Aker5ac55942010-11-10 14:59:47 -0600385 protected function _compile_controller_info()
Barry Mienydd671972010-10-04 16:33:58 +0200386 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200387 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300388 .'<fieldset id="ci_profiler_controller_info" style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
389 ."\n"
390 .'<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info')."&nbsp;&nbsp;</legend>\n"
391 .'<div style="color:#995300;font-weight:normal;padding:4px 0 4px 0;">'.$this->CI->router->fetch_class().'/'.$this->CI->router->fetch_method()
392 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
Derek Allard76af4092010-01-16 19:20:49 +0000394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 /**
398 * Compile memory usage
399 *
400 * Display total used memory
401 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 * @return string
403 */
Greg Aker5ac55942010-11-10 14:59:47 -0600404 protected function _compile_memory_usage()
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200406 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300407 .'<fieldset id="ci_profiler_memory_usage" style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
408 ."\n"
409 .'<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage')."&nbsp;&nbsp;</legend>\n"
410 .'<div style="color:#5a0099;font-weight:normal;padding:4px 0 4px 0;">'
Andrey Andreevc839d282012-06-07 14:35:27 +0300411 .(($usage = memory_get_usage()) != '' ? number_format($usage).' bytes' : $this->CI->lang->line('profiler_no_memory'))
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300412 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
414
415 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 /**
Derek Allard76af4092010-01-16 19:20:49 +0000418 * Compile header information
419 *
420 * Lists HTTP headers
421 *
Derek Allard76af4092010-01-16 19:20:49 +0000422 * @return string
423 */
Greg Aker5ac55942010-11-10 14:59:47 -0600424 protected function _compile_http_headers()
Derek Allard76af4092010-01-16 19:20:49 +0000425 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200426 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300427 .'<fieldset id="ci_profiler_http_headers" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
428 ."\n"
429 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_headers')
430 .'&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"
431 .'<table style="width:100%;display:none;" id="ci_profiler_httpheaders_table">'."\n";
Derek Allard76af4092010-01-16 19:20:49 +0000432
Pascal Kriete14287f32011-02-14 13:39:34 -0500433 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 +0000434 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300435 $val = isset($_SERVER[$header]) ? $_SERVER[$header] : '';
436 $output .= '<tr><td style="vertical-align:top;width:50%;padding:5px;color:#900;background-color:#ddd;">'
437 .$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 +0000438 }
439
Andrey Andreev195d3112011-12-25 03:59:13 +0200440 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000441 }
442
443 // --------------------------------------------------------------------
444
445 /**
446 * Compile config information
447 *
448 * Lists developer config variables
449 *
Derek Allard76af4092010-01-16 19:20:49 +0000450 * @return string
451 */
Greg Aker5ac55942010-11-10 14:59:47 -0600452 protected function _compile_config()
Derek Allard76af4092010-01-16 19:20:49 +0000453 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200454 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300455 .'<fieldset id="ci_profiler_config" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
456 ."\n"
457 .'<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"
458 .'<table style="width:100%;display:none;" id="ci_profiler_config_table">'."\n";
Derek Allard76af4092010-01-16 19:20:49 +0000459
Andrey Andreev963386b2012-03-02 01:52:01 +0200460 foreach ($this->CI->config->config as $config => $val)
Derek Allard76af4092010-01-16 19:20:49 +0000461 {
Andrey Andreev963386b2012-03-02 01:52:01 +0200462 if (is_array($val) OR is_object($val))
Greg Akere6026832010-04-29 13:41:39 -0500463 {
464 $val = print_r($val, TRUE);
465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300467 $output .= '<tr><td style="padding:5px;vertical-align:top;color:#900;background-color:#ddd;">'
468 .$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 +0000469 }
470
Andrey Andreev195d3112011-12-25 03:59:13 +0200471 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000472 }
473
474 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard76af4092010-01-16 19:20:49 +0000476 /**
Greg Aker62df1312011-04-18 11:18:02 -0500477 * Compile session userdata
478 *
479 * @return string
480 */
yterajima8310c9a2011-08-22 07:26:54 +0900481 protected function _compile_session_data()
Greg Aker62df1312011-04-18 11:18:02 -0500482 {
483 if ( ! isset($this->CI->session))
484 {
485 return;
486 }
487
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300488 $output = '<fieldset id="ci_profiler_csession" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
489 .'<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>'
490 .'<table style="width:100%;display:none;" id="ci_profiler_session_data">';
Greg Aker62df1312011-04-18 11:18:02 -0500491
492 foreach ($this->CI->session->all_userdata() as $key => $val)
493 {
Andrey Andreev963386b2012-03-02 01:52:01 +0200494 if (is_array($val) OR is_object($val))
Greg Aker62df1312011-04-18 11:18:02 -0500495 {
496 $val = print_r($val, TRUE);
497 }
498
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300499 $output .= '<tr><td style="padding:5px;vertical-align:top;color:#900;background-color:#ddd;">'
500 .$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 -0500501 }
502
Andrey Andreev195d3112011-12-25 03:59:13 +0200503 return $output."</table>\n</fieldset>";
Greg Aker62df1312011-04-18 11:18:02 -0500504 }
505
506 // --------------------------------------------------------------------
507
508 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 * Run the Profiler
510 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200512 */
Greg Aker5ac55942010-11-10 14:59:47 -0600513 public function run()
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300515 $output = '<div id="codeigniter_profiler" style="clear:both;background-color:#fff;padding:10px;">';
Derek Jonesee71c802010-03-10 10:05:05 -0600516 $fields_displayed = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Jonesee71c802010-03-10 10:05:05 -0600518 foreach ($this->_available_sections as $section)
519 {
520 if ($this->_compile_{$section} !== FALSE)
521 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300522 $func = '_compile_'.$section;
Derek Jonesee71c802010-03-10 10:05:05 -0600523 $output .= $this->{$func}();
524 $fields_displayed++;
525 }
526 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000527
Andrey Andreev195d3112011-12-25 03:59:13 +0200528 if ($fields_displayed === 0)
Derek Jonesee71c802010-03-10 10:05:05 -0600529 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300530 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee;">'
531 .$this->CI->lang->line('profiler_no_profiles').'</p>';
Derek Jonesee71c802010-03-10 10:05:05 -0600532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Andrey Andreev195d3112011-12-25 03:59:13 +0200534 return $output.'</div>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
Andrey Andreev56454792012-05-17 14:32:19 +0300536
Derek Allard2067d1a2008-11-13 22:59:24 +0000537}
538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539/* End of file Profiler.php */
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300540/* Location: ./system/libraries/Profiler.php */