blob: f243a287991dd66fa76c06a7c67c2faaea6eb164 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
17
18/**
19 * Loader Class
admine334c472006-10-21 19:44:22 +000020 *
adminb0dd10f2006-08-25 17:25:49 +000021 * Loads views and files
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @author Rick Ellis
26 * @category Loader
27 * @link http://www.codeigniter.com/user_guide/libraries/loader.html
28 */
29class CI_Loader {
30
admin34f7f2d2006-10-10 08:06:42 +000031 // All these are set automatically. Don't mess with them.
admincf493902006-10-10 07:12:31 +000032 var $_ci_ob_level;
33 var $_ci_view_path = '';
admin34f7f2d2006-10-10 08:06:42 +000034 var $_ci_is_php5 = FALSE;
admin1e303982006-10-10 16:35:43 +000035 var $_ci_is_instance = FALSE; // Whether we should use $this or $CI =& get_instance()
admincf493902006-10-10 07:12:31 +000036 var $_ci_cached_vars = array();
37 var $_ci_models = array();
38 var $_ci_helpers = array();
39 var $_ci_plugins = array();
40 var $_ci_scripts = array();
41 var $_ci_varmap = array('unit_test' => 'unit', 'user_agent' => 'agent');
42
adminb0dd10f2006-08-25 17:25:49 +000043
44 /**
45 * Constructor
46 *
admin34f7f2d2006-10-10 08:06:42 +000047 * Sets the path to the view files and gets the initial output buffering level
adminb0dd10f2006-08-25 17:25:49 +000048 *
49 * @access public
50 */
51 function CI_Loader()
admincf493902006-10-10 07:12:31 +000052 {
admin34f7f2d2006-10-10 08:06:42 +000053 $this->_ci_is_php5 = (floor(phpversion()) >= 5) ? TRUE : FALSE;
admincf493902006-10-10 07:12:31 +000054 $this->_ci_view_path = APPPATH.'views/';
55 $this->_ci_ob_level = ob_get_level();
adminb0dd10f2006-08-25 17:25:49 +000056
57 log_message('debug', "Loader Class Initialized");
58 }
adminb0dd10f2006-08-25 17:25:49 +000059
60 // --------------------------------------------------------------------
61
62 /**
63 * Class Loader
64 *
65 * This function lets users load and instantiate classes.
66 * It is designed to be called from a user's app controllers.
67 *
68 * @access public
69 * @param string the name of the class
admin80b2bd92006-10-12 18:30:36 +000070 * @param mixed the optional parameters
adminb0dd10f2006-08-25 17:25:49 +000071 * @return void
72 */
admin5a14ea12006-10-12 20:42:55 +000073 function library($class, $params = NULL)
adminb0dd10f2006-08-25 17:25:49 +000074 {
75 if ($class == '')
76 return;
77
admin80b2bd92006-10-12 18:30:36 +000078 $this->_ci_load_class($class, $params);
admin8f0a8f62006-10-07 01:17:25 +000079 $this->_ci_assign_to_models();
adminb0dd10f2006-08-25 17:25:49 +000080 }
adminb0dd10f2006-08-25 17:25:49 +000081
82 // --------------------------------------------------------------------
83
84 /**
85 * Model Loader
86 *
87 * This function lets users load and instantiate models.
88 *
89 * @access public
90 * @param string the name of the class
91 * @param mixed any initialization parameters
92 * @return void
93 */
94 function model($model, $name = '', $db_conn = FALSE)
95 {
96 if ($model == '')
97 return;
98
admincf493902006-10-10 07:12:31 +000099 // Is the model in a sub-folder? If so, parse out the filename and path.
admin8f0a8f62006-10-07 01:17:25 +0000100 if (strpos($model, '/') === FALSE)
101 {
102 $path = '';
103 }
104 else
105 {
106 $x = explode('/', $model);
107 $model = end($x);
108 unset($x[count($x)-1]);
109 $path = implode('/', $x).'/';
110 }
111
112 if ($name == '')
113 {
114 $name = $model;
115 }
116
admincf493902006-10-10 07:12:31 +0000117 if (in_array($name, $this->_ci_models, TRUE))
admin8f0a8f62006-10-07 01:17:25 +0000118 {
119 return;
admincf493902006-10-10 07:12:31 +0000120 }
admin8f0a8f62006-10-07 01:17:25 +0000121
admin0aef2222006-10-12 18:00:22 +0000122 $CI =& get_instance();
123 if (isset($CI->$name))
admin8f0a8f62006-10-07 01:17:25 +0000124 {
admin0aef2222006-10-12 18:00:22 +0000125 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
admin8f0a8f62006-10-07 01:17:25 +0000126 }
127
128 $model = strtolower($model);
129
130 if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
131 {
132 show_error('Unable to locate the model you have specified: '.$model);
133 }
134
135 if ($db_conn !== FALSE)
136 {
137 if ($db_conn === TRUE)
138 $db_conn = '';
139
admin0aef2222006-10-12 18:00:22 +0000140 $CI->load->database($db_conn, FALSE, TRUE);
admin8f0a8f62006-10-07 01:17:25 +0000141 }
142
143 if ( ! class_exists('Model'))
144 {
145 require_once(BASEPATH.'libraries/Model'.EXT);
146 }
147
148 require_once(APPPATH.'models/'.$path.$model.EXT);
149
150 $model = ucfirst($model);
admin34f7f2d2006-10-10 08:06:42 +0000151
admin0aef2222006-10-12 18:00:22 +0000152 $CI->$name = new $model();
153 $CI->$name->_assign_libraries();
admincf493902006-10-10 07:12:31 +0000154
admin0aef2222006-10-12 18:00:22 +0000155 $this->_ci_models[] = $name;
adminb0dd10f2006-08-25 17:25:49 +0000156 }
admin0aef2222006-10-12 18:00:22 +0000157
adminb0dd10f2006-08-25 17:25:49 +0000158 // --------------------------------------------------------------------
159
160 /**
161 * Database Loader
162 *
163 * @access public
164 * @param string the DB credentials
165 * @param bool whether to return the DB object
166 * @param bool whether to enable active record (this allows us to override the config setting)
admina5e812c2006-09-25 02:17:30 +0000167 * @return object
adminb0dd10f2006-08-25 17:25:49 +0000168 */
admin8f0a8f62006-10-07 01:17:25 +0000169 function database($params = '', $return = FALSE, $active_record = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000170 {
admin0aef2222006-10-12 18:00:22 +0000171 // Do we even need to load the database class?
172 if (class_exists('CI_DB') AND $return == FALSE AND $active_record == FALSE)
173 {
174 return FALSE;
175 }
176
admin8f0a8f62006-10-07 01:17:25 +0000177 require_once(BASEPATH.'database/DB'.EXT);
adminfd2750b2006-10-06 17:29:12 +0000178
adminb0dd10f2006-08-25 17:25:49 +0000179 if ($return === TRUE)
180 {
admin0aef2222006-10-12 18:00:22 +0000181 return DB($params, $active_record);
adminb0dd10f2006-08-25 17:25:49 +0000182 }
admin0aef2222006-10-12 18:00:22 +0000183
admin2b47ea42006-10-24 00:15:02 +0000184 // Grab the super object
admin0aef2222006-10-12 18:00:22 +0000185 $CI =& get_instance();
admin2b47ea42006-10-24 00:15:02 +0000186
187 // Initialize the db variable. Needed to prevent
188 // reference errors with some configurations
189 $CI->db = '';
190
191 // Load the DB class
192 $CI->db =& DB($params, $active_record);
193
194 // Assign the DB object to any existing models
admin0aef2222006-10-12 18:00:22 +0000195 $this->_ci_assign_to_models();
adminb0dd10f2006-08-25 17:25:49 +0000196 }
admin0aef2222006-10-12 18:00:22 +0000197
198 // --------------------------------------------------------------------
199
200 /**
201 * Load the Utilities Class
202 *
203 * @access public
admine334c472006-10-21 19:44:22 +0000204 * @return string
admin0aef2222006-10-12 18:00:22 +0000205 */
206 function dbutil()
207 {
208 if ( ! class_exists('CI_DB'))
209 {
210 $this->database();
211 }
adminb0dd10f2006-08-25 17:25:49 +0000212
admin0aef2222006-10-12 18:00:22 +0000213 $CI =& get_instance();
214
215 require_once(BASEPATH.'database/DB_utility'.EXT);
216 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT);
217 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
218
219 $CI->dbutil = new $class();
220 $CI->load->_ci_assign_to_models();
221 }
222
adminb0dd10f2006-08-25 17:25:49 +0000223 // --------------------------------------------------------------------
224
225 /**
226 * Load View
227 *
228 * This function is used to load a "view" file. It has three parameters:
229 *
230 * 1. The name of the "view" file to be included.
231 * 2. An associative array of data to be extracted for use in the view.
232 * 3. TRUE/FALSE - whether to return the data or load it. In
adminb807f322006-10-20 04:55:37 +0000233 * some cases it's advantageous to be able to return data so that
adminb0dd10f2006-08-25 17:25:49 +0000234 * a developer can process it in some way.
235 *
236 * @access public
237 * @param string
238 * @param array
239 * @param bool
240 * @return void
241 */
242 function view($view, $vars = array(), $return = FALSE)
243 {
244 return $this->_ci_load(array('view' => $view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return));
245 }
adminb0dd10f2006-08-25 17:25:49 +0000246
247 // --------------------------------------------------------------------
248
249 /**
250 * Load File
251 *
252 * This is a generic file loader
253 *
254 * @access public
255 * @param string
256 * @param bool
257 * @return string
258 */
259 function file($path, $return = FALSE)
260 {
261 return $this->_ci_load(array('path' => $path, 'return' => $return));
262 }
adminb0dd10f2006-08-25 17:25:49 +0000263
264 // --------------------------------------------------------------------
265
266 /**
267 * Set Variables
268 *
adminb807f322006-10-20 04:55:37 +0000269 * Once variables are set they become available within
adminb0dd10f2006-08-25 17:25:49 +0000270 * the controller class and its "view" files.
271 *
272 * @access public
273 * @param array
274 * @return void
275 */
276 function vars($vars = array())
277 {
278 $vars = $this->_ci_object_to_array($vars);
279
280 if (is_array($vars) AND count($vars) > 0)
281 {
282 foreach ($vars as $key => $val)
283 {
admincf493902006-10-10 07:12:31 +0000284 $this->_ci_cached_vars[$key] = $val;
adminb0dd10f2006-08-25 17:25:49 +0000285 }
286 }
287 }
adminb0dd10f2006-08-25 17:25:49 +0000288
289 // --------------------------------------------------------------------
290
291 /**
292 * Load Helper
293 *
294 * This function loads the specified helper file.
295 *
296 * @access public
297 * @param mixed
298 * @return void
299 */
300 function helper($helpers = array())
301 {
302 if ( ! is_array($helpers))
303 {
304 $helpers = array($helpers);
305 }
306
307 foreach ($helpers as $helper)
308 {
admincf493902006-10-10 07:12:31 +0000309 if (isset($this->_ci_helpers[$helper]))
adminb0dd10f2006-08-25 17:25:49 +0000310 {
311 continue;
312 }
313
314 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
315
admin480da812006-09-20 21:11:04 +0000316 if (file_exists(APPPATH.'helpers/'.$helper.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000317 {
admin480da812006-09-20 21:11:04 +0000318 include_once(APPPATH.'helpers/'.$helper.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000319 }
admin480da812006-09-20 21:11:04 +0000320 else
321 {
322 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
323 {
324 include_once(BASEPATH.'helpers/'.$helper.EXT);
325 }
326 else
327 {
328 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
329 }
330 }
adminb0dd10f2006-08-25 17:25:49 +0000331
admincf493902006-10-10 07:12:31 +0000332 $this->_ci_helpers[$helper] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000333 }
334
335 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
336 }
adminb0dd10f2006-08-25 17:25:49 +0000337
338 // --------------------------------------------------------------------
339
340 /**
341 * Load Helpers
342 *
343 * This is simply an alias to the above function in case the
344 * user has written the plural form of this function.
345 *
346 * @access public
347 * @param array
348 * @return void
349 */
350 function helpers($helpers = array())
351 {
352 $this->helper($helpers);
353 }
adminb0dd10f2006-08-25 17:25:49 +0000354
355 // --------------------------------------------------------------------
356
357 /**
358 * Load Plugin
359 *
360 * This function loads the specified plugin.
361 *
362 * @access public
363 * @param array
364 * @return void
365 */
366 function plugin($plugins = array())
367 {
368 if ( ! is_array($plugins))
369 {
370 $plugins = array($plugins);
371 }
372
373 foreach ($plugins as $plugin)
374 {
admincf493902006-10-10 07:12:31 +0000375 if (isset($this->_ci_plugins[$plugin]))
adminb0dd10f2006-08-25 17:25:49 +0000376 {
377 continue;
378 }
379
admin480da812006-09-20 21:11:04 +0000380 $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi');
adminb0dd10f2006-08-25 17:25:49 +0000381
admin480da812006-09-20 21:11:04 +0000382 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000383 {
admin480da812006-09-20 21:11:04 +0000384 include_once(APPPATH.'plugins/'.$plugin.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000385 }
admin480da812006-09-20 21:11:04 +0000386 else
387 {
388 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
389 {
390 include_once(BASEPATH.'plugins/'.$plugin.EXT);
391 }
392 else
393 {
394 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
395 }
396 }
adminb0dd10f2006-08-25 17:25:49 +0000397
admincf493902006-10-10 07:12:31 +0000398 $this->_ci_plugins[$plugin] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000399 }
400
401 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
402 }
adminfd2750b2006-10-06 17:29:12 +0000403
404 // --------------------------------------------------------------------
adminb0dd10f2006-08-25 17:25:49 +0000405
adminfd2750b2006-10-06 17:29:12 +0000406 /**
407 * Load Plugins
408 *
409 * This is simply an alias to the above function in case the
410 * user has written the plural form of this function.
411 *
412 * @access public
413 * @param array
414 * @return void
415 */
416 function plugins($plugins = array())
417 {
418 $this->plugin($plugins);
419 }
420
adminb0dd10f2006-08-25 17:25:49 +0000421 // --------------------------------------------------------------------
422
423 /**
424 * Load Script
425 *
426 * This function loads the specified include file from the
admin83b05a82006-09-25 21:06:46 +0000427 * application/scripts/ folder.
428 *
429 * NOTE: This feature has been deprecated but it will remain available
430 * for legacy users.
adminb0dd10f2006-08-25 17:25:49 +0000431 *
432 * @access public
433 * @param array
434 * @return void
435 */
436 function script($scripts = array())
437 {
438 if ( ! is_array($scripts))
439 {
440 $scripts = array($scripts);
441 }
442
443 foreach ($scripts as $script)
444 {
admincf493902006-10-10 07:12:31 +0000445 if (isset($this->_ci_scripts[$script]))
adminb0dd10f2006-08-25 17:25:49 +0000446 {
447 continue;
448 }
449
450 $script = strtolower(str_replace(EXT, '', $script));
451
452 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
453 {
454 show_error('Unable to load the requested script: scripts/'.$script.EXT);
455 }
456
457 include_once(APPPATH.'scripts/'.$script.EXT);
458
admincf493902006-10-10 07:12:31 +0000459 $this->_ci_scripts[$script] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000460 }
461
462 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
463 }
adminfd2750b2006-10-06 17:29:12 +0000464
adminb0dd10f2006-08-25 17:25:49 +0000465 // --------------------------------------------------------------------
466
467 /**
468 * Loads a language file
469 *
470 * @access public
471 * @param string
472 * @return void
473 */
474 function language($file = '', $lang = '', $return = FALSE)
475 {
admin0aef2222006-10-12 18:00:22 +0000476 $CI =& get_instance();
477 return $CI->lang->load($file, $lang, $return);
adminb0dd10f2006-08-25 17:25:49 +0000478 }
adminb0dd10f2006-08-25 17:25:49 +0000479
480 // --------------------------------------------------------------------
481
482 /**
483 * Loads a config file
484 *
485 * @access public
486 * @param string
487 * @return void
488 */
489 function config($file = '')
admincf493902006-10-10 07:12:31 +0000490 {
admin0aef2222006-10-12 18:00:22 +0000491 $CI =& get_instance();
492 $CI->config->load($file);
adminb0dd10f2006-08-25 17:25:49 +0000493 }
admin8f0a8f62006-10-07 01:17:25 +0000494
adminb0dd10f2006-08-25 17:25:49 +0000495 // --------------------------------------------------------------------
496
497 /**
admin8f0a8f62006-10-07 01:17:25 +0000498 * Scaffolding Loader
adminb0dd10f2006-08-25 17:25:49 +0000499 *
admin8f0a8f62006-10-07 01:17:25 +0000500 * This initializing function works a bit different than the
501 * others. It doesn't load the class. Instead, it simply
502 * sets a flag indicating that scaffolding is allowed to be
503 * used. The actual scaffolding function below is
504 * called by the front controller based on whether the
505 * second segment of the URL matches the "secret" scaffolding
506 * word stored in the application/config/routes.php
507 *
508 * @access public
adminb0dd10f2006-08-25 17:25:49 +0000509 * @param string
510 * @return void
admin8f0a8f62006-10-07 01:17:25 +0000511 */
512 function scaffolding($table = '')
513 {
514 if ($table === FALSE)
515 {
516 show_error('You must include the name of the table you would like access when you initialize scaffolding');
517 }
admincf493902006-10-10 07:12:31 +0000518
admin0aef2222006-10-12 18:00:22 +0000519 $CI =& get_instance();
520 $CI->_ci_scaffolding = TRUE;
521 $CI->_ci_scaff_table = $table;
adminb0dd10f2006-08-25 17:25:49 +0000522 }
admin8f0a8f62006-10-07 01:17:25 +0000523
adminb0dd10f2006-08-25 17:25:49 +0000524 // --------------------------------------------------------------------
admin8f0a8f62006-10-07 01:17:25 +0000525
adminb0dd10f2006-08-25 17:25:49 +0000526 /**
527 * Loader
528 *
admin8f0a8f62006-10-07 01:17:25 +0000529 * This function is used to load views and files.
adminb0dd10f2006-08-25 17:25:49 +0000530 *
531 * @access private
532 * @param array
533 * @return void
534 */
535 function _ci_load($data)
admin212a3fa2006-09-23 04:22:39 +0000536 {
adminb807f322006-10-20 04:55:37 +0000537 // This allows anything loaded using $this->load (views, files, etc.)
adminb0dd10f2006-08-25 17:25:49 +0000538 // to become accessible from within the Controller and Model functions.
admincf493902006-10-10 07:12:31 +0000539 // Only needed when running PHP 5
540
admin1e303982006-10-10 16:35:43 +0000541 if ($this->_ci_is_instance())
adminb0dd10f2006-08-25 17:25:49 +0000542 {
admincf493902006-10-10 07:12:31 +0000543 $CI =& get_instance();
544 foreach (get_object_vars($CI) as $key => $var)
adminb0dd10f2006-08-25 17:25:49 +0000545 {
admincf493902006-10-10 07:12:31 +0000546 if ( ! isset($this->$key))
547 {
548 $this->$key =& $CI->$key;
549 }
admine79dc712006-09-26 03:52:45 +0000550 }
551 }
admincf493902006-10-10 07:12:31 +0000552
adminb0dd10f2006-08-25 17:25:49 +0000553 // Set the default data variables
554 foreach (array('view', 'vars', 'path', 'return') as $val)
555 {
556 $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
557 }
558
559 /*
admin83b05a82006-09-25 21:06:46 +0000560 * Extract and cache variables
adminb0dd10f2006-08-25 17:25:49 +0000561 *
admine334c472006-10-21 19:44:22 +0000562 * You can either set variables using the dedicated $this->load_vars()
563 * function or via the second parameter of this function. We'll merge
564 * the two types and cache them so that views that are embedded within
admin212a3fa2006-09-23 04:22:39 +0000565 * other views can have access to these variables.
adminb0dd10f2006-08-25 17:25:49 +0000566 */
adminb0dd10f2006-08-25 17:25:49 +0000567 if (is_array($vars))
568 {
admincf493902006-10-10 07:12:31 +0000569 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $vars);
adminb0dd10f2006-08-25 17:25:49 +0000570 }
admincf493902006-10-10 07:12:31 +0000571 extract($this->_ci_cached_vars);
adminb0dd10f2006-08-25 17:25:49 +0000572
573 // Set the path to the requested file
574 if ($path == '')
575 {
576 $ext = pathinfo($view, PATHINFO_EXTENSION);
admin6ac4bea2006-09-02 17:46:15 +0000577 $file = ($ext == '') ? $view.EXT : $view;
admincf493902006-10-10 07:12:31 +0000578 $path = $this->_ci_view_path.$file;
adminb0dd10f2006-08-25 17:25:49 +0000579 }
580 else
581 {
582 $x = explode('/', $path);
583 $file = end($x);
584 }
585
586 /*
587 * Buffer the output
588 *
589 * We buffer the output for two reasons:
590 * 1. Speed. You get a significant speed boost.
admine334c472006-10-21 19:44:22 +0000591 * 2. So that the final rendered template can be
adminb0dd10f2006-08-25 17:25:49 +0000592 * post-processed by the output class. Why do we
admine334c472006-10-21 19:44:22 +0000593 * need post processing? For one thing, in order to
adminb0dd10f2006-08-25 17:25:49 +0000594 * show the elapsed page load time. Unless we
595 * can intercept the content right before it's sent to
adminb807f322006-10-20 04:55:37 +0000596 * the browser and then stop the timer it won't be accurate.
adminb0dd10f2006-08-25 17:25:49 +0000597 */
adminb0dd10f2006-08-25 17:25:49 +0000598 if ( ! file_exists($path))
599 {
600 show_error('Unable to load the requested file: '.$file);
601 }
602
603 ob_start();
admin30578ea2006-10-17 02:31:06 +0000604
605 // If the PHP installation does not support short tags we'll
606 // do a little string replacement, changing the short tags
607 // to standard PHP echo statements.
admin30578ea2006-10-17 02:31:06 +0000608
admin81a36812006-10-17 03:40:51 +0000609 if ((bool) @ini_get('short_open_tag') === FALSE)
610 {
611 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($path))).'<?php ');
admin30578ea2006-10-17 02:31:06 +0000612 }
613 else
614 {
615 include($path);
616 }
617
adminb0dd10f2006-08-25 17:25:49 +0000618 log_message('debug', 'File loaded: '.$path);
619
620 // Return the file data if requested to
621 if ($return === TRUE)
622 {
623 $buffer = ob_get_contents();
admine334c472006-10-21 19:44:22 +0000624 ob_end_clean();
adminb0dd10f2006-08-25 17:25:49 +0000625
626 return $buffer;
627 }
628
629 /*
630 * Flush the buffer... or buff the flusher?
631 *
admin212a3fa2006-09-23 04:22:39 +0000632 * In order to permit views to be nested within
admine334c472006-10-21 19:44:22 +0000633 * other views, we need to flush the content back out whenever
634 * we are beyond the first level of output buffering so that
635 * it can be seen and included properly by the first included
adminb0dd10f2006-08-25 17:25:49 +0000636 * template and any subsequent ones. Oy!
637 *
admincf493902006-10-10 07:12:31 +0000638 */
639 if (ob_get_level() > $this->_ci_ob_level + 1)
adminb0dd10f2006-08-25 17:25:49 +0000640 {
641 ob_end_flush();
642 }
643 else
644 {
admin34f7f2d2006-10-10 08:06:42 +0000645 // PHP 4 requires that we use a global
admincf493902006-10-10 07:12:31 +0000646 global $OUT;
647 $OUT->set_output(ob_get_contents());
adminb0dd10f2006-08-25 17:25:49 +0000648 ob_end_clean();
649 }
650 }
admin8f0a8f62006-10-07 01:17:25 +0000651
652 // --------------------------------------------------------------------
653
654 /**
655 * Load class
656 *
657 * This function loads the requested class.
658 *
659 * @access private
660 * @param string the item that is being loaded
661 * @param mixed any additional parameters
662 * @return void
663 */
admin5a14ea12006-10-12 20:42:55 +0000664 function _ci_load_class($class, $params = NULL)
admin8f0a8f62006-10-07 01:17:25 +0000665 {
666 // Prep the class name
admin0625e192006-10-20 22:16:54 +0000667 $class = ucfirst(strtolower(str_replace(EXT, '', $class)));
admin10c3f412006-10-08 07:21:12 +0000668
admin0625e192006-10-20 22:16:54 +0000669 // Is this a class extension request?
670 if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
671 {
672 if ( ! file_exists(BASEPATH.'libraries/'.$class.EXT))
admin8f0a8f62006-10-07 01:17:25 +0000673 {
admin0625e192006-10-20 22:16:54 +0000674 log_message('error', "Unable to load the requested class: ".$class);
675 show_error("Unable to load the requested class: ".$class);
admin8f0a8f62006-10-07 01:17:25 +0000676 }
admin0625e192006-10-20 22:16:54 +0000677
678 include_once(BASEPATH.'libraries/'.ucfirst($class).EXT);
679 include_once(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
680
admin570c1612006-10-20 22:28:39 +0000681 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params);
admin8f0a8f62006-10-07 01:17:25 +0000682 }
admin10c3f412006-10-08 07:21:12 +0000683
684 // Lets search for the requested library file and load it.
admin0625e192006-10-20 22:16:54 +0000685 for ($i = 1; $i < 3; $i++)
admin10c3f412006-10-08 07:21:12 +0000686 {
admin0625e192006-10-20 22:16:54 +0000687 $path = ($i % 2) ? APPPATH : BASEPATH;
admin570c1612006-10-20 22:28:39 +0000688 if (file_exists($path.'libraries/'.$class.EXT))
admin8f0a8f62006-10-07 01:17:25 +0000689 {
admin570c1612006-10-20 22:28:39 +0000690 include_once($path.'libraries/'.$class.EXT);
691 return $this->_ci_init_class($class, '', $params);
admin8f0a8f62006-10-07 01:17:25 +0000692 }
693 }
694
695 // If we got this far we were unable to find the requested class
696 log_message('error', "Unable to load the requested class: ".$class);
admin0625e192006-10-20 22:16:54 +0000697 show_error("Unable to load the requested class: ".$class);
admin8f0a8f62006-10-07 01:17:25 +0000698 }
699
700 // --------------------------------------------------------------------
701
702 /**
703 * Instantiates a class
704 *
705 * @access private
706 * @param string
707 * @param string
708 * @return null
709 */
admin30578ea2006-10-17 02:31:06 +0000710 function _ci_init_class($class, $prefix = '', $config = FALSE)
admin8f0a8f62006-10-07 01:17:25 +0000711 {
712 // Is there an associated config file for this class?
adminca3dafb2006-10-23 01:24:11 +0000713 if ($config === NULL)
admine334c472006-10-21 19:44:22 +0000714 {
adminca3dafb2006-10-23 01:24:11 +0000715 $config = NULL;
admin80b2bd92006-10-12 18:30:36 +0000716 if (file_exists(APPPATH.'config/'.$class.EXT))
717 {
718 include_once(APPPATH.'config/'.$class.EXT);
719 }
admin8f0a8f62006-10-07 01:17:25 +0000720 }
721
722 if ($prefix == '')
723 {
admin10c3f412006-10-08 07:21:12 +0000724 $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
admin8f0a8f62006-10-07 01:17:25 +0000725 }
726 else
727 {
728 $name = $prefix.$class;
729 }
admin2f0bac82006-10-09 17:36:45 +0000730
adminb1fddc02006-10-09 21:29:07 +0000731 // Set the variable name we will assign the class to
admin80b2bd92006-10-12 18:30:36 +0000732 $class = strtolower($class);
733 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
734
admincf493902006-10-10 07:12:31 +0000735 // Instantiate the class
admin0aef2222006-10-12 18:00:22 +0000736 $CI =& get_instance();
737 if ($config !== NULL)
admin8f0a8f62006-10-07 01:17:25 +0000738 {
admin0aef2222006-10-12 18:00:22 +0000739 $CI->$classvar = new $name($config);
admin8f0a8f62006-10-07 01:17:25 +0000740 }
741 else
admin0aef2222006-10-12 18:00:22 +0000742 {
743 $CI->$classvar = new $name;
744 }
admin8f0a8f62006-10-07 01:17:25 +0000745 }
adminb0dd10f2006-08-25 17:25:49 +0000746
747 // --------------------------------------------------------------------
748
749 /**
750 * Autoloader
751 *
admine334c472006-10-21 19:44:22 +0000752 * The config/autoload.php file contains an array that permits sub-systems,
admin8f0a8f62006-10-07 01:17:25 +0000753 * libraries, plugins, and helpers to be loaded automatically.
adminb0dd10f2006-08-25 17:25:49 +0000754 *
755 * @access private
756 * @param array
757 * @return void
758 */
admin8f0a8f62006-10-07 01:17:25 +0000759 function _ci_autoloader()
760 {
761 include_once(APPPATH.'config/autoload'.EXT);
762
763 if ( ! isset($autoload))
764 {
765 return FALSE;
766 }
767
768 // Load any custome config file
769 if (count($autoload['config']) > 0)
admincf493902006-10-10 07:12:31 +0000770 {
admin0aef2222006-10-12 18:00:22 +0000771 $CI =& get_instance();
772 foreach ($autoload['config'] as $key => $val)
admin8f0a8f62006-10-07 01:17:25 +0000773 {
admin0aef2222006-10-12 18:00:22 +0000774 $CI->config->load($val);
admin8f0a8f62006-10-07 01:17:25 +0000775 }
776 }
777
778 // Load plugins, helpers, and scripts
779 foreach (array('helper', 'plugin', 'script') as $type)
780 {
781 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
782 {
783 $this->$type($autoload[$type]);
784 }
785 }
786
787 // A little tweak to remain backward compatible
788 // The $autoload['core'] item was deprecated
789 if ( ! isset($autoload['libraries']))
790 {
791 $autoload['libraries'] = $autoload['core'];
792 }
793
794 // Load libraries
795 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
796 {
admine334c472006-10-21 19:44:22 +0000797 // Load the database driver.
admin8f0a8f62006-10-07 01:17:25 +0000798 if (in_array('database', $autoload['libraries']))
799 {
800 $this->database();
801 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
802 }
803
admine334c472006-10-21 19:44:22 +0000804 // Load the model class.
admin8f0a8f62006-10-07 01:17:25 +0000805 if (in_array('model', $autoload['libraries']))
806 {
807 $this->model();
808 $autoload['libraries'] = array_diff($autoload['libraries'], array('model'));
809 }
admin10c3f412006-10-08 07:21:12 +0000810
811 // Load scaffolding
admin8f0a8f62006-10-07 01:17:25 +0000812 if (in_array('scaffolding', $autoload['libraries']))
813 {
814 $this->scaffolding();
815 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
816 }
817
admin10c3f412006-10-08 07:21:12 +0000818 // Load all other libraries
admin8f0a8f62006-10-07 01:17:25 +0000819 foreach ($autoload['libraries'] as $item)
820 {
821 $this->library($item);
822 }
823 }
824 }
825
826 // --------------------------------------------------------------------
827
828 /**
829 * Assign to Models
830 *
831 * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
adminbd6bee72006-10-21 19:39:00 +0000832 * will be available to models, if any exist.
admin8f0a8f62006-10-07 01:17:25 +0000833 *
admin34f7f2d2006-10-10 08:06:42 +0000834 * @access private
admin8f0a8f62006-10-07 01:17:25 +0000835 * @param object
836 * @return array
837 */
838 function _ci_assign_to_models()
adminb0dd10f2006-08-25 17:25:49 +0000839 {
admin2bc13852006-10-24 23:16:17 +0000840 if (count($this->_ci_models) == 0)
841 {
842 return;
843 }
844
admin1e303982006-10-10 16:35:43 +0000845 if ($this->_ci_is_instance())
admincf493902006-10-10 07:12:31 +0000846 {
847 $CI =& get_instance();
848 foreach ($this->_ci_models as $model)
849 {
admin0aef2222006-10-12 18:00:22 +0000850 $CI->$model->_assign_libraries();
admincf493902006-10-10 07:12:31 +0000851 }
852 }
853 else
admin0aef2222006-10-12 18:00:22 +0000854 {
admincf493902006-10-10 07:12:31 +0000855 foreach ($this->_ci_models as $model)
856 {
admin0aef2222006-10-12 18:00:22 +0000857 $this->$model->_assign_libraries();
admincf493902006-10-10 07:12:31 +0000858 }
859 }
admin8f0a8f62006-10-07 01:17:25 +0000860 }
adminb0dd10f2006-08-25 17:25:49 +0000861
862 // --------------------------------------------------------------------
863
864 /**
865 * Object to Array
866 *
adminbd6bee72006-10-21 19:39:00 +0000867 * Takes an object as input and converts the class variables to array key/vals
adminb0dd10f2006-08-25 17:25:49 +0000868 *
admin34f7f2d2006-10-10 08:06:42 +0000869 * @access private
adminb0dd10f2006-08-25 17:25:49 +0000870 * @param object
871 * @return array
872 */
873 function _ci_object_to_array($object)
874 {
admin2e5872a2006-09-06 02:32:55 +0000875 return (is_object($object)) ? get_object_vars($object) : $object;
adminb0dd10f2006-08-25 17:25:49 +0000876 }
admin8f0a8f62006-10-07 01:17:25 +0000877
admin34f7f2d2006-10-10 08:06:42 +0000878 // --------------------------------------------------------------------
879
880 /**
881 * Determines whether we should use the CI instance or $this
882 *
883 * @access private
884 * @return bool
885 */
admin1e303982006-10-10 16:35:43 +0000886 function _ci_is_instance()
admin34f7f2d2006-10-10 08:06:42 +0000887 {
888 if ($this->_ci_is_php5 == TRUE)
889 {
890 return TRUE;
891 }
892
893 global $CI;
894 return (is_object($CI)) ? TRUE : FALSE;
895 }
adminb0dd10f2006-08-25 17:25:49 +0000896
897}
adminb0dd10f2006-08-25 17:25:49 +0000898?>