blob: 849ee731a1f0a846e9bf6d97751021c3786c54b8 [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
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
184 $CI =& get_instance();
185 $CI->db =& DB($params, $active_record);
186 $this->_ci_assign_to_models();
adminb0dd10f2006-08-25 17:25:49 +0000187 }
admin0aef2222006-10-12 18:00:22 +0000188
189 // --------------------------------------------------------------------
190
191 /**
192 * Load the Utilities Class
193 *
194 * @access public
195 * @return string
196 */
197 function dbutil()
198 {
199 if ( ! class_exists('CI_DB'))
200 {
201 $this->database();
202 }
adminb0dd10f2006-08-25 17:25:49 +0000203
admin0aef2222006-10-12 18:00:22 +0000204 $CI =& get_instance();
205
206 require_once(BASEPATH.'database/DB_utility'.EXT);
207 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT);
208 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
209
210 $CI->dbutil = new $class();
211 $CI->load->_ci_assign_to_models();
212 }
213
adminb0dd10f2006-08-25 17:25:49 +0000214 // --------------------------------------------------------------------
215
216 /**
217 * Load View
218 *
219 * This function is used to load a "view" file. It has three parameters:
220 *
221 * 1. The name of the "view" file to be included.
222 * 2. An associative array of data to be extracted for use in the view.
223 * 3. TRUE/FALSE - whether to return the data or load it. In
adminb807f322006-10-20 04:55:37 +0000224 * some cases it's advantageous to be able to return data so that
adminb0dd10f2006-08-25 17:25:49 +0000225 * a developer can process it in some way.
226 *
227 * @access public
228 * @param string
229 * @param array
230 * @param bool
231 * @return void
232 */
233 function view($view, $vars = array(), $return = FALSE)
234 {
235 return $this->_ci_load(array('view' => $view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return));
236 }
adminb0dd10f2006-08-25 17:25:49 +0000237
238 // --------------------------------------------------------------------
239
240 /**
241 * Load File
242 *
243 * This is a generic file loader
244 *
245 * @access public
246 * @param string
247 * @param bool
248 * @return string
249 */
250 function file($path, $return = FALSE)
251 {
252 return $this->_ci_load(array('path' => $path, 'return' => $return));
253 }
adminb0dd10f2006-08-25 17:25:49 +0000254
255 // --------------------------------------------------------------------
256
257 /**
258 * Set Variables
259 *
adminb807f322006-10-20 04:55:37 +0000260 * Once variables are set they become available within
adminb0dd10f2006-08-25 17:25:49 +0000261 * the controller class and its "view" files.
262 *
263 * @access public
264 * @param array
265 * @return void
266 */
267 function vars($vars = array())
268 {
269 $vars = $this->_ci_object_to_array($vars);
270
271 if (is_array($vars) AND count($vars) > 0)
272 {
273 foreach ($vars as $key => $val)
274 {
admincf493902006-10-10 07:12:31 +0000275 $this->_ci_cached_vars[$key] = $val;
adminb0dd10f2006-08-25 17:25:49 +0000276 }
277 }
278 }
adminb0dd10f2006-08-25 17:25:49 +0000279
280 // --------------------------------------------------------------------
281
282 /**
283 * Load Helper
284 *
285 * This function loads the specified helper file.
286 *
287 * @access public
288 * @param mixed
289 * @return void
290 */
291 function helper($helpers = array())
292 {
293 if ( ! is_array($helpers))
294 {
295 $helpers = array($helpers);
296 }
297
298 foreach ($helpers as $helper)
299 {
admincf493902006-10-10 07:12:31 +0000300 if (isset($this->_ci_helpers[$helper]))
adminb0dd10f2006-08-25 17:25:49 +0000301 {
302 continue;
303 }
304
305 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
306
admin480da812006-09-20 21:11:04 +0000307 if (file_exists(APPPATH.'helpers/'.$helper.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000308 {
admin480da812006-09-20 21:11:04 +0000309 include_once(APPPATH.'helpers/'.$helper.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000310 }
admin480da812006-09-20 21:11:04 +0000311 else
312 {
313 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
314 {
315 include_once(BASEPATH.'helpers/'.$helper.EXT);
316 }
317 else
318 {
319 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
320 }
321 }
adminb0dd10f2006-08-25 17:25:49 +0000322
admincf493902006-10-10 07:12:31 +0000323 $this->_ci_helpers[$helper] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000324 }
325
326 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
327 }
adminb0dd10f2006-08-25 17:25:49 +0000328
329 // --------------------------------------------------------------------
330
331 /**
332 * Load Helpers
333 *
334 * This is simply an alias to the above function in case the
335 * user has written the plural form of this function.
336 *
337 * @access public
338 * @param array
339 * @return void
340 */
341 function helpers($helpers = array())
342 {
343 $this->helper($helpers);
344 }
adminb0dd10f2006-08-25 17:25:49 +0000345
346 // --------------------------------------------------------------------
347
348 /**
349 * Load Plugin
350 *
351 * This function loads the specified plugin.
352 *
353 * @access public
354 * @param array
355 * @return void
356 */
357 function plugin($plugins = array())
358 {
359 if ( ! is_array($plugins))
360 {
361 $plugins = array($plugins);
362 }
363
364 foreach ($plugins as $plugin)
365 {
admincf493902006-10-10 07:12:31 +0000366 if (isset($this->_ci_plugins[$plugin]))
adminb0dd10f2006-08-25 17:25:49 +0000367 {
368 continue;
369 }
370
admin480da812006-09-20 21:11:04 +0000371 $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi');
adminb0dd10f2006-08-25 17:25:49 +0000372
admin480da812006-09-20 21:11:04 +0000373 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000374 {
admin480da812006-09-20 21:11:04 +0000375 include_once(APPPATH.'plugins/'.$plugin.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000376 }
admin480da812006-09-20 21:11:04 +0000377 else
378 {
379 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
380 {
381 include_once(BASEPATH.'plugins/'.$plugin.EXT);
382 }
383 else
384 {
385 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
386 }
387 }
adminb0dd10f2006-08-25 17:25:49 +0000388
admincf493902006-10-10 07:12:31 +0000389 $this->_ci_plugins[$plugin] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000390 }
391
392 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
393 }
adminfd2750b2006-10-06 17:29:12 +0000394
395 // --------------------------------------------------------------------
adminb0dd10f2006-08-25 17:25:49 +0000396
adminfd2750b2006-10-06 17:29:12 +0000397 /**
398 * Load Plugins
399 *
400 * This is simply an alias to the above function in case the
401 * user has written the plural form of this function.
402 *
403 * @access public
404 * @param array
405 * @return void
406 */
407 function plugins($plugins = array())
408 {
409 $this->plugin($plugins);
410 }
411
adminb0dd10f2006-08-25 17:25:49 +0000412 // --------------------------------------------------------------------
413
414 /**
415 * Load Script
416 *
417 * This function loads the specified include file from the
admin83b05a82006-09-25 21:06:46 +0000418 * application/scripts/ folder.
419 *
420 * NOTE: This feature has been deprecated but it will remain available
421 * for legacy users.
adminb0dd10f2006-08-25 17:25:49 +0000422 *
423 * @access public
424 * @param array
425 * @return void
426 */
427 function script($scripts = array())
428 {
429 if ( ! is_array($scripts))
430 {
431 $scripts = array($scripts);
432 }
433
434 foreach ($scripts as $script)
435 {
admincf493902006-10-10 07:12:31 +0000436 if (isset($this->_ci_scripts[$script]))
adminb0dd10f2006-08-25 17:25:49 +0000437 {
438 continue;
439 }
440
441 $script = strtolower(str_replace(EXT, '', $script));
442
443 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
444 {
445 show_error('Unable to load the requested script: scripts/'.$script.EXT);
446 }
447
448 include_once(APPPATH.'scripts/'.$script.EXT);
449
admincf493902006-10-10 07:12:31 +0000450 $this->_ci_scripts[$script] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000451 }
452
453 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
454 }
adminfd2750b2006-10-06 17:29:12 +0000455
adminb0dd10f2006-08-25 17:25:49 +0000456 // --------------------------------------------------------------------
457
458 /**
459 * Loads a language file
460 *
461 * @access public
462 * @param string
463 * @return void
464 */
465 function language($file = '', $lang = '', $return = FALSE)
466 {
admin0aef2222006-10-12 18:00:22 +0000467 $CI =& get_instance();
468 return $CI->lang->load($file, $lang, $return);
adminb0dd10f2006-08-25 17:25:49 +0000469 }
adminb0dd10f2006-08-25 17:25:49 +0000470
471 // --------------------------------------------------------------------
472
473 /**
474 * Loads a config file
475 *
476 * @access public
477 * @param string
478 * @return void
479 */
480 function config($file = '')
admincf493902006-10-10 07:12:31 +0000481 {
admin0aef2222006-10-12 18:00:22 +0000482 $CI =& get_instance();
483 $CI->config->load($file);
adminb0dd10f2006-08-25 17:25:49 +0000484 }
admin8f0a8f62006-10-07 01:17:25 +0000485
adminb0dd10f2006-08-25 17:25:49 +0000486 // --------------------------------------------------------------------
487
488 /**
admin8f0a8f62006-10-07 01:17:25 +0000489 * Scaffolding Loader
adminb0dd10f2006-08-25 17:25:49 +0000490 *
admin8f0a8f62006-10-07 01:17:25 +0000491 * This initializing function works a bit different than the
492 * others. It doesn't load the class. Instead, it simply
493 * sets a flag indicating that scaffolding is allowed to be
494 * used. The actual scaffolding function below is
495 * called by the front controller based on whether the
496 * second segment of the URL matches the "secret" scaffolding
497 * word stored in the application/config/routes.php
498 *
499 * @access public
adminb0dd10f2006-08-25 17:25:49 +0000500 * @param string
501 * @return void
admin8f0a8f62006-10-07 01:17:25 +0000502 */
503 function scaffolding($table = '')
504 {
505 if ($table === FALSE)
506 {
507 show_error('You must include the name of the table you would like access when you initialize scaffolding');
508 }
admincf493902006-10-10 07:12:31 +0000509
admin0aef2222006-10-12 18:00:22 +0000510 $CI =& get_instance();
511 $CI->_ci_scaffolding = TRUE;
512 $CI->_ci_scaff_table = $table;
adminb0dd10f2006-08-25 17:25:49 +0000513 }
admin8f0a8f62006-10-07 01:17:25 +0000514
adminb0dd10f2006-08-25 17:25:49 +0000515 // --------------------------------------------------------------------
admin8f0a8f62006-10-07 01:17:25 +0000516
adminb0dd10f2006-08-25 17:25:49 +0000517 /**
518 * Loader
519 *
admin8f0a8f62006-10-07 01:17:25 +0000520 * This function is used to load views and files.
adminb0dd10f2006-08-25 17:25:49 +0000521 *
522 * @access private
523 * @param array
524 * @return void
525 */
526 function _ci_load($data)
admin212a3fa2006-09-23 04:22:39 +0000527 {
adminb807f322006-10-20 04:55:37 +0000528 // This allows anything loaded using $this->load (views, files, etc.)
adminb0dd10f2006-08-25 17:25:49 +0000529 // to become accessible from within the Controller and Model functions.
admincf493902006-10-10 07:12:31 +0000530 // Only needed when running PHP 5
531
admin1e303982006-10-10 16:35:43 +0000532 if ($this->_ci_is_instance())
adminb0dd10f2006-08-25 17:25:49 +0000533 {
admincf493902006-10-10 07:12:31 +0000534 $CI =& get_instance();
535 foreach (get_object_vars($CI) as $key => $var)
adminb0dd10f2006-08-25 17:25:49 +0000536 {
admincf493902006-10-10 07:12:31 +0000537 if ( ! isset($this->$key))
538 {
539 $this->$key =& $CI->$key;
540 }
admine79dc712006-09-26 03:52:45 +0000541 }
542 }
admincf493902006-10-10 07:12:31 +0000543
adminb0dd10f2006-08-25 17:25:49 +0000544 // Set the default data variables
545 foreach (array('view', 'vars', 'path', 'return') as $val)
546 {
547 $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
548 }
549
550 /*
admin83b05a82006-09-25 21:06:46 +0000551 * Extract and cache variables
adminb0dd10f2006-08-25 17:25:49 +0000552 *
admin212a3fa2006-09-23 04:22:39 +0000553 * You can either set variables using the dedicated $this->load_vars()
554 * function or via the second parameter of this function. We'll merge
555 * the two types and cache them so that views that are embedded within
556 * other views can have access to these variables.
adminb0dd10f2006-08-25 17:25:49 +0000557 */
adminb0dd10f2006-08-25 17:25:49 +0000558 if (is_array($vars))
559 {
admincf493902006-10-10 07:12:31 +0000560 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $vars);
adminb0dd10f2006-08-25 17:25:49 +0000561 }
admincf493902006-10-10 07:12:31 +0000562 extract($this->_ci_cached_vars);
adminb0dd10f2006-08-25 17:25:49 +0000563
564 // Set the path to the requested file
565 if ($path == '')
566 {
567 $ext = pathinfo($view, PATHINFO_EXTENSION);
admin6ac4bea2006-09-02 17:46:15 +0000568 $file = ($ext == '') ? $view.EXT : $view;
admincf493902006-10-10 07:12:31 +0000569 $path = $this->_ci_view_path.$file;
adminb0dd10f2006-08-25 17:25:49 +0000570 }
571 else
572 {
573 $x = explode('/', $path);
574 $file = end($x);
575 }
576
577 /*
578 * Buffer the output
579 *
580 * We buffer the output for two reasons:
581 * 1. Speed. You get a significant speed boost.
582 * 2. So that the final rendered template can be
583 * post-processed by the output class. Why do we
584 * need post processing? For one thing, in order to
585 * show the elapsed page load time. Unless we
586 * can intercept the content right before it's sent to
adminb807f322006-10-20 04:55:37 +0000587 * the browser and then stop the timer it won't be accurate.
adminb0dd10f2006-08-25 17:25:49 +0000588 */
adminb0dd10f2006-08-25 17:25:49 +0000589 if ( ! file_exists($path))
590 {
591 show_error('Unable to load the requested file: '.$file);
592 }
593
594 ob_start();
admin30578ea2006-10-17 02:31:06 +0000595
596 // If the PHP installation does not support short tags we'll
597 // do a little string replacement, changing the short tags
598 // to standard PHP echo statements.
admin30578ea2006-10-17 02:31:06 +0000599
admin81a36812006-10-17 03:40:51 +0000600 if ((bool) @ini_get('short_open_tag') === FALSE)
601 {
602 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($path))).'<?php ');
admin30578ea2006-10-17 02:31:06 +0000603 }
604 else
605 {
606 include($path);
607 }
608
adminb0dd10f2006-08-25 17:25:49 +0000609 log_message('debug', 'File loaded: '.$path);
610
611 // Return the file data if requested to
612 if ($return === TRUE)
613 {
614 $buffer = ob_get_contents();
615 ob_end_clean();
616
617 return $buffer;
618 }
619
620 /*
621 * Flush the buffer... or buff the flusher?
622 *
admin212a3fa2006-09-23 04:22:39 +0000623 * In order to permit views to be nested within
adminb0dd10f2006-08-25 17:25:49 +0000624 * other views, we need to flush the content back out whenever
625 * we are beyond the first level of output buffering so that
admin34f7f2d2006-10-10 08:06:42 +0000626 * it can be seen and included properly by the first included
adminb0dd10f2006-08-25 17:25:49 +0000627 * template and any subsequent ones. Oy!
628 *
admincf493902006-10-10 07:12:31 +0000629 */
630 if (ob_get_level() > $this->_ci_ob_level + 1)
adminb0dd10f2006-08-25 17:25:49 +0000631 {
632 ob_end_flush();
633 }
634 else
635 {
admin34f7f2d2006-10-10 08:06:42 +0000636 // PHP 4 requires that we use a global
admincf493902006-10-10 07:12:31 +0000637 global $OUT;
638 $OUT->set_output(ob_get_contents());
adminb0dd10f2006-08-25 17:25:49 +0000639 ob_end_clean();
640 }
641 }
admin8f0a8f62006-10-07 01:17:25 +0000642
643 // --------------------------------------------------------------------
644
645 /**
646 * Load class
647 *
648 * This function loads the requested class.
649 *
650 * @access private
651 * @param string the item that is being loaded
652 * @param mixed any additional parameters
653 * @return void
654 */
admin5a14ea12006-10-12 20:42:55 +0000655 function _ci_load_class($class, $params = NULL)
admin8f0a8f62006-10-07 01:17:25 +0000656 {
657 // Prep the class name
admin0625e192006-10-20 22:16:54 +0000658 $class = ucfirst(strtolower(str_replace(EXT, '', $class)));
admin10c3f412006-10-08 07:21:12 +0000659
admin0625e192006-10-20 22:16:54 +0000660 // Is this a class extension request?
661 if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
662 {
663 if ( ! file_exists(BASEPATH.'libraries/'.$class.EXT))
admin8f0a8f62006-10-07 01:17:25 +0000664 {
admin0625e192006-10-20 22:16:54 +0000665 log_message('error', "Unable to load the requested class: ".$class);
666 show_error("Unable to load the requested class: ".$class);
admin8f0a8f62006-10-07 01:17:25 +0000667 }
admin0625e192006-10-20 22:16:54 +0000668
669 include_once(BASEPATH.'libraries/'.ucfirst($class).EXT);
670 include_once(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
671
admin570c1612006-10-20 22:28:39 +0000672 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params);
admin8f0a8f62006-10-07 01:17:25 +0000673 }
admin10c3f412006-10-08 07:21:12 +0000674
675 // Lets search for the requested library file and load it.
admin0625e192006-10-20 22:16:54 +0000676 for ($i = 1; $i < 3; $i++)
admin10c3f412006-10-08 07:21:12 +0000677 {
admin0625e192006-10-20 22:16:54 +0000678 $path = ($i % 2) ? APPPATH : BASEPATH;
admin570c1612006-10-20 22:28:39 +0000679 if (file_exists($path.'libraries/'.$class.EXT))
admin8f0a8f62006-10-07 01:17:25 +0000680 {
admin570c1612006-10-20 22:28:39 +0000681 include_once($path.'libraries/'.$class.EXT);
682 return $this->_ci_init_class($class, '', $params);
admin8f0a8f62006-10-07 01:17:25 +0000683 }
684 }
685
686 // If we got this far we were unable to find the requested class
687 log_message('error', "Unable to load the requested class: ".$class);
admin0625e192006-10-20 22:16:54 +0000688 show_error("Unable to load the requested class: ".$class);
admin8f0a8f62006-10-07 01:17:25 +0000689 }
690
691 // --------------------------------------------------------------------
692
693 /**
694 * Instantiates a class
695 *
696 * @access private
697 * @param string
698 * @param string
699 * @return null
700 */
admin30578ea2006-10-17 02:31:06 +0000701 function _ci_init_class($class, $prefix = '', $config = FALSE)
admin8f0a8f62006-10-07 01:17:25 +0000702 {
703 // Is there an associated config file for this class?
admin30578ea2006-10-17 02:31:06 +0000704 if ($config !== NULL)
admin9a661812006-10-16 19:02:48 +0000705 {
admin30578ea2006-10-17 02:31:06 +0000706 $cong = NULL;
admin80b2bd92006-10-12 18:30:36 +0000707 if (file_exists(APPPATH.'config/'.$class.EXT))
708 {
709 include_once(APPPATH.'config/'.$class.EXT);
710 }
admin8f0a8f62006-10-07 01:17:25 +0000711 }
712
713 if ($prefix == '')
714 {
admin10c3f412006-10-08 07:21:12 +0000715 $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
admin8f0a8f62006-10-07 01:17:25 +0000716 }
717 else
718 {
719 $name = $prefix.$class;
720 }
admin2f0bac82006-10-09 17:36:45 +0000721
adminb1fddc02006-10-09 21:29:07 +0000722 // Set the variable name we will assign the class to
admin80b2bd92006-10-12 18:30:36 +0000723 $class = strtolower($class);
724 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
725
admincf493902006-10-10 07:12:31 +0000726 // Instantiate the class
admin0aef2222006-10-12 18:00:22 +0000727 $CI =& get_instance();
728 if ($config !== NULL)
admin8f0a8f62006-10-07 01:17:25 +0000729 {
admin0aef2222006-10-12 18:00:22 +0000730 $CI->$classvar = new $name($config);
admin8f0a8f62006-10-07 01:17:25 +0000731 }
732 else
admin0aef2222006-10-12 18:00:22 +0000733 {
734 $CI->$classvar = new $name;
735 }
admin8f0a8f62006-10-07 01:17:25 +0000736 }
adminb0dd10f2006-08-25 17:25:49 +0000737
738 // --------------------------------------------------------------------
739
740 /**
741 * Autoloader
742 *
743 * The config/autoload.php file contains an array that permits sub-systems,
admin8f0a8f62006-10-07 01:17:25 +0000744 * libraries, plugins, and helpers to be loaded automatically.
adminb0dd10f2006-08-25 17:25:49 +0000745 *
746 * @access private
747 * @param array
748 * @return void
749 */
admin8f0a8f62006-10-07 01:17:25 +0000750 function _ci_autoloader()
751 {
752 include_once(APPPATH.'config/autoload'.EXT);
753
754 if ( ! isset($autoload))
755 {
756 return FALSE;
757 }
758
759 // Load any custome config file
760 if (count($autoload['config']) > 0)
admincf493902006-10-10 07:12:31 +0000761 {
admin0aef2222006-10-12 18:00:22 +0000762 $CI =& get_instance();
763 foreach ($autoload['config'] as $key => $val)
admin8f0a8f62006-10-07 01:17:25 +0000764 {
admin0aef2222006-10-12 18:00:22 +0000765 $CI->config->load($val);
admin8f0a8f62006-10-07 01:17:25 +0000766 }
767 }
768
769 // Load plugins, helpers, and scripts
770 foreach (array('helper', 'plugin', 'script') as $type)
771 {
772 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
773 {
774 $this->$type($autoload[$type]);
775 }
776 }
777
778 // A little tweak to remain backward compatible
779 // The $autoload['core'] item was deprecated
780 if ( ! isset($autoload['libraries']))
781 {
782 $autoload['libraries'] = $autoload['core'];
783 }
784
785 // Load libraries
786 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
787 {
admin10c3f412006-10-08 07:21:12 +0000788 // Load the database driver.
admin8f0a8f62006-10-07 01:17:25 +0000789 if (in_array('database', $autoload['libraries']))
790 {
791 $this->database();
792 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
793 }
794
admin10c3f412006-10-08 07:21:12 +0000795 // Load the model class.
admin8f0a8f62006-10-07 01:17:25 +0000796 if (in_array('model', $autoload['libraries']))
797 {
798 $this->model();
799 $autoload['libraries'] = array_diff($autoload['libraries'], array('model'));
800 }
admin10c3f412006-10-08 07:21:12 +0000801
802 // Load scaffolding
admin8f0a8f62006-10-07 01:17:25 +0000803 if (in_array('scaffolding', $autoload['libraries']))
804 {
805 $this->scaffolding();
806 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
807 }
808
admin10c3f412006-10-08 07:21:12 +0000809 // Load all other libraries
admin8f0a8f62006-10-07 01:17:25 +0000810 foreach ($autoload['libraries'] as $item)
811 {
812 $this->library($item);
813 }
814 }
815 }
816
817 // --------------------------------------------------------------------
818
819 /**
820 * Assign to Models
821 *
822 * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
adminbd6bee72006-10-21 19:39:00 +0000823 * will be available to models, if any exist.
admin8f0a8f62006-10-07 01:17:25 +0000824 *
admin34f7f2d2006-10-10 08:06:42 +0000825 * @access private
admin8f0a8f62006-10-07 01:17:25 +0000826 * @param object
827 * @return array
828 */
829 function _ci_assign_to_models()
adminb0dd10f2006-08-25 17:25:49 +0000830 {
admincf493902006-10-10 07:12:31 +0000831 if (count($this->_ci_models) == 0)
adminb0dd10f2006-08-25 17:25:49 +0000832 {
833 return;
834 }
admincf493902006-10-10 07:12:31 +0000835
admin1e303982006-10-10 16:35:43 +0000836 if ($this->_ci_is_instance())
admincf493902006-10-10 07:12:31 +0000837 {
838 $CI =& get_instance();
839 foreach ($this->_ci_models as $model)
840 {
admin0aef2222006-10-12 18:00:22 +0000841 $CI->$model->_assign_libraries();
admincf493902006-10-10 07:12:31 +0000842 }
843 }
844 else
admin0aef2222006-10-12 18:00:22 +0000845 {
admincf493902006-10-10 07:12:31 +0000846 foreach ($this->_ci_models as $model)
847 {
admin0aef2222006-10-12 18:00:22 +0000848 $this->$model->_assign_libraries();
admincf493902006-10-10 07:12:31 +0000849 }
850 }
admin8f0a8f62006-10-07 01:17:25 +0000851 }
adminb0dd10f2006-08-25 17:25:49 +0000852
853 // --------------------------------------------------------------------
854
855 /**
856 * Object to Array
857 *
adminbd6bee72006-10-21 19:39:00 +0000858 * Takes an object as input and converts the class variables to array key/vals
adminb0dd10f2006-08-25 17:25:49 +0000859 *
admin34f7f2d2006-10-10 08:06:42 +0000860 * @access private
adminb0dd10f2006-08-25 17:25:49 +0000861 * @param object
862 * @return array
863 */
864 function _ci_object_to_array($object)
865 {
admin2e5872a2006-09-06 02:32:55 +0000866 return (is_object($object)) ? get_object_vars($object) : $object;
adminb0dd10f2006-08-25 17:25:49 +0000867 }
admin8f0a8f62006-10-07 01:17:25 +0000868
admin34f7f2d2006-10-10 08:06:42 +0000869 // --------------------------------------------------------------------
870
871 /**
872 * Determines whether we should use the CI instance or $this
873 *
874 * @access private
875 * @return bool
876 */
admin1e303982006-10-10 16:35:43 +0000877 function _ci_is_instance()
admin34f7f2d2006-10-10 08:06:42 +0000878 {
879 if ($this->_ci_is_php5 == TRUE)
880 {
881 return TRUE;
882 }
883
884 global $CI;
885 return (is_object($CI)) ? TRUE : FALSE;
886 }
adminb0dd10f2006-08-25 17:25:49 +0000887
888}
adminb0dd10f2006-08-25 17:25:49 +0000889?>