Skip to content

Commit f29254d

Browse files
zgmunshichriskacerguis
authored andcommitted
Codeigniter Profiling Feature added (chriskacerguis#854)
* config parameter added config parameter for enabling / disabling profiler
 * hook post_controller_constructor hook for enabling / disabling the profiler through out the application
 * code igniter profiler default parameters
 code igniter profiler default parameters
 * added hook file for profiler added hook file for profiler * check for profiling before returning response added check for if profiling enabled then just return data without any formatting

1 parent 25761e7 commit f29254d

File tree

5 files changed

+130
-65
lines changed

5 files changed

+130
-65
lines changed

application/config/config.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@
102102
*/
103103
$config['enable_hooks'] = FALSE;
104104

105+
/*
106+
|--------------------------------------------------------------------------
107+
| Enable/Disable System Profling
108+
|--------------------------------------------------------------------------
109+
|
110+
| If you would like to use the 'profiling' feature you must enable it by
111+
| setting this variable to TRUE (boolean).
112+
|
113+
*/
114+
$config['enable_profiling'] = FALSE;
115+
105116
/*
106117
|--------------------------------------------------------------------------
107118
| Class Extension Prefix

application/config/hooks.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@
1111
| https://codeigniter.com/user_guide/general/hooks.html
1212
|
1313
*/
14+
// hook for enable/disable profiling
15+
$hook['post_controller_constructor'][] = array(
16+
'class' => 'ProfilerEnabler',
17+
'function' => 'enableProfiler',
18+
'filename' => 'hooks.profiler.php',
19+
'filepath' => 'hooks',
20+
'params' => array()
21+
);

application/config/profiler.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@
1212
| https://codeigniter.com/user_guide/general/profiling.html
1313
|
1414
*/
15+
$config['benchmarks'] = TRUE;
16+
$config['config'] = TRUE;
17+
$config['controller_info'] = TRUE;
18+
$config['get'] = TRUE;
19+
$config['http_headers'] = TRUE;
20+
$config['memory_usage'] = TRUE;
21+
$config['post'] = TRUE;
22+
$config['queries'] = TRUE;
23+
$config['eloquent'] = FALSE;
24+
$config['uri_string'] = TRUE;
25+
$config['view_data'] = TRUE;
26+
$config['query_toggle_count'] = 100;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/*
3+
* Class for enabling profiler through out the application
4+
*
5+
* @package CodeIgniter
6+
* @subpackage Libraries
7+
* @category Libraries
8+
* @author Zeeshan M
9+
*/
10+
class ProfilerEnabler
11+
{
12+
// enable or disable profiling based on config values
13+
function enableProfiler(){
14+
$CI = &get_instance();
15+
$CI->output->enable_profiler( config_item('enable_profiling') );
16+
}
17+
}
18+
?>

application/libraries/REST_Controller.php

Lines changed: 81 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -765,72 +765,88 @@ public function _remap($object_called, $arguments = [])
765765
*/
766766
public function response($data = NULL, $http_code = NULL)
767767
{
768+
//if profiling enabled then print profiling data
769+
$isProfilingEnabled = $this->config->item('enable_profiling');
770+
if(!$isProfilingEnabled){
768771
ob_start();
769-
// If the HTTP status is not NULL, then cast as an integer
770-
if ($http_code !== NULL)
771-
{
772-
// So as to be safe later on in the process
773-
$http_code = (int) $http_code;
774-
}
775-
776-
// Set the output as NULL by default
777-
$output = NULL;
778-
779-
// If data is NULL and no HTTP status code provided, then display, error and exit
780-
if ($data === NULL && $http_code === NULL)
781-
{
782-
$http_code = self::HTTP_NOT_FOUND;
783-
}
784-
785-
// If data is not NULL and a HTTP status code provided, then continue
786-
elseif ($data !== NULL)
787-
{
788-
// If the format method exists, call and return the output in that format
789-
if (method_exists($this->format, 'to_' . $this->response->format))
790-
{
791-
// Set the format header
792-
$this->output->set_content_type($this->_supported_formats[$this->response->format], strtolower($this->config->item('charset')));
793-
$output = $this->format->factory($data)->{'to_' . $this->response->format}();
794-
795-
// An array must be parsed as a string, so as not to cause an array to string error
796-
// Json is the most appropriate form for such a data type
797-
if ($this->response->format === 'array')
798-
{
799-
$output = $this->format->factory($output)->{'to_json'}();
800-
}
801-
}
802-
else
803-
{
804-
// If an array or object, then parse as a json, so as to be a 'string'
805-
if (is_array($data) || is_object($data))
806-
{
807-
$data = $this->format->factory($data)->{'to_json'}();
808-
}
809-
810-
// Format is not supported, so output the raw data as a string
811-
$output = $data;
812-
}
813-
}
814-
815-
// If not greater than zero, then set the HTTP status code as 200 by default
816-
// Though perhaps 500 should be set instead, for the developer not passing a
817-
// correct HTTP status code
818-
$http_code > 0 || $http_code = self::HTTP_OK;
819-
820-
$this->output->set_status_header($http_code);
821-
822-
// JC: Log response code only if rest logging enabled
823-
if ($this->config->item('rest_enable_logging') === TRUE)
824-
{
825-
$this->_log_response_code($http_code);
826-
}
827-
828-
// Output the data
829-
$this->output->set_output($output);
830-
831-
ob_end_flush();
832-
833-
// Otherwise dump the output automatically
772+
// If the HTTP status is not NULL, then cast as an integer
773+
if ($http_code !== NULL)
774+
{
775+
// So as to be safe later on in the process
776+
$http_code = (int) $http_code;
777+
}
778+
779+
// Set the output as NULL by default
780+
$output = NULL;
781+
782+
// If data is NULL and no HTTP status code provided, then display, error and exit
783+
if ($data === NULL && $http_code === NULL)
784+
{
785+
$http_code = self::HTTP_NOT_FOUND;
786+
}
787+
788+
// If data is not NULL and a HTTP status code provided, then continue
789+
elseif ($data !== NULL)
790+
{
791+
// If the format method exists, call and return the output in that format
792+
if (method_exists($this->format, 'to_' . $this->response->format))
793+
{
794+
// Set the format header
795+
$this->output->set_content_type($this->_supported_formats[$this->response->format], strtolower($this->config->item('charset')));
796+
$output = $this->format->factory($data)->{'to_' . $this->response->format}();
797+
798+
// An array must be parsed as a string, so as not to cause an array to string error
799+
// Json is the most appropriate form for such a data type
800+
if ($this->response->format === 'array')
801+
{
802+
$output = $this->format->factory($output)->{'to_json'}();
803+
}
804+
}
805+
else
806+
{
807+
// If an array or object, then parse as a json, so as to be a 'string'
808+
if (is_array($data) || is_object($data))
809+
{
810+
$data = $this->format->factory($data)->{'to_json'}();
811+
}
812+
813+
// Format is not supported, so output the raw data as a string
814+
$output = $data;
815+
}
816+
}
817+
818+
// If not greater than zero, then set the HTTP status code as 200 by default
819+
// Though perhaps 500 should be set instead, for the developer not passing a
820+
// correct HTTP status code
821+
$http_code > 0 || $http_code = self::HTTP_OK;
822+
823+
$this->output->set_status_header($http_code);
824+
825+
// JC: Log response code only if rest logging enabled
826+
if ($this->config->item('rest_enable_logging') === TRUE)
827+
{
828+
$this->_log_response_code($http_code);
829+
}
830+
831+
// Output the data
832+
$this->output->set_output($output);
833+
834+
if ($continue === FALSE)
835+
{
836+
// Display the data and exit execution
837+
$this->output->_display();
838+
exit;
839+
}
840+
else
841+
{
842+
ob_end_flush();
843+
}
844+
845+
// Otherwise dump the output automatically
846+
}
847+
else{
848+
echo json_encode($data);
849+
}
834850
}
835851

836852
/**

0 commit comments

Comments
 (0)