CI_Output Class Reference

List of all members.


Public Member Functions

 CI_Output ()
 get_output ()
 Get Output.
 set_output ($output)
 Set Output.
 append_output ($output)
 Append Output.
 set_header ($header)
 Set Header.
 enable_profiler ($val=TRUE)
 Enable/disable Profiler.
 cache ($time)
 Set Cache.
 _display ($output= '')
 Display Output.
 _write_cache ($output)
 Write a Cache File.
 _display_cache (&$CFG, &$URI)
 Update/serve a cached file.

Public Attributes

 $final_output
 $cache_expiration = 0
 $headers = array()
 $enable_profiler = FALSE

Detailed Description

Definition at line 29 of file Output.php.


Member Function Documentation

CI_Output::_display ( output = ''  ) 

Display Output.

All "view" data is automatically put into this variable by the controller class:

$this->final_output

This function sends the finalized output data to the browser along with any server headers and profile data. It also stops the benchmark timer so the page rendering speed and memory usage can be shown.

public

Returns:
mixed

Definition at line 159 of file Output.php.

References $BM, $CFG, $CI, _write_cache(), enable_profiler(), get_instance(), and log_message().

Referenced by _display_cache().

00160         {       
00161                 // Note:  We use globals because we can't use $CI =& get_instance()
00162                 // since this function is sometimes called by the caching mechanism,
00163                 // which happens before the CI super object is available.
00164                 global $BM, $CFG;
00165                 
00166                 // --------------------------------------------------------------------
00167                 
00168                 // Set the output data
00169                 if ($output == '')
00170                 {
00171                         $output =& $this->final_output;
00172                 }
00173                 
00174                 // --------------------------------------------------------------------
00175                 
00176                 // Do we need to write a cache file?
00177                 if ($this->cache_expiration > 0)
00178                 {
00179                         $this->_write_cache($output);
00180                 }
00181                 
00182                 // --------------------------------------------------------------------
00183 
00184                 // Parse out the elapsed time and memory usage,
00185                 // then swap the pseudo-variables with the data
00186                                 
00187                 $elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');         
00188                 $output = str_replace('{elapsed_time}', $elapsed, $output);
00189                 
00190                 $memory  = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
00191                 $output = str_replace('{memory_usage}', $memory, $output);              
00192 
00193                 // --------------------------------------------------------------------
00194                 
00195                 // Is compression requested?
00196                 if ($CFG->item('compress_output') === TRUE)
00197                 {
00198                         if (extension_loaded('zlib'))
00199                         {
00200                                 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
00201                                 {
00202                                         ob_start('ob_gzhandler');
00203                                 }
00204                         }
00205                 }
00206 
00207                 // --------------------------------------------------------------------
00208                 
00209                 // Are there any server headers to send?
00210                 if (count($this->headers) > 0)
00211                 {
00212                         foreach ($this->headers as $header)
00213                         {
00214                                 @header($header);
00215                         }
00216                 }               
00217 
00218                 // --------------------------------------------------------------------
00219                 
00220                 // Does the get_instance() function exist?
00221                 // If not we know we are dealing with a cache file so we'll
00222                 // simply echo out the data and exit.
00223                 if ( ! function_exists('get_instance'))
00224                 {
00225                         echo $output;
00226                         log_message('debug', "Final output sent to browser");
00227                         log_message('debug', "Total execution time: ".$elapsed);
00228                         return TRUE;
00229                 }
00230         
00231                 // --------------------------------------------------------------------
00232 
00233                 // Grab the super object.  We'll need it in a moment...
00234                 $CI =& get_instance();
00235                 
00236                 // Do we need to generate profile data?
00237                 // If so, load the Profile class and run it.
00238                 if ($this->enable_profiler == TRUE)
00239                 {
00240                         $CI->load->library('profiler');                         
00241                                                                                 
00242                         // If the output data contains closing </body> and </html> tags
00243                         // we will remove them and add them back after we insert the profile data
00244                         if (preg_match("|</body>.*?</html>|is", $output))
00245                         {
00246                                 $output  = preg_replace("|</body>.*?</html>|is", '', $output);
00247                                 $output .= $CI->profiler->run();
00248                                 $output .= '</body></html>';
00249                         }
00250                         else
00251                         {
00252                                 $output .= $CI->profiler->run();
00253                         }
00254                 }
00255                 
00256                 // --------------------------------------------------------------------
00257 
00258                 // Does the controller contain a function named _output()?
00259                 // If so send the output there.  Otherwise, echo it.
00260                 if (method_exists($CI, '_output'))
00261                 {
00262                         $CI->_output($output);
00263                 }
00264                 else
00265                 {
00266                         echo $output;  // Send it to the browser!
00267                 }
00268                 
00269                 log_message('debug', "Final output sent to browser");
00270                 log_message('debug', "Total execution time: ".$elapsed);                
00271         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Output::_display_cache ( &$  CFG,
&$  URI 
)

Update/serve a cached file.

public

Returns:
void

Definition at line 324 of file Output.php.

References $CFG, $URI, _display(), is_really_writable(), and log_message().

00325         {
00326                 $cache_path = ($CFG->item('cache_path') == '') ? BASEPATH.'cache/' : $CFG->item('cache_path');
00327                         
00328                 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
00329                 {
00330                         return FALSE;
00331                 }
00332                 
00333                 // Build the file path.  The file name is an MD5 hash of the full URI
00334                 $uri =  $CFG->item('base_url').
00335                                 $CFG->item('index_page').
00336                                 $URI->uri_string;
00337                                 
00338                 $filepath = $cache_path.md5($uri);
00339                 
00340                 if ( ! @file_exists($filepath))
00341                 {
00342                         return FALSE;
00343                 }
00344         
00345                 if ( ! $fp = @fopen($filepath, 'rb'))
00346                 {
00347                         return FALSE;
00348                 }
00349                         
00350                 flock($fp, LOCK_SH);
00351                 
00352                 $cache = '';
00353                 if (filesize($filepath) > 0)
00354                 {
00355                         $cache = fread($fp, filesize($filepath));
00356                 }
00357         
00358                 flock($fp, LOCK_UN);
00359                 fclose($fp);
00360                                         
00361                 // Strip out the embedded timestamp             
00362                 if ( ! preg_match("/(\d+TS--->)/", $cache, $match))
00363                 {
00364                         return FALSE;
00365                 }
00366                 
00367                 // Has the file expired? If so we'll delete it.
00368                 if (time() >= trim(str_replace('TS--->', '', $match['1'])))
00369                 {               
00370                         @unlink($filepath);
00371                         log_message('debug', "Cache file has expired. File deleted");
00372                         return FALSE;
00373                 }
00374 
00375                 // Display the cache
00376                 $this->_display(str_replace($match['0'], '', $cache));
00377                 log_message('debug', "Cache file is current. Sending it to browser.");          
00378                 return TRUE;
00379         }

Here is the call graph for this function:

CI_Output::_write_cache ( output  ) 

Write a Cache File.

public

Returns:
void

Definition at line 281 of file Output.php.

References $CI, get_instance(), is_really_writable(), and log_message().

Referenced by _display().

00282         {
00283                 $CI =& get_instance();  
00284                 $path = $CI->config->item('cache_path');
00285         
00286                 $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
00287                 
00288                 if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
00289                 {
00290                         return;
00291                 }
00292                 
00293                 $uri =  $CI->config->item('base_url').
00294                                 $CI->config->item('index_page').
00295                                 $CI->uri->uri_string();
00296                 
00297                 $cache_path .= md5($uri);
00298 
00299                 if ( ! $fp = @fopen($cache_path, 'wb'))
00300                 {
00301                         log_message('error', "Unable to write cache file: ".$cache_path);
00302                         return;
00303                 }
00304                 
00305                 $expire = time() + ($this->cache_expiration * 60);
00306                 
00307                 flock($fp, LOCK_EX);
00308                 fwrite($fp, $expire.'TS--->'.$output);
00309                 flock($fp, LOCK_UN);
00310                 fclose($fp);
00311                 @chmod($cache_path, DIR_WRITE_MODE);
00312 
00313                 log_message('debug', "Cache file written: ".$cache_path);
00314         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Output::append_output ( output  ) 

Append Output.

Appends data onto the output string

public

Parameters:
string 
Returns:
void

Definition at line 84 of file Output.php.

00085         {
00086                 if ($this->final_output == '')
00087                 {
00088                         $this->final_output = $output;
00089                 }
00090                 else
00091                 {
00092                         $this->final_output .= $output;
00093                 }
00094         }

CI_Output::cache ( time  ) 

Set Cache.

public

Parameters:
integer 
Returns:
void

Definition at line 138 of file Output.php.

00139         {
00140                 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
00141         }

CI_Output::CI_Output (  ) 

Definition at line 37 of file Output.php.

References log_message().

00038         {
00039                 log_message('debug', "Output Class Initialized");
00040         }

Here is the call graph for this function:

CI_Output::enable_profiler ( val = TRUE  ) 

Enable/disable Profiler.

public

Parameters:
bool 
Returns:
void

Definition at line 124 of file Output.php.

Referenced by _display().

00125         {
00126                 $this->enable_profiler = (is_bool($val)) ? $val : TRUE;
00127         }

Here is the caller graph for this function:

CI_Output::get_output (  ) 

Get Output.

Returns the current output string

public

Returns:
string

Definition at line 52 of file Output.php.

00053         {
00054                 return $this->final_output;
00055         }

CI_Output::set_header ( header  ) 

Set Header.

Lets you set a server header which will be outputted with the final display.

Note: If a file is cached, headers will not be sent. We need to figure out how to permit header data to be saved with the cache data...

public

Parameters:
string 
Returns:
void

Definition at line 110 of file Output.php.

00111         {
00112                 $this->headers[] = $header;
00113         }

CI_Output::set_output ( output  ) 

Set Output.

Sets the output string

public

Parameters:
string 
Returns:
void

Definition at line 68 of file Output.php.

00069         {
00070                 $this->final_output = $output;
00071         }


Member Data Documentation

CI_Output::$cache_expiration = 0

Definition at line 32 of file Output.php.

CI_Output::$enable_profiler = FALSE

Definition at line 34 of file Output.php.

CI_Output::$final_output

Definition at line 31 of file Output.php.

CI_Output::$headers = array()

Definition at line 33 of file Output.php.


The documentation for this class was generated from the following file: