Take two:
[www-register-wizard.git] / libraries / Profiler.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * CodeIgniter\r
4  *\r
5  * An open source application development framework for PHP 4.3.2 or newer\r
6  *\r
7  * @package             CodeIgniter\r
8  * @author              ExpressionEngine Dev Team\r
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.\r
10  * @license             http://codeigniter.com/user_guide/license.html\r
11  * @link                http://codeigniter.com\r
12  * @since               Version 1.0\r
13  * @filesource\r
14  */\r
15 \r
16 // ------------------------------------------------------------------------\r
17 \r
18 /**\r
19  * CodeIgniter Profiler Class\r
20  *\r
21  * This class enables you to display benchmark, query, and other data\r
22  * in order to help with debugging and optimization.\r
23  *\r
24  * Note: At some point it would be good to move all the HTML in this class\r
25  * into a set of template files in order to allow customization.\r
26  *\r
27  * @package             CodeIgniter\r
28  * @subpackage  Libraries\r
29  * @category    Libraries\r
30  * @author              ExpressionEngine Dev Team\r
31  * @link                http://codeigniter.com/user_guide/general/profiling.html\r
32  */\r
33 class CI_Profiler {\r
34 \r
35         var $CI;\r
36         \r
37         function CI_Profiler()\r
38         {\r
39                 $this->CI =& get_instance();\r
40                 $this->CI->load->language('profiler');\r
41         }\r
42         \r
43         // --------------------------------------------------------------------\r
44 \r
45         /**\r
46          * Auto Profiler\r
47          *\r
48          * This function cycles through the entire array of mark points and\r
49          * matches any two points that are named identically (ending in "_start"\r
50          * and "_end" respectively).  It then compiles the execution times for\r
51          * all points and returns it as an array\r
52          *\r
53          * @access      private\r
54          * @return      array\r
55          */\r
56         function _compile_benchmarks()\r
57         {\r
58                 $profile = array();\r
59                 foreach ($this->CI->benchmark->marker as $key => $val)\r
60                 {\r
61                         // We match the "end" marker so that the list ends\r
62                         // up in the order that it was defined\r
63                         if (preg_match("/(.+?)_end/i", $key, $match))\r
64                         {                       \r
65                                 if (isset($this->CI->benchmark->marker[$match[1].'_end']) AND isset($this->CI->benchmark->marker[$match[1].'_start']))\r
66                                 {\r
67                                         $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);\r
68                                 }\r
69                         }\r
70                 }\r
71 \r
72                 // Build a table containing the profile data.\r
73                 // Note: At some point we should turn this into a template that can\r
74                 // be modified.  We also might want to make this data available to be logged\r
75         \r
76                 $output  = "\n\n";\r
77                 $output .= '<fieldset style="border:1px solid #990000;padding:6px 10px 10px 10px;margin:0 0 20px 0;background-color:#eee">';\r
78                 $output .= "\n";\r
79                 $output .= '<legend style="color:#990000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks').'&nbsp;&nbsp;</legend>';\r
80                 $output .= "\n";                        \r
81                 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";\r
82                 \r
83                 foreach ($profile as $key => $val)\r
84                 {\r
85                         $key = ucwords(str_replace(array('_', '-'), ' ', $key));\r
86                         $output .= "<tr><td width='50%' style='color:#000;font-weight:bold;background-color:#ddd;'>".$key."&nbsp;&nbsp;</td><td width='50%' style='color:#990000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";\r
87                 }\r
88                 \r
89                 $output .= "</table>\n";\r
90                 $output .= "</fieldset>";\r
91                 \r
92                 return $output;\r
93         }\r
94         \r
95         // --------------------------------------------------------------------\r
96 \r
97         /**\r
98          * Compile Queries\r
99          *\r
100          * @access      private\r
101          * @return      string\r
102          */     \r
103         function _compile_queries()\r
104         {\r
105                 $dbs = array();\r
106                 \r
107                 // Let's determine which databases are currently connected to\r
108                 foreach (get_object_vars($this->CI) as $CI_object)\r
109                 {\r
110                         if ( is_subclass_of(get_class($CI_object), 'CI_DB') )\r
111                         {\r
112                                 $dbs[] = $CI_object;\r
113                         }\r
114                 }\r
115                                         \r
116                 if (count($dbs) == 0)\r
117                 {\r
118                         $output  = "\n\n";\r
119                         $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';\r
120                         $output .= "\n";\r
121                         $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>';\r
122                         $output .= "\n";                \r
123                         $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";\r
124                         $output .="<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_db')."</td></tr>\n";\r
125                         $output .= "</table>\n";\r
126                         $output .= "</fieldset>";\r
127                         \r
128                         return $output;\r
129                 }\r
130                 \r
131                 // Load the text helper so we can highlight the SQL\r
132                 $this->CI->load->helper('text');\r
133 \r
134                 // Key words we want bolded\r
135                 $highlight = array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'AND', 'LEFT&nbsp;JOIN', 'ORDER&nbsp;BY', 'GROUP&nbsp;BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR', 'HAVING', 'OFFSET', 'NOT&nbsp;IN', 'IN', 'LIKE', 'NOT&nbsp;LIKE', 'COUNT', 'MAX', 'MIN', 'ON', 'AS', 'AVG', 'SUM', '(', ')');\r
136 \r
137                 $output  = "\n\n";\r
138                         \r
139                 foreach ($dbs as $db)\r
140                 {\r
141                         $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';\r
142                         $output .= "\n";\r
143                         $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_database').':&nbsp; '.$db->database.'&nbsp;&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').': '.count($this->CI->db->queries).'&nbsp;&nbsp;&nbsp;</legend>';\r
144                         $output .= "\n";                \r
145                         $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";\r
146                 \r
147                         if (count($db->queries) == 0)\r
148                         {\r
149                                 $output .= "<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";\r
150                         }\r
151                         else\r
152                         {                               \r
153                                 foreach ($db->queries as $key => $val)\r
154                                 {                                       \r
155                                         $time = number_format($db->query_times[$key], 4);\r
156 \r
157                                         $val = highlight_code($val, ENT_QUOTES);\r
158         \r
159                                         foreach ($highlight as $bold)\r
160                                         {\r
161                                                 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);  \r
162                                         }\r
163                                         \r
164                                         $output .= "<tr><td width='1%' valign='top' style='color:#990000;font-weight:normal;background-color:#ddd;'>".$time."&nbsp;&nbsp;</td><td style='color:#000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";\r
165                                 }\r
166                         }\r
167                         \r
168                         $output .= "</table>\n";\r
169                         $output .= "</fieldset>";\r
170                         \r
171                 }\r
172                 \r
173                 return $output;\r
174         }\r
175 \r
176         \r
177         // --------------------------------------------------------------------\r
178 \r
179         /**\r
180          * Compile $_GET Data\r
181          *\r
182          * @access      private\r
183          * @return      string\r
184          */     \r
185         function _compile_get()\r
186         {       \r
187                 $output  = "\n\n";\r
188                 $output .= '<fieldset style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';\r
189                 $output .= "\n";\r
190                 $output .= '<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data').'&nbsp;&nbsp;</legend>';\r
191                 $output .= "\n";\r
192                                 \r
193                 if (count($_GET) == 0)\r
194                 {\r
195                         $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_get')."</div>";\r
196                 }\r
197                 else\r
198                 {\r
199                         $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";\r
200                 \r
201                         foreach ($_GET as $key => $val)\r
202                         {\r
203                                 if ( ! is_numeric($key))\r
204                                 {\r
205                                         $key = "'".$key."'";\r
206                                 }\r
207                         \r
208                                 $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>&#36;_GET[".$key."]&nbsp;&nbsp; </td><td width='50%' style='color:#cd6e00;font-weight:normal;background-color:#ddd;'>";\r
209                                 if (is_array($val))\r
210                                 {\r
211                                         $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";\r
212                                 }\r
213                                 else\r
214                                 {\r
215                                         $output .= htmlspecialchars(stripslashes($val));\r
216                                 }\r
217                                 $output .= "</td></tr>\n";\r
218                         }\r
219                         \r
220                         $output .= "</table>\n";\r
221                 }\r
222                 $output .= "</fieldset>";\r
223 \r
224                 return $output; \r
225         }\r
226         \r
227         // --------------------------------------------------------------------\r
228         \r
229         /**\r
230          * Compile $_POST Data\r
231          *\r
232          * @access      private\r
233          * @return      string\r
234          */     \r
235         function _compile_post()\r
236         {       \r
237                 $output  = "\n\n";\r
238                 $output .= '<fieldset style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';\r
239                 $output .= "\n";\r
240                 $output .= '<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data').'&nbsp;&nbsp;</legend>';\r
241                 $output .= "\n";\r
242                                 \r
243                 if (count($_POST) == 0)\r
244                 {\r
245                         $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";\r
246                 }\r
247                 else\r
248                 {\r
249                         $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";\r
250                 \r
251                         foreach ($_POST as $key => $val)\r
252                         {\r
253                                 if ( ! is_numeric($key))\r
254                                 {\r
255                                         $key = "'".$key."'";\r
256                                 }\r
257                         \r
258                                 $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>&#36;_POST[".$key."]&nbsp;&nbsp; </td><td width='50%' style='color:#009900;font-weight:normal;background-color:#ddd;'>";\r
259                                 if (is_array($val))\r
260                                 {\r
261                                         $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";\r
262                                 }\r
263                                 else\r
264                                 {\r
265                                         $output .= htmlspecialchars(stripslashes($val));\r
266                                 }\r
267                                 $output .= "</td></tr>\n";\r
268                         }\r
269                         \r
270                         $output .= "</table>\n";\r
271                 }\r
272                 $output .= "</fieldset>";\r
273 \r
274                 return $output; \r
275         }\r
276         \r
277         // --------------------------------------------------------------------\r
278         \r
279         /**\r
280          * Show query string\r
281          *\r
282          * @access      private\r
283          * @return      string\r
284          */     \r
285         function _compile_uri_string()\r
286         {       \r
287                 $output  = "\n\n";\r
288                 $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';\r
289                 $output .= "\n";\r
290                 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string').'&nbsp;&nbsp;</legend>';\r
291                 $output .= "\n";\r
292                 \r
293                 if ($this->CI->uri->uri_string == '')\r
294                 {\r
295                         $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_uri')."</div>";\r
296                 }\r
297                 else\r
298                 {\r
299                         $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->uri->uri_string."</div>";                               \r
300                 }\r
301                 \r
302                 $output .= "</fieldset>";\r
303 \r
304                 return $output; \r
305         }\r
306 \r
307         // --------------------------------------------------------------------\r
308         \r
309         /**\r
310          * Show the controller and function that were called\r
311          *\r
312          * @access      private\r
313          * @return      string\r
314          */     \r
315         function _compile_controller_info()\r
316         {       \r
317                 $output  = "\n\n";\r
318                 $output .= '<fieldset style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';\r
319                 $output .= "\n";\r
320                 $output .= '<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info').'&nbsp;&nbsp;</legend>';\r
321                 $output .= "\n";\r
322                 \r
323                 $output .= "<div style='color:#995300;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->router->fetch_class()."/".$this->CI->router->fetch_method()."</div>";                                \r
324 \r
325                 \r
326                 $output .= "</fieldset>";\r
327 \r
328                 return $output; \r
329         }\r
330         // --------------------------------------------------------------------\r
331         \r
332         /**\r
333          * Compile memory usage\r
334          *\r
335          * Display total used memory\r
336          *\r
337          * @access      public\r
338          * @return      string\r
339          */\r
340         function _compile_memory_usage()\r
341         {\r
342                 $output  = "\n\n";\r
343                 $output .= '<fieldset style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';\r
344                 $output .= "\n";\r
345                 $output .= '<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage').'&nbsp;&nbsp;</legend>';\r
346                 $output .= "\n";\r
347                 \r
348                 if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')\r
349                 {\r
350                         $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';\r
351                 }\r
352                 else\r
353                 {\r
354                         $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_memory_usage')."</div>";                             \r
355                 }\r
356                 \r
357                 $output .= "</fieldset>";\r
358 \r
359                 return $output;\r
360         }\r
361 \r
362         // --------------------------------------------------------------------\r
363         \r
364         /**\r
365          * Run the Profiler\r
366          *\r
367          * @access      private\r
368          * @return      string\r
369          */     \r
370         function run()\r
371         {\r
372                 $output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";\r
373 \r
374                 $output .= $this->_compile_uri_string();\r
375                 $output .= $this->_compile_controller_info();\r
376                 $output .= $this->_compile_memory_usage();\r
377                 $output .= $this->_compile_benchmarks();\r
378                 $output .= $this->_compile_get();\r
379                 $output .= $this->_compile_post();\r
380                 $output .= $this->_compile_queries();\r
381 \r
382                 $output .= '</div>';\r
383 \r
384                 return $output;\r
385         }\r
386 \r
387 }\r
388 \r
389 // END CI_Profiler class\r
390 \r
391 /* End of file Profiler.php */\r
392 /* Location: ./system/libraries/Profiler.php */