blob: 7c8b298acef1b3ae6ee1f143957dc870a02361ea [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?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 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Loader Class
20 *
21 * Loads views and files
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @author ExpressionEngine Dev Team
26 * @category Loader
27 * @link http://codeigniter.com/user_guide/libraries/loader.html
28 */
29class CI_Loader {
30
31 // All these are set automatically. Don't mess with them.
Greg Aker0c9ee4a2011-04-20 09:40:17 -050032 protected $_ci_ob_level;
33 protected $_ci_view_paths = array();
Greg Akerf5c84022011-04-19 17:13:03 -050034 protected $_ci_library_paths = array();
Greg Aker0c9ee4a2011-04-20 09:40:17 -050035 protected $_ci_model_paths = array();
36 protected $_ci_helper_paths = array();
37 protected $_base_classes = array(); // Set by the controller class
38 protected $_ci_cached_vars = array();
39 protected $_ci_classes = array();
40 protected $_ci_loaded_files = array();
41 protected $_ci_models = array();
42 protected $_ci_helpers = array();
Derek Jones4b9c6292011-07-01 17:40:48 -050043 protected $_ci_varmap = array('unit_test' => 'unit',
Greg Aker0c9ee4a2011-04-20 09:40:17 -050044 'user_agent' => 'agent');
Derek Allard2067d1a2008-11-13 22:59:24 +000045
46 /**
47 * Constructor
48 *
49 * Sets the path to the view files and gets the initial output buffering level
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
Greg Akerf5c84022011-04-19 17:13:03 -050051 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +020052 {
Derek Jones4b9c6292011-07-01 17:40:48 -050053 $this->_ci_ob_level = ob_get_level();
Derek Jones32bf1862010-03-02 13:46:07 -060054 $this->_ci_library_paths = array(APPPATH, BASEPATH);
55 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
56 $this->_ci_model_paths = array(APPPATH);
Greg Akerf5c84022011-04-19 17:13:03 -050057 $this->_ci_view_paths = array(APPPATH.'views/' => TRUE);
Derek Jones4b9c6292011-07-01 17:40:48 -050058
Derek Allard2067d1a2008-11-13 22:59:24 +000059 log_message('debug', "Loader Class Initialized");
60 }
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 // --------------------------------------------------------------------
Derek Jones4b9c6292011-07-01 17:40:48 -050063
Greg Aker0c9ee4a2011-04-20 09:40:17 -050064 /**
65 * Set _base_classes variable
66 *
67 * This method is called once in CI_Controller.
68 *
Derek Jones4b9c6292011-07-01 17:40:48 -050069 * @param array
Greg Aker0c9ee4a2011-04-20 09:40:17 -050070 * @return object
71 */
72 public function set_base_classes()
73 {
74 $this->_base_classes =& is_loaded();
Derek Jones4b9c6292011-07-01 17:40:48 -050075
Greg Aker0c9ee4a2011-04-20 09:40:17 -050076 return $this;
77 }
78
79 // --------------------------------------------------------------------
80
81 /**
82 * Is Loaded
83 *
84 * A utility function to test if a class is in the self::$_ci_classes array.
85 * This function returns the object name if the class tested for is loaded,
86 * and returns FALSE if it isn't.
87 *
88 * It is mainly used in the form_helper -> _get_validation_object()
89 *
90 * @param string class being checked for
91 * @return mixed class object name on the CI SuperObject or FALSE
92 */
93 public function is_loaded($class)
94 {
95 if (isset($this->_ci_classes[$class]))
96 {
97 return $this->_ci_classes[$class];
98 }
Derek Jones4b9c6292011-07-01 17:40:48 -050099
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500100 return FALSE;
101 }
102
103 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 /**
106 * Class Loader
107 *
108 * This function lets users load and instantiate classes.
109 * It is designed to be called from a user's app controllers.
110 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 * @param string the name of the class
112 * @param mixed the optional parameters
113 * @param string an optional object name
114 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200115 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500116 public function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
Greg Akerce433962010-10-12 09:29:35 -0500118 if (is_array($library))
119 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100120 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500121 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600122 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500123 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000124
Greg Akerce433962010-10-12 09:29:35 -0500125 return;
126 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000127
Derek Jones32bf1862010-03-02 13:46:07 -0600128 if ($library == '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
130 return FALSE;
131 }
132
Derek Jones32bf1862010-03-02 13:46:07 -0600133 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
135 $params = NULL;
136 }
137
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600138 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
140
141 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 /**
144 * Model Loader
145 *
146 * This function lets users load and instantiate models.
147 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 * @param string the name of the class
149 * @param string name for the model
150 * @param bool database connection
151 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200152 */
Greg Akerf5c84022011-04-19 17:13:03 -0500153 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200154 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 if (is_array($model))
156 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500157 foreach ($model as $babe)
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Barry Mienydd671972010-10-04 16:33:58 +0200159 $this->model($babe);
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
161 return;
162 }
163
164 if ($model == '')
165 {
166 return;
167 }
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Jones32bf1862010-03-02 13:46:07 -0600169 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600172 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
Derek Jones32bf1862010-03-02 13:46:07 -0600174 // The path is in front of the last slash
175 $path = substr($model, 0, $last_slash + 1);
176
177 // And the model name behind it
178 $model = substr($model, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 if ($name == '')
182 {
183 $name = $model;
184 }
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 if (in_array($name, $this->_ci_models, TRUE))
187 {
188 return;
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 $CI =& get_instance();
192 if (isset($CI->$name))
193 {
194 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000198
Derek Jones32bf1862010-03-02 13:46:07 -0600199 foreach ($this->_ci_model_paths as $mod_path)
200 {
Greg Aker3a746652011-04-19 10:59:47 -0500201 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600202 {
203 continue;
204 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000205
Derek Jones32bf1862010-03-02 13:46:07 -0600206 if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
207 {
208 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500209 {
Derek Jones32bf1862010-03-02 13:46:07 -0600210 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500211 }
Derek Jones32bf1862010-03-02 13:46:07 -0600212
213 $CI->load->database($db_conn, FALSE, TRUE);
214 }
215
Greg Akerbce13482010-10-11 15:37:16 -0500216 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600217 {
218 load_class('Model', 'core');
219 }
220
Greg Aker3a746652011-04-19 10:59:47 -0500221 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600222
223 $model = ucfirst($model);
224
225 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600226
227 $this->_ci_models[] = $name;
228 return;
229 }
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Jones32bf1862010-03-02 13:46:07 -0600231 // couldn't find the model
232 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 /**
238 * Database Loader
239 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 * @param string the DB credentials
241 * @param bool whether to return the DB object
242 * @param bool whether to enable active record (this allows us to override the config setting)
243 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200244 */
Greg Akerf5c84022011-04-19 17:13:03 -0500245 public function database($params = '', $return = FALSE, $active_record = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
247 // Grab the super object
248 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 // Do we even need to load the database class?
Derek Jones9fb6dd12009-12-05 15:32:48 +0000251 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 +0000252 {
253 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200254 }
255
Greg Aker3a746652011-04-19 10:59:47 -0500256 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000257
258 if ($return === TRUE)
259 {
260 return DB($params, $active_record);
261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Jones4b9c6292011-07-01 17:40:48 -0500263 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 // reference errors with some configurations
265 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 // Load the DB class
Barry Mienydd671972010-10-04 16:33:58 +0200268 $CI->db =& DB($params, $active_record);
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // --------------------------------------------------------------------
272
273 /**
274 * Load the Utilities Class
275 *
Barry Mienydd671972010-10-04 16:33:58 +0200276 * @return string
277 */
Greg Akerf5c84022011-04-19 17:13:03 -0500278 public function dbutil()
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 if ( ! class_exists('CI_DB'))
281 {
282 $this->database();
283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 $CI =& get_instance();
286
287 // for backwards compatibility, load dbforge so we can extend dbutils off it
288 // this use is deprecated and strongly discouraged
289 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200290
Greg Aker3a746652011-04-19 10:59:47 -0500291 require_once(BASEPATH.'database/DB_utility.php');
292 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
294
Pascal Kriete58560022010-11-10 16:01:20 -0500295 $CI->dbutil = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // --------------------------------------------------------------------
299
300 /**
301 * Load the Database Forge Class
302 *
Barry Mienydd671972010-10-04 16:33:58 +0200303 * @return string
304 */
Greg Akerf5c84022011-04-19 17:13:03 -0500305 public function dbforge()
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
307 if ( ! class_exists('CI_DB'))
308 {
309 $this->database();
310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200313
Greg Aker3a746652011-04-19 10:59:47 -0500314 require_once(BASEPATH.'database/DB_forge.php');
315 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
317
318 $CI->dbforge = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 }
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 /**
324 * Load View
325 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500326 * This function is used to load a "view" file. It has three parameters:
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 *
328 * 1. The name of the "view" file to be included.
329 * 2. An associative array of data to be extracted for use in the view.
Derek Jones4b9c6292011-07-01 17:40:48 -0500330 * 3. TRUE/FALSE - whether to return the data or load it. In
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 * some cases it's advantageous to be able to return data so that
332 * a developer can process it in some way.
333 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 * @param string
335 * @param array
336 * @param bool
337 * @return void
338 */
Greg Akerf5c84022011-04-19 17:13:03 -0500339 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
341 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 /**
347 * Load File
348 *
349 * This is a generic file loader
350 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 * @param string
352 * @param bool
353 * @return string
354 */
Greg Akerf5c84022011-04-19 17:13:03 -0500355 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
357 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 /**
363 * Set Variables
364 *
365 * Once variables are set they become available within
366 * the controller class and its "view" files.
367 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 * @param array
369 * @return void
370 */
Greg Akerf5c84022011-04-19 17:13:03 -0500371 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
373 if ($val != '' AND is_string($vars))
374 {
375 $vars = array($vars => $val);
376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 if (is_array($vars) AND count($vars) > 0)
381 {
382 foreach ($vars as $key => $val)
383 {
384 $this->_ci_cached_vars[$key] = $val;
385 }
386 }
387 }
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 /**
Phil Sturgeone0df0792011-07-22 16:11:34 -0600392 * Get Variable
393 *
394 * Check if a variable is set and retrieve it.
395 *
396 * @param array
397 * @return void
398 */
399 public function get_var($key)
400 {
401 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
402 }
403
404 // --------------------------------------------------------------------
405
406 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 * Load Helper
408 *
409 * This function loads the specified helper file.
410 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @param mixed
412 * @return void
413 */
Greg Akerf5c84022011-04-19 17:13:03 -0500414 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200415 {
Derek Jones32bf1862010-03-02 13:46:07 -0600416 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200417 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 if (isset($this->_ci_helpers[$helper]))
419 {
420 continue;
421 }
Derek Jones32bf1862010-03-02 13:46:07 -0600422
Greg Aker3a746652011-04-19 10:59:47 -0500423 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000424
Barry Mienydd671972010-10-04 16:33:58 +0200425 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 if (file_exists($ext_helper))
427 {
Greg Aker3a746652011-04-19 10:59:47 -0500428 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 if ( ! file_exists($base_helper))
431 {
Greg Aker3a746652011-04-19 10:59:47 -0500432 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 include_once($ext_helper);
436 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Jones32bf1862010-03-02 13:46:07 -0600438 $this->_ci_helpers[$helper] = TRUE;
439 log_message('debug', 'Helper loaded: '.$helper);
440 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Jones32bf1862010-03-02 13:46:07 -0600443 // Try to load the helper
444 foreach ($this->_ci_helper_paths as $path)
445 {
Greg Aker3a746652011-04-19 10:59:47 -0500446 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200447 {
Greg Aker3a746652011-04-19 10:59:47 -0500448 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600449
450 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200451 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600452 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
454 }
455
Derek Jones32bf1862010-03-02 13:46:07 -0600456 // unable to load the helper
457 if ( ! isset($this->_ci_helpers[$helper]))
458 {
Greg Aker3a746652011-04-19 10:59:47 -0500459 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 /**
467 * Load Helpers
468 *
469 * This is simply an alias to the above function in case the
470 * user has written the plural form of this function.
471 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 * @param array
473 * @return void
474 */
Greg Akerf5c84022011-04-19 17:13:03 -0500475 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
477 $this->helper($helpers);
478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 * Loads a language file
484 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 * @param array
486 * @param string
487 * @return void
488 */
Greg Akerf5c84022011-04-19 17:13:03 -0500489 public function language($file = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
491 $CI =& get_instance();
492
493 if ( ! is_array($file))
494 {
495 $file = array($file);
496 }
497
498 foreach ($file as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200499 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 $CI->lang->load($langfile, $lang);
501 }
502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 /**
507 * Loads a config file
508 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 * @param string
510 * @return void
511 */
Greg Akerf5c84022011-04-19 17:13:03 -0500512 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200513 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 $CI =& get_instance();
515 $CI->config->load($file, $use_sections, $fail_gracefully);
516 }
517
518 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 /**
Derek Jones8dca0412010-03-05 13:01:44 -0600521 * Driver
522 *
523 * Loads a driver library
524 *
525 * @param string the name of the class
526 * @param mixed the optional parameters
527 * @param string an optional object name
528 * @return void
529 */
Greg Akerf5c84022011-04-19 17:13:03 -0500530 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600531 {
532 if ( ! class_exists('CI_Driver_Library'))
533 {
534 // we aren't instantiating an object here, that'll be done by the Library itself
Greg Aker3a746652011-04-19 10:59:47 -0500535 require BASEPATH.'libraries/Driver.php';
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600536 }
Barry Mienydd671972010-10-04 16:33:58 +0200537
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600538 // We can save the loader some time since Drivers will *always* be in a subfolder,
539 // and typically identically named to the library
540 if ( ! strpos($library, '/'))
541 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500542 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600543 }
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Jones8dca0412010-03-05 13:01:44 -0600545 return $this->library($library, $params, $object_name);
546 }
547
548 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Jones8dca0412010-03-05 13:01:44 -0600550 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600551 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 *
Derek Jones32bf1862010-03-02 13:46:07 -0600553 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 * @param string
Derek Jones4b9c6292011-07-01 17:40:48 -0500556 * @param boolean
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600558 */
Greg Akerf5c84022011-04-19 17:13:03 -0500559 public function add_package_path($path, $view_cascade=TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600560 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500561 $path = rtrim($path, '/').'/';
Derek Jones4b9c6292011-07-01 17:40:48 -0500562
Derek Jones32bf1862010-03-02 13:46:07 -0600563 array_unshift($this->_ci_library_paths, $path);
564 array_unshift($this->_ci_model_paths, $path);
565 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200566
Greg Akerf5c84022011-04-19 17:13:03 -0500567 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
568
Derek Jones32bf1862010-03-02 13:46:07 -0600569 // Add config file path
570 $config =& $this->_ci_get_component('config');
571 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 }
573
574 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600575
576 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000577 * Get Package Paths
578 *
579 * Return a list of all package paths, by default it will ignore BASEPATH.
580 *
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000581 * @param string
582 * @return void
583 */
Greg Akerf5c84022011-04-19 17:13:03 -0500584 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000585 {
586 return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
587 }
588
589 // --------------------------------------------------------------------
590
591 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600592 * Remove Package Path
593 *
594 * Remove a path from the library, model, and helper path arrays if it exists
595 * If no path is provided, the most recently added path is removed.
596 *
Derek Jones32bf1862010-03-02 13:46:07 -0600597 * @param type
598 * @return type
599 */
Greg Akerf5c84022011-04-19 17:13:03 -0500600 public function remove_package_path($path = '', $remove_config_path = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600601 {
602 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Jones32bf1862010-03-02 13:46:07 -0600604 if ($path == '')
605 {
606 $void = array_shift($this->_ci_library_paths);
607 $void = array_shift($this->_ci_model_paths);
608 $void = array_shift($this->_ci_helper_paths);
Greg Akerf5c84022011-04-19 17:13:03 -0500609 $void = array_shift($this->_ci_view_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600610 $void = array_shift($config->_config_paths);
611 }
612 else
613 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500614 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600615 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
616 {
617 if (($key = array_search($path, $this->{$var})) !== FALSE)
618 {
619 unset($this->{$var}[$key]);
620 }
621 }
Derek Jones4b9c6292011-07-01 17:40:48 -0500622
Greg Akerf5c84022011-04-19 17:13:03 -0500623 if (isset($this->_ci_view_paths[$path.'views/']))
624 {
625 unset($this->_ci_view_paths[$path.'views/']);
626 }
Barry Mienydd671972010-10-04 16:33:58 +0200627
Derek Jones32bf1862010-03-02 13:46:07 -0600628 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
629 {
630 unset($config->_config_paths[$key]);
631 }
632 }
Barry Mienydd671972010-10-04 16:33:58 +0200633
Derek Jones32bf1862010-03-02 13:46:07 -0600634 // make sure the application default paths are still in the array
635 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
636 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
637 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500638 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600639 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
640 }
641
642 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 /**
645 * Loader
646 *
647 * This function is used to load views and files.
648 * Variables are prefixed with _ci_ to avoid symbol collision with
649 * variables made available to view files
650 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 * @param array
652 * @return void
653 */
Greg Akerf5c84022011-04-19 17:13:03 -0500654 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
656 // Set the default data variables
657 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
658 {
659 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
660 }
Derek Jones4b9c6292011-07-01 17:40:48 -0500661
Greg Akerf5c84022011-04-19 17:13:03 -0500662 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000663
664 // Set the path to the requested file
Greg Aker8807be32011-04-21 13:06:15 -0500665 if ($_ci_path != '')
666 {
667 $_ci_x = explode('/', $_ci_path);
668 $_ci_file = end($_ci_x);
669 }
670 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
672 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Greg Aker3a746652011-04-19 10:59:47 -0500673 $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500674
675 foreach ($this->_ci_view_paths as $view_file => $cascade)
676 {
677 if (file_exists($view_file.$_ci_file))
678 {
679 $_ci_path = $view_file.$_ci_file;
680 $file_exists = TRUE;
681 break;
682 }
Derek Jones4b9c6292011-07-01 17:40:48 -0500683
Greg Akerf5c84022011-04-19 17:13:03 -0500684 if ( ! $cascade)
685 {
686 break;
Derek Jones4b9c6292011-07-01 17:40:48 -0500687 }
Greg Akerf5c84022011-04-19 17:13:03 -0500688 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 }
Barry Mienydd671972010-10-04 16:33:58 +0200690
Greg Akerf5c84022011-04-19 17:13:03 -0500691 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
693 show_error('Unable to load the requested file: '.$_ci_file);
694 }
Barry Mienydd671972010-10-04 16:33:58 +0200695
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 // This allows anything loaded using $this->load (views, files, etc.)
697 // to become accessible from within the Controller and Model functions.
Barry Mienydd671972010-10-04 16:33:58 +0200698
Pascal Kriete89ace432010-11-10 15:49:10 -0500699 $_ci_CI =& get_instance();
700 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500702 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500704 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 }
706 }
707
708 /*
709 * Extract and cache variables
710 *
711 * You can either set variables using the dedicated $this->load_vars()
712 * function or via the second parameter of this function. We'll merge
713 * the two types and cache them so that views that are embedded within
714 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200715 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 if (is_array($_ci_vars))
717 {
718 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
719 }
720 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200721
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 /*
723 * Buffer the output
724 *
725 * We buffer the output for two reasons:
726 * 1. Speed. You get a significant speed boost.
727 * 2. So that the final rendered template can be
Derek Jones4b9c6292011-07-01 17:40:48 -0500728 * post-processed by the output class. Why do we
729 * need post processing? For one thing, in order to
730 * show the elapsed page load time. Unless we
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 * can intercept the content right before it's sent to
732 * the browser and then stop the timer it won't be accurate.
733 */
734 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 // If the PHP installation does not support short tags we'll
737 // do a little string replacement, changing the short tags
738 // to standard PHP echo statements.
Barry Mienydd671972010-10-04 16:33:58 +0200739
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
741 {
742 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
743 }
744 else
745 {
746 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
747 }
Barry Mienydd671972010-10-04 16:33:58 +0200748
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200750
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 // Return the file data if requested
752 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200753 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 $buffer = ob_get_contents();
755 @ob_end_clean();
756 return $buffer;
757 }
758
759 /*
760 * Flush the buffer... or buff the flusher?
761 *
762 * In order to permit views to be nested within
763 * other views, we need to flush the content back out whenever
764 * we are beyond the first level of output buffering so that
765 * it can be seen and included properly by the first included
766 * template and any subsequent ones. Oy!
767 *
Barry Mienydd671972010-10-04 16:33:58 +0200768 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 if (ob_get_level() > $this->_ci_ob_level + 1)
770 {
771 ob_end_flush();
772 }
773 else
774 {
Greg Aker22f1a632010-11-10 15:34:35 -0600775 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 @ob_end_clean();
777 }
778 }
779
780 // --------------------------------------------------------------------
781
782 /**
783 * Load class
784 *
785 * This function loads the requested class.
786 *
Barry Mienydd671972010-10-04 16:33:58 +0200787 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 * @param mixed any additional parameters
789 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200790 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 */
Greg Akerf5c84022011-04-19 17:13:03 -0500792 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200793 {
794 // Get the class name, and while we're at it trim any slashes.
795 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500797 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 // Was the path included with the class name?
800 // We look for a slash to determine this
801 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600802 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 {
Derek Jones32bf1862010-03-02 13:46:07 -0600804 // Extract the path
805 $subdir = substr($class, 0, $last_slash + 1);
Barry Mienydd671972010-10-04 16:33:58 +0200806
Derek Jones32bf1862010-03-02 13:46:07 -0600807 // Get the filename from the path
808 $class = substr($class, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 }
810
811 // We'll test for both lowercase and capitalized versions of the file name
812 foreach (array(ucfirst($class), strtolower($class)) as $class)
813 {
Greg Aker3a746652011-04-19 10:59:47 -0500814 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000815
Barry Mienydd671972010-10-04 16:33:58 +0200816 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 if (file_exists($subclass))
818 {
Greg Aker3a746652011-04-19 10:59:47 -0500819 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 if ( ! file_exists($baseclass))
822 {
823 log_message('error', "Unable to load the requested class: ".$class);
824 show_error("Unable to load the requested class: ".$class);
825 }
826
Derek Jones4b9c6292011-07-01 17:40:48 -0500827 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 if (in_array($subclass, $this->_ci_loaded_files))
829 {
830 // Before we deem this to be a duplicate request, let's see
Derek Jones4b9c6292011-07-01 17:40:48 -0500831 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 // return a new instance of the object
833 if ( ! is_null($object_name))
834 {
835 $CI =& get_instance();
836 if ( ! isset($CI->$object_name))
837 {
Barry Mienydd671972010-10-04 16:33:58 +0200838 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 }
840 }
Barry Mienydd671972010-10-04 16:33:58 +0200841
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 $is_duplicate = TRUE;
843 log_message('debug', $class." class already loaded. Second attempt ignored.");
844 return;
845 }
Barry Mienydd671972010-10-04 16:33:58 +0200846
847 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 include_once($subclass);
849 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200850
851 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 }
Barry Mienydd671972010-10-04 16:33:58 +0200853
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600855 $is_duplicate = FALSE;
856 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 {
Greg Aker3a746652011-04-19 10:59:47 -0500858 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600859
Derek Jones4b9c6292011-07-01 17:40:48 -0500860 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 if ( ! file_exists($filepath))
862 {
863 continue;
864 }
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Jones4b9c6292011-07-01 17:40:48 -0500866 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 if (in_array($filepath, $this->_ci_loaded_files))
868 {
869 // Before we deem this to be a duplicate request, let's see
Derek Jones4b9c6292011-07-01 17:40:48 -0500870 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 // return a new instance of the object
872 if ( ! is_null($object_name))
873 {
874 $CI =& get_instance();
875 if ( ! isset($CI->$object_name))
876 {
877 return $this->_ci_init_class($class, '', $params, $object_name);
878 }
879 }
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 $is_duplicate = TRUE;
882 log_message('debug', $class." class already loaded. Second attempt ignored.");
883 return;
884 }
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 include_once($filepath);
887 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +0200888 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 }
Derek Jones32bf1862010-03-02 13:46:07 -0600890
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 } // END FOREACH
892
Derek Jones4b9c6292011-07-01 17:40:48 -0500893 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 if ($subdir == '')
895 {
896 $path = strtolower($class).'/'.$class;
897 return $this->_ci_load_class($path, $params);
898 }
Barry Mienydd671972010-10-04 16:33:58 +0200899
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 // If we got this far we were unable to find the requested class.
901 // We do not issue errors if the load call failed due to a duplicate request
902 if ($is_duplicate == FALSE)
903 {
904 log_message('error', "Unable to load the requested class: ".$class);
905 show_error("Unable to load the requested class: ".$class);
906 }
907 }
Barry Mienydd671972010-10-04 16:33:58 +0200908
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 // --------------------------------------------------------------------
910
911 /**
912 * Instantiates a class
913 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 * @param string
915 * @param string
916 * @param string an optional object name
917 * @return null
918 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500919 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200920 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500921 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 if ($config === NULL)
923 {
Eric Barnes5e16ec62011-01-04 17:25:23 -0500924 // Fetch the config paths containing any package paths
925 $config_component = $this->_ci_get_component('config');
926
927 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 {
Eric Barnes5e16ec62011-01-04 17:25:23 -0500929 // Break on the first found file, thus package files
930 // are not overridden by default paths
931 foreach ($config_component->_config_paths as $path)
932 {
933 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +0100934 // are case-sensitive with regard to file names. Check for environment
935 // first, global next
Greg Aker3a746652011-04-19 10:59:47 -0500936 if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +0100937 {
Greg Aker3a746652011-04-19 10:59:47 -0500938 include_once($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +0100939 break;
940 }
Greg Aker3a746652011-04-19 10:59:47 -0500941 elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +0100942 {
Greg Aker3a746652011-04-19 10:59:47 -0500943 include_once($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +0100944 break;
945 }
Greg Aker3a746652011-04-19 10:59:47 -0500946 elseif (file_exists($path .'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -0500947 {
Greg Aker3a746652011-04-19 10:59:47 -0500948 include_once($path .'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -0500949 break;
950 }
Greg Aker3a746652011-04-19 10:59:47 -0500951 elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -0500952 {
Greg Aker3a746652011-04-19 10:59:47 -0500953 include_once($path .'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -0500954 break;
955 }
956 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 }
958 }
Barry Mienydd671972010-10-04 16:33:58 +0200959
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +0200961 {
962 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
964 $name = 'CI_'.$class;
965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
968 $name = config_item('subclass_prefix').$class;
969 }
970 else
971 {
972 $name = $class;
973 }
974 }
975 else
976 {
977 $name = $prefix.$class;
978 }
Barry Mienydd671972010-10-04 16:33:58 +0200979
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 // Is the class name valid?
981 if ( ! class_exists($name))
982 {
983 log_message('error', "Non-existent class: ".$name);
984 show_error("Non-existent class: ".$class);
985 }
Barry Mienydd671972010-10-04 16:33:58 +0200986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 // Set the variable name we will assign the class to
Derek Jones4b9c6292011-07-01 17:40:48 -0500988 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +0200990
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 if (is_null($object_name))
992 {
993 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
994 }
995 else
996 {
997 $classvar = $object_name;
998 }
999
Barry Mienydd671972010-10-04 16:33:58 +02001000 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 $this->_ci_classes[$class] = $classvar;
1002
Barry Mienydd671972010-10-04 16:33:58 +02001003 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 $CI =& get_instance();
1005 if ($config !== NULL)
1006 {
1007 $CI->$classvar = new $name($config);
1008 }
1009 else
Barry Mienydd671972010-10-04 16:33:58 +02001010 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 $CI->$classvar = new $name;
Barry Mienydd671972010-10-04 16:33:58 +02001012 }
1013 }
1014
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001016
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 /**
1018 * Autoloader
1019 *
1020 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -06001021 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 *
Derek Jones4b9c6292011-07-01 17:40:48 -05001023 * This function is public, as it's used in the CI_Controller class.
Greg Akerf5c84022011-04-19 17:13:03 -05001024 * However, there is no reason you should ever needs to use it.
1025 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 * @param array
1027 * @return void
1028 */
Greg Akerf5c84022011-04-19 17:13:03 -05001029 public function ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001030 {
Greg Aker3a746652011-04-19 10:59:47 -05001031 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001032 {
Greg Aker3a746652011-04-19 10:59:47 -05001033 include_once(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001034 }
1035 else
1036 {
Greg Aker3a746652011-04-19 10:59:47 -05001037 include_once(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001038 }
Derek Jones4b9c6292011-07-01 17:40:48 -05001039
Barry Mienydd671972010-10-04 16:33:58 +02001040
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 if ( ! isset($autoload))
1042 {
1043 return FALSE;
1044 }
Barry Mienydd671972010-10-04 16:33:58 +02001045
Phil Sturgeon9730c752010-12-15 10:50:15 +00001046 // Autoload packages
1047 if (isset($autoload['packages']))
1048 {
1049 foreach ($autoload['packages'] as $package_path)
1050 {
1051 $this->add_package_path($package_path);
1052 }
1053 }
1054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 // Load any custom config file
1056 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001057 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 $CI =& get_instance();
1059 foreach ($autoload['config'] as $key => $val)
1060 {
1061 $CI->config->load($val);
1062 }
Barry Mienydd671972010-10-04 16:33:58 +02001063 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001064
Derek Jonesc6da5032010-03-09 20:44:27 -06001065 // Autoload helpers and languages
1066 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001067 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
1069 {
1070 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001071 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 }
1073
1074 // A little tweak to remain backward compatible
1075 // The $autoload['core'] item was deprecated
Derek Jones32bf1862010-03-02 13:46:07 -06001076 if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 {
1078 $autoload['libraries'] = $autoload['core'];
1079 }
Barry Mienydd671972010-10-04 16:33:58 +02001080
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 // Load libraries
1082 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
1083 {
1084 // Load the database driver.
1085 if (in_array('database', $autoload['libraries']))
1086 {
1087 $this->database();
1088 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1089 }
Barry Mienydd671972010-10-04 16:33:58 +02001090
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 // Load all other libraries
1092 foreach ($autoload['libraries'] as $item)
1093 {
1094 $this->library($item);
1095 }
Barry Mienydd671972010-10-04 16:33:58 +02001096 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001097
1098 // Autoload models
1099 if (isset($autoload['model']))
1100 {
1101 $this->model($autoload['model']);
1102 }
Barry Mienydd671972010-10-04 16:33:58 +02001103 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001104
1105 // --------------------------------------------------------------------
1106
1107 /**
1108 * Object to Array
1109 *
1110 * Takes an object as input and converts the class variables to array key/vals
1111 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 * @param object
1113 * @return array
1114 */
Greg Akerf5c84022011-04-19 17:13:03 -05001115 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 {
1117 return (is_object($object)) ? get_object_vars($object) : $object;
1118 }
1119
1120 // --------------------------------------------------------------------
1121
1122 /**
Derek Jones32bf1862010-03-02 13:46:07 -06001123 * Get a reference to a specific library or model
1124 *
Derek Jones32bf1862010-03-02 13:46:07 -06001125 * @return bool
1126 */
Greg Akerf5c84022011-04-19 17:13:03 -05001127 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001128 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001129 $CI =& get_instance();
1130 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001131 }
1132
1133 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001134
Derek Jones32bf1862010-03-02 13:46:07 -06001135 /**
1136 * Prep filename
1137 *
1138 * This function preps the name of various items to make loading them more reliable.
1139 *
Derek Jones32bf1862010-03-02 13:46:07 -06001140 * @param mixed
1141 * @return array
1142 */
Greg Akerf5c84022011-04-19 17:13:03 -05001143 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001144 {
1145 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001146 {
Greg Aker3a746652011-04-19 10:59:47 -05001147 return array(strtolower(str_replace('.php', '', str_replace($extension, '', $filename)).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001148 }
1149 else
1150 {
1151 foreach ($filename as $key => $val)
1152 {
Greg Aker3a746652011-04-19 10:59:47 -05001153 $filename[$key] = strtolower(str_replace('.php', '', str_replace($extension, '', $val)).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001154 }
Barry Mienydd671972010-10-04 16:33:58 +02001155
Derek Jones32bf1862010-03-02 13:46:07 -06001156 return $filename;
1157 }
1158 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001159}
1160
1161/* End of file Loader.php */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001162/* Location: ./system/core/Loader.php */