blob: d42dbbf386d55d4ee3675f9226ad6794784b6d46 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
48 * @access protected
49 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050050 protected $_ci_ob_level;
David Behlercda768a2011-08-14 23:52:48 +020051 /**
52 * List of paths to load views from
53 *
54 * @var array
55 * @access protected
56 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050057 protected $_ci_view_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020058 /**
59 * List of paths to load libraries from
60 *
61 * @var array
62 * @access protected
63 */
Greg Akerf5c84022011-04-19 17:13:03 -050064 protected $_ci_library_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020065 /**
66 * List of paths to load models from
67 *
68 * @var array
69 * @access protected
70 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050071 protected $_ci_model_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020072 /**
73 * List of paths to load helpers from
74 *
75 * @var array
76 * @access protected
77 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050078 protected $_ci_helper_paths = array();
David Behlercda768a2011-08-14 23:52:48 +020079 /**
80 * List of loaded base classes
81 * Set by the controller class
82 *
83 * @var array
84 * @access protected
85 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050086 protected $_base_classes = array(); // Set by the controller class
David Behlercda768a2011-08-14 23:52:48 +020087 /**
88 * List of cached variables
89 *
90 * @var array
91 * @access protected
92 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050093 protected $_ci_cached_vars = array();
David Behlercda768a2011-08-14 23:52:48 +020094 /**
95 * List of loaded classes
96 *
97 * @var array
98 * @access protected
99 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500100 protected $_ci_classes = array();
David Behlercda768a2011-08-14 23:52:48 +0200101 /**
102 * List of loaded files
103 *
104 * @var array
105 * @access protected
106 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500107 protected $_ci_loaded_files = array();
David Behlercda768a2011-08-14 23:52:48 +0200108 /**
109 * List of loaded models
110 *
111 * @var array
112 * @access protected
113 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500114 protected $_ci_models = array();
David Behlercda768a2011-08-14 23:52:48 +0200115 /**
116 * List of loaded helpers
117 *
118 * @var array
119 * @access protected
120 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500121 protected $_ci_helpers = array();
David Behlercda768a2011-08-14 23:52:48 +0200122 /**
123 * List of class name mappings
124 *
125 * @var array
126 * @access protected
127 */
128 protected $_ci_varmap = array('unit_test' => 'unit',
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500129 'user_agent' => 'agent');
Derek Allard2067d1a2008-11-13 22:59:24 +0000130
131 /**
132 * Constructor
133 *
134 * Sets the path to the view files and gets the initial output buffering level
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 */
Greg Akerf5c84022011-04-19 17:13:03 -0500136 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +0200137 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500138 $this->_ci_ob_level = ob_get_level();
Derek Jones32bf1862010-03-02 13:46:07 -0600139 $this->_ci_library_paths = array(APPPATH, BASEPATH);
140 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
141 $this->_ci_model_paths = array(APPPATH);
Joe Cianflone8eef9c72011-08-21 10:39:06 -0400142 $this->_ci_view_paths = array(VIEWPATH => TRUE);
David Behlercda768a2011-08-14 23:52:48 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 log_message('debug', "Loader Class Initialized");
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200148
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500149 /**
Shane Pearson6adfe632011-08-10 16:42:53 -0500150 * Initialize the Loader
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500151 *
152 * This method is called once in CI_Controller.
153 *
David Behlercda768a2011-08-14 23:52:48 +0200154 * @param array
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500155 * @return object
156 */
Shane Pearson6adfe632011-08-10 16:42:53 -0500157 public function initialize()
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500158 {
Shane Pearson6adfe632011-08-10 16:42:53 -0500159 $this->_ci_classes = array();
160 $this->_ci_loaded_files = array();
161 $this->_ci_models = array();
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500162 $this->_base_classes =& is_loaded();
Shane Pearson6adfe632011-08-10 16:42:53 -0500163
164 $this->_ci_autoloader();
165
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500166 return $this;
167 }
168
169 // --------------------------------------------------------------------
170
171 /**
172 * Is Loaded
173 *
174 * A utility function to test if a class is in the self::$_ci_classes array.
175 * This function returns the object name if the class tested for is loaded,
176 * and returns FALSE if it isn't.
177 *
178 * It is mainly used in the form_helper -> _get_validation_object()
179 *
180 * @param string class being checked for
181 * @return mixed class object name on the CI SuperObject or FALSE
182 */
183 public function is_loaded($class)
184 {
185 if (isset($this->_ci_classes[$class]))
186 {
187 return $this->_ci_classes[$class];
188 }
David Behlercda768a2011-08-14 23:52:48 +0200189
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500190 return FALSE;
191 }
192
193 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 /**
196 * Class Loader
197 *
198 * This function lets users load and instantiate classes.
199 * It is designed to be called from a user's app controllers.
200 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 * @param string the name of the class
202 * @param mixed the optional parameters
203 * @param string an optional object name
204 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200205 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500206 public function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
Greg Akerce433962010-10-12 09:29:35 -0500208 if (is_array($library))
209 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100210 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500211 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600212 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500213 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000214
Greg Akerce433962010-10-12 09:29:35 -0500215 return;
216 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000217
Derek Jones32bf1862010-03-02 13:46:07 -0600218 if ($library == '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
220 return FALSE;
221 }
222
Derek Jones32bf1862010-03-02 13:46:07 -0600223 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
225 $params = NULL;
226 }
227
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600228 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
230
231 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 /**
234 * Model Loader
235 *
236 * This function lets users load and instantiate models.
237 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 * @param string the name of the class
239 * @param string name for the model
240 * @param bool database connection
241 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200242 */
Greg Akerf5c84022011-04-19 17:13:03 -0500243 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200244 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 if (is_array($model))
246 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500247 foreach ($model as $babe)
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Barry Mienydd671972010-10-04 16:33:58 +0200249 $this->model($babe);
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
251 return;
252 }
253
254 if ($model == '')
255 {
256 return;
257 }
Barry Mienydd671972010-10-04 16:33:58 +0200258
Derek Jones32bf1862010-03-02 13:46:07 -0600259 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600262 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
Derek Jones32bf1862010-03-02 13:46:07 -0600264 // The path is in front of the last slash
265 $path = substr($model, 0, $last_slash + 1);
266
267 // And the model name behind it
268 $model = substr($model, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 if ($name == '')
272 {
273 $name = $model;
274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 if (in_array($name, $this->_ci_models, TRUE))
277 {
278 return;
279 }
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 $CI =& get_instance();
282 if (isset($CI->$name))
283 {
284 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
285 }
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000288
Derek Jones32bf1862010-03-02 13:46:07 -0600289 foreach ($this->_ci_model_paths as $mod_path)
290 {
Greg Aker3a746652011-04-19 10:59:47 -0500291 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600292 {
293 continue;
294 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000295
Derek Jones32bf1862010-03-02 13:46:07 -0600296 if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
297 {
298 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500299 {
Derek Jones32bf1862010-03-02 13:46:07 -0600300 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500301 }
Derek Jones32bf1862010-03-02 13:46:07 -0600302
303 $CI->load->database($db_conn, FALSE, TRUE);
304 }
305
Greg Akerbce13482010-10-11 15:37:16 -0500306 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600307 {
308 load_class('Model', 'core');
309 }
310
Greg Aker3a746652011-04-19 10:59:47 -0500311 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600312
313 $model = ucfirst($model);
314
315 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600316
317 $this->_ci_models[] = $name;
318 return;
319 }
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Jones32bf1862010-03-02 13:46:07 -0600321 // couldn't find the model
322 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 /**
328 * Database Loader
329 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 * @param string the DB credentials
331 * @param bool whether to return the DB object
332 * @param bool whether to enable active record (this allows us to override the config setting)
333 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200334 */
Greg Akerf5c84022011-04-19 17:13:03 -0500335 public function database($params = '', $return = FALSE, $active_record = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 // Grab the super object
338 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 // Do we even need to load the database class?
Derek Jones9fb6dd12009-12-05 15:32:48 +0000341 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 +0000342 {
343 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200344 }
345
Greg Aker3a746652011-04-19 10:59:47 -0500346 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000347
348 if ($return === TRUE)
349 {
350 return DB($params, $active_record);
351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Jones37f4b9c2011-07-01 17:56:50 -0500353 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // reference errors with some configurations
355 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 // Load the DB class
Barry Mienydd671972010-10-04 16:33:58 +0200358 $CI->db =& DB($params, $active_record);
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // --------------------------------------------------------------------
362
363 /**
364 * Load the Utilities Class
365 *
Barry Mienydd671972010-10-04 16:33:58 +0200366 * @return string
367 */
Greg Akerf5c84022011-04-19 17:13:03 -0500368 public function dbutil()
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
370 if ( ! class_exists('CI_DB'))
371 {
372 $this->database();
373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 $CI =& get_instance();
376
377 // for backwards compatibility, load dbforge so we can extend dbutils off it
378 // this use is deprecated and strongly discouraged
379 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200380
Greg Aker3a746652011-04-19 10:59:47 -0500381 require_once(BASEPATH.'database/DB_utility.php');
382 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
384
Pascal Kriete58560022010-11-10 16:01:20 -0500385 $CI->dbutil = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 // --------------------------------------------------------------------
389
390 /**
391 * Load the Database Forge Class
392 *
Barry Mienydd671972010-10-04 16:33:58 +0200393 * @return string
394 */
Greg Akerf5c84022011-04-19 17:13:03 -0500395 public function dbforge()
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
397 if ( ! class_exists('CI_DB'))
398 {
399 $this->database();
400 }
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200403
Greg Aker3a746652011-04-19 10:59:47 -0500404 require_once(BASEPATH.'database/DB_forge.php');
405 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
407
408 $CI->dbforge = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 /**
414 * Load View
415 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500416 * This function is used to load a "view" file. It has three parameters:
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 *
418 * 1. The name of the "view" file to be included.
419 * 2. An associative array of data to be extracted for use in the view.
Derek Jones37f4b9c2011-07-01 17:56:50 -0500420 * 3. TRUE/FALSE - whether to return the data or load it. In
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 * some cases it's advantageous to be able to return data so that
422 * a developer can process it in some way.
423 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 * @param string
425 * @param array
426 * @param bool
427 * @return void
428 */
Greg Akerf5c84022011-04-19 17:13:03 -0500429 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
431 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 /**
437 * Load File
438 *
439 * This is a generic file loader
440 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 * @param string
442 * @param bool
443 * @return string
444 */
Greg Akerf5c84022011-04-19 17:13:03 -0500445 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
447 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 /**
453 * Set Variables
454 *
455 * Once variables are set they become available within
456 * the controller class and its "view" files.
457 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 * @param array
David Behlercda768a2011-08-14 23:52:48 +0200459 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 * @return void
461 */
Greg Akerf5c84022011-04-19 17:13:03 -0500462 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
464 if ($val != '' AND is_string($vars))
465 {
466 $vars = array($vars => $val);
467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 if (is_array($vars) AND count($vars) > 0)
472 {
473 foreach ($vars as $key => $val)
474 {
475 $this->_ci_cached_vars[$key] = $val;
476 }
477 }
478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600483 * Get Variable
484 *
485 * Check if a variable is set and retrieve it.
486 *
487 * @param array
488 * @return void
489 */
490 public function get_var($key)
491 {
492 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600498 * Get Variables
499 *
500 * Retrieve all loaded variables
501 *
502 * @return array
503 */
504 public function get_vars()
505 {
506 return $this->_ci_cached_vars;
507 }
508
509 // --------------------------------------------------------------------
510
511 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 * Load Helper
513 *
514 * This function loads the specified helper file.
515 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 * @param mixed
517 * @return void
518 */
Greg Akerf5c84022011-04-19 17:13:03 -0500519 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200520 {
Derek Jones32bf1862010-03-02 13:46:07 -0600521 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200522 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 if (isset($this->_ci_helpers[$helper]))
524 {
525 continue;
526 }
Derek Jones32bf1862010-03-02 13:46:07 -0600527
Greg Aker3a746652011-04-19 10:59:47 -0500528 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000529
Barry Mienydd671972010-10-04 16:33:58 +0200530 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 if (file_exists($ext_helper))
532 {
Greg Aker3a746652011-04-19 10:59:47 -0500533 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 if ( ! file_exists($base_helper))
536 {
Greg Aker3a746652011-04-19 10:59:47 -0500537 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 include_once($ext_helper);
541 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Jones32bf1862010-03-02 13:46:07 -0600543 $this->_ci_helpers[$helper] = TRUE;
544 log_message('debug', 'Helper loaded: '.$helper);
545 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 }
Barry Mienydd671972010-10-04 16:33:58 +0200547
Derek Jones32bf1862010-03-02 13:46:07 -0600548 // Try to load the helper
549 foreach ($this->_ci_helper_paths as $path)
550 {
Greg Aker3a746652011-04-19 10:59:47 -0500551 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200552 {
Greg Aker3a746652011-04-19 10:59:47 -0500553 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600554
555 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200556 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600557 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 }
559 }
560
Derek Jones32bf1862010-03-02 13:46:07 -0600561 // unable to load the helper
562 if ( ! isset($this->_ci_helpers[$helper]))
563 {
Greg Aker3a746652011-04-19 10:59:47 -0500564 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600565 }
Barry Mienydd671972010-10-04 16:33:58 +0200566 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 }
Barry Mienydd671972010-10-04 16:33:58 +0200568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 /**
572 * Load Helpers
573 *
574 * This is simply an alias to the above function in case the
575 * user has written the plural form of this function.
576 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * @param array
578 * @return void
579 */
Greg Akerf5c84022011-04-19 17:13:03 -0500580 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
582 $this->helper($helpers);
583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 * Loads a language file
589 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 * @param array
591 * @param string
592 * @return void
593 */
Greg Akerf5c84022011-04-19 17:13:03 -0500594 public function language($file = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
596 $CI =& get_instance();
597
598 if ( ! is_array($file))
599 {
600 $file = array($file);
601 }
602
603 foreach ($file as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200604 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 $CI->lang->load($langfile, $lang);
606 }
607 }
Barry Mienydd671972010-10-04 16:33:58 +0200608
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 /**
612 * Loads a config file
613 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200615 * @param bool
616 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 * @return void
618 */
Greg Akerf5c84022011-04-19 17:13:03 -0500619 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200620 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 $CI =& get_instance();
622 $CI->config->load($file, $use_sections, $fail_gracefully);
623 }
624
625 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600626
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 /**
Derek Jones8dca0412010-03-05 13:01:44 -0600628 * Driver
629 *
630 * Loads a driver library
631 *
632 * @param string the name of the class
633 * @param mixed the optional parameters
634 * @param string an optional object name
635 * @return void
636 */
Greg Akerf5c84022011-04-19 17:13:03 -0500637 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600638 {
639 if ( ! class_exists('CI_Driver_Library'))
640 {
641 // we aren't instantiating an object here, that'll be done by the Library itself
Greg Aker3a746652011-04-19 10:59:47 -0500642 require BASEPATH.'libraries/Driver.php';
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600643 }
Barry Mienydd671972010-10-04 16:33:58 +0200644
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200645 if ($library == '')
646 {
647 return FALSE;
648 }
649
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600650 // We can save the loader some time since Drivers will *always* be in a subfolder,
651 // and typically identically named to the library
652 if ( ! strpos($library, '/'))
653 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500654 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600655 }
Barry Mienydd671972010-10-04 16:33:58 +0200656
Derek Jones8dca0412010-03-05 13:01:44 -0600657 return $this->library($library, $params, $object_name);
658 }
659
660 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200661
Derek Jones8dca0412010-03-05 13:01:44 -0600662 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600663 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 *
Derek Jones32bf1862010-03-02 13:46:07 -0600665 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 * @param string
David Behlercda768a2011-08-14 23:52:48 +0200668 * @param boolean
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600670 */
Greg Akerf5c84022011-04-19 17:13:03 -0500671 public function add_package_path($path, $view_cascade=TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600672 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500673 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200674
Derek Jones32bf1862010-03-02 13:46:07 -0600675 array_unshift($this->_ci_library_paths, $path);
676 array_unshift($this->_ci_model_paths, $path);
677 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200678
Greg Akerf5c84022011-04-19 17:13:03 -0500679 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
680
Derek Jones32bf1862010-03-02 13:46:07 -0600681 // Add config file path
682 $config =& $this->_ci_get_component('config');
683 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 }
685
686 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600687
688 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000689 * Get Package Paths
690 *
691 * Return a list of all package paths, by default it will ignore BASEPATH.
692 *
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000693 * @param string
694 * @return void
695 */
Greg Akerf5c84022011-04-19 17:13:03 -0500696 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000697 {
698 return $include_base === TRUE ? $this->_ci_library_paths : $this->_ci_model_paths;
699 }
700
701 // --------------------------------------------------------------------
702
703 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600704 * Remove Package Path
705 *
706 * Remove a path from the library, model, and helper path arrays if it exists
707 * If no path is provided, the most recently added path is removed.
708 *
Derek Jones32bf1862010-03-02 13:46:07 -0600709 * @param type
David Behlercda768a2011-08-14 23:52:48 +0200710 * @param bool
Derek Jones32bf1862010-03-02 13:46:07 -0600711 * @return type
712 */
Greg Akerf5c84022011-04-19 17:13:03 -0500713 public function remove_package_path($path = '', $remove_config_path = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600714 {
715 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200716
Derek Jones32bf1862010-03-02 13:46:07 -0600717 if ($path == '')
718 {
719 $void = array_shift($this->_ci_library_paths);
720 $void = array_shift($this->_ci_model_paths);
721 $void = array_shift($this->_ci_helper_paths);
Greg Akerf5c84022011-04-19 17:13:03 -0500722 $void = array_shift($this->_ci_view_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600723 $void = array_shift($config->_config_paths);
724 }
725 else
726 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500727 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600728 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
729 {
730 if (($key = array_search($path, $this->{$var})) !== FALSE)
731 {
732 unset($this->{$var}[$key]);
733 }
734 }
David Behlercda768a2011-08-14 23:52:48 +0200735
Greg Akerf5c84022011-04-19 17:13:03 -0500736 if (isset($this->_ci_view_paths[$path.'views/']))
737 {
738 unset($this->_ci_view_paths[$path.'views/']);
739 }
Barry Mienydd671972010-10-04 16:33:58 +0200740
Derek Jones32bf1862010-03-02 13:46:07 -0600741 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
742 {
743 unset($config->_config_paths[$key]);
744 }
745 }
Barry Mienydd671972010-10-04 16:33:58 +0200746
Derek Jones32bf1862010-03-02 13:46:07 -0600747 // make sure the application default paths are still in the array
748 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
749 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
750 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500751 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600752 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
753 }
754
755 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200756
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 /**
758 * Loader
759 *
760 * This function is used to load views and files.
761 * Variables are prefixed with _ci_ to avoid symbol collision with
762 * variables made available to view files
763 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 * @param array
765 * @return void
766 */
Greg Akerf5c84022011-04-19 17:13:03 -0500767 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 {
769 // Set the default data variables
770 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
771 {
772 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
773 }
David Behlercda768a2011-08-14 23:52:48 +0200774
Greg Akerf5c84022011-04-19 17:13:03 -0500775 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000776
777 // Set the path to the requested file
Greg Aker8807be32011-04-21 13:06:15 -0500778 if ($_ci_path != '')
779 {
780 $_ci_x = explode('/', $_ci_path);
781 $_ci_file = end($_ci_x);
782 }
783 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 {
785 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Greg Aker3a746652011-04-19 10:59:47 -0500786 $_ci_file = ($_ci_ext == '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500787
788 foreach ($this->_ci_view_paths as $view_file => $cascade)
789 {
790 if (file_exists($view_file.$_ci_file))
791 {
792 $_ci_path = $view_file.$_ci_file;
793 $file_exists = TRUE;
794 break;
795 }
David Behlercda768a2011-08-14 23:52:48 +0200796
Greg Akerf5c84022011-04-19 17:13:03 -0500797 if ( ! $cascade)
798 {
799 break;
David Behlercda768a2011-08-14 23:52:48 +0200800 }
Greg Akerf5c84022011-04-19 17:13:03 -0500801 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Greg Akerf5c84022011-04-19 17:13:03 -0500804 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 {
806 show_error('Unable to load the requested file: '.$_ci_file);
807 }
Barry Mienydd671972010-10-04 16:33:58 +0200808
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 // This allows anything loaded using $this->load (views, files, etc.)
810 // to become accessible from within the Controller and Model functions.
Barry Mienydd671972010-10-04 16:33:58 +0200811
Pascal Kriete89ace432010-11-10 15:49:10 -0500812 $_ci_CI =& get_instance();
813 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500815 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500817 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 }
819 }
820
821 /*
822 * Extract and cache variables
823 *
824 * You can either set variables using the dedicated $this->load_vars()
825 * function or via the second parameter of this function. We'll merge
826 * the two types and cache them so that views that are embedded within
827 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200828 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 if (is_array($_ci_vars))
830 {
831 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
832 }
833 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 /*
836 * Buffer the output
837 *
838 * We buffer the output for two reasons:
839 * 1. Speed. You get a significant speed boost.
840 * 2. So that the final rendered template can be
Derek Jones37f4b9c2011-07-01 17:56:50 -0500841 * post-processed by the output class. Why do we
842 * need post processing? For one thing, in order to
843 * show the elapsed page load time. Unless we
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 * can intercept the content right before it's sent to
845 * the browser and then stop the timer it won't be accurate.
846 */
847 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200848
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 // If the PHP installation does not support short tags we'll
850 // do a little string replacement, changing the short tags
851 // to standard PHP echo statements.
Barry Mienydd671972010-10-04 16:33:58 +0200852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
854 {
855 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
856 }
857 else
858 {
859 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
860 }
Barry Mienydd671972010-10-04 16:33:58 +0200861
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 // Return the file data if requested
865 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200866 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 $buffer = ob_get_contents();
868 @ob_end_clean();
869 return $buffer;
870 }
871
872 /*
873 * Flush the buffer... or buff the flusher?
874 *
875 * In order to permit views to be nested within
876 * other views, we need to flush the content back out whenever
877 * we are beyond the first level of output buffering so that
878 * it can be seen and included properly by the first included
879 * template and any subsequent ones. Oy!
880 *
Barry Mienydd671972010-10-04 16:33:58 +0200881 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 if (ob_get_level() > $this->_ci_ob_level + 1)
883 {
884 ob_end_flush();
885 }
886 else
887 {
Greg Aker22f1a632010-11-10 15:34:35 -0600888 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 @ob_end_clean();
890 }
891 }
892
893 // --------------------------------------------------------------------
894
895 /**
896 * Load class
897 *
898 * This function loads the requested class.
899 *
Barry Mienydd671972010-10-04 16:33:58 +0200900 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 * @param mixed any additional parameters
902 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200903 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 */
Greg Akerf5c84022011-04-19 17:13:03 -0500905 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200906 {
907 // Get the class name, and while we're at it trim any slashes.
908 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500910 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200911
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 // Was the path included with the class name?
913 // We look for a slash to determine this
914 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600915 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 {
Derek Jones32bf1862010-03-02 13:46:07 -0600917 // Extract the path
918 $subdir = substr($class, 0, $last_slash + 1);
Barry Mienydd671972010-10-04 16:33:58 +0200919
Derek Jones32bf1862010-03-02 13:46:07 -0600920 // Get the filename from the path
921 $class = substr($class, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 }
923
924 // We'll test for both lowercase and capitalized versions of the file name
925 foreach (array(ucfirst($class), strtolower($class)) as $class)
926 {
Greg Aker3a746652011-04-19 10:59:47 -0500927 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000928
Barry Mienydd671972010-10-04 16:33:58 +0200929 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 if (file_exists($subclass))
931 {
Greg Aker3a746652011-04-19 10:59:47 -0500932 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200933
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 if ( ! file_exists($baseclass))
935 {
936 log_message('error', "Unable to load the requested class: ".$class);
937 show_error("Unable to load the requested class: ".$class);
938 }
939
Derek Jones37f4b9c2011-07-01 17:56:50 -0500940 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 if (in_array($subclass, $this->_ci_loaded_files))
942 {
943 // Before we deem this to be a duplicate request, let's see
Derek Jones37f4b9c2011-07-01 17:56:50 -0500944 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 // return a new instance of the object
946 if ( ! is_null($object_name))
947 {
948 $CI =& get_instance();
949 if ( ! isset($CI->$object_name))
950 {
Barry Mienydd671972010-10-04 16:33:58 +0200951 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 }
953 }
Barry Mienydd671972010-10-04 16:33:58 +0200954
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 $is_duplicate = TRUE;
956 log_message('debug', $class." class already loaded. Second attempt ignored.");
957 return;
958 }
Barry Mienydd671972010-10-04 16:33:58 +0200959
960 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 include_once($subclass);
962 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200963
964 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600968 $is_duplicate = FALSE;
969 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 {
Greg Aker3a746652011-04-19 10:59:47 -0500971 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600972
Derek Jones37f4b9c2011-07-01 17:56:50 -0500973 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 if ( ! file_exists($filepath))
975 {
976 continue;
977 }
Barry Mienydd671972010-10-04 16:33:58 +0200978
Derek Jones37f4b9c2011-07-01 17:56:50 -0500979 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 if (in_array($filepath, $this->_ci_loaded_files))
981 {
982 // Before we deem this to be a duplicate request, let's see
Derek Jones37f4b9c2011-07-01 17:56:50 -0500983 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 // return a new instance of the object
985 if ( ! is_null($object_name))
986 {
987 $CI =& get_instance();
988 if ( ! isset($CI->$object_name))
989 {
990 return $this->_ci_init_class($class, '', $params, $object_name);
991 }
992 }
Barry Mienydd671972010-10-04 16:33:58 +0200993
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 $is_duplicate = TRUE;
995 log_message('debug', $class." class already loaded. Second attempt ignored.");
996 return;
997 }
Barry Mienydd671972010-10-04 16:33:58 +0200998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 include_once($filepath);
1000 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +02001001 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 }
Derek Jones32bf1862010-03-02 13:46:07 -06001003
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 } // END FOREACH
1005
Derek Jones37f4b9c2011-07-01 17:56:50 -05001006 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 if ($subdir == '')
1008 {
1009 $path = strtolower($class).'/'.$class;
1010 return $this->_ci_load_class($path, $params);
1011 }
Barry Mienydd671972010-10-04 16:33:58 +02001012
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 // If we got this far we were unable to find the requested class.
1014 // We do not issue errors if the load call failed due to a duplicate request
1015 if ($is_duplicate == FALSE)
1016 {
1017 log_message('error', "Unable to load the requested class: ".$class);
1018 show_error("Unable to load the requested class: ".$class);
1019 }
1020 }
Barry Mienydd671972010-10-04 16:33:58 +02001021
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 // --------------------------------------------------------------------
1023
1024 /**
1025 * Instantiates a class
1026 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 * @param string
1028 * @param string
David Behlercda768a2011-08-14 23:52:48 +02001029 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 * @param string an optional object name
1031 * @return null
1032 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001033 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001034 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001035 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 if ($config === NULL)
1037 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001038 // Fetch the config paths containing any package paths
1039 $config_component = $this->_ci_get_component('config');
1040
1041 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001043 // Break on the first found file, thus package files
1044 // are not overridden by default paths
1045 foreach ($config_component->_config_paths as $path)
1046 {
1047 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001048 // are case-sensitive with regard to file names. Check for environment
1049 // first, global next
Greg Aker3a746652011-04-19 10:59:47 -05001050 if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001051 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001052 include($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001053 break;
1054 }
Greg Aker3a746652011-04-19 10:59:47 -05001055 elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001056 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001057 include($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001058 break;
1059 }
Greg Aker3a746652011-04-19 10:59:47 -05001060 elseif (file_exists($path .'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001061 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001062 include($path .'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001063 break;
1064 }
Greg Aker3a746652011-04-19 10:59:47 -05001065 elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001066 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001067 include($path .'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001068 break;
1069 }
1070 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 }
1072 }
Barry Mienydd671972010-10-04 16:33:58 +02001073
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +02001075 {
1076 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 {
1078 $name = 'CI_'.$class;
1079 }
Barry Mienydd671972010-10-04 16:33:58 +02001080 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 {
1082 $name = config_item('subclass_prefix').$class;
1083 }
1084 else
1085 {
1086 $name = $class;
1087 }
1088 }
1089 else
1090 {
1091 $name = $prefix.$class;
1092 }
Barry Mienydd671972010-10-04 16:33:58 +02001093
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 // Is the class name valid?
1095 if ( ! class_exists($name))
1096 {
1097 log_message('error', "Non-existent class: ".$name);
1098 show_error("Non-existent class: ".$class);
1099 }
Barry Mienydd671972010-10-04 16:33:58 +02001100
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 // Set the variable name we will assign the class to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001102 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001104
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 if (is_null($object_name))
1106 {
1107 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
1108 }
1109 else
1110 {
1111 $classvar = $object_name;
1112 }
1113
Barry Mienydd671972010-10-04 16:33:58 +02001114 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 $this->_ci_classes[$class] = $classvar;
1116
Barry Mienydd671972010-10-04 16:33:58 +02001117 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 $CI =& get_instance();
1119 if ($config !== NULL)
1120 {
1121 $CI->$classvar = new $name($config);
1122 }
1123 else
Barry Mienydd671972010-10-04 16:33:58 +02001124 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 $CI->$classvar = new $name;
Barry Mienydd671972010-10-04 16:33:58 +02001126 }
1127 }
1128
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001130
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 /**
1132 * Autoloader
1133 *
1134 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -06001135 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 * @param array
1138 * @return void
1139 */
Shane Pearson665baec2011-08-22 18:52:19 -05001140 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001141 {
Greg Aker3a746652011-04-19 10:59:47 -05001142 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001143 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001144 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001145 }
1146 else
1147 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001148 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001149 }
Barry Mienydd671972010-10-04 16:33:58 +02001150
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 if ( ! isset($autoload))
1152 {
1153 return FALSE;
1154 }
Barry Mienydd671972010-10-04 16:33:58 +02001155
Phil Sturgeon9730c752010-12-15 10:50:15 +00001156 // Autoload packages
1157 if (isset($autoload['packages']))
1158 {
1159 foreach ($autoload['packages'] as $package_path)
1160 {
1161 $this->add_package_path($package_path);
1162 }
1163 }
1164
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 // Load any custom config file
1166 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001167 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 $CI =& get_instance();
1169 foreach ($autoload['config'] as $key => $val)
1170 {
1171 $CI->config->load($val);
1172 }
Barry Mienydd671972010-10-04 16:33:58 +02001173 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001174
Derek Jonesc6da5032010-03-09 20:44:27 -06001175 // Autoload helpers and languages
1176 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001177 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
1179 {
1180 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001181 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 }
1183
1184 // A little tweak to remain backward compatible
1185 // The $autoload['core'] item was deprecated
Derek Jones32bf1862010-03-02 13:46:07 -06001186 if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 {
1188 $autoload['libraries'] = $autoload['core'];
1189 }
Barry Mienydd671972010-10-04 16:33:58 +02001190
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 // Load libraries
1192 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
1193 {
1194 // Load the database driver.
1195 if (in_array('database', $autoload['libraries']))
1196 {
1197 $this->database();
1198 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1199 }
Barry Mienydd671972010-10-04 16:33:58 +02001200
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 // Load all other libraries
1202 foreach ($autoload['libraries'] as $item)
1203 {
1204 $this->library($item);
1205 }
Barry Mienydd671972010-10-04 16:33:58 +02001206 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001207
1208 // Autoload models
1209 if (isset($autoload['model']))
1210 {
1211 $this->model($autoload['model']);
1212 }
Barry Mienydd671972010-10-04 16:33:58 +02001213 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001214
1215 // --------------------------------------------------------------------
1216
1217 /**
1218 * Object to Array
1219 *
1220 * Takes an object as input and converts the class variables to array key/vals
1221 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 * @param object
1223 * @return array
1224 */
Greg Akerf5c84022011-04-19 17:13:03 -05001225 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 {
1227 return (is_object($object)) ? get_object_vars($object) : $object;
1228 }
1229
1230 // --------------------------------------------------------------------
1231
1232 /**
Derek Jones32bf1862010-03-02 13:46:07 -06001233 * Get a reference to a specific library or model
1234 *
David Behlercda768a2011-08-14 23:52:48 +02001235 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001236 * @return bool
1237 */
Greg Akerf5c84022011-04-19 17:13:03 -05001238 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001239 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001240 $CI =& get_instance();
1241 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001242 }
1243
1244 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001245
Derek Jones32bf1862010-03-02 13:46:07 -06001246 /**
1247 * Prep filename
1248 *
1249 * This function preps the name of various items to make loading them more reliable.
1250 *
Derek Jones32bf1862010-03-02 13:46:07 -06001251 * @param mixed
David Behlercda768a2011-08-14 23:52:48 +02001252 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001253 * @return array
1254 */
Greg Akerf5c84022011-04-19 17:13:03 -05001255 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001256 {
1257 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001258 {
Greg Aker3a746652011-04-19 10:59:47 -05001259 return array(strtolower(str_replace('.php', '', str_replace($extension, '', $filename)).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001260 }
1261 else
1262 {
1263 foreach ($filename as $key => $val)
1264 {
Greg Aker3a746652011-04-19 10:59:47 -05001265 $filename[$key] = strtolower(str_replace('.php', '', str_replace($extension, '', $val)).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001266 }
Barry Mienydd671972010-10-04 16:33:58 +02001267
Derek Jones32bf1862010-03-02 13:46:07 -06001268 return $filename;
1269 }
1270 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001271}
1272
1273/* End of file Loader.php */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001274/* Location: ./system/core/Loader.php */