blob: 5539aae14a4bdba21009395c0ac14d6e2f2e1ac1 [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
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200619 if ($library == '')
620 {
621 return FALSE;
622 }
623
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600624 // We can save the loader some time since Drivers will *always* be in a subfolder,
625 // and typically identically named to the library
626 if ( ! strpos($library, '/'))
627 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500628 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600629 }
Barry Mienydd671972010-10-04 16:33:58 +0200630
Derek Jones8dca0412010-03-05 13:01:44 -0600631 return $this->library($library, $params, $object_name);
632 }
633
634 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200635
Derek Jones8dca0412010-03-05 13:01:44 -0600636 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600637 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 *
Derek Jones32bf1862010-03-02 13:46:07 -0600639 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200642 * @param boolean
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600644 */
Greg Akerf5c84022011-04-19 17:13:03 -0500645 public function add_package_path($path, $view_cascade=TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600646 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500647 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200648
Derek Jones32bf1862010-03-02 13:46:07 -0600649 array_unshift($this->_ci_library_paths, $path);
650 array_unshift($this->_ci_model_paths, $path);
651 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200652
Greg Akerf5c84022011-04-19 17:13:03 -0500653 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
654
Derek Jones32bf1862010-03-02 13:46:07 -0600655 // Add config file path
656 $config =& $this->_ci_get_component('config');
657 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 }
659
660 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600661
662 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000663 * Get Package Paths
664 *
665 * Return a list of all package paths, by default it will ignore BASEPATH.
666 *
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000667 * @param string
668 * @return void
669 */
Greg Akerf5c84022011-04-19 17:13:03 -0500670 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000671 {
672 return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
673 }
674
675 // --------------------------------------------------------------------
676
677 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600678 * Remove Package Path
679 *
680 * Remove a path from the library, model, and helper path arrays if it exists
681 * If no path is provided, the most recently added path is removed.
682 *
Derek Jones32bf1862010-03-02 13:46:07 -0600683 * @param type
David Behlercda768a2011-08-14 23:52:48 +0200684 * @param bool
Derek Jones32bf1862010-03-02 13:46:07 -0600685 * @return type
686 */
Greg Akerf5c84022011-04-19 17:13:03 -0500687 public function remove_package_path($path = '', $remove_config_path = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600688 {
689 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200690
Derek Jones32bf1862010-03-02 13:46:07 -0600691 if ($path == '')
692 {
693 $void = array_shift($this->_ci_library_paths);
694 $void = array_shift($this->_ci_model_paths);
695 $void = array_shift($this->_ci_helper_paths);
Greg Akerf5c84022011-04-19 17:13:03 -0500696 $void = array_shift($this->_ci_view_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600697 $void = array_shift($config->_config_paths);
698 }
699 else
700 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500701 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600702 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
703 {
704 if (($key = array_search($path, $this->{$var})) !== FALSE)
705 {
706 unset($this->{$var}[$key]);
707 }
708 }
David Behlercda768a2011-08-14 23:52:48 +0200709
Greg Akerf5c84022011-04-19 17:13:03 -0500710 if (isset($this->_ci_view_paths[$path.'views/']))
711 {
712 unset($this->_ci_view_paths[$path.'views/']);
713 }
Barry Mienydd671972010-10-04 16:33:58 +0200714
Derek Jones32bf1862010-03-02 13:46:07 -0600715 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
716 {
717 unset($config->_config_paths[$key]);
718 }
719 }
Barry Mienydd671972010-10-04 16:33:58 +0200720
Derek Jones32bf1862010-03-02 13:46:07 -0600721 // make sure the application default paths are still in the array
722 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
723 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
724 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500725 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600726 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
727 }
728
729 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200730
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 /**
732 * Loader
733 *
734 * This function is used to load views and files.
735 * Variables are prefixed with _ci_ to avoid symbol collision with
736 * variables made available to view files
737 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 * @param array
739 * @return void
740 */
Greg Akerf5c84022011-04-19 17:13:03 -0500741 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 {
743 // Set the default data variables
744 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
745 {
746 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
747 }
David Behlercda768a2011-08-14 23:52:48 +0200748
Greg Akerf5c84022011-04-19 17:13:03 -0500749 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000750
751 // Set the path to the requested file
Greg Aker8807be32011-04-21 13:06:15 -0500752 if ($_ci_path != '')
753 {
754 $_ci_x = explode('/', $_ci_path);
755 $_ci_file = end($_ci_x);
756 }
757 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 {
759 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Greg Aker3a746652011-04-19 10:59:47 -0500760 $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500761
762 foreach ($this->_ci_view_paths as $view_file => $cascade)
763 {
764 if (file_exists($view_file.$_ci_file))
765 {
766 $_ci_path = $view_file.$_ci_file;
767 $file_exists = TRUE;
768 break;
769 }
David Behlercda768a2011-08-14 23:52:48 +0200770
Greg Akerf5c84022011-04-19 17:13:03 -0500771 if ( ! $cascade)
772 {
773 break;
David Behlercda768a2011-08-14 23:52:48 +0200774 }
Greg Akerf5c84022011-04-19 17:13:03 -0500775 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 }
Barry Mienydd671972010-10-04 16:33:58 +0200777
Greg Akerf5c84022011-04-19 17:13:03 -0500778 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 {
780 show_error('Unable to load the requested file: '.$_ci_file);
781 }
Barry Mienydd671972010-10-04 16:33:58 +0200782
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 // This allows anything loaded using $this->load (views, files, etc.)
784 // to become accessible from within the Controller and Model functions.
Barry Mienydd671972010-10-04 16:33:58 +0200785
Pascal Kriete89ace432010-11-10 15:49:10 -0500786 $_ci_CI =& get_instance();
787 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500789 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500791 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 }
793 }
794
795 /*
796 * Extract and cache variables
797 *
798 * You can either set variables using the dedicated $this->load_vars()
799 * function or via the second parameter of this function. We'll merge
800 * the two types and cache them so that views that are embedded within
801 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200802 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 if (is_array($_ci_vars))
804 {
805 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
806 }
807 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200808
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 /*
810 * Buffer the output
811 *
812 * We buffer the output for two reasons:
813 * 1. Speed. You get a significant speed boost.
814 * 2. So that the final rendered template can be
Derek Jones37f4b9c2011-07-01 17:56:50 -0500815 * post-processed by the output class. Why do we
816 * need post processing? For one thing, in order to
817 * show the elapsed page load time. Unless we
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 * can intercept the content right before it's sent to
819 * the browser and then stop the timer it won't be accurate.
820 */
821 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200822
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 // If the PHP installation does not support short tags we'll
824 // do a little string replacement, changing the short tags
825 // to standard PHP echo statements.
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
828 {
829 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
830 }
831 else
832 {
833 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
834 }
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200837
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 // Return the file data if requested
839 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200840 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 $buffer = ob_get_contents();
842 @ob_end_clean();
843 return $buffer;
844 }
845
846 /*
847 * Flush the buffer... or buff the flusher?
848 *
849 * In order to permit views to be nested within
850 * other views, we need to flush the content back out whenever
851 * we are beyond the first level of output buffering so that
852 * it can be seen and included properly by the first included
853 * template and any subsequent ones. Oy!
854 *
Barry Mienydd671972010-10-04 16:33:58 +0200855 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 if (ob_get_level() > $this->_ci_ob_level + 1)
857 {
858 ob_end_flush();
859 }
860 else
861 {
Greg Aker22f1a632010-11-10 15:34:35 -0600862 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 @ob_end_clean();
864 }
865 }
866
867 // --------------------------------------------------------------------
868
869 /**
870 * Load class
871 *
872 * This function loads the requested class.
873 *
Barry Mienydd671972010-10-04 16:33:58 +0200874 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 * @param mixed any additional parameters
876 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200877 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 */
Greg Akerf5c84022011-04-19 17:13:03 -0500879 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200880 {
881 // Get the class name, and while we're at it trim any slashes.
882 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500884 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 // Was the path included with the class name?
887 // We look for a slash to determine this
888 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600889 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 {
Derek Jones32bf1862010-03-02 13:46:07 -0600891 // Extract the path
892 $subdir = substr($class, 0, $last_slash + 1);
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Jones32bf1862010-03-02 13:46:07 -0600894 // Get the filename from the path
895 $class = substr($class, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 }
897
898 // We'll test for both lowercase and capitalized versions of the file name
899 foreach (array(ucfirst($class), strtolower($class)) as $class)
900 {
Greg Aker3a746652011-04-19 10:59:47 -0500901 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000902
Barry Mienydd671972010-10-04 16:33:58 +0200903 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 if (file_exists($subclass))
905 {
Greg Aker3a746652011-04-19 10:59:47 -0500906 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200907
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 if ( ! file_exists($baseclass))
909 {
910 log_message('error', "Unable to load the requested class: ".$class);
911 show_error("Unable to load the requested class: ".$class);
912 }
913
Derek Jones37f4b9c2011-07-01 17:56:50 -0500914 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 if (in_array($subclass, $this->_ci_loaded_files))
916 {
917 // Before we deem this to be a duplicate request, let's see
Derek Jones37f4b9c2011-07-01 17:56:50 -0500918 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 // return a new instance of the object
920 if ( ! is_null($object_name))
921 {
922 $CI =& get_instance();
923 if ( ! isset($CI->$object_name))
924 {
Barry Mienydd671972010-10-04 16:33:58 +0200925 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 }
927 }
Barry Mienydd671972010-10-04 16:33:58 +0200928
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 $is_duplicate = TRUE;
930 log_message('debug', $class." class already loaded. Second attempt ignored.");
931 return;
932 }
Barry Mienydd671972010-10-04 16:33:58 +0200933
934 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 include_once($subclass);
936 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200937
938 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 }
Barry Mienydd671972010-10-04 16:33:58 +0200940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600942 $is_duplicate = FALSE;
943 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 {
Greg Aker3a746652011-04-19 10:59:47 -0500945 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600946
Derek Jones37f4b9c2011-07-01 17:56:50 -0500947 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 if ( ! file_exists($filepath))
949 {
950 continue;
951 }
Barry Mienydd671972010-10-04 16:33:58 +0200952
Derek Jones37f4b9c2011-07-01 17:56:50 -0500953 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 if (in_array($filepath, $this->_ci_loaded_files))
955 {
956 // Before we deem this to be a duplicate request, let's see
Derek Jones37f4b9c2011-07-01 17:56:50 -0500957 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 // return a new instance of the object
959 if ( ! is_null($object_name))
960 {
961 $CI =& get_instance();
962 if ( ! isset($CI->$object_name))
963 {
964 return $this->_ci_init_class($class, '', $params, $object_name);
965 }
966 }
Barry Mienydd671972010-10-04 16:33:58 +0200967
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 $is_duplicate = TRUE;
969 log_message('debug', $class." class already loaded. Second attempt ignored.");
970 return;
971 }
Barry Mienydd671972010-10-04 16:33:58 +0200972
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 include_once($filepath);
974 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +0200975 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 }
Derek Jones32bf1862010-03-02 13:46:07 -0600977
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 } // END FOREACH
979
Derek Jones37f4b9c2011-07-01 17:56:50 -0500980 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 if ($subdir == '')
982 {
983 $path = strtolower($class).'/'.$class;
984 return $this->_ci_load_class($path, $params);
985 }
Barry Mienydd671972010-10-04 16:33:58 +0200986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 // If we got this far we were unable to find the requested class.
988 // We do not issue errors if the load call failed due to a duplicate request
989 if ($is_duplicate == FALSE)
990 {
991 log_message('error', "Unable to load the requested class: ".$class);
992 show_error("Unable to load the requested class: ".$class);
993 }
994 }
Barry Mienydd671972010-10-04 16:33:58 +0200995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 // --------------------------------------------------------------------
997
998 /**
999 * Instantiates a class
1000 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 * @param string
1002 * @param string
David Behlercda768a2011-08-14 23:52:48 +02001003 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 * @param string an optional object name
1005 * @return null
1006 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001007 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001008 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001009 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 if ($config === NULL)
1011 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001012 // Fetch the config paths containing any package paths
1013 $config_component = $this->_ci_get_component('config');
1014
1015 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001017 // Break on the first found file, thus package files
1018 // are not overridden by default paths
1019 foreach ($config_component->_config_paths as $path)
1020 {
1021 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001022 // are case-sensitive with regard to file names. Check for environment
1023 // first, global next
Greg Aker3a746652011-04-19 10:59:47 -05001024 if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001025 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001026 include($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001027 break;
1028 }
Greg Aker3a746652011-04-19 10:59:47 -05001029 elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001030 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001031 include($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001032 break;
1033 }
Greg Aker3a746652011-04-19 10:59:47 -05001034 elseif (file_exists($path .'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001035 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001036 include($path .'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001037 break;
1038 }
Greg Aker3a746652011-04-19 10:59:47 -05001039 elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001040 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001041 include($path .'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001042 break;
1043 }
1044 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 }
1046 }
Barry Mienydd671972010-10-04 16:33:58 +02001047
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +02001049 {
1050 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 {
1052 $name = 'CI_'.$class;
1053 }
Barry Mienydd671972010-10-04 16:33:58 +02001054 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 {
1056 $name = config_item('subclass_prefix').$class;
1057 }
1058 else
1059 {
1060 $name = $class;
1061 }
1062 }
1063 else
1064 {
1065 $name = $prefix.$class;
1066 }
Barry Mienydd671972010-10-04 16:33:58 +02001067
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 // Is the class name valid?
1069 if ( ! class_exists($name))
1070 {
1071 log_message('error', "Non-existent class: ".$name);
1072 show_error("Non-existent class: ".$class);
1073 }
Barry Mienydd671972010-10-04 16:33:58 +02001074
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 // Set the variable name we will assign the class to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001076 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001078
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 if (is_null($object_name))
1080 {
1081 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
1082 }
1083 else
1084 {
1085 $classvar = $object_name;
1086 }
1087
Barry Mienydd671972010-10-04 16:33:58 +02001088 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 $this->_ci_classes[$class] = $classvar;
1090
Barry Mienydd671972010-10-04 16:33:58 +02001091 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 $CI =& get_instance();
1093 if ($config !== NULL)
1094 {
1095 $CI->$classvar = new $name($config);
1096 }
1097 else
Barry Mienydd671972010-10-04 16:33:58 +02001098 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 $CI->$classvar = new $name;
Barry Mienydd671972010-10-04 16:33:58 +02001100 }
1101 }
1102
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001104
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 /**
1106 * Autoloader
1107 *
1108 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -06001109 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 * @param array
1112 * @return void
1113 */
Shane Pearson665baec2011-08-22 18:52:19 -05001114 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001115 {
Greg Aker3a746652011-04-19 10:59:47 -05001116 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001117 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001118 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001119 }
1120 else
1121 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001122 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001123 }
Barry Mienydd671972010-10-04 16:33:58 +02001124
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 if ( ! isset($autoload))
1126 {
1127 return FALSE;
1128 }
Barry Mienydd671972010-10-04 16:33:58 +02001129
Phil Sturgeon9730c752010-12-15 10:50:15 +00001130 // Autoload packages
1131 if (isset($autoload['packages']))
1132 {
1133 foreach ($autoload['packages'] as $package_path)
1134 {
1135 $this->add_package_path($package_path);
1136 }
1137 }
1138
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 // Load any custom config file
1140 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001141 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 $CI =& get_instance();
1143 foreach ($autoload['config'] as $key => $val)
1144 {
1145 $CI->config->load($val);
1146 }
Barry Mienydd671972010-10-04 16:33:58 +02001147 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001148
Derek Jonesc6da5032010-03-09 20:44:27 -06001149 // Autoload helpers and languages
1150 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001151 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
1153 {
1154 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001155 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 }
1157
1158 // A little tweak to remain backward compatible
1159 // The $autoload['core'] item was deprecated
Derek Jones32bf1862010-03-02 13:46:07 -06001160 if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 {
1162 $autoload['libraries'] = $autoload['core'];
1163 }
Barry Mienydd671972010-10-04 16:33:58 +02001164
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 // Load libraries
1166 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
1167 {
1168 // Load the database driver.
1169 if (in_array('database', $autoload['libraries']))
1170 {
1171 $this->database();
1172 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1173 }
Barry Mienydd671972010-10-04 16:33:58 +02001174
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 // Load all other libraries
1176 foreach ($autoload['libraries'] as $item)
1177 {
1178 $this->library($item);
1179 }
Barry Mienydd671972010-10-04 16:33:58 +02001180 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001181
1182 // Autoload models
1183 if (isset($autoload['model']))
1184 {
1185 $this->model($autoload['model']);
1186 }
Barry Mienydd671972010-10-04 16:33:58 +02001187 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001188
1189 // --------------------------------------------------------------------
1190
1191 /**
1192 * Object to Array
1193 *
1194 * Takes an object as input and converts the class variables to array key/vals
1195 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 * @param object
1197 * @return array
1198 */
Greg Akerf5c84022011-04-19 17:13:03 -05001199 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 {
1201 return (is_object($object)) ? get_object_vars($object) : $object;
1202 }
1203
1204 // --------------------------------------------------------------------
1205
1206 /**
Derek Jones32bf1862010-03-02 13:46:07 -06001207 * Get a reference to a specific library or model
1208 *
David Behlercda768a2011-08-14 23:52:48 +02001209 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001210 * @return bool
1211 */
Greg Akerf5c84022011-04-19 17:13:03 -05001212 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001213 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001214 $CI =& get_instance();
1215 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001216 }
1217
1218 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001219
Derek Jones32bf1862010-03-02 13:46:07 -06001220 /**
1221 * Prep filename
1222 *
1223 * This function preps the name of various items to make loading them more reliable.
1224 *
Derek Jones32bf1862010-03-02 13:46:07 -06001225 * @param mixed
David Behlercda768a2011-08-14 23:52:48 +02001226 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001227 * @return array
1228 */
Greg Akerf5c84022011-04-19 17:13:03 -05001229 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001230 {
1231 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001232 {
Greg Aker3a746652011-04-19 10:59:47 -05001233 return array(strtolower(str_replace('.php', '', str_replace($extension, '', $filename)).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001234 }
1235 else
1236 {
1237 foreach ($filename as $key => $val)
1238 {
Greg Aker3a746652011-04-19 10:59:47 -05001239 $filename[$key] = strtolower(str_replace('.php', '', str_replace($extension, '', $val)).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001240 }
Barry Mienydd671972010-10-04 16:33:58 +02001241
Derek Jones32bf1862010-03-02 13:46:07 -06001242 return $filename;
1243 }
1244 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001245}
1246
1247/* End of file Loader.php */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001248/* Location: ./system/core/Loader.php */