日期:2014-05-17  浏览次数:20446 次

CI框架源码阅读---------Output.php
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------

/**
 * Output Class
 *
 * Responsible 负责 for sending final output to browser
 * 负责把最终的输出发送到浏览器
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Output
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/libraries/output.html
 */
class CI_Output {

	/**
	 * Current output string
	 * 当前输出的字符串
	 *
	 * @var string
	 * @access 	protected
	 */
	protected $final_output;
	/**
	 * Cache expiration time
	 * 缓存终结的时间
	 * @var int
	 * @access 	protected
	 */
	protected $cache_expiration	= 0;
	/**
	 * List of server headers
	 * 服务器头列表
	 * @var array
	 * @access 	protected
	 */
	protected $headers			= array();
	/**
	 * List of mime types
	 * 
	 * @var array
	 * @access 	protected
	 */
	protected $mime_types		= array();
	/**
	 * Determines wether profiler is enabled
	 * 是否启用分析器
	 * @var book
	 * @access 	protected
	 */
	protected $enable_profiler	= FALSE;
	/**
	 * Determines if output compression is enabled
	 * 是否开启输出压缩
	 * @var bool
	 * @access 	protected
	 */
	protected $_zlib_oc			= FALSE;
	/**
	 * List of profiler sections
	 * 分析器列表
	 *
	 * @var array
	 * @access 	protected
	 */
	protected $_profiler_sections = array();
	/**
	 * Whether or not to parse variables like {elapsed_time} and {memory_usage}
	 * 是否解析变量{elapsed_time} and {memory_usage}
	 * 注意文档说这里有错误详见http://codeigniter.org.cn/user_guide/libraries/output.html
	 * 最下方
	 * @var bool
	 * @access 	protected
	 */
	protected $parse_exec_vars	= TRUE;

	/**
	 * Constructor
	 *
	 */
	function __construct()
	{
		// 返回配置项zlib.output_compression的值并赋给$this->_zlib_oc 
		// 如果配置项中开启了输出压缩功能则	$this->_zlib_oc 的值为on
		$this->_zlib_oc = @ini_get('zlib.output_compression');

		// Get mime types for later
		// 获取mimetype
		if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
		{
		    include APPPATH.'config/'.ENVIRONMENT.'/mimes.php';
		}
		else
		{
			include APPPATH.'config/mimes.php';
		}
		
		// $mimes 是mimes.php中定义的一个数组
		$this->mime_types = $mimes;

		log_message('debug', "Output Class Initialized");
	}

	// --------------------------------

	/**
	 * Get Output
	 * 使用这个方法,你可以得到将要输出的数据,并把它保存起来
	 * Returns the current output string
	 * 返回当前输出的字符串
	 * @access	public
	 * @return	string
	 */
	function get_output()
	{
		return $this->final_output;
	}

	// --------------------------------

	/**
	 * Set Output
	 *
	 * Sets the output string
	 * 设置输出的字符串
	 * @access	public
	 * @param	string
	 * @return	void
	 */
	function set_output($output)
	{
		$this->final_output = $output;

		return $this;
	}

	// --------------------------------

	/**
	 * Append Output
	 * 在最终输出字符串后,追加数据
	 * Appends data onto the output string
	 * 
	 * @access	public
	 * @param	string
	 * @return	void
	 */
	function append_output($output)
	{
		if ($this->final_output == '')
		{
			$this->final_output = $output;
		}
		else
		{
			$this->final_output .= $output;
		}

		return $this;
	}

	// --------------------------------

	/**
	 * Set Header
	 * 使用此方法,允许你设置将会被发送到浏览器的HTTP协议的标头,作用相当于php的标准函数: 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...
	 *
	 * @access	public
	 * @param	string
	 * @param 	bool
	 * @return	void
	 */
	function set_header($header, $replace = TRUE)
	{
		// If zlib.o