blob: 689ae1ecd9921cf8dc58e8d7586c029f9f6fc160 [file] [log] [blame]
Andrey Andreevd7297352012-01-07 22:53:14 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevd7297352012-01-07 22:53:14 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevd7297352012-01-07 22:53:14 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Loader Class
32 *
33 * Loads views and files
34 *
35 * @package CodeIgniter
36 * @subpackage Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @category Loader
39 * @link http://codeigniter.com/user_guide/libraries/loader.html
40 */
41class CI_Loader {
42
43 // All these are set automatically. Don't mess with them.
David Behlercda768a2011-08-14 23:52:48 +020044 /**
45 * Nesting level of the output buffering mechanism
46 *
47 * @var int
David Behlercda768a2011-08-14 23:52:48 +020048 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050049 protected $_ci_ob_level;
David Behlercda768a2011-08-14 23:52:48 +020050 /**
51 * List of paths to load views from
52 *
53 * @var array
David Behlercda768a2011-08-14 23:52:48 +020054 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050055 protected $_ci_view_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020056 /**
57 * List of paths to load libraries from
58 *
59 * @var array
David Behlercda768a2011-08-14 23:52:48 +020060 */
Greg Akerf5c84022011-04-19 17:13:03 -050061 protected $_ci_library_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020062 /**
63 * List of paths to load models from
64 *
65 * @var array
David Behlercda768a2011-08-14 23:52:48 +020066 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050067 protected $_ci_model_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020068 /**
69 * List of paths to load helpers from
70 *
71 * @var array
David Behlercda768a2011-08-14 23:52:48 +020072 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050073 protected $_ci_helper_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020074 /**
75 * List of loaded base classes
David Behlercda768a2011-08-14 23:52:48 +020076 *
77 * @var array
David Behlercda768a2011-08-14 23:52:48 +020078 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050079 protected $_base_classes = array(); // Set by the controller class
David Behlercda768a2011-08-14 23:52:48 +020080 /**
81 * List of cached variables
82 *
83 * @var array
David Behlercda768a2011-08-14 23:52:48 +020084 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050085 protected $_ci_cached_vars = array();
David Behlercda768a2011-08-14 23:52:48 +020086 /**
87 * List of loaded classes
88 *
89 * @var array
David Behlercda768a2011-08-14 23:52:48 +020090 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050091 protected $_ci_classes = array();
David Behlercda768a2011-08-14 23:52:48 +020092 /**
93 * List of loaded files
94 *
95 * @var array
David Behlercda768a2011-08-14 23:52:48 +020096 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050097 protected $_ci_loaded_files = array();
David Behlercda768a2011-08-14 23:52:48 +020098 /**
99 * List of loaded models
100 *
101 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200102 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500103 protected $_ci_models = array();
David Behlercda768a2011-08-14 23:52:48 +0200104 /**
105 * List of loaded helpers
106 *
107 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200108 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500109 protected $_ci_helpers = array();
David Behlercda768a2011-08-14 23:52:48 +0200110 /**
111 * List of class name mappings
112 *
113 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200114 */
Andrey Andreevd7297352012-01-07 22:53:14 +0200115 protected $_ci_varmap = array(
116 'unit_test' => 'unit',
117 'user_agent' => 'agent'
118 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000119
120 /**
121 * Constructor
122 *
123 * Sets the path to the view files and gets the initial output buffering level
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 */
Greg Akerf5c84022011-04-19 17:13:03 -0500125 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +0200126 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500127 $this->_ci_ob_level = ob_get_level();
Derek Jones32bf1862010-03-02 13:46:07 -0600128 $this->_ci_library_paths = array(APPPATH, BASEPATH);
129 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
130 $this->_ci_model_paths = array(APPPATH);
Joe Cianflone8eef9c72011-08-21 10:39:06 -0400131 $this->_ci_view_paths = array(VIEWPATH => TRUE);
David Behlercda768a2011-08-14 23:52:48 +0200132
Andrey Andreevd7297352012-01-07 22:53:14 +0200133 log_message('debug', 'Loader Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200137
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500138 /**
Shane Pearson6adfe632011-08-10 16:42:53 -0500139 * Initialize the Loader
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500140 *
141 * This method is called once in CI_Controller.
142 *
David Behlercda768a2011-08-14 23:52:48 +0200143 * @param array
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500144 * @return object
145 */
Shane Pearson6adfe632011-08-10 16:42:53 -0500146 public function initialize()
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500147 {
Shane Pearson6adfe632011-08-10 16:42:53 -0500148 $this->_ci_classes = array();
149 $this->_ci_loaded_files = array();
150 $this->_ci_models = array();
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500151 $this->_base_classes =& is_loaded();
Shane Pearson6adfe632011-08-10 16:42:53 -0500152
153 $this->_ci_autoloader();
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500154 return $this;
155 }
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Is Loaded
161 *
162 * A utility function to test if a class is in the self::$_ci_classes array.
163 * This function returns the object name if the class tested for is loaded,
164 * and returns FALSE if it isn't.
165 *
166 * It is mainly used in the form_helper -> _get_validation_object()
167 *
168 * @param string class being checked for
169 * @return mixed class object name on the CI SuperObject or FALSE
170 */
171 public function is_loaded($class)
172 {
173 if (isset($this->_ci_classes[$class]))
174 {
175 return $this->_ci_classes[$class];
176 }
David Behlercda768a2011-08-14 23:52:48 +0200177
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500178 return FALSE;
179 }
180
181 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 /**
184 * Class Loader
185 *
186 * This function lets users load and instantiate classes.
187 * It is designed to be called from a user's app controllers.
188 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * @param string the name of the class
190 * @param mixed the optional parameters
191 * @param string an optional object name
192 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200193 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500194 public function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Greg Akerce433962010-10-12 09:29:35 -0500196 if (is_array($library))
197 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100198 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500199 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600200 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500201 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000202
Greg Akerce433962010-10-12 09:29:35 -0500203 return;
204 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000205
Derek Jones32bf1862010-03-02 13:46:07 -0600206 if ($library == '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
208 return FALSE;
209 }
210
Derek Jones32bf1862010-03-02 13:46:07 -0600211 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
213 $params = NULL;
214 }
215
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600216 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 }
218
219 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 /**
222 * Model Loader
223 *
224 * This function lets users load and instantiate models.
225 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * @param string the name of the class
227 * @param string name for the model
228 * @param bool database connection
229 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200230 */
Greg Akerf5c84022011-04-19 17:13:03 -0500231 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200232 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 if (is_array($model))
234 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500235 foreach ($model as $babe)
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Barry Mienydd671972010-10-04 16:33:58 +0200237 $this->model($babe);
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
239 return;
240 }
241
242 if ($model == '')
243 {
244 return;
245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Jones32bf1862010-03-02 13:46:07 -0600247 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600250 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
Derek Jones32bf1862010-03-02 13:46:07 -0600252 // The path is in front of the last slash
253 $path = substr($model, 0, $last_slash + 1);
254
255 // And the model name behind it
256 $model = substr($model, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
Barry Mienydd671972010-10-04 16:33:58 +0200258
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 if ($name == '')
260 {
261 $name = $model;
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 if (in_array($name, $this->_ci_models, TRUE))
265 {
266 return;
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 $CI =& get_instance();
270 if (isset($CI->$name))
271 {
272 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
273 }
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000276
Derek Jones32bf1862010-03-02 13:46:07 -0600277 foreach ($this->_ci_model_paths as $mod_path)
278 {
Greg Aker3a746652011-04-19 10:59:47 -0500279 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600280 {
281 continue;
282 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000283
Derek Jones32bf1862010-03-02 13:46:07 -0600284 if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
285 {
286 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500287 {
Derek Jones32bf1862010-03-02 13:46:07 -0600288 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500289 }
Derek Jones32bf1862010-03-02 13:46:07 -0600290
291 $CI->load->database($db_conn, FALSE, TRUE);
292 }
293
Greg Akerbce13482010-10-11 15:37:16 -0500294 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600295 {
296 load_class('Model', 'core');
297 }
298
Greg Aker3a746652011-04-19 10:59:47 -0500299 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600300
301 $model = ucfirst($model);
Derek Jones32bf1862010-03-02 13:46:07 -0600302 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600303 $this->_ci_models[] = $name;
304 return;
305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Jones32bf1862010-03-02 13:46:07 -0600307 // couldn't find the model
308 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 /**
314 * Database Loader
315 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 * @param string the DB credentials
317 * @param bool whether to return the DB object
318 * @param bool whether to enable active record (this allows us to override the config setting)
319 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200320 */
Greg Akerf5c84022011-04-19 17:13:03 -0500321 public function database($params = '', $return = FALSE, $active_record = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 // Grab the super object
324 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // Do we even need to load the database class?
Derek Jones9fb6dd12009-12-05 15:32:48 +0000327 if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db))
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200330 }
331
Greg Aker3a746652011-04-19 10:59:47 -0500332 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000333
334 if ($return === TRUE)
335 {
336 return DB($params, $active_record);
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Andrey Andreevd7297352012-01-07 22:53:14 +0200339 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 // reference errors with some configurations
341 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 // Load the DB class
Barry Mienydd671972010-10-04 16:33:58 +0200344 $CI->db =& DB($params, $active_record);
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 // --------------------------------------------------------------------
348
349 /**
350 * Load the Utilities Class
351 *
Barry Mienydd671972010-10-04 16:33:58 +0200352 * @return string
353 */
Greg Akerf5c84022011-04-19 17:13:03 -0500354 public function dbutil()
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
356 if ( ! class_exists('CI_DB'))
357 {
358 $this->database();
359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 $CI =& get_instance();
362
363 // for backwards compatibility, load dbforge so we can extend dbutils off it
364 // this use is deprecated and strongly discouraged
365 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200366
Greg Aker3a746652011-04-19 10:59:47 -0500367 require_once(BASEPATH.'database/DB_utility.php');
368 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
370
Pascal Kriete58560022010-11-10 16:01:20 -0500371 $CI->dbutil = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 // --------------------------------------------------------------------
375
376 /**
377 * Load the Database Forge Class
378 *
Barry Mienydd671972010-10-04 16:33:58 +0200379 * @return string
380 */
Greg Akerf5c84022011-04-19 17:13:03 -0500381 public function dbforge()
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
383 if ( ! class_exists('CI_DB'))
384 {
385 $this->database();
386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200389
Greg Aker3a746652011-04-19 10:59:47 -0500390 require_once(BASEPATH.'database/DB_forge.php');
391 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
393
394 $CI->dbforge = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 }
Barry Mienydd671972010-10-04 16:33:58 +0200396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 /**
400 * Load View
401 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500402 * This function is used to load a "view" file. It has three parameters:
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 *
404 * 1. The name of the "view" file to be included.
405 * 2. An associative array of data to be extracted for use in the view.
Derek Jones37f4b9c2011-07-01 17:56:50 -0500406 * 3. TRUE/FALSE - whether to return the data or load it. In
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 * some cases it's advantageous to be able to return data so that
408 * a developer can process it in some way.
409 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 * @param string
411 * @param array
412 * @param bool
413 * @return void
414 */
Greg Akerf5c84022011-04-19 17:13:03 -0500415 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
417 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 /**
423 * Load File
424 *
425 * This is a generic file loader
426 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 * @param string
428 * @param bool
429 * @return string
430 */
Greg Akerf5c84022011-04-19 17:13:03 -0500431 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
433 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 /**
439 * Set Variables
440 *
441 * Once variables are set they become available within
442 * the controller class and its "view" files.
443 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * @param array
David Behlercda768a2011-08-14 23:52:48 +0200445 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 * @return void
447 */
Greg Akerf5c84022011-04-19 17:13:03 -0500448 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
450 if ($val != '' AND is_string($vars))
451 {
452 $vars = array($vars => $val);
453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 if (is_array($vars) AND count($vars) > 0)
458 {
459 foreach ($vars as $key => $val)
460 {
461 $this->_ci_cached_vars[$key] = $val;
462 }
463 }
464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600469 * Get Variable
470 *
471 * Check if a variable is set and retrieve it.
472 *
473 * @param array
474 * @return void
475 */
476 public function get_var($key)
477 {
478 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
479 }
480
481 // --------------------------------------------------------------------
482
483 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600484 * Get Variables
485 *
486 * Retrieve all loaded variables
487 *
488 * @return array
489 */
490 public function get_vars()
491 {
492 return $this->_ci_cached_vars;
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 * Load Helper
499 *
500 * This function loads the specified helper file.
501 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 * @param mixed
503 * @return void
504 */
Greg Akerf5c84022011-04-19 17:13:03 -0500505 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200506 {
Derek Jones32bf1862010-03-02 13:46:07 -0600507 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200508 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 if (isset($this->_ci_helpers[$helper]))
510 {
511 continue;
512 }
Derek Jones32bf1862010-03-02 13:46:07 -0600513
Greg Aker3a746652011-04-19 10:59:47 -0500514 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000515
Barry Mienydd671972010-10-04 16:33:58 +0200516 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 if (file_exists($ext_helper))
518 {
Greg Aker3a746652011-04-19 10:59:47 -0500519 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 if ( ! file_exists($base_helper))
522 {
Greg Aker3a746652011-04-19 10:59:47 -0500523 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 include_once($ext_helper);
527 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Jones32bf1862010-03-02 13:46:07 -0600529 $this->_ci_helpers[$helper] = TRUE;
530 log_message('debug', 'Helper loaded: '.$helper);
531 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Jones32bf1862010-03-02 13:46:07 -0600534 // Try to load the helper
535 foreach ($this->_ci_helper_paths as $path)
536 {
Greg Aker3a746652011-04-19 10:59:47 -0500537 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200538 {
Greg Aker3a746652011-04-19 10:59:47 -0500539 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600540
541 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200542 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600543 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 }
545 }
546
Derek Jones32bf1862010-03-02 13:46:07 -0600547 // unable to load the helper
548 if ( ! isset($this->_ci_helpers[$helper]))
549 {
Greg Aker3a746652011-04-19 10:59:47 -0500550 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 /**
558 * Load Helpers
559 *
560 * This is simply an alias to the above function in case the
561 * user has written the plural form of this function.
562 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 * @param array
564 * @return void
565 */
Greg Akerf5c84022011-04-19 17:13:03 -0500566 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
568 $this->helper($helpers);
569 }
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 * Loads a language file
575 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 * @param array
577 * @param string
578 * @return void
579 */
Greg Akerf5c84022011-04-19 17:13:03 -0500580 public function language($file = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
582 $CI =& get_instance();
583
584 if ( ! is_array($file))
585 {
586 $file = array($file);
587 }
588
589 foreach ($file as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200590 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 $CI->lang->load($langfile, $lang);
592 }
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 /**
598 * Loads a config file
599 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200601 * @param bool
602 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 * @return void
604 */
Greg Akerf5c84022011-04-19 17:13:03 -0500605 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200606 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 $CI =& get_instance();
608 $CI->config->load($file, $use_sections, $fail_gracefully);
609 }
610
611 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 /**
Derek Jones8dca0412010-03-05 13:01:44 -0600614 * Driver
615 *
616 * Loads a driver library
617 *
618 * @param string the name of the class
619 * @param mixed the optional parameters
620 * @param string an optional object name
621 * @return void
622 */
Greg Akerf5c84022011-04-19 17:13:03 -0500623 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600624 {
625 if ( ! class_exists('CI_Driver_Library'))
626 {
627 // we aren't instantiating an object here, that'll be done by the Library itself
Greg Aker3a746652011-04-19 10:59:47 -0500628 require BASEPATH.'libraries/Driver.php';
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600629 }
Barry Mienydd671972010-10-04 16:33:58 +0200630
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200631 if ($library == '')
632 {
633 return FALSE;
634 }
635
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600636 // We can save the loader some time since Drivers will *always* be in a subfolder,
637 // and typically identically named to the library
638 if ( ! strpos($library, '/'))
639 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500640 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600641 }
Barry Mienydd671972010-10-04 16:33:58 +0200642
Derek Jones8dca0412010-03-05 13:01:44 -0600643 return $this->library($library, $params, $object_name);
644 }
645
646 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Jones8dca0412010-03-05 13:01:44 -0600648 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600649 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 *
Derek Jones32bf1862010-03-02 13:46:07 -0600651 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200654 * @param boolean
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600656 */
Greg Akerf5c84022011-04-19 17:13:03 -0500657 public function add_package_path($path, $view_cascade=TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600658 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500659 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200660
Derek Jones32bf1862010-03-02 13:46:07 -0600661 array_unshift($this->_ci_library_paths, $path);
662 array_unshift($this->_ci_model_paths, $path);
663 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200664
Greg Akerf5c84022011-04-19 17:13:03 -0500665 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
666
Derek Jones32bf1862010-03-02 13:46:07 -0600667 // Add config file path
668 $config =& $this->_ci_get_component('config');
669 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 }
671
672 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600673
674 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000675 * Get Package Paths
676 *
677 * Return a list of all package paths, by default it will ignore BASEPATH.
678 *
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000679 * @param string
680 * @return void
681 */
Greg Akerf5c84022011-04-19 17:13:03 -0500682 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000683 {
684 return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
685 }
686
687 // --------------------------------------------------------------------
688
689 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600690 * Remove Package Path
691 *
692 * Remove a path from the library, model, and helper path arrays if it exists
693 * If no path is provided, the most recently added path is removed.
694 *
Derek Jones32bf1862010-03-02 13:46:07 -0600695 * @param type
David Behlercda768a2011-08-14 23:52:48 +0200696 * @param bool
Derek Jones32bf1862010-03-02 13:46:07 -0600697 * @return type
698 */
Greg Akerf5c84022011-04-19 17:13:03 -0500699 public function remove_package_path($path = '', $remove_config_path = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600700 {
701 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200702
Derek Jones32bf1862010-03-02 13:46:07 -0600703 if ($path == '')
704 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200705 array_shift($this->_ci_library_paths);
706 array_shift($this->_ci_model_paths);
707 array_shift($this->_ci_helper_paths);
708 array_shift($this->_ci_view_paths);
709 array_shift($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600710 }
711 else
712 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500713 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600714 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
715 {
716 if (($key = array_search($path, $this->{$var})) !== FALSE)
717 {
718 unset($this->{$var}[$key]);
719 }
720 }
David Behlercda768a2011-08-14 23:52:48 +0200721
Greg Akerf5c84022011-04-19 17:13:03 -0500722 if (isset($this->_ci_view_paths[$path.'views/']))
723 {
724 unset($this->_ci_view_paths[$path.'views/']);
725 }
Barry Mienydd671972010-10-04 16:33:58 +0200726
Derek Jones32bf1862010-03-02 13:46:07 -0600727 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
728 {
729 unset($config->_config_paths[$key]);
730 }
731 }
Barry Mienydd671972010-10-04 16:33:58 +0200732
Derek Jones32bf1862010-03-02 13:46:07 -0600733 // make sure the application default paths are still in the array
734 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
735 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
736 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500737 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600738 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
739 }
740
741 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 /**
744 * Loader
745 *
746 * This function is used to load views and files.
747 * Variables are prefixed with _ci_ to avoid symbol collision with
748 * variables made available to view files
749 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 * @param array
751 * @return void
752 */
Greg Akerf5c84022011-04-19 17:13:03 -0500753 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
755 // Set the default data variables
756 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
757 {
758 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
759 }
David Behlercda768a2011-08-14 23:52:48 +0200760
Greg Akerf5c84022011-04-19 17:13:03 -0500761 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000762
763 // Set the path to the requested file
Greg Aker8807be32011-04-21 13:06:15 -0500764 if ($_ci_path != '')
765 {
766 $_ci_x = explode('/', $_ci_path);
767 $_ci_file = end($_ci_x);
768 }
769 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 {
771 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Greg Aker3a746652011-04-19 10:59:47 -0500772 $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500773
774 foreach ($this->_ci_view_paths as $view_file => $cascade)
775 {
776 if (file_exists($view_file.$_ci_file))
777 {
778 $_ci_path = $view_file.$_ci_file;
779 $file_exists = TRUE;
780 break;
781 }
David Behlercda768a2011-08-14 23:52:48 +0200782
Greg Akerf5c84022011-04-19 17:13:03 -0500783 if ( ! $cascade)
784 {
785 break;
David Behlercda768a2011-08-14 23:52:48 +0200786 }
Greg Akerf5c84022011-04-19 17:13:03 -0500787 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 }
Barry Mienydd671972010-10-04 16:33:58 +0200789
Greg Akerf5c84022011-04-19 17:13:03 -0500790 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 {
792 show_error('Unable to load the requested file: '.$_ci_file);
793 }
Barry Mienydd671972010-10-04 16:33:58 +0200794
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 // This allows anything loaded using $this->load (views, files, etc.)
796 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500797 $_ci_CI =& get_instance();
798 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500800 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500802 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 }
804 }
805
806 /*
807 * Extract and cache variables
808 *
809 * You can either set variables using the dedicated $this->load_vars()
810 * function or via the second parameter of this function. We'll merge
811 * the two types and cache them so that views that are embedded within
812 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200813 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 if (is_array($_ci_vars))
815 {
816 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
817 }
818 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 /*
821 * Buffer the output
822 *
823 * We buffer the output for two reasons:
824 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200825 * 2. So that the final rendered template can be post-processed by
826 * the output class. Why do we need post processing? For one thing,
827 * in order to show the elapsed page load time. Unless we can
828 * intercept the content right before it's sent to the browser and
829 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 */
831 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 // If the PHP installation does not support short tags we'll
834 // do a little string replacement, changing the short tags
835 // to standard PHP echo statements.
Barry Mienydd671972010-10-04 16:33:58 +0200836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
838 {
839 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
840 }
841 else
842 {
843 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
844 }
Barry Mienydd671972010-10-04 16:33:58 +0200845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200847
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 // Return the file data if requested
849 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200850 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 $buffer = ob_get_contents();
852 @ob_end_clean();
853 return $buffer;
854 }
855
856 /*
857 * Flush the buffer... or buff the flusher?
858 *
859 * In order to permit views to be nested within
860 * other views, we need to flush the content back out whenever
861 * we are beyond the first level of output buffering so that
862 * it can be seen and included properly by the first included
863 * template and any subsequent ones. Oy!
864 *
Barry Mienydd671972010-10-04 16:33:58 +0200865 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 if (ob_get_level() > $this->_ci_ob_level + 1)
867 {
868 ob_end_flush();
869 }
870 else
871 {
Greg Aker22f1a632010-11-10 15:34:35 -0600872 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 @ob_end_clean();
874 }
875 }
876
877 // --------------------------------------------------------------------
878
879 /**
880 * Load class
881 *
882 * This function loads the requested class.
883 *
Barry Mienydd671972010-10-04 16:33:58 +0200884 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 * @param mixed any additional parameters
886 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200887 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 */
Greg Akerf5c84022011-04-19 17:13:03 -0500889 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200890 {
891 // Get the class name, and while we're at it trim any slashes.
892 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500894 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 // Was the path included with the class name?
897 // We look for a slash to determine this
898 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600899 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 {
Derek Jones32bf1862010-03-02 13:46:07 -0600901 // Extract the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200902 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200903
Derek Jones32bf1862010-03-02 13:46:07 -0600904 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200905 $class = substr($class, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 }
907
908 // We'll test for both lowercase and capitalized versions of the file name
909 foreach (array(ucfirst($class), strtolower($class)) as $class)
910 {
Greg Aker3a746652011-04-19 10:59:47 -0500911 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000912
Barry Mienydd671972010-10-04 16:33:58 +0200913 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 if (file_exists($subclass))
915 {
Greg Aker3a746652011-04-19 10:59:47 -0500916 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200917
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 if ( ! file_exists($baseclass))
919 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200920 log_message('error', 'Unable to load the requested class: '.$class);
921 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 }
923
Andrey Andreevd7297352012-01-07 22:53:14 +0200924 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 if (in_array($subclass, $this->_ci_loaded_files))
926 {
927 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200928 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 // return a new instance of the object
930 if ( ! is_null($object_name))
931 {
932 $CI =& get_instance();
933 if ( ! isset($CI->$object_name))
934 {
Barry Mienydd671972010-10-04 16:33:58 +0200935 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 }
937 }
Barry Mienydd671972010-10-04 16:33:58 +0200938
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200940 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 return;
942 }
Barry Mienydd671972010-10-04 16:33:58 +0200943
944 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 include_once($subclass);
946 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200947
948 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 }
Barry Mienydd671972010-10-04 16:33:58 +0200950
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600952 $is_duplicate = FALSE;
953 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 {
Greg Aker3a746652011-04-19 10:59:47 -0500955 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600956
Andrey Andreevd7297352012-01-07 22:53:14 +0200957 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 if ( ! file_exists($filepath))
959 {
960 continue;
961 }
Barry Mienydd671972010-10-04 16:33:58 +0200962
Andrey Andreevd7297352012-01-07 22:53:14 +0200963 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 if (in_array($filepath, $this->_ci_loaded_files))
965 {
966 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200967 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 // return a new instance of the object
969 if ( ! is_null($object_name))
970 {
971 $CI =& get_instance();
972 if ( ! isset($CI->$object_name))
973 {
974 return $this->_ci_init_class($class, '', $params, $object_name);
975 }
976 }
Barry Mienydd671972010-10-04 16:33:58 +0200977
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200979 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 return;
981 }
Barry Mienydd671972010-10-04 16:33:58 +0200982
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 include_once($filepath);
984 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +0200985 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 }
Derek Jones32bf1862010-03-02 13:46:07 -0600987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 } // END FOREACH
989
Andrey Andreevd7297352012-01-07 22:53:14 +0200990 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 if ($subdir == '')
992 {
993 $path = strtolower($class).'/'.$class;
994 return $this->_ci_load_class($path, $params);
995 }
Barry Mienydd671972010-10-04 16:33:58 +0200996
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 // If we got this far we were unable to find the requested class.
998 // We do not issue errors if the load call failed due to a duplicate request
999 if ($is_duplicate == FALSE)
1000 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001001 log_message('error', 'Unable to load the requested class: '.$class);
1002 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
1004 }
Barry Mienydd671972010-10-04 16:33:58 +02001005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 // --------------------------------------------------------------------
1007
1008 /**
1009 * Instantiates a class
1010 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 * @param string
1012 * @param string
David Behlercda768a2011-08-14 23:52:48 +02001013 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 * @param string an optional object name
1015 * @return null
1016 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001017 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001018 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001019 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 if ($config === NULL)
1021 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001022 // Fetch the config paths containing any package paths
1023 $config_component = $this->_ci_get_component('config');
1024
1025 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001027 // Break on the first found file, thus package files
1028 // are not overridden by default paths
1029 foreach ($config_component->_config_paths as $path)
1030 {
1031 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001032 // are case-sensitive with regard to file names. Check for environment
1033 // first, global next
Greg Aker3a746652011-04-19 10:59:47 -05001034 if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001035 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001036 include($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001037 break;
1038 }
Greg Aker3a746652011-04-19 10:59:47 -05001039 elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001040 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001041 include($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001042 break;
1043 }
Greg Aker3a746652011-04-19 10:59:47 -05001044 elseif (file_exists($path .'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001045 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001046 include($path .'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001047 break;
1048 }
Greg Aker3a746652011-04-19 10:59:47 -05001049 elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001050 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001051 include($path .'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001052 break;
1053 }
1054 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 }
1056 }
Barry Mienydd671972010-10-04 16:33:58 +02001057
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +02001059 {
1060 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 {
1062 $name = 'CI_'.$class;
1063 }
Barry Mienydd671972010-10-04 16:33:58 +02001064 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 {
1066 $name = config_item('subclass_prefix').$class;
1067 }
1068 else
1069 {
1070 $name = $class;
1071 }
1072 }
1073 else
1074 {
1075 $name = $prefix.$class;
1076 }
Barry Mienydd671972010-10-04 16:33:58 +02001077
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 // Is the class name valid?
1079 if ( ! class_exists($name))
1080 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001081 log_message('error', 'Non-existent class: '.$name);
1082 show_error('Non-existent class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 }
Barry Mienydd671972010-10-04 16:33:58 +02001084
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001086 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001088
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 if (is_null($object_name))
1090 {
1091 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
1092 }
1093 else
1094 {
1095 $classvar = $object_name;
1096 }
1097
Barry Mienydd671972010-10-04 16:33:58 +02001098 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 $this->_ci_classes[$class] = $classvar;
1100
Barry Mienydd671972010-10-04 16:33:58 +02001101 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 $CI =& get_instance();
1103 if ($config !== NULL)
1104 {
1105 $CI->$classvar = new $name($config);
1106 }
1107 else
Barry Mienydd671972010-10-04 16:33:58 +02001108 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 $CI->$classvar = new $name;
Barry Mienydd671972010-10-04 16:33:58 +02001110 }
1111 }
1112
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001114
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 /**
1116 * Autoloader
1117 *
1118 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -06001119 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 * @param array
1122 * @return void
1123 */
Shane Pearson665baec2011-08-22 18:52:19 -05001124 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001125 {
Greg Akerd96f8822011-12-27 16:23:47 -06001126 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
1127 {
1128 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
1129 }
1130 else
1131 {
1132 include(APPPATH.'config/autoload.php');
1133 }
Barry Mienydd671972010-10-04 16:33:58 +02001134
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 if ( ! isset($autoload))
1136 {
1137 return FALSE;
1138 }
Barry Mienydd671972010-10-04 16:33:58 +02001139
Phil Sturgeon9730c752010-12-15 10:50:15 +00001140 // Autoload packages
1141 if (isset($autoload['packages']))
1142 {
1143 foreach ($autoload['packages'] as $package_path)
1144 {
1145 $this->add_package_path($package_path);
1146 }
1147 }
1148
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 // Load any custom config file
1150 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001151 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 $CI =& get_instance();
1153 foreach ($autoload['config'] as $key => $val)
1154 {
1155 $CI->config->load($val);
1156 }
Barry Mienydd671972010-10-04 16:33:58 +02001157 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001158
Derek Jonesc6da5032010-03-09 20:44:27 -06001159 // Autoload helpers and languages
1160 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001161 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
1163 {
1164 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001165 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 }
1167
1168 // A little tweak to remain backward compatible
1169 // The $autoload['core'] item was deprecated
Derek Jones32bf1862010-03-02 13:46:07 -06001170 if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 {
1172 $autoload['libraries'] = $autoload['core'];
1173 }
Barry Mienydd671972010-10-04 16:33:58 +02001174
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 // Load libraries
1176 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
1177 {
1178 // Load the database driver.
1179 if (in_array('database', $autoload['libraries']))
1180 {
1181 $this->database();
1182 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1183 }
Barry Mienydd671972010-10-04 16:33:58 +02001184
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 // Load all other libraries
1186 foreach ($autoload['libraries'] as $item)
1187 {
1188 $this->library($item);
1189 }
Barry Mienydd671972010-10-04 16:33:58 +02001190 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001191
1192 // Autoload models
1193 if (isset($autoload['model']))
1194 {
1195 $this->model($autoload['model']);
1196 }
Barry Mienydd671972010-10-04 16:33:58 +02001197 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001198
1199 // --------------------------------------------------------------------
1200
1201 /**
1202 * Object to Array
1203 *
1204 * Takes an object as input and converts the class variables to array key/vals
1205 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 * @param object
1207 * @return array
1208 */
Greg Akerf5c84022011-04-19 17:13:03 -05001209 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 {
1211 return (is_object($object)) ? get_object_vars($object) : $object;
1212 }
1213
1214 // --------------------------------------------------------------------
1215
1216 /**
Derek Jones32bf1862010-03-02 13:46:07 -06001217 * Get a reference to a specific library or model
1218 *
David Behlercda768a2011-08-14 23:52:48 +02001219 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001220 * @return bool
1221 */
Greg Akerf5c84022011-04-19 17:13:03 -05001222 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001223 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001224 $CI =& get_instance();
1225 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001226 }
1227
1228 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001229
Derek Jones32bf1862010-03-02 13:46:07 -06001230 /**
1231 * Prep filename
1232 *
1233 * This function preps the name of various items to make loading them more reliable.
1234 *
Derek Jones32bf1862010-03-02 13:46:07 -06001235 * @param mixed
David Behlercda768a2011-08-14 23:52:48 +02001236 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001237 * @return array
1238 */
Greg Akerf5c84022011-04-19 17:13:03 -05001239 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001240 {
1241 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001242 {
Greg Aker3a746652011-04-19 10:59:47 -05001243 return array(strtolower(str_replace('.php', '', str_replace($extension, '', $filename)).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001244 }
1245 else
1246 {
1247 foreach ($filename as $key => $val)
1248 {
Greg Aker3a746652011-04-19 10:59:47 -05001249 $filename[$key] = strtolower(str_replace('.php', '', str_replace($extension, '', $val)).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001250 }
Barry Mienydd671972010-10-04 16:33:58 +02001251
Derek Jones32bf1862010-03-02 13:46:07 -06001252 return $filename;
1253 }
1254 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001255}
1256
1257/* End of file Loader.php */
Andrey Andreevd7297352012-01-07 22:53:14 +02001258/* Location: ./system/core/Loader.php */