/system/core/Loader.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
 */

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

/**
 * Loader Class
 * 装载机类
 * 
 * Loads views and files
 * 负载的意见和文件
 * 
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @author		ExpressionEngine Dev Team
 * @category	Loader
 * @link		http://codeigniter.com/user_guide/libraries/loader.html
 */
class CI_Loader {

	// All these are set automatically. Don't mess with them.
	// 所有这些都自动设置。不要惹他们。
	
	
	/**
	 * Nesting level of the output buffering mechanism
	 * 嵌套级别的输出缓冲机制
	 * 
	 * @var int
	 * @access protected
	 */
	protected $_ci_ob_level;
	
	
	
	/**
	 * List of paths to load views from
	 * 的路径列表加载意见
	 * @var array
	 * @access protected
	 */
	protected $_ci_view_paths		= array();
	
	
	/**
	 * List of paths to load libraries from
	 * 加载库的路径列表
	 * @var array
	 * @access protected
	 */
	protected $_ci_library_paths	= array();
	
	
	/**
	 * List of paths to load models from
	 * 加载模型的路径列表
	 * @var array
	 * @access protected
	 */
	protected $_ci_model_paths		= array();
	
	
	/**
	 * List of paths to load helpers from
	 * 加载佣工的路径列表
	 * @var array
	 * @access protected
	 */
	protected $_ci_helper_paths		= array();
	
	
	/**
	 * List of loaded base classes
	 * Set by the controller class
	 * 列表的加载基类
	 * 设置控制器类
	 *
	 * @var array
	 * @access protected
	 */
	protected $_base_classes		= array(); // Set by the controller class
	
	
	/**
	 * List of cached variables
	 * 缓存变量列表
	 * @var array
	 * @access protected
	 */
	protected $_ci_cached_vars		= array();
	
	
	/**
	 * List of loaded classes
	 * 加载的类列表
	 * @var array
	 * @access protected
	 */
	protected $_ci_classes			= array();
	
	
	/**
	 * List of loaded files
	 * 加载的文件列表
	 * @var array
	 * @access protected
	 */
	protected $_ci_loaded_files		= array();
	
	
	/**
	 * List of loaded models
	 * 装车型一览
	 * @var array
	 * @access protected
	 */
	protected $_ci_models			= array();
	
	
	/**
	 * List of loaded helpers
	 * 加载佣工名单
	 * @var array
	 * @access protected
	 */
	protected $_ci_helpers			= array();
	
	
	/**
	 * List of class name mappings
	 * 类名映射列表
	 * @var array
	 * @access protected
	 */
	protected $_ci_varmap			= array('unit_test' => 'unit',
											'user_agent' => 'agent');

	/**
	 * Constructor
	 *
	 * Sets the path to the view files and gets the initial output buffering level
	 * 设置视图文件的路径,并获得初始的输出缓冲级
	 */
	public function __construct()
	{
		//说明 int ob_get_level ( void ) 返回输出缓冲机制的嵌套级别。
		$this->_ci_ob_level  = ob_get_level();
		//项目目录 以及系统 目录 
		$this->_ci_library_paths = array(APPPATH, BASEPATH);
		//helper有项目目录和系统目录 
		$this->_ci_helper_paths = array(APPPATH, BASEPATH);
		
		//model只有项目目录
		$this->_ci_model_paths = array(APPPATH);
		
		//view只有项目目录
		$this->_ci_view_paths = array(APPPATH.'views/'	=> TRUE);

		log_message('debug', "Loader Class Initialized 装载机类初始化");
	}

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

	/**
	 * Initialize the Loader
	 * 初始化装载机
	 * This method is called once in CI_Controller.
	 * 这种方法被称为一次在CI_Controller。
	 * @param 	array
	 * @return 	object
	 */
	public function initialize()
	{
		//_ci_classes为空数组
		$this->_ci_classes = array();
		//加载文件为空数组
		$this->_ci_loaded_files = array();
		//加载的model为空数组
		$this->_ci_models = array();
		
		//设置需要加载的类信息
		$this->_base_classes =& is_loaded();

		//自动加载磁带机
		$this->_ci_autoloader();

		//返回对象自己
		return $this;
	}

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

	/**
	 * Is Loaded
	 *
	 * A utility function to test if a class is in the self::$_ci_classes array.
	 * This function returns the object name if the class tested for is loaded,
	 * and returns FALSE if it isn't.
	 *
	 * 实用功能测试,如果一类是在自我::$_ci_classes阵列。
	 * 这个函数返回对象的名称,如果类加载测试,
	 * 如果不是,则返回FALSE。
	 * It is mainly used in the form_helper -> _get_validation_object()
	 *
	 * @param 	string	class being checked for
	 * @return 	mixed	class object name on the CI SuperObject or FALSE
	 */
	//判断指定的类是否已经加载
	public function is_loaded($class)
	{
		//判断在_ci_classes数组对象中,类是否已经存在
		if (isset($this->_ci_classes[$class]))
		{
			return $this->_ci_classes[$class];
		}
        //不存在返回false
		return FALSE;
	}

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

	/**
	 * Class Loader
	 *
	 * This function lets users load and instantiate classes.
	 * It is designed to be called from a user's app controllers.
	 * 此功能允许用户装载并实例化类。
	 * 它的目的是从用户的应用程序的控制器被称为。
	 * @param	string	the name of the class
	 * @param	mixed	the optional parameters
	 * @param	string	an optional object name
	 * @return	void
	 */
	//类的加载
	public function library($library = '', $params = NULL, $object_name = NULL)
	{
		//如果$library是数组
		if (is_array($library))
		{
			//循环
			foreach ($library as $class)
			{
				//加载每一个类,还有叁数值哦
				$this->library($class, $params);
			}

			return;
		}

		//如果$library为空或者在_base_classes中不存在的话,直接返回空值
		if ($library == '' OR isset($this->_base_classes[$library]))
		{
			return FALSE;
		}

		//如果$params 不为null,并且 不是数组的话,那么设置为空值
		if ( ! is_null($params) && ! is_array($params))
		{
			$params = NULL;
		}

		//开始加载class类
		//$library是类的名称,$params 是参数,$object_name是对象的名称
		$this->_ci_load_class($library, $params, $object_name);
	}

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

	/**
	 * Model Loader
	 * Model类的加载
	 * 
	 * This function lets users load and instantiate models.
	 * 此功能允许用户加载和实例模型
	 * 
	 * @param	string	the name of the class
	 * @param	string	name for the model
	 * @param	bool	database connection
	 * @return	void
	 */
	//$model model类的class名称
	//$name 加载后model类的名称
	//$db_conn 数据库链接
	public function model($model, $name = '', $db_conn = FALSE)
	{
		//如果为数组
		if (is_array($model))
		{
			//循环加载所有元素
			foreach ($model as $babe)
			{
				$this->model($babe);
			}
			return; //然后返回
		}

		//如果为空值,直接返回
		if ($model == '')
		{
			return;
		}

		//定义$path变量
		$path = '';

		
		// Is the model in a sub-folder? If so, parse out the filename and path.
		//$last_slash = strtpos($model,'/') !== false
		//如果$model有/字符串在中
		if (($last_slash = strrpos($model, '/')) !== FALSE)
		{
			// The path is in front of the last slash
			//取得/前面的字符,也就是model的路径名称
			$path = substr($model, 0, $last_slash + 1);

			// And the model name behind it
			//取得model的名称
			$model = substr($model, $last_slash + 1);
		}

		//如果没有自定义name那么name就用$model所定义的名称
		if ($name == '')
		{
			$name = $model;
		}

		//如果该$name已经在$this->_ci_models中存在,直接返回空值
		if (in_array($name, $this->_ci_models, TRUE))
		{
			return;
		}

		//取得超级的CI控制器类
		$CI =& get_instance();
		//
		if (isset($CI->$name))
		{
			show_error('The model name you are loading is the name of a resource that is already being used: 加载的型号名称是资源的名称已被使用 '.$name);
		}
       
		//变为小写
		$model = strtolower($model);

		//开始循环加载所有_ci_model_paths信息
		foreach ($this->_ci_model_paths as $mod_path)
		{
			//判断指定的models类是否存在
			if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
			{
				continue;
			}

			//如果$db_conn不为空,而且CI_DB类不为空
			if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
			{
				//如果为true直接将其设置为空字符串
				if ($db_conn === TRUE)
				{
					$db_conn = '';
				}

				//然后加载databases这个类
				$CI->load->database($db_conn, FALSE, TRUE);
			}

			//判断CI_Model如果不存在
			if ( ! class_exists('CI_Model'))
			{
				//加载core目录下面的Model
				load_class('Model', 'core');
			}

			//开始包含指定的model文件
			require_once($mod_path.'models/'.$path.$model.'.php');

			$model = ucfirst($model);//ucfirst将字符串的第一个字符大写

			//new 一个model对象
			$CI->$name = new $model();

			//然后将其name存放入_ci_models数组中
			$this->_ci_models[] = $name;
			return;
		}

		// couldn't find the model
		show_error('Unable to locate the model you have specified: 找不到您所指定的模型'.$model);
	}

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

	/**
	 * Database Loader
	 * 数据库装载机
	 * 
	 * @param	string	the DB credentials DB凭据
	 * @param	bool	whether to return the DB object 是否返回DB对象
	 * @param	bool	whether to enable active record (this allows us to override the config setting)
	 * 是否启用活动记录(这使我们能够覆盖配置设置)
	 * @return	object
	 */
	public function database($params = '', $return = FALSE, $active_record = NULL)
	{
		// Grab the super object 抓住超级对象
		$CI =& get_instance();

		// Do we even need to load the database class?
		//我们甚至需要加载数据库类?
		
		//这里有很多条件同时来判断我们是否需要加载数据库类
		//CI_DB如果已经存
		//不用返回DB对象
		//不用启用活动记录
		//$CI->db已经存在
		//并且$CI->db已经是个对象
		if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db))
		{
			return FALSE;
		}

		//加载系统下面的databsase/DB.php文件
		require_once(BASEPATH.'database/DB.php');

		//如果需要返回DB对象
		if ($return === TRUE)
		{
			//这里直接返回
			return DB($params, $active_record);
		}

		// Initialize the db variable.  Needed to prevent
		// reference errors with some configurations
		// 初始化DB变量。需要防止
		// 引用错误与某些配置
		$CI->db = ''; //先将以前的$CI->db设置为空值

		// Load the DB class
		//加载DB类
		$CI->db =& DB($params, $active_record);
	}

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

	/**
	 * Load the Utilities Class
	 * 装入实用程序类
	 * @return	string
	 */
	public function dbutil()
	{
		//如果CI_DB不存在,直接国载
		if ( ! class_exists('CI_DB'))
		{
			$this->database();
		}

		//抓取超级类
		$CI =& get_instance();

		// for backwards compatibility, load dbforge so we can extend dbutils off it
		// this use is deprecated and strongly discouraged
		// 向后兼容性,加载dbforge关闭它,所以我们可以扩展dbutils
		// 这已被弃用,极力劝阻
		$CI->load->dbforge();

		//加载系统下面的database/DB_tuility.php
		require_once(BASEPATH.'database/DB_utility.php');
		//根据数据库类型加载指定数据库类型的utility.php
		require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
		$class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
		//class的名称

		$CI->dbutil = new $class(); //实例化
	}

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

	/**
	 * Load the Database Forge Class
	 * 装入数据库福尔类
	 * 
	 * @return	string
	 */
	public function dbforge()
	{
		//CI_DB是否存在
		if ( ! class_exists('CI_DB'))
		{
			$this->database();
		}

		//超级类
		$CI =& get_instance();

		require_once(BASEPATH.'database/DB_forge.php');
		require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
		$class = 'CI_DB_'.$CI->db->dbdriver.'_forge';

		$CI->dbforge = new $class();
	}

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

	/**
	 * Load View
	 * 负载查看
	 * This function is used to load a "view" file.  It has three parameters:
	 * 这个函数是用来加载一个“意见”文件。它有三个参数:
	 * 
	 * 1. The name of the "view" file to be included.
	 * 2. An associative array of data to be extracted for use in the view.
	 * 3. TRUE/FALSE - whether to return the data or load it.  In
	 * some cases it's advantageous to be able to return data so that
	 * a developer can process it in some way.
	 * 1。被列入“查看”文件的名称。
	 * 2。用于在视图中要提取的数据的关联数组。
	 * 3。 TRUE/ FALSE - 无论是将数据返回或加载它。在
	 * 某些情况下,它的优势是能够返回的数据,以便开发人员可以以某种方式处理。

	 * @param	string
	 * @param	array
	 * @param	bool
	 * @return	void
	 */
	public function view($view, $vars = array(), $return = FALSE)
	{
	    //_ci_load()函数,是直接进行输出操作的,要么返回输出结果,要么直接从缓存中输出
		return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
	}

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

	/**
	 * Load File
	 *
	 * This is a generic file loader
	 * 这是一个通用的文件加载
	 * @param	string
	 * @param	bool
	 * @return	string
	 */
	public function file($path, $return = FALSE)
	{
		return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
	}

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

	/**
	 * Set Variables
	 * 设置变量
	 * 
	 * Once variables are set they become available within
	 * the controller class and its "view" files.
	 * 一旦变量被设置成为内控制器类和它的“意见”文件。
	 * @param	array
	 * @param 	string
	 * @return	void
	 */
	public function vars($vars = array(), $val = '')
	{
		//如果$val为空,然后$vars是字符串的话,那么组合成一个数组键 值对
		if ($val != '' AND is_string($vars))
		{
			$vars = array($vars => $val);
		}

		//转换为数组
		$vars = $this->_ci_object_to_array($vars);

		//如果$vars为数组,
		if (is_array($vars) AND count($vars) > 0)
		{
			foreach ($vars as $key => $val)
			{
				//放入到缓存变量列表中去
				$this->_ci_cached_vars[$key] = $val;
			}
		}
	}

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

	/**
	 * Get Variable
	 * 获取变量
	 * Check if a variable is set and retrieve it.
	 * 检查如果一个变量设置和检索。
	 * 
	 * @param	array
	 * @return	void
	 */
	public function get_var($key)
	{
		return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
	}

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

	/**
	 * Load Helper
	 *
	 * This function loads the specified helper file.
	 * 此函数加载指定的辅助文件。
	 * 
	 * @param	mixed
	 * @return	void
	 */
	public function helper($helpers = array())
	{
		//helpers是指文件名,'_helper'是指扩展后缀
		foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
		{
			if (isset($this->_ci_helpers[$helper]))
			{
				continue;
			}
            //取得helpers的全部路径信息
            //subclass_prefix 类扩展前缀  
			$ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';

			
			// Is this a helper extension request? 这是一个辅助扩展的要求吗?
			if (file_exists($ext_helper)) 
			{
				//helper的基类
				$base_helper = BASEPATH.'helpers/'.$helper.'.php';

				if ( ! file_exists($base_helper))
				{
					show_error('Unable to load the requested file: helpers 无法加载所需的文件:佣工/'.$helper.'.php');
				}

				include_once($ext_helper); //加载helper
				include_once($base_helper);//加载base_helper,应该helper的基类

				//在_ci_helpers[$helper]设置为true
				$this->_ci_helpers[$helper] = TRUE;
				
				log_message('debug', 'Helper loaded: 加载助手:'.$helper);
				continue; //跳向下一次循环
			}

			// Try to load the helper
			// 尝试加载帮手
			foreach ($this->_ci_helper_paths as $path)
			{
				if (file_exists($path.'helpers/'.$helper.'.php'))
				{
					include_once($path.'helpers/'.$helper.'.php');

					$this->_ci_helpers[$helper] = TRUE;
					log_message('debug', 'Helper loaded: '.$helper);
					break;
				}
			}

			// unable to load the helper
			// 无法加载帮手
			if ( ! isset($this->_ci_helpers[$helper]))
			{
				show_error('Unable to load the requested file: helpers 能够加载所需的文件:佣工/'.$helper.'.php');
			}
		}
	}

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

	/**
	 * Load Helpers
	 *
	 * This is simply an alias to the above function in case the
	 * user has written the plural form of this function.
	 * 这是一个单纯的别名上述功能的情况下,
	 * 用户写这个函数的复数形式。
	 * @param	array
	 * @return	void
	 */
	public function helpers($helpers = array())
	{
		$this->helper($helpers);
	}

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

	/**
	 * Loads a language file
	 * 加载语言文件
	 * 
	 * @param	array
	 * @param	string
	 * @return	void
	 */
	public function language($file = array(), $lang = '')
	{
		//超级类
		$CI =& get_instance();

		//如果不为数组,需要转换为数组
		if ( ! is_array($file))
		{
			$file = array($file);
		}

		//开始循环加载语言文件
		foreach ($file as $langfile)
		{
			$CI->lang->load($langfile, $lang);
		}
	}
	

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

	/**
	 * Loads a config file
	 * 加载配置文件
	 * @param	string
	 * @param	bool
	 * @param 	bool
	 * @return	void
	 */
	public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
	{
		$CI =& get_instance();
		$CI->config->load($file, $use_sections, $fail_gracefully);
	}

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

	/**
	 * Driver
	 * 司机
	 * Loads a driver library
	 * 加载驱动程序库
	 * 
	 * @param	string	the name of the class    的类名
	 * @param	mixed	the optional parameters  可选参数
	 * @param	string	an optional object name  一个可选的对象名称
	 * @return	void
	 */
	public function driver($library = '', $params = NULL, $object_name = NULL)
	{
		if ( ! class_exists('CI_Driver_Library'))
		{
			// we aren't instantiating an object here, that'll be done by the Library itself
			// 我们不是在这里实例化一个对象,那将图书馆本身
			require BASEPATH.'libraries/Driver.php';
		}

		if ($library == '')
		{
			return FALSE;
		}

		// We can save the loader some time since Drivers will *always* be in a subfolder,
		// 我们可以节省一些时间,因为装载机司机将总是在一个子文件夹,		
		// and typically identically named to the library
		// 通常相同的命名图书馆
		// 如果$library没有/字符的话,那么将重新组合$library
		if ( ! strpos($library, '/'))
		{
			$library = ucfirst($library).'/'.$library;
		}

		//加载类
		return $this->library($library, $params, $object_name);
	}

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

	/**
	 * Add Package Path
	 * 添加包路径
	 * Prepends a parent path to the library, model, helper, and config path arrays
	 * 前置一个父路径库,模型,辅助,配置路径阵列
	 * @param	string
	 * @param 	boolean
	 * @return	void
	 */
	public function add_package_path($path, $view_cascade=TRUE)
	{
		//去掉/然后再加上
		$path = rtrim($path, '/').'/';

		//在库的路径列表中删除$path
		array_unshift($this->_ci_library_paths, $path);
		//在model的路径列表中删除$path
		array_unshift($this->_ci_model_paths, $path);
		//在helper的路径表中删除$path
		array_unshift($this->_ci_helper_paths, $path);
		
		 
        //设置_ci_view_paths的目录 
        //$path.view/等行view的缓存加上$_ci_view_paths;
		$this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;

		// Add config file path
		// 添加配置文件路径
		$config =& $this->_ci_get_component('config');
		//$config的config路径数组中删除path目录 
		array_unshift($config->_config_paths, $path);
	}

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

	/**
	 * Get Package Paths
	 * 包路径
	 * 
	 * Return a list of all package paths, by default it will ignore BASEPATH.
	 * 所有包路径返回一个列表,默认情况下它会忽略BASEPATH的。
	 * 
	 * @param	string
	 * @return	void
	 */
	public function get_package_paths($include_base = FALSE)
	{
		//如果为true返回所有library路径数组,否则显示model路径数组
		return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
	}

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

	/**
	 * Remove Package Path
	 * 删除包路径
	 * 
	 * Remove a path from the library, model, and helper path arrays if it exists
	 * If no path is provided, the most recently added path is removed.
	 * 删除路径从图书馆,型号,和助手路径阵列,如果它存在
	 * 如果没有提供路径,最近添加的路径被删除。
	 * @param	type
	 * @param 	bool
	 * @return	type
	 */
	public function remove_package_path($path = '', $remove_config_path = TRUE)
	{
		$config =& $this->_ci_get_component('config');

		if ($path == '')
		{
			$void = array_shift($this->_ci_library_paths);
			$void = array_shift($this->_ci_model_paths);
			$void = array_shift($this->_ci_helper_paths);
			$void = array_shift($this->_ci_view_paths);
			$void = array_shift($config->_config_paths);
		}
		else
		{
			$path = rtrim($path, '/').'/';
			//去掉路径
			foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
			{
				//$key = array_search($path, $this->{$var}) != false
				if (($key = array_search($path, $this->{$var})) !== FALSE)
				{
					//删除 $this->{$var}[$key]
					unset($this->{$var}[$key]);
				}
			}

			//查看在view里面是否存存该目录 
			if (isset($this->_ci_view_paths[$path.'views/']))
			{
				unset($this->_ci_view_paths[$path.'views/']);
			}

			//在config_paths里面是否存在该目录
			if (($key = array_search($path, $config->_config_paths)) !== FALSE)
			{
				unset($config->_config_paths[$key]);
			}
		}

		// make sure the application default paths are still in the array
		// 确保应用程序的默认路径仍然在数组中
		//array_merge 合并两个以上的数组
		// array_unique  从数组中移出相同的值
		$this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
		$this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
		$this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
		$this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
		$config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
	}

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

	/**
	 * Loader
	 *
	 * This function is used to load views and files.
	 * Variables are prefixed with _ci_ to avoid symbol collision with
	 * variables made available to view files
	 *
	 * 此功能是用来加载意见和文件。
	 * 变量都带有前缀_ci_避免符号​​碰撞变量以查看文件
	 * @param	array
	 * @return	void
	 */
	protected function _ci_load($_ci_data)
	{
		// Set the default data variables 默认数据变量
		foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
		{
			//$$_ci_val 相当于$_ci_vaie $_ci_vars $_ci_path $_ci_return
			//如果在$_ci_data存在这几个常量值时,设置为$_ci_data传过来的值,否则设置为false
			$$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
		}

		$file_exists = FALSE;

		// Set the path to the requested file
		// 设置到所需的文件的路径
		if ($_ci_path != '')
		{
			//如果$_ci_path不为空值,
			//分割字符串 
			//将最后一个字符数组元素赋值给$_ci_file;
			$_ci_x = explode('/', $_ci_path);
			$_ci_file = end($_ci_x);
		}
		else
		{
			// define ('PATHINFO_EXTENSION', 4);
			// PATHINFO_EXTENSION - 只返回 extension
			//只反回路径的一个扩展名
			$_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
			
			//如果等于空,直接用.php
			$_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
			
            //开始循环view_paths数组 
			foreach ($this->_ci_view_paths as $view_file => $cascade)
			{
				//判断该文件是否存在
				if (file_exists($view_file.$_ci_file))
				{
					//设置为$_ci_path路径,$file_exists为true 然后返回
					$_ci_path = $view_file.$_ci_file;
					$file_exists = TRUE;
					break;
				}

				//如果没有缓存,直接返回
				if ( ! $cascade)
				{
					break;
				}
			}
		}

		//如果文件不存,并且 $_ci_path路径也不存在
		if ( ! $file_exists && ! file_exists($_ci_path))
		{
			show_error('Unable to load the requested file: 无法加载所需的文件'.$_ci_file);
		}

		// This allows anything loaded using $this->load (views, files, etc.)
		// to become accessible from within the Controller and Model functions.
		// 这允许任何加载使用 - >负载(视图,文件等)
		// 从控制器和模型功能成为访问。
		$_ci_CI =& get_instance(); //加载超级CI控制器类
		//get_object_vars() 返回由对象属性组成的关联数组
		foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
		{
			//如果该$_ci_key值不存在的话,那么
			if ( ! isset($this->$_ci_key))
			{
				//设置该 _ci_key值,方法是用引用调用$_ci_CI->$_ci_key;
				$this->$_ci_key =& $_ci_CI->$_ci_key;
			}
		}

		/*
		 * Extract and cache variables
		 * 提取和缓存变量
		 * 
		 * You can either set variables using the dedicated $this->load_vars()
		 * function or via the second parameter of this function. We'll merge
		 * the two types and cache them so that views that are embedded within
		 * other views can have access to these variables.
		 * 
		 * 您可以设置使用专用> load_vars(变量)
		 * 函数或通过在此函数中的第二个参数。我们将合并
		 * 两种类型并将其缓存的意见,内嵌入其他意见,可以对这些变量的访问。
		 * 
		 * 
		 */
		//将$_ci_vars合并到缓存变量中去
		if (is_array($_ci_vars))
		{
			$this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
		}
		extract($this->_ci_cached_vars);
		//extract() 从数组中输出变量到符号列表中

		/*
		 * Buffer the output
		 * 缓冲输出
		 * 
		 * We buffer the output for two reasons:
		 * 1. Speed. You get a significant speed boost.
		 * 2. So that the final rendered template can be
		 * post-processed by the output class.  Why do we
		 * need post processing?  For one thing, in order to
		 * show the elapsed page load time.  Unless we
		 * can intercept the content right before it's sent to
		 * the browser and then stop the timer it won't be accurate.
		 * 
		 * *缓冲输出,有两个原因:
		 * 1。速度。你得到了显着的速度提升。
		 * 2。所以,最终呈现的模板可以是
		 * 后期处理的输出类。为什么我们
		 * 需要后期处理?一方面,为了
		 * 显示经过的页面加载时间。除非我们
		 * 可以拦截内容的权利,发出之前,
		 * 浏览器,然后停止计时器,它不会是准确的。
		 */
		ob_start(); //打开缓冲区

		// If the PHP installation does not support short tags we'll
		// do a little string replacement, changing the short tags
		// to standard PHP echo statements.
		// 如果PHP安装不支持短标记,我们会做一个小的字符串替换,
		// 改变短标签标准的PHP的echo语句。
		
		//short_open_tag是否在phpinfo中打开了短标答
		//rewrite_short_tags如果在系统不支持短标签时,可以用CI自行开启短标签设置
		if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
		{
			//下面进行直接老输出
			
			//str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))
			//将所有$_ci_path里面的内容都进行替换操作,也就是<?= 替换成<?php echo 
			
			//然后还需要进行一个正则的替换

			echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
		}
		else
		{
			//允许具有相同名称的多个视图
			include($_ci_path); // include() vs include_once() allows for multiple views with the same name
		}

		log_message('debug', 'File loaded: 加载的文件 '.$_ci_path);

		// Return the file data if requested
		// 如果要求返回文件数据
		if ($_ci_return === TRUE)
		{
			$buffer = ob_get_contents(); //ob_get_contents() 保存缓冲数据
			@ob_end_clean();  //关闭缓冲区
			return $buffer;   //返回数据
		}

		/*
		 * Flush the buffer... or buff the flusher?
		 * 刷新缓冲区...或浅黄色的冲水?
		 * 
		 * In order to permit views to be nested within
		 * other views, we need to flush the content back out whenever
		 * we are beyond the first level of output buffering so that
		 * it can be seen and included properly by the first included
		 * template and any subsequent ones. Oy!
		 * 为了允许意见嵌套在
		 * 其他意见,我们需要冲洗的内容,回时
		 * 我们是超越第一级的输出缓冲,所以,
		 * 可以看出,适当地包括由第一
		 * 模板和任何后续的。 Oy公司!
		 */
		//ob_get_level() 错误级别,大小_ci_ob_level+1的话
		if (ob_get_level() > $this->_ci_ob_level + 1)
		{
			ob_end_flush(); //ob_end_flush 清除输出缓冲并且关闭输出缓冲区
		}
		else
		{
			//添加到output的append_output里面去
			$_ci_CI->output->append_output(ob_get_contents());
			@ob_end_clean(); //关闭缓冲区
		}
	}

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

	/**
	 * Load class
	 * 加载class类
	 * 
	 * This function loads the requested class.
	 * 此功能加载所请求的类。
	 * 
	 * @param	string	the item that is being loaded  正在装入的产品
	 * @param	mixed	any additional parameters      任何额外的参数
	 * @param	string	an optional object name        一个可选的对象名称
	 * @return	void
	 */
	protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
	{
		// Get the class name, and while we're at it trim any slashes.
		// The directory path can be included as part of the class name,
		// but we don't want a leading slash
		
		// 获取类的名字,虽然我们在修剪任何斜线。的目录路径可以包含类名的一部分,
		// 但我们不希望领先的斜线
		
		$class = str_replace('.php', '', trim($class, '/'));

		// Was the path included with the class name?
		// We look for a slash to determine this
		// 路径的类名?
		// 我们期待一个斜线确定此
		$subdir = '';
		//取最后的斜线
		if (($last_slash = strrpos($class, '/')) !== FALSE)
		{
			// Extract the path
			//路径名
			$subdir = substr($class, 0, $last_slash + 1);

			// Get the filename from the path
			// 类名
			$class = substr($class, $last_slash + 1);
		}

		// We'll test for both lowercase and capitalized versions of the file name
		//我们将测试的文件名小写和大写两种版本
		foreach (array(ucfirst($class), strtolower($class)) as $class)
		{
			//subclass的整个路径
			$subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';

			// Is this a class extension request?
			// 这是一类扩展的要求吗? 文件是否存在
			if (file_exists($subclass))
			{
				//取得在系统的libraries上面将$class首字母大写的一个.php文件
				$baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';

				//如果该文件不存在
				if ( ! file_exists($baseclass))
				{
					log_message('error', "Unable to load the requested class 无法加载所请求的类: ".$class);
					show_error("Unable to load the requested class 无法加载所请求的类: ".$class);
				}

				// Safety:  Was the class already loaded by a previous call?
				// 安全:由先前调用类已经加载?
				//如果该类的已经在_ci_loader_files中存在
				if (in_array($subclass, $this->_ci_loaded_files))
				{
					// Before we deem this to be a duplicate request, let's see
					// if a custom object name is being supplied.  If so, we'll
					// return a new instance of the object
					// 之前,我们认为这是一个重复的请求,让我们来看看
					// 如果自定义对象的名称。如果是这样,我们会
					// 返回一个新的对象实例
					// 如果对象名称不为空,
					if ( ! is_null($object_name))
					{
						$CI =& get_instance();
					    //如果在超级类中这个对象名称不存在
						if ( ! isset($CI->$object_name))
						{
							//实例化一个类
							return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
						}
					}

					$is_duplicate = TRUE;
					log_message('debug', $class." class already loaded. Second attempt ignored. 已加载的类。第二次尝试被忽略");
					return;
				}

				include_once($baseclass);
				include_once($subclass);
				$this->_ci_loaded_files[] = $subclass;

				return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
			}

			// Lets search for the requested library file and load it.
			// 让搜索请求的库文件,并加载它。
			$is_duplicate = FALSE;
			foreach ($this->_ci_library_paths as $path)
			{
				//在库文件下面查找指定的类文件
				$filepath = $path.'libraries/'.$subdir.$class.'.php';

				// Does the file exist?  No?  Bummer...
				if ( ! file_exists($filepath)) 
				{
					continue;//不存在,直接跳出本次循环
				}

				// Safety:  Was the class already loaded by a previous call?
				// 安全:由先前调用类已经加载?
				if (in_array($filepath, $this->_ci_loaded_files))
				{
					// Before we deem this to be a duplicate request, let's see
					// if a custom object name is being supplied.  If so, we'll
					// return a new instance of the object
					if ( ! is_null($object_name))
					{
						$CI =& get_instance();
						if ( ! isset($CI->$object_name))
						{
							return $this->_ci_init_class($class, '', $params, $object_name);
						}
					}

					$is_duplicate = TRUE;
					log_message('debug', $class." class already loaded. Second attempt ignored.");
					return;
				}

				include_once($filepath);
				$this->_ci_loaded_files[] = $filepath;
				return $this->_ci_init_class($class, '', $params, $object_name);
			}

		} // END FOREACH

		// One last attempt.  Maybe the library is in a subdirectory, but it wasn't specified?
		// 最后一次尝试。图书馆也许是在一个子目录,但它没有指定?
		if ($subdir == '')
		{
			$path = strtolower($class).'/'.$class;
			return $this->_ci_load_class($path, $params);
		}

		// If we got this far we were unable to find the requested class.
		// We do not issue errors if the load call failed due to a duplicate request
		// 如果我们走了这么远,我们无法找到所请求的类。
		// 我们不发出错误如果负载调用失败,因为重复的请求
		if ($is_duplicate == FALSE)
		{
			log_message('error', "Unable to load the requested class: 无法加载所请求的类 ".$class);
			show_error("Unable to load the requested class:  无法加载所请求的类".$class);
		}
	}

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

	/**
	 * Instantiates a class
	 * 实例化一个类
	 * @param	string
	 * @param	string
	 * @param	bool
	 * @param	string	an optional object name  一个可选的对象名称
	 * @return	null
	 */
	protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
	{
		// Is there an associated config file for this class?  Note: these should always be lowercase
		// 这个类是否有相关的配置文件?注:这些应该总是​​小写。
		//如果没有配置文件
		if ($config === NULL)
		{
			// Fetch the config paths containing any package paths
			// 取配置路径包含任何包的路径
			$config_component = $this->_ci_get_component('config'); //相当于取$CI->config

			if (is_array($config_component->_config_paths))
			{
				// Break on the first found file, thus package files
				// are not overridden by default paths
				// 突破首次发现的文件,因此包文件
				// 是不是默认路径覆盖
				foreach ($config_component->_config_paths as $path)
				{
					// We test for both uppercase and lowercase, for servers that
					// are case-sensitive with regard to file names. Check for environment
					// first, global next
					//我们测试大写和小写,用于服务器
					//文件名是大小写敏感的。检查环境
					//首先,全球下
					
					if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
					{
						include($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
						break;
					}
					elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
					{
						include($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
						break;
					}
					elseif (file_exists($path .'config/'.strtolower($class).'.php'))
					{
						include($path .'config/'.strtolower($class).'.php');
						break;
					}
					elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).'.php'))
					{
						include($path .'config/'.ucfirst(strtolower($class)).'.php');
						break;
					}
				}
			}
		}

		//如果类的前缀为空
		if ($prefix == '')
		{
			//判断是否为CI_ClassName
			if (class_exists('CI_'.$class))
			{
				$name = 'CI_'.$class;
			}
			//是否为MY_ClassName
			elseif (class_exists(config_item('subclass_prefix').$class))
			{
				$name = config_item('subclass_prefix').$class;
			}
			else
			{
				$name = $class;
			}
		}
		else
		{
			$name = $prefix.$class;
		}

		// Is the class name valid?
		// 类名是否有效?
		if ( ! class_exists($name))
		{
			log_message('error', "Non-existent class: 不存在类 ".$name);
			show_error("Non-existent class: 不存在类 ".$class);
		}

		// Set the variable name we will assign the class to
		// Was a custom class name supplied?  If so we'll use it
		// 设置变量的名称,我们将类分配给
		// 提供一个自定义的类名?如果是这样,我们将用它
		$class = strtolower($class);

		//$this->_ci_varmap() 类名映射列表
		//看是否对该类进行了映射片时
		if (is_null($object_name))
		{
			$classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
		}
		else
		{
			$classvar = $object_name;
		}

		// Save the class name and object name
		// 保存的类名和对象名
		$this->_ci_classes[$class] = $classvar;

		// Instantiate the class 实例化的类
		$CI =& get_instance();
		if ($config !== NULL)
		{
			$CI->$classvar = new $name($config);
		}
		else
		{
			$CI->$classvar = new $name;
		}
	}

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

	/**
	 * Autoloader
	 * 自动加载磁带机
	 * The config/autoload.php file contains an array that permits sub-systems,
	 * libraries, and helpers to be loaded automatically.
	 *
	 * 的config/autoload.php的文件包含一个数组,允许子系统,库和佣工被自动加载。
	 * @param	array
	 * @return	void
	 */
	private function _ci_autoloader()
	{
		if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
		{
			include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
		}
		else
		{
			include(APPPATH.'config/autoload.php');
		}
		//config/autoload.php 设置初始化需要加载的一些类及方法或者其它信息等

		//如果$autoload变量为空的话,直接返回
		if ( ! isset($autoload))
		{
			return FALSE;
		}

		// Autoload packages
		// 自动加载包
		if (isset($autoload['packages']))
		{
			foreach ($autoload['packages'] as $package_path)
			{
				//添加到add_package_path中去
				$this->add_package_path($package_path);
			}
		}

		// Load any custom config file
		// 加载任何自定义的配置文件...
		if (count($autoload['config']) > 0)
		{
			$CI =& get_instance();
			foreach ($autoload['config'] as $key => $val)
			{
				$CI->config->load($val);
			}
		}

		// Autoload helpers and languages
		// 自动加载佣工和语言
		foreach (array('helper', 'language') as $type)
		{
			if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
			{
				$this->$type($autoload[$type]);
			}
		}

		// A little tweak to remain backward compatible
		// The $autoload['core'] item was deprecated
		// 一个小的调整,以保持向后兼容
		// 自动加载['核心']项目被废弃
		if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
		{
			$autoload['libraries'] = $autoload['core'];
		}

		// Load libraries 负载库
		if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
		{
			// Load the database driver.
			// 加载数据库驱动程序
			if (in_array('database', $autoload['libraries']))
			{
				$this->database();
				//array_diff计算数组的差值
				$autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
			}

			// Load all other libraries装入其它所有库
			foreach ($autoload['libraries'] as $item)
			{
				$this->library($item);
			}
		}

		// Autoload models
		// 自动加载模型
		if (isset($autoload['model']))
		{
			$this->model($autoload['model']);
		}
	}

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

	/**
	 * Object to Array
	 * 对象阵列
	 * Takes an object as input and converts the class variables to array key/vals
	 * 一个对象作为输入,并将其转换类变量数组的键/丘壑
	 * 
	 * @param	object
	 * @return	array
	 */
	protected function _ci_object_to_array($object)
	{
		//get_object_vars() 取得该类的所有属性返回数组
		return (is_object($object)) ? get_object_vars($object) : $object;
	}

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

	/**
	 * Get a reference to a specific library or model
	 * 参考到一个特定的库或模型
	 * 
	 * @param 	string
	 * @return	bool
	 */
	protected function &_ci_get_component($component)
	{
		$CI =& get_instance(); //取得超级控件器对象,然后取得该模型
		return $CI->$component;
	}

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

	/**
	 * Prep filename
	 * 准备文件名
	 * This function preps the name of various items to make loading them more reliable.
	 * 此功能PREPS各种项目,使加载它们更可靠的名称。
	 * @param	mixed
	 * @param 	string
	 * @return	array
	 */
	protected function _ci_prep_filename($filename, $extension)
	{
		//如果filename不为数组
		if ( ! is_array($filename))
		{
			//
			//$a = str_replace($extension, '', $filename) //在filename把后缀替换为空值
			//$a = str_replace('.php', '', $a); 将php替换为空值
			//return array(strtolower($a.$extension));
			return array(strtolower(str_replace('.php', '', str_replace($extension, '', $filename)).$extension));
		}
		else
		{
			foreach ($filename as $key => $val)
			{
				//处理方法一样,
				$filename[$key] = strtolower(str_replace('.php', '', str_replace($extension, '', $val)).$extension);
			}

			return $filename;
		}
	}
}

/* End of file Loader.php */
/* Location: ./system/core/Loader.php */

  

posted @ 2013-05-12 22:05  简单--生活  阅读(556)  评论(0)    收藏  举报
简单--生活(CSDN)