blob: 8146cd5631fbc7018424a9a44e34a9287a46fb9b [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;
Greg Akerf5c84022011-04-19 17:13:03 -050033 var $_ci_view_paths = array();
34 protected $_ci_library_paths = array();
Derek Jones32bf1862010-03-02 13:46:07 -060035 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
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 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 /**
65 * Class Loader
66 *
67 * This function lets users load and instantiate classes.
68 * It is designed to be called from a user's app controllers.
69 *
70 * @access public
71 * @param string the name of the class
72 * @param mixed the optional parameters
73 * @param string an optional object name
74 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020075 */
Derek Allard2067d1a2008-11-13 22:59:24 +000076 function library($library = '', $params = NULL, $object_name = NULL)
77 {
Greg Akerce433962010-10-12 09:29:35 -050078 if (is_array($library))
79 {
Phil Sturgeon08b51692011-04-03 18:05:42 +010080 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -050081 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -060082 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -050083 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +000084
Greg Akerce433962010-10-12 09:29:35 -050085 return;
86 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +000087
Derek Jones32bf1862010-03-02 13:46:07 -060088 if ($library == '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
90 return FALSE;
91 }
92
Derek Jones32bf1862010-03-02 13:46:07 -060093 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
95 $params = NULL;
96 }
97
Kellas Reeves3c6e4852011-02-09 11:57:56 -060098 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +000099 }
100
101 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 /**
104 * Model Loader
105 *
106 * This function lets users load and instantiate models.
107 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 * @param string the name of the class
109 * @param string name for the model
110 * @param bool database connection
111 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200112 */
Greg Akerf5c84022011-04-19 17:13:03 -0500113 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200114 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 if (is_array($model))
116 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500117 foreach ($model as $babe)
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Barry Mienydd671972010-10-04 16:33:58 +0200119 $this->model($babe);
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 }
121 return;
122 }
123
124 if ($model == '')
125 {
126 return;
127 }
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Jones32bf1862010-03-02 13:46:07 -0600129 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600132 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
Derek Jones32bf1862010-03-02 13:46:07 -0600134 // The path is in front of the last slash
135 $path = substr($model, 0, $last_slash + 1);
136
137 // And the model name behind it
138 $model = substr($model, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 if ($name == '')
142 {
143 $name = $model;
144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 if (in_array($name, $this->_ci_models, TRUE))
147 {
148 return;
149 }
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 $CI =& get_instance();
152 if (isset($CI->$name))
153 {
154 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
155 }
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000158
Derek Jones32bf1862010-03-02 13:46:07 -0600159 foreach ($this->_ci_model_paths as $mod_path)
160 {
Greg Aker3a746652011-04-19 10:59:47 -0500161 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600162 {
163 continue;
164 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000165
Derek Jones32bf1862010-03-02 13:46:07 -0600166 if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
167 {
168 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500169 {
Derek Jones32bf1862010-03-02 13:46:07 -0600170 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500171 }
Derek Jones32bf1862010-03-02 13:46:07 -0600172
173 $CI->load->database($db_conn, FALSE, TRUE);
174 }
175
Greg Akerbce13482010-10-11 15:37:16 -0500176 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600177 {
178 load_class('Model', 'core');
179 }
180
Greg Aker3a746652011-04-19 10:59:47 -0500181 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600182
183 $model = ucfirst($model);
184
185 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600186
187 $this->_ci_models[] = $name;
188 return;
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Jones32bf1862010-03-02 13:46:07 -0600191 // couldn't find the model
192 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 }
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 /**
198 * Database Loader
199 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 * @param string the DB credentials
201 * @param bool whether to return the DB object
202 * @param bool whether to enable active record (this allows us to override the config setting)
203 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200204 */
Greg Akerf5c84022011-04-19 17:13:03 -0500205 public function database($params = '', $return = FALSE, $active_record = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
207 // Grab the super object
208 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 // Do we even need to load the database class?
Derek Jones9fb6dd12009-12-05 15:32:48 +0000211 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 +0000212 {
213 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200214 }
215
Greg Aker3a746652011-04-19 10:59:47 -0500216 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000217
218 if ($return === TRUE)
219 {
220 return DB($params, $active_record);
221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
223 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // reference errors with some configurations
225 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // Load the DB class
Barry Mienydd671972010-10-04 16:33:58 +0200228 $CI->db =& DB($params, $active_record);
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 // --------------------------------------------------------------------
232
233 /**
234 * Load the Utilities Class
235 *
Barry Mienydd671972010-10-04 16:33:58 +0200236 * @return string
237 */
Greg Akerf5c84022011-04-19 17:13:03 -0500238 public function dbutil()
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
240 if ( ! class_exists('CI_DB'))
241 {
242 $this->database();
243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 $CI =& get_instance();
246
247 // for backwards compatibility, load dbforge so we can extend dbutils off it
248 // this use is deprecated and strongly discouraged
249 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200250
Greg Aker3a746652011-04-19 10:59:47 -0500251 require_once(BASEPATH.'database/DB_utility.php');
252 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
254
Pascal Kriete58560022010-11-10 16:01:20 -0500255 $CI->dbutil = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // --------------------------------------------------------------------
259
260 /**
261 * Load the Database Forge Class
262 *
Barry Mienydd671972010-10-04 16:33:58 +0200263 * @return string
264 */
Greg Akerf5c84022011-04-19 17:13:03 -0500265 public function dbforge()
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 {
267 if ( ! class_exists('CI_DB'))
268 {
269 $this->database();
270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200273
Greg Aker3a746652011-04-19 10:59:47 -0500274 require_once(BASEPATH.'database/DB_forge.php');
275 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
277
278 $CI->dbforge = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 /**
284 * Load View
285 *
286 * This function is used to load a "view" file. It has three parameters:
287 *
288 * 1. The name of the "view" file to be included.
289 * 2. An associative array of data to be extracted for use in the view.
290 * 3. TRUE/FALSE - whether to return the data or load it. In
291 * some cases it's advantageous to be able to return data so that
292 * a developer can process it in some way.
293 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 * @param string
295 * @param array
296 * @param bool
297 * @return void
298 */
Greg Akerf5c84022011-04-19 17:13:03 -0500299 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
301 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
302 }
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 /**
307 * Load File
308 *
309 * This is a generic file loader
310 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 * @param string
312 * @param bool
313 * @return string
314 */
Greg Akerf5c84022011-04-19 17:13:03 -0500315 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
317 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 /**
323 * Set Variables
324 *
325 * Once variables are set they become available within
326 * the controller class and its "view" files.
327 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * @param array
329 * @return void
330 */
Greg Akerf5c84022011-04-19 17:13:03 -0500331 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
333 if ($val != '' AND is_string($vars))
334 {
335 $vars = array($vars => $val);
336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 if (is_array($vars) AND count($vars) > 0)
341 {
342 foreach ($vars as $key => $val)
343 {
344 $this->_ci_cached_vars[$key] = $val;
345 }
346 }
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 /**
352 * Load Helper
353 *
354 * This function loads the specified helper file.
355 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @param mixed
357 * @return void
358 */
Greg Akerf5c84022011-04-19 17:13:03 -0500359 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200360 {
Derek Jones32bf1862010-03-02 13:46:07 -0600361 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200362 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 if (isset($this->_ci_helpers[$helper]))
364 {
365 continue;
366 }
Derek Jones32bf1862010-03-02 13:46:07 -0600367
Greg Aker3a746652011-04-19 10:59:47 -0500368 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000369
Barry Mienydd671972010-10-04 16:33:58 +0200370 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 if (file_exists($ext_helper))
372 {
Greg Aker3a746652011-04-19 10:59:47 -0500373 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 if ( ! file_exists($base_helper))
376 {
Greg Aker3a746652011-04-19 10:59:47 -0500377 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 }
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 include_once($ext_helper);
381 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Jones32bf1862010-03-02 13:46:07 -0600383 $this->_ci_helpers[$helper] = TRUE;
384 log_message('debug', 'Helper loaded: '.$helper);
385 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Jones32bf1862010-03-02 13:46:07 -0600388 // Try to load the helper
389 foreach ($this->_ci_helper_paths as $path)
390 {
Greg Aker3a746652011-04-19 10:59:47 -0500391 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200392 {
Greg Aker3a746652011-04-19 10:59:47 -0500393 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600394
395 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200396 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600397 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
399 }
400
Derek Jones32bf1862010-03-02 13:46:07 -0600401 // unable to load the helper
402 if ( ! isset($this->_ci_helpers[$helper]))
403 {
Greg Aker3a746652011-04-19 10:59:47 -0500404 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 /**
412 * Load Helpers
413 *
414 * This is simply an alias to the above function in case the
415 * user has written the plural form of this function.
416 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 * @param array
418 * @return void
419 */
Greg Akerf5c84022011-04-19 17:13:03 -0500420 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
422 $this->helper($helpers);
423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * Loads a language file
429 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @param array
431 * @param string
432 * @return void
433 */
Greg Akerf5c84022011-04-19 17:13:03 -0500434 public function language($file = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
436 $CI =& get_instance();
437
438 if ( ! is_array($file))
439 {
440 $file = array($file);
441 }
442
443 foreach ($file as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200444 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 $CI->lang->load($langfile, $lang);
446 }
447 }
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 * Loads a config file
453 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 * @param string
455 * @return void
456 */
Greg Akerf5c84022011-04-19 17:13:03 -0500457 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200458 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 $CI =& get_instance();
460 $CI->config->load($file, $use_sections, $fail_gracefully);
461 }
462
463 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 /**
Derek Jones8dca0412010-03-05 13:01:44 -0600466 * Driver
467 *
468 * Loads a driver library
469 *
470 * @param string the name of the class
471 * @param mixed the optional parameters
472 * @param string an optional object name
473 * @return void
474 */
Greg Akerf5c84022011-04-19 17:13:03 -0500475 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600476 {
477 if ( ! class_exists('CI_Driver_Library'))
478 {
479 // we aren't instantiating an object here, that'll be done by the Library itself
Greg Aker3a746652011-04-19 10:59:47 -0500480 require BASEPATH.'libraries/Driver.php';
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600483 // We can save the loader some time since Drivers will *always* be in a subfolder,
484 // and typically identically named to the library
485 if ( ! strpos($library, '/'))
486 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500487 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600488 }
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Jones8dca0412010-03-05 13:01:44 -0600490 return $this->library($library, $params, $object_name);
491 }
492
493 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Jones8dca0412010-03-05 13:01:44 -0600495 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600496 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 *
Derek Jones32bf1862010-03-02 13:46:07 -0600498 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 * @param string
Greg Akerf5c84022011-04-19 17:13:03 -0500501 * @param boolean
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600503 */
Greg Akerf5c84022011-04-19 17:13:03 -0500504 public function add_package_path($path, $view_cascade=TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600505 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500506 $path = rtrim($path, '/').'/';
Greg Akerf5c84022011-04-19 17:13:03 -0500507
Derek Jones32bf1862010-03-02 13:46:07 -0600508 array_unshift($this->_ci_library_paths, $path);
509 array_unshift($this->_ci_model_paths, $path);
510 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200511
Greg Akerf5c84022011-04-19 17:13:03 -0500512 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
513
Derek Jones32bf1862010-03-02 13:46:07 -0600514 // Add config file path
515 $config =& $this->_ci_get_component('config');
516 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 }
518
519 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600520
521 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000522 * Get Package Paths
523 *
524 * Return a list of all package paths, by default it will ignore BASEPATH.
525 *
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000526 * @param string
527 * @return void
528 */
Greg Akerf5c84022011-04-19 17:13:03 -0500529 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000530 {
531 return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
532 }
533
534 // --------------------------------------------------------------------
535
536 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600537 * Remove Package Path
538 *
539 * Remove a path from the library, model, and helper path arrays if it exists
540 * If no path is provided, the most recently added path is removed.
541 *
Derek Jones32bf1862010-03-02 13:46:07 -0600542 * @param type
543 * @return type
544 */
Greg Akerf5c84022011-04-19 17:13:03 -0500545 public function remove_package_path($path = '', $remove_config_path = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600546 {
547 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200548
Derek Jones32bf1862010-03-02 13:46:07 -0600549 if ($path == '')
550 {
551 $void = array_shift($this->_ci_library_paths);
552 $void = array_shift($this->_ci_model_paths);
553 $void = array_shift($this->_ci_helper_paths);
Greg Akerf5c84022011-04-19 17:13:03 -0500554 $void = array_shift($this->_ci_view_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600555 $void = array_shift($config->_config_paths);
556 }
557 else
558 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500559 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600560 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
561 {
562 if (($key = array_search($path, $this->{$var})) !== FALSE)
563 {
564 unset($this->{$var}[$key]);
565 }
566 }
Greg Akerf5c84022011-04-19 17:13:03 -0500567
568 if (isset($this->_ci_view_paths[$path.'views/']))
569 {
570 unset($this->_ci_view_paths[$path.'views/']);
571 }
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Jones32bf1862010-03-02 13:46:07 -0600573 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
574 {
575 unset($config->_config_paths[$key]);
576 }
577 }
Barry Mienydd671972010-10-04 16:33:58 +0200578
Derek Jones32bf1862010-03-02 13:46:07 -0600579 // make sure the application default paths are still in the array
580 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
581 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
582 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500583 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600584 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
585 }
586
587 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 /**
590 * Loader
591 *
592 * This function is used to load views and files.
593 * Variables are prefixed with _ci_ to avoid symbol collision with
594 * variables made available to view files
595 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 * @param array
597 * @return void
598 */
Greg Akerf5c84022011-04-19 17:13:03 -0500599 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
601 // Set the default data variables
602 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
603 {
604 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
605 }
Greg Akerf5c84022011-04-19 17:13:03 -0500606
607 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000608
609 // Set the path to the requested file
610 if ($_ci_path == '')
611 {
612 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Greg Aker3a746652011-04-19 10:59:47 -0500613 $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500614
615 foreach ($this->_ci_view_paths as $view_file => $cascade)
616 {
617 if (file_exists($view_file.$_ci_file))
618 {
619 $_ci_path = $view_file.$_ci_file;
620 $file_exists = TRUE;
621 break;
622 }
623
624 if ( ! $cascade)
625 {
626 break;
627 }
628 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 }
630 else
631 {
632 $_ci_x = explode('/', $_ci_path);
633 $_ci_file = end($_ci_x);
634 }
Barry Mienydd671972010-10-04 16:33:58 +0200635
Greg Akerf5c84022011-04-19 17:13:03 -0500636 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 {
638 show_error('Unable to load the requested file: '.$_ci_file);
639 }
Barry Mienydd671972010-10-04 16:33:58 +0200640
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 // This allows anything loaded using $this->load (views, files, etc.)
642 // to become accessible from within the Controller and Model functions.
Barry Mienydd671972010-10-04 16:33:58 +0200643
Pascal Kriete89ace432010-11-10 15:49:10 -0500644 $_ci_CI =& get_instance();
645 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500647 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500649 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 }
651 }
652
653 /*
654 * Extract and cache variables
655 *
656 * You can either set variables using the dedicated $this->load_vars()
657 * function or via the second parameter of this function. We'll merge
658 * the two types and cache them so that views that are embedded within
659 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200660 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 if (is_array($_ci_vars))
662 {
663 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
664 }
665 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 /*
668 * Buffer the output
669 *
670 * We buffer the output for two reasons:
671 * 1. Speed. You get a significant speed boost.
672 * 2. So that the final rendered template can be
673 * post-processed by the output class. Why do we
674 * need post processing? For one thing, in order to
675 * show the elapsed page load time. Unless we
676 * can intercept the content right before it's sent to
677 * the browser and then stop the timer it won't be accurate.
678 */
679 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 // If the PHP installation does not support short tags we'll
682 // do a little string replacement, changing the short tags
683 // to standard PHP echo statements.
Barry Mienydd671972010-10-04 16:33:58 +0200684
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
686 {
687 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
688 }
689 else
690 {
691 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
692 }
Barry Mienydd671972010-10-04 16:33:58 +0200693
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200695
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 // Return the file data if requested
697 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200698 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 $buffer = ob_get_contents();
700 @ob_end_clean();
701 return $buffer;
702 }
703
704 /*
705 * Flush the buffer... or buff the flusher?
706 *
707 * In order to permit views to be nested within
708 * other views, we need to flush the content back out whenever
709 * we are beyond the first level of output buffering so that
710 * it can be seen and included properly by the first included
711 * template and any subsequent ones. Oy!
712 *
Barry Mienydd671972010-10-04 16:33:58 +0200713 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 if (ob_get_level() > $this->_ci_ob_level + 1)
715 {
716 ob_end_flush();
717 }
718 else
719 {
Greg Aker22f1a632010-11-10 15:34:35 -0600720 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 @ob_end_clean();
722 }
723 }
724
725 // --------------------------------------------------------------------
726
727 /**
728 * Load class
729 *
730 * This function loads the requested class.
731 *
Barry Mienydd671972010-10-04 16:33:58 +0200732 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 * @param mixed any additional parameters
734 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200735 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 */
Greg Akerf5c84022011-04-19 17:13:03 -0500737 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200738 {
739 // Get the class name, and while we're at it trim any slashes.
740 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500742 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200743
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 // Was the path included with the class name?
745 // We look for a slash to determine this
746 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600747 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 {
Derek Jones32bf1862010-03-02 13:46:07 -0600749 // Extract the path
750 $subdir = substr($class, 0, $last_slash + 1);
Barry Mienydd671972010-10-04 16:33:58 +0200751
Derek Jones32bf1862010-03-02 13:46:07 -0600752 // Get the filename from the path
753 $class = substr($class, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 }
755
756 // We'll test for both lowercase and capitalized versions of the file name
757 foreach (array(ucfirst($class), strtolower($class)) as $class)
758 {
Greg Aker3a746652011-04-19 10:59:47 -0500759 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000760
Barry Mienydd671972010-10-04 16:33:58 +0200761 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 if (file_exists($subclass))
763 {
Greg Aker3a746652011-04-19 10:59:47 -0500764 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200765
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 if ( ! file_exists($baseclass))
767 {
768 log_message('error', "Unable to load the requested class: ".$class);
769 show_error("Unable to load the requested class: ".$class);
770 }
771
772 // Safety: Was the class already loaded by a previous call?
773 if (in_array($subclass, $this->_ci_loaded_files))
774 {
775 // Before we deem this to be a duplicate request, let's see
776 // if a custom object name is being supplied. If so, we'll
777 // return a new instance of the object
778 if ( ! is_null($object_name))
779 {
780 $CI =& get_instance();
781 if ( ! isset($CI->$object_name))
782 {
Barry Mienydd671972010-10-04 16:33:58 +0200783 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 }
785 }
Barry Mienydd671972010-10-04 16:33:58 +0200786
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 $is_duplicate = TRUE;
788 log_message('debug', $class." class already loaded. Second attempt ignored.");
789 return;
790 }
Barry Mienydd671972010-10-04 16:33:58 +0200791
792 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 include_once($subclass);
794 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200795
796 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 }
Barry Mienydd671972010-10-04 16:33:58 +0200798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600800 $is_duplicate = FALSE;
801 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 {
Greg Aker3a746652011-04-19 10:59:47 -0500803 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600804
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 // Does the file exist? No? Bummer...
806 if ( ! file_exists($filepath))
807 {
808 continue;
809 }
Barry Mienydd671972010-10-04 16:33:58 +0200810
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 // Safety: Was the class already loaded by a previous call?
812 if (in_array($filepath, $this->_ci_loaded_files))
813 {
814 // Before we deem this to be a duplicate request, let's see
815 // if a custom object name is being supplied. If so, we'll
816 // return a new instance of the object
817 if ( ! is_null($object_name))
818 {
819 $CI =& get_instance();
820 if ( ! isset($CI->$object_name))
821 {
822 return $this->_ci_init_class($class, '', $params, $object_name);
823 }
824 }
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 $is_duplicate = TRUE;
827 log_message('debug', $class." class already loaded. Second attempt ignored.");
828 return;
829 }
Barry Mienydd671972010-10-04 16:33:58 +0200830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 include_once($filepath);
832 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +0200833 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 }
Derek Jones32bf1862010-03-02 13:46:07 -0600835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 } // END FOREACH
837
838 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
839 if ($subdir == '')
840 {
841 $path = strtolower($class).'/'.$class;
842 return $this->_ci_load_class($path, $params);
843 }
Barry Mienydd671972010-10-04 16:33:58 +0200844
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 // If we got this far we were unable to find the requested class.
846 // We do not issue errors if the load call failed due to a duplicate request
847 if ($is_duplicate == FALSE)
848 {
849 log_message('error', "Unable to load the requested class: ".$class);
850 show_error("Unable to load the requested class: ".$class);
851 }
852 }
Barry Mienydd671972010-10-04 16:33:58 +0200853
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 // --------------------------------------------------------------------
855
856 /**
857 * Instantiates a class
858 *
859 * @access private
860 * @param string
861 * @param string
862 * @param string an optional object name
863 * @return null
864 */
865 function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200866 {
Derek Jones32bf1862010-03-02 13:46:07 -0600867 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 if ($config === NULL)
869 {
Eric Barnes5e16ec62011-01-04 17:25:23 -0500870 // Fetch the config paths containing any package paths
871 $config_component = $this->_ci_get_component('config');
872
873 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 {
Eric Barnes5e16ec62011-01-04 17:25:23 -0500875 // Break on the first found file, thus package files
876 // are not overridden by default paths
877 foreach ($config_component->_config_paths as $path)
878 {
879 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +0100880 // are case-sensitive with regard to file names. Check for environment
881 // first, global next
Greg Aker3a746652011-04-19 10:59:47 -0500882 if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +0100883 {
Greg Aker3a746652011-04-19 10:59:47 -0500884 include_once($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +0100885 break;
886 }
Greg Aker3a746652011-04-19 10:59:47 -0500887 elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +0100888 {
Greg Aker3a746652011-04-19 10:59:47 -0500889 include_once($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +0100890 break;
891 }
Greg Aker3a746652011-04-19 10:59:47 -0500892 elseif (file_exists($path .'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -0500893 {
Greg Aker3a746652011-04-19 10:59:47 -0500894 include_once($path .'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -0500895 break;
896 }
Greg Aker3a746652011-04-19 10:59:47 -0500897 elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -0500898 {
Greg Aker3a746652011-04-19 10:59:47 -0500899 include_once($path .'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -0500900 break;
901 }
902 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 }
904 }
Barry Mienydd671972010-10-04 16:33:58 +0200905
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +0200907 {
908 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 {
910 $name = 'CI_'.$class;
911 }
Barry Mienydd671972010-10-04 16:33:58 +0200912 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
914 $name = config_item('subclass_prefix').$class;
915 }
916 else
917 {
918 $name = $class;
919 }
920 }
921 else
922 {
923 $name = $prefix.$class;
924 }
Barry Mienydd671972010-10-04 16:33:58 +0200925
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 // Is the class name valid?
927 if ( ! class_exists($name))
928 {
929 log_message('error', "Non-existent class: ".$name);
930 show_error("Non-existent class: ".$class);
931 }
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 // Set the variable name we will assign the class to
934 // Was a custom class name supplied? If so we'll use it
935 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +0200936
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 if (is_null($object_name))
938 {
939 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
940 }
941 else
942 {
943 $classvar = $object_name;
944 }
945
Barry Mienydd671972010-10-04 16:33:58 +0200946 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 $this->_ci_classes[$class] = $classvar;
948
Barry Mienydd671972010-10-04 16:33:58 +0200949 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 $CI =& get_instance();
951 if ($config !== NULL)
952 {
953 $CI->$classvar = new $name($config);
954 }
955 else
Barry Mienydd671972010-10-04 16:33:58 +0200956 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 $CI->$classvar = new $name;
Barry Mienydd671972010-10-04 16:33:58 +0200958 }
959 }
960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 /**
964 * Autoloader
965 *
966 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -0600967 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 *
Greg Akerf5c84022011-04-19 17:13:03 -0500969 * This function is public, as it's used in the CI_Controller class.
970 * However, there is no reason you should ever needs to use it.
971 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 * @param array
973 * @return void
974 */
Greg Akerf5c84022011-04-19 17:13:03 -0500975 public function ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +0200976 {
Greg Aker3a746652011-04-19 10:59:47 -0500977 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -0500978 {
Greg Aker3a746652011-04-19 10:59:47 -0500979 include_once(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -0500980 }
981 else
982 {
Greg Aker3a746652011-04-19 10:59:47 -0500983 include_once(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -0500984 }
985
Barry Mienydd671972010-10-04 16:33:58 +0200986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 if ( ! isset($autoload))
988 {
989 return FALSE;
990 }
Barry Mienydd671972010-10-04 16:33:58 +0200991
Phil Sturgeon9730c752010-12-15 10:50:15 +0000992 // Autoload packages
993 if (isset($autoload['packages']))
994 {
995 foreach ($autoload['packages'] as $package_path)
996 {
997 $this->add_package_path($package_path);
998 }
999 }
1000
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 // Load any custom config file
1002 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001003 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 $CI =& get_instance();
1005 foreach ($autoload['config'] as $key => $val)
1006 {
1007 $CI->config->load($val);
1008 }
Barry Mienydd671972010-10-04 16:33:58 +02001009 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001010
Derek Jonesc6da5032010-03-09 20:44:27 -06001011 // Autoload helpers and languages
1012 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001013 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
1015 {
1016 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001017 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 }
1019
1020 // A little tweak to remain backward compatible
1021 // The $autoload['core'] item was deprecated
Derek Jones32bf1862010-03-02 13:46:07 -06001022 if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 {
1024 $autoload['libraries'] = $autoload['core'];
1025 }
Barry Mienydd671972010-10-04 16:33:58 +02001026
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 // Load libraries
1028 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
1029 {
1030 // Load the database driver.
1031 if (in_array('database', $autoload['libraries']))
1032 {
1033 $this->database();
1034 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1035 }
Barry Mienydd671972010-10-04 16:33:58 +02001036
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 // Load all other libraries
1038 foreach ($autoload['libraries'] as $item)
1039 {
1040 $this->library($item);
1041 }
Barry Mienydd671972010-10-04 16:33:58 +02001042 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001043
1044 // Autoload models
1045 if (isset($autoload['model']))
1046 {
1047 $this->model($autoload['model']);
1048 }
Barry Mienydd671972010-10-04 16:33:58 +02001049 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001050
1051 // --------------------------------------------------------------------
1052
1053 /**
1054 * Object to Array
1055 *
1056 * Takes an object as input and converts the class variables to array key/vals
1057 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 * @param object
1059 * @return array
1060 */
Greg Akerf5c84022011-04-19 17:13:03 -05001061 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 {
1063 return (is_object($object)) ? get_object_vars($object) : $object;
1064 }
1065
1066 // --------------------------------------------------------------------
1067
1068 /**
Derek Jones32bf1862010-03-02 13:46:07 -06001069 * Get a reference to a specific library or model
1070 *
Derek Jones32bf1862010-03-02 13:46:07 -06001071 * @return bool
1072 */
Greg Akerf5c84022011-04-19 17:13:03 -05001073 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001074 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001075 $CI =& get_instance();
1076 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001077 }
1078
1079 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001080
Derek Jones32bf1862010-03-02 13:46:07 -06001081 /**
1082 * Prep filename
1083 *
1084 * This function preps the name of various items to make loading them more reliable.
1085 *
Derek Jones32bf1862010-03-02 13:46:07 -06001086 * @param mixed
1087 * @return array
1088 */
Greg Akerf5c84022011-04-19 17:13:03 -05001089 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001090 {
1091 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001092 {
Greg Aker3a746652011-04-19 10:59:47 -05001093 return array(strtolower(str_replace('.php', '', str_replace($extension, '', $filename)).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001094 }
1095 else
1096 {
1097 foreach ($filename as $key => $val)
1098 {
Greg Aker3a746652011-04-19 10:59:47 -05001099 $filename[$key] = strtolower(str_replace('.php', '', str_replace($extension, '', $val)).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001100 }
Barry Mienydd671972010-10-04 16:33:58 +02001101
Derek Jones32bf1862010-03-02 13:46:07 -06001102 return $filename;
1103 }
1104 }
Barry Mienydd671972010-10-04 16:33:58 +02001105
Derek Allard2067d1a2008-11-13 22:59:24 +00001106
1107}
1108
1109/* End of file Loader.php */
Phil Sturgeona36f6372011-04-01 11:40:09 +01001110/* Location: ./system/core/Loader.php */