blob: 750e8cdd62cacac4d59d391f0c989e7d6d632555 [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 *
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 *
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
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000318 * @param bool whether to enable query builder (this allows us to override the config setting)
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200320 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000321 public function database($params = '', $return = FALSE, $query_builder = 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?
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000327 if (class_exists('CI_DB') AND $return == FALSE AND $query_builder == 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 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000336 return DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 }
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
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000344 $CI->db =& DB($params, $query_builder);
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 *
618 * @param string the name of the class
619 * @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 {
625 if ( ! class_exists('CI_Driver_Library'))
626 {
627 // we aren't instantiating an object here, that'll be done by the Library itself
Greg Aker3a746652011-04-19 10:59:47 -0500628 require BASEPATH.'libraries/Driver.php';
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600629 }
Barry Mienydd671972010-10-04 16:33:58 +0200630
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200631 if ($library == '')
632 {
633 return FALSE;
634 }
635
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600636 // We can save the loader some time since Drivers will *always* be in a subfolder,
637 // and typically identically named to the library
638 if ( ! strpos($library, '/'))
639 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500640 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600641 }
Barry Mienydd671972010-10-04 16:33:58 +0200642
Derek Jones8dca0412010-03-05 13:01:44 -0600643 return $this->library($library, $params, $object_name);
644 }
645
646 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Jones8dca0412010-03-05 13:01:44 -0600648 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600649 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 *
Derek Jones32bf1862010-03-02 13:46:07 -0600651 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200654 * @param boolean
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600656 */
Greg Akerf5c84022011-04-19 17:13:03 -0500657 public function add_package_path($path, $view_cascade=TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600658 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500659 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200660
Derek Jones32bf1862010-03-02 13:46:07 -0600661 array_unshift($this->_ci_library_paths, $path);
662 array_unshift($this->_ci_model_paths, $path);
663 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200664
Greg Akerf5c84022011-04-19 17:13:03 -0500665 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
666
Derek Jones32bf1862010-03-02 13:46:07 -0600667 // Add config file path
668 $config =& $this->_ci_get_component('config');
669 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 }
671
672 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600673
674 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000675 * Get Package Paths
676 *
677 * Return a list of all package paths, by default it will ignore BASEPATH.
678 *
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000679 * @param string
680 * @return void
681 */
Greg Akerf5c84022011-04-19 17:13:03 -0500682 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000683 {
684 return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
685 }
686
687 // --------------------------------------------------------------------
688
689 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600690 * Remove Package Path
691 *
692 * Remove a path from the library, model, and helper path arrays if it exists
693 * If no path is provided, the most recently added path is removed.
694 *
Derek Jones32bf1862010-03-02 13:46:07 -0600695 * @param type
David Behlercda768a2011-08-14 23:52:48 +0200696 * @param bool
Derek Jones32bf1862010-03-02 13:46:07 -0600697 * @return type
698 */
Greg Akerf5c84022011-04-19 17:13:03 -0500699 public function remove_package_path($path = '', $remove_config_path = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600700 {
701 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200702
Derek Jones32bf1862010-03-02 13:46:07 -0600703 if ($path == '')
704 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200705 array_shift($this->_ci_library_paths);
706 array_shift($this->_ci_model_paths);
707 array_shift($this->_ci_helper_paths);
708 array_shift($this->_ci_view_paths);
709 array_shift($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600710 }
711 else
712 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500713 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600714 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
715 {
716 if (($key = array_search($path, $this->{$var})) !== FALSE)
717 {
718 unset($this->{$var}[$key]);
719 }
720 }
David Behlercda768a2011-08-14 23:52:48 +0200721
Greg Akerf5c84022011-04-19 17:13:03 -0500722 if (isset($this->_ci_view_paths[$path.'views/']))
723 {
724 unset($this->_ci_view_paths[$path.'views/']);
725 }
Barry Mienydd671972010-10-04 16:33:58 +0200726
Derek Jones32bf1862010-03-02 13:46:07 -0600727 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
728 {
729 unset($config->_config_paths[$key]);
730 }
731 }
Barry Mienydd671972010-10-04 16:33:58 +0200732
Derek Jones32bf1862010-03-02 13:46:07 -0600733 // make sure the application default paths are still in the array
734 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
735 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
736 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500737 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600738 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
739 }
740
741 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 /**
744 * Loader
745 *
746 * This function is used to load views and files.
747 * Variables are prefixed with _ci_ to avoid symbol collision with
748 * variables made available to view files
749 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 * @param array
751 * @return void
752 */
Greg Akerf5c84022011-04-19 17:13:03 -0500753 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
755 // Set the default data variables
756 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
757 {
758 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
759 }
David Behlercda768a2011-08-14 23:52:48 +0200760
Greg Akerf5c84022011-04-19 17:13:03 -0500761 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000762
763 // Set the path to the requested file
Greg Aker8807be32011-04-21 13:06:15 -0500764 if ($_ci_path != '')
765 {
766 $_ci_x = explode('/', $_ci_path);
767 $_ci_file = end($_ci_x);
768 }
769 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 {
771 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Greg Aker3a746652011-04-19 10:59:47 -0500772 $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500773
774 foreach ($this->_ci_view_paths as $view_file => $cascade)
775 {
776 if (file_exists($view_file.$_ci_file))
777 {
778 $_ci_path = $view_file.$_ci_file;
779 $file_exists = TRUE;
780 break;
781 }
David Behlercda768a2011-08-14 23:52:48 +0200782
Greg Akerf5c84022011-04-19 17:13:03 -0500783 if ( ! $cascade)
784 {
785 break;
David Behlercda768a2011-08-14 23:52:48 +0200786 }
Greg Akerf5c84022011-04-19 17:13:03 -0500787 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 }
Barry Mienydd671972010-10-04 16:33:58 +0200789
Greg Akerf5c84022011-04-19 17:13:03 -0500790 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 {
792 show_error('Unable to load the requested file: '.$_ci_file);
793 }
Barry Mienydd671972010-10-04 16:33:58 +0200794
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 // This allows anything loaded using $this->load (views, files, etc.)
796 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500797 $_ci_CI =& get_instance();
798 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500800 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500802 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 }
804 }
805
806 /*
807 * Extract and cache variables
808 *
809 * You can either set variables using the dedicated $this->load_vars()
810 * function or via the second parameter of this function. We'll merge
811 * the two types and cache them so that views that are embedded within
812 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200813 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 if (is_array($_ci_vars))
815 {
816 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
817 }
818 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 /*
821 * Buffer the output
822 *
823 * We buffer the output for two reasons:
824 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200825 * 2. So that the final rendered template can be post-processed by
826 * the output class. Why do we need post processing? For one thing,
827 * in order to show the elapsed page load time. Unless we can
828 * intercept the content right before it's sent to the browser and
829 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 */
831 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 // If the PHP installation does not support short tags we'll
834 // do a little string replacement, changing the short tags
835 // to standard PHP echo statements.
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
837 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200838 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 }
840 else
841 {
842 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
843 }
Barry Mienydd671972010-10-04 16:33:58 +0200844
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200846
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 // Return the file data if requested
848 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200849 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 $buffer = ob_get_contents();
851 @ob_end_clean();
852 return $buffer;
853 }
854
855 /*
856 * Flush the buffer... or buff the flusher?
857 *
858 * In order to permit views to be nested within
859 * other views, we need to flush the content back out whenever
860 * we are beyond the first level of output buffering so that
861 * it can be seen and included properly by the first included
862 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200863 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 if (ob_get_level() > $this->_ci_ob_level + 1)
865 {
866 ob_end_flush();
867 }
868 else
869 {
Greg Aker22f1a632010-11-10 15:34:35 -0600870 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 @ob_end_clean();
872 }
873 }
874
875 // --------------------------------------------------------------------
876
877 /**
878 * Load class
879 *
880 * This function loads the requested class.
881 *
Barry Mienydd671972010-10-04 16:33:58 +0200882 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 * @param mixed any additional parameters
884 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200885 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 */
Greg Akerf5c84022011-04-19 17:13:03 -0500887 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200888 {
889 // Get the class name, and while we're at it trim any slashes.
890 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500892 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 // Was the path included with the class name?
895 // We look for a slash to determine this
896 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600897 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
Derek Jones32bf1862010-03-02 13:46:07 -0600899 // Extract the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200900 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200901
Derek Jones32bf1862010-03-02 13:46:07 -0600902 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200903 $class = substr($class, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 }
905
906 // We'll test for both lowercase and capitalized versions of the file name
907 foreach (array(ucfirst($class), strtolower($class)) as $class)
908 {
Greg Aker3a746652011-04-19 10:59:47 -0500909 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000910
Barry Mienydd671972010-10-04 16:33:58 +0200911 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 if (file_exists($subclass))
913 {
Greg Aker3a746652011-04-19 10:59:47 -0500914 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 if ( ! file_exists($baseclass))
917 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200918 log_message('error', 'Unable to load the requested class: '.$class);
919 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 }
921
Andrey Andreevd7297352012-01-07 22:53:14 +0200922 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 if (in_array($subclass, $this->_ci_loaded_files))
924 {
925 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200926 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 // return a new instance of the object
928 if ( ! is_null($object_name))
929 {
930 $CI =& get_instance();
931 if ( ! isset($CI->$object_name))
932 {
Barry Mienydd671972010-10-04 16:33:58 +0200933 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 }
935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200938 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 return;
940 }
Barry Mienydd671972010-10-04 16:33:58 +0200941
942 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 include_once($subclass);
944 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200945
946 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 }
Barry Mienydd671972010-10-04 16:33:58 +0200948
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600950 $is_duplicate = FALSE;
951 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
Greg Aker3a746652011-04-19 10:59:47 -0500953 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600954
Andrey Andreevd7297352012-01-07 22:53:14 +0200955 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 if ( ! file_exists($filepath))
957 {
958 continue;
959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
Andrey Andreevd7297352012-01-07 22:53:14 +0200961 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 if (in_array($filepath, $this->_ci_loaded_files))
963 {
964 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200965 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 // return a new instance of the object
967 if ( ! is_null($object_name))
968 {
969 $CI =& get_instance();
970 if ( ! isset($CI->$object_name))
971 {
972 return $this->_ci_init_class($class, '', $params, $object_name);
973 }
974 }
Barry Mienydd671972010-10-04 16:33:58 +0200975
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200977 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 return;
979 }
Barry Mienydd671972010-10-04 16:33:58 +0200980
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 include_once($filepath);
982 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +0200983 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 }
Derek Jones32bf1862010-03-02 13:46:07 -0600985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 } // END FOREACH
987
Andrey Andreevd7297352012-01-07 22:53:14 +0200988 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 if ($subdir == '')
990 {
991 $path = strtolower($class).'/'.$class;
992 return $this->_ci_load_class($path, $params);
993 }
Barry Mienydd671972010-10-04 16:33:58 +0200994
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 // If we got this far we were unable to find the requested class.
996 // We do not issue errors if the load call failed due to a duplicate request
997 if ($is_duplicate == FALSE)
998 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200999 log_message('error', 'Unable to load the requested class: '.$class);
1000 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 }
1002 }
Barry Mienydd671972010-10-04 16:33:58 +02001003
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 // --------------------------------------------------------------------
1005
1006 /**
1007 * Instantiates a class
1008 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 * @param string
1010 * @param string
David Behlercda768a2011-08-14 23:52:48 +02001011 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 * @param string an optional object name
1013 * @return null
1014 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001015 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001016 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001017 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 if ($config === NULL)
1019 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001020 // Fetch the config paths containing any package paths
1021 $config_component = $this->_ci_get_component('config');
1022
1023 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001025 // Break on the first found file, thus package files
1026 // are not overridden by default paths
1027 foreach ($config_component->_config_paths as $path)
1028 {
1029 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001030 // are case-sensitive with regard to file names. Check for environment
1031 // first, global next
Greg Aker3a746652011-04-19 10:59:47 -05001032 if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001033 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001034 include($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001035 break;
1036 }
Greg Aker3a746652011-04-19 10:59:47 -05001037 elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001038 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001039 include($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001040 break;
1041 }
Greg Aker3a746652011-04-19 10:59:47 -05001042 elseif (file_exists($path .'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001043 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001044 include($path .'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001045 break;
1046 }
Greg Aker3a746652011-04-19 10:59:47 -05001047 elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001048 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001049 include($path .'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001050 break;
1051 }
1052 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 }
1054 }
Barry Mienydd671972010-10-04 16:33:58 +02001055
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +02001057 {
1058 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 {
1060 $name = 'CI_'.$class;
1061 }
Barry Mienydd671972010-10-04 16:33:58 +02001062 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 {
1064 $name = config_item('subclass_prefix').$class;
1065 }
1066 else
1067 {
1068 $name = $class;
1069 }
1070 }
1071 else
1072 {
1073 $name = $prefix.$class;
1074 }
Barry Mienydd671972010-10-04 16:33:58 +02001075
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 // Is the class name valid?
1077 if ( ! class_exists($name))
1078 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001079 log_message('error', 'Non-existent class: '.$name);
1080 show_error('Non-existent class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 }
Barry Mienydd671972010-10-04 16:33:58 +02001082
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001084 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001086
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 if (is_null($object_name))
1088 {
1089 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
1090 }
1091 else
1092 {
1093 $classvar = $object_name;
1094 }
1095
Barry Mienydd671972010-10-04 16:33:58 +02001096 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 $this->_ci_classes[$class] = $classvar;
1098
Barry Mienydd671972010-10-04 16:33:58 +02001099 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 $CI =& get_instance();
1101 if ($config !== NULL)
1102 {
1103 $CI->$classvar = new $name($config);
1104 }
1105 else
Barry Mienydd671972010-10-04 16:33:58 +02001106 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 $CI->$classvar = new $name;
Barry Mienydd671972010-10-04 16:33:58 +02001108 }
1109 }
1110
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001112
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 /**
1114 * Autoloader
1115 *
1116 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -06001117 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 * @param array
1120 * @return void
1121 */
Shane Pearson665baec2011-08-22 18:52:19 -05001122 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001123 {
Greg Akerd96f8822011-12-27 16:23:47 -06001124 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
1125 {
1126 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
1127 }
1128 else
1129 {
1130 include(APPPATH.'config/autoload.php');
1131 }
Barry Mienydd671972010-10-04 16:33:58 +02001132
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 if ( ! isset($autoload))
1134 {
1135 return FALSE;
1136 }
Barry Mienydd671972010-10-04 16:33:58 +02001137
Phil Sturgeon9730c752010-12-15 10:50:15 +00001138 // Autoload packages
1139 if (isset($autoload['packages']))
1140 {
1141 foreach ($autoload['packages'] as $package_path)
1142 {
1143 $this->add_package_path($package_path);
1144 }
1145 }
1146
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 // Load any custom config file
1148 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001149 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 $CI =& get_instance();
1151 foreach ($autoload['config'] as $key => $val)
1152 {
1153 $CI->config->load($val);
1154 }
Barry Mienydd671972010-10-04 16:33:58 +02001155 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001156
Derek Jonesc6da5032010-03-09 20:44:27 -06001157 // Autoload helpers and languages
1158 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001159 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
1161 {
1162 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001163 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 }
1165
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 // Load libraries
1167 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
1168 {
1169 // Load the database driver.
1170 if (in_array('database', $autoload['libraries']))
1171 {
1172 $this->database();
1173 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1174 }
Barry Mienydd671972010-10-04 16:33:58 +02001175
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 // Load all other libraries
1177 foreach ($autoload['libraries'] as $item)
1178 {
1179 $this->library($item);
1180 }
Barry Mienydd671972010-10-04 16:33:58 +02001181 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001182
1183 // Autoload models
1184 if (isset($autoload['model']))
1185 {
1186 $this->model($autoload['model']);
1187 }
Barry Mienydd671972010-10-04 16:33:58 +02001188 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001189
1190 // --------------------------------------------------------------------
1191
1192 /**
1193 * Object to Array
1194 *
1195 * Takes an object as input and converts the class variables to array key/vals
1196 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 * @param object
1198 * @return array
1199 */
Greg Akerf5c84022011-04-19 17:13:03 -05001200 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 {
1202 return (is_object($object)) ? get_object_vars($object) : $object;
1203 }
1204
1205 // --------------------------------------------------------------------
1206
1207 /**
Derek Jones32bf1862010-03-02 13:46:07 -06001208 * Get a reference to a specific library or model
1209 *
David Behlercda768a2011-08-14 23:52:48 +02001210 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001211 * @return bool
1212 */
Greg Akerf5c84022011-04-19 17:13:03 -05001213 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001214 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001215 $CI =& get_instance();
1216 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001217 }
1218
1219 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001220
Derek Jones32bf1862010-03-02 13:46:07 -06001221 /**
1222 * Prep filename
1223 *
1224 * This function preps the name of various items to make loading them more reliable.
1225 *
Derek Jones32bf1862010-03-02 13:46:07 -06001226 * @param mixed
David Behlercda768a2011-08-14 23:52:48 +02001227 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001228 * @return array
1229 */
Greg Akerf5c84022011-04-19 17:13:03 -05001230 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001231 {
1232 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001233 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001234 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001235 }
1236 else
1237 {
1238 foreach ($filename as $key => $val)
1239 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001240 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001241 }
Barry Mienydd671972010-10-04 16:33:58 +02001242
Derek Jones32bf1862010-03-02 13:46:07 -06001243 return $filename;
1244 }
1245 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001246}
1247
1248/* End of file Loader.php */
Andrey Andreevd7297352012-01-07 22:53:14 +02001249/* Location: ./system/core/Loader.php */