Skip to content

Commit a605f5b

Browse files
committed
Long overdue update. This brings the folder structure into readiness for being a Spark and address the following topics:
* Two additional bar locations (bottom and top, both full-width) provided * Simpler method for setting the bar location. $bar_location variable at the top of the view file. * View Data is available under the 'Vars' tab of the profiler bar. Fixes lonnieezell#14 * Works correctly with $_GET data now. Fixes lonnieezell#13 * No longer uses $_ci_view_path so should work with HMVC extension. Fixes # 10
1 parent e099d4e commit a605f5b

File tree

4 files changed

+248
-143
lines changed

4 files changed

+248
-143
lines changed

config/profiler.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2+
3+
//--------------------------------------------------------------------
4+
// Profiler Sections
5+
//--------------------------------------------------------------------
6+
// Choose which sections you want to show up in your profiler bar.
7+
//
8+
9+
$config['benchmarks'] = TRUE;
10+
$config['config'] = TRUE;
11+
$config['controller_info'] = TRUE;
12+
$config['get'] = TRUE;
13+
$config['http_headers'] = TRUE;
14+
$config['memory_usage'] = TRUE;
15+
$config['post'] = TRUE;
16+
$config['queries'] = TRUE;
17+
$config['uri_string'] = TRUE;
18+
$config['view_data'] = TRUE;
19+
$config['query_toggle_count'] = 50;

libraries/Profiler.php

+104-68
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
* @author ExpressionEngine Dev Team
3131
* @link http://codeigniter.com/user_guide/general/profiling.html
3232
*/
33-
class CI_Profiler {
33+
class CI_Profiler extends CI_Loader {
3434

35-
var $CI;
35+
protected $CI;
3636

3737
protected $_available_sections = array(
3838
'benchmarks',
@@ -46,9 +46,13 @@ class CI_Profiler {
4646
'config',
4747
'files',
4848
'console',
49-
'userdata'
49+
'userdata',
50+
'view_data'
5051
);
51-
protected $_sections = array(); // Stores _compile_x() results
52+
53+
protected $_sections = array(); // Stores _compile_x() results
54+
55+
protected $_query_toggle_count = 25;
5256

5357
// --------------------------------------------------------------------
5458

@@ -57,6 +61,14 @@ public function __construct($config = array())
5761
$this->CI =& get_instance();
5862
$this->CI->load->language('profiler');
5963

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+
6072
// default all sections to display
6173
foreach ($this->_available_sections as $section)
6274
{
@@ -65,14 +77,18 @@ public function __construct($config = array())
6577
$this->_compile_{$section} = TRUE;
6678
}
6779
}
68-
80+
6981
// Make sure the Console is loaded.
7082
if (!class_exists('Console'))
7183
{
7284
$this->CI->load->library('Console');
7385
}
7486

7587
$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;
7692
}
7793

7894
// --------------------------------------------------------------------
@@ -112,7 +128,7 @@ protected function _compile_benchmarks()
112128
{
113129
$profile = array();
114130
$output = array();
115-
131+
116132
foreach ($this->CI->benchmark->marker as $key => $val)
117133
{
118134
// We match the "end" marker so that the list ends
@@ -128,13 +144,14 @@ protected function _compile_benchmarks()
128144

129145
// Build a table containing the profile data.
130146
// Note: At some point we might want to make this data available to be logged.
131-
132147
foreach ($profile as $key => $val)
133148
{
134149
$key = ucwords(str_replace(array('_', '-'), ' ', $key));
135150
$output[$key] = $val;
136151
}
137152

153+
unset($profile);
154+
138155
return $output;
139156
}
140157

@@ -167,6 +184,9 @@ protected function _compile_queries()
167184
// Load the text helper so we can highlight the SQL
168185
$this->CI->load->helper('text');
169186

187+
// Key words we want bolded
188+
$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', '(', ')');
189+
170190
foreach ($dbs as $db)
171191
{
172192
if (count($db->queries) == 0)
@@ -176,14 +196,20 @@ protected function _compile_queries()
176196
else
177197
{
178198
$total = 0; // total query time
179-
199+
180200
foreach ($db->queries as $key => $val)
181201
{
182202
$time = number_format($db->query_times[$key], 4);
183203
$total += $db->query_times[$key];
204+
205+
foreach ($highlight as $bold)
206+
{
207+
$val = str_replace($bold, '<b>'. $bold .'</b>', $val);
208+
}
209+
184210
$output[][$time] = $val;
185211
}
186-
212+
187213
$total = number_format($total, 4);
188214
$output[][$total] = 'Total Query Execution Time';
189215
}
@@ -204,28 +230,24 @@ protected function _compile_queries()
204230
protected function _compile_get()
205231
{
206232
$output = array();
207-
208-
if (count($_GET) == 0)
233+
234+
$get = $this->CI->input->get();
235+
236+
if (count($get) == 0)
209237
{
210238
$output = $this->CI->lang->line('profiler_no_get');
211239
}
212240
else
213241
{
214-
foreach ($_GET as $key => $val)
242+
foreach ($get as $key => $val)
215243
{
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'>&#36;_GET[".$key."]&nbsp;&nbsp; </td><td style='width:50%;padding:5px;color:#cd6e00;font-weight:normal;background-color:#ddd;'>";
222244
if (is_array($val))
223245
{
224-
$output['&#36;_GET['. $key .']'] = "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
246+
$output[$key] = "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
225247
}
226248
else
227249
{
228-
$output['&#36;_GET['. $key .']'] = htmlspecialchars(stripslashes($val));
250+
$output[$key] = htmlspecialchars(stripslashes($val));
229251
}
230252
}
231253
}
@@ -243,7 +265,7 @@ protected function _compile_get()
243265
protected function _compile_post()
244266
{
245267
$output = array();
246-
268+
247269
if (count($_POST) == 0)
248270
{
249271
$output = $this->CI->lang->line('profiler_no_post');
@@ -379,60 +401,60 @@ protected function _compile_config()
379401

380402
// --------------------------------------------------------------------
381403

382-
public function _compile_files()
404+
public function _compile_files()
383405
{
384406
$files = get_included_files();
385-
407+
386408
sort($files);
387-
409+
388410
return $files;
389411
}
390-
412+
391413
//--------------------------------------------------------------------
392-
393-
public function _compile_console()
414+
415+
public function _compile_console()
394416
{
395417
$logs = Console::get_logs();
396-
397-
if ($logs['console'])
418+
419+
if ($logs['console'])
398420
{
399-
foreach ($logs['console'] as $key => $log)
421+
foreach ($logs['console'] as $key => $log)
400422
{
401-
if ($log['type'] == 'log')
423+
if ($log['type'] == 'log')
402424
{
403425
$logs['console'][$key]['data'] = print_r($log['data'], true);
404426
}
405-
elseif ($log['type'] == 'memory')
427+
elseif ($log['type'] == 'memory')
406428
{
407429
$logs['console'][$key]['data'] = $this->get_file_size($log['data']);
408430
}
409431
}
410432
}
411-
433+
412434
return $logs;
413435
}
414-
436+
415437
//--------------------------------------------------------------------
416-
438+
417439
function _compile_userdata()
418440
{
419441
$output = array();
420-
421-
if (FALSE !== $this->CI->load->is_loaded('session'))
442+
443+
if (FALSE !== $this->CI->load->is_loaded('session'))
422444
{
423-
445+
424446
$compiled_userdata = $this->CI->session->all_userdata();
425447

426448
if (count($compiled_userdata))
427-
{
449+
{
428450
foreach ($compiled_userdata as $key => $val)
429451
{
430452
if (is_numeric($key))
431453
{
432454
$output[$key] = "'$val'";
433455
}
434456

435-
if (is_array($val))
457+
if (is_array($val) || is_object($val))
436458
{
437459
$output[$key] = htmlspecialchars(stripslashes(print_r($val, true)));
438460
}
@@ -443,12 +465,46 @@ function _compile_userdata()
443465
}
444466
}
445467
}
446-
468+
447469
return $output;
448470
}
449-
471+
450472
//--------------------------------------------------------------------
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+
452508
public static function get_file_size($size, $retstring = null) {
453509
// adapted from code at http://aidanlister.com/repos/v/function.size_readable.php
454510
$sizes = array('bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
@@ -461,11 +517,11 @@ public static function get_file_size($size, $retstring = null) {
461517
if ($size < 1024) { break; }
462518
if ($sizestring != $lastsizestring) { $size /= 1024; }
463519
}
464-
520+
465521
if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't normally fractional
466522
return sprintf($retstring, $size, $sizestring);
467523
}
468-
524+
469525
//--------------------------------------------------------------------
470526

471527
/**
@@ -476,7 +532,7 @@ public static function get_file_size($size, $retstring = null) {
476532
public function run()
477533
{
478534
$this->CI->load->helper('language');
479-
535+
480536
$fields_displayed = 0;
481537

482538
foreach ($this->_available_sections as $section)
@@ -486,31 +542,11 @@ public function run()
486542
$func = "_compile_{$section}";
487543
if ($section == 'http_headers') $section = 'headers';
488544
$this->_sections[$section] = $this->{$func}();
489-
$fields_displayed++;
545+
$fields_displayed++;
490546
}
491547
}
492548

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);
514550
}
515551

516552
}

spark.info

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This is the spark-sdk specification. It's in a magical format called YAML.
2+
# Use this format while developing your own sparks!
3+
4+
# This is the spark name. This should be the registered name of the spark.
5+
# It is here for informational purposes only.
6+
name: forensics
7+
8+
# This is the current version of this spark. All sparks should be in
9+
# x.x.x format. Validation will fail otherwise.
10+
version: 1.0.0
11+
12+
# This is the version of CodeIgniter this spark is compatible up to. It should
13+
# be in x.x.x format
14+
compatibility: 2.1.4
15+
16+
# There are no dependencies now, but when there are, uncomment below.
17+
#dependencies:
18+
# some-spark-1: 1.0.0
19+
# some-other-spark-2: 1.0.0

0 commit comments

Comments
 (0)