blob: 9b9cc2fef46a88b62fffbfc0f705db722cff9f16 [file] [log] [blame]
Andrey Andreevd7297352012-01-07 22:53:14 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevd7297352012-01-07 22:53:14 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevd7297352012-01-07 22:53:14 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Loader Class
32 *
33 * Loads views and files
34 *
35 * @package CodeIgniter
36 * @subpackage Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @category Loader
39 * @link http://codeigniter.com/user_guide/libraries/loader.html
40 */
41class CI_Loader {
42
43 // All these are set automatically. Don't mess with them.
David Behlercda768a2011-08-14 23:52:48 +020044 /**
45 * Nesting level of the output buffering mechanism
46 *
47 * @var int
David Behlercda768a2011-08-14 23:52:48 +020048 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050049 protected $_ci_ob_level;
David Behlercda768a2011-08-14 23:52:48 +020050 /**
51 * List of paths to load views from
52 *
53 * @var array
David Behlercda768a2011-08-14 23:52:48 +020054 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050055 protected $_ci_view_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020056 /**
57 * List of paths to load libraries from
58 *
59 * @var array
David Behlercda768a2011-08-14 23:52:48 +020060 */
Greg Akerf5c84022011-04-19 17:13:03 -050061 protected $_ci_library_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020062 /**
63 * List of paths to load models from
64 *
65 * @var array
David Behlercda768a2011-08-14 23:52:48 +020066 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050067 protected $_ci_model_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020068 /**
69 * List of paths to load helpers from
70 *
71 * @var array
David Behlercda768a2011-08-14 23:52:48 +020072 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050073 protected $_ci_helper_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020074 /**
75 * List of loaded base classes
David Behlercda768a2011-08-14 23:52:48 +020076 *
77 * @var array
David Behlercda768a2011-08-14 23:52:48 +020078 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050079 protected $_base_classes = array(); // Set by the controller class
David Behlercda768a2011-08-14 23:52:48 +020080 /**
81 * List of cached variables
82 *
83 * @var array
David Behlercda768a2011-08-14 23:52:48 +020084 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050085 protected $_ci_cached_vars = array();
David Behlercda768a2011-08-14 23:52:48 +020086 /**
87 * List of loaded classes
88 *
89 * @var array
David Behlercda768a2011-08-14 23:52:48 +020090 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050091 protected $_ci_classes = array();
David Behlercda768a2011-08-14 23:52:48 +020092 /**
93 * List of loaded files
94 *
95 * @var array
David Behlercda768a2011-08-14 23:52:48 +020096 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050097 protected $_ci_loaded_files = array();
David Behlercda768a2011-08-14 23:52:48 +020098 /**
99 * List of loaded models
100 *
101 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200102 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500103 protected $_ci_models = array();
David Behlercda768a2011-08-14 23:52:48 +0200104 /**
105 * List of loaded helpers
106 *
107 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200108 */
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
David Behlercda768a2011-08-14 23:52:48 +0200114 */
Andrey Andreevd7297352012-01-07 22:53:14 +0200115 protected $_ci_varmap = array(
116 'unit_test' => 'unit',
117 'user_agent' => 'agent'
118 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000119
120 /**
121 * Constructor
122 *
123 * Sets the path to the view files and gets the initial output buffering level
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 */
Greg Akerf5c84022011-04-19 17:13:03 -0500125 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +0200126 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500127 $this->_ci_ob_level = ob_get_level();
Derek Jones32bf1862010-03-02 13:46:07 -0600128 $this->_ci_library_paths = array(APPPATH, BASEPATH);
129 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
130 $this->_ci_model_paths = array(APPPATH);
Joe Cianflone8eef9c72011-08-21 10:39:06 -0400131 $this->_ci_view_paths = array(VIEWPATH => TRUE);
David Behlercda768a2011-08-14 23:52:48 +0200132
Andrey Andreevd7297352012-01-07 22:53:14 +0200133 log_message('debug', 'Loader Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200137
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500138 /**
Shane Pearson6adfe632011-08-10 16:42:53 -0500139 * Initialize the Loader
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500140 *
141 * This method is called once in CI_Controller.
142 *
David Behlercda768a2011-08-14 23:52:48 +0200143 * @param array
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500144 * @return object
145 */
Shane Pearson6adfe632011-08-10 16:42:53 -0500146 public function initialize()
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500147 {
Shane Pearson6adfe632011-08-10 16:42:53 -0500148 $this->_ci_classes = array();
149 $this->_ci_loaded_files = array();
150 $this->_ci_models = array();
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500151 $this->_base_classes =& is_loaded();
Shane Pearson6adfe632011-08-10 16:42:53 -0500152
153 $this->_ci_autoloader();
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
Andrey Andreevd47baab2012-01-09 16:56:46 +0200253 $path = substr($model, 0, ++$last_slash);
Derek Jones32bf1862010-03-02 13:46:07 -0600254
255 // And the model name behind it
Andrey Andreevd47baab2012-01-09 16:56:46 +0200256 $model = substr($model, $last_slash);
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);
Derek Jones32bf1862010-03-02 13:46:07 -0600302 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600303 $this->_ci_models[] = $name;
304 return;
305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Jones32bf1862010-03-02 13:46:07 -0600307 // couldn't find the model
308 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 /**
314 * Database Loader
315 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 * @param string the DB credentials
317 * @param bool whether to return the DB object
318 * @param bool whether to enable active record (this allows us to override the config setting)
319 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200320 */
Greg Akerf5c84022011-04-19 17:13:03 -0500321 public function database($params = '', $return = FALSE, $active_record = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 // Grab the super object
324 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // Do we even need to load the database class?
Derek Jones9fb6dd12009-12-05 15:32:48 +0000327 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 +0000328 {
329 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200330 }
331
Greg Aker3a746652011-04-19 10:59:47 -0500332 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000333
334 if ($return === TRUE)
335 {
336 return DB($params, $active_record);
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Andrey Andreevd7297352012-01-07 22:53:14 +0200339 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 // reference errors with some configurations
341 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 // Load the DB class
Barry Mienydd671972010-10-04 16:33:58 +0200344 $CI->db =& DB($params, $active_record);
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 // --------------------------------------------------------------------
348
349 /**
350 * Load the Utilities Class
351 *
Barry Mienydd671972010-10-04 16:33:58 +0200352 * @return string
353 */
Greg Akerf5c84022011-04-19 17:13:03 -0500354 public function dbutil()
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
356 if ( ! class_exists('CI_DB'))
357 {
358 $this->database();
359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 $CI =& get_instance();
362
363 // for backwards compatibility, load dbforge so we can extend dbutils off it
364 // this use is deprecated and strongly discouraged
365 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200366
Greg Aker3a746652011-04-19 10:59:47 -0500367 require_once(BASEPATH.'database/DB_utility.php');
368 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
370
Pascal Kriete58560022010-11-10 16:01:20 -0500371 $CI->dbutil = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 // --------------------------------------------------------------------
375
376 /**
377 * Load the Database Forge Class
378 *
Barry Mienydd671972010-10-04 16:33:58 +0200379 * @return string
380 */
Greg Akerf5c84022011-04-19 17:13:03 -0500381 public function dbforge()
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
383 if ( ! class_exists('CI_DB'))
384 {
385 $this->database();
386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200389
Greg Aker3a746652011-04-19 10:59:47 -0500390 require_once(BASEPATH.'database/DB_forge.php');
391 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
393
394 $CI->dbforge = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 }
Barry Mienydd671972010-10-04 16:33:58 +0200396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 /**
400 * Load View
401 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500402 * This function is used to load a "view" file. It has three parameters:
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 *
404 * 1. The name of the "view" file to be included.
405 * 2. An associative array of data to be extracted for use in the view.
Derek Jones37f4b9c2011-07-01 17:56:50 -0500406 * 3. TRUE/FALSE - whether to return the data or load it. In
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 * some cases it's advantageous to be able to return data so that
408 * a developer can process it in some way.
409 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 * @param string
411 * @param array
412 * @param bool
413 * @return void
414 */
Greg Akerf5c84022011-04-19 17:13:03 -0500415 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
417 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 /**
423 * Load File
424 *
425 * This is a generic file loader
426 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 * @param string
428 * @param bool
429 * @return string
430 */
Greg Akerf5c84022011-04-19 17:13:03 -0500431 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
433 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 /**
439 * Set Variables
440 *
441 * Once variables are set they become available within
442 * the controller class and its "view" files.
443 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * @param array
David Behlercda768a2011-08-14 23:52:48 +0200445 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 * @return void
447 */
Greg Akerf5c84022011-04-19 17:13:03 -0500448 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
450 if ($val != '' AND is_string($vars))
451 {
452 $vars = array($vars => $val);
453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 if (is_array($vars) AND count($vars) > 0)
458 {
459 foreach ($vars as $key => $val)
460 {
461 $this->_ci_cached_vars[$key] = $val;
462 }
463 }
464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600469 * Get Variable
470 *
471 * Check if a variable is set and retrieve it.
472 *
473 * @param array
474 * @return void
475 */
476 public function get_var($key)
477 {
478 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
479 }
480
481 // --------------------------------------------------------------------
482
483 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600484 * Get Variables
485 *
486 * Retrieve all loaded variables
487 *
488 * @return array
489 */
490 public function get_vars()
491 {
492 return $this->_ci_cached_vars;
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 * Load Helper
499 *
500 * This function loads the specified helper file.
501 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 * @param mixed
503 * @return void
504 */
Greg Akerf5c84022011-04-19 17:13:03 -0500505 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200506 {
Derek Jones32bf1862010-03-02 13:46:07 -0600507 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200508 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 if (isset($this->_ci_helpers[$helper]))
510 {
511 continue;
512 }
Derek Jones32bf1862010-03-02 13:46:07 -0600513
Greg Aker3a746652011-04-19 10:59:47 -0500514 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000515
Barry Mienydd671972010-10-04 16:33:58 +0200516 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 if (file_exists($ext_helper))
518 {
Greg Aker3a746652011-04-19 10:59:47 -0500519 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 if ( ! file_exists($base_helper))
522 {
Greg Aker3a746652011-04-19 10:59:47 -0500523 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 include_once($ext_helper);
527 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Jones32bf1862010-03-02 13:46:07 -0600529 $this->_ci_helpers[$helper] = TRUE;
530 log_message('debug', 'Helper loaded: '.$helper);
531 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Jones32bf1862010-03-02 13:46:07 -0600534 // Try to load the helper
535 foreach ($this->_ci_helper_paths as $path)
536 {
Greg Aker3a746652011-04-19 10:59:47 -0500537 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200538 {
Greg Aker3a746652011-04-19 10:59:47 -0500539 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600540
541 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200542 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600543 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 }
545 }
546
Derek Jones32bf1862010-03-02 13:46:07 -0600547 // unable to load the helper
548 if ( ! isset($this->_ci_helpers[$helper]))
549 {
Greg Aker3a746652011-04-19 10:59:47 -0500550 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 /**
558 * Load Helpers
559 *
560 * This is simply an alias to the above function in case the
561 * user has written the plural form of this function.
562 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 * @param array
564 * @return void
565 */
Greg Akerf5c84022011-04-19 17:13:03 -0500566 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
568 $this->helper($helpers);
569 }
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 * Loads a language file
575 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 * @param array
577 * @param string
578 * @return void
579 */
Greg Akerf5c84022011-04-19 17:13:03 -0500580 public function language($file = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
582 $CI =& get_instance();
583
584 if ( ! is_array($file))
585 {
586 $file = array($file);
587 }
588
589 foreach ($file as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200590 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 $CI->lang->load($langfile, $lang);
592 }
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 /**
598 * Loads a config file
599 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200601 * @param bool
602 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 * @return void
604 */
Greg Akerf5c84022011-04-19 17:13:03 -0500605 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200606 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 $CI =& get_instance();
608 $CI->config->load($file, $use_sections, $fail_gracefully);
609 }
610
611 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 /**
Derek Jones8dca0412010-03-05 13:01:44 -0600614 * Driver
615 *
616 * Loads a driver library
617 *
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800618 * @param mixed the name of the class or array of classes
Derek Jones8dca0412010-03-05 13:01:44 -0600619 * @param mixed the optional parameters
620 * @param string an optional object name
621 * @return void
622 */
Greg Akerf5c84022011-04-19 17:13:03 -0500623 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600624 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800625 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800626 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800627 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800628 {
629 $this->driver($driver);
630 }
631 return FALSE;
632 }
633
Derek Jones8dca0412010-03-05 13:01:44 -0600634 if ( ! class_exists('CI_Driver_Library'))
635 {
636 // we aren't instantiating an object here, that'll be done by the Library itself
Greg Aker3a746652011-04-19 10:59:47 -0500637 require BASEPATH.'libraries/Driver.php';
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600638 }
Barry Mienydd671972010-10-04 16:33:58 +0200639
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200640 if ($library == '')
641 {
642 return FALSE;
643 }
644
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600645 // We can save the loader some time since Drivers will *always* be in a subfolder,
646 // and typically identically named to the library
647 if ( ! strpos($library, '/'))
648 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500649 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600650 }
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Jones8dca0412010-03-05 13:01:44 -0600652 return $this->library($library, $params, $object_name);
653 }
654
655 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200656
Derek Jones8dca0412010-03-05 13:01:44 -0600657 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600658 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 *
Derek Jones32bf1862010-03-02 13:46:07 -0600660 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200663 * @param boolean
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600665 */
Greg Akerf5c84022011-04-19 17:13:03 -0500666 public function add_package_path($path, $view_cascade=TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600667 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500668 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200669
Derek Jones32bf1862010-03-02 13:46:07 -0600670 array_unshift($this->_ci_library_paths, $path);
671 array_unshift($this->_ci_model_paths, $path);
672 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200673
Greg Akerf5c84022011-04-19 17:13:03 -0500674 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
675
Derek Jones32bf1862010-03-02 13:46:07 -0600676 // Add config file path
677 $config =& $this->_ci_get_component('config');
678 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 }
680
681 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600682
683 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000684 * Get Package Paths
685 *
686 * Return a list of all package paths, by default it will ignore BASEPATH.
687 *
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000688 * @param string
689 * @return void
690 */
Greg Akerf5c84022011-04-19 17:13:03 -0500691 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000692 {
693 return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
694 }
695
696 // --------------------------------------------------------------------
697
698 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600699 * Remove Package Path
700 *
701 * Remove a path from the library, model, and helper path arrays if it exists
702 * If no path is provided, the most recently added path is removed.
703 *
Derek Jones32bf1862010-03-02 13:46:07 -0600704 * @param type
David Behlercda768a2011-08-14 23:52:48 +0200705 * @param bool
Derek Jones32bf1862010-03-02 13:46:07 -0600706 * @return type
707 */
Greg Akerf5c84022011-04-19 17:13:03 -0500708 public function remove_package_path($path = '', $remove_config_path = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600709 {
710 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200711
Derek Jones32bf1862010-03-02 13:46:07 -0600712 if ($path == '')
713 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200714 array_shift($this->_ci_library_paths);
715 array_shift($this->_ci_model_paths);
716 array_shift($this->_ci_helper_paths);
717 array_shift($this->_ci_view_paths);
718 array_shift($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600719 }
720 else
721 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500722 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600723 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
724 {
725 if (($key = array_search($path, $this->{$var})) !== FALSE)
726 {
727 unset($this->{$var}[$key]);
728 }
729 }
David Behlercda768a2011-08-14 23:52:48 +0200730
Greg Akerf5c84022011-04-19 17:13:03 -0500731 if (isset($this->_ci_view_paths[$path.'views/']))
732 {
733 unset($this->_ci_view_paths[$path.'views/']);
734 }
Barry Mienydd671972010-10-04 16:33:58 +0200735
Derek Jones32bf1862010-03-02 13:46:07 -0600736 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
737 {
738 unset($config->_config_paths[$key]);
739 }
740 }
Barry Mienydd671972010-10-04 16:33:58 +0200741
Derek Jones32bf1862010-03-02 13:46:07 -0600742 // make sure the application default paths are still in the array
743 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
744 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
745 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500746 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600747 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
748 }
749
750 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200751
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 /**
753 * Loader
754 *
755 * This function is used to load views and files.
756 * Variables are prefixed with _ci_ to avoid symbol collision with
757 * variables made available to view files
758 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 * @param array
760 * @return void
761 */
Greg Akerf5c84022011-04-19 17:13:03 -0500762 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 {
764 // Set the default data variables
765 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
766 {
767 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
768 }
David Behlercda768a2011-08-14 23:52:48 +0200769
Greg Akerf5c84022011-04-19 17:13:03 -0500770 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000771
772 // Set the path to the requested file
Greg Aker8807be32011-04-21 13:06:15 -0500773 if ($_ci_path != '')
774 {
775 $_ci_x = explode('/', $_ci_path);
776 $_ci_file = end($_ci_x);
777 }
778 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 {
780 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Greg Aker3a746652011-04-19 10:59:47 -0500781 $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500782
783 foreach ($this->_ci_view_paths as $view_file => $cascade)
784 {
785 if (file_exists($view_file.$_ci_file))
786 {
787 $_ci_path = $view_file.$_ci_file;
788 $file_exists = TRUE;
789 break;
790 }
David Behlercda768a2011-08-14 23:52:48 +0200791
Greg Akerf5c84022011-04-19 17:13:03 -0500792 if ( ! $cascade)
793 {
794 break;
David Behlercda768a2011-08-14 23:52:48 +0200795 }
Greg Akerf5c84022011-04-19 17:13:03 -0500796 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 }
Barry Mienydd671972010-10-04 16:33:58 +0200798
Greg Akerf5c84022011-04-19 17:13:03 -0500799 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 {
801 show_error('Unable to load the requested file: '.$_ci_file);
802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 // This allows anything loaded using $this->load (views, files, etc.)
805 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500806 $_ci_CI =& get_instance();
807 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500809 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500811 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 }
813 }
814
815 /*
816 * Extract and cache variables
817 *
818 * You can either set variables using the dedicated $this->load_vars()
819 * function or via the second parameter of this function. We'll merge
820 * the two types and cache them so that views that are embedded within
821 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200822 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 if (is_array($_ci_vars))
824 {
825 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
826 }
827 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200828
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 /*
830 * Buffer the output
831 *
832 * We buffer the output for two reasons:
833 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200834 * 2. So that the final rendered template can be post-processed by
835 * the output class. Why do we need post processing? For one thing,
836 * in order to show the elapsed page load time. Unless we can
837 * intercept the content right before it's sent to the browser and
838 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 */
840 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200841
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 // If the PHP installation does not support short tags we'll
843 // do a little string replacement, changing the short tags
844 // to standard PHP echo statements.
Andrey Andreevf5e8e1c2012-03-06 13:11:27 +0200845 if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE && config_item('rewrite_short_tags') == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200847 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 }
849 else
850 {
851 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
852 }
Barry Mienydd671972010-10-04 16:33:58 +0200853
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 // Return the file data if requested
857 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200858 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 $buffer = ob_get_contents();
860 @ob_end_clean();
861 return $buffer;
862 }
863
864 /*
865 * Flush the buffer... or buff the flusher?
866 *
867 * In order to permit views to be nested within
868 * other views, we need to flush the content back out whenever
869 * we are beyond the first level of output buffering so that
870 * it can be seen and included properly by the first included
871 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200872 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 if (ob_get_level() > $this->_ci_ob_level + 1)
874 {
875 ob_end_flush();
876 }
877 else
878 {
Greg Aker22f1a632010-11-10 15:34:35 -0600879 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 @ob_end_clean();
881 }
882 }
883
884 // --------------------------------------------------------------------
885
886 /**
887 * Load class
888 *
889 * This function loads the requested class.
890 *
Barry Mienydd671972010-10-04 16:33:58 +0200891 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 * @param mixed any additional parameters
893 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200894 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 */
Greg Akerf5c84022011-04-19 17:13:03 -0500896 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200897 {
898 // Get the class name, and while we're at it trim any slashes.
899 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500901 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200902
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 // Was the path included with the class name?
904 // We look for a slash to determine this
905 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600906 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 {
Derek Jones32bf1862010-03-02 13:46:07 -0600908 // Extract the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200909 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Jones32bf1862010-03-02 13:46:07 -0600911 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200912 $class = substr($class, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 }
914
915 // We'll test for both lowercase and capitalized versions of the file name
916 foreach (array(ucfirst($class), strtolower($class)) as $class)
917 {
Greg Aker3a746652011-04-19 10:59:47 -0500918 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000919
Barry Mienydd671972010-10-04 16:33:58 +0200920 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 if (file_exists($subclass))
922 {
Greg Aker3a746652011-04-19 10:59:47 -0500923 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200924
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 if ( ! file_exists($baseclass))
926 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200927 log_message('error', 'Unable to load the requested class: '.$class);
928 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 }
930
Andrey Andreevd7297352012-01-07 22:53:14 +0200931 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 if (in_array($subclass, $this->_ci_loaded_files))
933 {
934 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200935 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 // return a new instance of the object
937 if ( ! is_null($object_name))
938 {
939 $CI =& get_instance();
940 if ( ! isset($CI->$object_name))
941 {
Barry Mienydd671972010-10-04 16:33:58 +0200942 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 }
944 }
Barry Mienydd671972010-10-04 16:33:58 +0200945
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200947 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 return;
949 }
Barry Mienydd671972010-10-04 16:33:58 +0200950
951 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 include_once($subclass);
953 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200954
955 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
Barry Mienydd671972010-10-04 16:33:58 +0200957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600959 $is_duplicate = FALSE;
960 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 {
Greg Aker3a746652011-04-19 10:59:47 -0500962 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600963
Andrey Andreevd7297352012-01-07 22:53:14 +0200964 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 if ( ! file_exists($filepath))
966 {
967 continue;
968 }
Barry Mienydd671972010-10-04 16:33:58 +0200969
Andrey Andreevd7297352012-01-07 22:53:14 +0200970 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 if (in_array($filepath, $this->_ci_loaded_files))
972 {
973 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200974 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 // return a new instance of the object
976 if ( ! is_null($object_name))
977 {
978 $CI =& get_instance();
979 if ( ! isset($CI->$object_name))
980 {
981 return $this->_ci_init_class($class, '', $params, $object_name);
982 }
983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200986 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 return;
988 }
Barry Mienydd671972010-10-04 16:33:58 +0200989
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 include_once($filepath);
991 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +0200992 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 }
Derek Jones32bf1862010-03-02 13:46:07 -0600994
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 } // END FOREACH
996
Andrey Andreevd7297352012-01-07 22:53:14 +0200997 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 if ($subdir == '')
999 {
1000 $path = strtolower($class).'/'.$class;
1001 return $this->_ci_load_class($path, $params);
1002 }
Barry Mienydd671972010-10-04 16:33:58 +02001003
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 // If we got this far we were unable to find the requested class.
1005 // We do not issue errors if the load call failed due to a duplicate request
1006 if ($is_duplicate == FALSE)
1007 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001008 log_message('error', 'Unable to load the requested class: '.$class);
1009 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 }
1011 }
Barry Mienydd671972010-10-04 16:33:58 +02001012
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 // --------------------------------------------------------------------
1014
1015 /**
1016 * Instantiates a class
1017 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 * @param string
1019 * @param string
David Behlercda768a2011-08-14 23:52:48 +02001020 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 * @param string an optional object name
1022 * @return null
1023 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001024 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001025 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001026 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 if ($config === NULL)
1028 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001029 // Fetch the config paths containing any package paths
1030 $config_component = $this->_ci_get_component('config');
1031
1032 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001034 // Break on the first found file, thus package files
1035 // are not overridden by default paths
1036 foreach ($config_component->_config_paths as $path)
1037 {
1038 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001039 // are case-sensitive with regard to file names. Check for environment
1040 // first, global next
Greg Aker3a746652011-04-19 10:59:47 -05001041 if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001042 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001043 include($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001044 break;
1045 }
Greg Aker3a746652011-04-19 10:59:47 -05001046 elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001047 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001048 include($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001049 break;
1050 }
Greg Aker3a746652011-04-19 10:59:47 -05001051 elseif (file_exists($path .'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001052 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001053 include($path .'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001054 break;
1055 }
Greg Aker3a746652011-04-19 10:59:47 -05001056 elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001057 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001058 include($path .'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001059 break;
1060 }
1061 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 }
1063 }
Barry Mienydd671972010-10-04 16:33:58 +02001064
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +02001066 {
1067 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 {
1069 $name = 'CI_'.$class;
1070 }
Barry Mienydd671972010-10-04 16:33:58 +02001071 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 {
1073 $name = config_item('subclass_prefix').$class;
1074 }
1075 else
1076 {
1077 $name = $class;
1078 }
1079 }
1080 else
1081 {
1082 $name = $prefix.$class;
1083 }
Barry Mienydd671972010-10-04 16:33:58 +02001084
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 // Is the class name valid?
1086 if ( ! class_exists($name))
1087 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001088 log_message('error', 'Non-existent class: '.$name);
1089 show_error('Non-existent class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 }
Barry Mienydd671972010-10-04 16:33:58 +02001091
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001093 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001095
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 if (is_null($object_name))
1097 {
1098 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
1099 }
1100 else
1101 {
1102 $classvar = $object_name;
1103 }
1104
Barry Mienydd671972010-10-04 16:33:58 +02001105 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 $this->_ci_classes[$class] = $classvar;
1107
Barry Mienydd671972010-10-04 16:33:58 +02001108 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 $CI =& get_instance();
1110 if ($config !== NULL)
1111 {
1112 $CI->$classvar = new $name($config);
1113 }
1114 else
Barry Mienydd671972010-10-04 16:33:58 +02001115 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 $CI->$classvar = new $name;
Barry Mienydd671972010-10-04 16:33:58 +02001117 }
1118 }
1119
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001121
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 /**
1123 * Autoloader
1124 *
1125 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -06001126 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 * @param array
1129 * @return void
1130 */
Shane Pearson665baec2011-08-22 18:52:19 -05001131 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001132 {
Greg Akerd96f8822011-12-27 16:23:47 -06001133 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
1134 {
1135 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
1136 }
1137 else
1138 {
1139 include(APPPATH.'config/autoload.php');
1140 }
Barry Mienydd671972010-10-04 16:33:58 +02001141
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 if ( ! isset($autoload))
1143 {
1144 return FALSE;
1145 }
Barry Mienydd671972010-10-04 16:33:58 +02001146
Phil Sturgeon9730c752010-12-15 10:50:15 +00001147 // Autoload packages
1148 if (isset($autoload['packages']))
1149 {
1150 foreach ($autoload['packages'] as $package_path)
1151 {
1152 $this->add_package_path($package_path);
1153 }
1154 }
1155
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 // Load any custom config file
1157 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001158 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 $CI =& get_instance();
1160 foreach ($autoload['config'] as $key => $val)
1161 {
1162 $CI->config->load($val);
1163 }
Barry Mienydd671972010-10-04 16:33:58 +02001164 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001165
Derek Jonesc6da5032010-03-09 20:44:27 -06001166 // Autoload helpers and languages
1167 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001168 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
1170 {
1171 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001172 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 }
1174
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 // Load libraries
1176 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
1177 {
1178 // Load the database driver.
1179 if (in_array('database', $autoload['libraries']))
1180 {
1181 $this->database();
1182 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1183 }
Barry Mienydd671972010-10-04 16:33:58 +02001184
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 // Load all other libraries
1186 foreach ($autoload['libraries'] as $item)
1187 {
1188 $this->library($item);
1189 }
Barry Mienydd671972010-10-04 16:33:58 +02001190 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001191
1192 // Autoload models
1193 if (isset($autoload['model']))
1194 {
1195 $this->model($autoload['model']);
1196 }
Barry Mienydd671972010-10-04 16:33:58 +02001197 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001198
1199 // --------------------------------------------------------------------
1200
1201 /**
1202 * Object to Array
1203 *
1204 * Takes an object as input and converts the class variables to array key/vals
1205 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 * @param object
1207 * @return array
1208 */
Greg Akerf5c84022011-04-19 17:13:03 -05001209 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 {
1211 return (is_object($object)) ? get_object_vars($object) : $object;
1212 }
1213
1214 // --------------------------------------------------------------------
1215
1216 /**
Derek Jones32bf1862010-03-02 13:46:07 -06001217 * Get a reference to a specific library or model
1218 *
David Behlercda768a2011-08-14 23:52:48 +02001219 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001220 * @return bool
1221 */
Greg Akerf5c84022011-04-19 17:13:03 -05001222 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001223 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001224 $CI =& get_instance();
1225 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001226 }
1227
1228 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001229
Derek Jones32bf1862010-03-02 13:46:07 -06001230 /**
1231 * Prep filename
1232 *
1233 * This function preps the name of various items to make loading them more reliable.
1234 *
Derek Jones32bf1862010-03-02 13:46:07 -06001235 * @param mixed
David Behlercda768a2011-08-14 23:52:48 +02001236 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001237 * @return array
1238 */
Greg Akerf5c84022011-04-19 17:13:03 -05001239 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001240 {
1241 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001242 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001243 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001244 }
1245 else
1246 {
1247 foreach ($filename as $key => $val)
1248 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001249 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001250 }
Barry Mienydd671972010-10-04 16:33:58 +02001251
Derek Jones32bf1862010-03-02 13:46:07 -06001252 return $filename;
1253 }
1254 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001255}
1256
1257/* End of file Loader.php */
Andrey Andreevd7297352012-01-07 22:53:14 +02001258/* Location: ./system/core/Loader.php */