blob: 4e14b54afeaabdf0b8a19e8cd77f5343b678f683 [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 /**
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 {
705 $void = array_shift($this->_ci_library_paths);
706 $void = array_shift($this->_ci_model_paths);
707 $void = array_shift($this->_ci_helper_paths);
Greg Akerf5c84022011-04-19 17:13:03 -0500708 $void = array_shift($this->_ci_view_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600709 $void = array_shift($config->_config_paths);
710 }
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.
Barry Mienydd671972010-10-04 16:33:58 +0200797
Pascal Kriete89ace432010-11-10 15:49:10 -0500798 $_ci_CI =& get_instance();
799 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500801 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500803 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 }
805 }
806
807 /*
808 * Extract and cache variables
809 *
810 * You can either set variables using the dedicated $this->load_vars()
811 * function or via the second parameter of this function. We'll merge
812 * the two types and cache them so that views that are embedded within
813 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200814 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 if (is_array($_ci_vars))
816 {
817 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
818 }
819 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 /*
822 * Buffer the output
823 *
824 * We buffer the output for two reasons:
825 * 1. Speed. You get a significant speed boost.
826 * 2. So that the final rendered template can be
Derek Jones37f4b9c2011-07-01 17:56:50 -0500827 * post-processed by the output class. Why do we
828 * need post processing? For one thing, in order to
829 * show the elapsed page load time. Unless we
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 * can intercept the content right before it's sent to
831 * the browser and then stop the timer it won't be accurate.
832 */
833 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 // If the PHP installation does not support short tags we'll
836 // do a little string replacement, changing the short tags
837 // to standard PHP echo statements.
Barry Mienydd671972010-10-04 16:33:58 +0200838
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
840 {
841 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
842 }
843 else
844 {
845 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
846 }
Barry Mienydd671972010-10-04 16:33:58 +0200847
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200849
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 // Return the file data if requested
851 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200852 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 $buffer = ob_get_contents();
854 @ob_end_clean();
855 return $buffer;
856 }
857
858 /*
859 * Flush the buffer... or buff the flusher?
860 *
861 * In order to permit views to be nested within
862 * other views, we need to flush the content back out whenever
863 * we are beyond the first level of output buffering so that
864 * it can be seen and included properly by the first included
865 * template and any subsequent ones. Oy!
866 *
Barry Mienydd671972010-10-04 16:33:58 +0200867 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 if (ob_get_level() > $this->_ci_ob_level + 1)
869 {
870 ob_end_flush();
871 }
872 else
873 {
Greg Aker22f1a632010-11-10 15:34:35 -0600874 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 @ob_end_clean();
876 }
877 }
878
879 // --------------------------------------------------------------------
880
881 /**
882 * Load class
883 *
884 * This function loads the requested class.
885 *
Barry Mienydd671972010-10-04 16:33:58 +0200886 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 * @param mixed any additional parameters
888 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200889 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 */
Greg Akerf5c84022011-04-19 17:13:03 -0500891 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200892 {
893 // Get the class name, and while we're at it trim any slashes.
894 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500896 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200897
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 // Was the path included with the class name?
899 // We look for a slash to determine this
900 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600901 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 {
Derek Jones32bf1862010-03-02 13:46:07 -0600903 // Extract the path
904 $subdir = substr($class, 0, $last_slash + 1);
Barry Mienydd671972010-10-04 16:33:58 +0200905
Derek Jones32bf1862010-03-02 13:46:07 -0600906 // Get the filename from the path
907 $class = substr($class, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 }
909
910 // We'll test for both lowercase and capitalized versions of the file name
911 foreach (array(ucfirst($class), strtolower($class)) as $class)
912 {
Greg Aker3a746652011-04-19 10:59:47 -0500913 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000914
Barry Mienydd671972010-10-04 16:33:58 +0200915 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 if (file_exists($subclass))
917 {
Greg Aker3a746652011-04-19 10:59:47 -0500918 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 if ( ! file_exists($baseclass))
921 {
922 log_message('error', "Unable to load the requested class: ".$class);
923 show_error("Unable to load the requested class: ".$class);
924 }
925
Derek Jones37f4b9c2011-07-01 17:56:50 -0500926 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 if (in_array($subclass, $this->_ci_loaded_files))
928 {
929 // Before we deem this to be a duplicate request, let's see
Derek Jones37f4b9c2011-07-01 17:56:50 -0500930 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 // return a new instance of the object
932 if ( ! is_null($object_name))
933 {
934 $CI =& get_instance();
935 if ( ! isset($CI->$object_name))
936 {
Barry Mienydd671972010-10-04 16:33:58 +0200937 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 }
939 }
Barry Mienydd671972010-10-04 16:33:58 +0200940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 $is_duplicate = TRUE;
942 log_message('debug', $class." class already loaded. Second attempt ignored.");
943 return;
944 }
Barry Mienydd671972010-10-04 16:33:58 +0200945
946 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 include_once($subclass);
948 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200949
950 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 }
Barry Mienydd671972010-10-04 16:33:58 +0200952
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600954 $is_duplicate = FALSE;
955 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 {
Greg Aker3a746652011-04-19 10:59:47 -0500957 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600958
Derek Jones37f4b9c2011-07-01 17:56:50 -0500959 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 if ( ! file_exists($filepath))
961 {
962 continue;
963 }
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Jones37f4b9c2011-07-01 17:56:50 -0500965 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 if (in_array($filepath, $this->_ci_loaded_files))
967 {
968 // Before we deem this to be a duplicate request, let's see
Derek Jones37f4b9c2011-07-01 17:56:50 -0500969 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 // return a new instance of the object
971 if ( ! is_null($object_name))
972 {
973 $CI =& get_instance();
974 if ( ! isset($CI->$object_name))
975 {
976 return $this->_ci_init_class($class, '', $params, $object_name);
977 }
978 }
Barry Mienydd671972010-10-04 16:33:58 +0200979
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 $is_duplicate = TRUE;
981 log_message('debug', $class." class already loaded. Second attempt ignored.");
982 return;
983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 include_once($filepath);
986 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +0200987 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 }
Derek Jones32bf1862010-03-02 13:46:07 -0600989
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 } // END FOREACH
991
Derek Jones37f4b9c2011-07-01 17:56:50 -0500992 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 if ($subdir == '')
994 {
995 $path = strtolower($class).'/'.$class;
996 return $this->_ci_load_class($path, $params);
997 }
Barry Mienydd671972010-10-04 16:33:58 +0200998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 // If we got this far we were unable to find the requested class.
1000 // We do not issue errors if the load call failed due to a duplicate request
1001 if ($is_duplicate == FALSE)
1002 {
1003 log_message('error', "Unable to load the requested class: ".$class);
1004 show_error("Unable to load the requested class: ".$class);
1005 }
1006 }
Barry Mienydd671972010-10-04 16:33:58 +02001007
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 // --------------------------------------------------------------------
1009
1010 /**
1011 * Instantiates a class
1012 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 * @param string
1014 * @param string
David Behlercda768a2011-08-14 23:52:48 +02001015 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 * @param string an optional object name
1017 * @return null
1018 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001019 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001020 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001021 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 if ($config === NULL)
1023 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001024 // Fetch the config paths containing any package paths
1025 $config_component = $this->_ci_get_component('config');
1026
1027 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001029 // Break on the first found file, thus package files
1030 // are not overridden by default paths
1031 foreach ($config_component->_config_paths as $path)
1032 {
1033 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001034 // are case-sensitive with regard to file names. Check for environment
1035 // first, global next
Greg Aker3a746652011-04-19 10:59:47 -05001036 if (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001037 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001038 include($path .'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001039 break;
1040 }
Greg Aker3a746652011-04-19 10:59:47 -05001041 elseif (defined('ENVIRONMENT') AND file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001042 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001043 include($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001044 break;
1045 }
Greg Aker3a746652011-04-19 10:59:47 -05001046 elseif (file_exists($path .'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001047 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001048 include($path .'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001049 break;
1050 }
Greg Aker3a746652011-04-19 10:59:47 -05001051 elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001052 {
Phil Sturgeon6f1b3842011-08-13 10:28:28 -06001053 include($path .'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001054 break;
1055 }
1056 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 }
1058 }
Barry Mienydd671972010-10-04 16:33:58 +02001059
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +02001061 {
1062 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 {
1064 $name = 'CI_'.$class;
1065 }
Barry Mienydd671972010-10-04 16:33:58 +02001066 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 {
1068 $name = config_item('subclass_prefix').$class;
1069 }
1070 else
1071 {
1072 $name = $class;
1073 }
1074 }
1075 else
1076 {
1077 $name = $prefix.$class;
1078 }
Barry Mienydd671972010-10-04 16:33:58 +02001079
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 // Is the class name valid?
1081 if ( ! class_exists($name))
1082 {
1083 log_message('error', "Non-existent class: ".$name);
1084 show_error("Non-existent class: ".$class);
1085 }
Barry Mienydd671972010-10-04 16:33:58 +02001086
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 // Set the variable name we will assign the class to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001088 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001090
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 if (is_null($object_name))
1092 {
1093 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
1094 }
1095 else
1096 {
1097 $classvar = $object_name;
1098 }
1099
Barry Mienydd671972010-10-04 16:33:58 +02001100 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 $this->_ci_classes[$class] = $classvar;
1102
Barry Mienydd671972010-10-04 16:33:58 +02001103 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 $CI =& get_instance();
1105 if ($config !== NULL)
1106 {
1107 $CI->$classvar = new $name($config);
1108 }
1109 else
Barry Mienydd671972010-10-04 16:33:58 +02001110 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 $CI->$classvar = new $name;
Barry Mienydd671972010-10-04 16:33:58 +02001112 }
1113 }
1114
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001116
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 /**
1118 * Autoloader
1119 *
1120 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -06001121 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 * @param array
1124 * @return void
1125 */
Shane Pearson665baec2011-08-22 18:52:19 -05001126 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001127 {
Greg Aker3a746652011-04-19 10:59:47 -05001128 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001129 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001130 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001131 }
1132 else
1133 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001134 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001135 }
Barry Mienydd671972010-10-04 16:33:58 +02001136
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 if ( ! isset($autoload))
1138 {
1139 return FALSE;
1140 }
Barry Mienydd671972010-10-04 16:33:58 +02001141
Phil Sturgeon9730c752010-12-15 10:50:15 +00001142 // Autoload packages
1143 if (isset($autoload['packages']))
1144 {
1145 foreach ($autoload['packages'] as $package_path)
1146 {
1147 $this->add_package_path($package_path);
1148 }
1149 }
1150
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 // Load any custom config file
1152 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001153 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 $CI =& get_instance();
1155 foreach ($autoload['config'] as $key => $val)
1156 {
1157 $CI->config->load($val);
1158 }
Barry Mienydd671972010-10-04 16:33:58 +02001159 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001160
Derek Jonesc6da5032010-03-09 20:44:27 -06001161 // Autoload helpers and languages
1162 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001163 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
1165 {
1166 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001167 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 }
1169
1170 // A little tweak to remain backward compatible
1171 // The $autoload['core'] item was deprecated
Derek Jones32bf1862010-03-02 13:46:07 -06001172 if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 {
1174 $autoload['libraries'] = $autoload['core'];
1175 }
Barry Mienydd671972010-10-04 16:33:58 +02001176
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 // Load libraries
1178 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
1179 {
1180 // Load the database driver.
1181 if (in_array('database', $autoload['libraries']))
1182 {
1183 $this->database();
1184 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1185 }
Barry Mienydd671972010-10-04 16:33:58 +02001186
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 // Load all other libraries
1188 foreach ($autoload['libraries'] as $item)
1189 {
1190 $this->library($item);
1191 }
Barry Mienydd671972010-10-04 16:33:58 +02001192 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001193
1194 // Autoload models
1195 if (isset($autoload['model']))
1196 {
1197 $this->model($autoload['model']);
1198 }
Barry Mienydd671972010-10-04 16:33:58 +02001199 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001200
1201 // --------------------------------------------------------------------
1202
1203 /**
1204 * Object to Array
1205 *
1206 * Takes an object as input and converts the class variables to array key/vals
1207 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 * @param object
1209 * @return array
1210 */
Greg Akerf5c84022011-04-19 17:13:03 -05001211 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 {
1213 return (is_object($object)) ? get_object_vars($object) : $object;
1214 }
1215
1216 // --------------------------------------------------------------------
1217
1218 /**
Derek Jones32bf1862010-03-02 13:46:07 -06001219 * Get a reference to a specific library or model
1220 *
David Behlercda768a2011-08-14 23:52:48 +02001221 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001222 * @return bool
1223 */
Greg Akerf5c84022011-04-19 17:13:03 -05001224 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001225 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001226 $CI =& get_instance();
1227 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001228 }
1229
1230 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001231
Derek Jones32bf1862010-03-02 13:46:07 -06001232 /**
1233 * Prep filename
1234 *
1235 * This function preps the name of various items to make loading them more reliable.
1236 *
Derek Jones32bf1862010-03-02 13:46:07 -06001237 * @param mixed
David Behlercda768a2011-08-14 23:52:48 +02001238 * @param string
Derek Jones32bf1862010-03-02 13:46:07 -06001239 * @return array
1240 */
Greg Akerf5c84022011-04-19 17:13:03 -05001241 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001242 {
1243 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001244 {
Greg Aker3a746652011-04-19 10:59:47 -05001245 return array(strtolower(str_replace('.php', '', str_replace($extension, '', $filename)).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001246 }
1247 else
1248 {
1249 foreach ($filename as $key => $val)
1250 {
Greg Aker3a746652011-04-19 10:59:47 -05001251 $filename[$key] = strtolower(str_replace('.php', '', str_replace($extension, '', $val)).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001252 }
Barry Mienydd671972010-10-04 16:33:58 +02001253
Derek Jones32bf1862010-03-02 13:46:07 -06001254 return $filename;
1255 }
1256 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001257}
1258
1259/* End of file Loader.php */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001260/* Location: ./system/core/Loader.php */