blob: 842e375a9be613d0eb4273a72cea5565f45a3d1c [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
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, EllisLab, Inc.
10 * @license http://www.codeigniter.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
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
155 if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
156 {
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
168 if ( ! class_exists('Model'))
169 {
170 require_once(BASEPATH.'libraries/Model'.EXT);
171 }
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 {
196 // Do we even need to load the database class?
197 if (class_exists('CI_DB') AND $return == FALSE AND $active_record == FALSE)
198 {
199 return FALSE;
200 }
201
202 require_once(BASEPATH.'database/DB'.EXT);
203
204 if ($return === TRUE)
205 {
206 return DB($params, $active_record);
207 }
208
209 // Grab the super object
210 $CI =& get_instance();
211
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 {
233 if ( ! class_exists('CI_DB'))
234 {
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 {
263 if ( ! class_exists('CI_DB'))
264 {
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 {
356 if ( ! is_array($helpers))
357 {
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 }
369
370 if (file_exists(APPPATH.'helpers/'.$helper.EXT))
371 {
372 include_once(APPPATH.'helpers/'.$helper.EXT);
373 }
374 else
375 {
376 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
377 {
378 include(BASEPATH.'helpers/'.$helper.EXT);
379 }
380 else
381 {
382 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
383 }
384 }
385
386 $this->_ci_helpers[$helper] = TRUE;
387
388 }
389
390 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
391 }
392
393 // --------------------------------------------------------------------
394
395 /**
396 * Load Helpers
397 *
398 * This is simply an alias to the above function in case the
399 * user has written the plural form of this function.
400 *
401 * @access public
402 * @param array
403 * @return void
404 */
405 function helpers($helpers = array())
406 {
407 $this->helper($helpers);
408 }
409
410 // --------------------------------------------------------------------
411
412 /**
413 * Load Plugin
414 *
415 * This function loads the specified plugin.
416 *
417 * @access public
418 * @param array
419 * @return void
420 */
421 function plugin($plugins = array())
422 {
423 if ( ! is_array($plugins))
424 {
425 $plugins = array($plugins);
426 }
427
428 foreach ($plugins as $plugin)
429 {
Derek Allard17f74062008-01-16 01:22:44 +0000430 $plugin = strtolower(str_replace(EXT, '', str_replace('_pi', '', $plugin)).'_pi');
Derek Allard02658572007-12-20 14:00:50 +0000431
432 if (isset($this->_ci_plugins[$plugin]))
433 {
434 continue;
435 }
436
437 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
438 {
439 include(APPPATH.'plugins/'.$plugin.EXT);
440 }
441 else
442 {
443 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
444 {
445 include(BASEPATH.'plugins/'.$plugin.EXT);
446 }
447 else
448 {
449 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
450 }
451 }
452
453 $this->_ci_plugins[$plugin] = TRUE;
454 }
455
456 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
457 }
458
459 // --------------------------------------------------------------------
460
461 /**
462 * Load Plugins
463 *
464 * This is simply an alias to the above function in case the
465 * user has written the plural form of this function.
466 *
467 * @access public
468 * @param array
469 * @return void
470 */
471 function plugins($plugins = array())
472 {
473 $this->plugin($plugins);
474 }
475
476 // --------------------------------------------------------------------
477
478 /**
479 * Load Script
480 *
481 * This function loads the specified include file from the
482 * application/scripts/ folder.
483 *
484 * NOTE: This feature has been deprecated but it will remain available
485 * for legacy users.
486 *
487 * @access public
488 * @param array
489 * @return void
490 */
491 function script($scripts = array())
492 {
493 if ( ! is_array($scripts))
494 {
495 $scripts = array($scripts);
496 }
497
498 foreach ($scripts as $script)
499 {
500 $script = strtolower(str_replace(EXT, '', $script));
501
502 if (isset($this->_ci_scripts[$script]))
503 {
504 continue;
505 }
506
507 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
508 {
509 show_error('Unable to load the requested script: scripts/'.$script.EXT);
510 }
511
512 include(APPPATH.'scripts/'.$script.EXT);
513 }
514
515 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
516 }
517
518 // --------------------------------------------------------------------
519
520 /**
521 * Loads a language file
522 *
523 * @access public
524 * @param array
525 * @param string
526 * @return void
527 */
528 function language($file = array(), $lang = '')
529 {
530 $CI =& get_instance();
531
532 if ( ! is_array($file))
533 {
534 $file = array($file);
535 }
536
537 foreach ($file as $langfile)
538 {
539 $CI->lang->load($langfile, $lang);
540 }
541 }
542
543 /**
544 * Loads language files for scaffolding
545 *
546 * @access public
547 * @param string
548 * @return arra
549 */
550 function scaffold_language($file = '', $lang = '', $return = FALSE)
551 {
552 $CI =& get_instance();
553 return $CI->lang->load($file, $lang, $return);
554 }
555
556 // --------------------------------------------------------------------
557
558 /**
559 * Loads a config file
560 *
561 * @access public
562 * @param string
563 * @return void
564 */
565 function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
566 {
567 $CI =& get_instance();
568 $CI->config->load($file, $use_sections, $fail_gracefully);
569 }
570
571 // --------------------------------------------------------------------
572
573 /**
574 * Scaffolding Loader
575 *
576 * This initializing function works a bit different than the
577 * others. It doesn't load the class. Instead, it simply
578 * sets a flag indicating that scaffolding is allowed to be
579 * used. The actual scaffolding function below is
580 * called by the front controller based on whether the
581 * second segment of the URL matches the "secret" scaffolding
582 * word stored in the application/config/routes.php
583 *
584 * @access public
585 * @param string
586 * @return void
587 */
588 function scaffolding($table = '')
589 {
590 if ($table === FALSE)
591 {
592 show_error('You must include the name of the table you would like to access when you initialize scaffolding');
593 }
594
595 $CI =& get_instance();
596 $CI->_ci_scaffolding = TRUE;
597 $CI->_ci_scaff_table = $table;
598 }
599
600 // --------------------------------------------------------------------
601
602 /**
603 * Loader
604 *
605 * This function is used to load views and files.
606 *
607 * @access private
608 * @param array
609 * @return void
610 */
611 function _ci_load($data)
612 {
613 // Set the default data variables
Derek Jones47845f22008-01-16 23:23:02 +0000614 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $val)
Derek Allard02658572007-12-20 14:00:50 +0000615 {
616 $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
617 }
618
619 // Set the path to the requested file
Derek Jones47845f22008-01-16 23:23:02 +0000620 if ($_ci_path == '')
Derek Allard02658572007-12-20 14:00:50 +0000621 {
Derek Jones47845f22008-01-16 23:23:02 +0000622 $ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
623 $file = ($ext == '') ? $_ci_view.EXT : $_ci_view;
624 $_ci_path = $this->_ci_view_path.$file;
Derek Allard02658572007-12-20 14:00:50 +0000625 }
626 else
627 {
Derek Jones47845f22008-01-16 23:23:02 +0000628 $x = explode('/', $_ci_path);
Derek Allard02658572007-12-20 14:00:50 +0000629 $file = end($x);
630 }
631
Derek Jones47845f22008-01-16 23:23:02 +0000632 if ( ! file_exists($_ci_path))
Derek Allard02658572007-12-20 14:00:50 +0000633 {
634 show_error('Unable to load the requested file: '.$file);
635 }
636
637 // This allows anything loaded using $this->load (views, files, etc.)
638 // to become accessible from within the Controller and Model functions.
639 // Only needed when running PHP 5
640
641 if ($this->_ci_is_instance())
642 {
643 $CI =& get_instance();
644 foreach (get_object_vars($CI) as $key => $var)
645 {
646 if ( ! isset($this->$key))
647 {
648 $this->$key =& $CI->$key;
649 }
650 }
651 }
652
653 /*
654 * Extract and cache variables
655 *
656 * You can either set variables using the dedicated $this->load_vars()
657 * function or via the second parameter of this function. We'll merge
658 * the two types and cache them so that views that are embedded within
659 * other views can have access to these variables.
660 */
Derek Jones47845f22008-01-16 23:23:02 +0000661 if (is_array($_ci_vars))
Derek Allard02658572007-12-20 14:00:50 +0000662 {
Derek Jones47845f22008-01-16 23:23:02 +0000663 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
Derek Allard02658572007-12-20 14:00:50 +0000664 }
665 extract($this->_ci_cached_vars);
666
667 /*
668 * Buffer the output
669 *
670 * We buffer the output for two reasons:
671 * 1. Speed. You get a significant speed boost.
672 * 2. So that the final rendered template can be
673 * post-processed by the output class. Why do we
674 * need post processing? For one thing, in order to
675 * show the elapsed page load time. Unless we
676 * can intercept the content right before it's sent to
677 * the browser and then stop the timer it won't be accurate.
678 */
679 ob_start();
680
681 // If the PHP installation does not support short tags we'll
682 // do a little string replacement, changing the short tags
683 // to standard PHP echo statements.
684
685 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
686 {
Derek Jones47845f22008-01-16 23:23:02 +0000687 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))).'<?php ');
Derek Allard02658572007-12-20 14:00:50 +0000688 }
689 else
690 {
Derek Jones47845f22008-01-16 23:23:02 +0000691 include($_ci_path);
Derek Allard02658572007-12-20 14:00:50 +0000692 }
693
Derek Jones47845f22008-01-16 23:23:02 +0000694 log_message('debug', 'File loaded: '.$_ci_path);
Derek Allard02658572007-12-20 14:00:50 +0000695
696 // Return the file data if requested
Derek Jones47845f22008-01-16 23:23:02 +0000697 if ($_ci_return === TRUE)
Derek Allard02658572007-12-20 14:00:50 +0000698 {
699 $buffer = ob_get_contents();
700 @ob_end_clean();
701 return $buffer;
702 }
703
704 /*
705 * Flush the buffer... or buff the flusher?
706 *
707 * In order to permit views to be nested within
708 * other views, we need to flush the content back out whenever
709 * we are beyond the first level of output buffering so that
710 * it can be seen and included properly by the first included
711 * template and any subsequent ones. Oy!
712 *
713 */
714 if (ob_get_level() > $this->_ci_ob_level + 1)
715 {
716 ob_end_flush();
717 }
718 else
719 {
720 // PHP 4 requires that we use a global
721 global $OUT;
722 $OUT->set_output(ob_get_contents());
723 @ob_end_clean();
724 }
725 }
726
727 // --------------------------------------------------------------------
728
729 /**
730 * Load class
731 *
732 * This function loads the requested class.
733 *
734 * @access private
735 * @param string the item that is being loaded
736 * @param mixed any additional parameters
737 * @return void
738 */
739 function _ci_load_class($class, $params = NULL)
740 {
741 // Get the class name
742 $class = str_replace(EXT, '', $class);
743
744 // We'll test for both lowercase and capitalized versions of the file name
745 foreach (array(ucfirst($class), strtolower($class)) as $class)
746 {
747 $subclass = APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT;
748
749 // Is this a class extension request?
750 if (file_exists($subclass))
751 {
752 $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
753
754 if ( ! file_exists($baseclass))
755 {
756 log_message('error', "Unable to load the requested class: ".$class);
757 show_error("Unable to load the requested class: ".$class);
758 }
759
760 // Safety: Was the class already loaded by a previous call?
761 if (in_array($subclass, $this->_ci_classes))
762 {
763 $is_duplicate = TRUE;
764 log_message('debug', $class." class already loaded. Second attempt ignored.");
765 return;
766 }
767
768 include($baseclass);
769 include($subclass);
770 $this->_ci_classes[] = $subclass;
771
772 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params);
773 }
774
775 // Lets search for the requested library file and load it.
776 $is_duplicate = FALSE;
777 for ($i = 1; $i < 3; $i++)
778 {
779 $path = ($i % 2) ? APPPATH : BASEPATH;
780 $filepath = $path.'libraries/'.$class.EXT;
781
782 // Does the file exist? No? Bummer...
783 if ( ! file_exists($filepath))
784 {
785 continue;
786 }
787
788 // Safety: Was the class already loaded by a previous call?
789 if (in_array($filepath, $this->_ci_classes))
790 {
791 $is_duplicate = TRUE;
792 log_message('debug', $class." class already loaded. Second attempt ignored.");
793 return;
794 }
795
796 include($filepath);
797 $this->_ci_classes[] = $filepath;
798 return $this->_ci_init_class($class, '', $params);
799 }
800 } // END FOREACH
801
802 // If we got this far we were unable to find the requested class.
803 // We do not issue errors if the load call failed due to a duplicate request
804 if ($is_duplicate == FALSE)
805 {
806 log_message('error', "Unable to load the requested class: ".$class);
807 show_error("Unable to load the requested class: ".$class);
808 }
809 }
810
811 // --------------------------------------------------------------------
812
813 /**
814 * Instantiates a class
815 *
816 * @access private
817 * @param string
818 * @param string
819 * @return null
820 */
821 function _ci_init_class($class, $prefix = '', $config = FALSE)
822 {
Derek Allard0796b8d2008-01-17 03:41:37 +0000823 $class = strtolower($class);
824
Derek Allard02658572007-12-20 14:00:50 +0000825 // Is there an associated config file for this class?
826 if ($config === NULL)
827 {
828 if (file_exists(APPPATH.'config/'.$class.EXT))
829 {
830 include(APPPATH.'config/'.$class.EXT);
831 }
832 }
833
834 if ($prefix == '')
835 {
836 $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
837 }
838 else
839 {
840 $name = $prefix.$class;
841 }
842
Derek Allard0796b8d2008-01-17 03:41:37 +0000843 // Set the variable name we will assign the class to
Derek Allard02658572007-12-20 14:00:50 +0000844 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
845
846 // Instantiate the class
847 $CI =& get_instance();
848 if ($config !== NULL)
849 {
850 $CI->$classvar = new $name($config);
851 }
852 else
853 {
854 $CI->$classvar = new $name;
855 }
856 }
857
858 // --------------------------------------------------------------------
859
860 /**
861 * Autoloader
862 *
863 * The config/autoload.php file contains an array that permits sub-systems,
864 * libraries, plugins, and helpers to be loaded automatically.
865 *
866 * @access private
867 * @param array
868 * @return void
869 */
870 function _ci_autoloader()
871 {
872 include(APPPATH.'config/autoload'.EXT);
873
874 if ( ! isset($autoload))
875 {
876 return FALSE;
877 }
878
879 // Load any custom config file
880 if (count($autoload['config']) > 0)
881 {
882 $CI =& get_instance();
883 foreach ($autoload['config'] as $key => $val)
884 {
885 $CI->config->load($val);
886 }
887 }
888
889 // Autoload plugins, helpers, scripts and languages
890 foreach (array('helper', 'plugin', 'script', 'language') as $type)
891 {
892 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
893 {
894 $this->$type($autoload[$type]);
895 }
896 }
897
898 // Autoload models
899 if (isset($autoload['model']))
900 {
901 $this->model($autoload['model']);
902 }
903
904 // A little tweak to remain backward compatible
905 // The $autoload['core'] item was deprecated
906 if ( ! isset($autoload['libraries']))
907 {
908 $autoload['libraries'] = $autoload['core'];
909 }
910
911 // Load libraries
912 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
913 {
914 // Load the database driver.
915 if (in_array('database', $autoload['libraries']))
916 {
917 $this->database();
918 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
919 }
920
921 // Load scaffolding
922 if (in_array('scaffolding', $autoload['libraries']))
923 {
924 $this->scaffolding();
925 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
926 }
927
928 // Load all other libraries
929 foreach ($autoload['libraries'] as $item)
930 {
931 $this->library($item);
932 }
933
934 // Load the model class.
935 if (in_array('model', $autoload['libraries']))
936 {
937 $this->model();
938 $autoload['libraries'] = array_diff($autoload['libraries'], array('model'));
939 }
940 }
941 }
942
943 // --------------------------------------------------------------------
944
945 /**
946 * Assign to Models
947 *
948 * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
949 * will be available to models, if any exist.
950 *
951 * @access private
952 * @param object
953 * @return array
954 */
955 function _ci_assign_to_models()
956 {
957 if (count($this->_ci_models) == 0)
958 {
959 return;
960 }
961
962 if ($this->_ci_is_instance())
963 {
964 $CI =& get_instance();
965 foreach ($this->_ci_models as $model)
966 {
967 $CI->$model->_assign_libraries();
968 }
969 }
970 else
971 {
972 foreach ($this->_ci_models as $model)
973 {
974 $this->$model->_assign_libraries();
975 }
976 }
977 }
978
979 // --------------------------------------------------------------------
980
981 /**
982 * Object to Array
983 *
984 * Takes an object as input and converts the class variables to array key/vals
985 *
986 * @access private
987 * @param object
988 * @return array
989 */
990 function _ci_object_to_array($object)
991 {
992 return (is_object($object)) ? get_object_vars($object) : $object;
993 }
994
995 // --------------------------------------------------------------------
996
997 /**
998 * Determines whether we should use the CI instance or $this
999 *
1000 * @access private
1001 * @return bool
1002 */
1003 function _ci_is_instance()
1004 {
1005 if ($this->_ci_is_php5 == TRUE)
1006 {
1007 return TRUE;
1008 }
1009
1010 global $CI;
1011 return (is_object($CI)) ? TRUE : FALSE;
1012 }
1013
1014}
1015?>