blob: 96ec0c919899252939f55fff0521ccf292ec76db [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.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.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 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
admin2f0bac82006-10-09 17:36:45 +000070 * @param sring the optional class variable name to assign the library to
adminb0dd10f2006-08-25 17:25:49 +000071 * @return void
72 */
admin2f0bac82006-10-09 17:36:45 +000073 function library($class, $varname = NULL)
adminb0dd10f2006-08-25 17:25:49 +000074 {
75 if ($class == '')
76 return;
77
admin2f0bac82006-10-09 17:36:45 +000078 $this->_ci_load_class($class, $varname);
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
admin1e303982006-10-10 16:35:43 +0000122 if ($this->_ci_is_instance())
admin8f0a8f62006-10-07 01:17:25 +0000123 {
admincf493902006-10-10 07:12:31 +0000124 $CI =& get_instance();
125 if (isset($CI->$name))
126 {
127 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
128 }
129 }
130 else
131 {
132 if (isset($this->$name))
133 {
134 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
135 }
admin8f0a8f62006-10-07 01:17:25 +0000136 }
137
138 $model = strtolower($model);
139
140 if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
141 {
142 show_error('Unable to locate the model you have specified: '.$model);
143 }
144
145 if ($db_conn !== FALSE)
146 {
147 if ($db_conn === TRUE)
148 $db_conn = '';
149
admin1e303982006-10-10 16:35:43 +0000150 if ($this->_ci_is_instance())
admincf493902006-10-10 07:12:31 +0000151 {
152 $CI->load->database($db_conn, FALSE, TRUE);
153 }
154 else
155 {
156 $this->database($db_conn, FALSE, TRUE);
157 }
admin8f0a8f62006-10-07 01:17:25 +0000158 }
159
160 if ( ! class_exists('Model'))
161 {
162 require_once(BASEPATH.'libraries/Model'.EXT);
163 }
164
165 require_once(APPPATH.'models/'.$path.$model.EXT);
166
167 $model = ucfirst($model);
admin34f7f2d2006-10-10 08:06:42 +0000168
admin1e303982006-10-10 16:35:43 +0000169 if ($this->_ci_is_instance())
admincf493902006-10-10 07:12:31 +0000170 {
admin34f7f2d2006-10-10 08:06:42 +0000171 $CI->$name = new $model();
172 foreach (get_object_vars($CI) as $key => $var)
173 {
174 if ( ! isset($CI->$name->$key))
175 {
176 $CI->$name->$key =& $CI->$key;
177 }
178 }
admincf493902006-10-10 07:12:31 +0000179 }
180 else
181 {
182 $this->$name = new $model();
admin34f7f2d2006-10-10 08:06:42 +0000183 foreach (get_object_vars($this) as $key => $var)
184 {
185 if ( ! isset($this->$name->$key))
186 {
187 $this->$name->$key =& $CI->$key;
188 }
189 }
190 }
admincf493902006-10-10 07:12:31 +0000191
192 $this->_ci_models[] = $name;
adminb0dd10f2006-08-25 17:25:49 +0000193 }
adminb0dd10f2006-08-25 17:25:49 +0000194
admin34f7f2d2006-10-10 08:06:42 +0000195
adminb0dd10f2006-08-25 17:25:49 +0000196 // --------------------------------------------------------------------
197
198 /**
199 * Database Loader
200 *
201 * @access public
202 * @param string the DB credentials
203 * @param bool whether to return the DB object
204 * @param bool whether to enable active record (this allows us to override the config setting)
admina5e812c2006-09-25 02:17:30 +0000205 * @return object
adminb0dd10f2006-08-25 17:25:49 +0000206 */
admin8f0a8f62006-10-07 01:17:25 +0000207 function database($params = '', $return = FALSE, $active_record = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000208 {
admin8f0a8f62006-10-07 01:17:25 +0000209 require_once(BASEPATH.'database/DB'.EXT);
adminfd2750b2006-10-06 17:29:12 +0000210
adminb0dd10f2006-08-25 17:25:49 +0000211 if ($return === TRUE)
212 {
admin8f0a8f62006-10-07 01:17:25 +0000213 return DB($params, $return, $active_record);
adminb0dd10f2006-08-25 17:25:49 +0000214 }
215 else
216 {
admin8f0a8f62006-10-07 01:17:25 +0000217 DB($params, $return, $active_record);
218 $this->_ci_assign_to_models();
adminb0dd10f2006-08-25 17:25:49 +0000219 }
220 }
adminb0dd10f2006-08-25 17:25:49 +0000221
adminb0dd10f2006-08-25 17:25:49 +0000222 // --------------------------------------------------------------------
223
224 /**
225 * Load View
226 *
227 * This function is used to load a "view" file. It has three parameters:
228 *
229 * 1. The name of the "view" file to be included.
230 * 2. An associative array of data to be extracted for use in the view.
231 * 3. TRUE/FALSE - whether to return the data or load it. In
232 * some cases it's advantageous to be able to retun data so that
233 * a developer can process it in some way.
234 *
235 * @access public
236 * @param string
237 * @param array
238 * @param bool
239 * @return void
240 */
241 function view($view, $vars = array(), $return = FALSE)
242 {
243 return $this->_ci_load(array('view' => $view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return));
244 }
adminb0dd10f2006-08-25 17:25:49 +0000245
246 // --------------------------------------------------------------------
247
248 /**
249 * Load File
250 *
251 * This is a generic file loader
252 *
253 * @access public
254 * @param string
255 * @param bool
256 * @return string
257 */
258 function file($path, $return = FALSE)
259 {
260 return $this->_ci_load(array('path' => $path, 'return' => $return));
261 }
adminb0dd10f2006-08-25 17:25:49 +0000262
263 // --------------------------------------------------------------------
264
265 /**
266 * Set Variables
267 *
268 * Once variables are set they become availabe within
269 * the controller class and its "view" files.
270 *
271 * @access public
272 * @param array
273 * @return void
274 */
275 function vars($vars = array())
276 {
277 $vars = $this->_ci_object_to_array($vars);
278
279 if (is_array($vars) AND count($vars) > 0)
280 {
281 foreach ($vars as $key => $val)
282 {
admincf493902006-10-10 07:12:31 +0000283 $this->_ci_cached_vars[$key] = $val;
adminb0dd10f2006-08-25 17:25:49 +0000284 }
285 }
286 }
adminb0dd10f2006-08-25 17:25:49 +0000287
288 // --------------------------------------------------------------------
289
290 /**
291 * Load Helper
292 *
293 * This function loads the specified helper file.
294 *
295 * @access public
296 * @param mixed
297 * @return void
298 */
299 function helper($helpers = array())
300 {
301 if ( ! is_array($helpers))
302 {
303 $helpers = array($helpers);
304 }
305
306 foreach ($helpers as $helper)
307 {
admincf493902006-10-10 07:12:31 +0000308 if (isset($this->_ci_helpers[$helper]))
adminb0dd10f2006-08-25 17:25:49 +0000309 {
310 continue;
311 }
312
313 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
314
admin480da812006-09-20 21:11:04 +0000315 if (file_exists(APPPATH.'helpers/'.$helper.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000316 {
admin480da812006-09-20 21:11:04 +0000317 include_once(APPPATH.'helpers/'.$helper.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000318 }
admin480da812006-09-20 21:11:04 +0000319 else
320 {
321 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
322 {
323 include_once(BASEPATH.'helpers/'.$helper.EXT);
324 }
325 else
326 {
327 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
328 }
329 }
adminb0dd10f2006-08-25 17:25:49 +0000330
admincf493902006-10-10 07:12:31 +0000331 $this->_ci_helpers[$helper] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000332 }
333
334 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
335 }
adminb0dd10f2006-08-25 17:25:49 +0000336
337 // --------------------------------------------------------------------
338
339 /**
340 * Load Helpers
341 *
342 * This is simply an alias to the above function in case the
343 * user has written the plural form of this function.
344 *
345 * @access public
346 * @param array
347 * @return void
348 */
349 function helpers($helpers = array())
350 {
351 $this->helper($helpers);
352 }
adminb0dd10f2006-08-25 17:25:49 +0000353
354 // --------------------------------------------------------------------
355
356 /**
357 * Load Plugin
358 *
359 * This function loads the specified plugin.
360 *
361 * @access public
362 * @param array
363 * @return void
364 */
365 function plugin($plugins = array())
366 {
367 if ( ! is_array($plugins))
368 {
369 $plugins = array($plugins);
370 }
371
372 foreach ($plugins as $plugin)
373 {
admincf493902006-10-10 07:12:31 +0000374 if (isset($this->_ci_plugins[$plugin]))
adminb0dd10f2006-08-25 17:25:49 +0000375 {
376 continue;
377 }
378
admin480da812006-09-20 21:11:04 +0000379 $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi');
adminb0dd10f2006-08-25 17:25:49 +0000380
admin480da812006-09-20 21:11:04 +0000381 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000382 {
admin480da812006-09-20 21:11:04 +0000383 include_once(APPPATH.'plugins/'.$plugin.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000384 }
admin480da812006-09-20 21:11:04 +0000385 else
386 {
387 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
388 {
389 include_once(BASEPATH.'plugins/'.$plugin.EXT);
390 }
391 else
392 {
393 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
394 }
395 }
adminb0dd10f2006-08-25 17:25:49 +0000396
admincf493902006-10-10 07:12:31 +0000397 $this->_ci_plugins[$plugin] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000398 }
399
400 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
401 }
adminfd2750b2006-10-06 17:29:12 +0000402
403 // --------------------------------------------------------------------
adminb0dd10f2006-08-25 17:25:49 +0000404
adminfd2750b2006-10-06 17:29:12 +0000405 /**
406 * Load Plugins
407 *
408 * This is simply an alias to the above function in case the
409 * user has written the plural form of this function.
410 *
411 * @access public
412 * @param array
413 * @return void
414 */
415 function plugins($plugins = array())
416 {
417 $this->plugin($plugins);
418 }
419
adminb0dd10f2006-08-25 17:25:49 +0000420 // --------------------------------------------------------------------
421
422 /**
423 * Load Script
424 *
425 * This function loads the specified include file from the
admin83b05a82006-09-25 21:06:46 +0000426 * application/scripts/ folder.
427 *
428 * NOTE: This feature has been deprecated but it will remain available
429 * for legacy users.
adminb0dd10f2006-08-25 17:25:49 +0000430 *
431 * @access public
432 * @param array
433 * @return void
434 */
435 function script($scripts = array())
436 {
437 if ( ! is_array($scripts))
438 {
439 $scripts = array($scripts);
440 }
441
442 foreach ($scripts as $script)
443 {
admincf493902006-10-10 07:12:31 +0000444 if (isset($this->_ci_scripts[$script]))
adminb0dd10f2006-08-25 17:25:49 +0000445 {
446 continue;
447 }
448
449 $script = strtolower(str_replace(EXT, '', $script));
450
451 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
452 {
453 show_error('Unable to load the requested script: scripts/'.$script.EXT);
454 }
455
456 include_once(APPPATH.'scripts/'.$script.EXT);
457
admincf493902006-10-10 07:12:31 +0000458 $this->_ci_scripts[$script] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000459 }
460
461 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
462 }
adminfd2750b2006-10-06 17:29:12 +0000463
adminb0dd10f2006-08-25 17:25:49 +0000464 // --------------------------------------------------------------------
465
466 /**
467 * Loads a language file
468 *
469 * @access public
470 * @param string
471 * @return void
472 */
473 function language($file = '', $lang = '', $return = FALSE)
474 {
admin1e303982006-10-10 16:35:43 +0000475 if ($this->_ci_is_instance())
admincf493902006-10-10 07:12:31 +0000476 {
477 $CI =& get_instance();
478 return $CI->lang->load($file, $lang, $return);
479 }
480 else
481 {
482 return $this->lang->load($file, $lang, $return);
483 }
adminb0dd10f2006-08-25 17:25:49 +0000484 }
adminb0dd10f2006-08-25 17:25:49 +0000485
486 // --------------------------------------------------------------------
487
488 /**
489 * Loads a config file
490 *
491 * @access public
492 * @param string
493 * @return void
494 */
495 function config($file = '')
admincf493902006-10-10 07:12:31 +0000496 {
admin1e303982006-10-10 16:35:43 +0000497 if ($this->_ci_is_instance())
admincf493902006-10-10 07:12:31 +0000498 {
499 $CI =& get_instance();
500 $CI->config->load($file);
501 }
502 else
503 {
504 $this->config->load($file);
505 }
adminb0dd10f2006-08-25 17:25:49 +0000506 }
admin8f0a8f62006-10-07 01:17:25 +0000507
adminb0dd10f2006-08-25 17:25:49 +0000508 // --------------------------------------------------------------------
509
510 /**
admin8f0a8f62006-10-07 01:17:25 +0000511 * Scaffolding Loader
adminb0dd10f2006-08-25 17:25:49 +0000512 *
admin8f0a8f62006-10-07 01:17:25 +0000513 * This initializing function works a bit different than the
514 * others. It doesn't load the class. Instead, it simply
515 * sets a flag indicating that scaffolding is allowed to be
516 * used. The actual scaffolding function below is
517 * called by the front controller based on whether the
518 * second segment of the URL matches the "secret" scaffolding
519 * word stored in the application/config/routes.php
520 *
521 * @access public
adminb0dd10f2006-08-25 17:25:49 +0000522 * @param string
523 * @return void
admin8f0a8f62006-10-07 01:17:25 +0000524 */
525 function scaffolding($table = '')
526 {
527 if ($table === FALSE)
528 {
529 show_error('You must include the name of the table you would like access when you initialize scaffolding');
530 }
admincf493902006-10-10 07:12:31 +0000531
admin1e303982006-10-10 16:35:43 +0000532 if ($this->_ci_is_instance())
admincf493902006-10-10 07:12:31 +0000533 {
534 $CI =& get_instance();
535 $CI->_ci_scaffolding = TRUE;
536 $CI->_ci_scaff_table = $table;
537 }
538 else
539 {
540 $this->_ci_scaffolding = TRUE;
541 $this->_ci_scaff_table = $table;
542 }
adminb0dd10f2006-08-25 17:25:49 +0000543 }
admin8f0a8f62006-10-07 01:17:25 +0000544
adminb0dd10f2006-08-25 17:25:49 +0000545 // --------------------------------------------------------------------
admin8f0a8f62006-10-07 01:17:25 +0000546
adminb0dd10f2006-08-25 17:25:49 +0000547 /**
548 * Loader
549 *
admin8f0a8f62006-10-07 01:17:25 +0000550 * This function is used to load views and files.
adminb0dd10f2006-08-25 17:25:49 +0000551 *
552 * @access private
553 * @param array
554 * @return void
555 */
556 function _ci_load($data)
admin212a3fa2006-09-23 04:22:39 +0000557 {
adminb0dd10f2006-08-25 17:25:49 +0000558 // This allows anything loaded using $this->load (viwes, files, etc.)
559 // to become accessible from within the Controller and Model functions.
admincf493902006-10-10 07:12:31 +0000560 // Only needed when running PHP 5
561
admin1e303982006-10-10 16:35:43 +0000562 if ($this->_ci_is_instance())
adminb0dd10f2006-08-25 17:25:49 +0000563 {
admincf493902006-10-10 07:12:31 +0000564 $CI =& get_instance();
565 foreach (get_object_vars($CI) as $key => $var)
adminb0dd10f2006-08-25 17:25:49 +0000566 {
admincf493902006-10-10 07:12:31 +0000567 if ( ! isset($this->$key))
568 {
569 $this->$key =& $CI->$key;
570 }
admine79dc712006-09-26 03:52:45 +0000571 }
572 }
admincf493902006-10-10 07:12:31 +0000573
adminb0dd10f2006-08-25 17:25:49 +0000574 // Set the default data variables
575 foreach (array('view', 'vars', 'path', 'return') as $val)
576 {
577 $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
578 }
579
580 /*
admin83b05a82006-09-25 21:06:46 +0000581 * Extract and cache variables
adminb0dd10f2006-08-25 17:25:49 +0000582 *
admin212a3fa2006-09-23 04:22:39 +0000583 * You can either set variables using the dedicated $this->load_vars()
584 * function or via the second parameter of this function. We'll merge
585 * the two types and cache them so that views that are embedded within
586 * other views can have access to these variables.
adminb0dd10f2006-08-25 17:25:49 +0000587 */
adminb0dd10f2006-08-25 17:25:49 +0000588 if (is_array($vars))
589 {
admincf493902006-10-10 07:12:31 +0000590 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $vars);
adminb0dd10f2006-08-25 17:25:49 +0000591 }
admincf493902006-10-10 07:12:31 +0000592 extract($this->_ci_cached_vars);
adminb0dd10f2006-08-25 17:25:49 +0000593
594 // Set the path to the requested file
595 if ($path == '')
596 {
597 $ext = pathinfo($view, PATHINFO_EXTENSION);
admin6ac4bea2006-09-02 17:46:15 +0000598 $file = ($ext == '') ? $view.EXT : $view;
admincf493902006-10-10 07:12:31 +0000599 $path = $this->_ci_view_path.$file;
adminb0dd10f2006-08-25 17:25:49 +0000600 }
601 else
602 {
603 $x = explode('/', $path);
604 $file = end($x);
605 }
606
607 /*
608 * Buffer the output
609 *
610 * We buffer the output for two reasons:
611 * 1. Speed. You get a significant speed boost.
612 * 2. So that the final rendered template can be
613 * post-processed by the output class. Why do we
614 * need post processing? For one thing, in order to
615 * show the elapsed page load time. Unless we
616 * can intercept the content right before it's sent to
admin212a3fa2006-09-23 04:22:39 +0000617 * the browser and then stop the timer it won't be acurate.
adminb0dd10f2006-08-25 17:25:49 +0000618 */
adminb0dd10f2006-08-25 17:25:49 +0000619 if ( ! file_exists($path))
620 {
621 show_error('Unable to load the requested file: '.$file);
622 }
623
624 ob_start();
625
626 include($path);
627 log_message('debug', 'File loaded: '.$path);
628
629 // Return the file data if requested to
630 if ($return === TRUE)
631 {
632 $buffer = ob_get_contents();
633 ob_end_clean();
634
635 return $buffer;
636 }
637
638 /*
639 * Flush the buffer... or buff the flusher?
640 *
admin212a3fa2006-09-23 04:22:39 +0000641 * In order to permit views to be nested within
adminb0dd10f2006-08-25 17:25:49 +0000642 * other views, we need to flush the content back out whenever
643 * we are beyond the first level of output buffering so that
admin34f7f2d2006-10-10 08:06:42 +0000644 * it can be seen and included properly by the first included
adminb0dd10f2006-08-25 17:25:49 +0000645 * template and any subsequent ones. Oy!
646 *
admincf493902006-10-10 07:12:31 +0000647 */
648 if (ob_get_level() > $this->_ci_ob_level + 1)
adminb0dd10f2006-08-25 17:25:49 +0000649 {
650 ob_end_flush();
651 }
652 else
653 {
admin34f7f2d2006-10-10 08:06:42 +0000654 // PHP 4 requires that we use a global
admincf493902006-10-10 07:12:31 +0000655 global $OUT;
656 $OUT->set_output(ob_get_contents());
adminb0dd10f2006-08-25 17:25:49 +0000657 ob_end_clean();
658 }
659 }
admin8f0a8f62006-10-07 01:17:25 +0000660
661 // --------------------------------------------------------------------
662
663 /**
664 * Load class
665 *
666 * This function loads the requested class.
667 *
668 * @access private
669 * @param string the item that is being loaded
670 * @param mixed any additional parameters
671 * @return void
672 */
admin2f0bac82006-10-09 17:36:45 +0000673 function _ci_load_class($class, $varname = NULL)
admin8f0a8f62006-10-07 01:17:25 +0000674 {
675 // Prep the class name
676 $class = strtolower(str_replace(EXT, '', $class));
677
admin8f0a8f62006-10-07 01:17:25 +0000678 // Is this a class extension request?
679 if (substr($class, 0, 3) == 'my_')
680 {
681 $class = preg_replace("/my_(.+)/", "\\1", $class);
admin10c3f412006-10-08 07:21:12 +0000682
admin8f0a8f62006-10-07 01:17:25 +0000683 // Load the requested library from the main system/libraries folder
684 if (file_exists(BASEPATH.'libraries/'.ucfirst($class).EXT))
685 {
686 include_once(BASEPATH.'libraries/'.ucfirst($class).EXT);
687 }
688
689 // Now look for a matching library
690 foreach (array(ucfirst($class), $class) as $filename)
691 {
692 if (file_exists(APPPATH.'libraries/'.$filename.EXT))
693 {
694 include_once(APPPATH.'libraries/'.$filename.EXT);
695 }
696 }
697
admin2f0bac82006-10-09 17:36:45 +0000698 return $this->_ci_init_class($filename, 'MY_', $varname);
admin8f0a8f62006-10-07 01:17:25 +0000699 }
admin10c3f412006-10-08 07:21:12 +0000700
701 // Lets search for the requested library file and load it.
702 // For backward compatibility we'll test for filenames that are
703 // both uppercase and lower.
704 foreach (array(ucfirst($class), $class) as $filename)
705 {
706 for ($i = 1; $i < 3; $i++)
admin8f0a8f62006-10-07 01:17:25 +0000707 {
admin10c3f412006-10-08 07:21:12 +0000708 $path = ($i % 2) ? APPPATH : BASEPATH;
709
710 if (file_exists($path.'libraries/'.$filename.EXT))
admin8f0a8f62006-10-07 01:17:25 +0000711 {
admin10c3f412006-10-08 07:21:12 +0000712 include_once($path.'libraries/'.$filename.EXT);
admin2f0bac82006-10-09 17:36:45 +0000713 return $this->_ci_init_class($filename, '', $varname);
admin8f0a8f62006-10-07 01:17:25 +0000714 }
715 }
716 }
717
718 // If we got this far we were unable to find the requested class
719 log_message('error', "Unable to load the requested class: ".$class);
720 show_error("Unable to load the class: ".$class);
721 }
722
723 // --------------------------------------------------------------------
724
725 /**
726 * Instantiates a class
727 *
728 * @access private
729 * @param string
730 * @param string
731 * @return null
732 */
admin2f0bac82006-10-09 17:36:45 +0000733 function _ci_init_class($class, $prefix = '', $varname = NULL)
admin8f0a8f62006-10-07 01:17:25 +0000734 {
735 // Is there an associated config file for this class?
adminb1fddc02006-10-09 21:29:07 +0000736 $config = NULL;
admin2f0bac82006-10-09 17:36:45 +0000737 if (file_exists(APPPATH.'config/'.$class.EXT))
admin8f0a8f62006-10-07 01:17:25 +0000738 {
admin2f0bac82006-10-09 17:36:45 +0000739 include_once(APPPATH.'config/'.$class.EXT);
admin8f0a8f62006-10-07 01:17:25 +0000740 }
741
742 if ($prefix == '')
743 {
admin10c3f412006-10-08 07:21:12 +0000744 $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
admin8f0a8f62006-10-07 01:17:25 +0000745 }
746 else
747 {
748 $name = $prefix.$class;
749 }
admin2f0bac82006-10-09 17:36:45 +0000750
adminb1fddc02006-10-09 21:29:07 +0000751 // Set the variable name we will assign the class to
752 if ( ! is_null($varname))
753 {
754 $classvar = $varname;
755 }
756 else
757 {
758 $class = strtolower($class);
admincf493902006-10-10 07:12:31 +0000759 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
adminb1fddc02006-10-09 21:29:07 +0000760 }
admin8f0a8f62006-10-07 01:17:25 +0000761
admincf493902006-10-10 07:12:31 +0000762 // Instantiate the class
admin1e303982006-10-10 16:35:43 +0000763 if ($this->_ci_is_instance())
admin8f0a8f62006-10-07 01:17:25 +0000764 {
admincf493902006-10-10 07:12:31 +0000765 $CI =& get_instance();
766 if ($config !== NULL)
767 {
768 $CI->$classvar = new $name($config);
769 }
770 else
771 {
772 $CI->$classvar = new $name;
773 }
admin8f0a8f62006-10-07 01:17:25 +0000774 }
775 else
admincf493902006-10-10 07:12:31 +0000776 {
777 if ($config !== NULL)
778 {
779 $this->$classvar = new $name($config);
780 }
781 else
782 {
783 $this->$classvar = new $name;
784 }
785 }
admin8f0a8f62006-10-07 01:17:25 +0000786 }
adminb0dd10f2006-08-25 17:25:49 +0000787
788 // --------------------------------------------------------------------
789
790 /**
791 * Autoloader
792 *
793 * The config/autoload.php file contains an array that permits sub-systems,
admin8f0a8f62006-10-07 01:17:25 +0000794 * libraries, plugins, and helpers to be loaded automatically.
adminb0dd10f2006-08-25 17:25:49 +0000795 *
796 * @access private
797 * @param array
798 * @return void
799 */
admin8f0a8f62006-10-07 01:17:25 +0000800 function _ci_autoloader()
801 {
802 include_once(APPPATH.'config/autoload'.EXT);
803
804 if ( ! isset($autoload))
805 {
806 return FALSE;
807 }
808
809 // Load any custome config file
810 if (count($autoload['config']) > 0)
admincf493902006-10-10 07:12:31 +0000811 {
admin1e303982006-10-10 16:35:43 +0000812 if ($this->_ci_is_instance())
admin8f0a8f62006-10-07 01:17:25 +0000813 {
admincf493902006-10-10 07:12:31 +0000814 $CI =& get_instance();
815 foreach ($autoload['config'] as $key => $val)
816 {
817 $CI->config->load($val);
818 }
819 }
820 else
821 {
822 foreach ($autoload['config'] as $key => $val)
823 {
824 $this->config->load($val);
825 }
admin8f0a8f62006-10-07 01:17:25 +0000826 }
827 }
828
829 // Load plugins, helpers, and scripts
830 foreach (array('helper', 'plugin', 'script') as $type)
831 {
832 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
833 {
834 $this->$type($autoload[$type]);
835 }
836 }
837
838 // A little tweak to remain backward compatible
839 // The $autoload['core'] item was deprecated
840 if ( ! isset($autoload['libraries']))
841 {
842 $autoload['libraries'] = $autoload['core'];
843 }
844
845 // Load libraries
846 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
847 {
admin10c3f412006-10-08 07:21:12 +0000848 // Load the database driver.
admin8f0a8f62006-10-07 01:17:25 +0000849 if (in_array('database', $autoload['libraries']))
850 {
851 $this->database();
852 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
853 }
854
admin10c3f412006-10-08 07:21:12 +0000855 // Load the model class.
admin8f0a8f62006-10-07 01:17:25 +0000856 if (in_array('model', $autoload['libraries']))
857 {
858 $this->model();
859 $autoload['libraries'] = array_diff($autoload['libraries'], array('model'));
860 }
admin10c3f412006-10-08 07:21:12 +0000861
862 // Load scaffolding
admin8f0a8f62006-10-07 01:17:25 +0000863 if (in_array('scaffolding', $autoload['libraries']))
864 {
865 $this->scaffolding();
866 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
867 }
868
admin10c3f412006-10-08 07:21:12 +0000869 // Load all other libraries
admin8f0a8f62006-10-07 01:17:25 +0000870 foreach ($autoload['libraries'] as $item)
871 {
872 $this->library($item);
873 }
874 }
875 }
876
877 // --------------------------------------------------------------------
878
879 /**
880 * Assign to Models
881 *
882 * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
883 * will be available to modles, if any exist.
884 *
admin34f7f2d2006-10-10 08:06:42 +0000885 * @access private
admin8f0a8f62006-10-07 01:17:25 +0000886 * @param object
887 * @return array
888 */
889 function _ci_assign_to_models()
adminb0dd10f2006-08-25 17:25:49 +0000890 {
admincf493902006-10-10 07:12:31 +0000891 if (count($this->_ci_models) == 0)
adminb0dd10f2006-08-25 17:25:49 +0000892 {
893 return;
894 }
admincf493902006-10-10 07:12:31 +0000895
admin1e303982006-10-10 16:35:43 +0000896 if ($this->_ci_is_instance())
admincf493902006-10-10 07:12:31 +0000897 {
898 $CI =& get_instance();
899 foreach ($this->_ci_models as $model)
900 {
901 $CI->$model->_assign_libraries();
902 }
903 }
904 else
905 {
906 foreach ($this->_ci_models as $model)
907 {
908 $this->$model->_assign_libraries();
909 }
910 }
admin8f0a8f62006-10-07 01:17:25 +0000911 }
adminb0dd10f2006-08-25 17:25:49 +0000912
913 // --------------------------------------------------------------------
914
915 /**
916 * Object to Array
917 *
918 * Takes an object as input and convers the class variables to array key/vals
919 *
admin34f7f2d2006-10-10 08:06:42 +0000920 * @access private
adminb0dd10f2006-08-25 17:25:49 +0000921 * @param object
922 * @return array
923 */
924 function _ci_object_to_array($object)
925 {
admin2e5872a2006-09-06 02:32:55 +0000926 return (is_object($object)) ? get_object_vars($object) : $object;
adminb0dd10f2006-08-25 17:25:49 +0000927 }
admin8f0a8f62006-10-07 01:17:25 +0000928
admin34f7f2d2006-10-10 08:06:42 +0000929 // --------------------------------------------------------------------
930
931 /**
932 * Determines whether we should use the CI instance or $this
933 *
934 * @access private
935 * @return bool
936 */
admin1e303982006-10-10 16:35:43 +0000937 function _ci_is_instance()
admin34f7f2d2006-10-10 08:06:42 +0000938 {
939 if ($this->_ci_is_php5 == TRUE)
940 {
941 return TRUE;
942 }
943
944 global $CI;
945 return (is_object($CI)) ? TRUE : FALSE;
946 }
adminb0dd10f2006-08-25 17:25:49 +0000947
948}
adminb0dd10f2006-08-25 17:25:49 +0000949?>