blob: a52ef288a134bb3f50c277e28a04c8e9462db8b5 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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();
43 protected $_ci_varmap = array('unit_test' => 'unit',
44 '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 Allard2067d1a2008-11-13 22:59:24 +000053 $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);
58
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 // --------------------------------------------------------------------
Greg Aker0c9ee4a2011-04-20 09:40:17 -050063
64 /**
65 * Set _base_classes variable
66 *
67 * This method is called once in CI_Controller.
68 *
69 * @param array
70 * @return object
71 */
72 public function set_base_classes()
73 {
74 $this->_base_classes =& is_loaded();
75
76 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 }
99
100 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
263 // 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 *
326 * This function is used to load a "view" file. It has three parameters:
327 *
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.
330 * 3. TRUE/FALSE - whether to return the data or load it. In
331 * 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 /**
392 * Load Helper
393 *
394 * This function loads the specified helper file.
395 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * @param mixed
397 * @return void
398 */
Greg Akerf5c84022011-04-19 17:13:03 -0500399 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200400 {
Derek Jones32bf1862010-03-02 13:46:07 -0600401 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200402 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 if (isset($this->_ci_helpers[$helper]))
404 {
405 continue;
406 }
Derek Jones32bf1862010-03-02 13:46:07 -0600407
Greg Aker3a746652011-04-19 10:59:47 -0500408 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000409
Barry Mienydd671972010-10-04 16:33:58 +0200410 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 if (file_exists($ext_helper))
412 {
Greg Aker3a746652011-04-19 10:59:47 -0500413 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 if ( ! file_exists($base_helper))
416 {
Greg Aker3a746652011-04-19 10:59:47 -0500417 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 include_once($ext_helper);
421 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Jones32bf1862010-03-02 13:46:07 -0600423 $this->_ci_helpers[$helper] = TRUE;
424 log_message('debug', 'Helper loaded: '.$helper);
425 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Jones32bf1862010-03-02 13:46:07 -0600428 // Try to load the helper
429 foreach ($this->_ci_helper_paths as $path)
430 {
Greg Aker3a746652011-04-19 10:59:47 -0500431 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200432 {
Greg Aker3a746652011-04-19 10:59:47 -0500433 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600434
435 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200436 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600437 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
439 }
440
Derek Jones32bf1862010-03-02 13:46:07 -0600441 // unable to load the helper
442 if ( ! isset($this->_ci_helpers[$helper]))
443 {
Greg Aker3a746652011-04-19 10:59:47 -0500444 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600445 }
Barry Mienydd671972010-10-04 16:33:58 +0200446 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 }
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 /**
452 * Load Helpers
453 *
454 * This is simply an alias to the above function in case the
455 * user has written the plural form of this function.
456 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 * @param array
458 * @return void
459 */
Greg Akerf5c84022011-04-19 17:13:03 -0500460 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 $this->helper($helpers);
463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 * Loads a language file
469 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @param array
471 * @param string
472 * @return void
473 */
Greg Akerf5c84022011-04-19 17:13:03 -0500474 public function language($file = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
476 $CI =& get_instance();
477
478 if ( ! is_array($file))
479 {
480 $file = array($file);
481 }
482
483 foreach ($file as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200484 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 $CI->lang->load($langfile, $lang);
486 }
487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 /**
492 * Loads a config file
493 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 * @param string
495 * @return void
496 */
Greg Akerf5c84022011-04-19 17:13:03 -0500497 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200498 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 $CI =& get_instance();
500 $CI->config->load($file, $use_sections, $fail_gracefully);
501 }
502
503 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 /**
Derek Jones8dca0412010-03-05 13:01:44 -0600506 * Driver
507 *
508 * Loads a driver library
509 *
510 * @param string the name of the class
511 * @param mixed the optional parameters
512 * @param string an optional object name
513 * @return void
514 */
Greg Akerf5c84022011-04-19 17:13:03 -0500515 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600516 {
517 if ( ! class_exists('CI_Driver_Library'))
518 {
519 // we aren't instantiating an object here, that'll be done by the Library itself
Greg Aker3a746652011-04-19 10:59:47 -0500520 require BASEPATH.'libraries/Driver.php';
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600523 // We can save the loader some time since Drivers will *always* be in a subfolder,
524 // and typically identically named to the library
525 if ( ! strpos($library, '/'))
526 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500527 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600528 }
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Jones8dca0412010-03-05 13:01:44 -0600530 return $this->library($library, $params, $object_name);
531 }
532
533 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Jones8dca0412010-03-05 13:01:44 -0600535 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600536 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 *
Derek Jones32bf1862010-03-02 13:46:07 -0600538 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 * @param string
Greg Akerf5c84022011-04-19 17:13:03 -0500541 * @param boolean
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600543 */
Greg Akerf5c84022011-04-19 17:13:03 -0500544 public function add_package_path($path, $view_cascade=TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600545 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500546 $path = rtrim($path, '/').'/';
Greg Akerf5c84022011-04-19 17:13:03 -0500547
Derek Jones32bf1862010-03-02 13:46:07 -0600548 array_unshift($this->_ci_library_paths, $path);
549 array_unshift($this->_ci_model_paths, $path);
550 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200551
Greg Akerf5c84022011-04-19 17:13:03 -0500552 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
553
Derek Jones32bf1862010-03-02 13:46:07 -0600554 // Add config file path
555 $config =& $this->_ci_get_component('config');
556 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 }
558
559 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600560
561 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000562 * Get Package Paths
563 *
564 * Return a list of all package paths, by default it will ignore BASEPATH.
565 *
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000566 * @param string
567 * @return void
568 */
Greg Akerf5c84022011-04-19 17:13:03 -0500569 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000570 {
571 return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
572 }
573
574 // --------------------------------------------------------------------
575
576 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600577 * Remove Package Path
578 *
579 * Remove a path from the library, model, and helper path arrays if it exists
580 * If no path is provided, the most recently added path is removed.
581 *
Derek Jones32bf1862010-03-02 13:46:07 -0600582 * @param type
583 * @return type
584 */
Greg Akerf5c84022011-04-19 17:13:03 -0500585 public function remove_package_path($path = '', $remove_config_path = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600586 {
587 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Jones32bf1862010-03-02 13:46:07 -0600589 if ($path == '')
590 {
591 $void = array_shift($this->_ci_library_paths);
592 $void = array_shift($this->_ci_model_paths);
593 $void = array_shift($this->_ci_helper_paths);
Greg Akerf5c84022011-04-19 17:13:03 -0500594 $void = array_shift($this->_ci_view_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600595 $void = array_shift($config->_config_paths);
596 }
597 else
598 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500599 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600600 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
601 {
602 if (($key = array_search($path, $this->{$var})) !== FALSE)
603 {
604 unset($this->{$var}[$key]);
605 }
606 }
Greg Akerf5c84022011-04-19 17:13:03 -0500607
608 if (isset($this->_ci_view_paths[$path.'views/']))
609 {
610 unset($this->_ci_view_paths[$path.'views/']);
611 }
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Jones32bf1862010-03-02 13:46:07 -0600613 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
614 {
615 unset($config->_config_paths[$key]);
616 }
617 }
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Jones32bf1862010-03-02 13:46:07 -0600619 // make sure the application default paths are still in the array
620 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
621 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
622 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500623 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600624 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
625 }
626
627 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 /**
630 * Loader
631 *
632 * This function is used to load views and files.
633 * Variables are prefixed with _ci_ to avoid symbol collision with
634 * variables made available to view files
635 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 * @param array
637 * @return void
638 */
Greg Akerf5c84022011-04-19 17:13:03 -0500639 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
641 // Set the default data variables
642 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
643 {
644 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
645 }
Greg Akerf5c84022011-04-19 17:13:03 -0500646
647 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000648
649 // Set the path to the requested file
650 if ($_ci_path == '')
651 {
652 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Greg Aker3a746652011-04-19 10:59:47 -0500653 $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500654
655 foreach ($this->_ci_view_paths as $view_file => $cascade)
656 {
657 if (file_exists($view_file.$_ci_file))
658 {
659 $_ci_path = $view_file.$_ci_file;
660 $file_exists = TRUE;
661 break;
662 }
663
664 if ( ! $cascade)
665 {
666 break;
667 }
668 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 }
670 else
671 {
672 $_ci_x = explode('/', $_ci_path);
673 $_ci_file = end($_ci_x);
674 }
Barry Mienydd671972010-10-04 16:33:58 +0200675
Greg Akerf5c84022011-04-19 17:13:03 -0500676 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 {
678 show_error('Unable to load the requested file: '.$_ci_file);
679 }
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 // This allows anything loaded using $this->load (views, files, etc.)
682 // to become accessible from within the Controller and Model functions.
Barry Mienydd671972010-10-04 16:33:58 +0200683
Pascal Kriete89ace432010-11-10 15:49:10 -0500684 $_ci_CI =& get_instance();
685 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500687 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500689 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 }
691 }
692
693 /*
694 * Extract and cache variables
695 *
696 * You can either set variables using the dedicated $this->load_vars()
697 * function or via the second parameter of this function. We'll merge
698 * the two types and cache them so that views that are embedded within
699 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200700 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 if (is_array($_ci_vars))
702 {
703 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
704 }
705 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 /*
708 * Buffer the output
709 *
710 * We buffer the output for two reasons:
711 * 1. Speed. You get a significant speed boost.
712 * 2. So that the final rendered template can be
713 * post-processed by the output class. Why do we
714 * need post processing? For one thing, in order to
715 * show the elapsed page load time. Unless we
716 * can intercept the content right before it's sent to
717 * the browser and then stop the timer it won't be accurate.
718 */
719 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200720
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 // If the PHP installation does not support short tags we'll
722 // do a little string replacement, changing the short tags
723 // to standard PHP echo statements.
Barry Mienydd671972010-10-04 16:33:58 +0200724
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
726 {
727 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
728 }
729 else
730 {
731 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
732 }
Barry Mienydd671972010-10-04 16:33:58 +0200733
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 // Return the file data if requested
737 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200738 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 $buffer = ob_get_contents();
740 @ob_end_clean();
741 return $buffer;
742 }
743
744 /*
745 * Flush the buffer... or buff the flusher?
746 *
747 * In order to permit views to be nested within
748 * other views, we need to flush the content back out whenever
749 * we are beyond the first level of output buffering so that
750 * it can be seen and included properly by the first included
751 * template and any subsequent ones. Oy!
752 *
Barry Mienydd671972010-10-04 16:33:58 +0200753 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 if (ob_get_level() > $this->_ci_ob_level + 1)
755 {
756 ob_end_flush();
757 }
758 else
759 {
Greg Aker22f1a632010-11-10 15:34:35 -0600760 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 @ob_end_clean();
762 }
763 }
764
765 // --------------------------------------------------------------------
766
767 /**
768 * Load class
769 *
770 * This function loads the requested class.
771 *
Barry Mienydd671972010-10-04 16:33:58 +0200772 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 * @param mixed any additional parameters
774 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200775 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 */
Greg Akerf5c84022011-04-19 17:13:03 -0500777 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200778 {
779 // Get the class name, and while we're at it trim any slashes.
780 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500782 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200783
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 // Was the path included with the class name?
785 // We look for a slash to determine this
786 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600787 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 {
Derek Jones32bf1862010-03-02 13:46:07 -0600789 // Extract the path
790 $subdir = substr($class, 0, $last_slash + 1);
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Jones32bf1862010-03-02 13:46:07 -0600792 // Get the filename from the path
793 $class = substr($class, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 }
795
796 // We'll test for both lowercase and capitalized versions of the file name
797 foreach (array(ucfirst($class), strtolower($class)) as $class)
798 {
Greg Aker3a746652011-04-19 10:59:47 -0500799 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000800
Barry Mienydd671972010-10-04 16:33:58 +0200801 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 if (file_exists($subclass))
803 {
Greg Aker3a746652011-04-19 10:59:47 -0500804 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200805
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 if ( ! file_exists($baseclass))
807 {
808 log_message('error', "Unable to load the requested class: ".$class);
809 show_error("Unable to load the requested class: ".$class);
810 }
811
812 // Safety: Was the class already loaded by a previous call?
813 if (in_array($subclass, $this->_ci_loaded_files))
814 {
815 // Before we deem this to be a duplicate request, let's see
816 // if a custom object name is being supplied. If so, we'll
817 // return a new instance of the object
818 if ( ! is_null($object_name))
819 {
820 $CI =& get_instance();
821 if ( ! isset($CI->$object_name))
822 {
Barry Mienydd671972010-10-04 16:33:58 +0200823 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 }
825 }
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 $is_duplicate = TRUE;
828 log_message('debug', $class." class already loaded. Second attempt ignored.");
829 return;
830 }
Barry Mienydd671972010-10-04 16:33:58 +0200831
832 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 include_once($subclass);
834 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200835
836 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 }
Barry Mienydd671972010-10-04 16:33:58 +0200838
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600840 $is_duplicate = FALSE;
841 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 {
Greg Aker3a746652011-04-19 10:59:47 -0500843 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600844
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 // Does the file exist? No? Bummer...
846 if ( ! file_exists($filepath))
847 {
848 continue;
849 }
Barry Mienydd671972010-10-04 16:33:58 +0200850
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 // Safety: Was the class already loaded by a previous call?
852 if (in_array($filepath, $this->_ci_loaded_files))
853 {
854 // Before we deem this to be a duplicate request, let's see
855 // if a custom object name is being supplied. If so, we'll
856 // return a new instance of the object
857 if ( ! is_null($object_name))
858 {
859 $CI =& get_instance();
860 if ( ! isset($CI->$object_name))
861 {
862 return $this->_ci_init_class($class, '', $params, $object_name);
863 }
864 }
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 $is_duplicate = TRUE;
867 log_message('debug', $class." class already loaded. Second attempt ignored.");
868 return;
869 }
Barry Mienydd671972010-10-04 16:33:58 +0200870
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 include_once($filepath);
872 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +0200873 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 }
Derek Jones32bf1862010-03-02 13:46:07 -0600875
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 } // END FOREACH
877
878 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
879 if ($subdir == '')
880 {
881 $path = strtolower($class).'/'.$class;
882 return $this->_ci_load_class($path, $params);
883 }
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 // If we got this far we were unable to find the requested class.
886 // We do not issue errors if the load call failed due to a duplicate request
887 if ($is_duplicate == FALSE)
888 {
889 log_message('error', "Unable to load the requested class: ".$class);
890 show_error("Unable to load the requested class: ".$class);
891 }
892 }
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 // --------------------------------------------------------------------
895
896 /**
897 * Instantiates a class
898 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 * @param string
900 * @param string
901 * @param string an optional object name
902 * @return null
903 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500904 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200905 {
Derek Jones32bf1862010-03-02 13:46:07 -0600906 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 if ($config === NULL)
908 {
Eric Barnes5e16ec62011-01-04 17:25:23 -0500909 // Fetch the config paths containing any package paths
910 $config_component = $this->_ci_get_component('config');
911
912 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
Eric Barnes5e16ec62011-01-04 17:25:23 -0500914 // Break on the first found file, thus package files
915 // are not overridden by default paths
916 foreach ($config_component->_config_paths as $path)
917 {
918 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +0100919 // are case-sensitive with regard to file names. Check for environment
920 // first, global next
Greg Aker3a746652011-04-19 10:59:47 -0500921 if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +0100922 {
Greg Aker3a746652011-04-19 10:59:47 -0500923 include_once($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +0100924 break;
925 }
Greg Aker3a746652011-04-19 10:59:47 -0500926 elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +0100927 {
Greg Aker3a746652011-04-19 10:59:47 -0500928 include_once($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +0100929 break;
930 }
Greg Aker3a746652011-04-19 10:59:47 -0500931 elseif (file_exists($path .'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -0500932 {
Greg Aker3a746652011-04-19 10:59:47 -0500933 include_once($path .'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -0500934 break;
935 }
Greg Aker3a746652011-04-19 10:59:47 -0500936 elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -0500937 {
Greg Aker3a746652011-04-19 10:59:47 -0500938 include_once($path .'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -0500939 break;
940 }
941 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 }
943 }
Barry Mienydd671972010-10-04 16:33:58 +0200944
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +0200946 {
947 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
949 $name = 'CI_'.$class;
950 }
Barry Mienydd671972010-10-04 16:33:58 +0200951 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
953 $name = config_item('subclass_prefix').$class;
954 }
955 else
956 {
957 $name = $class;
958 }
959 }
960 else
961 {
962 $name = $prefix.$class;
963 }
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 // Is the class name valid?
966 if ( ! class_exists($name))
967 {
968 log_message('error', "Non-existent class: ".$name);
969 show_error("Non-existent class: ".$class);
970 }
Barry Mienydd671972010-10-04 16:33:58 +0200971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 // Set the variable name we will assign the class to
973 // Was a custom class name supplied? If so we'll use it
974 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +0200975
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 if (is_null($object_name))
977 {
978 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
979 }
980 else
981 {
982 $classvar = $object_name;
983 }
984
Barry Mienydd671972010-10-04 16:33:58 +0200985 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 $this->_ci_classes[$class] = $classvar;
987
Barry Mienydd671972010-10-04 16:33:58 +0200988 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 $CI =& get_instance();
990 if ($config !== NULL)
991 {
992 $CI->$classvar = new $name($config);
993 }
994 else
Barry Mienydd671972010-10-04 16:33:58 +0200995 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 $CI->$classvar = new $name;
Barry Mienydd671972010-10-04 16:33:58 +0200997 }
998 }
999
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 /**
1003 * Autoloader
1004 *
1005 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -06001006 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 *
Greg Akerf5c84022011-04-19 17:13:03 -05001008 * This function is public, as it's used in the CI_Controller class.
1009 * However, there is no reason you should ever needs to use it.
1010 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 * @param array
1012 * @return void
1013 */
Greg Akerf5c84022011-04-19 17:13:03 -05001014 public function ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001015 {
Greg Aker3a746652011-04-19 10:59:47 -05001016 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001017 {
Greg Aker3a746652011-04-19 10:59:47 -05001018 include_once(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001019 }
1020 else
1021 {
Greg Aker3a746652011-04-19 10:59:47 -05001022 include_once(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001023 }
1024
Barry Mienydd671972010-10-04 16:33:58 +02001025
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 if ( ! isset($autoload))
1027 {
1028 return FALSE;
1029 }
Barry Mienydd671972010-10-04 16:33:58 +02001030
Phil Sturgeon9730c752010-12-15 10:50:15 +00001031 // Autoload packages
1032 if (isset($autoload['packages']))
1033 {
1034 foreach ($autoload['packages'] as $package_path)
1035 {
1036 $this->add_package_path($package_path);
1037 }
1038 }
1039
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 // Load any custom config file
1041 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001042 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 $CI =& get_instance();
1044 foreach ($autoload['config'] as $key => $val)
1045 {
1046 $CI->config->load($val);
1047 }
Barry Mienydd671972010-10-04 16:33:58 +02001048 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001049
Derek Jonesc6da5032010-03-09 20:44:27 -06001050 // Autoload helpers and languages
1051 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001052 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
1054 {
1055 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001056 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 }
1058
1059 // A little tweak to remain backward compatible
1060 // The $autoload['core'] item was deprecated
Derek Jones32bf1862010-03-02 13:46:07 -06001061 if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 {
1063 $autoload['libraries'] = $autoload['core'];
1064 }
Barry Mienydd671972010-10-04 16:33:58 +02001065
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 // Load libraries
1067 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
1068 {
1069 // Load the database driver.
1070 if (in_array('database', $autoload['libraries']))
1071 {
1072 $this->database();
1073 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1074 }
Barry Mienydd671972010-10-04 16:33:58 +02001075
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 // Load all other libraries
1077 foreach ($autoload['libraries'] as $item)
1078 {
1079 $this->library($item);
1080 }
Barry Mienydd671972010-10-04 16:33:58 +02001081 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001082
1083 // Autoload models
1084 if (isset($autoload['model']))
1085 {
1086 $this->model($autoload['model']);
1087 }
Barry Mienydd671972010-10-04 16:33:58 +02001088 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001089
1090 // --------------------------------------------------------------------
1091
1092 /**
1093 * Object to Array
1094 *
1095 * Takes an object as input and converts the class variables to array key/vals
1096 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 * @param object
1098 * @return array
1099 */
Greg Akerf5c84022011-04-19 17:13:03 -05001100 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 {
1102 return (is_object($object)) ? get_object_vars($object) : $object;
1103 }
1104
1105 // --------------------------------------------------------------------
1106
1107 /**
Derek Jones32bf1862010-03-02 13:46:07 -06001108 * Get a reference to a specific library or model
1109 *
Derek Jones32bf1862010-03-02 13:46:07 -06001110 * @return bool
1111 */
Greg Akerf5c84022011-04-19 17:13:03 -05001112 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001113 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001114 $CI =& get_instance();
1115 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001116 }
1117
1118 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001119
Derek Jones32bf1862010-03-02 13:46:07 -06001120 /**
1121 * Prep filename
1122 *
1123 * This function preps the name of various items to make loading them more reliable.
1124 *
Derek Jones32bf1862010-03-02 13:46:07 -06001125 * @param mixed
1126 * @return array
1127 */
Greg Akerf5c84022011-04-19 17:13:03 -05001128 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001129 {
1130 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001131 {
Greg Aker3a746652011-04-19 10:59:47 -05001132 return array(strtolower(str_replace('.php', '', str_replace($extension, '', $filename)).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001133 }
1134 else
1135 {
1136 foreach ($filename as $key => $val)
1137 {
Greg Aker3a746652011-04-19 10:59:47 -05001138 $filename[$key] = strtolower(str_replace('.php', '', str_replace($extension, '', $val)).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001139 }
Barry Mienydd671972010-10-04 16:33:58 +02001140
Derek Jones32bf1862010-03-02 13:46:07 -06001141 return $filename;
1142 }
1143 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001144}
1145
1146/* End of file Loader.php */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001147/* Location: ./system/core/Loader.php */