blob: 59415b72abedaaccc0c550b5414fe187102a9255 [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.
32 var $_ci_ob_level;
33 var $_ci_view_path = '';
Derek Jones32bf1862010-03-02 13:46:07 -060034 var $_ci_library_paths = array();
35 var $_ci_model_paths = array();
36 var $_ci_helper_paths = array();
Derek Jones32bf1862010-03-02 13:46:07 -060037 var $_base_classes = array(); // Set by the controller class
Derek Allard2067d1a2008-11-13 22:59:24 +000038 var $_ci_cached_vars = array();
39 var $_ci_classes = array();
40 var $_ci_loaded_files = array();
41 var $_ci_models = array();
42 var $_ci_helpers = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000043 var $_ci_varmap = array('unit_test' => 'unit', 'user_agent' => 'agent');
Barry Mienydd671972010-10-04 16:33:58 +020044
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
50 *
51 * @access public
52 */
Greg Akera9263282010-11-10 15:26:43 -060053 function __construct()
Barry Mienydd671972010-10-04 16:33:58 +020054 {
Derek Allard2067d1a2008-11-13 22:59:24 +000055 $this->_ci_view_path = APPPATH.'views/';
56 $this->_ci_ob_level = ob_get_level();
Derek Jones32bf1862010-03-02 13:46:07 -060057 $this->_ci_library_paths = array(APPPATH, BASEPATH);
58 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
59 $this->_ci_model_paths = array(APPPATH);
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 log_message('debug', "Loader Class Initialized");
62 }
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020065
Derek Allard2067d1a2008-11-13 22:59:24 +000066 /**
67 * Class Loader
68 *
69 * This function lets users load and instantiate classes.
70 * It is designed to be called from a user's app controllers.
71 *
72 * @access public
73 * @param string the name of the class
74 * @param mixed the optional parameters
75 * @param string an optional object name
76 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020077 */
Derek Allard2067d1a2008-11-13 22:59:24 +000078 function library($library = '', $params = NULL, $object_name = NULL)
79 {
Greg Akerce433962010-10-12 09:29:35 -050080 if (is_array($library))
81 {
Phil Sturgeon08b51692011-04-03 18:05:42 +010082 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -050083 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -060084 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -050085 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +000086
Greg Akerce433962010-10-12 09:29:35 -050087 return;
88 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +000089
Derek Jones32bf1862010-03-02 13:46:07 -060090 if ($library == '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
92 return FALSE;
93 }
94
Derek Jones32bf1862010-03-02 13:46:07 -060095 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +000096 {
97 $params = NULL;
98 }
99
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600100 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 }
102
103 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 /**
106 * Model Loader
107 *
108 * This function lets users load and instantiate models.
109 *
110 * @access public
111 * @param string the name of the class
112 * @param string name for the model
113 * @param bool database connection
114 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200115 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200117 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 if (is_array($model))
119 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500120 foreach ($model as $babe)
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
Barry Mienydd671972010-10-04 16:33:58 +0200122 $this->model($babe);
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 }
124 return;
125 }
126
127 if ($model == '')
128 {
129 return;
130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Jones32bf1862010-03-02 13:46:07 -0600132 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600135 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
Derek Jones32bf1862010-03-02 13:46:07 -0600137 // The path is in front of the last slash
138 $path = substr($model, 0, $last_slash + 1);
139
140 // And the model name behind it
141 $model = substr($model, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 if ($name == '')
145 {
146 $name = $model;
147 }
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 if (in_array($name, $this->_ci_models, TRUE))
150 {
151 return;
152 }
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 $CI =& get_instance();
155 if (isset($CI->$name))
156 {
157 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
158 }
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000161
Derek Jones32bf1862010-03-02 13:46:07 -0600162 foreach ($this->_ci_model_paths as $mod_path)
163 {
Greg Aker3a746652011-04-19 10:59:47 -0500164 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600165 {
166 continue;
167 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000168
Derek Jones32bf1862010-03-02 13:46:07 -0600169 if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
170 {
171 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500172 {
Derek Jones32bf1862010-03-02 13:46:07 -0600173 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500174 }
Derek Jones32bf1862010-03-02 13:46:07 -0600175
176 $CI->load->database($db_conn, FALSE, TRUE);
177 }
178
Greg Akerbce13482010-10-11 15:37:16 -0500179 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600180 {
181 load_class('Model', 'core');
182 }
183
Greg Aker3a746652011-04-19 10:59:47 -0500184 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600185
186 $model = ucfirst($model);
187
188 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600189
190 $this->_ci_models[] = $name;
191 return;
192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Jones32bf1862010-03-02 13:46:07 -0600194 // couldn't find the model
195 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 /**
201 * Database Loader
202 *
203 * @access public
204 * @param string the DB credentials
205 * @param bool whether to return the DB object
206 * @param bool whether to enable active record (this allows us to override the config setting)
207 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200208 */
Derek Jonesffca6c22009-12-05 15:31:44 +0000209 function database($params = '', $return = FALSE, $active_record = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
211 // Grab the super object
212 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 // Do we even need to load the database class?
Derek Jones9fb6dd12009-12-05 15:32:48 +0000215 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 +0000216 {
217 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200218 }
219
Greg Aker3a746652011-04-19 10:59:47 -0500220 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000221
222 if ($return === TRUE)
223 {
224 return DB($params, $active_record);
225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
227 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 // reference errors with some configurations
229 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 // Load the DB class
Barry Mienydd671972010-10-04 16:33:58 +0200232 $CI->db =& DB($params, $active_record);
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 // --------------------------------------------------------------------
236
237 /**
238 * Load the Utilities Class
239 *
240 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200241 * @return string
242 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 function dbutil()
244 {
245 if ( ! class_exists('CI_DB'))
246 {
247 $this->database();
248 }
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 $CI =& get_instance();
251
252 // for backwards compatibility, load dbforge so we can extend dbutils off it
253 // this use is deprecated and strongly discouraged
254 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200255
Greg Aker3a746652011-04-19 10:59:47 -0500256 require_once(BASEPATH.'database/DB_utility.php');
257 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
259
Pascal Kriete58560022010-11-10 16:01:20 -0500260 $CI->dbutil = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 // --------------------------------------------------------------------
264
265 /**
266 * Load the Database Forge Class
267 *
268 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200269 * @return string
270 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 function dbforge()
272 {
273 if ( ! class_exists('CI_DB'))
274 {
275 $this->database();
276 }
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200279
Greg Aker3a746652011-04-19 10:59:47 -0500280 require_once(BASEPATH.'database/DB_forge.php');
281 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
283
284 $CI->dbforge = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 }
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 /**
290 * Load View
291 *
292 * This function is used to load a "view" file. It has three parameters:
293 *
294 * 1. The name of the "view" file to be included.
295 * 2. An associative array of data to be extracted for use in the view.
296 * 3. TRUE/FALSE - whether to return the data or load it. In
297 * some cases it's advantageous to be able to return data so that
298 * a developer can process it in some way.
299 *
300 * @access public
301 * @param string
302 * @param array
303 * @param bool
304 * @return void
305 */
306 function view($view, $vars = array(), $return = FALSE)
307 {
308 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 /**
314 * Load File
315 *
316 * This is a generic file loader
317 *
318 * @access public
319 * @param string
320 * @param bool
321 * @return string
322 */
323 function file($path, $return = FALSE)
324 {
325 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 /**
331 * Set Variables
332 *
333 * Once variables are set they become available within
334 * the controller class and its "view" files.
335 *
336 * @access public
337 * @param array
338 * @return void
339 */
340 function vars($vars = array(), $val = '')
341 {
342 if ($val != '' AND is_string($vars))
343 {
344 $vars = array($vars => $val);
345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 if (is_array($vars) AND count($vars) > 0)
350 {
351 foreach ($vars as $key => $val)
352 {
353 $this->_ci_cached_vars[$key] = $val;
354 }
355 }
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 /**
361 * Load Helper
362 *
363 * This function loads the specified helper file.
364 *
365 * @access public
366 * @param mixed
367 * @return void
368 */
369 function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200370 {
Derek Jones32bf1862010-03-02 13:46:07 -0600371 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200372 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 if (isset($this->_ci_helpers[$helper]))
374 {
375 continue;
376 }
Derek Jones32bf1862010-03-02 13:46:07 -0600377
Greg Aker3a746652011-04-19 10:59:47 -0500378 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000379
Barry Mienydd671972010-10-04 16:33:58 +0200380 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 if (file_exists($ext_helper))
382 {
Greg Aker3a746652011-04-19 10:59:47 -0500383 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 if ( ! file_exists($base_helper))
386 {
Greg Aker3a746652011-04-19 10:59:47 -0500387 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 }
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 include_once($ext_helper);
391 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Jones32bf1862010-03-02 13:46:07 -0600393 $this->_ci_helpers[$helper] = TRUE;
394 log_message('debug', 'Helper loaded: '.$helper);
395 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Jones32bf1862010-03-02 13:46:07 -0600398 // Try to load the helper
399 foreach ($this->_ci_helper_paths as $path)
400 {
Greg Aker3a746652011-04-19 10:59:47 -0500401 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200402 {
Greg Aker3a746652011-04-19 10:59:47 -0500403 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600404
405 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200406 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600407 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
409 }
410
Derek Jones32bf1862010-03-02 13:46:07 -0600411 // unable to load the helper
412 if ( ! isset($this->_ci_helpers[$helper]))
413 {
Greg Aker3a746652011-04-19 10:59:47 -0500414 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 /**
422 * Load Helpers
423 *
424 * This is simply an alias to the above function in case the
425 * user has written the plural form of this function.
426 *
427 * @access public
428 * @param array
429 * @return void
430 */
431 function helpers($helpers = array())
432 {
433 $this->helper($helpers);
434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 * Loads a language file
440 *
441 * @access public
442 * @param array
443 * @param string
444 * @return void
445 */
446 function language($file = array(), $lang = '')
447 {
448 $CI =& get_instance();
449
450 if ( ! is_array($file))
451 {
452 $file = array($file);
453 }
454
455 foreach ($file as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200456 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 $CI->lang->load($langfile, $lang);
458 }
459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 /**
464 * Loads a config file
465 *
466 * @access public
467 * @param string
468 * @return void
469 */
470 function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200471 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 $CI =& get_instance();
473 $CI->config->load($file, $use_sections, $fail_gracefully);
474 }
475
476 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 /**
Derek Jones8dca0412010-03-05 13:01:44 -0600479 * Driver
480 *
481 * Loads a driver library
482 *
483 * @param string the name of the class
484 * @param mixed the optional parameters
485 * @param string an optional object name
486 * @return void
487 */
488 function driver($library = '', $params = NULL, $object_name = NULL)
489 {
490 if ( ! class_exists('CI_Driver_Library'))
491 {
492 // we aren't instantiating an object here, that'll be done by the Library itself
Greg Aker3a746652011-04-19 10:59:47 -0500493 require BASEPATH.'libraries/Driver.php';
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600496 // We can save the loader some time since Drivers will *always* be in a subfolder,
497 // and typically identically named to the library
498 if ( ! strpos($library, '/'))
499 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500500 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Jones8dca0412010-03-05 13:01:44 -0600503 return $this->library($library, $params, $object_name);
504 }
505
506 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Jones8dca0412010-03-05 13:01:44 -0600508 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600509 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 *
Derek Jones32bf1862010-03-02 13:46:07 -0600511 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 *
513 * @access public
514 * @param string
515 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600516 */
517 function add_package_path($path)
518 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500519 $path = rtrim($path, '/').'/';
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000520
Derek Jones32bf1862010-03-02 13:46:07 -0600521 array_unshift($this->_ci_library_paths, $path);
522 array_unshift($this->_ci_model_paths, $path);
523 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Jones32bf1862010-03-02 13:46:07 -0600525 // Add config file path
526 $config =& $this->_ci_get_component('config');
527 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
529
530 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600531
532 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000533 * Get Package Paths
534 *
535 * Return a list of all package paths, by default it will ignore BASEPATH.
536 *
537 * @access public
538 * @param string
539 * @return void
540 */
541 function get_package_paths($include_base = FALSE)
542 {
543 return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
544 }
545
546 // --------------------------------------------------------------------
547
548 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600549 * Remove Package Path
550 *
551 * Remove a path from the library, model, and helper path arrays if it exists
552 * If no path is provided, the most recently added path is removed.
553 *
554 * @access public
555 * @param type
556 * @return type
557 */
558 function remove_package_path($path = '', $remove_config_path = TRUE)
559 {
560 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Jones32bf1862010-03-02 13:46:07 -0600562 if ($path == '')
563 {
564 $void = array_shift($this->_ci_library_paths);
565 $void = array_shift($this->_ci_model_paths);
566 $void = array_shift($this->_ci_helper_paths);
567 $void = array_shift($config->_config_paths);
568 }
569 else
570 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500571 $path = rtrim($path, '/').'/';
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000572
Derek Jones32bf1862010-03-02 13:46:07 -0600573 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
574 {
575 if (($key = array_search($path, $this->{$var})) !== FALSE)
576 {
577 unset($this->{$var}[$key]);
578 }
579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Jones32bf1862010-03-02 13:46:07 -0600581 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
582 {
583 unset($config->_config_paths[$key]);
584 }
585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Jones32bf1862010-03-02 13:46:07 -0600587 // make sure the application default paths are still in the array
588 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
589 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
590 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
591 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
592 }
593
594 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 /**
597 * Loader
598 *
599 * This function is used to load views and files.
600 * Variables are prefixed with _ci_ to avoid symbol collision with
601 * variables made available to view files
602 *
603 * @access private
604 * @param array
605 * @return void
606 */
607 function _ci_load($_ci_data)
608 {
609 // Set the default data variables
610 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
611 {
612 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
613 }
614
615 // Set the path to the requested file
616 if ($_ci_path == '')
617 {
618 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Greg Aker3a746652011-04-19 10:59:47 -0500619 $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 $_ci_path = $this->_ci_view_path.$_ci_file;
621 }
622 else
623 {
624 $_ci_x = explode('/', $_ci_path);
625 $_ci_file = end($_ci_x);
626 }
Barry Mienydd671972010-10-04 16:33:58 +0200627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 if ( ! file_exists($_ci_path))
629 {
630 show_error('Unable to load the requested file: '.$_ci_file);
631 }
Barry Mienydd671972010-10-04 16:33:58 +0200632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 // This allows anything loaded using $this->load (views, files, etc.)
634 // to become accessible from within the Controller and Model functions.
Barry Mienydd671972010-10-04 16:33:58 +0200635
Pascal Kriete89ace432010-11-10 15:49:10 -0500636 $_ci_CI =& get_instance();
637 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500639 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500641 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 }
643 }
644
645 /*
646 * Extract and cache variables
647 *
648 * You can either set variables using the dedicated $this->load_vars()
649 * function or via the second parameter of this function. We'll merge
650 * the two types and cache them so that views that are embedded within
651 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200652 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 if (is_array($_ci_vars))
654 {
655 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
656 }
657 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 /*
660 * Buffer the output
661 *
662 * We buffer the output for two reasons:
663 * 1. Speed. You get a significant speed boost.
664 * 2. So that the final rendered template can be
665 * post-processed by the output class. Why do we
666 * need post processing? For one thing, in order to
667 * show the elapsed page load time. Unless we
668 * can intercept the content right before it's sent to
669 * the browser and then stop the timer it won't be accurate.
670 */
671 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 // If the PHP installation does not support short tags we'll
674 // do a little string replacement, changing the short tags
675 // to standard PHP echo statements.
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
678 {
679 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
680 }
681 else
682 {
683 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
684 }
Barry Mienydd671972010-10-04 16:33:58 +0200685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200687
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 // Return the file data if requested
689 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200690 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 $buffer = ob_get_contents();
692 @ob_end_clean();
693 return $buffer;
694 }
695
696 /*
697 * Flush the buffer... or buff the flusher?
698 *
699 * In order to permit views to be nested within
700 * other views, we need to flush the content back out whenever
701 * we are beyond the first level of output buffering so that
702 * it can be seen and included properly by the first included
703 * template and any subsequent ones. Oy!
704 *
Barry Mienydd671972010-10-04 16:33:58 +0200705 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 if (ob_get_level() > $this->_ci_ob_level + 1)
707 {
708 ob_end_flush();
709 }
710 else
711 {
Greg Aker22f1a632010-11-10 15:34:35 -0600712 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 @ob_end_clean();
714 }
715 }
716
717 // --------------------------------------------------------------------
718
719 /**
720 * Load class
721 *
722 * This function loads the requested class.
723 *
724 * @access private
Barry Mienydd671972010-10-04 16:33:58 +0200725 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 * @param mixed any additional parameters
727 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200728 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 */
730 function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200731 {
732 // Get the class name, and while we're at it trim any slashes.
733 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500735 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200736
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 // Was the path included with the class name?
738 // We look for a slash to determine this
739 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600740 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 {
Derek Jones32bf1862010-03-02 13:46:07 -0600742 // Extract the path
743 $subdir = substr($class, 0, $last_slash + 1);
Barry Mienydd671972010-10-04 16:33:58 +0200744
Derek Jones32bf1862010-03-02 13:46:07 -0600745 // Get the filename from the path
746 $class = substr($class, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 }
748
749 // We'll test for both lowercase and capitalized versions of the file name
750 foreach (array(ucfirst($class), strtolower($class)) as $class)
751 {
Greg Aker3a746652011-04-19 10:59:47 -0500752 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000753
Barry Mienydd671972010-10-04 16:33:58 +0200754 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 if (file_exists($subclass))
756 {
Greg Aker3a746652011-04-19 10:59:47 -0500757 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200758
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 if ( ! file_exists($baseclass))
760 {
761 log_message('error', "Unable to load the requested class: ".$class);
762 show_error("Unable to load the requested class: ".$class);
763 }
764
765 // Safety: Was the class already loaded by a previous call?
766 if (in_array($subclass, $this->_ci_loaded_files))
767 {
768 // Before we deem this to be a duplicate request, let's see
769 // if a custom object name is being supplied. If so, we'll
770 // return a new instance of the object
771 if ( ! is_null($object_name))
772 {
773 $CI =& get_instance();
774 if ( ! isset($CI->$object_name))
775 {
Barry Mienydd671972010-10-04 16:33:58 +0200776 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 }
778 }
Barry Mienydd671972010-10-04 16:33:58 +0200779
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 $is_duplicate = TRUE;
781 log_message('debug', $class." class already loaded. Second attempt ignored.");
782 return;
783 }
Barry Mienydd671972010-10-04 16:33:58 +0200784
785 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 include_once($subclass);
787 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200788
789 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 }
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600793 $is_duplicate = FALSE;
794 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 {
Greg Aker3a746652011-04-19 10:59:47 -0500796 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600797
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 // Does the file exist? No? Bummer...
799 if ( ! file_exists($filepath))
800 {
801 continue;
802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 // Safety: Was the class already loaded by a previous call?
805 if (in_array($filepath, $this->_ci_loaded_files))
806 {
807 // Before we deem this to be a duplicate request, let's see
808 // if a custom object name is being supplied. If so, we'll
809 // return a new instance of the object
810 if ( ! is_null($object_name))
811 {
812 $CI =& get_instance();
813 if ( ! isset($CI->$object_name))
814 {
815 return $this->_ci_init_class($class, '', $params, $object_name);
816 }
817 }
Barry Mienydd671972010-10-04 16:33:58 +0200818
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 $is_duplicate = TRUE;
820 log_message('debug', $class." class already loaded. Second attempt ignored.");
821 return;
822 }
Barry Mienydd671972010-10-04 16:33:58 +0200823
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 include_once($filepath);
825 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +0200826 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 }
Derek Jones32bf1862010-03-02 13:46:07 -0600828
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 } // END FOREACH
830
831 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
832 if ($subdir == '')
833 {
834 $path = strtolower($class).'/'.$class;
835 return $this->_ci_load_class($path, $params);
836 }
Barry Mienydd671972010-10-04 16:33:58 +0200837
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 // If we got this far we were unable to find the requested class.
839 // We do not issue errors if the load call failed due to a duplicate request
840 if ($is_duplicate == FALSE)
841 {
842 log_message('error', "Unable to load the requested class: ".$class);
843 show_error("Unable to load the requested class: ".$class);
844 }
845 }
Barry Mienydd671972010-10-04 16:33:58 +0200846
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 // --------------------------------------------------------------------
848
849 /**
850 * Instantiates a class
851 *
852 * @access private
853 * @param string
854 * @param string
855 * @param string an optional object name
856 * @return null
857 */
858 function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200859 {
Derek Jones32bf1862010-03-02 13:46:07 -0600860 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 if ($config === NULL)
862 {
Eric Barnes5e16ec62011-01-04 17:25:23 -0500863 // Fetch the config paths containing any package paths
864 $config_component = $this->_ci_get_component('config');
865
866 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 {
Eric Barnes5e16ec62011-01-04 17:25:23 -0500868 // Break on the first found file, thus package files
869 // are not overridden by default paths
870 foreach ($config_component->_config_paths as $path)
871 {
872 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +0100873 // are case-sensitive with regard to file names. Check for environment
874 // first, global next
Greg Aker3a746652011-04-19 10:59:47 -0500875 if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +0100876 {
Greg Aker3a746652011-04-19 10:59:47 -0500877 include_once($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +0100878 break;
879 }
Greg Aker3a746652011-04-19 10:59:47 -0500880 elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +0100881 {
Greg Aker3a746652011-04-19 10:59:47 -0500882 include_once($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +0100883 break;
884 }
Greg Aker3a746652011-04-19 10:59:47 -0500885 elseif (file_exists($path .'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -0500886 {
Greg Aker3a746652011-04-19 10:59:47 -0500887 include_once($path .'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -0500888 break;
889 }
Greg Aker3a746652011-04-19 10:59:47 -0500890 elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -0500891 {
Greg Aker3a746652011-04-19 10:59:47 -0500892 include_once($path .'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -0500893 break;
894 }
895 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 }
897 }
Barry Mienydd671972010-10-04 16:33:58 +0200898
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +0200900 {
901 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 {
903 $name = 'CI_'.$class;
904 }
Barry Mienydd671972010-10-04 16:33:58 +0200905 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 {
907 $name = config_item('subclass_prefix').$class;
908 }
909 else
910 {
911 $name = $class;
912 }
913 }
914 else
915 {
916 $name = $prefix.$class;
917 }
Barry Mienydd671972010-10-04 16:33:58 +0200918
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 // Is the class name valid?
920 if ( ! class_exists($name))
921 {
922 log_message('error', "Non-existent class: ".$name);
923 show_error("Non-existent class: ".$class);
924 }
Barry Mienydd671972010-10-04 16:33:58 +0200925
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 // Set the variable name we will assign the class to
927 // Was a custom class name supplied? If so we'll use it
928 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +0200929
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 if (is_null($object_name))
931 {
932 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
933 }
934 else
935 {
936 $classvar = $object_name;
937 }
938
Barry Mienydd671972010-10-04 16:33:58 +0200939 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 $this->_ci_classes[$class] = $classvar;
941
Barry Mienydd671972010-10-04 16:33:58 +0200942 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 $CI =& get_instance();
944 if ($config !== NULL)
945 {
946 $CI->$classvar = new $name($config);
947 }
948 else
Barry Mienydd671972010-10-04 16:33:58 +0200949 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 $CI->$classvar = new $name;
Barry Mienydd671972010-10-04 16:33:58 +0200951 }
952 }
953
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200955
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 /**
957 * Autoloader
958 *
959 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -0600960 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 *
962 * @access private
963 * @param array
964 * @return void
965 */
966 function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +0200967 {
Greg Aker3a746652011-04-19 10:59:47 -0500968 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -0500969 {
Greg Aker3a746652011-04-19 10:59:47 -0500970 include_once(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -0500971 }
972 else
973 {
Greg Aker3a746652011-04-19 10:59:47 -0500974 include_once(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -0500975 }
976
Barry Mienydd671972010-10-04 16:33:58 +0200977
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 if ( ! isset($autoload))
979 {
980 return FALSE;
981 }
Barry Mienydd671972010-10-04 16:33:58 +0200982
Phil Sturgeon9730c752010-12-15 10:50:15 +0000983 // Autoload packages
984 if (isset($autoload['packages']))
985 {
986 foreach ($autoload['packages'] as $package_path)
987 {
988 $this->add_package_path($package_path);
989 }
990 }
991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 // Load any custom config file
993 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200994 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 $CI =& get_instance();
996 foreach ($autoload['config'] as $key => $val)
997 {
998 $CI->config->load($val);
999 }
Barry Mienydd671972010-10-04 16:33:58 +02001000 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001001
Derek Jonesc6da5032010-03-09 20:44:27 -06001002 // Autoload helpers and languages
1003 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001004 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
1006 {
1007 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001008 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 }
1010
1011 // A little tweak to remain backward compatible
1012 // The $autoload['core'] item was deprecated
Derek Jones32bf1862010-03-02 13:46:07 -06001013 if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 {
1015 $autoload['libraries'] = $autoload['core'];
1016 }
Barry Mienydd671972010-10-04 16:33:58 +02001017
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 // Load libraries
1019 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
1020 {
1021 // Load the database driver.
1022 if (in_array('database', $autoload['libraries']))
1023 {
1024 $this->database();
1025 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1026 }
Barry Mienydd671972010-10-04 16:33:58 +02001027
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 // Load all other libraries
1029 foreach ($autoload['libraries'] as $item)
1030 {
1031 $this->library($item);
1032 }
Barry Mienydd671972010-10-04 16:33:58 +02001033 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001034
1035 // Autoload models
1036 if (isset($autoload['model']))
1037 {
1038 $this->model($autoload['model']);
1039 }
Barry Mienydd671972010-10-04 16:33:58 +02001040 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001041
1042 // --------------------------------------------------------------------
1043
1044 /**
1045 * Object to Array
1046 *
1047 * Takes an object as input and converts the class variables to array key/vals
1048 *
1049 * @access private
1050 * @param object
1051 * @return array
1052 */
1053 function _ci_object_to_array($object)
1054 {
1055 return (is_object($object)) ? get_object_vars($object) : $object;
1056 }
1057
1058 // --------------------------------------------------------------------
1059
1060 /**
Derek Jones32bf1862010-03-02 13:46:07 -06001061 * Get a reference to a specific library or model
1062 *
1063 * @access private
1064 * @return bool
1065 */
1066 function &_ci_get_component($component)
1067 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001068 $CI =& get_instance();
1069 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001070 }
1071
1072 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001073
Derek Jones32bf1862010-03-02 13:46:07 -06001074 /**
1075 * Prep filename
1076 *
1077 * This function preps the name of various items to make loading them more reliable.
1078 *
1079 * @access private
1080 * @param mixed
1081 * @return array
1082 */
1083 function _ci_prep_filename($filename, $extension)
1084 {
1085 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001086 {
Greg Aker3a746652011-04-19 10:59:47 -05001087 return array(strtolower(str_replace('.php', '', str_replace($extension, '', $filename)).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001088 }
1089 else
1090 {
1091 foreach ($filename as $key => $val)
1092 {
Greg Aker3a746652011-04-19 10:59:47 -05001093 $filename[$key] = strtolower(str_replace('.php', '', str_replace($extension, '', $val)).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001094 }
Barry Mienydd671972010-10-04 16:33:58 +02001095
Derek Jones32bf1862010-03-02 13:46:07 -06001096 return $filename;
1097 }
1098 }
Barry Mienydd671972010-10-04 16:33:58 +02001099
Derek Allard2067d1a2008-11-13 22:59:24 +00001100
1101}
1102
1103/* End of file Loader.php */
Phil Sturgeona36f6372011-04-01 11:40:09 +01001104/* Location: ./system/core/Loader.php */