blob: 2b36c1cad21d8ad540dba44cc309c72fbeb3a7ab [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Loader Class
20 *
21 * Loads views and files
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @author ExpressionEngine Dev Team
26 * @category Loader
27 * @link http://codeigniter.com/user_guide/libraries/loader.html
28 */
29class CI_Loader {
30
31 // All these are set automatically. Don't mess with them.
Greg Aker0c9ee4a2011-04-20 09:40:17 -050032 protected $_ci_ob_level;
33 protected $_ci_view_paths = array();
Greg Akerf5c84022011-04-19 17:13:03 -050034 protected $_ci_library_paths = array();
Greg Aker0c9ee4a2011-04-20 09:40:17 -050035 protected $_ci_model_paths = array();
36 protected $_ci_helper_paths = array();
37 protected $_base_classes = array(); // Set by the controller class
38 protected $_ci_cached_vars = array();
39 protected $_ci_classes = array();
40 protected $_ci_loaded_files = array();
41 protected $_ci_models = array();
42 protected $_ci_helpers = array();
Derek Jones37f4b9c2011-07-01 17:56:50 -050043 protected $_ci_varmap = array('unit_test' => 'unit',
Greg Aker0c9ee4a2011-04-20 09:40:17 -050044 'user_agent' => 'agent');
Derek Allard2067d1a2008-11-13 22:59:24 +000045
46 /**
47 * Constructor
48 *
49 * Sets the path to the view files and gets the initial output buffering level
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
Greg Akerf5c84022011-04-19 17:13:03 -050051 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +020052 {
Derek Jones37f4b9c2011-07-01 17:56:50 -050053 $this->_ci_ob_level = ob_get_level();
Derek Jones32bf1862010-03-02 13:46:07 -060054 $this->_ci_library_paths = array(APPPATH, BASEPATH);
55 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
56 $this->_ci_model_paths = array(APPPATH);
Greg Akerf5c84022011-04-19 17:13:03 -050057 $this->_ci_view_paths = array(APPPATH.'views/' => TRUE);
Derek Jones37f4b9c2011-07-01 17:56:50 -050058
Derek Allard2067d1a2008-11-13 22:59:24 +000059 log_message('debug', "Loader Class Initialized");
60 }
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 // --------------------------------------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050063
Greg Aker0c9ee4a2011-04-20 09:40:17 -050064 /**
Shane Pearson6adfe632011-08-10 16:42:53 -050065 * Initialize the Loader
Greg Aker0c9ee4a2011-04-20 09:40:17 -050066 *
67 * This method is called once in CI_Controller.
68 *
Derek Jones37f4b9c2011-07-01 17:56:50 -050069 * @param array
Greg Aker0c9ee4a2011-04-20 09:40:17 -050070 * @return object
71 */
Shane Pearson6adfe632011-08-10 16:42:53 -050072 public function initialize()
Greg Aker0c9ee4a2011-04-20 09:40:17 -050073 {
Shane Pearson6adfe632011-08-10 16:42:53 -050074 $this->_ci_classes = array();
75 $this->_ci_loaded_files = array();
76 $this->_ci_models = array();
Greg Aker0c9ee4a2011-04-20 09:40:17 -050077 $this->_base_classes =& is_loaded();
Shane Pearson6adfe632011-08-10 16:42:53 -050078
79 $this->_ci_autoloader();
80
Greg Aker0c9ee4a2011-04-20 09:40:17 -050081 return $this;
82 }
83
84 // --------------------------------------------------------------------
85
86 /**
87 * Is Loaded
88 *
89 * A utility function to test if a class is in the self::$_ci_classes array.
90 * This function returns the object name if the class tested for is loaded,
91 * and returns FALSE if it isn't.
92 *
93 * It is mainly used in the form_helper -> _get_validation_object()
94 *
95 * @param string class being checked for
96 * @return mixed class object name on the CI SuperObject or FALSE
97 */
98 public function is_loaded($class)
99 {
100 if (isset($this->_ci_classes[$class]))
101 {
102 return $this->_ci_classes[$class];
103 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500104
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500105 return FALSE;
106 }
107
108 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 /**
111 * Class Loader
112 *
113 * This function lets users load and instantiate classes.
114 * It is designed to be called from a user's app controllers.
115 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 * @param string the name of the class
117 * @param mixed the optional parameters
118 * @param string an optional object name
119 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200120 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500121 public function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Greg Akerce433962010-10-12 09:29:35 -0500123 if (is_array($library))
124 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100125 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500126 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600127 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500128 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000129
Greg Akerce433962010-10-12 09:29:35 -0500130 return;
131 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000132
Derek Jones32bf1862010-03-02 13:46:07 -0600133 if ($library == '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
135 return FALSE;
136 }
137
Derek Jones32bf1862010-03-02 13:46:07 -0600138 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
140 $params = NULL;
141 }
142
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600143 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
145
146 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 /**
149 * Model Loader
150 *
151 * This function lets users load and instantiate models.
152 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 * @param string the name of the class
154 * @param string name for the model
155 * @param bool database connection
156 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200157 */
Greg Akerf5c84022011-04-19 17:13:03 -0500158 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200159 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 if (is_array($model))
161 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500162 foreach ($model as $babe)
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Barry Mienydd671972010-10-04 16:33:58 +0200164 $this->model($babe);
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166 return;
167 }
168
169 if ($model == '')
170 {
171 return;
172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Jones32bf1862010-03-02 13:46:07 -0600174 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600177 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
Derek Jones32bf1862010-03-02 13:46:07 -0600179 // The path is in front of the last slash
180 $path = substr($model, 0, $last_slash + 1);
181
182 // And the model name behind it
183 $model = substr($model, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 if ($name == '')
187 {
188 $name = $model;
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 if (in_array($name, $this->_ci_models, TRUE))
192 {
193 return;
194 }
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 $CI =& get_instance();
197 if (isset($CI->$name))
198 {
199 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000203
Derek Jones32bf1862010-03-02 13:46:07 -0600204 foreach ($this->_ci_model_paths as $mod_path)
205 {
Greg Aker3a746652011-04-19 10:59:47 -0500206 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600207 {
208 continue;
209 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000210
Derek Jones32bf1862010-03-02 13:46:07 -0600211 if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
212 {
213 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500214 {
Derek Jones32bf1862010-03-02 13:46:07 -0600215 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500216 }
Derek Jones32bf1862010-03-02 13:46:07 -0600217
218 $CI->load->database($db_conn, FALSE, TRUE);
219 }
220
Greg Akerbce13482010-10-11 15:37:16 -0500221 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600222 {
223 load_class('Model', 'core');
224 }
225
Greg Aker3a746652011-04-19 10:59:47 -0500226 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600227
228 $model = ucfirst($model);
229
230 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600231
232 $this->_ci_models[] = $name;
233 return;
234 }
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Jones32bf1862010-03-02 13:46:07 -0600236 // couldn't find the model
237 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 /**
243 * Database Loader
244 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 * @param string the DB credentials
246 * @param bool whether to return the DB object
247 * @param bool whether to enable active record (this allows us to override the config setting)
248 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200249 */
Greg Akerf5c84022011-04-19 17:13:03 -0500250 public function database($params = '', $return = FALSE, $active_record = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
252 // Grab the super object
253 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 // Do we even need to load the database class?
Derek Jones9fb6dd12009-12-05 15:32:48 +0000256 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 +0000257 {
258 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200259 }
260
Greg Aker3a746652011-04-19 10:59:47 -0500261 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000262
263 if ($return === TRUE)
264 {
265 return DB($params, $active_record);
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Jones37f4b9c2011-07-01 17:56:50 -0500268 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // reference errors with some configurations
270 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // Load the DB class
Barry Mienydd671972010-10-04 16:33:58 +0200273 $CI->db =& DB($params, $active_record);
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 // --------------------------------------------------------------------
277
278 /**
279 * Load the Utilities Class
280 *
Barry Mienydd671972010-10-04 16:33:58 +0200281 * @return string
282 */
Greg Akerf5c84022011-04-19 17:13:03 -0500283 public function dbutil()
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
285 if ( ! class_exists('CI_DB'))
286 {
287 $this->database();
288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 $CI =& get_instance();
291
292 // for backwards compatibility, load dbforge so we can extend dbutils off it
293 // this use is deprecated and strongly discouraged
294 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200295
Greg Aker3a746652011-04-19 10:59:47 -0500296 require_once(BASEPATH.'database/DB_utility.php');
297 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
299
Pascal Kriete58560022010-11-10 16:01:20 -0500300 $CI->dbutil = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 // --------------------------------------------------------------------
304
305 /**
306 * Load the Database Forge Class
307 *
Barry Mienydd671972010-10-04 16:33:58 +0200308 * @return string
309 */
Greg Akerf5c84022011-04-19 17:13:03 -0500310 public function dbforge()
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
312 if ( ! class_exists('CI_DB'))
313 {
314 $this->database();
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200318
Greg Aker3a746652011-04-19 10:59:47 -0500319 require_once(BASEPATH.'database/DB_forge.php');
320 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
322
323 $CI->dbforge = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 /**
329 * Load View
330 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500331 * This function is used to load a "view" file. It has three parameters:
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 *
333 * 1. The name of the "view" file to be included.
334 * 2. An associative array of data to be extracted for use in the view.
Derek Jones37f4b9c2011-07-01 17:56:50 -0500335 * 3. TRUE/FALSE - whether to return the data or load it. In
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 * some cases it's advantageous to be able to return data so that
337 * a developer can process it in some way.
338 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 * @param string
340 * @param array
341 * @param bool
342 * @return void
343 */
Greg Akerf5c84022011-04-19 17:13:03 -0500344 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
346 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
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 File
353 *
354 * This is a generic file loader
355 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @param string
357 * @param bool
358 * @return string
359 */
Greg Akerf5c84022011-04-19 17:13:03 -0500360 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 /**
368 * Set Variables
369 *
370 * Once variables are set they become available within
371 * the controller class and its "view" files.
372 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 * @param array
374 * @return void
375 */
Greg Akerf5c84022011-04-19 17:13:03 -0500376 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 if ($val != '' AND is_string($vars))
379 {
380 $vars = array($vars => $val);
381 }
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 if (is_array($vars) AND count($vars) > 0)
386 {
387 foreach ($vars as $key => $val)
388 {
389 $this->_ci_cached_vars[$key] = $val;
390 }
391 }
392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600397 * Get Variable
398 *
399 * Check if a variable is set and retrieve it.
400 *
401 * @param array
402 * @return void
403 */
404 public function get_var($key)
405 {
406 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
407 }
408
409 // --------------------------------------------------------------------
410
411 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 * Load Helper
413 *
414 * This function loads the specified helper file.
415 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 * @param mixed
417 * @return void
418 */
Greg Akerf5c84022011-04-19 17:13:03 -0500419 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200420 {
Derek Jones32bf1862010-03-02 13:46:07 -0600421 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200422 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 if (isset($this->_ci_helpers[$helper]))
424 {
425 continue;
426 }
Derek Jones32bf1862010-03-02 13:46:07 -0600427
Greg Aker3a746652011-04-19 10:59:47 -0500428 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000429
Barry Mienydd671972010-10-04 16:33:58 +0200430 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 if (file_exists($ext_helper))
432 {
Greg Aker3a746652011-04-19 10:59:47 -0500433 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 if ( ! file_exists($base_helper))
436 {
Greg Aker3a746652011-04-19 10:59:47 -0500437 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 include_once($ext_helper);
441 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Jones32bf1862010-03-02 13:46:07 -0600443 $this->_ci_helpers[$helper] = TRUE;
444 log_message('debug', 'Helper loaded: '.$helper);
445 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Jones32bf1862010-03-02 13:46:07 -0600448 // Try to load the helper
449 foreach ($this->_ci_helper_paths as $path)
450 {
Greg Aker3a746652011-04-19 10:59:47 -0500451 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200452 {
Greg Aker3a746652011-04-19 10:59:47 -0500453 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600454
455 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200456 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600457 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
459 }
460
Derek Jones32bf1862010-03-02 13:46:07 -0600461 // unable to load the helper
462 if ( ! isset($this->_ci_helpers[$helper]))
463 {
Greg Aker3a746652011-04-19 10:59:47 -0500464 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 /**
472 * Load Helpers
473 *
474 * This is simply an alias to the above function in case the
475 * user has written the plural form of this function.
476 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 * @param array
478 * @return void
479 */
Greg Akerf5c84022011-04-19 17:13:03 -0500480 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
482 $this->helper($helpers);
483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 * Loads a language file
489 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @param array
491 * @param string
492 * @return void
493 */
Greg Akerf5c84022011-04-19 17:13:03 -0500494 public function language($file = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
496 $CI =& get_instance();
497
498 if ( ! is_array($file))
499 {
500 $file = array($file);
501 }
502
503 foreach ($file as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200504 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 $CI->lang->load($langfile, $lang);
506 }
507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 /**
512 * Loads a config file
513 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 * @param string
515 * @return void
516 */
Greg Akerf5c84022011-04-19 17:13:03 -0500517 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200518 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 $CI =& get_instance();
520 $CI->config->load($file, $use_sections, $fail_gracefully);
521 }
522
523 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 /**
Derek Jones8dca0412010-03-05 13:01:44 -0600526 * Driver
527 *
528 * Loads a driver library
529 *
530 * @param string the name of the class
531 * @param mixed the optional parameters
532 * @param string an optional object name
533 * @return void
534 */
Greg Akerf5c84022011-04-19 17:13:03 -0500535 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600536 {
537 if ( ! class_exists('CI_Driver_Library'))
538 {
539 // we aren't instantiating an object here, that'll be done by the Library itself
Greg Aker3a746652011-04-19 10:59:47 -0500540 require BASEPATH.'libraries/Driver.php';
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600541 }
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600543 // We can save the loader some time since Drivers will *always* be in a subfolder,
544 // and typically identically named to the library
545 if ( ! strpos($library, '/'))
546 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500547 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Jones8dca0412010-03-05 13:01:44 -0600550 return $this->library($library, $params, $object_name);
551 }
552
553 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Jones8dca0412010-03-05 13:01:44 -0600555 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600556 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 *
Derek Jones32bf1862010-03-02 13:46:07 -0600558 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 * @param string
Derek Jones37f4b9c2011-07-01 17:56:50 -0500561 * @param boolean
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600563 */
Greg Akerf5c84022011-04-19 17:13:03 -0500564 public function add_package_path($path, $view_cascade=TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600565 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500566 $path = rtrim($path, '/').'/';
Derek Jones37f4b9c2011-07-01 17:56:50 -0500567
Derek Jones32bf1862010-03-02 13:46:07 -0600568 array_unshift($this->_ci_library_paths, $path);
569 array_unshift($this->_ci_model_paths, $path);
570 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200571
Greg Akerf5c84022011-04-19 17:13:03 -0500572 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
573
Derek Jones32bf1862010-03-02 13:46:07 -0600574 // Add config file path
575 $config =& $this->_ci_get_component('config');
576 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 }
578
579 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600580
581 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000582 * Get Package Paths
583 *
584 * Return a list of all package paths, by default it will ignore BASEPATH.
585 *
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000586 * @param string
587 * @return void
588 */
Greg Akerf5c84022011-04-19 17:13:03 -0500589 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000590 {
591 return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
592 }
593
594 // --------------------------------------------------------------------
595
596 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600597 * Remove Package Path
598 *
599 * Remove a path from the library, model, and helper path arrays if it exists
600 * If no path is provided, the most recently added path is removed.
601 *
Derek Jones32bf1862010-03-02 13:46:07 -0600602 * @param type
603 * @return type
604 */
Greg Akerf5c84022011-04-19 17:13:03 -0500605 public function remove_package_path($path = '', $remove_config_path = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600606 {
607 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200608
Derek Jones32bf1862010-03-02 13:46:07 -0600609 if ($path == '')
610 {
611 $void = array_shift($this->_ci_library_paths);
612 $void = array_shift($this->_ci_model_paths);
613 $void = array_shift($this->_ci_helper_paths);
Greg Akerf5c84022011-04-19 17:13:03 -0500614 $void = array_shift($this->_ci_view_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600615 $void = array_shift($config->_config_paths);
616 }
617 else
618 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500619 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600620 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
621 {
622 if (($key = array_search($path, $this->{$var})) !== FALSE)
623 {
624 unset($this->{$var}[$key]);
625 }
626 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500627
Greg Akerf5c84022011-04-19 17:13:03 -0500628 if (isset($this->_ci_view_paths[$path.'views/']))
629 {
630 unset($this->_ci_view_paths[$path.'views/']);
631 }
Barry Mienydd671972010-10-04 16:33:58 +0200632
Derek Jones32bf1862010-03-02 13:46:07 -0600633 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
634 {
635 unset($config->_config_paths[$key]);
636 }
637 }
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Jones32bf1862010-03-02 13:46:07 -0600639 // make sure the application default paths are still in the array
640 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
641 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
642 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500643 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600644 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
645 }
646
647 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 /**
650 * Loader
651 *
652 * This function is used to load views and files.
653 * Variables are prefixed with _ci_ to avoid symbol collision with
654 * variables made available to view files
655 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 * @param array
657 * @return void
658 */
Greg Akerf5c84022011-04-19 17:13:03 -0500659 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
661 // Set the default data variables
662 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
663 {
664 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
665 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500666
Greg Akerf5c84022011-04-19 17:13:03 -0500667 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000668
669 // Set the path to the requested file
Greg Aker8807be32011-04-21 13:06:15 -0500670 if ($_ci_path != '')
671 {
672 $_ci_x = explode('/', $_ci_path);
673 $_ci_file = end($_ci_x);
674 }
675 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
677 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Greg Aker3a746652011-04-19 10:59:47 -0500678 $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500679
680 foreach ($this->_ci_view_paths as $view_file => $cascade)
681 {
682 if (file_exists($view_file.$_ci_file))
683 {
684 $_ci_path = $view_file.$_ci_file;
685 $file_exists = TRUE;
686 break;
687 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500688
Greg Akerf5c84022011-04-19 17:13:03 -0500689 if ( ! $cascade)
690 {
691 break;
Derek Jones37f4b9c2011-07-01 17:56:50 -0500692 }
Greg Akerf5c84022011-04-19 17:13:03 -0500693 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 }
Barry Mienydd671972010-10-04 16:33:58 +0200695
Greg Akerf5c84022011-04-19 17:13:03 -0500696 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 {
698 show_error('Unable to load the requested file: '.$_ci_file);
699 }
Barry Mienydd671972010-10-04 16:33:58 +0200700
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 // This allows anything loaded using $this->load (views, files, etc.)
702 // to become accessible from within the Controller and Model functions.
Barry Mienydd671972010-10-04 16:33:58 +0200703
Pascal Kriete89ace432010-11-10 15:49:10 -0500704 $_ci_CI =& get_instance();
705 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500707 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500709 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 }
711 }
712
713 /*
714 * Extract and cache variables
715 *
716 * You can either set variables using the dedicated $this->load_vars()
717 * function or via the second parameter of this function. We'll merge
718 * the two types and cache them so that views that are embedded within
719 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200720 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 if (is_array($_ci_vars))
722 {
723 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
724 }
725 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200726
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 /*
728 * Buffer the output
729 *
730 * We buffer the output for two reasons:
731 * 1. Speed. You get a significant speed boost.
732 * 2. So that the final rendered template can be
Derek Jones37f4b9c2011-07-01 17:56:50 -0500733 * post-processed by the output class. Why do we
734 * need post processing? For one thing, in order to
735 * show the elapsed page load time. Unless we
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 * can intercept the content right before it's sent to
737 * the browser and then stop the timer it won't be accurate.
738 */
739 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200740
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 // If the PHP installation does not support short tags we'll
742 // do a little string replacement, changing the short tags
743 // to standard PHP echo statements.
Barry Mienydd671972010-10-04 16:33:58 +0200744
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
746 {
747 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
748 }
749 else
750 {
751 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
752 }
Barry Mienydd671972010-10-04 16:33:58 +0200753
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200755
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 // Return the file data if requested
757 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200758 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 $buffer = ob_get_contents();
760 @ob_end_clean();
761 return $buffer;
762 }
763
764 /*
765 * Flush the buffer... or buff the flusher?
766 *
767 * In order to permit views to be nested within
768 * other views, we need to flush the content back out whenever
769 * we are beyond the first level of output buffering so that
770 * it can be seen and included properly by the first included
771 * template and any subsequent ones. Oy!
772 *
Barry Mienydd671972010-10-04 16:33:58 +0200773 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 if (ob_get_level() > $this->_ci_ob_level + 1)
775 {
776 ob_end_flush();
777 }
778 else
779 {
Greg Aker22f1a632010-11-10 15:34:35 -0600780 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 @ob_end_clean();
782 }
783 }
784
785 // --------------------------------------------------------------------
786
787 /**
788 * Load class
789 *
790 * This function loads the requested class.
791 *
Barry Mienydd671972010-10-04 16:33:58 +0200792 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 * @param mixed any additional parameters
794 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200795 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 */
Greg Akerf5c84022011-04-19 17:13:03 -0500797 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200798 {
799 // Get the class name, and while we're at it trim any slashes.
800 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500802 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 // Was the path included with the class name?
805 // We look for a slash to determine this
806 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600807 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 {
Derek Jones32bf1862010-03-02 13:46:07 -0600809 // Extract the path
810 $subdir = substr($class, 0, $last_slash + 1);
Barry Mienydd671972010-10-04 16:33:58 +0200811
Derek Jones32bf1862010-03-02 13:46:07 -0600812 // Get the filename from the path
813 $class = substr($class, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 }
815
816 // We'll test for both lowercase and capitalized versions of the file name
817 foreach (array(ucfirst($class), strtolower($class)) as $class)
818 {
Greg Aker3a746652011-04-19 10:59:47 -0500819 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000820
Barry Mienydd671972010-10-04 16:33:58 +0200821 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 if (file_exists($subclass))
823 {
Greg Aker3a746652011-04-19 10:59:47 -0500824 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 if ( ! file_exists($baseclass))
827 {
828 log_message('error', "Unable to load the requested class: ".$class);
829 show_error("Unable to load the requested class: ".$class);
830 }
831
Derek Jones37f4b9c2011-07-01 17:56:50 -0500832 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 if (in_array($subclass, $this->_ci_loaded_files))
834 {
835 // Before we deem this to be a duplicate request, let's see
Derek Jones37f4b9c2011-07-01 17:56:50 -0500836 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 // return a new instance of the object
838 if ( ! is_null($object_name))
839 {
840 $CI =& get_instance();
841 if ( ! isset($CI->$object_name))
842 {
Barry Mienydd671972010-10-04 16:33:58 +0200843 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 }
845 }
Barry Mienydd671972010-10-04 16:33:58 +0200846
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 $is_duplicate = TRUE;
848 log_message('debug', $class." class already loaded. Second attempt ignored.");
849 return;
850 }
Barry Mienydd671972010-10-04 16:33:58 +0200851
852 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 include_once($subclass);
854 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200855
856 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 }
Barry Mienydd671972010-10-04 16:33:58 +0200858
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600860 $is_duplicate = FALSE;
861 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 {
Greg Aker3a746652011-04-19 10:59:47 -0500863 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600864
Derek Jones37f4b9c2011-07-01 17:56:50 -0500865 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 if ( ! file_exists($filepath))
867 {
868 continue;
869 }
Barry Mienydd671972010-10-04 16:33:58 +0200870
Derek Jones37f4b9c2011-07-01 17:56:50 -0500871 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 if (in_array($filepath, $this->_ci_loaded_files))
873 {
874 // Before we deem this to be a duplicate request, let's see
Derek Jones37f4b9c2011-07-01 17:56:50 -0500875 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 // return a new instance of the object
877 if ( ! is_null($object_name))
878 {
879 $CI =& get_instance();
880 if ( ! isset($CI->$object_name))
881 {
882 return $this->_ci_init_class($class, '', $params, $object_name);
883 }
884 }
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 $is_duplicate = TRUE;
887 log_message('debug', $class." class already loaded. Second attempt ignored.");
888 return;
889 }
Barry Mienydd671972010-10-04 16:33:58 +0200890
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 include_once($filepath);
892 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +0200893 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 }
Derek Jones32bf1862010-03-02 13:46:07 -0600895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 } // END FOREACH
897
Derek Jones37f4b9c2011-07-01 17:56:50 -0500898 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 if ($subdir == '')
900 {
901 $path = strtolower($class).'/'.$class;
902 return $this->_ci_load_class($path, $params);
903 }
Barry Mienydd671972010-10-04 16:33:58 +0200904
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 // If we got this far we were unable to find the requested class.
906 // We do not issue errors if the load call failed due to a duplicate request
907 if ($is_duplicate == FALSE)
908 {
909 log_message('error', "Unable to load the requested class: ".$class);
910 show_error("Unable to load the requested class: ".$class);
911 }
912 }
Barry Mienydd671972010-10-04 16:33:58 +0200913
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 // --------------------------------------------------------------------
915
916 /**
917 * Instantiates a class
918 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 * @param string
920 * @param string
921 * @param string an optional object name
922 * @return null
923 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500924 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200925 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500926 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 if ($config === NULL)
928 {
Eric Barnes5e16ec62011-01-04 17:25:23 -0500929 // Fetch the config paths containing any package paths
930 $config_component = $this->_ci_get_component('config');
931
932 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 {
Eric Barnes5e16ec62011-01-04 17:25:23 -0500934 // Break on the first found file, thus package files
935 // are not overridden by default paths
936 foreach ($config_component->_config_paths as $path)
937 {
938 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +0100939 // are case-sensitive with regard to file names. Check for environment
940 // first, global next
Greg Aker3a746652011-04-19 10:59:47 -0500941 if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +0100942 {
Greg Aker3a746652011-04-19 10:59:47 -0500943 include_once($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +0100944 break;
945 }
Greg Aker3a746652011-04-19 10:59:47 -0500946 elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +0100947 {
Greg Aker3a746652011-04-19 10:59:47 -0500948 include_once($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +0100949 break;
950 }
Greg Aker3a746652011-04-19 10:59:47 -0500951 elseif (file_exists($path .'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -0500952 {
Greg Aker3a746652011-04-19 10:59:47 -0500953 include_once($path .'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -0500954 break;
955 }
Greg Aker3a746652011-04-19 10:59:47 -0500956 elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -0500957 {
Greg Aker3a746652011-04-19 10:59:47 -0500958 include_once($path .'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -0500959 break;
960 }
961 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 }
963 }
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +0200966 {
967 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 {
969 $name = 'CI_'.$class;
970 }
Barry Mienydd671972010-10-04 16:33:58 +0200971 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 {
973 $name = config_item('subclass_prefix').$class;
974 }
975 else
976 {
977 $name = $class;
978 }
979 }
980 else
981 {
982 $name = $prefix.$class;
983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 // Is the class name valid?
986 if ( ! class_exists($name))
987 {
988 log_message('error', "Non-existent class: ".$name);
989 show_error("Non-existent class: ".$class);
990 }
Barry Mienydd671972010-10-04 16:33:58 +0200991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 // Set the variable name we will assign the class to
Derek Jones37f4b9c2011-07-01 17:56:50 -0500993 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +0200995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 if (is_null($object_name))
997 {
998 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
999 }
1000 else
1001 {
1002 $classvar = $object_name;
1003 }
1004
Barry Mienydd671972010-10-04 16:33:58 +02001005 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 $this->_ci_classes[$class] = $classvar;
1007
Barry Mienydd671972010-10-04 16:33:58 +02001008 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 $CI =& get_instance();
1010 if ($config !== NULL)
1011 {
1012 $CI->$classvar = new $name($config);
1013 }
1014 else
Barry Mienydd671972010-10-04 16:33:58 +02001015 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 $CI->$classvar = new $name;
Barry Mienydd671972010-10-04 16:33:58 +02001017 }
1018 }
1019
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001021
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 /**
1023 * Autoloader
1024 *
1025 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -06001026 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 * @param array
1029 * @return void
1030 */
Shane Pearson6adfe632011-08-10 16:42:53 -05001031 private function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001032 {
Greg Aker3a746652011-04-19 10:59:47 -05001033 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001034 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001035 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001036 }
1037 else
1038 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001039 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001040 }
Barry Mienydd671972010-10-04 16:33:58 +02001041
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 if ( ! isset($autoload))
1043 {
1044 return FALSE;
1045 }
Barry Mienydd671972010-10-04 16:33:58 +02001046
Phil Sturgeon9730c752010-12-15 10:50:15 +00001047 // Autoload packages
1048 if (isset($autoload['packages']))
1049 {
1050 foreach ($autoload['packages'] as $package_path)
1051 {
1052 $this->add_package_path($package_path);
1053 }
1054 }
1055
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 // Load any custom config file
1057 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001058 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 $CI =& get_instance();
1060 foreach ($autoload['config'] as $key => $val)
1061 {
1062 $CI->config->load($val);
1063 }
Barry Mienydd671972010-10-04 16:33:58 +02001064 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001065
Derek Jonesc6da5032010-03-09 20:44:27 -06001066 // Autoload helpers and languages
1067 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001068 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
1070 {
1071 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001072 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 }
1074
1075 // A little tweak to remain backward compatible
1076 // The $autoload['core'] item was deprecated
Derek Jones32bf1862010-03-02 13:46:07 -06001077 if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 {
1079 $autoload['libraries'] = $autoload['core'];
1080 }
Barry Mienydd671972010-10-04 16:33:58 +02001081
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 // Load libraries
1083 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
1084 {
1085 // Load the database driver.
1086 if (in_array('database', $autoload['libraries']))
1087 {
1088 $this->database();
1089 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1090 }
Barry Mienydd671972010-10-04 16:33:58 +02001091
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 // Load all other libraries
1093 foreach ($autoload['libraries'] as $item)
1094 {
1095 $this->library($item);
1096 }
Barry Mienydd671972010-10-04 16:33:58 +02001097 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001098
1099 // Autoload models
1100 if (isset($autoload['model']))
1101 {
1102 $this->model($autoload['model']);
1103 }
Barry Mienydd671972010-10-04 16:33:58 +02001104 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001105
1106 // --------------------------------------------------------------------
1107
1108 /**
1109 * Object to Array
1110 *
1111 * Takes an object as input and converts the class variables to array key/vals
1112 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 * @param object
1114 * @return array
1115 */
Greg Akerf5c84022011-04-19 17:13:03 -05001116 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 {
1118 return (is_object($object)) ? get_object_vars($object) : $object;
1119 }
1120
1121 // --------------------------------------------------------------------
1122
1123 /**
Derek Jones32bf1862010-03-02 13:46:07 -06001124 * Get a reference to a specific library or model
1125 *
Derek Jones32bf1862010-03-02 13:46:07 -06001126 * @return bool
1127 */
Greg Akerf5c84022011-04-19 17:13:03 -05001128 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001129 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001130 $CI =& get_instance();
1131 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001132 }
1133
1134 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001135
Derek Jones32bf1862010-03-02 13:46:07 -06001136 /**
1137 * Prep filename
1138 *
1139 * This function preps the name of various items to make loading them more reliable.
1140 *
Derek Jones32bf1862010-03-02 13:46:07 -06001141 * @param mixed
1142 * @return array
1143 */
Greg Akerf5c84022011-04-19 17:13:03 -05001144 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001145 {
1146 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001147 {
Greg Aker3a746652011-04-19 10:59:47 -05001148 return array(strtolower(str_replace('.php', '', str_replace($extension, '', $filename)).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001149 }
1150 else
1151 {
1152 foreach ($filename as $key => $val)
1153 {
Greg Aker3a746652011-04-19 10:59:47 -05001154 $filename[$key] = strtolower(str_replace('.php', '', str_replace($extension, '', $val)).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001155 }
Barry Mienydd671972010-10-04 16:33:58 +02001156
Derek Jones32bf1862010-03-02 13:46:07 -06001157 return $filename;
1158 }
1159 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001160}
1161
1162/* End of file Loader.php */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001163/* Location: ./system/core/Loader.php */