CI框架 -- 核心文件 之 Loader.php(加载器)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CI框架 -- 核心文件 之 Loader.php(加载器)相关的知识,希望对你有一定的参考价值。

顾名思义,装载器就是加载元素的,使用CI时,经常加载的有:

加载类库文件:$this->load->library()
 
加载视图文件:$this->load->view()
 
加载模型文件:$this->load->model()
 
加载数据库文件:$this->load->database()
 
加载帮助文件:$this->load->helper()
 
加载配置文件:$this->load->config()
 
加载包路径:$this->load->add_package_path()
 
源码:
技术分享
   1 /**
   2  * Loader Class
   3  *
   4  * 用户加载views和files,常见的函数有model(),view(),library(),helper()
   5  * 
   6  * Controller的好助手,$this->load =& load_class(‘Loader‘, ‘core‘);,加载了loader,Controller就无比强大了
   7  * @link http://www.phpddt.com
   8  */
   9 class CI_Loader {
  10  
  11     protected $_ci_ob_level;
  12         
  13     protected $_ci_view_paths        = array();
  14     protected $_ci_library_paths    = array();
  15     protected $_ci_model_paths        = array();
  16     protected $_ci_helper_paths        = array();
  17  
  18     protected $_base_classes        = array(); // Set by the controller class
  19  
  20     protected $_ci_cached_vars        = array();
  21  
  22     protected $_ci_classes            = array();
  23  
  24     protected $_ci_loaded_files        = array();
  25     
  26     protected $_ci_models            = array();
  27  
  28     protected $_ci_helpers            = array();
  29  
  30     protected $_ci_varmap            = array(‘unit_test‘ => ‘unit‘,
  31                                             ‘user_agent‘ => ‘agent‘);
  32  
  33     public function __construct()
  34     {       
  35                 //获取缓冲嵌套级别
  36         $this->_ci_ob_level  = ob_get_level();
  37         //library路径
  38                 $this->_ci_library_paths = array(APPPATH, BASEPATH);
  39                 //helper路径
  40         $this->_ci_helper_paths = array(APPPATH, BASEPATH);
  41                 //model路径
  42         $this->_ci_model_paths = array(APPPATH);
  43                 //view路径
  44         $this->_ci_view_paths = array(APPPATH.‘views/‘    => TRUE);
  45  
  46         log_message(‘debug‘, "Loader Class Initialized");
  47     }
  48  
  49     // --------------------------------------------------------------------
  50  
  51     /**
  52      * 初始化Loader
  53      *
  54      */
  55     public function initialize()
  56     {
  57         $this->_ci_classes = array();
  58         $this->_ci_loaded_files = array();
  59         $this->_ci_models = array();
  60                 //将is_loaded(common中记录加载核心类函数)加载的核心类交给_base_classes
  61         $this->_base_classes =& is_loaded();
  62                 
  63                 //加载autoload.php配置中文件
  64         $this->_ci_autoloader();
  65  
  66         return $this;
  67     }
  68  
  69     // --------------------------------------------------------------------
  70  
  71     /**
  72      * 检测类是否加载
  73      */
  74     public function is_loaded($class)
  75     {
  76         if (isset($this->_ci_classes[$class]))
  77         {
  78             return $this->_ci_classes[$class];
  79         }
  80  
  81         return FALSE;
  82     }
  83  
  84     // --------------------------------------------------------------------
  85  
  86     /**
  87      * 加载Class
  88      */
  89     public function library($library = ‘‘, $params = NULL, $object_name = NULL)
  90     {
  91         if (is_array($library))
  92         {
  93             foreach ($library as $class)
  94             {
  95                 $this->library($class, $params);
  96             }
  97  
  98             return;
  99         }
 100                 
 101                 //如果$library为空或者已经加载。。。
 102         if ($library == ‘‘ OR isset($this->_base_classes[$library]))
 103         {
 104             return FALSE;
 105         }
 106  
 107         if ( ! is_null($params) && ! is_array($params))
 108         {
 109             $params = NULL;
 110         }
 111  
 112         $this->_ci_load_class($library, $params, $object_name);
 113     }
 114  
 115     // --------------------------------------------------------------------
 116  
 117     /**
 118      * 加载和实例化model
 119      */
 120     public function model($model, $name = ‘‘, $db_conn = FALSE)
 121     {
 122                 //CI支持数组加载多个model
 123         if (is_array($model))
 124         {
 125             foreach ($model as $babe)
 126             {
 127                 $this->model($babe);
 128             }
 129             return;
 130         }
 131  
 132         if ($model == ‘‘)
 133         {
 134             return;
 135         }
 136  
 137         $path = ‘‘;
 138  
 139         // 是否存在子目录
 140         if (($last_slash = strrpos($model, ‘/‘)) !== FALSE)
 141         {
 142             // The path is in front of the last slash
 143             $path = substr($model, 0, $last_slash + 1);
 144  
 145             // And the model name behind it
 146             $model = substr($model, $last_slash + 1);
 147         }
 148  
 149         if ($name == ‘‘)
 150         {
 151             $name = $model;
 152         }
 153  
 154         if (in_array($name, $this->_ci_models, TRUE))
 155         {
 156             return;
 157         }
 158  
 159         $CI =& get_instance();
 160         if (isset($CI->$name))
 161         {
 162             show_error(‘The model name you are loading is the name of a resource that is already being used: ‘.$name);
 163         }
 164  
 165         $model = strtolower($model); //model文件名全小写
 166  
 167         foreach ($this->_ci_model_paths as $mod_path)
 168         {
 169             if ( ! file_exists($mod_path.‘models/‘.$path.$model.‘.php‘))
 170             {
 171                 continue;
 172             }
 173  
 174             if ($db_conn !== FALSE AND ! class_exists(‘CI_DB‘))
 175             {
 176                 if ($db_conn === TRUE)
 177                 {
 178                     $db_conn = ‘‘;
 179                 }
 180  
 181                 $CI->load->database($db_conn, FALSE, TRUE);
 182             }
 183  
 184             if ( ! class_exists(‘CI_Model‘))
 185             {
 186                 load_class(‘Model‘, ‘core‘);
 187             }
 188  
 189             require_once($mod_path.‘models/‘.$path.$model.‘.php‘);
 190  
 191             $model = ucfirst($model);
 192  
 193             $CI->$name = new $model();
 194                         //保存在Loader::_ci_models中,以后可以用它来判断某个model是否已经加载过。
 195             $this->_ci_models[] = $name;
 196             return;
 197         }
 198  
 199         // couldn‘t find the model
 200         show_error(‘Unable to locate the model you have specified: ‘.$model);
 201     }
 202  
 203     // --------------------------------------------------------------------
 204  
 205     /**
 206      * 数据库Loader
 207      */
 208     public function database($params = ‘‘, $return = FALSE, $active_record = NULL)
 209     {
 210         // Grab the super object
 211         $CI =& get_instance();
 212  
 213         // 是否需要加载db
 214         if (class_exists(‘CI_DB‘) AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db))
 215         {
 216             return FALSE;
 217         }
 218                 
 219         require_once(BASEPATH.‘database/DB.php‘);
 220                 
 221         if ($return === TRUE)
 222         {
 223             return DB($params, $active_record);
 224         }
 225  
 226         // Initialize the db variable.  Needed to prevent
 227         // reference errors with some configurations
 228         $CI->db = ‘‘;
 229  
 230         // Load the DB class
 231         $CI->db =& DB($params, $active_record);
 232     }
 233  
 234     // --------------------------------------------------------------------
 235  
 236     /**
 237      * 加载数据库工具类
 238      */
 239     public function dbutil()
 240     {
 241         if ( ! class_exists(‘CI_DB‘))
 242         {
 243             $this->database();
 244         }
 245  
 246         $CI =& get_instance();
 247  
 248         // for backwards compatibility, load dbforge so we can extend dbutils off it
 249         // this use is deprecated and strongly discouraged
 250         $CI->load->dbforge();
 251  
 252         require_once(BASEPATH.‘database/DB_utility.php‘);
 253         require_once(BASEPATH.‘database/drivers/‘.$CI->db->dbdriver.‘/‘.$CI->db->dbdriver.‘_utility.php‘);
 254         $class = ‘CI_DB_‘.$CI->db->dbdriver.‘_utility‘;
 255  
 256         $CI->dbutil = new $class();
 257     }
 258  
 259     // --------------------------------------------------------------------
 260  
 261     /**
 262      * Load the Database Forge Class
 263      *
 264      * @return    string
 265      */
 266     public function dbforge()
 267     {
 268         if ( ! class_exists(‘CI_DB‘))
 269         {
 270             $this->database();
 271         }
 272  
 273         $CI =& get_instance();
 274  
 275         require_once(BASEPATH.‘database/DB_forge.php‘);
 276         require_once(BASEPATH.‘database/drivers/‘.$CI->db->dbdriver.‘/‘.$CI->db->dbdriver.‘_forge.php‘);
 277         $class = ‘CI_DB_‘.$CI->db->dbdriver.‘_forge‘;
 278  
 279         $CI->dbforge = new $class();
 280     }
 281  
 282     // --------------------------------------------------------------------
 283  
 284     /**
 285      * 加载视图文件
 286      */
 287     public function view($view, $vars = array(), $return = FALSE)
 288     {
 289         return $this->_ci_load(array(‘_ci_view‘ => $view, ‘_ci_vars‘ => $this->_ci_object_to_array($vars), ‘_ci_return‘ => $return));
 290     }
 291  
 292     // --------------------------------------------------------------------
 293  
 294     /**
 295      * 加载普通文件
 296      */
 297     public function file($path, $return = FALSE)
 298     {
 299         return $this->_ci_load(array(‘_ci_path‘ => $path, ‘_ci_return‘ => $return));
 300     }
 301  
 302     // --------------------------------------------------------------------
 303  
 304     /**
 305      * 设置变量
 306      *
 307      * Once variables are set they become available within
 308      * the controller class and its "view" files.
 309      *
 310      */
 311     public function vars($vars = array(), $val = ‘‘)
 312     {
 313         if ($val != ‘‘ AND is_string($vars))
 314         {
 315             $vars = array($vars => $val);
 316         }
 317  
 318         $vars = $this->_ci_object_to_array($vars);
 319  
 320         if (is_array($vars) AND count($vars) > 0)
 321         {
 322             foreach ($vars as $key => $val)
 323             {
 324                 $this->_ci_cached_vars[$key] = $val;
 325             }
 326         }
 327     }
 328  
 329     // --------------------------------------------------------------------
 330  
 331     /**
 332      * 检查并获取变量
 333      */
 334     public function get_var($key)
 335     {
 336         return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
 337     }
 338  
 339     // --------------------------------------------------------------------
 340  
 341     /**
 342      * 加载helper
 343      */
 344     public function helper($helpers = array())
 345     {
 346         foreach ($this->_ci_prep_filename($helpers, ‘_helper‘) as $helper)
 347         {
 348             if (isset($this->_ci_helpers[$helper]))
 349             {
 350                 continue;
 351             }
 352  
 353             $ext_helper = APPPATH.‘helpers/‘.config_item(‘subclass_prefix‘).$helper.‘.php‘;
 354  
 355             // 如果是扩展helper的话
 356             if (file_exists($ext_helper))
 357             {
 358                 $base_helper = BASEPATH.‘helpers/‘.$helper.‘.php‘;
 359  
 360                 if ( ! file_exists($base_helper))
 361                 {
 362                     show_error(‘Unable to load the requested file: helpers/‘.$helper.‘.php‘);
 363                 }
 364  
 365                 include_once($ext_helper);
 366                 include_once($base_helper);
 367  
 368                 $this->_ci_helpers[$helper] = TRUE;
 369                 log_message(‘debug‘, ‘Helper loaded: ‘.$helper);
 370                 continue;
 371             }
 372  
 373             // 如果不是扩展helper,helper路径中加载helper
 374             foreach ($this->_ci_helper_paths as $path)
 375             {
 376                 if (file_exists($path.‘helpers/‘.$helper.‘.php‘))
 377                 {
 378                     include_once($path.‘helpers/‘.$helper.‘.php‘);
 379  
 380                     $this->_ci_helpers[$helper] = TRUE;
 381                     log_message(‘debug‘, ‘Helper loaded: ‘.$helper);
 382                     break;
 383                 }
 384             }
 385  
 386             // 如果该helper还没加载成功的话,说明加载helper失败
 387             if ( ! isset($this->_ci_helpers[$helper]))
 388             {
 389                 show_error(‘Unable to load the requested file: helpers/‘.$helper.‘.php‘);
 390             }
 391         }
 392     }
 393  
 394     // --------------------------------------------------------------------
 395  
 396     /**
 397      * 可以看到helpers调用也是上面的helper,只是helpers的别名而已
 398      */
 399     public function helpers($helpers = array())
 400     {
 401         $this->helper($helpers);
 402     }
 403  
 404     // --------------------------------------------------------------------
 405  
 406     /**
 407      * 加载language文件
 408      */
 409     public function language($file = array(), $lang = ‘‘)
 410     {
 411         $CI =& get_instance();
 412  
 413         if ( ! is_array($file))
 414         {
 415             $file = array($file);
 416         }
 417  
 418         foreach ($file as $langfile)
 419         {
 420             $CI->lang->load($langfile, $lang);
 421         }
 422     }
 423  
 424     // --------------------------------------------------------------------
 425  
 426     /**
 427      * 加载配置文件
 428      */
 429     public function config($file = ‘‘, $use_sections = FALSE, $fail_gracefully = FALSE)
 430     {
 431         $CI =& get_instance();
 432         $CI->config->load($file, $use_sections, $fail_gracefully);
 433     }
 434  
 435     // --------------------------------------------------------------------
 436  
 437     /**
 438      * Driver
 439      *
 440      * 加载 driver library
 441      */
 442     public function driver($library = ‘‘, $params = NULL, $object_name = NULL)
 443     {
 444         if ( ! class_exists(‘CI_Driver_Library‘))
 445         {
 446             // we aren‘t instantiating an object here, that‘ll be done by the Library itself
 447             require BASEPATH.‘libraries/Driver.php‘;
 448         }
 449  
 450         if ($library == ‘‘)
 451         {
 452             return FALSE;
 453         }
 454  
 455         // We can save the loader some time since Drivers will *always* be in a subfolder,
 456         // and typically identically named to the library
 457         if ( ! strpo

以上是关于CI框架 -- 核心文件 之 Loader.php(加载器)的主要内容,如果未能解决你的问题,请参考以下文章

CI框架 -- 核心文件 之 Lang.php(加载语言包)

CI框架 -- 核心文件 之 Input.php(输入数据处理文件)

CI框架中自定义view文件夹位置

CI框架视图继承

CI中的超级对象

ci框架基础之部署百度编辑器