blob: 1738438ed4fc9ef8fcb8e0f2b4b829844b18243b [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
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, pMachine, Inc.
10 * @license http://www.codeignitor.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
admincf493902006-10-10 07:12:31 +000031 var $_ci_ob_level;
32 var $_ci_view_path = '';
33 var $_ci_cached_vars = array();
34 var $_ci_models = array();
35 var $_ci_helpers = array();
36 var $_ci_plugins = array();
37 var $_ci_scripts = array();
38 var $_ci_varmap = array('unit_test' => 'unit', 'user_agent' => 'agent');
39
40 var $_ci_use_instance = FALSE; // This variable determines whether we should
41 // use $this or $CI =& get_instance()
42 // throughout this class. Don't mess with it.
adminb0dd10f2006-08-25 17:25:49 +000043
44 /**
45 * Constructor
46 *
47 * Sets the path to the view files and gets the initial output
48 * buffering level
49 *
50 * @access public
51 */
52 function CI_Loader()
admincf493902006-10-10 07:12:31 +000053 {
54 $this->_ci_view_path = APPPATH.'views/';
55 $this->_ci_ob_level = ob_get_level();
adminb0dd10f2006-08-25 17:25:49 +000056
57 log_message('debug', "Loader Class Initialized");
58 }
adminb0dd10f2006-08-25 17:25:49 +000059
60 // --------------------------------------------------------------------
61
62 /**
63 * Class Loader
64 *
65 * This function lets users load and instantiate classes.
66 * It is designed to be called from a user's app controllers.
67 *
68 * @access public
69 * @param string the name of the class
admin2f0bac82006-10-09 17:36:45 +000070 * @param sring the optional class variable name to assign the library to
adminb0dd10f2006-08-25 17:25:49 +000071 * @return void
72 */
admin2f0bac82006-10-09 17:36:45 +000073 function library($class, $varname = NULL)
adminb0dd10f2006-08-25 17:25:49 +000074 {
75 if ($class == '')
76 return;
77
admin2f0bac82006-10-09 17:36:45 +000078 $this->_ci_load_class($class, $varname);
admin8f0a8f62006-10-07 01:17:25 +000079 $this->_ci_assign_to_models();
adminb0dd10f2006-08-25 17:25:49 +000080 }
adminb0dd10f2006-08-25 17:25:49 +000081
82 // --------------------------------------------------------------------
83
84 /**
85 * Model Loader
86 *
87 * This function lets users load and instantiate models.
88 *
89 * @access public
90 * @param string the name of the class
91 * @param mixed any initialization parameters
92 * @return void
93 */
94 function model($model, $name = '', $db_conn = FALSE)
95 {
96 if ($model == '')
97 return;
98
admincf493902006-10-10 07:12:31 +000099 // Is the model in a sub-folder? If so, parse out the filename and path.
admin8f0a8f62006-10-07 01:17:25 +0000100 if (strpos($model, '/') === FALSE)
101 {
102 $path = '';
103 }
104 else
105 {
106 $x = explode('/', $model);
107 $model = end($x);
108 unset($x[count($x)-1]);
109 $path = implode('/', $x).'/';
110 }
111
112 if ($name == '')
113 {
114 $name = $model;
115 }
116
admincf493902006-10-10 07:12:31 +0000117 if (in_array($name, $this->_ci_models, TRUE))
admin8f0a8f62006-10-07 01:17:25 +0000118 {
119 return;
admincf493902006-10-10 07:12:31 +0000120 }
admin8f0a8f62006-10-07 01:17:25 +0000121
admincf493902006-10-10 07:12:31 +0000122 if ($this->_ci_use_instance)
admin8f0a8f62006-10-07 01:17:25 +0000123 {
admincf493902006-10-10 07:12:31 +0000124 $CI =& get_instance();
125 if (isset($CI->$name))
126 {
127 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
128 }
129 }
130 else
131 {
132 if (isset($this->$name))
133 {
134 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
135 }
admin8f0a8f62006-10-07 01:17:25 +0000136 }
137
138 $model = strtolower($model);
139
140 if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
141 {
142 show_error('Unable to locate the model you have specified: '.$model);
143 }
144
145 if ($db_conn !== FALSE)
146 {
147 if ($db_conn === TRUE)
148 $db_conn = '';
149
admincf493902006-10-10 07:12:31 +0000150 if ($this->_ci_use_instance)
151 {
152 $CI->load->database($db_conn, FALSE, TRUE);
153 }
154 else
155 {
156 $this->database($db_conn, FALSE, TRUE);
157 }
admin8f0a8f62006-10-07 01:17:25 +0000158 }
159
160 if ( ! class_exists('Model'))
161 {
162 require_once(BASEPATH.'libraries/Model'.EXT);
163 }
164
165 require_once(APPPATH.'models/'.$path.$model.EXT);
166
167 $model = ucfirst($model);
admincf493902006-10-10 07:12:31 +0000168
169 if ($this->_ci_use_instance)
170 {
171 $CI->$name = new $model();
172 }
173 else
174 {
175 $this->$name = new $model();
176 }
177
178 $this->_ci_models[] = $name;
admin8f0a8f62006-10-07 01:17:25 +0000179 $this->_ci_assign_to_models();
adminb0dd10f2006-08-25 17:25:49 +0000180 }
adminb0dd10f2006-08-25 17:25:49 +0000181
182 // --------------------------------------------------------------------
183
184 /**
185 * Database Loader
186 *
187 * @access public
188 * @param string the DB credentials
189 * @param bool whether to return the DB object
190 * @param bool whether to enable active record (this allows us to override the config setting)
admina5e812c2006-09-25 02:17:30 +0000191 * @return object
adminb0dd10f2006-08-25 17:25:49 +0000192 */
admin8f0a8f62006-10-07 01:17:25 +0000193 function database($params = '', $return = FALSE, $active_record = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000194 {
admin8f0a8f62006-10-07 01:17:25 +0000195 require_once(BASEPATH.'database/DB'.EXT);
adminfd2750b2006-10-06 17:29:12 +0000196
adminb0dd10f2006-08-25 17:25:49 +0000197 if ($return === TRUE)
198 {
admin8f0a8f62006-10-07 01:17:25 +0000199 return DB($params, $return, $active_record);
adminb0dd10f2006-08-25 17:25:49 +0000200 }
201 else
202 {
admin8f0a8f62006-10-07 01:17:25 +0000203 DB($params, $return, $active_record);
204 $this->_ci_assign_to_models();
adminb0dd10f2006-08-25 17:25:49 +0000205 }
206 }
adminb0dd10f2006-08-25 17:25:49 +0000207
adminb0dd10f2006-08-25 17:25:49 +0000208 // --------------------------------------------------------------------
209
210 /**
211 * Load View
212 *
213 * This function is used to load a "view" file. It has three parameters:
214 *
215 * 1. The name of the "view" file to be included.
216 * 2. An associative array of data to be extracted for use in the view.
217 * 3. TRUE/FALSE - whether to return the data or load it. In
218 * some cases it's advantageous to be able to retun data so that
219 * a developer can process it in some way.
220 *
221 * @access public
222 * @param string
223 * @param array
224 * @param bool
225 * @return void
226 */
227 function view($view, $vars = array(), $return = FALSE)
228 {
229 return $this->_ci_load(array('view' => $view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return));
230 }
adminb0dd10f2006-08-25 17:25:49 +0000231
232 // --------------------------------------------------------------------
233
234 /**
235 * Load File
236 *
237 * This is a generic file loader
238 *
239 * @access public
240 * @param string
241 * @param bool
242 * @return string
243 */
244 function file($path, $return = FALSE)
245 {
246 return $this->_ci_load(array('path' => $path, 'return' => $return));
247 }
adminb0dd10f2006-08-25 17:25:49 +0000248
249 // --------------------------------------------------------------------
250
251 /**
252 * Set Variables
253 *
254 * Once variables are set they become availabe within
255 * the controller class and its "view" files.
256 *
257 * @access public
258 * @param array
259 * @return void
260 */
261 function vars($vars = array())
262 {
263 $vars = $this->_ci_object_to_array($vars);
264
265 if (is_array($vars) AND count($vars) > 0)
266 {
267 foreach ($vars as $key => $val)
268 {
admincf493902006-10-10 07:12:31 +0000269 $this->_ci_cached_vars[$key] = $val;
adminb0dd10f2006-08-25 17:25:49 +0000270 }
271 }
272 }
adminb0dd10f2006-08-25 17:25:49 +0000273
274 // --------------------------------------------------------------------
275
276 /**
277 * Load Helper
278 *
279 * This function loads the specified helper file.
280 *
281 * @access public
282 * @param mixed
283 * @return void
284 */
285 function helper($helpers = array())
286 {
287 if ( ! is_array($helpers))
288 {
289 $helpers = array($helpers);
290 }
291
292 foreach ($helpers as $helper)
293 {
admincf493902006-10-10 07:12:31 +0000294 if (isset($this->_ci_helpers[$helper]))
adminb0dd10f2006-08-25 17:25:49 +0000295 {
296 continue;
297 }
298
299 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
300
admin480da812006-09-20 21:11:04 +0000301 if (file_exists(APPPATH.'helpers/'.$helper.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000302 {
admin480da812006-09-20 21:11:04 +0000303 include_once(APPPATH.'helpers/'.$helper.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000304 }
admin480da812006-09-20 21:11:04 +0000305 else
306 {
307 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
308 {
309 include_once(BASEPATH.'helpers/'.$helper.EXT);
310 }
311 else
312 {
313 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
314 }
315 }
adminb0dd10f2006-08-25 17:25:49 +0000316
admincf493902006-10-10 07:12:31 +0000317 $this->_ci_helpers[$helper] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000318 }
319
320 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
321 }
adminb0dd10f2006-08-25 17:25:49 +0000322
323 // --------------------------------------------------------------------
324
325 /**
326 * Load Helpers
327 *
328 * This is simply an alias to the above function in case the
329 * user has written the plural form of this function.
330 *
331 * @access public
332 * @param array
333 * @return void
334 */
335 function helpers($helpers = array())
336 {
337 $this->helper($helpers);
338 }
adminb0dd10f2006-08-25 17:25:49 +0000339
340 // --------------------------------------------------------------------
341
342 /**
343 * Load Plugin
344 *
345 * This function loads the specified plugin.
346 *
347 * @access public
348 * @param array
349 * @return void
350 */
351 function plugin($plugins = array())
352 {
353 if ( ! is_array($plugins))
354 {
355 $plugins = array($plugins);
356 }
357
358 foreach ($plugins as $plugin)
359 {
admincf493902006-10-10 07:12:31 +0000360 if (isset($this->_ci_plugins[$plugin]))
adminb0dd10f2006-08-25 17:25:49 +0000361 {
362 continue;
363 }
364
admin480da812006-09-20 21:11:04 +0000365 $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi');
adminb0dd10f2006-08-25 17:25:49 +0000366
admin480da812006-09-20 21:11:04 +0000367 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000368 {
admin480da812006-09-20 21:11:04 +0000369 include_once(APPPATH.'plugins/'.$plugin.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000370 }
admin480da812006-09-20 21:11:04 +0000371 else
372 {
373 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
374 {
375 include_once(BASEPATH.'plugins/'.$plugin.EXT);
376 }
377 else
378 {
379 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
380 }
381 }
adminb0dd10f2006-08-25 17:25:49 +0000382
admincf493902006-10-10 07:12:31 +0000383 $this->_ci_plugins[$plugin] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000384 }
385
386 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
387 }
adminfd2750b2006-10-06 17:29:12 +0000388
389 // --------------------------------------------------------------------
adminb0dd10f2006-08-25 17:25:49 +0000390
adminfd2750b2006-10-06 17:29:12 +0000391 /**
392 * Load Plugins
393 *
394 * This is simply an alias to the above function in case the
395 * user has written the plural form of this function.
396 *
397 * @access public
398 * @param array
399 * @return void
400 */
401 function plugins($plugins = array())
402 {
403 $this->plugin($plugins);
404 }
405
adminb0dd10f2006-08-25 17:25:49 +0000406 // --------------------------------------------------------------------
407
408 /**
409 * Load Script
410 *
411 * This function loads the specified include file from the
admin83b05a82006-09-25 21:06:46 +0000412 * application/scripts/ folder.
413 *
414 * NOTE: This feature has been deprecated but it will remain available
415 * for legacy users.
adminb0dd10f2006-08-25 17:25:49 +0000416 *
417 * @access public
418 * @param array
419 * @return void
420 */
421 function script($scripts = array())
422 {
423 if ( ! is_array($scripts))
424 {
425 $scripts = array($scripts);
426 }
427
428 foreach ($scripts as $script)
429 {
admincf493902006-10-10 07:12:31 +0000430 if (isset($this->_ci_scripts[$script]))
adminb0dd10f2006-08-25 17:25:49 +0000431 {
432 continue;
433 }
434
435 $script = strtolower(str_replace(EXT, '', $script));
436
437 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
438 {
439 show_error('Unable to load the requested script: scripts/'.$script.EXT);
440 }
441
442 include_once(APPPATH.'scripts/'.$script.EXT);
443
admincf493902006-10-10 07:12:31 +0000444 $this->_ci_scripts[$script] = TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000445 }
446
447 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
448 }
adminfd2750b2006-10-06 17:29:12 +0000449
adminb0dd10f2006-08-25 17:25:49 +0000450 // --------------------------------------------------------------------
451
452 /**
453 * Loads a language file
454 *
455 * @access public
456 * @param string
457 * @return void
458 */
459 function language($file = '', $lang = '', $return = FALSE)
460 {
admincf493902006-10-10 07:12:31 +0000461 if ($this->_ci_use_instance)
462 {
463 $CI =& get_instance();
464 return $CI->lang->load($file, $lang, $return);
465 }
466 else
467 {
468 return $this->lang->load($file, $lang, $return);
469 }
adminb0dd10f2006-08-25 17:25:49 +0000470 }
adminb0dd10f2006-08-25 17:25:49 +0000471
472 // --------------------------------------------------------------------
473
474 /**
475 * Loads a config file
476 *
477 * @access public
478 * @param string
479 * @return void
480 */
481 function config($file = '')
admincf493902006-10-10 07:12:31 +0000482 {
483 if ($this->_ci_use_instance)
484 {
485 $CI =& get_instance();
486 $CI->config->load($file);
487 }
488 else
489 {
490 $this->config->load($file);
491 }
adminb0dd10f2006-08-25 17:25:49 +0000492 }
admin8f0a8f62006-10-07 01:17:25 +0000493
adminb0dd10f2006-08-25 17:25:49 +0000494 // --------------------------------------------------------------------
495
496 /**
admin8f0a8f62006-10-07 01:17:25 +0000497 * Scaffolding Loader
adminb0dd10f2006-08-25 17:25:49 +0000498 *
admin8f0a8f62006-10-07 01:17:25 +0000499 * This initializing function works a bit different than the
500 * others. It doesn't load the class. Instead, it simply
501 * sets a flag indicating that scaffolding is allowed to be
502 * used. The actual scaffolding function below is
503 * called by the front controller based on whether the
504 * second segment of the URL matches the "secret" scaffolding
505 * word stored in the application/config/routes.php
506 *
507 * @access public
adminb0dd10f2006-08-25 17:25:49 +0000508 * @param string
509 * @return void
admin8f0a8f62006-10-07 01:17:25 +0000510 */
511 function scaffolding($table = '')
512 {
513 if ($table === FALSE)
514 {
515 show_error('You must include the name of the table you would like access when you initialize scaffolding');
516 }
admincf493902006-10-10 07:12:31 +0000517
518 if ($this->_ci_use_instance)
519 {
520 $CI =& get_instance();
521 $CI->_ci_scaffolding = TRUE;
522 $CI->_ci_scaff_table = $table;
523 }
524 else
525 {
526 $this->_ci_scaffolding = TRUE;
527 $this->_ci_scaff_table = $table;
528 }
adminb0dd10f2006-08-25 17:25:49 +0000529 }
admin8f0a8f62006-10-07 01:17:25 +0000530
adminb0dd10f2006-08-25 17:25:49 +0000531 // --------------------------------------------------------------------
admin8f0a8f62006-10-07 01:17:25 +0000532
adminb0dd10f2006-08-25 17:25:49 +0000533 /**
534 * Loader
535 *
admin8f0a8f62006-10-07 01:17:25 +0000536 * This function is used to load views and files.
adminb0dd10f2006-08-25 17:25:49 +0000537 *
538 * @access private
539 * @param array
540 * @return void
541 */
542 function _ci_load($data)
admin212a3fa2006-09-23 04:22:39 +0000543 {
adminb0dd10f2006-08-25 17:25:49 +0000544 // This allows anything loaded using $this->load (viwes, files, etc.)
545 // to become accessible from within the Controller and Model functions.
admincf493902006-10-10 07:12:31 +0000546 // Only needed when running PHP 5
547
548 if ($this->_ci_use_instance)
adminb0dd10f2006-08-25 17:25:49 +0000549 {
admincf493902006-10-10 07:12:31 +0000550 $CI =& get_instance();
551 foreach (get_object_vars($CI) as $key => $var)
adminb0dd10f2006-08-25 17:25:49 +0000552 {
admincf493902006-10-10 07:12:31 +0000553 if ( ! isset($this->$key))
554 {
555 $this->$key =& $CI->$key;
556 }
admine79dc712006-09-26 03:52:45 +0000557 }
558 }
admincf493902006-10-10 07:12:31 +0000559
adminb0dd10f2006-08-25 17:25:49 +0000560 // Set the default data variables
561 foreach (array('view', 'vars', 'path', 'return') as $val)
562 {
563 $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
564 }
565
566 /*
admin83b05a82006-09-25 21:06:46 +0000567 * Extract and cache variables
adminb0dd10f2006-08-25 17:25:49 +0000568 *
admin212a3fa2006-09-23 04:22:39 +0000569 * You can either set variables using the dedicated $this->load_vars()
570 * function or via the second parameter of this function. We'll merge
571 * the two types and cache them so that views that are embedded within
572 * other views can have access to these variables.
adminb0dd10f2006-08-25 17:25:49 +0000573 */
adminb0dd10f2006-08-25 17:25:49 +0000574 if (is_array($vars))
575 {
admincf493902006-10-10 07:12:31 +0000576 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $vars);
adminb0dd10f2006-08-25 17:25:49 +0000577 }
admincf493902006-10-10 07:12:31 +0000578 extract($this->_ci_cached_vars);
adminb0dd10f2006-08-25 17:25:49 +0000579
580 // Set the path to the requested file
581 if ($path == '')
582 {
583 $ext = pathinfo($view, PATHINFO_EXTENSION);
admin6ac4bea2006-09-02 17:46:15 +0000584 $file = ($ext == '') ? $view.EXT : $view;
admincf493902006-10-10 07:12:31 +0000585 $path = $this->_ci_view_path.$file;
adminb0dd10f2006-08-25 17:25:49 +0000586 }
587 else
588 {
589 $x = explode('/', $path);
590 $file = end($x);
591 }
592
593 /*
594 * Buffer the output
595 *
596 * We buffer the output for two reasons:
597 * 1. Speed. You get a significant speed boost.
598 * 2. So that the final rendered template can be
599 * post-processed by the output class. Why do we
600 * need post processing? For one thing, in order to
601 * show the elapsed page load time. Unless we
602 * can intercept the content right before it's sent to
admin212a3fa2006-09-23 04:22:39 +0000603 * the browser and then stop the timer it won't be acurate.
adminb0dd10f2006-08-25 17:25:49 +0000604 */
adminb0dd10f2006-08-25 17:25:49 +0000605 if ( ! file_exists($path))
606 {
607 show_error('Unable to load the requested file: '.$file);
608 }
609
610 ob_start();
611
612 include($path);
613 log_message('debug', 'File loaded: '.$path);
614
615 // Return the file data if requested to
616 if ($return === TRUE)
617 {
618 $buffer = ob_get_contents();
619 ob_end_clean();
620
621 return $buffer;
622 }
623
624 /*
625 * Flush the buffer... or buff the flusher?
626 *
admin212a3fa2006-09-23 04:22:39 +0000627 * In order to permit views to be nested within
adminb0dd10f2006-08-25 17:25:49 +0000628 * other views, we need to flush the content back out whenever
629 * we are beyond the first level of output buffering so that
630 * it can be seen and included properly by the first included
631 * template and any subsequent ones. Oy!
632 *
admincf493902006-10-10 07:12:31 +0000633 */
634 if (ob_get_level() > $this->_ci_ob_level + 1)
adminb0dd10f2006-08-25 17:25:49 +0000635 {
636 ob_end_flush();
637 }
638 else
639 {
admincf493902006-10-10 07:12:31 +0000640 global $OUT;
641 $OUT->set_output(ob_get_contents());
adminb0dd10f2006-08-25 17:25:49 +0000642 ob_end_clean();
643 }
644 }
admin8f0a8f62006-10-07 01:17:25 +0000645
646 // --------------------------------------------------------------------
647
648 /**
649 * Load class
650 *
651 * This function loads the requested class.
652 *
653 * @access private
654 * @param string the item that is being loaded
655 * @param mixed any additional parameters
656 * @return void
657 */
admin2f0bac82006-10-09 17:36:45 +0000658 function _ci_load_class($class, $varname = NULL)
admin8f0a8f62006-10-07 01:17:25 +0000659 {
660 // Prep the class name
661 $class = strtolower(str_replace(EXT, '', $class));
662
admin8f0a8f62006-10-07 01:17:25 +0000663 // Is this a class extension request?
664 if (substr($class, 0, 3) == 'my_')
665 {
666 $class = preg_replace("/my_(.+)/", "\\1", $class);
admin10c3f412006-10-08 07:21:12 +0000667
admin8f0a8f62006-10-07 01:17:25 +0000668 // Load the requested library from the main system/libraries folder
669 if (file_exists(BASEPATH.'libraries/'.ucfirst($class).EXT))
670 {
671 include_once(BASEPATH.'libraries/'.ucfirst($class).EXT);
672 }
673
674 // Now look for a matching library
675 foreach (array(ucfirst($class), $class) as $filename)
676 {
677 if (file_exists(APPPATH.'libraries/'.$filename.EXT))
678 {
679 include_once(APPPATH.'libraries/'.$filename.EXT);
680 }
681 }
682
admin2f0bac82006-10-09 17:36:45 +0000683 return $this->_ci_init_class($filename, 'MY_', $varname);
admin8f0a8f62006-10-07 01:17:25 +0000684 }
admin10c3f412006-10-08 07:21:12 +0000685
686 // Lets search for the requested library file and load it.
687 // For backward compatibility we'll test for filenames that are
688 // both uppercase and lower.
689 foreach (array(ucfirst($class), $class) as $filename)
690 {
691 for ($i = 1; $i < 3; $i++)
admin8f0a8f62006-10-07 01:17:25 +0000692 {
admin10c3f412006-10-08 07:21:12 +0000693 $path = ($i % 2) ? APPPATH : BASEPATH;
694
695 if (file_exists($path.'libraries/'.$filename.EXT))
admin8f0a8f62006-10-07 01:17:25 +0000696 {
admin10c3f412006-10-08 07:21:12 +0000697 include_once($path.'libraries/'.$filename.EXT);
admin2f0bac82006-10-09 17:36:45 +0000698 return $this->_ci_init_class($filename, '', $varname);
admin8f0a8f62006-10-07 01:17:25 +0000699 }
700 }
701 }
702
703 // If we got this far we were unable to find the requested class
704 log_message('error', "Unable to load the requested class: ".$class);
705 show_error("Unable to load the class: ".$class);
706 }
707
708 // --------------------------------------------------------------------
709
710 /**
711 * Instantiates a class
712 *
713 * @access private
714 * @param string
715 * @param string
716 * @return null
717 */
admin2f0bac82006-10-09 17:36:45 +0000718 function _ci_init_class($class, $prefix = '', $varname = NULL)
admin8f0a8f62006-10-07 01:17:25 +0000719 {
720 // Is there an associated config file for this class?
adminb1fddc02006-10-09 21:29:07 +0000721 $config = NULL;
admin2f0bac82006-10-09 17:36:45 +0000722 if (file_exists(APPPATH.'config/'.$class.EXT))
admin8f0a8f62006-10-07 01:17:25 +0000723 {
admin2f0bac82006-10-09 17:36:45 +0000724 include_once(APPPATH.'config/'.$class.EXT);
admin8f0a8f62006-10-07 01:17:25 +0000725 }
726
727 if ($prefix == '')
728 {
admin10c3f412006-10-08 07:21:12 +0000729 $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
admin8f0a8f62006-10-07 01:17:25 +0000730 }
731 else
732 {
733 $name = $prefix.$class;
734 }
admin2f0bac82006-10-09 17:36:45 +0000735
adminb1fddc02006-10-09 21:29:07 +0000736 // Set the variable name we will assign the class to
737 if ( ! is_null($varname))
738 {
739 $classvar = $varname;
740 }
741 else
742 {
743 $class = strtolower($class);
admincf493902006-10-10 07:12:31 +0000744 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
adminb1fddc02006-10-09 21:29:07 +0000745 }
admin8f0a8f62006-10-07 01:17:25 +0000746
admincf493902006-10-10 07:12:31 +0000747 // Instantiate the class
748 if ($this->_ci_use_instance)
admin8f0a8f62006-10-07 01:17:25 +0000749 {
admincf493902006-10-10 07:12:31 +0000750 $CI =& get_instance();
751 if ($config !== NULL)
752 {
753 $CI->$classvar = new $name($config);
754 }
755 else
756 {
757 $CI->$classvar = new $name;
758 }
admin8f0a8f62006-10-07 01:17:25 +0000759 }
760 else
admincf493902006-10-10 07:12:31 +0000761 {
762 if ($config !== NULL)
763 {
764 $this->$classvar = new $name($config);
765 }
766 else
767 {
768 $this->$classvar = new $name;
769 }
770 }
admin8f0a8f62006-10-07 01:17:25 +0000771 }
adminb0dd10f2006-08-25 17:25:49 +0000772
773 // --------------------------------------------------------------------
774
775 /**
776 * Autoloader
777 *
778 * The config/autoload.php file contains an array that permits sub-systems,
admin8f0a8f62006-10-07 01:17:25 +0000779 * libraries, plugins, and helpers to be loaded automatically.
adminb0dd10f2006-08-25 17:25:49 +0000780 *
781 * @access private
782 * @param array
783 * @return void
784 */
admin8f0a8f62006-10-07 01:17:25 +0000785 function _ci_autoloader()
786 {
787 include_once(APPPATH.'config/autoload'.EXT);
788
789 if ( ! isset($autoload))
790 {
791 return FALSE;
792 }
793
794 // Load any custome config file
795 if (count($autoload['config']) > 0)
admincf493902006-10-10 07:12:31 +0000796 {
797 if ($this->_ci_use_instance)
admin8f0a8f62006-10-07 01:17:25 +0000798 {
admincf493902006-10-10 07:12:31 +0000799 $CI =& get_instance();
800 foreach ($autoload['config'] as $key => $val)
801 {
802 $CI->config->load($val);
803 }
804 }
805 else
806 {
807 foreach ($autoload['config'] as $key => $val)
808 {
809 $this->config->load($val);
810 }
admin8f0a8f62006-10-07 01:17:25 +0000811 }
812 }
813
814 // Load plugins, helpers, and scripts
815 foreach (array('helper', 'plugin', 'script') as $type)
816 {
817 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
818 {
819 $this->$type($autoload[$type]);
820 }
821 }
822
823 // A little tweak to remain backward compatible
824 // The $autoload['core'] item was deprecated
825 if ( ! isset($autoload['libraries']))
826 {
827 $autoload['libraries'] = $autoload['core'];
828 }
829
830 // Load libraries
831 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
832 {
admin10c3f412006-10-08 07:21:12 +0000833 // Load the database driver.
admin8f0a8f62006-10-07 01:17:25 +0000834 if (in_array('database', $autoload['libraries']))
835 {
836 $this->database();
837 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
838 }
839
admin10c3f412006-10-08 07:21:12 +0000840 // Load the model class.
admin8f0a8f62006-10-07 01:17:25 +0000841 if (in_array('model', $autoload['libraries']))
842 {
843 $this->model();
844 $autoload['libraries'] = array_diff($autoload['libraries'], array('model'));
845 }
admin10c3f412006-10-08 07:21:12 +0000846
847 // Load scaffolding
admin8f0a8f62006-10-07 01:17:25 +0000848 if (in_array('scaffolding', $autoload['libraries']))
849 {
850 $this->scaffolding();
851 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
852 }
853
admin10c3f412006-10-08 07:21:12 +0000854 // Load all other libraries
admin8f0a8f62006-10-07 01:17:25 +0000855 foreach ($autoload['libraries'] as $item)
856 {
857 $this->library($item);
858 }
859 }
860 }
861
862 // --------------------------------------------------------------------
863
864 /**
865 * Assign to Models
866 *
867 * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
868 * will be available to modles, if any exist.
869 *
870 * @access public
871 * @param object
872 * @return array
873 */
874 function _ci_assign_to_models()
adminb0dd10f2006-08-25 17:25:49 +0000875 {
admincf493902006-10-10 07:12:31 +0000876 if (count($this->_ci_models) == 0)
adminb0dd10f2006-08-25 17:25:49 +0000877 {
878 return;
879 }
admincf493902006-10-10 07:12:31 +0000880
881 if ($this->_ci_use_instance)
882 {
883 $CI =& get_instance();
884 foreach ($this->_ci_models as $model)
885 {
886 $CI->$model->_assign_libraries();
887 }
888 }
889 else
890 {
891 foreach ($this->_ci_models as $model)
892 {
893 $this->$model->_assign_libraries();
894 }
895 }
admin8f0a8f62006-10-07 01:17:25 +0000896 }
adminb0dd10f2006-08-25 17:25:49 +0000897
898 // --------------------------------------------------------------------
899
900 /**
901 * Object to Array
902 *
903 * Takes an object as input and convers the class variables to array key/vals
904 *
905 * @access public
906 * @param object
907 * @return array
908 */
909 function _ci_object_to_array($object)
910 {
admin2e5872a2006-09-06 02:32:55 +0000911 return (is_object($object)) ? get_object_vars($object) : $object;
adminb0dd10f2006-08-25 17:25:49 +0000912 }
admin8f0a8f62006-10-07 01:17:25 +0000913
adminb0dd10f2006-08-25 17:25:49 +0000914
915}
adminb0dd10f2006-08-25 17:25:49 +0000916?>