blob: de0fc06d2a934e6b1891701d4a05e0c9d247c1d9 [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.
David Behlercda768a2011-08-14 23:52:48 +020032 /**
33 * Nesting level of the output buffering mechanism
34 *
35 * @var int
36 * @access protected
37 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050038 protected $_ci_ob_level;
David Behlercda768a2011-08-14 23:52:48 +020039 /**
40 * List of paths to load views from
41 *
42 * @var array
43 * @access protected
44 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050045 protected $_ci_view_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020046 /**
47 * List of paths to load libraries from
48 *
49 * @var array
50 * @access protected
51 */
Greg Akerf5c84022011-04-19 17:13:03 -050052 protected $_ci_library_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020053 /**
54 * List of paths to load models from
55 *
56 * @var array
57 * @access protected
58 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050059 protected $_ci_model_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020060 /**
61 * List of paths to load helpers from
62 *
63 * @var array
64 * @access protected
65 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050066 protected $_ci_helper_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020067 /**
68 * List of loaded base classes
69 * Set by the controller class
70 *
71 * @var array
72 * @access protected
73 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050074 protected $_base_classes = array(); // Set by the controller class
David Behlercda768a2011-08-14 23:52:48 +020075 /**
76 * List of cached variables
77 *
78 * @var array
79 * @access protected
80 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050081 protected $_ci_cached_vars = array();
David Behlercda768a2011-08-14 23:52:48 +020082 /**
83 * List of loaded classes
84 *
85 * @var array
86 * @access protected
87 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050088 protected $_ci_classes = array();
David Behlercda768a2011-08-14 23:52:48 +020089 /**
90 * List of loaded files
91 *
92 * @var array
93 * @access protected
94 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050095 protected $_ci_loaded_files = array();
David Behlercda768a2011-08-14 23:52:48 +020096 /**
97 * List of loaded models
98 *
99 * @var array
100 * @access protected
101 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500102 protected $_ci_models = array();
David Behlercda768a2011-08-14 23:52:48 +0200103 /**
104 * List of loaded helpers
105 *
106 * @var array
107 * @access protected
108 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500109 protected $_ci_helpers = array();
David Behlercda768a2011-08-14 23:52:48 +0200110 /**
111 * List of class name mappings
112 *
113 * @var array
114 * @access protected
115 */
116 protected $_ci_varmap = array('unit_test' => 'unit',
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500117 'user_agent' => 'agent');
Derek Allard2067d1a2008-11-13 22:59:24 +0000118
119 /**
120 * Constructor
121 *
122 * Sets the path to the view files and gets the initial output buffering level
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 */
Greg Akerf5c84022011-04-19 17:13:03 -0500124 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +0200125 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500126 $this->_ci_ob_level = ob_get_level();
Derek Jones32bf1862010-03-02 13:46:07 -0600127 $this->_ci_library_paths = array(APPPATH, BASEPATH);
128 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
129 $this->_ci_model_paths = array(APPPATH);
Joe Cianflone8eef9c72011-08-21 10:39:06 -0400130 $this->_ci_view_paths = array(VIEWPATH => TRUE);
David Behlercda768a2011-08-14 23:52:48 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 log_message('debug', "Loader Class Initialized");
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200136
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500137 /**
Shane Pearson6adfe632011-08-10 16:42:53 -0500138 * Initialize the Loader
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500139 *
140 * This method is called once in CI_Controller.
141 *
David Behlercda768a2011-08-14 23:52:48 +0200142 * @param array
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500143 * @return object
144 */
Shane Pearson6adfe632011-08-10 16:42:53 -0500145 public function initialize()
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500146 {
Shane Pearson6adfe632011-08-10 16:42:53 -0500147 $this->_ci_classes = array();
148 $this->_ci_loaded_files = array();
149 $this->_ci_models = array();
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500150 $this->_base_classes =& is_loaded();
Shane Pearson6adfe632011-08-10 16:42:53 -0500151
152 $this->_ci_autoloader();
153
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500154 return $this;
155 }
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Is Loaded
161 *
162 * A utility function to test if a class is in the self::$_ci_classes array.
163 * This function returns the object name if the class tested for is loaded,
164 * and returns FALSE if it isn't.
165 *
166 * It is mainly used in the form_helper -> _get_validation_object()
167 *
168 * @param string class being checked for
169 * @return mixed class object name on the CI SuperObject or FALSE
170 */
171 public function is_loaded($class)
172 {
173 if (isset($this->_ci_classes[$class]))
174 {
175 return $this->_ci_classes[$class];
176 }
David Behlercda768a2011-08-14 23:52:48 +0200177
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500178 return FALSE;
179 }
180
181 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 /**
184 * Class Loader
185 *
186 * This function lets users load and instantiate classes.
187 * It is designed to be called from a user's app controllers.
188 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * @param string the name of the class
190 * @param mixed the optional parameters
191 * @param string an optional object name
192 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200193 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500194 public function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Greg Akerce433962010-10-12 09:29:35 -0500196 if (is_array($library))
197 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100198 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500199 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600200 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500201 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000202
Greg Akerce433962010-10-12 09:29:35 -0500203 return;
204 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000205
Derek Jones32bf1862010-03-02 13:46:07 -0600206 if ($library == '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
208 return FALSE;
209 }
210
Derek Jones32bf1862010-03-02 13:46:07 -0600211 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
213 $params = NULL;
214 }
215
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600216 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 }
218
219 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 /**
222 * Model Loader
223 *
224 * This function lets users load and instantiate models.
225 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * @param string the name of the class
227 * @param string name for the model
228 * @param bool database connection
229 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200230 */
Greg Akerf5c84022011-04-19 17:13:03 -0500231 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200232 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 if (is_array($model))
234 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500235 foreach ($model as $babe)
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Barry Mienydd671972010-10-04 16:33:58 +0200237 $this->model($babe);
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
239 return;
240 }
241
242 if ($model == '')
243 {
244 return;
245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Jones32bf1862010-03-02 13:46:07 -0600247 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600250 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
Derek Jones32bf1862010-03-02 13:46:07 -0600252 // The path is in front of the last slash
253 $path = substr($model, 0, $last_slash + 1);
254
255 // And the model name behind it
256 $model = substr($model, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
Barry Mienydd671972010-10-04 16:33:58 +0200258
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 if ($name == '')
260 {
261 $name = $model;
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 if (in_array($name, $this->_ci_models, TRUE))
265 {
266 return;
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 $CI =& get_instance();
270 if (isset($CI->$name))
271 {
272 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
273 }
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000276
Derek Jones32bf1862010-03-02 13:46:07 -0600277 foreach ($this->_ci_model_paths as $mod_path)
278 {
Greg Aker3a746652011-04-19 10:59:47 -0500279 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600280 {
281 continue;
282 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000283
Derek Jones32bf1862010-03-02 13:46:07 -0600284 if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
285 {
286 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500287 {
Derek Jones32bf1862010-03-02 13:46:07 -0600288 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500289 }
Derek Jones32bf1862010-03-02 13:46:07 -0600290
291 $CI->load->database($db_conn, FALSE, TRUE);
292 }
293
Greg Akerbce13482010-10-11 15:37:16 -0500294 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600295 {
296 load_class('Model', 'core');
297 }
298
Greg Aker3a746652011-04-19 10:59:47 -0500299 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600300
301 $model = ucfirst($model);
302
303 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600304
305 $this->_ci_models[] = $name;
306 return;
307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Jones32bf1862010-03-02 13:46:07 -0600309 // couldn't find the model
310 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 /**
316 * Database Loader
317 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 * @param string the DB credentials
319 * @param bool whether to return the DB object
320 * @param bool whether to enable active record (this allows us to override the config setting)
321 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200322 */
Greg Akerf5c84022011-04-19 17:13:03 -0500323 public function database($params = '', $return = FALSE, $active_record = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
325 // Grab the super object
326 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // Do we even need to load the database class?
Derek Jones9fb6dd12009-12-05 15:32:48 +0000329 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 +0000330 {
331 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200332 }
333
Greg Aker3a746652011-04-19 10:59:47 -0500334 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000335
336 if ($return === TRUE)
337 {
338 return DB($params, $active_record);
339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Jones37f4b9c2011-07-01 17:56:50 -0500341 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // reference errors with some configurations
343 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 // Load the DB class
Barry Mienydd671972010-10-04 16:33:58 +0200346 $CI->db =& DB($params, $active_record);
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // --------------------------------------------------------------------
350
351 /**
352 * Load the Utilities Class
353 *
Barry Mienydd671972010-10-04 16:33:58 +0200354 * @return string
355 */
Greg Akerf5c84022011-04-19 17:13:03 -0500356 public function dbutil()
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
358 if ( ! class_exists('CI_DB'))
359 {
360 $this->database();
361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 $CI =& get_instance();
364
365 // for backwards compatibility, load dbforge so we can extend dbutils off it
366 // this use is deprecated and strongly discouraged
367 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200368
Greg Aker3a746652011-04-19 10:59:47 -0500369 require_once(BASEPATH.'database/DB_utility.php');
370 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
372
Pascal Kriete58560022010-11-10 16:01:20 -0500373 $CI->dbutil = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // --------------------------------------------------------------------
377
378 /**
379 * Load the Database Forge Class
380 *
Barry Mienydd671972010-10-04 16:33:58 +0200381 * @return string
382 */
Greg Akerf5c84022011-04-19 17:13:03 -0500383 public function dbforge()
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
385 if ( ! class_exists('CI_DB'))
386 {
387 $this->database();
388 }
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200391
Greg Aker3a746652011-04-19 10:59:47 -0500392 require_once(BASEPATH.'database/DB_forge.php');
393 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
395
396 $CI->dbforge = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 /**
402 * Load View
403 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500404 * This function is used to load a "view" file. It has three parameters:
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 *
406 * 1. The name of the "view" file to be included.
407 * 2. An associative array of data to be extracted for use in the view.
Derek Jones37f4b9c2011-07-01 17:56:50 -0500408 * 3. TRUE/FALSE - whether to return the data or load it. In
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 * some cases it's advantageous to be able to return data so that
410 * a developer can process it in some way.
411 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 * @param string
413 * @param array
414 * @param bool
415 * @return void
416 */
Greg Akerf5c84022011-04-19 17:13:03 -0500417 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
419 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 /**
425 * Load File
426 *
427 * This is a generic file loader
428 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 * @param string
430 * @param bool
431 * @return string
432 */
Greg Akerf5c84022011-04-19 17:13:03 -0500433 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
435 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 /**
441 * Set Variables
442 *
443 * Once variables are set they become available within
444 * the controller class and its "view" files.
445 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 * @param array
David Behlercda768a2011-08-14 23:52:48 +0200447 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @return void
449 */
Greg Akerf5c84022011-04-19 17:13:03 -0500450 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
452 if ($val != '' AND is_string($vars))
453 {
454 $vars = array($vars => $val);
455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 if (is_array($vars) AND count($vars) > 0)
460 {
461 foreach ($vars as $key => $val)
462 {
463 $this->_ci_cached_vars[$key] = $val;
464 }
465 }
466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600471 * Get Variable
472 *
473 * Check if a variable is set and retrieve it.
474 *
475 * @param array
476 * @return void
477 */
478 public function get_var($key)
479 {
480 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
481 }
482
483 // --------------------------------------------------------------------
484
485 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 * Load Helper
487 *
488 * This function loads the specified helper file.
489 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @param mixed
491 * @return void
492 */
Greg Akerf5c84022011-04-19 17:13:03 -0500493 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200494 {
Derek Jones32bf1862010-03-02 13:46:07 -0600495 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200496 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 if (isset($this->_ci_helpers[$helper]))
498 {
499 continue;
500 }
Derek Jones32bf1862010-03-02 13:46:07 -0600501
Greg Aker3a746652011-04-19 10:59:47 -0500502 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000503
Barry Mienydd671972010-10-04 16:33:58 +0200504 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 if (file_exists($ext_helper))
506 {
Greg Aker3a746652011-04-19 10:59:47 -0500507 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 if ( ! file_exists($base_helper))
510 {
Greg Aker3a746652011-04-19 10:59:47 -0500511 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 include_once($ext_helper);
515 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Jones32bf1862010-03-02 13:46:07 -0600517 $this->_ci_helpers[$helper] = TRUE;
518 log_message('debug', 'Helper loaded: '.$helper);
519 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Jones32bf1862010-03-02 13:46:07 -0600522 // Try to load the helper
523 foreach ($this->_ci_helper_paths as $path)
524 {
Greg Aker3a746652011-04-19 10:59:47 -0500525 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200526 {
Greg Aker3a746652011-04-19 10:59:47 -0500527 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600528
529 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200530 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600531 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 }
533 }
534
Derek Jones32bf1862010-03-02 13:46:07 -0600535 // unable to load the helper
536 if ( ! isset($this->_ci_helpers[$helper]))
537 {
Greg Aker3a746652011-04-19 10:59:47 -0500538 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 }
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 /**
546 * Load Helpers
547 *
548 * This is simply an alias to the above function in case the
549 * user has written the plural form of this function.
550 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 * @param array
552 * @return void
553 */
Greg Akerf5c84022011-04-19 17:13:03 -0500554 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 {
556 $this->helper($helpers);
557 }
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 * Loads a language file
563 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 * @param array
565 * @param string
566 * @return void
567 */
Greg Akerf5c84022011-04-19 17:13:03 -0500568 public function language($file = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
570 $CI =& get_instance();
571
572 if ( ! is_array($file))
573 {
574 $file = array($file);
575 }
576
577 foreach ($file as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200578 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 $CI->lang->load($langfile, $lang);
580 }
581 }
Barry Mienydd671972010-10-04 16:33:58 +0200582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 /**
586 * Loads a config file
587 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200589 * @param bool
590 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 * @return void
592 */
Greg Akerf5c84022011-04-19 17:13:03 -0500593 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200594 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 $CI =& get_instance();
596 $CI->config->load($file, $use_sections, $fail_gracefully);
597 }
598
599 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 /**
Derek Jones8dca0412010-03-05 13:01:44 -0600602 * Driver
603 *
604 * Loads a driver library
605 *
606 * @param string the name of the class
607 * @param mixed the optional parameters
608 * @param string an optional object name
609 * @return void
610 */
Greg Akerf5c84022011-04-19 17:13:03 -0500611 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600612 {
613 if ( ! class_exists('CI_Driver_Library'))
614 {
615 // we aren't instantiating an object here, that'll be done by the Library itself
Greg Aker3a746652011-04-19 10:59:47 -0500616 require BASEPATH.'libraries/Driver.php';
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600617 }
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600619 // We can save the loader some time since Drivers will *always* be in a subfolder,
620 // and typically identically named to the library
621 if ( ! strpos($library, '/'))
622 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500623 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600624 }
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Jones8dca0412010-03-05 13:01:44 -0600626 return $this->library($library, $params, $object_name);
627 }
628
629 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200630
Derek Jones8dca0412010-03-05 13:01:44 -0600631 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600632 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 *
Derek Jones32bf1862010-03-02 13:46:07 -0600634 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200637 * @param boolean
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600639 */
Greg Akerf5c84022011-04-19 17:13:03 -0500640 public function add_package_path($path, $view_cascade=TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600641 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500642 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200643
Derek Jones32bf1862010-03-02 13:46:07 -0600644 array_unshift($this->_ci_library_paths, $path);
645 array_unshift($this->_ci_model_paths, $path);
646 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200647
Greg Akerf5c84022011-04-19 17:13:03 -0500648 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
649
Derek Jones32bf1862010-03-02 13:46:07 -0600650 // Add config file path
651 $config =& $this->_ci_get_component('config');
652 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 }
654
655 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600656
657 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000658 * Get Package Paths
659 *
660 * Return a list of all package paths, by default it will ignore BASEPATH.
661 *
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000662 * @param string
663 * @return void
664 */
Greg Akerf5c84022011-04-19 17:13:03 -0500665 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000666 {
667 return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
668 }
669
670 // --------------------------------------------------------------------
671
672 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600673 * Remove Package Path
674 *
675 * Remove a path from the library, model, and helper path arrays if it exists
676 * If no path is provided, the most recently added path is removed.
677 *
Derek Jones32bf1862010-03-02 13:46:07 -0600678 * @param type
David Behlercda768a2011-08-14 23:52:48 +0200679 * @param bool
Derek Jones32bf1862010-03-02 13:46:07 -0600680 * @return type
681 */
Greg Akerf5c84022011-04-19 17:13:03 -0500682 public function remove_package_path($path = '', $remove_config_path = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600683 {
684 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200685
Derek Jones32bf1862010-03-02 13:46:07 -0600686 if ($path == '')
687 {
688 $void = array_shift($this->_ci_library_paths);
689 $void = array_shift($this->_ci_model_paths);
690 $void = array_shift($this->_ci_helper_paths);
Greg Akerf5c84022011-04-19 17:13:03 -0500691 $void = array_shift($this->_ci_view_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600692 $void = array_shift($config->_config_paths);
693 }
694 else
695 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500696 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600697 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
698 {
699 if (($key = array_search($path, $this->{$var})) !== FALSE)
700 {
701 unset($this->{$var}[$key]);
702 }
703 }
David Behlercda768a2011-08-14 23:52:48 +0200704
Greg Akerf5c84022011-04-19 17:13:03 -0500705 if (isset($this->_ci_view_paths[$path.'views/']))
706 {
707 unset($this->_ci_view_paths[$path.'views/']);
708 }
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Jones32bf1862010-03-02 13:46:07 -0600710 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
711 {
712 unset($config->_config_paths[$key]);
713 }
714 }
Barry Mienydd671972010-10-04 16:33:58 +0200715
Derek Jones32bf1862010-03-02 13:46:07 -0600716 // make sure the application default paths are still in the array
717 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
718 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
719 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500720 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600721 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
722 }
723
724 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200725
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 /**
727 * Loader
728 *
729 * This function is used to load views and files.
730 * Variables are prefixed with _ci_ to avoid symbol collision with
731 * variables made available to view files
732 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 * @param array
734 * @return void
735 */
Greg Akerf5c84022011-04-19 17:13:03 -0500736 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 {
738 // Set the default data variables
739 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
740 {
741 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
742 }
David Behlercda768a2011-08-14 23:52:48 +0200743
Greg Akerf5c84022011-04-19 17:13:03 -0500744 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000745
746 // Set the path to the requested file
Greg Aker8807be32011-04-21 13:06:15 -0500747 if ($_ci_path != '')
748 {
749 $_ci_x = explode('/', $_ci_path);
750 $_ci_file = end($_ci_x);
751 }
752 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 {
754 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Greg Aker3a746652011-04-19 10:59:47 -0500755 $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500756
757 foreach ($this->_ci_view_paths as $view_file => $cascade)
758 {
759 if (file_exists($view_file.$_ci_file))
760 {
761 $_ci_path = $view_file.$_ci_file;
762 $file_exists = TRUE;
763 break;
764 }
David Behlercda768a2011-08-14 23:52:48 +0200765
Greg Akerf5c84022011-04-19 17:13:03 -0500766 if ( ! $cascade)
767 {
768 break;
David Behlercda768a2011-08-14 23:52:48 +0200769 }
Greg Akerf5c84022011-04-19 17:13:03 -0500770 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 }
Barry Mienydd671972010-10-04 16:33:58 +0200772
Greg Akerf5c84022011-04-19 17:13:03 -0500773 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 {
775 show_error('Unable to load the requested file: '.$_ci_file);
776 }
Barry Mienydd671972010-10-04 16:33:58 +0200777
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 // This allows anything loaded using $this->load (views, files, etc.)
779 // to become accessible from within the Controller and Model functions.
Barry Mienydd671972010-10-04 16:33:58 +0200780
Pascal Kriete89ace432010-11-10 15:49:10 -0500781 $_ci_CI =& get_instance();
782 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500784 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500786 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 }
788 }
789
790 /*
791 * Extract and cache variables
792 *
793 * You can either set variables using the dedicated $this->load_vars()
794 * function or via the second parameter of this function. We'll merge
795 * the two types and cache them so that views that are embedded within
796 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200797 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 if (is_array($_ci_vars))
799 {
800 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
801 }
802 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 /*
805 * Buffer the output
806 *
807 * We buffer the output for two reasons:
808 * 1. Speed. You get a significant speed boost.
809 * 2. So that the final rendered template can be
Derek Jones37f4b9c2011-07-01 17:56:50 -0500810 * post-processed by the output class. Why do we
811 * need post processing? For one thing, in order to
812 * show the elapsed page load time. Unless we
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 * can intercept the content right before it's sent to
814 * the browser and then stop the timer it won't be accurate.
815 */
816 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200817
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 // If the PHP installation does not support short tags we'll
819 // do a little string replacement, changing the short tags
820 // to standard PHP echo statements.
Barry Mienydd671972010-10-04 16:33:58 +0200821
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
823 {
824 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
825 }
826 else
827 {
828 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
829 }
Barry Mienydd671972010-10-04 16:33:58 +0200830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 // Return the file data if requested
834 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200835 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 $buffer = ob_get_contents();
837 @ob_end_clean();
838 return $buffer;
839 }
840
841 /*
842 * Flush the buffer... or buff the flusher?
843 *
844 * In order to permit views to be nested within
845 * other views, we need to flush the content back out whenever
846 * we are beyond the first level of output buffering so that
847 * it can be seen and included properly by the first included
848 * template and any subsequent ones. Oy!
849 *
Barry Mienydd671972010-10-04 16:33:58 +0200850 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 if (ob_get_level() > $this->_ci_ob_level + 1)
852 {
853 ob_end_flush();
854 }
855 else
856 {
Greg Aker22f1a632010-11-10 15:34:35 -0600857 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 @ob_end_clean();
859 }
860 }
861
862 // --------------------------------------------------------------------
863
864 /**
865 * Load class
866 *
867 * This function loads the requested class.
868 *
Barry Mienydd671972010-10-04 16:33:58 +0200869 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 * @param mixed any additional parameters
871 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200872 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 */
Greg Akerf5c84022011-04-19 17:13:03 -0500874 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200875 {
876 // Get the class name, and while we're at it trim any slashes.
877 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500879 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 // Was the path included with the class name?
882 // We look for a slash to determine this
883 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600884 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
Derek Jones32bf1862010-03-02 13:46:07 -0600886 // Extract the path
887 $subdir = substr($class, 0, $last_slash + 1);
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Jones32bf1862010-03-02 13:46:07 -0600889 // Get the filename from the path
890 $class = substr($class, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 }
892
893 // We'll test for both lowercase and capitalized versions of the file name
894 foreach (array(ucfirst($class), strtolower($class)) as $class)
895 {
Greg Aker3a746652011-04-19 10:59:47 -0500896 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000897
Barry Mienydd671972010-10-04 16:33:58 +0200898 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 if (file_exists($subclass))
900 {
Greg Aker3a746652011-04-19 10:59:47 -0500901 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200902
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 if ( ! file_exists($baseclass))
904 {
905 log_message('error', "Unable to load the requested class: ".$class);
906 show_error("Unable to load the requested class: ".$class);
907 }
908
Derek Jones37f4b9c2011-07-01 17:56:50 -0500909 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 if (in_array($subclass, $this->_ci_loaded_files))
911 {
912 // Before we deem this to be a duplicate request, let's see
Derek Jones37f4b9c2011-07-01 17:56:50 -0500913 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 // return a new instance of the object
915 if ( ! is_null($object_name))
916 {
917 $CI =& get_instance();
918 if ( ! isset($CI->$object_name))
919 {
Barry Mienydd671972010-10-04 16:33:58 +0200920 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 }
922 }
Barry Mienydd671972010-10-04 16:33:58 +0200923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 $is_duplicate = TRUE;
925 log_message('debug', $class." class already loaded. Second attempt ignored.");
926 return;
927 }
Barry Mienydd671972010-10-04 16:33:58 +0200928
929 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 include_once($subclass);
931 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200932
933 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 }
Barry Mienydd671972010-10-04 16:33:58 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600937 $is_duplicate = FALSE;
938 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
Greg Aker3a746652011-04-19 10:59:47 -0500940 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600941
Derek Jones37f4b9c2011-07-01 17:56:50 -0500942 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 if ( ! file_exists($filepath))
944 {
945 continue;
946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Jones37f4b9c2011-07-01 17:56:50 -0500948 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 if (in_array($filepath, $this->_ci_loaded_files))
950 {
951 // Before we deem this to be a duplicate request, let's see
Derek Jones37f4b9c2011-07-01 17:56:50 -0500952 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 // return a new instance of the object
954 if ( ! is_null($object_name))
955 {
956 $CI =& get_instance();
957 if ( ! isset($CI->$object_name))
958 {
959 return $this->_ci_init_class($class, '', $params, $object_name);
960 }
961 }
Barry Mienydd671972010-10-04 16:33:58 +0200962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 $is_duplicate = TRUE;
964 log_message('debug', $class." class already loaded. Second attempt ignored.");
965 return;
966 }
Barry Mienydd671972010-10-04 16:33:58 +0200967
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 include_once($filepath);
969 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +0200970 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 }
Derek Jones32bf1862010-03-02 13:46:07 -0600972
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 } // END FOREACH
974
Derek Jones37f4b9c2011-07-01 17:56:50 -0500975 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 if ($subdir == '')
977 {
978 $path = strtolower($class).'/'.$class;
979 return $this->_ci_load_class($path, $params);
980 }
Barry Mienydd671972010-10-04 16:33:58 +0200981
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 // If we got this far we were unable to find the requested class.
983 // We do not issue errors if the load call failed due to a duplicate request
984 if ($is_duplicate == FALSE)
985 {
986 log_message('error', "Unable to load the requested class: ".$class);
987 show_error("Unable to load the requested class: ".$class);
988 }
989 }
Barry Mienydd671972010-10-04 16:33:58 +0200990
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 // --------------------------------------------------------------------
992
993 /**
994 * Instantiates a class
995 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 * @param string
997 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200998 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 * @param string an optional object name
1000 * @return null
1001 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001002 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001003 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001004 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 if ($config === NULL)
1006 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001007 // Fetch the config paths containing any package paths
1008 $config_component = $this->_ci_get_component('config');
1009
1010 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001012 // Break on the first found file, thus package files
1013 // are not overridden by default paths
1014 foreach ($config_component->_config_paths as $path)
1015 {
1016 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001017 // are case-sensitive with regard to file names. Check for environment
1018 // first, global next
Greg Aker3a746652011-04-19 10:59:47 -05001019 if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001020 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001021 include($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001022 break;
1023 }
Greg Aker3a746652011-04-19 10:59:47 -05001024 elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001025 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001026 include($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001027 break;
1028 }
Greg Aker3a746652011-04-19 10:59:47 -05001029 elseif (file_exists($path .'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001030 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001031 include($path .'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001032 break;
1033 }
Greg Aker3a746652011-04-19 10:59:47 -05001034 elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001035 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001036 include($path .'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001037 break;
1038 }
1039 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 }
1041 }
Barry Mienydd671972010-10-04 16:33:58 +02001042
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +02001044 {
1045 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 {
1047 $name = 'CI_'.$class;
1048 }
Barry Mienydd671972010-10-04 16:33:58 +02001049 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 {
1051 $name = config_item('subclass_prefix').$class;
1052 }
1053 else
1054 {
1055 $name = $class;
1056 }
1057 }
1058 else
1059 {
1060 $name = $prefix.$class;
1061 }
Barry Mienydd671972010-10-04 16:33:58 +02001062
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 // Is the class name valid?
1064 if ( ! class_exists($name))
1065 {
1066 log_message('error', "Non-existent class: ".$name);
1067 show_error("Non-existent class: ".$class);
1068 }
Barry Mienydd671972010-10-04 16:33:58 +02001069
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 // Set the variable name we will assign the class to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001071 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001073
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 if (is_null($object_name))
1075 {
1076 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
1077 }
1078 else
1079 {
1080 $classvar = $object_name;
1081 }
1082
Barry Mienydd671972010-10-04 16:33:58 +02001083 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 $this->_ci_classes[$class] = $classvar;
1085
Barry Mienydd671972010-10-04 16:33:58 +02001086 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 $CI =& get_instance();
1088 if ($config !== NULL)
1089 {
1090 $CI->$classvar = new $name($config);
1091 }
1092 else
Barry Mienydd671972010-10-04 16:33:58 +02001093 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 $CI->$classvar = new $name;
Barry Mienydd671972010-10-04 16:33:58 +02001095 }
1096 }
1097
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001099
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 /**
1101 * Autoloader
1102 *
1103 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -06001104 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 * @param array
1107 * @return void
1108 */
Shane Pearson665baec2011-08-22 18:52:19 -05001109 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001110 {
Greg Aker3a746652011-04-19 10:59:47 -05001111 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001112 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001113 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001114 }
1115 else
1116 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001117 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001118 }
Barry Mienydd671972010-10-04 16:33:58 +02001119
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 if ( ! isset($autoload))
1121 {
1122 return FALSE;
1123 }
Barry Mienydd671972010-10-04 16:33:58 +02001124
Phil Sturgeon9730c752010-12-15 10:50:15 +00001125 // Autoload packages
1126 if (isset($autoload['packages']))
1127 {
1128 foreach ($autoload['packages'] as $package_path)
1129 {
1130 $this->add_package_path($package_path);
1131 }
1132 }
1133
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 // Load any custom config file
1135 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001136 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 $CI =& get_instance();
1138 foreach ($autoload['config'] as $key => $val)
1139 {
1140 $CI->config->load($val);
1141 }
Barry Mienydd671972010-10-04 16:33:58 +02001142 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001143
Derek Jonesc6da5032010-03-09 20:44:27 -06001144 // Autoload helpers and languages
1145 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001146 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
1148 {
1149 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001150 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 }
1152
1153 // A little tweak to remain backward compatible
1154 // The $autoload['core'] item was deprecated
Derek Jones32bf1862010-03-02 13:46:07 -06001155 if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 {
1157 $autoload['libraries'] = $autoload['core'];
1158 }
Barry Mienydd671972010-10-04 16:33:58 +02001159
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 // Load libraries
1161 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
1162 {
1163 // Load the database driver.
1164 if (in_array('database', $autoload['libraries']))
1165 {
1166 $this->database();
1167 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1168 }
Barry Mienydd671972010-10-04 16:33:58 +02001169
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 // Load all other libraries
1171 foreach ($autoload['libraries'] as $item)
1172 {
1173 $this->library($item);
1174 }
Barry Mienydd671972010-10-04 16:33:58 +02001175 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001176
1177 // Autoload models
1178 if (isset($autoload['model']))
1179 {
1180 $this->model($autoload['model']);
1181 }
Barry Mienydd671972010-10-04 16:33:58 +02001182 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001183
1184 // --------------------------------------------------------------------
1185
1186 /**
1187 * Object to Array
1188 *
1189 * Takes an object as input and converts the class variables to array key/vals
1190 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 * @param object
1192 * @return array
1193 */
Greg Akerf5c84022011-04-19 17:13:03 -05001194 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 {
1196 return (is_object($object)) ? get_object_vars($object) : $object;
1197 }
1198
1199 // --------------------------------------------------------------------
1200
1201 /**
Derek Jones32bf1862010-03-02 13:46:07 -06001202 * Get a reference to a specific library or model
1203 *
David Behlercda768a2011-08-14 23:52:48 +02001204 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001205 * @return bool
1206 */
Greg Akerf5c84022011-04-19 17:13:03 -05001207 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001208 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001209 $CI =& get_instance();
1210 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001211 }
1212
1213 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001214
Derek Jones32bf1862010-03-02 13:46:07 -06001215 /**
1216 * Prep filename
1217 *
1218 * This function preps the name of various items to make loading them more reliable.
1219 *
Derek Jones32bf1862010-03-02 13:46:07 -06001220 * @param mixed
David Behlercda768a2011-08-14 23:52:48 +02001221 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001222 * @return array
1223 */
Greg Akerf5c84022011-04-19 17:13:03 -05001224 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001225 {
1226 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001227 {
Greg Aker3a746652011-04-19 10:59:47 -05001228 return array(strtolower(str_replace('.php', '', str_replace($extension, '', $filename)).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001229 }
1230 else
1231 {
1232 foreach ($filename as $key => $val)
1233 {
Greg Aker3a746652011-04-19 10:59:47 -05001234 $filename[$key] = strtolower(str_replace('.php', '', str_replace($extension, '', $val)).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001235 }
Barry Mienydd671972010-10-04 16:33:58 +02001236
Derek Jones32bf1862010-03-02 13:46:07 -06001237 return $filename;
1238 }
1239 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001240}
1241
1242/* End of file Loader.php */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001243/* Location: ./system/core/Loader.php */