blob: 5cf1f2ae6f3874690e41ad1104500393f57d6624 [file] [log] [blame]
Derek Allard02658572007-12-20 14:00:50 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allard02658572007-12-20 14:00:50 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allard02658572007-12-20 14:00:50 +000012 * @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
Derek Allard3d879d52008-01-18 19:41:32 +000025 * @author ExpressionEngine Dev Team
Derek Allard02658572007-12-20 14:00:50 +000026 * @category Loader
Derek Jones7a9193a2008-01-21 18:39:20 +000027 * @link http://codeigniter.com/user_guide/libraries/loader.html
Derek Allard02658572007-12-20 14:00:50 +000028 */
29class CI_Loader {
30
31 // All these are set automatically. Don't mess with them.
32 var $_ci_ob_level;
33 var $_ci_view_path = '';
34 var $_ci_is_php5 = FALSE;
35 var $_ci_is_instance = FALSE; // Whether we should use $this or $CI =& get_instance()
36 var $_ci_cached_vars = array();
37 var $_ci_classes = array();
38 var $_ci_models = array();
39 var $_ci_helpers = array();
40 var $_ci_plugins = array();
41 var $_ci_scripts = array();
42 var $_ci_varmap = array('unit_test' => 'unit', 'user_agent' => 'agent');
43
44
45 /**
46 * Constructor
47 *
48 * Sets the path to the view files and gets the initial output buffering level
49 *
50 * @access public
51 */
52 function CI_Loader()
53 {
54 $this->_ci_is_php5 = (floor(phpversion()) >= 5) ? TRUE : FALSE;
55 $this->_ci_view_path = APPPATH.'views/';
56 $this->_ci_ob_level = ob_get_level();
57
58 log_message('debug', "Loader Class Initialized");
59 }
60
61 // --------------------------------------------------------------------
62
63 /**
64 * Class Loader
65 *
66 * This function lets users load and instantiate classes.
67 * It is designed to be called from a user's app controllers.
68 *
69 * @access public
70 * @param string the name of the class
71 * @param mixed the optional parameters
72 * @return void
73 */
74 function library($library = '', $params = NULL)
75 {
76 if ($library == '')
77 {
78 return FALSE;
79 }
80
81 if (is_array($library))
82 {
83 foreach ($library as $class)
84 {
85 $this->_ci_load_class($class, $params);
86 }
87 }
88 else
89 {
90 $this->_ci_load_class($library, $params);
91 }
92
93 $this->_ci_assign_to_models();
94 }
95
96 // --------------------------------------------------------------------
97
98 /**
99 * Model Loader
100 *
101 * This function lets users load and instantiate models.
102 *
103 * @access public
104 * @param string the name of the class
105 * @param mixed any initialization parameters
106 * @return void
107 */
108 function model($model, $name = '', $db_conn = FALSE)
109 {
110 if (is_array($model))
111 {
112 foreach($model as $babe)
113 {
114 $this->model($babe);
115 }
116 return;
117 }
118
119 if ($model == '')
120 {
121 return;
122 }
123
124 // Is the model in a sub-folder? If so, parse out the filename and path.
125 if (strpos($model, '/') === FALSE)
126 {
127 $path = '';
128 }
129 else
130 {
131 $x = explode('/', $model);
132 $model = end($x);
133 unset($x[count($x)-1]);
134 $path = implode('/', $x).'/';
135 }
136
137 if ($name == '')
138 {
139 $name = $model;
140 }
141
142 if (in_array($name, $this->_ci_models, TRUE))
143 {
144 return;
145 }
146
147 $CI =& get_instance();
148 if (isset($CI->$name))
149 {
150 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
151 }
152
153 $model = strtolower($model);
154
Derek Allard73274992008-05-05 16:39:18 +0000155 if (! file_exists(APPPATH.'models/'.$path.$model.EXT))
Derek Allard02658572007-12-20 14:00:50 +0000156 {
157 show_error('Unable to locate the model you have specified: '.$model);
158 }
159
160 if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
161 {
162 if ($db_conn === TRUE)
163 $db_conn = '';
164
165 $CI->load->database($db_conn, FALSE, TRUE);
166 }
167
Derek Allard73274992008-05-05 16:39:18 +0000168 if (! class_exists('Model'))
Derek Allard02658572007-12-20 14:00:50 +0000169 {
Derek Allardff943eb2008-01-22 18:08:10 +0000170 load_class('Model', FALSE);
Derek Allard02658572007-12-20 14:00:50 +0000171 }
172
173 require_once(APPPATH.'models/'.$path.$model.EXT);
174
175 $model = ucfirst($model);
176
177 $CI->$name = new $model();
178 $CI->$name->_assign_libraries();
179
180 $this->_ci_models[] = $name;
181 }
182
183 // --------------------------------------------------------------------
184
185 /**
186 * Database Loader
187 *
188 * @access public
189 * @param string the DB credentials
190 * @param bool whether to return the DB object
191 * @param bool whether to enable active record (this allows us to override the config setting)
192 * @return object
193 */
194 function database($params = '', $return = FALSE, $active_record = FALSE)
195 {
Derek Jones72d61332008-01-30 20:52:22 +0000196 // Grab the super object
197 $CI =& get_instance();
198
Derek Allard02658572007-12-20 14:00:50 +0000199 // Do we even need to load the database class?
Derek Jones72d61332008-01-30 20:52:22 +0000200 if (class_exists('CI_DB') AND $return == FALSE AND $active_record == FALSE AND isset($CI->db) AND is_object($CI->db))
Derek Allard02658572007-12-20 14:00:50 +0000201 {
202 return FALSE;
203 }
204
205 require_once(BASEPATH.'database/DB'.EXT);
206
207 if ($return === TRUE)
208 {
209 return DB($params, $active_record);
210 }
Derek Allard02658572007-12-20 14:00:50 +0000211
212 // Initialize the db variable. Needed to prevent
213 // reference errors with some configurations
214 $CI->db = '';
215
216 // Load the DB class
217 $CI->db =& DB($params, $active_record);
218
219 // Assign the DB object to any existing models
220 $this->_ci_assign_to_models();
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Load the Utilities Class
227 *
228 * @access public
229 * @return string
230 */
231 function dbutil()
232 {
Derek Allard73274992008-05-05 16:39:18 +0000233 if (! class_exists('CI_DB'))
Derek Allard02658572007-12-20 14:00:50 +0000234 {
235 $this->database();
236 }
237
238 $CI =& get_instance();
Derek Allard39b622d2008-01-16 21:10:09 +0000239
240 // for backwards compatibility, load dbforge so we can extend dbutils off it
241 // this use is deprecated and strongly discouraged
242 $CI->load->dbforge();
Derek Allard02658572007-12-20 14:00:50 +0000243
244 require_once(BASEPATH.'database/DB_utility'.EXT);
245 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT);
246 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
247
Derek Allard39b622d2008-01-16 21:10:09 +0000248 $CI->dbutil =& new $class();
249
Derek Allard02658572007-12-20 14:00:50 +0000250 $CI->load->_ci_assign_to_models();
251 }
252
253 // --------------------------------------------------------------------
Derek Allard39b622d2008-01-16 21:10:09 +0000254
255 /**
256 * Load the Database Forge Class
257 *
258 * @access public
259 * @return string
260 */
261 function dbforge()
262 {
Derek Allard73274992008-05-05 16:39:18 +0000263 if (! class_exists('CI_DB'))
Derek Allard39b622d2008-01-16 21:10:09 +0000264 {
265 $this->database();
266 }
267
268 $CI =& get_instance();
269
270 require_once(BASEPATH.'database/DB_forge'.EXT);
271 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge'.EXT);
272 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
273
274 $CI->dbforge = new $class();
275 }
276
277 // --------------------------------------------------------------------
Derek Allard02658572007-12-20 14:00:50 +0000278
279 /**
280 * Load View
281 *
282 * This function is used to load a "view" file. It has three parameters:
283 *
284 * 1. The name of the "view" file to be included.
285 * 2. An associative array of data to be extracted for use in the view.
286 * 3. TRUE/FALSE - whether to return the data or load it. In
287 * some cases it's advantageous to be able to return data so that
288 * a developer can process it in some way.
289 *
290 * @access public
291 * @param string
292 * @param array
293 * @param bool
294 * @return void
295 */
296 function view($view, $vars = array(), $return = FALSE)
297 {
Derek Jones47845f22008-01-16 23:23:02 +0000298 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
Derek Allard02658572007-12-20 14:00:50 +0000299 }
300
301 // --------------------------------------------------------------------
302
303 /**
304 * Load File
305 *
306 * This is a generic file loader
307 *
308 * @access public
309 * @param string
310 * @param bool
311 * @return string
312 */
313 function file($path, $return = FALSE)
314 {
Derek Jones47845f22008-01-16 23:23:02 +0000315 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
Derek Allard02658572007-12-20 14:00:50 +0000316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * Set Variables
322 *
323 * Once variables are set they become available within
324 * the controller class and its "view" files.
325 *
326 * @access public
327 * @param array
328 * @return void
329 */
330 function vars($vars = array())
331 {
332 $vars = $this->_ci_object_to_array($vars);
333
334 if (is_array($vars) AND count($vars) > 0)
335 {
336 foreach ($vars as $key => $val)
337 {
338 $this->_ci_cached_vars[$key] = $val;
339 }
340 }
341 }
342
343 // --------------------------------------------------------------------
344
345 /**
346 * Load Helper
347 *
348 * This function loads the specified helper file.
349 *
350 * @access public
351 * @param mixed
352 * @return void
353 */
354 function helper($helpers = array())
355 {
Derek Allard73274992008-05-05 16:39:18 +0000356 if (! is_array($helpers))
Derek Allard02658572007-12-20 14:00:50 +0000357 {
358 $helpers = array($helpers);
359 }
360
361 foreach ($helpers as $helper)
362 {
363 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
364
365 if (isset($this->_ci_helpers[$helper]))
366 {
367 continue;
368 }
Derek Jones269b9422008-01-28 21:00:20 +0000369
370 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.EXT;
Derek Allard02658572007-12-20 14:00:50 +0000371
Derek Jones269b9422008-01-28 21:00:20 +0000372 // Is this a helper extension request?
373 if (file_exists($ext_helper))
374 {
375 $base_helper = BASEPATH.'helpers/'.$helper.EXT;
376
Derek Allard73274992008-05-05 16:39:18 +0000377 if (! file_exists($base_helper))
Derek Jones269b9422008-01-28 21:00:20 +0000378 {
379 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
380 }
381
382 include_once($ext_helper);
383 include_once($base_helper);
384 }
385 elseif (file_exists(APPPATH.'helpers/'.$helper.EXT))
Derek Allard02658572007-12-20 14:00:50 +0000386 {
387 include_once(APPPATH.'helpers/'.$helper.EXT);
388 }
389 else
390 {
391 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
392 {
Derek Allardd888c352008-03-18 04:04:33 +0000393 include_once(BASEPATH.'helpers/'.$helper.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000394 }
395 else
396 {
397 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
398 }
399 }
400
401 $this->_ci_helpers[$helper] = TRUE;
402
403 }
404
405 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
406 }
407
408 // --------------------------------------------------------------------
409
410 /**
411 * Load Helpers
412 *
413 * This is simply an alias to the above function in case the
414 * user has written the plural form of this function.
415 *
416 * @access public
417 * @param array
418 * @return void
419 */
420 function helpers($helpers = array())
421 {
422 $this->helper($helpers);
423 }
424
425 // --------------------------------------------------------------------
426
427 /**
428 * Load Plugin
429 *
430 * This function loads the specified plugin.
431 *
432 * @access public
433 * @param array
434 * @return void
435 */
436 function plugin($plugins = array())
437 {
Derek Allard73274992008-05-05 16:39:18 +0000438 if (! is_array($plugins))
Derek Allard02658572007-12-20 14:00:50 +0000439 {
440 $plugins = array($plugins);
441 }
442
443 foreach ($plugins as $plugin)
444 {
Derek Allard17f74062008-01-16 01:22:44 +0000445 $plugin = strtolower(str_replace(EXT, '', str_replace('_pi', '', $plugin)).'_pi');
Derek Allard02658572007-12-20 14:00:50 +0000446
447 if (isset($this->_ci_plugins[$plugin]))
448 {
449 continue;
450 }
451
452 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
453 {
Derek Allardd888c352008-03-18 04:04:33 +0000454 include_once(APPPATH.'plugins/'.$plugin.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000455 }
456 else
457 {
458 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
459 {
Derek Allardd888c352008-03-18 04:04:33 +0000460 include_once(BASEPATH.'plugins/'.$plugin.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000461 }
462 else
463 {
464 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
465 }
466 }
467
468 $this->_ci_plugins[$plugin] = TRUE;
469 }
470
471 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
472 }
473
474 // --------------------------------------------------------------------
475
476 /**
477 * Load Plugins
478 *
479 * This is simply an alias to the above function in case the
480 * user has written the plural form of this function.
481 *
482 * @access public
483 * @param array
484 * @return void
485 */
486 function plugins($plugins = array())
487 {
488 $this->plugin($plugins);
489 }
490
491 // --------------------------------------------------------------------
492
493 /**
494 * Load Script
495 *
496 * This function loads the specified include file from the
497 * application/scripts/ folder.
498 *
499 * NOTE: This feature has been deprecated but it will remain available
500 * for legacy users.
501 *
502 * @access public
503 * @param array
504 * @return void
505 */
506 function script($scripts = array())
507 {
Derek Allard73274992008-05-05 16:39:18 +0000508 if (! is_array($scripts))
Derek Allard02658572007-12-20 14:00:50 +0000509 {
510 $scripts = array($scripts);
511 }
512
513 foreach ($scripts as $script)
514 {
515 $script = strtolower(str_replace(EXT, '', $script));
516
517 if (isset($this->_ci_scripts[$script]))
518 {
519 continue;
520 }
521
Derek Allard73274992008-05-05 16:39:18 +0000522 if (! file_exists(APPPATH.'scripts/'.$script.EXT))
Derek Allard02658572007-12-20 14:00:50 +0000523 {
524 show_error('Unable to load the requested script: scripts/'.$script.EXT);
525 }
526
Derek Allardd888c352008-03-18 04:04:33 +0000527 include_once(APPPATH.'scripts/'.$script.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000528 }
529
530 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
531 }
532
533 // --------------------------------------------------------------------
534
535 /**
536 * Loads a language file
537 *
538 * @access public
539 * @param array
540 * @param string
541 * @return void
542 */
543 function language($file = array(), $lang = '')
544 {
545 $CI =& get_instance();
546
Derek Allard73274992008-05-05 16:39:18 +0000547 if (! is_array($file))
Derek Allard02658572007-12-20 14:00:50 +0000548 {
549 $file = array($file);
550 }
551
552 foreach ($file as $langfile)
553 {
554 $CI->lang->load($langfile, $lang);
555 }
556 }
557
558 /**
559 * Loads language files for scaffolding
560 *
561 * @access public
562 * @param string
563 * @return arra
564 */
565 function scaffold_language($file = '', $lang = '', $return = FALSE)
566 {
567 $CI =& get_instance();
568 return $CI->lang->load($file, $lang, $return);
569 }
570
571 // --------------------------------------------------------------------
572
573 /**
574 * Loads a config file
575 *
576 * @access public
577 * @param string
578 * @return void
579 */
580 function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
581 {
582 $CI =& get_instance();
583 $CI->config->load($file, $use_sections, $fail_gracefully);
584 }
585
586 // --------------------------------------------------------------------
587
588 /**
589 * Scaffolding Loader
590 *
591 * This initializing function works a bit different than the
592 * others. It doesn't load the class. Instead, it simply
593 * sets a flag indicating that scaffolding is allowed to be
594 * used. The actual scaffolding function below is
595 * called by the front controller based on whether the
596 * second segment of the URL matches the "secret" scaffolding
597 * word stored in the application/config/routes.php
598 *
599 * @access public
600 * @param string
601 * @return void
602 */
603 function scaffolding($table = '')
604 {
605 if ($table === FALSE)
606 {
607 show_error('You must include the name of the table you would like to access when you initialize scaffolding');
608 }
609
610 $CI =& get_instance();
611 $CI->_ci_scaffolding = TRUE;
612 $CI->_ci_scaff_table = $table;
613 }
614
615 // --------------------------------------------------------------------
616
617 /**
618 * Loader
619 *
620 * This function is used to load views and files.
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000621 * Variables are prefixed with _ci_ to avoid symbol collision with
622 * variables made available to view files
Derek Allard02658572007-12-20 14:00:50 +0000623 *
624 * @access private
625 * @param array
626 * @return void
627 */
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000628 function _ci_load($_ci_data)
Derek Allard02658572007-12-20 14:00:50 +0000629 {
630 // Set the default data variables
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000631 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
Derek Allard02658572007-12-20 14:00:50 +0000632 {
Derek Allard73274992008-05-05 16:39:18 +0000633 $$_ci_val = (! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
Derek Allard02658572007-12-20 14:00:50 +0000634 }
635
636 // Set the path to the requested file
Derek Jones47845f22008-01-16 23:23:02 +0000637 if ($_ci_path == '')
Derek Allard02658572007-12-20 14:00:50 +0000638 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000639 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
640 $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view;
641 $_ci_path = $this->_ci_view_path.$_ci_file;
Derek Allard02658572007-12-20 14:00:50 +0000642 }
643 else
644 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000645 $_ci_x = explode('/', $_ci_path);
646 $_ci_file = end($_ci_x);
Derek Allard02658572007-12-20 14:00:50 +0000647 }
648
Derek Allard73274992008-05-05 16:39:18 +0000649 if (! file_exists($_ci_path))
Derek Allard02658572007-12-20 14:00:50 +0000650 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000651 show_error('Unable to load the requested file: '.$_ci_file);
Derek Allard02658572007-12-20 14:00:50 +0000652 }
653
654 // This allows anything loaded using $this->load (views, files, etc.)
655 // to become accessible from within the Controller and Model functions.
656 // Only needed when running PHP 5
657
658 if ($this->_ci_is_instance())
659 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000660 $_ci_CI =& get_instance();
661 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard02658572007-12-20 14:00:50 +0000662 {
Derek Allard73274992008-05-05 16:39:18 +0000663 if (! isset($this->$_ci_key))
Derek Allard02658572007-12-20 14:00:50 +0000664 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000665 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard02658572007-12-20 14:00:50 +0000666 }
667 }
668 }
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000669
Derek Allard02658572007-12-20 14:00:50 +0000670 /*
671 * Extract and cache variables
672 *
673 * You can either set variables using the dedicated $this->load_vars()
674 * function or via the second parameter of this function. We'll merge
675 * the two types and cache them so that views that are embedded within
676 * other views can have access to these variables.
677 */
Derek Jones47845f22008-01-16 23:23:02 +0000678 if (is_array($_ci_vars))
Derek Allard02658572007-12-20 14:00:50 +0000679 {
Derek Jones47845f22008-01-16 23:23:02 +0000680 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
Derek Allard02658572007-12-20 14:00:50 +0000681 }
682 extract($this->_ci_cached_vars);
683
684 /*
685 * Buffer the output
686 *
687 * We buffer the output for two reasons:
688 * 1. Speed. You get a significant speed boost.
689 * 2. So that the final rendered template can be
690 * post-processed by the output class. Why do we
691 * need post processing? For one thing, in order to
692 * show the elapsed page load time. Unless we
693 * can intercept the content right before it's sent to
694 * the browser and then stop the timer it won't be accurate.
695 */
696 ob_start();
697
698 // If the PHP installation does not support short tags we'll
699 // do a little string replacement, changing the short tags
700 // to standard PHP echo statements.
701
702 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
703 {
Derek Jones47845f22008-01-16 23:23:02 +0000704 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))).'<?php ');
Derek Allard02658572007-12-20 14:00:50 +0000705 }
706 else
707 {
Derek Allard72c82c12008-04-04 11:34:58 +0000708 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
Derek Allard02658572007-12-20 14:00:50 +0000709 }
710
Derek Jones47845f22008-01-16 23:23:02 +0000711 log_message('debug', 'File loaded: '.$_ci_path);
Derek Allard02658572007-12-20 14:00:50 +0000712
713 // Return the file data if requested
Derek Jones47845f22008-01-16 23:23:02 +0000714 if ($_ci_return === TRUE)
Derek Allard02658572007-12-20 14:00:50 +0000715 {
716 $buffer = ob_get_contents();
717 @ob_end_clean();
718 return $buffer;
719 }
720
721 /*
722 * Flush the buffer... or buff the flusher?
723 *
724 * In order to permit views to be nested within
725 * other views, we need to flush the content back out whenever
726 * we are beyond the first level of output buffering so that
727 * it can be seen and included properly by the first included
728 * template and any subsequent ones. Oy!
729 *
730 */
731 if (ob_get_level() > $this->_ci_ob_level + 1)
732 {
733 ob_end_flush();
734 }
735 else
736 {
737 // PHP 4 requires that we use a global
738 global $OUT;
Derek Allard66f07242008-01-18 22:36:14 +0000739 $OUT->append_output(ob_get_contents());
Derek Allard02658572007-12-20 14:00:50 +0000740 @ob_end_clean();
741 }
742 }
743
744 // --------------------------------------------------------------------
745
746 /**
747 * Load class
748 *
749 * This function loads the requested class.
750 *
751 * @access private
752 * @param string the item that is being loaded
753 * @param mixed any additional parameters
754 * @return void
755 */
756 function _ci_load_class($class, $params = NULL)
757 {
758 // Get the class name
759 $class = str_replace(EXT, '', $class);
760
761 // We'll test for both lowercase and capitalized versions of the file name
762 foreach (array(ucfirst($class), strtolower($class)) as $class)
763 {
764 $subclass = APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT;
765
766 // Is this a class extension request?
767 if (file_exists($subclass))
768 {
769 $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
770
Derek Allard73274992008-05-05 16:39:18 +0000771 if (! file_exists($baseclass))
Derek Allard02658572007-12-20 14:00:50 +0000772 {
773 log_message('error', "Unable to load the requested class: ".$class);
774 show_error("Unable to load the requested class: ".$class);
775 }
776
777 // Safety: Was the class already loaded by a previous call?
778 if (in_array($subclass, $this->_ci_classes))
779 {
780 $is_duplicate = TRUE;
781 log_message('debug', $class." class already loaded. Second attempt ignored.");
782 return;
783 }
784
Derek Allardd888c352008-03-18 04:04:33 +0000785 include_once($baseclass);
786 include_once($subclass);
Derek Allard02658572007-12-20 14:00:50 +0000787 $this->_ci_classes[] = $subclass;
788
789 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params);
790 }
791
792 // Lets search for the requested library file and load it.
793 $is_duplicate = FALSE;
794 for ($i = 1; $i < 3; $i++)
795 {
796 $path = ($i % 2) ? APPPATH : BASEPATH;
797 $filepath = $path.'libraries/'.$class.EXT;
798
799 // Does the file exist? No? Bummer...
Derek Allard73274992008-05-05 16:39:18 +0000800 if (! file_exists($filepath))
Derek Allard02658572007-12-20 14:00:50 +0000801 {
802 continue;
803 }
804
805 // Safety: Was the class already loaded by a previous call?
806 if (in_array($filepath, $this->_ci_classes))
807 {
808 $is_duplicate = TRUE;
809 log_message('debug', $class." class already loaded. Second attempt ignored.");
810 return;
811 }
812
Derek Allardd888c352008-03-18 04:04:33 +0000813 include_once($filepath);
Derek Allard02658572007-12-20 14:00:50 +0000814 $this->_ci_classes[] = $filepath;
815 return $this->_ci_init_class($class, '', $params);
816 }
817 } // END FOREACH
818
819 // If we got this far we were unable to find the requested class.
820 // We do not issue errors if the load call failed due to a duplicate request
821 if ($is_duplicate == FALSE)
822 {
823 log_message('error', "Unable to load the requested class: ".$class);
824 show_error("Unable to load the requested class: ".$class);
825 }
826 }
827
828 // --------------------------------------------------------------------
829
830 /**
831 * Instantiates a class
832 *
833 * @access private
834 * @param string
835 * @param string
836 * @return null
837 */
838 function _ci_init_class($class, $prefix = '', $config = FALSE)
839 {
Derek Allard0796b8d2008-01-17 03:41:37 +0000840 $class = strtolower($class);
841
Derek Allard02658572007-12-20 14:00:50 +0000842 // Is there an associated config file for this class?
843 if ($config === NULL)
844 {
845 if (file_exists(APPPATH.'config/'.$class.EXT))
846 {
Derek Allardd888c352008-03-18 04:04:33 +0000847 include_once(APPPATH.'config/'.$class.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000848 }
849 }
850
851 if ($prefix == '')
852 {
853 $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
854 }
855 else
856 {
857 $name = $prefix.$class;
858 }
859
Derek Allard0796b8d2008-01-17 03:41:37 +0000860 // Set the variable name we will assign the class to
Derek Allard73274992008-05-05 16:39:18 +0000861 $classvar = (! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
Derek Allard02658572007-12-20 14:00:50 +0000862
863 // Instantiate the class
864 $CI =& get_instance();
865 if ($config !== NULL)
866 {
867 $CI->$classvar = new $name($config);
868 }
869 else
870 {
871 $CI->$classvar = new $name;
872 }
873 }
874
875 // --------------------------------------------------------------------
876
877 /**
878 * Autoloader
879 *
880 * The config/autoload.php file contains an array that permits sub-systems,
881 * libraries, plugins, and helpers to be loaded automatically.
882 *
883 * @access private
884 * @param array
885 * @return void
886 */
887 function _ci_autoloader()
888 {
Derek Allardd888c352008-03-18 04:04:33 +0000889 include_once(APPPATH.'config/autoload'.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000890
Derek Allard73274992008-05-05 16:39:18 +0000891 if (! isset($autoload))
Derek Allard02658572007-12-20 14:00:50 +0000892 {
893 return FALSE;
894 }
895
896 // Load any custom config file
897 if (count($autoload['config']) > 0)
898 {
899 $CI =& get_instance();
900 foreach ($autoload['config'] as $key => $val)
901 {
902 $CI->config->load($val);
903 }
904 }
905
906 // Autoload plugins, helpers, scripts and languages
907 foreach (array('helper', 'plugin', 'script', 'language') as $type)
908 {
909 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
910 {
911 $this->$type($autoload[$type]);
912 }
913 }
914
Derek Allard22259b52008-01-22 01:47:11 +0000915
Derek Allard02658572007-12-20 14:00:50 +0000916
917 // A little tweak to remain backward compatible
918 // The $autoload['core'] item was deprecated
Derek Allard73274992008-05-05 16:39:18 +0000919 if (! isset($autoload['libraries']))
Derek Allard02658572007-12-20 14:00:50 +0000920 {
921 $autoload['libraries'] = $autoload['core'];
922 }
923
924 // Load libraries
925 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
926 {
927 // Load the database driver.
928 if (in_array('database', $autoload['libraries']))
929 {
930 $this->database();
931 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
932 }
933
934 // Load scaffolding
935 if (in_array('scaffolding', $autoload['libraries']))
936 {
937 $this->scaffolding();
938 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
939 }
940
941 // Load all other libraries
942 foreach ($autoload['libraries'] as $item)
943 {
944 $this->library($item);
945 }
Derek Allard02658572007-12-20 14:00:50 +0000946 }
Derek Allard22259b52008-01-22 01:47:11 +0000947
948 // Autoload models
949 if (isset($autoload['model']))
950 {
951 $this->model($autoload['model']);
952 }
953
Derek Allard02658572007-12-20 14:00:50 +0000954 }
955
956 // --------------------------------------------------------------------
957
958 /**
959 * Assign to Models
960 *
961 * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
962 * will be available to models, if any exist.
963 *
964 * @access private
965 * @param object
966 * @return array
967 */
968 function _ci_assign_to_models()
969 {
970 if (count($this->_ci_models) == 0)
971 {
972 return;
973 }
974
975 if ($this->_ci_is_instance())
976 {
977 $CI =& get_instance();
978 foreach ($this->_ci_models as $model)
979 {
980 $CI->$model->_assign_libraries();
981 }
982 }
983 else
984 {
985 foreach ($this->_ci_models as $model)
986 {
987 $this->$model->_assign_libraries();
988 }
989 }
990 }
991
992 // --------------------------------------------------------------------
993
994 /**
995 * Object to Array
996 *
997 * Takes an object as input and converts the class variables to array key/vals
998 *
999 * @access private
1000 * @param object
1001 * @return array
1002 */
1003 function _ci_object_to_array($object)
1004 {
1005 return (is_object($object)) ? get_object_vars($object) : $object;
1006 }
1007
1008 // --------------------------------------------------------------------
1009
1010 /**
1011 * Determines whether we should use the CI instance or $this
1012 *
1013 * @access private
1014 * @return bool
1015 */
1016 function _ci_is_instance()
1017 {
1018 if ($this->_ci_is_php5 == TRUE)
1019 {
1020 return TRUE;
1021 }
1022
1023 global $CI;
1024 return (is_object($CI)) ? TRUE : FALSE;
1025 }
1026
1027}
1028?>