blob: 687598096e5ac9ea046bf6566eb9157582a4cad8 [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();
Derek Allardff390bd2008-05-06 00:37:12 +0000275
276 $CI->load->_ci_assign_to_models();
Derek Allard39b622d2008-01-16 21:10:09 +0000277 }
278
279 // --------------------------------------------------------------------
Derek Allard02658572007-12-20 14:00:50 +0000280
281 /**
282 * Load View
283 *
284 * This function is used to load a "view" file. It has three parameters:
285 *
286 * 1. The name of the "view" file to be included.
287 * 2. An associative array of data to be extracted for use in the view.
288 * 3. TRUE/FALSE - whether to return the data or load it. In
289 * some cases it's advantageous to be able to return data so that
290 * a developer can process it in some way.
291 *
292 * @access public
293 * @param string
294 * @param array
295 * @param bool
296 * @return void
297 */
298 function view($view, $vars = array(), $return = FALSE)
299 {
Derek Jones47845f22008-01-16 23:23:02 +0000300 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 +0000301 }
302
303 // --------------------------------------------------------------------
304
305 /**
306 * Load File
307 *
308 * This is a generic file loader
309 *
310 * @access public
311 * @param string
312 * @param bool
313 * @return string
314 */
315 function file($path, $return = FALSE)
316 {
Derek Jones47845f22008-01-16 23:23:02 +0000317 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
Derek Allard02658572007-12-20 14:00:50 +0000318 }
319
320 // --------------------------------------------------------------------
321
322 /**
323 * Set Variables
324 *
325 * Once variables are set they become available within
326 * the controller class and its "view" files.
327 *
328 * @access public
329 * @param array
330 * @return void
331 */
332 function vars($vars = array())
333 {
334 $vars = $this->_ci_object_to_array($vars);
335
336 if (is_array($vars) AND count($vars) > 0)
337 {
338 foreach ($vars as $key => $val)
339 {
340 $this->_ci_cached_vars[$key] = $val;
341 }
342 }
343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * Load Helper
349 *
350 * This function loads the specified helper file.
351 *
352 * @access public
353 * @param mixed
354 * @return void
355 */
356 function helper($helpers = array())
357 {
Derek Allard73274992008-05-05 16:39:18 +0000358 if (! is_array($helpers))
Derek Allard02658572007-12-20 14:00:50 +0000359 {
360 $helpers = array($helpers);
361 }
362
363 foreach ($helpers as $helper)
364 {
365 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
366
367 if (isset($this->_ci_helpers[$helper]))
368 {
369 continue;
370 }
Derek Jones269b9422008-01-28 21:00:20 +0000371
372 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.EXT;
Derek Allard02658572007-12-20 14:00:50 +0000373
Derek Jones269b9422008-01-28 21:00:20 +0000374 // Is this a helper extension request?
375 if (file_exists($ext_helper))
376 {
377 $base_helper = BASEPATH.'helpers/'.$helper.EXT;
378
Derek Allard73274992008-05-05 16:39:18 +0000379 if (! file_exists($base_helper))
Derek Jones269b9422008-01-28 21:00:20 +0000380 {
381 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
382 }
383
384 include_once($ext_helper);
385 include_once($base_helper);
386 }
387 elseif (file_exists(APPPATH.'helpers/'.$helper.EXT))
Derek Allard02658572007-12-20 14:00:50 +0000388 {
389 include_once(APPPATH.'helpers/'.$helper.EXT);
390 }
391 else
392 {
393 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
394 {
Derek Allardd888c352008-03-18 04:04:33 +0000395 include_once(BASEPATH.'helpers/'.$helper.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000396 }
397 else
398 {
399 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
400 }
401 }
402
403 $this->_ci_helpers[$helper] = TRUE;
404
405 }
406
407 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
408 }
409
410 // --------------------------------------------------------------------
411
412 /**
413 * Load Helpers
414 *
415 * This is simply an alias to the above function in case the
416 * user has written the plural form of this function.
417 *
418 * @access public
419 * @param array
420 * @return void
421 */
422 function helpers($helpers = array())
423 {
424 $this->helper($helpers);
425 }
426
427 // --------------------------------------------------------------------
428
429 /**
430 * Load Plugin
431 *
432 * This function loads the specified plugin.
433 *
434 * @access public
435 * @param array
436 * @return void
437 */
438 function plugin($plugins = array())
439 {
Derek Allard73274992008-05-05 16:39:18 +0000440 if (! is_array($plugins))
Derek Allard02658572007-12-20 14:00:50 +0000441 {
442 $plugins = array($plugins);
443 }
444
445 foreach ($plugins as $plugin)
446 {
Derek Allard17f74062008-01-16 01:22:44 +0000447 $plugin = strtolower(str_replace(EXT, '', str_replace('_pi', '', $plugin)).'_pi');
Derek Allard02658572007-12-20 14:00:50 +0000448
449 if (isset($this->_ci_plugins[$plugin]))
450 {
451 continue;
452 }
453
454 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
455 {
Derek Allardd888c352008-03-18 04:04:33 +0000456 include_once(APPPATH.'plugins/'.$plugin.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000457 }
458 else
459 {
460 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
461 {
Derek Allardd888c352008-03-18 04:04:33 +0000462 include_once(BASEPATH.'plugins/'.$plugin.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000463 }
464 else
465 {
466 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
467 }
468 }
469
470 $this->_ci_plugins[$plugin] = TRUE;
471 }
472
473 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
474 }
475
476 // --------------------------------------------------------------------
477
478 /**
479 * Load Plugins
480 *
481 * This is simply an alias to the above function in case the
482 * user has written the plural form of this function.
483 *
484 * @access public
485 * @param array
486 * @return void
487 */
488 function plugins($plugins = array())
489 {
490 $this->plugin($plugins);
491 }
492
493 // --------------------------------------------------------------------
494
495 /**
496 * Load Script
497 *
498 * This function loads the specified include file from the
499 * application/scripts/ folder.
500 *
501 * NOTE: This feature has been deprecated but it will remain available
502 * for legacy users.
503 *
504 * @access public
505 * @param array
506 * @return void
507 */
508 function script($scripts = array())
509 {
Derek Allard73274992008-05-05 16:39:18 +0000510 if (! is_array($scripts))
Derek Allard02658572007-12-20 14:00:50 +0000511 {
512 $scripts = array($scripts);
513 }
514
515 foreach ($scripts as $script)
516 {
517 $script = strtolower(str_replace(EXT, '', $script));
518
519 if (isset($this->_ci_scripts[$script]))
520 {
521 continue;
522 }
523
Derek Allard73274992008-05-05 16:39:18 +0000524 if (! file_exists(APPPATH.'scripts/'.$script.EXT))
Derek Allard02658572007-12-20 14:00:50 +0000525 {
526 show_error('Unable to load the requested script: scripts/'.$script.EXT);
527 }
528
Derek Allardd888c352008-03-18 04:04:33 +0000529 include_once(APPPATH.'scripts/'.$script.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000530 }
531
532 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
533 }
534
535 // --------------------------------------------------------------------
536
537 /**
538 * Loads a language file
539 *
540 * @access public
541 * @param array
542 * @param string
543 * @return void
544 */
545 function language($file = array(), $lang = '')
546 {
547 $CI =& get_instance();
548
Derek Allard73274992008-05-05 16:39:18 +0000549 if (! is_array($file))
Derek Allard02658572007-12-20 14:00:50 +0000550 {
551 $file = array($file);
552 }
553
554 foreach ($file as $langfile)
555 {
556 $CI->lang->load($langfile, $lang);
557 }
558 }
559
560 /**
561 * Loads language files for scaffolding
562 *
563 * @access public
564 * @param string
565 * @return arra
566 */
567 function scaffold_language($file = '', $lang = '', $return = FALSE)
568 {
569 $CI =& get_instance();
570 return $CI->lang->load($file, $lang, $return);
571 }
572
573 // --------------------------------------------------------------------
574
575 /**
576 * Loads a config file
577 *
578 * @access public
579 * @param string
580 * @return void
581 */
582 function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
583 {
584 $CI =& get_instance();
585 $CI->config->load($file, $use_sections, $fail_gracefully);
586 }
587
588 // --------------------------------------------------------------------
589
590 /**
591 * Scaffolding Loader
592 *
593 * This initializing function works a bit different than the
594 * others. It doesn't load the class. Instead, it simply
595 * sets a flag indicating that scaffolding is allowed to be
596 * used. The actual scaffolding function below is
597 * called by the front controller based on whether the
598 * second segment of the URL matches the "secret" scaffolding
599 * word stored in the application/config/routes.php
600 *
601 * @access public
602 * @param string
603 * @return void
604 */
605 function scaffolding($table = '')
606 {
607 if ($table === FALSE)
608 {
609 show_error('You must include the name of the table you would like to access when you initialize scaffolding');
610 }
611
612 $CI =& get_instance();
613 $CI->_ci_scaffolding = TRUE;
614 $CI->_ci_scaff_table = $table;
615 }
616
617 // --------------------------------------------------------------------
618
619 /**
620 * Loader
621 *
622 * This function is used to load views and files.
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000623 * Variables are prefixed with _ci_ to avoid symbol collision with
624 * variables made available to view files
Derek Allard02658572007-12-20 14:00:50 +0000625 *
626 * @access private
627 * @param array
628 * @return void
629 */
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000630 function _ci_load($_ci_data)
Derek Allard02658572007-12-20 14:00:50 +0000631 {
632 // Set the default data variables
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000633 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
Derek Allard02658572007-12-20 14:00:50 +0000634 {
Derek Allard73274992008-05-05 16:39:18 +0000635 $$_ci_val = (! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
Derek Allard02658572007-12-20 14:00:50 +0000636 }
637
638 // Set the path to the requested file
Derek Jones47845f22008-01-16 23:23:02 +0000639 if ($_ci_path == '')
Derek Allard02658572007-12-20 14:00:50 +0000640 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000641 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
642 $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view;
643 $_ci_path = $this->_ci_view_path.$_ci_file;
Derek Allard02658572007-12-20 14:00:50 +0000644 }
645 else
646 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000647 $_ci_x = explode('/', $_ci_path);
648 $_ci_file = end($_ci_x);
Derek Allard02658572007-12-20 14:00:50 +0000649 }
650
Derek Allard73274992008-05-05 16:39:18 +0000651 if (! file_exists($_ci_path))
Derek Allard02658572007-12-20 14:00:50 +0000652 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000653 show_error('Unable to load the requested file: '.$_ci_file);
Derek Allard02658572007-12-20 14:00:50 +0000654 }
655
656 // This allows anything loaded using $this->load (views, files, etc.)
657 // to become accessible from within the Controller and Model functions.
658 // Only needed when running PHP 5
659
660 if ($this->_ci_is_instance())
661 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000662 $_ci_CI =& get_instance();
663 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard02658572007-12-20 14:00:50 +0000664 {
Derek Allard73274992008-05-05 16:39:18 +0000665 if (! isset($this->$_ci_key))
Derek Allard02658572007-12-20 14:00:50 +0000666 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000667 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard02658572007-12-20 14:00:50 +0000668 }
669 }
670 }
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000671
Derek Allard02658572007-12-20 14:00:50 +0000672 /*
673 * Extract and cache variables
674 *
675 * You can either set variables using the dedicated $this->load_vars()
676 * function or via the second parameter of this function. We'll merge
677 * the two types and cache them so that views that are embedded within
678 * other views can have access to these variables.
679 */
Derek Jones47845f22008-01-16 23:23:02 +0000680 if (is_array($_ci_vars))
Derek Allard02658572007-12-20 14:00:50 +0000681 {
Derek Jones47845f22008-01-16 23:23:02 +0000682 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
Derek Allard02658572007-12-20 14:00:50 +0000683 }
684 extract($this->_ci_cached_vars);
685
686 /*
687 * Buffer the output
688 *
689 * We buffer the output for two reasons:
690 * 1. Speed. You get a significant speed boost.
691 * 2. So that the final rendered template can be
692 * post-processed by the output class. Why do we
693 * need post processing? For one thing, in order to
694 * show the elapsed page load time. Unless we
695 * can intercept the content right before it's sent to
696 * the browser and then stop the timer it won't be accurate.
697 */
698 ob_start();
699
700 // If the PHP installation does not support short tags we'll
701 // do a little string replacement, changing the short tags
702 // to standard PHP echo statements.
703
704 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
705 {
Derek Jones47845f22008-01-16 23:23:02 +0000706 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))).'<?php ');
Derek Allard02658572007-12-20 14:00:50 +0000707 }
708 else
709 {
Derek Allard72c82c12008-04-04 11:34:58 +0000710 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
Derek Allard02658572007-12-20 14:00:50 +0000711 }
712
Derek Jones47845f22008-01-16 23:23:02 +0000713 log_message('debug', 'File loaded: '.$_ci_path);
Derek Allard02658572007-12-20 14:00:50 +0000714
715 // Return the file data if requested
Derek Jones47845f22008-01-16 23:23:02 +0000716 if ($_ci_return === TRUE)
Derek Allard02658572007-12-20 14:00:50 +0000717 {
718 $buffer = ob_get_contents();
719 @ob_end_clean();
720 return $buffer;
721 }
722
723 /*
724 * Flush the buffer... or buff the flusher?
725 *
726 * In order to permit views to be nested within
727 * other views, we need to flush the content back out whenever
728 * we are beyond the first level of output buffering so that
729 * it can be seen and included properly by the first included
730 * template and any subsequent ones. Oy!
731 *
732 */
733 if (ob_get_level() > $this->_ci_ob_level + 1)
734 {
735 ob_end_flush();
736 }
737 else
738 {
739 // PHP 4 requires that we use a global
740 global $OUT;
Derek Allard66f07242008-01-18 22:36:14 +0000741 $OUT->append_output(ob_get_contents());
Derek Allard02658572007-12-20 14:00:50 +0000742 @ob_end_clean();
743 }
744 }
745
746 // --------------------------------------------------------------------
747
748 /**
749 * Load class
750 *
751 * This function loads the requested class.
752 *
753 * @access private
754 * @param string the item that is being loaded
755 * @param mixed any additional parameters
756 * @return void
757 */
758 function _ci_load_class($class, $params = NULL)
759 {
760 // Get the class name
761 $class = str_replace(EXT, '', $class);
762
763 // We'll test for both lowercase and capitalized versions of the file name
764 foreach (array(ucfirst($class), strtolower($class)) as $class)
765 {
766 $subclass = APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT;
767
768 // Is this a class extension request?
769 if (file_exists($subclass))
770 {
771 $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
772
Derek Allard73274992008-05-05 16:39:18 +0000773 if (! file_exists($baseclass))
Derek Allard02658572007-12-20 14:00:50 +0000774 {
775 log_message('error', "Unable to load the requested class: ".$class);
776 show_error("Unable to load the requested class: ".$class);
777 }
778
779 // Safety: Was the class already loaded by a previous call?
780 if (in_array($subclass, $this->_ci_classes))
781 {
782 $is_duplicate = TRUE;
783 log_message('debug', $class." class already loaded. Second attempt ignored.");
784 return;
785 }
786
Derek Allardd888c352008-03-18 04:04:33 +0000787 include_once($baseclass);
788 include_once($subclass);
Derek Allard02658572007-12-20 14:00:50 +0000789 $this->_ci_classes[] = $subclass;
790
791 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params);
792 }
793
794 // Lets search for the requested library file and load it.
795 $is_duplicate = FALSE;
796 for ($i = 1; $i < 3; $i++)
797 {
798 $path = ($i % 2) ? APPPATH : BASEPATH;
799 $filepath = $path.'libraries/'.$class.EXT;
800
801 // Does the file exist? No? Bummer...
Derek Allard73274992008-05-05 16:39:18 +0000802 if (! file_exists($filepath))
Derek Allard02658572007-12-20 14:00:50 +0000803 {
804 continue;
805 }
806
807 // Safety: Was the class already loaded by a previous call?
808 if (in_array($filepath, $this->_ci_classes))
809 {
810 $is_duplicate = TRUE;
811 log_message('debug', $class." class already loaded. Second attempt ignored.");
812 return;
813 }
814
Derek Allardd888c352008-03-18 04:04:33 +0000815 include_once($filepath);
Derek Allard02658572007-12-20 14:00:50 +0000816 $this->_ci_classes[] = $filepath;
817 return $this->_ci_init_class($class, '', $params);
818 }
819 } // END FOREACH
820
821 // If we got this far we were unable to find the requested class.
822 // We do not issue errors if the load call failed due to a duplicate request
823 if ($is_duplicate == FALSE)
824 {
825 log_message('error', "Unable to load the requested class: ".$class);
826 show_error("Unable to load the requested class: ".$class);
827 }
828 }
829
830 // --------------------------------------------------------------------
831
832 /**
833 * Instantiates a class
834 *
835 * @access private
836 * @param string
837 * @param string
838 * @return null
839 */
840 function _ci_init_class($class, $prefix = '', $config = FALSE)
841 {
Derek Allard0796b8d2008-01-17 03:41:37 +0000842 $class = strtolower($class);
843
Derek Allard02658572007-12-20 14:00:50 +0000844 // Is there an associated config file for this class?
845 if ($config === NULL)
846 {
847 if (file_exists(APPPATH.'config/'.$class.EXT))
848 {
Derek Allardd888c352008-03-18 04:04:33 +0000849 include_once(APPPATH.'config/'.$class.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000850 }
851 }
852
853 if ($prefix == '')
854 {
855 $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
856 }
857 else
858 {
859 $name = $prefix.$class;
860 }
861
Derek Allard0796b8d2008-01-17 03:41:37 +0000862 // Set the variable name we will assign the class to
Derek Allard73274992008-05-05 16:39:18 +0000863 $classvar = (! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
Derek Allard02658572007-12-20 14:00:50 +0000864
865 // Instantiate the class
866 $CI =& get_instance();
867 if ($config !== NULL)
868 {
869 $CI->$classvar = new $name($config);
870 }
871 else
872 {
873 $CI->$classvar = new $name;
874 }
875 }
876
877 // --------------------------------------------------------------------
878
879 /**
880 * Autoloader
881 *
882 * The config/autoload.php file contains an array that permits sub-systems,
883 * libraries, plugins, and helpers to be loaded automatically.
884 *
885 * @access private
886 * @param array
887 * @return void
888 */
889 function _ci_autoloader()
890 {
Derek Allardd888c352008-03-18 04:04:33 +0000891 include_once(APPPATH.'config/autoload'.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000892
Derek Allard73274992008-05-05 16:39:18 +0000893 if (! isset($autoload))
Derek Allard02658572007-12-20 14:00:50 +0000894 {
895 return FALSE;
896 }
897
898 // Load any custom config file
899 if (count($autoload['config']) > 0)
900 {
901 $CI =& get_instance();
902 foreach ($autoload['config'] as $key => $val)
903 {
904 $CI->config->load($val);
905 }
906 }
907
908 // Autoload plugins, helpers, scripts and languages
909 foreach (array('helper', 'plugin', 'script', 'language') as $type)
910 {
911 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
912 {
913 $this->$type($autoload[$type]);
914 }
915 }
916
Derek Allard22259b52008-01-22 01:47:11 +0000917
Derek Allard02658572007-12-20 14:00:50 +0000918
919 // A little tweak to remain backward compatible
920 // The $autoload['core'] item was deprecated
Derek Allard73274992008-05-05 16:39:18 +0000921 if (! isset($autoload['libraries']))
Derek Allard02658572007-12-20 14:00:50 +0000922 {
923 $autoload['libraries'] = $autoload['core'];
924 }
925
926 // Load libraries
927 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
928 {
929 // Load the database driver.
930 if (in_array('database', $autoload['libraries']))
931 {
932 $this->database();
933 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
934 }
935
936 // Load scaffolding
937 if (in_array('scaffolding', $autoload['libraries']))
938 {
939 $this->scaffolding();
940 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
941 }
942
943 // Load all other libraries
944 foreach ($autoload['libraries'] as $item)
945 {
946 $this->library($item);
947 }
Derek Allard02658572007-12-20 14:00:50 +0000948 }
Derek Allard22259b52008-01-22 01:47:11 +0000949
950 // Autoload models
951 if (isset($autoload['model']))
952 {
953 $this->model($autoload['model']);
954 }
955
Derek Allard02658572007-12-20 14:00:50 +0000956 }
957
958 // --------------------------------------------------------------------
959
960 /**
961 * Assign to Models
962 *
963 * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
964 * will be available to models, if any exist.
965 *
966 * @access private
967 * @param object
968 * @return array
969 */
970 function _ci_assign_to_models()
971 {
972 if (count($this->_ci_models) == 0)
973 {
974 return;
975 }
976
977 if ($this->_ci_is_instance())
978 {
979 $CI =& get_instance();
980 foreach ($this->_ci_models as $model)
981 {
982 $CI->$model->_assign_libraries();
983 }
984 }
985 else
986 {
987 foreach ($this->_ci_models as $model)
988 {
989 $this->$model->_assign_libraries();
990 }
991 }
992 }
993
994 // --------------------------------------------------------------------
995
996 /**
997 * Object to Array
998 *
999 * Takes an object as input and converts the class variables to array key/vals
1000 *
1001 * @access private
1002 * @param object
1003 * @return array
1004 */
1005 function _ci_object_to_array($object)
1006 {
1007 return (is_object($object)) ? get_object_vars($object) : $object;
1008 }
1009
1010 // --------------------------------------------------------------------
1011
1012 /**
1013 * Determines whether we should use the CI instance or $this
1014 *
1015 * @access private
1016 * @return bool
1017 */
1018 function _ci_is_instance()
1019 {
1020 if ($this->_ci_is_php5 == TRUE)
1021 {
1022 return TRUE;
1023 }
1024
1025 global $CI;
1026 return (is_object($CI)) ? TRUE : FALSE;
1027 }
1028
1029}
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001030
1031/* End of file Loader.php */
1032/* Location: ./system/libraries/Loader.php */