blob: e2970613b4a31ba6b61e46cbda871929be81040f [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Loader Class
20 *
21 * Loads views and files
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @author ExpressionEngine Dev Team
26 * @category Loader
27 * @link http://codeigniter.com/user_guide/libraries/loader.html
28 */
29class CI_Loader {
30
31 // All these are set automatically. Don't mess with them.
32 var $_ci_ob_level;
33 var $_ci_view_path = '';
Derek Jones32bf1862010-03-02 13:46:07 -060034 var $_ci_library_paths = array();
35 var $_ci_model_paths = array();
36 var $_ci_helper_paths = array();
Barry Mienydd671972010-10-04 16:33:58 +020037 var $_ci_is_instance = FALSE; // Whether we should use $this or $CI =& get_instance()
Derek Jones32bf1862010-03-02 13:46:07 -060038 var $_base_classes = array(); // Set by the controller class
Derek Allard2067d1a2008-11-13 22:59:24 +000039 var $_ci_cached_vars = array();
40 var $_ci_classes = array();
41 var $_ci_loaded_files = array();
42 var $_ci_models = array();
43 var $_ci_helpers = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000044 var $_ci_varmap = array('unit_test' => 'unit', 'user_agent' => 'agent');
Barry Mienydd671972010-10-04 16:33:58 +020045
Derek Allard2067d1a2008-11-13 22:59:24 +000046
47 /**
48 * Constructor
49 *
50 * Sets the path to the view files and gets the initial output buffering level
51 *
52 * @access public
53 */
54 function CI_Loader()
Barry Mienydd671972010-10-04 16:33:58 +020055 {
Derek Allard2067d1a2008-11-13 22:59:24 +000056 $this->_ci_view_path = APPPATH.'views/';
57 $this->_ci_ob_level = ob_get_level();
Derek Jones32bf1862010-03-02 13:46:07 -060058 $this->_ci_library_paths = array(APPPATH, BASEPATH);
59 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
60 $this->_ci_model_paths = array(APPPATH);
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 log_message('debug', "Loader Class Initialized");
63 }
Barry Mienydd671972010-10-04 16:33:58 +020064
Derek Allard2067d1a2008-11-13 22:59:24 +000065 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Allard2067d1a2008-11-13 22:59:24 +000067 /**
68 * Class Loader
69 *
70 * This function lets users load and instantiate classes.
71 * It is designed to be called from a user's app controllers.
72 *
73 * @access public
74 * @param string the name of the class
75 * @param mixed the optional parameters
76 * @param string an optional object name
77 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020078 */
Derek Allard2067d1a2008-11-13 22:59:24 +000079 function library($library = '', $params = NULL, $object_name = NULL)
80 {
Greg Akerce433962010-10-12 09:29:35 -050081 if (is_array($library))
82 {
83 foreach($library as $read)
84 {
85 $this->library($read);
86 }
87
88 return;
89 }
90
Derek Jones32bf1862010-03-02 13:46:07 -060091 if ($library == '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
93 return FALSE;
94 }
95
Derek Jones32bf1862010-03-02 13:46:07 -060096 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
98 $params = NULL;
99 }
100
101 if (is_array($library))
102 {
103 foreach ($library as $class)
104 {
105 $this->_ci_load_class($class, $params, $object_name);
106 }
107 }
108 else
109 {
110 $this->_ci_load_class($library, $params, $object_name);
111 }
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 $this->_ci_assign_to_models();
114 }
115
116 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 /**
119 * Model Loader
120 *
121 * This function lets users load and instantiate models.
122 *
123 * @access public
124 * @param string the name of the class
125 * @param string name for the model
126 * @param bool database connection
127 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200128 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200130 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 if (is_array($model))
132 {
133 foreach($model as $babe)
134 {
Barry Mienydd671972010-10-04 16:33:58 +0200135 $this->model($babe);
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
137 return;
138 }
139
140 if ($model == '')
141 {
142 return;
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Jones32bf1862010-03-02 13:46:07 -0600145 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600148 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
Derek Jones32bf1862010-03-02 13:46:07 -0600150 // The path is in front of the last slash
151 $path = substr($model, 0, $last_slash + 1);
152
153 // And the model name behind it
154 $model = substr($model, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 if ($name == '')
158 {
159 $name = $model;
160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 if (in_array($name, $this->_ci_models, TRUE))
163 {
164 return;
165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 $CI =& get_instance();
168 if (isset($CI->$name))
169 {
170 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000174
Derek Jones32bf1862010-03-02 13:46:07 -0600175 foreach ($this->_ci_model_paths as $mod_path)
176 {
177 if ( ! file_exists($mod_path.'models/'.$path.$model.EXT))
178 {
179 continue;
180 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000181
Derek Jones32bf1862010-03-02 13:46:07 -0600182 if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
183 {
184 if ($db_conn === TRUE)
185 $db_conn = '';
186
187 $CI->load->database($db_conn, FALSE, TRUE);
188 }
189
Greg Akerbce13482010-10-11 15:37:16 -0500190 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600191 {
192 load_class('Model', 'core');
193 }
194
195 require_once($mod_path.'models/'.$path.$model.EXT);
196
197 $model = ucfirst($model);
198
199 $CI->$name = new $model();
200 $CI->$name->_assign_libraries();
201
202 $this->_ci_models[] = $name;
203 return;
204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Jones32bf1862010-03-02 13:46:07 -0600206 // couldn't find the model
207 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 /**
213 * Database Loader
214 *
215 * @access public
216 * @param string the DB credentials
217 * @param bool whether to return the DB object
218 * @param bool whether to enable active record (this allows us to override the config setting)
219 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200220 */
Derek Jonesffca6c22009-12-05 15:31:44 +0000221 function database($params = '', $return = FALSE, $active_record = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 // Grab the super object
224 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 // Do we even need to load the database class?
Derek Jones9fb6dd12009-12-05 15:32:48 +0000227 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 +0000228 {
229 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200230 }
231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 require_once(BASEPATH.'database/DB'.EXT);
233
234 if ($return === TRUE)
235 {
236 return DB($params, $active_record);
237 }
Barry Mienydd671972010-10-04 16:33:58 +0200238
239 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 // reference errors with some configurations
241 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 // Load the DB class
Barry Mienydd671972010-10-04 16:33:58 +0200244 $CI->db =& DB($params, $active_record);
245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 // Assign the DB object to any existing models
247 $this->_ci_assign_to_models();
248 }
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 // --------------------------------------------------------------------
251
252 /**
253 * Load the Utilities Class
254 *
255 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200256 * @return string
257 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 function dbutil()
259 {
260 if ( ! class_exists('CI_DB'))
261 {
262 $this->database();
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 $CI =& get_instance();
266
267 // for backwards compatibility, load dbforge so we can extend dbutils off it
268 // this use is deprecated and strongly discouraged
269 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 require_once(BASEPATH.'database/DB_utility'.EXT);
272 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT);
273 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
274
Derek Jonesf0a9b332009-07-29 14:19:18 +0000275 $CI->dbutil =& instantiate_class(new $class());
Derek Allard2067d1a2008-11-13 22:59:24 +0000276
277 $CI->load->_ci_assign_to_models();
278 }
Barry Mienydd671972010-10-04 16:33:58 +0200279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 // --------------------------------------------------------------------
281
282 /**
283 * Load the Database Forge Class
284 *
285 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200286 * @return string
287 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 function dbforge()
289 {
290 if ( ! class_exists('CI_DB'))
291 {
292 $this->database();
293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 require_once(BASEPATH.'database/DB_forge'.EXT);
298 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge'.EXT);
299 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
300
301 $CI->dbforge = new $class();
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 $CI->load->_ci_assign_to_models();
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 /**
309 * Load View
310 *
311 * This function is used to load a "view" file. It has three parameters:
312 *
313 * 1. The name of the "view" file to be included.
314 * 2. An associative array of data to be extracted for use in the view.
315 * 3. TRUE/FALSE - whether to return the data or load it. In
316 * some cases it's advantageous to be able to return data so that
317 * a developer can process it in some way.
318 *
319 * @access public
320 * @param string
321 * @param array
322 * @param bool
323 * @return void
324 */
325 function view($view, $vars = array(), $return = FALSE)
326 {
327 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 /**
333 * Load File
334 *
335 * This is a generic file loader
336 *
337 * @access public
338 * @param string
339 * @param bool
340 * @return string
341 */
342 function file($path, $return = FALSE)
343 {
344 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 /**
350 * Set Variables
351 *
352 * Once variables are set they become available within
353 * the controller class and its "view" files.
354 *
355 * @access public
356 * @param array
357 * @return void
358 */
359 function vars($vars = array(), $val = '')
360 {
361 if ($val != '' AND is_string($vars))
362 {
363 $vars = array($vars => $val);
364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 if (is_array($vars) AND count($vars) > 0)
369 {
370 foreach ($vars as $key => $val)
371 {
372 $this->_ci_cached_vars[$key] = $val;
373 }
374 }
375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 /**
380 * Load Helper
381 *
382 * This function loads the specified helper file.
383 *
384 * @access public
385 * @param mixed
386 * @return void
387 */
388 function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200389 {
Derek Jones32bf1862010-03-02 13:46:07 -0600390 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200391 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 if (isset($this->_ci_helpers[$helper]))
393 {
394 continue;
395 }
Derek Jones32bf1862010-03-02 13:46:07 -0600396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.EXT;
398
Barry Mienydd671972010-10-04 16:33:58 +0200399 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 if (file_exists($ext_helper))
401 {
402 $base_helper = BASEPATH.'helpers/'.$helper.EXT;
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 if ( ! file_exists($base_helper))
405 {
406 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 include_once($ext_helper);
410 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Jones32bf1862010-03-02 13:46:07 -0600412 $this->_ci_helpers[$helper] = TRUE;
413 log_message('debug', 'Helper loaded: '.$helper);
414 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Jones32bf1862010-03-02 13:46:07 -0600417 // Try to load the helper
418 foreach ($this->_ci_helper_paths as $path)
419 {
420 if (file_exists($path.'helpers/'.$helper.EXT))
Barry Mienydd671972010-10-04 16:33:58 +0200421 {
Derek Jones32bf1862010-03-02 13:46:07 -0600422 include_once($path.'helpers/'.$helper.EXT);
423
424 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200425 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600426 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
428 }
429
Derek Jones32bf1862010-03-02 13:46:07 -0600430 // unable to load the helper
431 if ( ! isset($this->_ci_helpers[$helper]))
432 {
Barry Mienydd671972010-10-04 16:33:58 +0200433 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
Derek Jones32bf1862010-03-02 13:46:07 -0600434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 /**
441 * Load Helpers
442 *
443 * This is simply an alias to the above function in case the
444 * user has written the plural form of this function.
445 *
446 * @access public
447 * @param array
448 * @return void
449 */
450 function helpers($helpers = array())
451 {
452 $this->helper($helpers);
453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 * Loads a language file
459 *
460 * @access public
461 * @param array
462 * @param string
463 * @return void
464 */
465 function language($file = array(), $lang = '')
466 {
467 $CI =& get_instance();
468
469 if ( ! is_array($file))
470 {
471 $file = array($file);
472 }
473
474 foreach ($file as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200475 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 $CI->lang->load($langfile, $lang);
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 /**
483 * Loads a config file
484 *
485 * @access public
486 * @param string
487 * @return void
488 */
489 function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200490 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 $CI =& get_instance();
492 $CI->config->load($file, $use_sections, $fail_gracefully);
493 }
494
495 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 /**
Derek Jones8dca0412010-03-05 13:01:44 -0600498 * Driver
499 *
500 * Loads a driver library
501 *
502 * @param string the name of the class
503 * @param mixed the optional parameters
504 * @param string an optional object name
505 * @return void
506 */
507 function driver($library = '', $params = NULL, $object_name = NULL)
508 {
509 if ( ! class_exists('CI_Driver_Library'))
510 {
511 // we aren't instantiating an object here, that'll be done by the Library itself
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600512 require BASEPATH.'libraries/Driver'.EXT;
513 }
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600515 // We can save the loader some time since Drivers will *always* be in a subfolder,
516 // and typically identically named to the library
517 if ( ! strpos($library, '/'))
518 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500519 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Jones8dca0412010-03-05 13:01:44 -0600522 return $this->library($library, $params, $object_name);
523 }
524
525 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Jones8dca0412010-03-05 13:01:44 -0600527 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600528 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 *
Derek Jones32bf1862010-03-02 13:46:07 -0600530 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 *
532 * @access public
533 * @param string
534 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600535 */
536 function add_package_path($path)
537 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500538 $path = rtrim($path, '/').'/';
539
Derek Jones32bf1862010-03-02 13:46:07 -0600540 array_unshift($this->_ci_library_paths, $path);
541 array_unshift($this->_ci_model_paths, $path);
542 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Jones32bf1862010-03-02 13:46:07 -0600544 // Add config file path
545 $config =& $this->_ci_get_component('config');
546 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 }
548
549 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600550
551 /**
552 * Remove Package Path
553 *
554 * Remove a path from the library, model, and helper path arrays if it exists
555 * If no path is provided, the most recently added path is removed.
556 *
557 * @access public
558 * @param type
559 * @return type
560 */
561 function remove_package_path($path = '', $remove_config_path = TRUE)
562 {
563 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200564
Derek Jones32bf1862010-03-02 13:46:07 -0600565 if ($path == '')
566 {
567 $void = array_shift($this->_ci_library_paths);
568 $void = array_shift($this->_ci_model_paths);
569 $void = array_shift($this->_ci_helper_paths);
570 $void = array_shift($config->_config_paths);
571 }
572 else
573 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500574 $path = rtrim($path, '/').'/';
575
Derek Jones32bf1862010-03-02 13:46:07 -0600576 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
577 {
578 if (($key = array_search($path, $this->{$var})) !== FALSE)
579 {
580 unset($this->{$var}[$key]);
581 }
582 }
Barry Mienydd671972010-10-04 16:33:58 +0200583
Derek Jones32bf1862010-03-02 13:46:07 -0600584 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
585 {
586 unset($config->_config_paths[$key]);
587 }
588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Jones32bf1862010-03-02 13:46:07 -0600590 // make sure the application default paths are still in the array
591 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
592 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
593 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
594 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
595 }
596
597 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 /**
600 * Loader
601 *
602 * This function is used to load views and files.
603 * Variables are prefixed with _ci_ to avoid symbol collision with
604 * variables made available to view files
605 *
606 * @access private
607 * @param array
608 * @return void
609 */
610 function _ci_load($_ci_data)
611 {
612 // Set the default data variables
613 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
614 {
615 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
616 }
617
618 // Set the path to the requested file
619 if ($_ci_path == '')
620 {
621 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
622 $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view;
623 $_ci_path = $this->_ci_view_path.$_ci_file;
624 }
625 else
626 {
627 $_ci_x = explode('/', $_ci_path);
628 $_ci_file = end($_ci_x);
629 }
Barry Mienydd671972010-10-04 16:33:58 +0200630
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 if ( ! file_exists($_ci_path))
632 {
633 show_error('Unable to load the requested file: '.$_ci_file);
634 }
Barry Mienydd671972010-10-04 16:33:58 +0200635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 // This allows anything loaded using $this->load (views, files, etc.)
637 // to become accessible from within the Controller and Model functions.
638 // Only needed when running PHP 5
Barry Mienydd671972010-10-04 16:33:58 +0200639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 if ($this->_ci_is_instance())
641 {
642 $_ci_CI =& get_instance();
643 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
644 {
645 if ( ! isset($this->$_ci_key))
646 {
647 $this->$_ci_key =& $_ci_CI->$_ci_key;
648 }
649 }
650 }
651
652 /*
653 * Extract and cache variables
654 *
655 * You can either set variables using the dedicated $this->load_vars()
656 * function or via the second parameter of this function. We'll merge
657 * the two types and cache them so that views that are embedded within
658 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200659 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 if (is_array($_ci_vars))
661 {
662 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
663 }
664 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 /*
667 * Buffer the output
668 *
669 * We buffer the output for two reasons:
670 * 1. Speed. You get a significant speed boost.
671 * 2. So that the final rendered template can be
672 * post-processed by the output class. Why do we
673 * need post processing? For one thing, in order to
674 * show the elapsed page load time. Unless we
675 * can intercept the content right before it's sent to
676 * the browser and then stop the timer it won't be accurate.
677 */
678 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 // If the PHP installation does not support short tags we'll
681 // do a little string replacement, changing the short tags
682 // to standard PHP echo statements.
Barry Mienydd671972010-10-04 16:33:58 +0200683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
685 {
686 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
687 }
688 else
689 {
690 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
691 }
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 // Return the file data if requested
696 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200697 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 $buffer = ob_get_contents();
699 @ob_end_clean();
700 return $buffer;
701 }
702
703 /*
704 * Flush the buffer... or buff the flusher?
705 *
706 * In order to permit views to be nested within
707 * other views, we need to flush the content back out whenever
708 * we are beyond the first level of output buffering so that
709 * it can be seen and included properly by the first included
710 * template and any subsequent ones. Oy!
711 *
Barry Mienydd671972010-10-04 16:33:58 +0200712 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 if (ob_get_level() > $this->_ci_ob_level + 1)
714 {
715 ob_end_flush();
716 }
717 else
718 {
719 // PHP 4 requires that we use a global
720 global $OUT;
721 $OUT->append_output(ob_get_contents());
722 @ob_end_clean();
723 }
724 }
725
726 // --------------------------------------------------------------------
727
728 /**
729 * Load class
730 *
731 * This function loads the requested class.
732 *
733 * @access private
Barry Mienydd671972010-10-04 16:33:58 +0200734 * @param string the item that is being loaded
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 * @param mixed any additional parameters
736 * @param string an optional object name
Barry Mienydd671972010-10-04 16:33:58 +0200737 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 */
739 function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200740 {
741 // Get the class name, and while we're at it trim any slashes.
742 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 // but we don't want a leading slash
744 $class = str_replace(EXT, '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 // Was the path included with the class name?
747 // We look for a slash to determine this
748 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600749 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 {
Derek Jones32bf1862010-03-02 13:46:07 -0600751 // Extract the path
752 $subdir = substr($class, 0, $last_slash + 1);
Barry Mienydd671972010-10-04 16:33:58 +0200753
Derek Jones32bf1862010-03-02 13:46:07 -0600754 // Get the filename from the path
755 $class = substr($class, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 }
757
758 // We'll test for both lowercase and capitalized versions of the file name
759 foreach (array(ucfirst($class), strtolower($class)) as $class)
760 {
761 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.EXT;
762
Barry Mienydd671972010-10-04 16:33:58 +0200763 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 if (file_exists($subclass))
765 {
766 $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 if ( ! file_exists($baseclass))
769 {
770 log_message('error', "Unable to load the requested class: ".$class);
771 show_error("Unable to load the requested class: ".$class);
772 }
773
774 // Safety: Was the class already loaded by a previous call?
775 if (in_array($subclass, $this->_ci_loaded_files))
776 {
777 // Before we deem this to be a duplicate request, let's see
778 // if a custom object name is being supplied. If so, we'll
779 // return a new instance of the object
780 if ( ! is_null($object_name))
781 {
782 $CI =& get_instance();
783 if ( ! isset($CI->$object_name))
784 {
Barry Mienydd671972010-10-04 16:33:58 +0200785 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 }
787 }
Barry Mienydd671972010-10-04 16:33:58 +0200788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 $is_duplicate = TRUE;
790 log_message('debug', $class." class already loaded. Second attempt ignored.");
791 return;
792 }
Barry Mienydd671972010-10-04 16:33:58 +0200793
794 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 include_once($subclass);
796 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200797
798 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 }
Barry Mienydd671972010-10-04 16:33:58 +0200800
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600802 $is_duplicate = FALSE;
803 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 $filepath = $path.'libraries/'.$subdir.$class.EXT;
Derek Jones32bf1862010-03-02 13:46:07 -0600806
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 // Does the file exist? No? Bummer...
808 if ( ! file_exists($filepath))
809 {
810 continue;
811 }
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 // Safety: Was the class already loaded by a previous call?
814 if (in_array($filepath, $this->_ci_loaded_files))
815 {
816 // Before we deem this to be a duplicate request, let's see
817 // if a custom object name is being supplied. If so, we'll
818 // return a new instance of the object
819 if ( ! is_null($object_name))
820 {
821 $CI =& get_instance();
822 if ( ! isset($CI->$object_name))
823 {
824 return $this->_ci_init_class($class, '', $params, $object_name);
825 }
826 }
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 $is_duplicate = TRUE;
829 log_message('debug', $class." class already loaded. Second attempt ignored.");
830 return;
831 }
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 include_once($filepath);
834 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +0200835 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 }
Derek Jones32bf1862010-03-02 13:46:07 -0600837
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 } // END FOREACH
839
840 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
841 if ($subdir == '')
842 {
843 $path = strtolower($class).'/'.$class;
844 return $this->_ci_load_class($path, $params);
845 }
Barry Mienydd671972010-10-04 16:33:58 +0200846
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 // If we got this far we were unable to find the requested class.
848 // We do not issue errors if the load call failed due to a duplicate request
849 if ($is_duplicate == FALSE)
850 {
851 log_message('error', "Unable to load the requested class: ".$class);
852 show_error("Unable to load the requested class: ".$class);
853 }
854 }
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 // --------------------------------------------------------------------
857
858 /**
859 * Instantiates a class
860 *
861 * @access private
862 * @param string
863 * @param string
864 * @param string an optional object name
865 * @return null
866 */
867 function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200868 {
Derek Jones32bf1862010-03-02 13:46:07 -0600869 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 if ($config === NULL)
871 {
872 // We test for both uppercase and lowercase, for servers that
873 // are case-sensitive with regard to file names
874 if (file_exists(APPPATH.'config/'.strtolower($class).EXT))
875 {
876 include_once(APPPATH.'config/'.strtolower($class).EXT);
Barry Mienydd671972010-10-04 16:33:58 +0200877 }
Derek Jonesc8dddd92009-07-10 18:58:03 +0000878 elseif (file_exists(APPPATH.'config/'.ucfirst(strtolower($class)).EXT))
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
Derek Jonesc8dddd92009-07-10 18:58:03 +0000880 include_once(APPPATH.'config/'.ucfirst(strtolower($class)).EXT);
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 }
882 }
Barry Mienydd671972010-10-04 16:33:58 +0200883
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 if ($prefix == '')
Barry Mienydd671972010-10-04 16:33:58 +0200885 {
886 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 {
888 $name = 'CI_'.$class;
889 }
Barry Mienydd671972010-10-04 16:33:58 +0200890 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 {
892 $name = config_item('subclass_prefix').$class;
893 }
894 else
895 {
896 $name = $class;
897 }
898 }
899 else
900 {
901 $name = $prefix.$class;
902 }
Barry Mienydd671972010-10-04 16:33:58 +0200903
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 // Is the class name valid?
905 if ( ! class_exists($name))
906 {
907 log_message('error', "Non-existent class: ".$name);
908 show_error("Non-existent class: ".$class);
909 }
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 // Set the variable name we will assign the class to
912 // Was a custom class name supplied? If so we'll use it
913 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +0200914
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 if (is_null($object_name))
916 {
917 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
918 }
919 else
920 {
921 $classvar = $object_name;
922 }
923
Barry Mienydd671972010-10-04 16:33:58 +0200924 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 $this->_ci_classes[$class] = $classvar;
926
Barry Mienydd671972010-10-04 16:33:58 +0200927 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 $CI =& get_instance();
929 if ($config !== NULL)
930 {
931 $CI->$classvar = new $name($config);
932 }
933 else
Barry Mienydd671972010-10-04 16:33:58 +0200934 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 $CI->$classvar = new $name;
Barry Mienydd671972010-10-04 16:33:58 +0200936 }
937 }
938
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 /**
942 * Autoloader
943 *
944 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -0600945 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 *
947 * @access private
948 * @param array
949 * @return void
950 */
951 function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +0200952 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 include_once(APPPATH.'config/autoload'.EXT);
Barry Mienydd671972010-10-04 16:33:58 +0200954
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 if ( ! isset($autoload))
956 {
957 return FALSE;
958 }
Barry Mienydd671972010-10-04 16:33:58 +0200959
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 // Load any custom config file
961 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200962 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 $CI =& get_instance();
964 foreach ($autoload['config'] as $key => $val)
965 {
966 $CI->config->load($val);
967 }
Barry Mienydd671972010-10-04 16:33:58 +0200968 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000969
Derek Jonesc6da5032010-03-09 20:44:27 -0600970 // Autoload helpers and languages
971 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +0200972 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
974 {
975 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +0200976 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 }
978
979 // A little tweak to remain backward compatible
980 // The $autoload['core'] item was deprecated
Derek Jones32bf1862010-03-02 13:46:07 -0600981 if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 {
983 $autoload['libraries'] = $autoload['core'];
984 }
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 // Load libraries
987 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
988 {
989 // Load the database driver.
990 if (in_array('database', $autoload['libraries']))
991 {
992 $this->database();
993 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
994 }
Barry Mienydd671972010-10-04 16:33:58 +0200995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 // Load all other libraries
997 foreach ($autoload['libraries'] as $item)
998 {
999 $this->library($item);
1000 }
Barry Mienydd671972010-10-04 16:33:58 +02001001 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001002
1003 // Autoload models
1004 if (isset($autoload['model']))
1005 {
1006 $this->model($autoload['model']);
1007 }
1008
1009 }
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 // --------------------------------------------------------------------
1012
1013 /**
1014 * Assign to Models
1015 *
Derek Jonesc6da5032010-03-09 20:44:27 -06001016 * Makes sure that anything loaded by the loader class (libraries, etc.)
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 * will be available to models, if any exist.
1018 *
1019 * @access private
1020 * @param object
1021 * @return array
1022 */
1023 function _ci_assign_to_models()
1024 {
1025 if (count($this->_ci_models) == 0)
1026 {
1027 return;
1028 }
Barry Mienydd671972010-10-04 16:33:58 +02001029
Derek Jones32bf1862010-03-02 13:46:07 -06001030 foreach($this->_ci_models as $model)
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 {
Derek Jones32bf1862010-03-02 13:46:07 -06001032 $model = $this->_ci_get_component($model);
1033 $model->_assign_libraries();
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 }
Barry Mienydd671972010-10-04 16:33:58 +02001035 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001036
1037 // --------------------------------------------------------------------
1038
1039 /**
1040 * Object to Array
1041 *
1042 * Takes an object as input and converts the class variables to array key/vals
1043 *
1044 * @access private
1045 * @param object
1046 * @return array
1047 */
1048 function _ci_object_to_array($object)
1049 {
1050 return (is_object($object)) ? get_object_vars($object) : $object;
1051 }
1052
1053 // --------------------------------------------------------------------
1054
1055 /**
1056 * Determines whether we should use the CI instance or $this
Derek Jones32bf1862010-03-02 13:46:07 -06001057 * @PHP4
Barry Mienydd671972010-10-04 16:33:58 +02001058 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 * @access private
1060 * @return bool
1061 */
1062 function _ci_is_instance()
1063 {
Derek Jones32bf1862010-03-02 13:46:07 -06001064 if (is_php('5.0.0') == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 {
1066 return TRUE;
1067 }
Barry Mienydd671972010-10-04 16:33:58 +02001068
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 global $CI;
1070 return (is_object($CI)) ? TRUE : FALSE;
1071 }
Barry Mienydd671972010-10-04 16:33:58 +02001072
Derek Jones32bf1862010-03-02 13:46:07 -06001073 // --------------------------------------------------------------------
1074
1075 /**
1076 * Get a reference to a specific library or model
1077 *
1078 * @access private
1079 * @return bool
1080 */
1081 function &_ci_get_component($component)
1082 {
1083 if ($this->_ci_is_instance())
1084 {
1085 $CI =& get_instance();
1086 return $CI->$component;
1087 }
1088 else
1089 {
1090 return $this->$component;
1091 }
1092 }
1093
1094 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001095
Derek Jones32bf1862010-03-02 13:46:07 -06001096 /**
1097 * Prep filename
1098 *
1099 * This function preps the name of various items to make loading them more reliable.
1100 *
1101 * @access private
1102 * @param mixed
1103 * @return array
1104 */
1105 function _ci_prep_filename($filename, $extension)
1106 {
1107 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001108 {
1109 return array(strtolower(str_replace(EXT, '', str_replace($extension, '', $filename)).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001110 }
1111 else
1112 {
1113 foreach ($filename as $key => $val)
1114 {
Barry Mienydd671972010-10-04 16:33:58 +02001115 $filename[$key] = strtolower(str_replace(EXT, '', str_replace($extension, '', $val)).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001116 }
Barry Mienydd671972010-10-04 16:33:58 +02001117
Derek Jones32bf1862010-03-02 13:46:07 -06001118 return $filename;
1119 }
1120 }
Barry Mienydd671972010-10-04 16:33:58 +02001121
Derek Allard2067d1a2008-11-13 22:59:24 +00001122
1123}
1124
1125/* End of file Loader.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -06001126/* Location: ./system/core/Loader.php */