CI =& get_instance(); $this->CI->load->language('profiler'); // default all sections to display foreach ($this->_available_sections as $section) { if ( ! isset($config[$section])) { $this->_compile_{$section} = TRUE; } } $this->set_sections($config); log_message('info', 'Profiler Class Initialized'); } // -------------------------------------------------------------------- /** * Set Sections * * Sets the private _compile_* properties to enable/disable Profiler sections * * @param mixed $config * @return void */ public function set_sections($config) { if (isset($config['query_toggle_count'])) { $this->_query_toggle_count = (int) $config['query_toggle_count']; unset($config['query_toggle_count']); } foreach ($config as $method => $enable) { if (in_array($method, $this->_available_sections)) { $this->_compile_{$method} = ($enable !== FALSE); } } } // -------------------------------------------------------------------- /** * Auto Profiler * * This function cycles through the entire array of mark points and * matches any two points that are named identically (ending in "_start" * and "_end" respectively). It then compiles the execution times for * all points and returns it as an array * * @return array */ protected function _compile_benchmarks() { $profile = array(); foreach ($this->CI->benchmark->marker as $key => $val) { // We match the "end" marker so that the list ends // up in the order that it was defined if (preg_match('/(.+?)_end$/i', $key, $match) && isset($this->CI->benchmark->marker[$match[1].'_end'], $this->CI->benchmark->marker[$match[1].'_start'])) { $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key); } } // Build a table containing the profile data. // Note: At some point we should turn this into a template that can // be modified. We also might want to make this data available to be logged $output = "\n\n" .'
' ."\n" .'  '.$this->CI->lang->line('profiler_benchmarks')."  " ."\n\n\n\n"; foreach ($profile as $key => $val) { $key = ucwords(str_replace(array('_', '-'), ' ', $key)); $output .= '\n"; } return $output."
' .$key.'  ' .$val."
\n
"; } // -------------------------------------------------------------------- /** * Compile Queries * * @return string */ protected function _compile_queries() { $dbs = array(); // Let's determine which databases are currently connected to foreach (get_object_vars($this->CI) as $name => $cobject) { if (is_object($cobject)) { if ($cobject instanceof CI_DB) { $dbs[get_class($this->CI).':$'.$name] = $cobject; } elseif ($cobject instanceof CI_Model) { foreach (get_object_vars($cobject) as $mname => $mobject) { if ($mobject instanceof CI_DB) { $dbs[get_class($cobject).':$'.$mname] = $mobject; } } } } } if (count($dbs) === 0) { return "\n\n" .'
' ."\n" .'  '.$this->CI->lang->line('profiler_queries').'  ' ."\n\n\n\n" .'\n
' .$this->CI->lang->line('profiler_no_db') ."
\n
"; } // Load the text helper so we can highlight the SQL $this->CI->load->helper('text'); // Key words we want bolded $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', '(', ')'); $output = "\n\n"; $count = 0; foreach ($dbs as $name => $db) { $hide_queries = (count($db->queries) > $this->_query_toggle_count) ? ' display:none' : ''; $total_time = number_format(array_sum($db->query_times), 4).' '.$this->CI->lang->line('profiler_seconds'); $show_hide_js = '('.$this->CI->lang->line('profiler_section_hide').')'; if ($hide_queries !== '') { $show_hide_js = '('.$this->CI->lang->line('profiler_section_show').')'; } $output .= '
' ."\n" .'  '.$this->CI->lang->line('profiler_database') .':  '.$db->database.' ('.$name.')   '.$this->CI->lang->line('profiler_queries') .': '.count($db->queries).' ('.$total_time.')  '.$show_hide_js."\n\n\n" .'\n"; if (count($db->queries) === 0) { $output .= '\n"; } else { foreach ($db->queries as $key => $val) { $time = number_format($db->query_times[$key], 4); $val = highlight_code($val); foreach ($highlight as $bold) { $val = str_replace($bold, ''.$bold.'', $val); } $output .= '\n"; } } $output .= "
' .$this->CI->lang->line('profiler_no_queries')."
' .$time.'  ' .$val."
\n
"; $count++; } return $output; } // -------------------------------------------------------------------- /** * Compile $_GET Data * * @return string */ protected function _compile_get() { $output = "\n\n" .'
' ."\n" .'  '.$this->CI->lang->line('profiler_get_data')."  \n"; if (count($_GET) === 0) { $output .= '
'.$this->CI->lang->line('profiler_no_get').'
'; } else { $output .= "\n\n\n"; foreach ($_GET as $key => $val) { is_int($key) OR $key = "'".htmlspecialchars($key, ENT_QUOTES, config_item('charset'))."'"; $val = (is_array($val) OR is_object($val)) ? '
'.htmlspecialchars(print_r($val, TRUE), ENT_QUOTES, config_item('charset')).'
' : htmlspecialchars($val, ENT_QUOTES, config_item('charset')); $output .= '\n"; } $output .= "
$_GET[' .$key.']   ' .$val."
\n"; } return $output.'
'; } // -------------------------------------------------------------------- /** * Compile $_POST Data * * @return string */ protected function _compile_post() { $output = "\n\n" .'
' ."\n" .'  '.$this->CI->lang->line('profiler_post_data')."  \n"; if (count($_POST) === 0 && count($_FILES) === 0) { $output .= '
'.$this->CI->lang->line('profiler_no_post').'
'; } else { $output .= "\n\n\n"; foreach ($_POST as $key => $val) { is_int($key) OR $key = "'".htmlspecialchars($key, ENT_QUOTES, config_item('charset'))."'"; $val = (is_array($val) OR is_object($val)) ? '
'.htmlspecialchars(print_r($val, TRUE), ENT_QUOTES, config_item('charset')).'
' : htmlspecialchars($val, ENT_QUOTES, config_item('charset')); $output .= '\n"; } foreach ($_FILES as $key => $val) { is_int($key) OR $key = "'".htmlspecialchars($key, ENT_QUOTES, config_item('charset'))."'"; $val = (is_array($val) OR is_object($val)) ? '
'.htmlspecialchars(print_r($val, TRUE), ENT_QUOTES, config_item('charset')).'
' : htmlspecialchars($val, ENT_QUOTES, config_item('charset')); $output .= '\n"; } $output .= "
$_POST[' .$key.']   ' .$val."
$_FILES[' .$key.']   ' .$val."
\n"; } return $output.'
'; } // -------------------------------------------------------------------- /** * Show query string * * @return string */ protected function _compile_uri_string() { return "\n\n" .'
' ."\n" .'  '.$this->CI->lang->line('profiler_uri_string')."  \n" .'
' .($this->CI->uri->uri_string === '' ? $this->CI->lang->line('profiler_no_uri') : $this->CI->uri->uri_string) .'
'; } // -------------------------------------------------------------------- /** * Show the controller and function that were called * * @return string */ protected function _compile_controller_info() { return "\n\n" .'
' ."\n" .'  '.$this->CI->lang->line('profiler_controller_info')."  \n" .'
'.$this->CI->router->class.'/'.$this->CI->router->method .'
'; } // -------------------------------------------------------------------- /** * Compile memory usage * * Display total used memory * * @return string */ protected function _compile_memory_usage() { return "\n\n" .'
' ."\n" .'  '.$this->CI->lang->line('profiler_memory_usage')."  \n" .'
' .(($usage = memory_get_usage()) != '' ? number_format($usage).' bytes' : $this->CI->lang->line('profiler_no_memory')) .'
'; } // -------------------------------------------------------------------- /** * Compile header information * * Lists HTTP headers * * @return string */ protected function _compile_http_headers() { $output = "\n\n" .'
' ."\n" .'  '.$this->CI->lang->line('profiler_headers') .'  ('.$this->CI->lang->line('profiler_section_show').")\n\n\n" .''."\n"; 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', 'HTTP_DNT') as $header) { $val = isset($_SERVER[$header]) ? htmlspecialchars($_SERVER[$header], ENT_QUOTES, config_item('charset')) : ''; $output .= '\n"; } return $output."\n
"; } // -------------------------------------------------------------------- /** * Compile config information * * Lists developer config variables * * @return string */ protected function _compile_config() { $output = "\n\n" .'
' ."\n" .'  '.$this->CI->lang->line('profiler_config').'  ('.$this->CI->lang->line('profiler_section_show').")\n\n\n" .''."\n"; foreach ($this->CI->config->config as $config => $val) { $pre = ''; $pre_close = ''; if (is_array($val) OR is_object($val)) { $val = print_r($val, TRUE); $pre = '
' ;
                                $pre_close = '
'; } $output .= '\n"; } return $output."\n
"; } // -------------------------------------------------------------------- /** * Compile session userdata * * @return string */ protected function _compile_session_data() { if ( ! isset($this->CI->session)) { return; } $output = '
' .'  '.$this->CI->lang->line('profiler_session_data').'  ('.$this->CI->lang->line('profiler_section_show').')' .''; foreach ($this->CI->session->userdata() as $key => $val) { $pre = ''; $pre_close = ''; if (is_array($val) OR is_object($val)) { $val = print_r($val, TRUE); $pre = '
' ;
                                $pre_close = '
'; } $output .= '\n"; } return $output."\n
"; } // -------------------------------------------------------------------- /** * Run the Profiler * * @return string */ public function run() { $output = '
'; $fields_displayed = 0; foreach ($this->_available_sections as $section) { if ($this->_compile_{$section} !== FALSE) { $func = '_compile_'.$section; $output .= $this->{$func}(); $fields_displayed++; } } if ($fields_displayed === 0) { $output .= '

' .$this->CI->lang->line('profiler_no_profiles').'

'; } return $output.'
'; } }