blob: e090cc09ca6de71f5c890310ec01154aee91d820 [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard02658572007-12-20 14:00:50 +00002/**
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
Rick Ellis6ad53292008-08-21 18:31:28 +000072 * @param string an optional object name
Derek Allard02658572007-12-20 14:00:50 +000073 * @return void
74 */
Rick Ellis6ad53292008-08-21 18:31:28 +000075 function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard02658572007-12-20 14:00:50 +000076 {
77 if ($library == '')
78 {
79 return FALSE;
80 }
81
82 if (is_array($library))
83 {
84 foreach ($library as $class)
85 {
86 $this->_ci_load_class($class, $params);
87 }
88 }
89 else
90 {
Rick Ellis6ad53292008-08-21 18:31:28 +000091 $this->_ci_load_class($library, $params, $object_name);
Derek Allard02658572007-12-20 14:00:50 +000092 }
93
94 $this->_ci_assign_to_models();
95 }
96
97 // --------------------------------------------------------------------
98
99 /**
100 * Model Loader
101 *
102 * This function lets users load and instantiate models.
103 *
Derek Allard02658572007-12-20 14:00:50 +0000104 * @param string the name of the class
Derek Allard9736d3f2008-06-16 21:36:01 +0000105 * @param string name for the model
106 * @param bool database connection
Derek Allard02658572007-12-20 14:00:50 +0000107 * @return void
108 */
109 function model($model, $name = '', $db_conn = FALSE)
110 {
111 if (is_array($model))
112 {
113 foreach($model as $babe)
114 {
115 $this->model($babe);
116 }
117 return;
118 }
119
120 if ($model == '')
121 {
122 return;
123 }
124
125 // Is the model in a sub-folder? If so, parse out the filename and path.
126 if (strpos($model, '/') === FALSE)
127 {
128 $path = '';
129 }
130 else
131 {
132 $x = explode('/', $model);
133 $model = end($x);
134 unset($x[count($x)-1]);
135 $path = implode('/', $x).'/';
136 }
137
138 if ($name == '')
139 {
140 $name = $model;
141 }
142
143 if (in_array($name, $this->_ci_models, TRUE))
144 {
145 return;
146 }
147
148 $CI =& get_instance();
149 if (isset($CI->$name))
150 {
151 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
152 }
153
154 $model = strtolower($model);
155
Derek Jones0b59f272008-05-13 04:22:33 +0000156 if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
Derek Allard02658572007-12-20 14:00:50 +0000157 {
158 show_error('Unable to locate the model you have specified: '.$model);
159 }
160
161 if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
162 {
163 if ($db_conn === TRUE)
164 $db_conn = '';
165
166 $CI->load->database($db_conn, FALSE, TRUE);
167 }
168
Derek Jones0b59f272008-05-13 04:22:33 +0000169 if ( ! class_exists('Model'))
Derek Allard02658572007-12-20 14:00:50 +0000170 {
Derek Allardff943eb2008-01-22 18:08:10 +0000171 load_class('Model', FALSE);
Derek Allard02658572007-12-20 14:00:50 +0000172 }
173
174 require_once(APPPATH.'models/'.$path.$model.EXT);
175
176 $model = ucfirst($model);
177
178 $CI->$name = new $model();
179 $CI->$name->_assign_libraries();
180
181 $this->_ci_models[] = $name;
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Database Loader
188 *
189 * @access public
190 * @param string the DB credentials
191 * @param bool whether to return the DB object
192 * @param bool whether to enable active record (this allows us to override the config setting)
193 * @return object
194 */
195 function database($params = '', $return = FALSE, $active_record = FALSE)
196 {
Derek Jones72d61332008-01-30 20:52:22 +0000197 // Grab the super object
198 $CI =& get_instance();
199
Derek Allard02658572007-12-20 14:00:50 +0000200 // Do we even need to load the database class?
Derek Jones72d61332008-01-30 20:52:22 +0000201 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 +0000202 {
203 return FALSE;
204 }
205
206 require_once(BASEPATH.'database/DB'.EXT);
207
208 if ($return === TRUE)
209 {
210 return DB($params, $active_record);
211 }
Derek Allard02658572007-12-20 14:00:50 +0000212
213 // Initialize the db variable. Needed to prevent
214 // reference errors with some configurations
215 $CI->db = '';
216
217 // Load the DB class
218 $CI->db =& DB($params, $active_record);
219
220 // Assign the DB object to any existing models
221 $this->_ci_assign_to_models();
222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * Load the Utilities Class
228 *
229 * @access public
230 * @return string
231 */
232 function dbutil()
233 {
Derek Jones0b59f272008-05-13 04:22:33 +0000234 if ( ! class_exists('CI_DB'))
Derek Allard02658572007-12-20 14:00:50 +0000235 {
236 $this->database();
237 }
238
239 $CI =& get_instance();
Derek Allard39b622d2008-01-16 21:10:09 +0000240
241 // for backwards compatibility, load dbforge so we can extend dbutils off it
242 // this use is deprecated and strongly discouraged
243 $CI->load->dbforge();
Derek Allard02658572007-12-20 14:00:50 +0000244
245 require_once(BASEPATH.'database/DB_utility'.EXT);
246 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT);
247 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
248
Derek Allard39b622d2008-01-16 21:10:09 +0000249 $CI->dbutil =& new $class();
250
Derek Allard02658572007-12-20 14:00:50 +0000251 $CI->load->_ci_assign_to_models();
252 }
253
254 // --------------------------------------------------------------------
Derek Allard39b622d2008-01-16 21:10:09 +0000255
256 /**
257 * Load the Database Forge Class
258 *
259 * @access public
260 * @return string
261 */
262 function dbforge()
263 {
Derek Jones0b59f272008-05-13 04:22:33 +0000264 if ( ! class_exists('CI_DB'))
Derek Allard39b622d2008-01-16 21:10:09 +0000265 {
266 $this->database();
267 }
268
269 $CI =& get_instance();
270
271 require_once(BASEPATH.'database/DB_forge'.EXT);
272 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge'.EXT);
273 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
274
275 $CI->dbforge = new $class();
Derek Allardff390bd2008-05-06 00:37:12 +0000276
277 $CI->load->_ci_assign_to_models();
Derek Allard39b622d2008-01-16 21:10:09 +0000278 }
279
280 // --------------------------------------------------------------------
Derek Allard02658572007-12-20 14:00:50 +0000281
282 /**
283 * Load View
284 *
285 * This function is used to load a "view" file. It has three parameters:
286 *
287 * 1. The name of the "view" file to be included.
288 * 2. An associative array of data to be extracted for use in the view.
289 * 3. TRUE/FALSE - whether to return the data or load it. In
290 * some cases it's advantageous to be able to return data so that
291 * a developer can process it in some way.
292 *
293 * @access public
294 * @param string
295 * @param array
296 * @param bool
297 * @return void
298 */
299 function view($view, $vars = array(), $return = FALSE)
300 {
Derek Jones47845f22008-01-16 23:23:02 +0000301 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 +0000302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Load File
308 *
309 * This is a generic file loader
310 *
311 * @access public
312 * @param string
313 * @param bool
314 * @return string
315 */
316 function file($path, $return = FALSE)
317 {
Derek Jones47845f22008-01-16 23:23:02 +0000318 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
Derek Allard02658572007-12-20 14:00:50 +0000319 }
320
321 // --------------------------------------------------------------------
322
323 /**
324 * Set Variables
325 *
326 * Once variables are set they become available within
327 * the controller class and its "view" files.
328 *
329 * @access public
330 * @param array
331 * @return void
332 */
Rick Ellis6ad53292008-08-21 18:31:28 +0000333 function vars($vars = array(), $val = '')
Derek Allard02658572007-12-20 14:00:50 +0000334 {
Rick Ellis6ad53292008-08-21 18:31:28 +0000335 if ($val != '' AND is_string($vars))
336 {
337 $vars = array($vars => $val);
338 }
339
Derek Allard02658572007-12-20 14:00:50 +0000340 $vars = $this->_ci_object_to_array($vars);
341
342 if (is_array($vars) AND count($vars) > 0)
343 {
344 foreach ($vars as $key => $val)
345 {
346 $this->_ci_cached_vars[$key] = $val;
347 }
348 }
349 }
350
351 // --------------------------------------------------------------------
352
353 /**
354 * Load Helper
355 *
356 * This function loads the specified helper file.
357 *
358 * @access public
359 * @param mixed
360 * @return void
361 */
362 function helper($helpers = array())
363 {
Derek Jones0b59f272008-05-13 04:22:33 +0000364 if ( ! is_array($helpers))
Derek Allard02658572007-12-20 14:00:50 +0000365 {
366 $helpers = array($helpers);
367 }
368
369 foreach ($helpers as $helper)
370 {
371 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
372
373 if (isset($this->_ci_helpers[$helper]))
374 {
375 continue;
376 }
Derek Jones269b9422008-01-28 21:00:20 +0000377
378 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.EXT;
Derek Allard02658572007-12-20 14:00:50 +0000379
Derek Jones269b9422008-01-28 21:00:20 +0000380 // Is this a helper extension request?
381 if (file_exists($ext_helper))
382 {
383 $base_helper = BASEPATH.'helpers/'.$helper.EXT;
384
Derek Jones0b59f272008-05-13 04:22:33 +0000385 if ( ! file_exists($base_helper))
Derek Jones269b9422008-01-28 21:00:20 +0000386 {
387 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
388 }
389
390 include_once($ext_helper);
391 include_once($base_helper);
392 }
393 elseif (file_exists(APPPATH.'helpers/'.$helper.EXT))
Derek Allard02658572007-12-20 14:00:50 +0000394 {
395 include_once(APPPATH.'helpers/'.$helper.EXT);
396 }
397 else
398 {
399 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
400 {
Derek Allardd888c352008-03-18 04:04:33 +0000401 include_once(BASEPATH.'helpers/'.$helper.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000402 }
403 else
404 {
405 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
406 }
407 }
408
409 $this->_ci_helpers[$helper] = TRUE;
410
411 }
412
413 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
414 }
415
416 // --------------------------------------------------------------------
417
418 /**
419 * Load Helpers
420 *
421 * This is simply an alias to the above function in case the
422 * user has written the plural form of this function.
423 *
424 * @access public
425 * @param array
426 * @return void
427 */
428 function helpers($helpers = array())
429 {
430 $this->helper($helpers);
431 }
432
433 // --------------------------------------------------------------------
434
435 /**
436 * Load Plugin
437 *
438 * This function loads the specified plugin.
439 *
440 * @access public
441 * @param array
442 * @return void
443 */
444 function plugin($plugins = array())
445 {
Derek Jones0b59f272008-05-13 04:22:33 +0000446 if ( ! is_array($plugins))
Derek Allard02658572007-12-20 14:00:50 +0000447 {
448 $plugins = array($plugins);
449 }
450
451 foreach ($plugins as $plugin)
452 {
Derek Allard17f74062008-01-16 01:22:44 +0000453 $plugin = strtolower(str_replace(EXT, '', str_replace('_pi', '', $plugin)).'_pi');
Derek Allard02658572007-12-20 14:00:50 +0000454
455 if (isset($this->_ci_plugins[$plugin]))
456 {
457 continue;
458 }
459
460 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
461 {
Derek Allardd888c352008-03-18 04:04:33 +0000462 include_once(APPPATH.'plugins/'.$plugin.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000463 }
464 else
465 {
466 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
467 {
Derek Allardd888c352008-03-18 04:04:33 +0000468 include_once(BASEPATH.'plugins/'.$plugin.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000469 }
470 else
471 {
472 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
473 }
474 }
475
476 $this->_ci_plugins[$plugin] = TRUE;
477 }
478
479 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
480 }
481
482 // --------------------------------------------------------------------
483
484 /**
485 * Load Plugins
486 *
487 * This is simply an alias to the above function in case the
488 * user has written the plural form of this function.
489 *
490 * @access public
491 * @param array
492 * @return void
493 */
494 function plugins($plugins = array())
495 {
496 $this->plugin($plugins);
497 }
498
499 // --------------------------------------------------------------------
500
501 /**
502 * Load Script
503 *
504 * This function loads the specified include file from the
505 * application/scripts/ folder.
506 *
507 * NOTE: This feature has been deprecated but it will remain available
508 * for legacy users.
509 *
510 * @access public
511 * @param array
512 * @return void
513 */
514 function script($scripts = array())
515 {
Derek Jones0b59f272008-05-13 04:22:33 +0000516 if ( ! is_array($scripts))
Derek Allard02658572007-12-20 14:00:50 +0000517 {
518 $scripts = array($scripts);
519 }
520
521 foreach ($scripts as $script)
522 {
523 $script = strtolower(str_replace(EXT, '', $script));
524
525 if (isset($this->_ci_scripts[$script]))
526 {
527 continue;
528 }
529
Derek Jones0b59f272008-05-13 04:22:33 +0000530 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
Derek Allard02658572007-12-20 14:00:50 +0000531 {
532 show_error('Unable to load the requested script: scripts/'.$script.EXT);
533 }
534
Derek Allardd888c352008-03-18 04:04:33 +0000535 include_once(APPPATH.'scripts/'.$script.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000536 }
537
538 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
539 }
540
541 // --------------------------------------------------------------------
542
543 /**
544 * Loads a language file
545 *
546 * @access public
547 * @param array
548 * @param string
549 * @return void
550 */
551 function language($file = array(), $lang = '')
552 {
553 $CI =& get_instance();
554
Derek Jones0b59f272008-05-13 04:22:33 +0000555 if ( ! is_array($file))
Derek Allard02658572007-12-20 14:00:50 +0000556 {
557 $file = array($file);
558 }
559
560 foreach ($file as $langfile)
561 {
562 $CI->lang->load($langfile, $lang);
563 }
564 }
565
566 /**
567 * Loads language files for scaffolding
568 *
569 * @access public
570 * @param string
571 * @return arra
572 */
573 function scaffold_language($file = '', $lang = '', $return = FALSE)
574 {
575 $CI =& get_instance();
576 return $CI->lang->load($file, $lang, $return);
577 }
578
579 // --------------------------------------------------------------------
580
581 /**
582 * Loads a config file
583 *
584 * @access public
585 * @param string
586 * @return void
587 */
588 function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
589 {
590 $CI =& get_instance();
591 $CI->config->load($file, $use_sections, $fail_gracefully);
592 }
593
594 // --------------------------------------------------------------------
595
596 /**
597 * Scaffolding Loader
598 *
599 * This initializing function works a bit different than the
600 * others. It doesn't load the class. Instead, it simply
601 * sets a flag indicating that scaffolding is allowed to be
602 * used. The actual scaffolding function below is
603 * called by the front controller based on whether the
604 * second segment of the URL matches the "secret" scaffolding
605 * word stored in the application/config/routes.php
606 *
607 * @access public
608 * @param string
609 * @return void
610 */
611 function scaffolding($table = '')
612 {
613 if ($table === FALSE)
614 {
615 show_error('You must include the name of the table you would like to access when you initialize scaffolding');
616 }
617
618 $CI =& get_instance();
619 $CI->_ci_scaffolding = TRUE;
620 $CI->_ci_scaff_table = $table;
621 }
622
623 // --------------------------------------------------------------------
624
625 /**
626 * Loader
627 *
628 * This function is used to load views and files.
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000629 * Variables are prefixed with _ci_ to avoid symbol collision with
630 * variables made available to view files
Derek Allard02658572007-12-20 14:00:50 +0000631 *
632 * @access private
633 * @param array
634 * @return void
635 */
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000636 function _ci_load($_ci_data)
Derek Allard02658572007-12-20 14:00:50 +0000637 {
638 // Set the default data variables
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000639 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
Derek Allard02658572007-12-20 14:00:50 +0000640 {
Derek Jones0b59f272008-05-13 04:22:33 +0000641 $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
Derek Allard02658572007-12-20 14:00:50 +0000642 }
643
644 // Set the path to the requested file
Derek Jones47845f22008-01-16 23:23:02 +0000645 if ($_ci_path == '')
Derek Allard02658572007-12-20 14:00:50 +0000646 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000647 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
648 $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view;
649 $_ci_path = $this->_ci_view_path.$_ci_file;
Derek Allard02658572007-12-20 14:00:50 +0000650 }
651 else
652 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000653 $_ci_x = explode('/', $_ci_path);
654 $_ci_file = end($_ci_x);
Derek Allard02658572007-12-20 14:00:50 +0000655 }
656
Derek Jones0b59f272008-05-13 04:22:33 +0000657 if ( ! file_exists($_ci_path))
Derek Allard02658572007-12-20 14:00:50 +0000658 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000659 show_error('Unable to load the requested file: '.$_ci_file);
Derek Allard02658572007-12-20 14:00:50 +0000660 }
661
662 // This allows anything loaded using $this->load (views, files, etc.)
663 // to become accessible from within the Controller and Model functions.
664 // Only needed when running PHP 5
665
666 if ($this->_ci_is_instance())
667 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000668 $_ci_CI =& get_instance();
669 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard02658572007-12-20 14:00:50 +0000670 {
Derek Jones0b59f272008-05-13 04:22:33 +0000671 if ( ! isset($this->$_ci_key))
Derek Allard02658572007-12-20 14:00:50 +0000672 {
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000673 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard02658572007-12-20 14:00:50 +0000674 }
675 }
676 }
Derek Jonesca0e7fa2008-01-22 16:42:13 +0000677
Derek Allard02658572007-12-20 14:00:50 +0000678 /*
679 * Extract and cache variables
680 *
681 * You can either set variables using the dedicated $this->load_vars()
682 * function or via the second parameter of this function. We'll merge
683 * the two types and cache them so that views that are embedded within
684 * other views can have access to these variables.
685 */
Derek Jones47845f22008-01-16 23:23:02 +0000686 if (is_array($_ci_vars))
Derek Allard02658572007-12-20 14:00:50 +0000687 {
Derek Jones47845f22008-01-16 23:23:02 +0000688 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
Derek Allard02658572007-12-20 14:00:50 +0000689 }
690 extract($this->_ci_cached_vars);
691
692 /*
693 * Buffer the output
694 *
695 * We buffer the output for two reasons:
696 * 1. Speed. You get a significant speed boost.
697 * 2. So that the final rendered template can be
698 * post-processed by the output class. Why do we
699 * need post processing? For one thing, in order to
700 * show the elapsed page load time. Unless we
701 * can intercept the content right before it's sent to
702 * the browser and then stop the timer it won't be accurate.
703 */
704 ob_start();
705
706 // If the PHP installation does not support short tags we'll
707 // do a little string replacement, changing the short tags
708 // to standard PHP echo statements.
709
710 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
711 {
Derek Jones454fa7e2008-05-13 22:26:09 +0000712 echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard02658572007-12-20 14:00:50 +0000713 }
714 else
715 {
Derek Allard72c82c12008-04-04 11:34:58 +0000716 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
Derek Allard02658572007-12-20 14:00:50 +0000717 }
718
Derek Jones47845f22008-01-16 23:23:02 +0000719 log_message('debug', 'File loaded: '.$_ci_path);
Derek Allard02658572007-12-20 14:00:50 +0000720
721 // Return the file data if requested
Derek Jones47845f22008-01-16 23:23:02 +0000722 if ($_ci_return === TRUE)
Derek Allard02658572007-12-20 14:00:50 +0000723 {
724 $buffer = ob_get_contents();
725 @ob_end_clean();
726 return $buffer;
727 }
728
729 /*
730 * Flush the buffer... or buff the flusher?
731 *
732 * In order to permit views to be nested within
733 * other views, we need to flush the content back out whenever
734 * we are beyond the first level of output buffering so that
735 * it can be seen and included properly by the first included
736 * template and any subsequent ones. Oy!
737 *
738 */
739 if (ob_get_level() > $this->_ci_ob_level + 1)
740 {
741 ob_end_flush();
742 }
743 else
744 {
745 // PHP 4 requires that we use a global
746 global $OUT;
Derek Allard66f07242008-01-18 22:36:14 +0000747 $OUT->append_output(ob_get_contents());
Derek Allard02658572007-12-20 14:00:50 +0000748 @ob_end_clean();
749 }
750 }
751
752 // --------------------------------------------------------------------
753
754 /**
755 * Load class
756 *
757 * This function loads the requested class.
758 *
759 * @access private
760 * @param string the item that is being loaded
761 * @param mixed any additional parameters
Rick Ellis6ad53292008-08-21 18:31:28 +0000762 * @param string an optional object name
Derek Allard02658572007-12-20 14:00:50 +0000763 * @return void
764 */
Rick Ellis6ad53292008-08-21 18:31:28 +0000765 function _ci_load_class($class, $params = NULL, $object_name = NULL)
Derek Allard02658572007-12-20 14:00:50 +0000766 {
Rick Ellis0ea8f982008-08-12 00:39:10 +0000767 // Get the class name, and while we're at it trim any slashes.
768 // The directory path can be included as part of the class name,
769 // but we don't want a leading slash
770 $class = str_replace(EXT, '', trim($class, '/'));
771
772 // Was the path included with the class name?
773 // We look for a slash to determine this
774 $subdir = '';
775 if (strpos($class, '/') !== FALSE)
776 {
777 // explode the path so we can separate the filename from the path
778 $x = explode('/', $class);
779
780 // Reset the $class variable now that we know the actual filename
781 $class = end($x);
782
783 // Kill the filename from the array
784 unset($x[count($x)-1]);
785
786 // Glue the path back together, sans filename
787 $subdir = implode($x, '/').'/';
788 }
Derek Allard02658572007-12-20 14:00:50 +0000789
790 // We'll test for both lowercase and capitalized versions of the file name
791 foreach (array(ucfirst($class), strtolower($class)) as $class)
792 {
Rick Ellisafb056a2008-08-12 00:44:25 +0000793 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.EXT;
Derek Allard02658572007-12-20 14:00:50 +0000794
795 // Is this a class extension request?
796 if (file_exists($subclass))
797 {
798 $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
799
Derek Jones0b59f272008-05-13 04:22:33 +0000800 if ( ! file_exists($baseclass))
Derek Allard02658572007-12-20 14:00:50 +0000801 {
802 log_message('error', "Unable to load the requested class: ".$class);
803 show_error("Unable to load the requested class: ".$class);
804 }
805
806 // Safety: Was the class already loaded by a previous call?
807 if (in_array($subclass, $this->_ci_classes))
808 {
Rick Ellis6ad53292008-08-21 18:31:28 +0000809 // Before we deem this to be a duplicate request, let's see
810 // if a custom object name is being supplied. If so, we'll
811 // return a new instance of the object
812 if ( ! is_null($object_name))
813 {
814 $CI =& get_instance();
815 if ( ! isset($CI->$object_name))
816 {
817 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
818 }
819 }
820
Derek Allard02658572007-12-20 14:00:50 +0000821 $is_duplicate = TRUE;
822 log_message('debug', $class." class already loaded. Second attempt ignored.");
823 return;
824 }
825
Derek Allardd888c352008-03-18 04:04:33 +0000826 include_once($baseclass);
827 include_once($subclass);
Derek Allard02658572007-12-20 14:00:50 +0000828 $this->_ci_classes[] = $subclass;
829
Rick Ellis6ad53292008-08-21 18:31:28 +0000830 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard02658572007-12-20 14:00:50 +0000831 }
832
833 // Lets search for the requested library file and load it.
834 $is_duplicate = FALSE;
835 for ($i = 1; $i < 3; $i++)
836 {
837 $path = ($i % 2) ? APPPATH : BASEPATH;
Rick Ellisafb056a2008-08-12 00:44:25 +0000838 $filepath = $path.'libraries/'.$subdir.$class.EXT;
Derek Allard02658572007-12-20 14:00:50 +0000839
840 // Does the file exist? No? Bummer...
Derek Jones0b59f272008-05-13 04:22:33 +0000841 if ( ! file_exists($filepath))
Derek Allard02658572007-12-20 14:00:50 +0000842 {
843 continue;
844 }
845
846 // Safety: Was the class already loaded by a previous call?
847 if (in_array($filepath, $this->_ci_classes))
848 {
Rick Ellis6ad53292008-08-21 18:31:28 +0000849 // Before we deem this to be a duplicate request, let's see
850 // if a custom object name is being supplied. If so, we'll
851 // return a new instance of the object
852 if ( ! is_null($object_name))
853 {
854 $CI =& get_instance();
855 if ( ! isset($CI->$object_name))
856 {
857 return $this->_ci_init_class($class, '', $params, $object_name);
858 }
859 }
860
Derek Allard02658572007-12-20 14:00:50 +0000861 $is_duplicate = TRUE;
862 log_message('debug', $class." class already loaded. Second attempt ignored.");
863 return;
864 }
865
Derek Allardd888c352008-03-18 04:04:33 +0000866 include_once($filepath);
Derek Allard02658572007-12-20 14:00:50 +0000867 $this->_ci_classes[] = $filepath;
Rick Ellis6ad53292008-08-21 18:31:28 +0000868 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard02658572007-12-20 14:00:50 +0000869 }
870 } // END FOREACH
Rick Ellis6ad53292008-08-21 18:31:28 +0000871
872 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
873 if ($subdir == '')
874 {
875 $path = strtolower($class).'/'.$class;
876 return $this->_ci_load_class($path, $params);
877 }
Derek Allard02658572007-12-20 14:00:50 +0000878
879 // If we got this far we were unable to find the requested class.
880 // We do not issue errors if the load call failed due to a duplicate request
881 if ($is_duplicate == FALSE)
882 {
883 log_message('error', "Unable to load the requested class: ".$class);
884 show_error("Unable to load the requested class: ".$class);
885 }
886 }
887
888 // --------------------------------------------------------------------
889
890 /**
891 * Instantiates a class
892 *
893 * @access private
894 * @param string
895 * @param string
Rick Ellis6ad53292008-08-21 18:31:28 +0000896 * @param string an optional object name
Derek Allard02658572007-12-20 14:00:50 +0000897 * @return null
898 */
Rick Ellis6ad53292008-08-21 18:31:28 +0000899 function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Derek Allard02658572007-12-20 14:00:50 +0000900 {
Derek Allard0796b8d2008-01-17 03:41:37 +0000901 $class = strtolower($class);
902
Derek Allard02658572007-12-20 14:00:50 +0000903 // Is there an associated config file for this class?
904 if ($config === NULL)
905 {
906 if (file_exists(APPPATH.'config/'.$class.EXT))
907 {
Derek Allardd888c352008-03-18 04:04:33 +0000908 include_once(APPPATH.'config/'.$class.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000909 }
910 }
911
912 if ($prefix == '')
913 {
914 $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
915 }
916 else
917 {
918 $name = $prefix.$class;
919 }
920
Rick Ellis6ad53292008-08-21 18:31:28 +0000921 // Set the variable name we will assign the class to
922 // Was a custom class name supplied? If so we'll use it
923 if (is_null($object_name))
924 {
925 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
926 }
927 else
928 {
929 $classvar = $object_name;
930 }
Derek Allard02658572007-12-20 14:00:50 +0000931
932 // Instantiate the class
933 $CI =& get_instance();
934 if ($config !== NULL)
935 {
936 $CI->$classvar = new $name($config);
937 }
938 else
939 {
940 $CI->$classvar = new $name;
941 }
942 }
943
944 // --------------------------------------------------------------------
945
946 /**
947 * Autoloader
948 *
949 * The config/autoload.php file contains an array that permits sub-systems,
950 * libraries, plugins, and helpers to be loaded automatically.
951 *
952 * @access private
953 * @param array
954 * @return void
955 */
956 function _ci_autoloader()
957 {
Derek Allardd888c352008-03-18 04:04:33 +0000958 include_once(APPPATH.'config/autoload'.EXT);
Derek Allard02658572007-12-20 14:00:50 +0000959
Derek Jones0b59f272008-05-13 04:22:33 +0000960 if ( ! isset($autoload))
Derek Allard02658572007-12-20 14:00:50 +0000961 {
962 return FALSE;
963 }
964
965 // Load any custom config file
966 if (count($autoload['config']) > 0)
967 {
968 $CI =& get_instance();
969 foreach ($autoload['config'] as $key => $val)
970 {
971 $CI->config->load($val);
972 }
973 }
974
Derek Allard15dcf492008-05-12 21:37:04 +0000975 // Autoload plugins, helpers and languages
976 foreach (array('helper', 'plugin', 'language') as $type)
Derek Allard02658572007-12-20 14:00:50 +0000977 {
978 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
979 {
980 $this->$type($autoload[$type]);
981 }
982 }
983
Derek Allard22259b52008-01-22 01:47:11 +0000984
Derek Allard02658572007-12-20 14:00:50 +0000985
986 // A little tweak to remain backward compatible
987 // The $autoload['core'] item was deprecated
Derek Jones0b59f272008-05-13 04:22:33 +0000988 if ( ! isset($autoload['libraries']))
Derek Allard02658572007-12-20 14:00:50 +0000989 {
990 $autoload['libraries'] = $autoload['core'];
991 }
992
993 // Load libraries
994 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
995 {
996 // Load the database driver.
997 if (in_array('database', $autoload['libraries']))
998 {
999 $this->database();
1000 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1001 }
1002
1003 // Load scaffolding
1004 if (in_array('scaffolding', $autoload['libraries']))
1005 {
1006 $this->scaffolding();
1007 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
1008 }
1009
1010 // Load all other libraries
1011 foreach ($autoload['libraries'] as $item)
1012 {
1013 $this->library($item);
1014 }
Derek Allard02658572007-12-20 14:00:50 +00001015 }
Derek Allard22259b52008-01-22 01:47:11 +00001016
1017 // Autoload models
1018 if (isset($autoload['model']))
1019 {
1020 $this->model($autoload['model']);
1021 }
1022
Derek Allard02658572007-12-20 14:00:50 +00001023 }
1024
1025 // --------------------------------------------------------------------
1026
1027 /**
1028 * Assign to Models
1029 *
1030 * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
1031 * will be available to models, if any exist.
1032 *
1033 * @access private
1034 * @param object
1035 * @return array
1036 */
1037 function _ci_assign_to_models()
1038 {
1039 if (count($this->_ci_models) == 0)
1040 {
1041 return;
1042 }
1043
1044 if ($this->_ci_is_instance())
1045 {
1046 $CI =& get_instance();
1047 foreach ($this->_ci_models as $model)
1048 {
1049 $CI->$model->_assign_libraries();
1050 }
1051 }
1052 else
1053 {
1054 foreach ($this->_ci_models as $model)
1055 {
1056 $this->$model->_assign_libraries();
1057 }
1058 }
1059 }
1060
1061 // --------------------------------------------------------------------
1062
1063 /**
1064 * Object to Array
1065 *
1066 * Takes an object as input and converts the class variables to array key/vals
1067 *
1068 * @access private
1069 * @param object
1070 * @return array
1071 */
1072 function _ci_object_to_array($object)
1073 {
1074 return (is_object($object)) ? get_object_vars($object) : $object;
1075 }
1076
1077 // --------------------------------------------------------------------
1078
1079 /**
1080 * Determines whether we should use the CI instance or $this
1081 *
1082 * @access private
1083 * @return bool
1084 */
1085 function _ci_is_instance()
1086 {
1087 if ($this->_ci_is_php5 == TRUE)
1088 {
1089 return TRUE;
1090 }
1091
1092 global $CI;
1093 return (is_object($CI)) ? TRUE : FALSE;
1094 }
1095
1096}
Derek Allard15dcf492008-05-12 21:37:04 +00001097
1098/* End of file Loader.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001099/* Location: ./system/libraries/Loader.php */