30
30
* @author ExpressionEngine Dev Team
31
31
* @link http://codeigniter.com/user_guide/general/profiling.html
32
32
*/
33
- class CI_Profiler {
33
+ class CI_Profiler extends CI_Loader {
34
34
35
- var $ CI ;
35
+ protected $ CI ;
36
36
37
37
protected $ _available_sections = array (
38
38
'benchmarks ' ,
@@ -46,9 +46,13 @@ class CI_Profiler {
46
46
'config ' ,
47
47
'files ' ,
48
48
'console ' ,
49
- 'userdata '
49
+ 'userdata ' ,
50
+ 'view_data '
50
51
);
51
- protected $ _sections = array (); // Stores _compile_x() results
52
+
53
+ protected $ _sections = array (); // Stores _compile_x() results
54
+
55
+ protected $ _query_toggle_count = 25 ;
52
56
53
57
// --------------------------------------------------------------------
54
58
@@ -57,6 +61,14 @@ public function __construct($config = array())
57
61
$ this ->CI =& get_instance ();
58
62
$ this ->CI ->load ->language ('profiler ' );
59
63
64
+ // If the config file has a query_toggle_count,
65
+ // use it, but remove it from the config array.
66
+ if ( isset ($ config ['query_toggle_count ' ]) )
67
+ {
68
+ $ this ->_query_toggle_count = (int ) $ config ['query_toggle_count ' ];
69
+ unset($ config ['query_toggle_count ' ]);
70
+ }
71
+
60
72
// default all sections to display
61
73
foreach ($ this ->_available_sections as $ section )
62
74
{
@@ -65,14 +77,18 @@ public function __construct($config = array())
65
77
$ this ->_compile_ {$ section } = TRUE ;
66
78
}
67
79
}
68
-
80
+
69
81
// Make sure the Console is loaded.
70
82
if (!class_exists ('Console ' ))
71
83
{
72
84
$ this ->CI ->load ->library ('Console ' );
73
85
}
74
86
75
87
$ this ->set_sections ($ config );
88
+
89
+ // Strange hack to get access to the current
90
+ // vars in the CI_Loader class.
91
+ $ this ->_ci_cached_vars = $ this ->CI ->load ->_ci_cached_vars ;
76
92
}
77
93
78
94
// --------------------------------------------------------------------
@@ -112,7 +128,7 @@ protected function _compile_benchmarks()
112
128
{
113
129
$ profile = array ();
114
130
$ output = array ();
115
-
131
+
116
132
foreach ($ this ->CI ->benchmark ->marker as $ key => $ val )
117
133
{
118
134
// We match the "end" marker so that the list ends
@@ -128,13 +144,14 @@ protected function _compile_benchmarks()
128
144
129
145
// Build a table containing the profile data.
130
146
// Note: At some point we might want to make this data available to be logged.
131
-
132
147
foreach ($ profile as $ key => $ val )
133
148
{
134
149
$ key = ucwords (str_replace (array ('_ ' , '- ' ), ' ' , $ key ));
135
150
$ output [$ key ] = $ val ;
136
151
}
137
152
153
+ unset($ profile );
154
+
138
155
return $ output ;
139
156
}
140
157
@@ -167,6 +184,9 @@ protected function _compile_queries()
167
184
// Load the text helper so we can highlight the SQL
168
185
$ this ->CI ->load ->helper ('text ' );
169
186
187
+ // Key words we want bolded
188
+ $ highlight = array ('SELECT ' , 'DISTINCT ' , 'FROM ' , 'WHERE ' , 'AND ' , 'LEFT JOIN ' , 'ORDER BY ' , 'GROUP BY ' , 'LIMIT ' , 'INSERT ' , 'INTO ' , 'VALUES ' , 'UPDATE ' , 'OR ' , 'HAVING ' , 'OFFSET ' , 'NOT IN ' , 'IN ' , 'LIKE ' , 'NOT LIKE ' , 'COUNT ' , 'MAX ' , 'MIN ' , 'ON ' , 'AS ' , 'AVG ' , 'SUM ' , '( ' , ') ' );
189
+
170
190
foreach ($ dbs as $ db )
171
191
{
172
192
if (count ($ db ->queries ) == 0 )
@@ -176,14 +196,20 @@ protected function _compile_queries()
176
196
else
177
197
{
178
198
$ total = 0 ; // total query time
179
-
199
+
180
200
foreach ($ db ->queries as $ key => $ val )
181
201
{
182
202
$ time = number_format ($ db ->query_times [$ key ], 4 );
183
203
$ total += $ db ->query_times [$ key ];
204
+
205
+ foreach ($ highlight as $ bold )
206
+ {
207
+ $ val = str_replace ($ bold , '<b> ' . $ bold .'</b> ' , $ val );
208
+ }
209
+
184
210
$ output [][$ time ] = $ val ;
185
211
}
186
-
212
+
187
213
$ total = number_format ($ total , 4 );
188
214
$ output [][$ total ] = 'Total Query Execution Time ' ;
189
215
}
@@ -204,28 +230,24 @@ protected function _compile_queries()
204
230
protected function _compile_get ()
205
231
{
206
232
$ output = array ();
207
-
208
- if (count ($ _GET ) == 0 )
233
+
234
+ $ get = $ this ->CI ->input ->get ();
235
+
236
+ if (count ($ get ) == 0 )
209
237
{
210
238
$ output = $ this ->CI ->lang ->line ('profiler_no_get ' );
211
239
}
212
240
else
213
241
{
214
- foreach ($ _GET as $ key => $ val )
242
+ foreach ($ get as $ key => $ val )
215
243
{
216
- if ( ! is_numeric ($ key ))
217
- {
218
- $ key = "' " .$ key ."' " ;
219
- }
220
-
221
- $ output .= "<tr><td style='width:50%;color:#000;background-color:#ddd;padding:5px'>$_GET[ " .$ key ."] </td><td style='width:50%;padding:5px;color:#cd6e00;font-weight:normal;background-color:#ddd;'> " ;
222
244
if (is_array ($ val ))
223
245
{
224
- $ output [' $_GET[ ' . $ key . ' ] ' ] = "<pre> " . htmlspecialchars (stripslashes (print_r ($ val , true ))) . "</pre> " ;
246
+ $ output [$ key ] = "<pre> " . htmlspecialchars (stripslashes (print_r ($ val , true ))) . "</pre> " ;
225
247
}
226
248
else
227
249
{
228
- $ output [' $_GET[ ' . $ key . ' ] ' ] = htmlspecialchars (stripslashes ($ val ));
250
+ $ output [$ key ] = htmlspecialchars (stripslashes ($ val ));
229
251
}
230
252
}
231
253
}
@@ -243,7 +265,7 @@ protected function _compile_get()
243
265
protected function _compile_post ()
244
266
{
245
267
$ output = array ();
246
-
268
+
247
269
if (count ($ _POST ) == 0 )
248
270
{
249
271
$ output = $ this ->CI ->lang ->line ('profiler_no_post ' );
@@ -379,60 +401,60 @@ protected function _compile_config()
379
401
380
402
// --------------------------------------------------------------------
381
403
382
- public function _compile_files ()
404
+ public function _compile_files ()
383
405
{
384
406
$ files = get_included_files ();
385
-
407
+
386
408
sort ($ files );
387
-
409
+
388
410
return $ files ;
389
411
}
390
-
412
+
391
413
//--------------------------------------------------------------------
392
-
393
- public function _compile_console ()
414
+
415
+ public function _compile_console ()
394
416
{
395
417
$ logs = Console::get_logs ();
396
-
397
- if ($ logs ['console ' ])
418
+
419
+ if ($ logs ['console ' ])
398
420
{
399
- foreach ($ logs ['console ' ] as $ key => $ log )
421
+ foreach ($ logs ['console ' ] as $ key => $ log )
400
422
{
401
- if ($ log ['type ' ] == 'log ' )
423
+ if ($ log ['type ' ] == 'log ' )
402
424
{
403
425
$ logs ['console ' ][$ key ]['data ' ] = print_r ($ log ['data ' ], true );
404
426
}
405
- elseif ($ log ['type ' ] == 'memory ' )
427
+ elseif ($ log ['type ' ] == 'memory ' )
406
428
{
407
429
$ logs ['console ' ][$ key ]['data ' ] = $ this ->get_file_size ($ log ['data ' ]);
408
430
}
409
431
}
410
432
}
411
-
433
+
412
434
return $ logs ;
413
435
}
414
-
436
+
415
437
//--------------------------------------------------------------------
416
-
438
+
417
439
function _compile_userdata ()
418
440
{
419
441
$ output = array ();
420
-
421
- if (FALSE !== $ this ->CI ->load ->is_loaded ('session ' ))
442
+
443
+ if (FALSE !== $ this ->CI ->load ->is_loaded ('session ' ))
422
444
{
423
-
445
+
424
446
$ compiled_userdata = $ this ->CI ->session ->all_userdata ();
425
447
426
448
if (count ($ compiled_userdata ))
427
- {
449
+ {
428
450
foreach ($ compiled_userdata as $ key => $ val )
429
451
{
430
452
if (is_numeric ($ key ))
431
453
{
432
454
$ output [$ key ] = "' $ val' " ;
433
455
}
434
456
435
- if (is_array ($ val ))
457
+ if (is_array ($ val ) || is_object ( $ val ) )
436
458
{
437
459
$ output [$ key ] = htmlspecialchars (stripslashes (print_r ($ val , true )));
438
460
}
@@ -443,12 +465,46 @@ function _compile_userdata()
443
465
}
444
466
}
445
467
}
446
-
468
+
447
469
return $ output ;
448
470
}
449
-
471
+
450
472
//--------------------------------------------------------------------
451
-
473
+
474
+ /**
475
+ * Compile View Data
476
+ *
477
+ * Allows any data passed to views to be available in the profiler bar.
478
+ *
479
+ * @return array
480
+ */
481
+ public function _compile_view_data ()
482
+ {
483
+ $ output = '' ;
484
+
485
+ foreach ($ this ->_ci_cached_vars as $ key => $ val )
486
+ {
487
+ if (is_numeric ($ key ))
488
+ {
489
+ $ output [$ key ] = "' $ val' " ;
490
+ }
491
+
492
+ if (is_array ($ val ) || is_object ($ val ))
493
+ {
494
+ $ output [$ key ] = htmlspecialchars (stripslashes (print_r ($ val , true )));
495
+ }
496
+ else
497
+ {
498
+ $ output [$ key ] = htmlspecialchars (stripslashes ($ val ));
499
+ }
500
+ }
501
+
502
+ return $ output ;
503
+ }
504
+
505
+ //--------------------------------------------------------------------
506
+
507
+
452
508
public static function get_file_size ($ size , $ retstring = null ) {
453
509
// adapted from code at http://aidanlister.com/repos/v/function.size_readable.php
454
510
$ sizes = array ('bytes ' , 'kB ' , 'MB ' , 'GB ' , 'TB ' , 'PB ' , 'EB ' , 'ZB ' , 'YB ' );
@@ -461,11 +517,11 @@ public static function get_file_size($size, $retstring = null) {
461
517
if ($ size < 1024 ) { break ; }
462
518
if ($ sizestring != $ lastsizestring ) { $ size /= 1024 ; }
463
519
}
464
-
520
+
465
521
if ($ sizestring == $ sizes [0 ]) { $ retstring = '%01d %s ' ; } // Bytes aren't normally fractional
466
522
return sprintf ($ retstring , $ size , $ sizestring );
467
523
}
468
-
524
+
469
525
//--------------------------------------------------------------------
470
526
471
527
/**
@@ -476,7 +532,7 @@ public static function get_file_size($size, $retstring = null) {
476
532
public function run ()
477
533
{
478
534
$ this ->CI ->load ->helper ('language ' );
479
-
535
+
480
536
$ fields_displayed = 0 ;
481
537
482
538
foreach ($ this ->_available_sections as $ section )
@@ -486,31 +542,11 @@ public function run()
486
542
$ func = "_compile_ {$ section }" ;
487
543
if ($ section == 'http_headers ' ) $ section = 'headers ' ;
488
544
$ this ->_sections [$ section ] = $ this ->{$ func }();
489
- $ fields_displayed ++;
545
+ $ fields_displayed ++;
490
546
}
491
547
}
492
548
493
- // Has the user created an override in application/views?
494
- if (is_file (APPPATH .'views/profiler_template ' .EXT ))
495
- {
496
- $ output = $ this ->CI ->load ->view ('profiler_template ' , array ('sections ' => $ this ->_sections ), true );
497
- }
498
- else
499
- {
500
- // Load the view from system/views
501
- $ orig_view_path = $ this ->CI ->load ->_ci_view_path ;
502
- $ this ->CI ->load ->_ci_view_path = BASEPATH .'views/ ' ;
503
-
504
- $ output = $ this ->CI ->load ->_ci_load (array (
505
- '_ci_view ' => 'profiler_template ' ,
506
- '_ci_vars ' => array ('sections ' => $ this ->_sections ),
507
- '_ci_return ' => true ,
508
- ));
509
-
510
- $ this ->CI ->load ->_ci_view_path = $ orig_view_path ;
511
- }
512
-
513
- return $ output ;
549
+ return $ this ->CI ->load ->view ('profiler_template ' , array ('sections ' => $ this ->_sections ), true );
514
550
}
515
551
516
552
}
0 commit comments