blob: e64006e93caff1e8749258b98e9af1d8b60b14f1 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +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 ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://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 ExpressionEngine Dev Team
26 * @category Loader
27 * @link http://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 = '';
Derek Jones32bf1862010-03-02 13:46:07 -060034 var $_ci_library_paths = array();
35 var $_ci_model_paths = array();
36 var $_ci_helper_paths = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000037 var $_ci_is_instance = FALSE; // Whether we should use $this or $CI =& get_instance()
Derek Jones32bf1862010-03-02 13:46:07 -060038 var $_base_classes = array(); // Set by the controller class
Derek Allard2067d1a2008-11-13 22:59:24 +000039 var $_ci_cached_vars = array();
40 var $_ci_classes = array();
41 var $_ci_loaded_files = array();
42 var $_ci_models = array();
43 var $_ci_helpers = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000044 var $_ci_varmap = array('unit_test' => 'unit', 'user_agent' => 'agent');
45
46
47 /**
48 * Constructor
49 *
50 * Sets the path to the view files and gets the initial output buffering level
51 *
52 * @access public
53 */
54 function CI_Loader()
55 {
Derek Allard2067d1a2008-11-13 22:59:24 +000056 $this->_ci_view_path = APPPATH.'views/';
57 $this->_ci_ob_level = ob_get_level();
Derek Jones32bf1862010-03-02 13:46:07 -060058 $this->_ci_library_paths = array(APPPATH, BASEPATH);
59 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
60 $this->_ci_model_paths = array(APPPATH);
61
Derek Allard2067d1a2008-11-13 22:59:24 +000062 log_message('debug', "Loader Class Initialized");
63 }
64
65 // --------------------------------------------------------------------
66
67 /**
68 * Class Loader
69 *
70 * This function lets users load and instantiate classes.
71 * It is designed to be called from a user's app controllers.
72 *
73 * @access public
74 * @param string the name of the class
75 * @param mixed the optional parameters
76 * @param string an optional object name
77 * @return void
78 */
79 function library($library = '', $params = NULL, $object_name = NULL)
80 {
Greg Akerce433962010-10-12 09:29:35 -050081 if (is_array($library))
82 {
83 foreach($library as $read)
84 {
85 $this->library($read);
86 }
87
88 return;
89 }
90
Derek Jones32bf1862010-03-02 13:46:07 -060091 if ($library == '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
93 return FALSE;
94 }
95
Derek Jones32bf1862010-03-02 13:46:07 -060096 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
98 $params = NULL;
99 }
100
101 if (is_array($library))
102 {
103 foreach ($library as $class)
104 {
105 $this->_ci_load_class($class, $params, $object_name);
106 }
107 }
108 else
109 {
110 $this->_ci_load_class($library, $params, $object_name);
111 }
112
113 $this->_ci_assign_to_models();
114 }
115
116 // --------------------------------------------------------------------
117
118 /**
119 * Model Loader
120 *
121 * This function lets users load and instantiate models.
122 *
123 * @access public
124 * @param string the name of the class
125 * @param string name for the model
126 * @param bool database connection
127 * @return void
128 */
129 function model($model, $name = '', $db_conn = FALSE)
130 {
131 if (is_array($model))
132 {
133 foreach($model as $babe)
134 {
135 $this->model($babe);
136 }
137 return;
138 }
139
140 if ($model == '')
141 {
142 return;
143 }
144
Derek Jones32bf1862010-03-02 13:46:07 -0600145 $path = '';
146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600148 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
Derek Jones32bf1862010-03-02 13:46:07 -0600150 // The path is in front of the last slash
151 $path = substr($model, 0, $last_slash + 1);
152
153 // And the model name behind it
154 $model = substr($model, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
156
157 if ($name == '')
158 {
159 $name = $model;
160 }
161
162 if (in_array($name, $this->_ci_models, TRUE))
163 {
164 return;
165 }
166
167 $CI =& get_instance();
168 if (isset($CI->$name))
169 {
170 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
171 }
172
173 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000174
Derek Jones32bf1862010-03-02 13:46:07 -0600175 foreach ($this->_ci_model_paths as $mod_path)
176 {
177 if ( ! file_exists($mod_path.'models/'.$path.$model.EXT))
178 {
179 continue;
180 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000181
Derek Jones32bf1862010-03-02 13:46:07 -0600182 if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
183 {
184 if ($db_conn === TRUE)
185 $db_conn = '';
186
187 $CI->load->database($db_conn, FALSE, TRUE);
188 }
189
Greg Akerbce13482010-10-11 15:37:16 -0500190 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600191 {
192 load_class('Model', 'core');
193 }
194
195 require_once($mod_path.'models/'.$path.$model.EXT);
196
197 $model = ucfirst($model);
198
199 $CI->$name = new $model();
200 $CI->$name->_assign_libraries();
201
202 $this->_ci_models[] = $name;
203 return;
204 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000205
Derek Jones32bf1862010-03-02 13:46:07 -0600206 // couldn't find the model
207 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Database Loader
214 *
215 * @access public
216 * @param string the DB credentials
217 * @param bool whether to return the DB object
218 * @param bool whether to enable active record (this allows us to override the config setting)
219 * @return object
220 */
Derek Jonesffca6c22009-12-05 15:31:44 +0000221 function database($params = '', $return = FALSE, $active_record = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 // Grab the super object
224 $CI =& get_instance();
225
226 // Do we even need to load the database class?
Derek Jones9fb6dd12009-12-05 15:32:48 +0000227 if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db))
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
229 return FALSE;
230 }
231
232 require_once(BASEPATH.'database/DB'.EXT);
233
234 if ($return === TRUE)
235 {
236 return DB($params, $active_record);
237 }
238
239 // Initialize the db variable. Needed to prevent
240 // reference errors with some configurations
241 $CI->db = '';
242
243 // Load the DB class
244 $CI->db =& DB($params, $active_record);
245
246 // Assign the DB object to any existing models
247 $this->_ci_assign_to_models();
248 }
249
250 // --------------------------------------------------------------------
251
252 /**
253 * Load the Utilities Class
254 *
255 * @access public
256 * @return string
257 */
258 function dbutil()
259 {
260 if ( ! class_exists('CI_DB'))
261 {
262 $this->database();
263 }
264
265 $CI =& get_instance();
266
267 // for backwards compatibility, load dbforge so we can extend dbutils off it
268 // this use is deprecated and strongly discouraged
269 $CI->load->dbforge();
270
271 require_once(BASEPATH.'database/DB_utility'.EXT);
272 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT);
273 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
274
Derek Jonesf0a9b332009-07-29 14:19:18 +0000275 $CI->dbutil =& instantiate_class(new $class());
Derek Allard2067d1a2008-11-13 22:59:24 +0000276
277 $CI->load->_ci_assign_to_models();
278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * Load the Database Forge Class
284 *
285 * @access public
286 * @return string
287 */
288 function dbforge()
289 {
290 if ( ! class_exists('CI_DB'))
291 {
292 $this->database();
293 }
294
295 $CI =& get_instance();
296
297 require_once(BASEPATH.'database/DB_forge'.EXT);
298 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge'.EXT);
299 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
300
301 $CI->dbforge = new $class();
302
303 $CI->load->_ci_assign_to_models();
304 }
305
306 // --------------------------------------------------------------------
307
308 /**
309 * Load View
310 *
311 * This function is used to load a "view" file. It has three parameters:
312 *
313 * 1. The name of the "view" file to be included.
314 * 2. An associative array of data to be extracted for use in the view.
315 * 3. TRUE/FALSE - whether to return the data or load it. In
316 * some cases it's advantageous to be able to return data so that
317 * a developer can process it in some way.
318 *
319 * @access public
320 * @param string
321 * @param array
322 * @param bool
323 * @return void
324 */
325 function view($view, $vars = array(), $return = FALSE)
326 {
327 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
328 }
329
330 // --------------------------------------------------------------------
331
332 /**
333 * Load File
334 *
335 * This is a generic file loader
336 *
337 * @access public
338 * @param string
339 * @param bool
340 * @return string
341 */
342 function file($path, $return = FALSE)
343 {
344 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
345 }
346
347 // --------------------------------------------------------------------
348
349 /**
350 * Set Variables
351 *
352 * Once variables are set they become available within
353 * the controller class and its "view" files.
354 *
355 * @access public
356 * @param array
357 * @return void
358 */
359 function vars($vars = array(), $val = '')
360 {
361 if ($val != '' AND is_string($vars))
362 {
363 $vars = array($vars => $val);
364 }
365
366 $vars = $this->_ci_object_to_array($vars);
367
368 if (is_array($vars) AND count($vars) > 0)
369 {
370 foreach ($vars as $key => $val)
371 {
372 $this->_ci_cached_vars[$key] = $val;
373 }
374 }
375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Load Helper
381 *
382 * This function loads the specified helper file.
383 *
384 * @access public
385 * @param mixed
386 * @return void
387 */
388 function helper($helpers = array())
Derek Jones32bf1862010-03-02 13:46:07 -0600389 {
390 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
391 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 if (isset($this->_ci_helpers[$helper]))
393 {
394 continue;
395 }
Derek Jones32bf1862010-03-02 13:46:07 -0600396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.EXT;
398
399 // Is this a helper extension request?
400 if (file_exists($ext_helper))
401 {
402 $base_helper = BASEPATH.'helpers/'.$helper.EXT;
403
404 if ( ! file_exists($base_helper))
405 {
406 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
407 }
408
409 include_once($ext_helper);
410 include_once($base_helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600411
412 $this->_ci_helpers[$helper] = TRUE;
413 log_message('debug', 'Helper loaded: '.$helper);
414 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
Derek Jones32bf1862010-03-02 13:46:07 -0600416
417 // Try to load the helper
418 foreach ($this->_ci_helper_paths as $path)
419 {
420 if (file_exists($path.'helpers/'.$helper.EXT))
421 {
422 include_once($path.'helpers/'.$helper.EXT);
423
424 $this->_ci_helpers[$helper] = TRUE;
425 log_message('debug', 'Helper loaded: '.$helper);
426 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
428 }
429
Derek Jones32bf1862010-03-02 13:46:07 -0600430 // unable to load the helper
431 if ( ! isset($this->_ci_helpers[$helper]))
432 {
433 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
434 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 }
436 }
437
438 // --------------------------------------------------------------------
439
440 /**
441 * Load Helpers
442 *
443 * This is simply an alias to the above function in case the
444 * user has written the plural form of this function.
445 *
446 * @access public
447 * @param array
448 * @return void
449 */
450 function helpers($helpers = array())
451 {
452 $this->helper($helpers);
453 }
454
455 // --------------------------------------------------------------------
456
457 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 * Loads a language file
459 *
460 * @access public
461 * @param array
462 * @param string
463 * @return void
464 */
465 function language($file = array(), $lang = '')
466 {
467 $CI =& get_instance();
468
469 if ( ! is_array($file))
470 {
471 $file = array($file);
472 }
473
474 foreach ($file as $langfile)
475 {
476 $CI->lang->load($langfile, $lang);
477 }
478 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000479
480 // --------------------------------------------------------------------
481
482 /**
483 * Loads a config file
484 *
485 * @access public
486 * @param string
487 * @return void
488 */
489 function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
490 {
491 $CI =& get_instance();
492 $CI->config->load($file, $use_sections, $fail_gracefully);
493 }
494
495 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 /**
Derek Jones8dca0412010-03-05 13:01:44 -0600498 * Driver
499 *
500 * Loads a driver library
501 *
502 * @param string the name of the class
503 * @param mixed the optional parameters
504 * @param string an optional object name
505 * @return void
506 */
507 function driver($library = '', $params = NULL, $object_name = NULL)
508 {
509 if ( ! class_exists('CI_Driver_Library'))
510 {
511 // we aren't instantiating an object here, that'll be done by the Library itself
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600512 require BASEPATH.'libraries/Driver'.EXT;
513 }
514
515 // We can save the loader some time since Drivers will *always* be in a subfolder,
516 // and typically identically named to the library
517 if ( ! strpos($library, '/'))
518 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500519 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600520 }
521
522 return $this->library($library, $params, $object_name);
523 }
524
525 // --------------------------------------------------------------------
526
527 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600528 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 *
Derek Jones32bf1862010-03-02 13:46:07 -0600530 * Prepends a parent path to the library, model, helper, and config path arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 *
532 * @access public
533 * @param string
534 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600535 */
536 function add_package_path($path)
537 {
538 array_unshift($this->_ci_library_paths, $path);
539 array_unshift($this->_ci_model_paths, $path);
540 array_unshift($this->_ci_helper_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000541
Derek Jones32bf1862010-03-02 13:46:07 -0600542 // Add config file path
543 $config =& $this->_ci_get_component('config');
544 array_unshift($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 }
546
547 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600548
549 /**
550 * Remove Package Path
551 *
552 * Remove a path from the library, model, and helper path arrays if it exists
553 * If no path is provided, the most recently added path is removed.
554 *
555 * @access public
556 * @param type
557 * @return type
558 */
559 function remove_package_path($path = '', $remove_config_path = TRUE)
560 {
561 $config =& $this->_ci_get_component('config');
Derek Allard2067d1a2008-11-13 22:59:24 +0000562
Derek Jones32bf1862010-03-02 13:46:07 -0600563 if ($path == '')
564 {
565 $void = array_shift($this->_ci_library_paths);
566 $void = array_shift($this->_ci_model_paths);
567 $void = array_shift($this->_ci_helper_paths);
568 $void = array_shift($config->_config_paths);
569 }
570 else
571 {
572 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
573 {
574 if (($key = array_search($path, $this->{$var})) !== FALSE)
575 {
576 unset($this->{$var}[$key]);
577 }
578 }
579
580 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
581 {
582 unset($config->_config_paths[$key]);
583 }
584 }
585
586 // make sure the application default paths are still in the array
587 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
588 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
589 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
590 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
591 }
592
593 // --------------------------------------------------------------------
594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 /**
596 * Loader
597 *
598 * This function is used to load views and files.
599 * Variables are prefixed with _ci_ to avoid symbol collision with
600 * variables made available to view files
601 *
602 * @access private
603 * @param array
604 * @return void
605 */
606 function _ci_load($_ci_data)
607 {
608 // Set the default data variables
609 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
610 {
611 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
612 }
613
614 // Set the path to the requested file
615 if ($_ci_path == '')
616 {
617 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
618 $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view;
619 $_ci_path = $this->_ci_view_path.$_ci_file;
620 }
621 else
622 {
623 $_ci_x = explode('/', $_ci_path);
624 $_ci_file = end($_ci_x);
625 }
626
627 if ( ! file_exists($_ci_path))
628 {
629 show_error('Unable to load the requested file: '.$_ci_file);
630 }
631
632 // This allows anything loaded using $this->load (views, files, etc.)
633 // to become accessible from within the Controller and Model functions.
634 // Only needed when running PHP 5
635
636 if ($this->_ci_is_instance())
637 {
638 $_ci_CI =& get_instance();
639 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
640 {
641 if ( ! isset($this->$_ci_key))
642 {
643 $this->$_ci_key =& $_ci_CI->$_ci_key;
644 }
645 }
646 }
647
648 /*
649 * Extract and cache variables
650 *
651 * You can either set variables using the dedicated $this->load_vars()
652 * function or via the second parameter of this function. We'll merge
653 * the two types and cache them so that views that are embedded within
654 * other views can have access to these variables.
655 */
656 if (is_array($_ci_vars))
657 {
658 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
659 }
660 extract($this->_ci_cached_vars);
661
662 /*
663 * Buffer the output
664 *
665 * We buffer the output for two reasons:
666 * 1. Speed. You get a significant speed boost.
667 * 2. So that the final rendered template can be
668 * post-processed by the output class. Why do we
669 * need post processing? For one thing, in order to
670 * show the elapsed page load time. Unless we
671 * can intercept the content right before it's sent to
672 * the browser and then stop the timer it won't be accurate.
673 */
674 ob_start();
675
676 // If the PHP installation does not support short tags we'll
677 // do a little string replacement, changing the short tags
678 // to standard PHP echo statements.
679
680 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
681 {
682 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
683 }
684 else
685 {
686 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
687 }
688
689 log_message('debug', 'File loaded: '.$_ci_path);
690
691 // Return the file data if requested
692 if ($_ci_return === TRUE)
693 {
694 $buffer = ob_get_contents();
695 @ob_end_clean();
696 return $buffer;
697 }
698
699 /*
700 * Flush the buffer... or buff the flusher?
701 *
702 * In order to permit views to be nested within
703 * other views, we need to flush the content back out whenever
704 * we are beyond the first level of output buffering so that
705 * it can be seen and included properly by the first included
706 * template and any subsequent ones. Oy!
707 *
708 */
709 if (ob_get_level() > $this->_ci_ob_level + 1)
710 {
711 ob_end_flush();
712 }
713 else
714 {
715 // PHP 4 requires that we use a global
716 global $OUT;
717 $OUT->append_output(ob_get_contents());
718 @ob_end_clean();
719 }
720 }
721
722 // --------------------------------------------------------------------
723
724 /**
725 * Load class
726 *
727 * This function loads the requested class.
728 *
729 * @access private
730 * @param string the item that is being loaded
731 * @param mixed any additional parameters
732 * @param string an optional object name
733 * @return void
734 */
735 function _ci_load_class($class, $params = NULL, $object_name = NULL)
736 {
737 // Get the class name, and while we're at it trim any slashes.
738 // The directory path can be included as part of the class name,
739 // but we don't want a leading slash
740 $class = str_replace(EXT, '', trim($class, '/'));
741
742 // Was the path included with the class name?
743 // We look for a slash to determine this
744 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600745 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 {
Derek Jones32bf1862010-03-02 13:46:07 -0600747 // Extract the path
748 $subdir = substr($class, 0, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000749
Derek Jones32bf1862010-03-02 13:46:07 -0600750 // Get the filename from the path
751 $class = substr($class, $last_slash + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 }
753
754 // We'll test for both lowercase and capitalized versions of the file name
755 foreach (array(ucfirst($class), strtolower($class)) as $class)
756 {
757 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.EXT;
758
759 // Is this a class extension request?
760 if (file_exists($subclass))
761 {
762 $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
763
764 if ( ! file_exists($baseclass))
765 {
766 log_message('error', "Unable to load the requested class: ".$class);
767 show_error("Unable to load the requested class: ".$class);
768 }
769
770 // Safety: Was the class already loaded by a previous call?
771 if (in_array($subclass, $this->_ci_loaded_files))
772 {
773 // Before we deem this to be a duplicate request, let's see
774 // if a custom object name is being supplied. If so, we'll
775 // return a new instance of the object
776 if ( ! is_null($object_name))
777 {
778 $CI =& get_instance();
779 if ( ! isset($CI->$object_name))
780 {
781 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
782 }
783 }
784
785 $is_duplicate = TRUE;
786 log_message('debug', $class." class already loaded. Second attempt ignored.");
787 return;
788 }
789
790 include_once($baseclass);
791 include_once($subclass);
792 $this->_ci_loaded_files[] = $subclass;
793
794 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
795 }
796
797 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600798 $is_duplicate = FALSE;
799 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 $filepath = $path.'libraries/'.$subdir.$class.EXT;
Derek Jones32bf1862010-03-02 13:46:07 -0600802
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 // Does the file exist? No? Bummer...
804 if ( ! file_exists($filepath))
805 {
806 continue;
807 }
808
809 // Safety: Was the class already loaded by a previous call?
810 if (in_array($filepath, $this->_ci_loaded_files))
811 {
812 // Before we deem this to be a duplicate request, let's see
813 // if a custom object name is being supplied. If so, we'll
814 // return a new instance of the object
815 if ( ! is_null($object_name))
816 {
817 $CI =& get_instance();
818 if ( ! isset($CI->$object_name))
819 {
820 return $this->_ci_init_class($class, '', $params, $object_name);
821 }
822 }
823
824 $is_duplicate = TRUE;
825 log_message('debug', $class." class already loaded. Second attempt ignored.");
826 return;
827 }
828
829 include_once($filepath);
830 $this->_ci_loaded_files[] = $filepath;
Derek Jones32bf1862010-03-02 13:46:07 -0600831 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 }
Derek Jones32bf1862010-03-02 13:46:07 -0600833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 } // END FOREACH
835
836 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
837 if ($subdir == '')
838 {
839 $path = strtolower($class).'/'.$class;
840 return $this->_ci_load_class($path, $params);
841 }
842
843 // If we got this far we were unable to find the requested class.
844 // We do not issue errors if the load call failed due to a duplicate request
845 if ($is_duplicate == FALSE)
846 {
847 log_message('error', "Unable to load the requested class: ".$class);
848 show_error("Unable to load the requested class: ".$class);
849 }
850 }
851
852 // --------------------------------------------------------------------
853
854 /**
855 * Instantiates a class
856 *
857 * @access private
858 * @param string
859 * @param string
860 * @param string an optional object name
861 * @return null
862 */
863 function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
864 {
Derek Jones32bf1862010-03-02 13:46:07 -0600865 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 if ($config === NULL)
867 {
868 // We test for both uppercase and lowercase, for servers that
869 // are case-sensitive with regard to file names
870 if (file_exists(APPPATH.'config/'.strtolower($class).EXT))
871 {
872 include_once(APPPATH.'config/'.strtolower($class).EXT);
873 }
Derek Jonesc8dddd92009-07-10 18:58:03 +0000874 elseif (file_exists(APPPATH.'config/'.ucfirst(strtolower($class)).EXT))
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 {
Derek Jonesc8dddd92009-07-10 18:58:03 +0000876 include_once(APPPATH.'config/'.ucfirst(strtolower($class)).EXT);
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 }
878 }
879
880 if ($prefix == '')
881 {
882 if (class_exists('CI_'.$class))
883 {
884 $name = 'CI_'.$class;
885 }
886 elseif (class_exists(config_item('subclass_prefix').$class))
887 {
888 $name = config_item('subclass_prefix').$class;
889 }
890 else
891 {
892 $name = $class;
893 }
894 }
895 else
896 {
897 $name = $prefix.$class;
898 }
899
900 // Is the class name valid?
901 if ( ! class_exists($name))
902 {
903 log_message('error', "Non-existent class: ".$name);
904 show_error("Non-existent class: ".$class);
905 }
906
907 // Set the variable name we will assign the class to
908 // Was a custom class name supplied? If so we'll use it
909 $class = strtolower($class);
910
911 if (is_null($object_name))
912 {
913 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
914 }
915 else
916 {
917 $classvar = $object_name;
918 }
919
920 // Save the class name and object name
921 $this->_ci_classes[$class] = $classvar;
922
923 // Instantiate the class
924 $CI =& get_instance();
925 if ($config !== NULL)
926 {
927 $CI->$classvar = new $name($config);
928 }
929 else
930 {
931 $CI->$classvar = new $name;
932 }
933 }
934
935 // --------------------------------------------------------------------
936
937 /**
938 * Autoloader
939 *
940 * The config/autoload.php file contains an array that permits sub-systems,
Derek Jonesc6da5032010-03-09 20:44:27 -0600941 * libraries, and helpers to be loaded automatically.
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 *
943 * @access private
944 * @param array
945 * @return void
946 */
947 function _ci_autoloader()
948 {
949 include_once(APPPATH.'config/autoload'.EXT);
950
951 if ( ! isset($autoload))
952 {
953 return FALSE;
954 }
955
956 // Load any custom config file
957 if (count($autoload['config']) > 0)
958 {
959 $CI =& get_instance();
960 foreach ($autoload['config'] as $key => $val)
961 {
962 $CI->config->load($val);
963 }
964 }
965
Derek Jonesc6da5032010-03-09 20:44:27 -0600966 // Autoload helpers and languages
967 foreach (array('helper', 'language') as $type)
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 {
969 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
970 {
971 $this->$type($autoload[$type]);
972 }
973 }
974
975 // A little tweak to remain backward compatible
976 // The $autoload['core'] item was deprecated
Derek Jones32bf1862010-03-02 13:46:07 -0600977 if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 {
979 $autoload['libraries'] = $autoload['core'];
980 }
981
982 // Load libraries
983 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
984 {
985 // Load the database driver.
986 if (in_array('database', $autoload['libraries']))
987 {
988 $this->database();
989 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
990 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000991
992 // Load all other libraries
993 foreach ($autoload['libraries'] as $item)
994 {
995 $this->library($item);
996 }
997 }
998
999 // Autoload models
1000 if (isset($autoload['model']))
1001 {
1002 $this->model($autoload['model']);
1003 }
1004
1005 }
1006
1007 // --------------------------------------------------------------------
1008
1009 /**
1010 * Assign to Models
1011 *
Derek Jonesc6da5032010-03-09 20:44:27 -06001012 * Makes sure that anything loaded by the loader class (libraries, etc.)
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 * will be available to models, if any exist.
1014 *
1015 * @access private
1016 * @param object
1017 * @return array
1018 */
1019 function _ci_assign_to_models()
1020 {
1021 if (count($this->_ci_models) == 0)
1022 {
1023 return;
1024 }
1025
Derek Jones32bf1862010-03-02 13:46:07 -06001026 foreach($this->_ci_models as $model)
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 {
Derek Jones32bf1862010-03-02 13:46:07 -06001028 $model = $this->_ci_get_component($model);
1029 $model->_assign_libraries();
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 }
1031 }
1032
1033 // --------------------------------------------------------------------
1034
1035 /**
1036 * Object to Array
1037 *
1038 * Takes an object as input and converts the class variables to array key/vals
1039 *
1040 * @access private
1041 * @param object
1042 * @return array
1043 */
1044 function _ci_object_to_array($object)
1045 {
1046 return (is_object($object)) ? get_object_vars($object) : $object;
1047 }
1048
1049 // --------------------------------------------------------------------
1050
1051 /**
1052 * Determines whether we should use the CI instance or $this
Derek Jones32bf1862010-03-02 13:46:07 -06001053 * @PHP4
1054 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 * @access private
1056 * @return bool
1057 */
1058 function _ci_is_instance()
1059 {
Derek Jones32bf1862010-03-02 13:46:07 -06001060 if (is_php('5.0.0') == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 {
1062 return TRUE;
1063 }
1064
1065 global $CI;
1066 return (is_object($CI)) ? TRUE : FALSE;
1067 }
Derek Jones32bf1862010-03-02 13:46:07 -06001068
1069 // --------------------------------------------------------------------
1070
1071 /**
1072 * Get a reference to a specific library or model
1073 *
1074 * @access private
1075 * @return bool
1076 */
1077 function &_ci_get_component($component)
1078 {
1079 if ($this->_ci_is_instance())
1080 {
1081 $CI =& get_instance();
1082 return $CI->$component;
1083 }
1084 else
1085 {
1086 return $this->$component;
1087 }
1088 }
1089
1090 // --------------------------------------------------------------------
1091
1092 /**
1093 * Prep filename
1094 *
1095 * This function preps the name of various items to make loading them more reliable.
1096 *
1097 * @access private
1098 * @param mixed
1099 * @return array
1100 */
1101 function _ci_prep_filename($filename, $extension)
1102 {
1103 if ( ! is_array($filename))
1104 {
1105 return array(strtolower(str_replace(EXT, '', str_replace($extension, '', $filename)).$extension));
1106 }
1107 else
1108 {
1109 foreach ($filename as $key => $val)
1110 {
1111 $filename[$key] = strtolower(str_replace(EXT, '', str_replace($extension, '', $val)).$extension);
1112 }
1113
1114 return $filename;
1115 }
1116 }
1117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118
1119}
1120
1121/* End of file Loader.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -06001122/* Location: ./system/core/Loader.php */