blob: dc588bb0c2ebbde1d0a5d3e447121aa43860a90b [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
admin8f0a8f62006-10-07 01:17:25 +000031 var $CI;
adminb0dd10f2006-08-25 17:25:49 +000032 var $ob_level;
adminb1fddc02006-10-09 21:29:07 +000033 var $view_path = '';
adminb0dd10f2006-08-25 17:25:49 +000034 var $cached_vars = array();
adminb3ab70b2006-10-07 03:07:29 +000035 var $models = array();
adminb0dd10f2006-08-25 17:25:49 +000036 var $helpers = array();
37 var $plugins = array();
38 var $scripts = array();
adminb1fddc02006-10-09 21:29:07 +000039 var $varmap = array('unit_test' => 'unit', 'user_agent' => 'agent');
adminb0dd10f2006-08-25 17:25:49 +000040
41 /**
42 * Constructor
43 *
44 * Sets the path to the view files and gets the initial output
45 * buffering level
46 *
47 * @access public
48 */
49 function CI_Loader()
50 {
admin8f0a8f62006-10-07 01:17:25 +000051 $this->CI =& get_instance();
52
adminb0dd10f2006-08-25 17:25:49 +000053 $this->view_path = APPPATH.'views/';
54 $this->ob_level = ob_get_level();
55
56 log_message('debug', "Loader Class Initialized");
57 }
adminb0dd10f2006-08-25 17:25:49 +000058
59 // --------------------------------------------------------------------
60
61 /**
62 * Class Loader
63 *
64 * This function lets users load and instantiate classes.
65 * It is designed to be called from a user's app controllers.
66 *
67 * @access public
68 * @param string the name of the class
admin2f0bac82006-10-09 17:36:45 +000069 * @param sring the optional class variable name to assign the library to
adminb0dd10f2006-08-25 17:25:49 +000070 * @return void
71 */
admin2f0bac82006-10-09 17:36:45 +000072 function library($class, $varname = NULL)
adminb0dd10f2006-08-25 17:25:49 +000073 {
74 if ($class == '')
75 return;
76
admin2f0bac82006-10-09 17:36:45 +000077 $this->_ci_load_class($class, $varname);
admin8f0a8f62006-10-07 01:17:25 +000078 $this->_ci_assign_to_models();
adminb0dd10f2006-08-25 17:25:49 +000079 }
adminb0dd10f2006-08-25 17:25:49 +000080
81 // --------------------------------------------------------------------
82
83 /**
84 * Model Loader
85 *
86 * This function lets users load and instantiate models.
87 *
88 * @access public
89 * @param string the name of the class
90 * @param mixed any initialization parameters
91 * @return void
92 */
93 function model($model, $name = '', $db_conn = FALSE)
94 {
95 if ($model == '')
96 return;
97
admin8f0a8f62006-10-07 01:17:25 +000098 // Is the model in a sub-folder?
99 // If so, parse out the filename and path.
100 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
117 if (in_array($name, $this->models, TRUE))
118 {
119 return;
120 }
121
122 if (isset($this->CI->$name))
123 {
124 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
125 }
126
127 $model = strtolower($model);
128
129 if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
130 {
131 show_error('Unable to locate the model you have specified: '.$model);
132 }
133
134 if ($db_conn !== FALSE)
135 {
136 if ($db_conn === TRUE)
137 $db_conn = '';
138
139 $this->CI->load->database($db_conn, FALSE, TRUE);
140 }
141
142 if ( ! class_exists('Model'))
143 {
144 require_once(BASEPATH.'libraries/Model'.EXT);
145 }
146
147 require_once(APPPATH.'models/'.$path.$model.EXT);
148
149 $model = ucfirst($model);
150 $this->CI->$name = new $model();
151 $this->models[] = $name;
152 $this->_ci_assign_to_models();
adminb0dd10f2006-08-25 17:25:49 +0000153 }
adminb0dd10f2006-08-25 17:25:49 +0000154
155 // --------------------------------------------------------------------
156
157 /**
158 * Database Loader
159 *
160 * @access public
161 * @param string the DB credentials
162 * @param bool whether to return the DB object
163 * @param bool whether to enable active record (this allows us to override the config setting)
admina5e812c2006-09-25 02:17:30 +0000164 * @return object
adminb0dd10f2006-08-25 17:25:49 +0000165 */
admin8f0a8f62006-10-07 01:17:25 +0000166 function database($params = '', $return = FALSE, $active_record = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000167 {
admin8f0a8f62006-10-07 01:17:25 +0000168 require_once(BASEPATH.'database/DB'.EXT);
adminfd2750b2006-10-06 17:29:12 +0000169
adminb0dd10f2006-08-25 17:25:49 +0000170 if ($return === TRUE)
171 {
admin8f0a8f62006-10-07 01:17:25 +0000172 return DB($params, $return, $active_record);
adminb0dd10f2006-08-25 17:25:49 +0000173 }
174 else
175 {
admin8f0a8f62006-10-07 01:17:25 +0000176 DB($params, $return, $active_record);
177 $this->_ci_assign_to_models();
adminb0dd10f2006-08-25 17:25:49 +0000178 }
179 }
adminb0dd10f2006-08-25 17:25:49 +0000180
adminb0dd10f2006-08-25 17:25:49 +0000181 // --------------------------------------------------------------------
182
183 /**
184 * Load View
185 *
186 * This function is used to load a "view" file. It has three parameters:
187 *
188 * 1. The name of the "view" file to be included.
189 * 2. An associative array of data to be extracted for use in the view.
190 * 3. TRUE/FALSE - whether to return the data or load it. In
191 * some cases it's advantageous to be able to retun data so that
192 * a developer can process it in some way.
193 *
194 * @access public
195 * @param string
196 * @param array
197 * @param bool
198 * @return void
199 */
200 function view($view, $vars = array(), $return = FALSE)
201 {
202 return $this->_ci_load(array('view' => $view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return));
203 }
adminb0dd10f2006-08-25 17:25:49 +0000204
205 // --------------------------------------------------------------------
206
207 /**
208 * Load File
209 *
210 * This is a generic file loader
211 *
212 * @access public
213 * @param string
214 * @param bool
215 * @return string
216 */
217 function file($path, $return = FALSE)
218 {
219 return $this->_ci_load(array('path' => $path, 'return' => $return));
220 }
adminb0dd10f2006-08-25 17:25:49 +0000221
222 // --------------------------------------------------------------------
223
224 /**
225 * Set Variables
226 *
227 * Once variables are set they become availabe within
228 * the controller class and its "view" files.
229 *
230 * @access public
231 * @param array
232 * @return void
233 */
234 function vars($vars = array())
235 {
236 $vars = $this->_ci_object_to_array($vars);
237
238 if (is_array($vars) AND count($vars) > 0)
239 {
240 foreach ($vars as $key => $val)
241 {
242 $this->cached_vars[$key] = $val;
243 }
244 }
245 }
adminb0dd10f2006-08-25 17:25:49 +0000246
247 // --------------------------------------------------------------------
248
249 /**
250 * Load Helper
251 *
252 * This function loads the specified helper file.
253 *
254 * @access public
255 * @param mixed
256 * @return void
257 */
258 function helper($helpers = array())
259 {
260 if ( ! is_array($helpers))
261 {
262 $helpers = array($helpers);
263 }
264
265 foreach ($helpers as $helper)
266 {
267 if (isset($this->helpers[$helper]))
268 {
269 continue;
270 }
271
272 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
273
admin480da812006-09-20 21:11:04 +0000274 if (file_exists(APPPATH.'helpers/'.$helper.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000275 {
admin480da812006-09-20 21:11:04 +0000276 include_once(APPPATH.'helpers/'.$helper.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000277 }
admin480da812006-09-20 21:11:04 +0000278 else
279 {
280 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
281 {
282 include_once(BASEPATH.'helpers/'.$helper.EXT);
283 }
284 else
285 {
286 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
287 }
288 }
adminb0dd10f2006-08-25 17:25:49 +0000289
290 $this->helpers[$helper] = TRUE;
291 }
292
293 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
294 }
adminb0dd10f2006-08-25 17:25:49 +0000295
296 // --------------------------------------------------------------------
297
298 /**
299 * Load Helpers
300 *
301 * This is simply an alias to the above function in case the
302 * user has written the plural form of this function.
303 *
304 * @access public
305 * @param array
306 * @return void
307 */
308 function helpers($helpers = array())
309 {
310 $this->helper($helpers);
311 }
adminb0dd10f2006-08-25 17:25:49 +0000312
313 // --------------------------------------------------------------------
314
315 /**
316 * Load Plugin
317 *
318 * This function loads the specified plugin.
319 *
320 * @access public
321 * @param array
322 * @return void
323 */
324 function plugin($plugins = array())
325 {
326 if ( ! is_array($plugins))
327 {
328 $plugins = array($plugins);
329 }
330
331 foreach ($plugins as $plugin)
332 {
333 if (isset($this->plugins[$plugin]))
334 {
335 continue;
336 }
337
admin480da812006-09-20 21:11:04 +0000338 $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi');
adminb0dd10f2006-08-25 17:25:49 +0000339
admin480da812006-09-20 21:11:04 +0000340 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000341 {
admin480da812006-09-20 21:11:04 +0000342 include_once(APPPATH.'plugins/'.$plugin.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000343 }
admin480da812006-09-20 21:11:04 +0000344 else
345 {
346 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
347 {
348 include_once(BASEPATH.'plugins/'.$plugin.EXT);
349 }
350 else
351 {
352 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
353 }
354 }
adminb0dd10f2006-08-25 17:25:49 +0000355
356 $this->plugins[$plugin] = TRUE;
357 }
358
359 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
360 }
adminfd2750b2006-10-06 17:29:12 +0000361
362 // --------------------------------------------------------------------
adminb0dd10f2006-08-25 17:25:49 +0000363
adminfd2750b2006-10-06 17:29:12 +0000364 /**
365 * Load Plugins
366 *
367 * This is simply an alias to the above function in case the
368 * user has written the plural form of this function.
369 *
370 * @access public
371 * @param array
372 * @return void
373 */
374 function plugins($plugins = array())
375 {
376 $this->plugin($plugins);
377 }
378
adminb0dd10f2006-08-25 17:25:49 +0000379 // --------------------------------------------------------------------
380
381 /**
382 * Load Script
383 *
384 * This function loads the specified include file from the
admin83b05a82006-09-25 21:06:46 +0000385 * application/scripts/ folder.
386 *
387 * NOTE: This feature has been deprecated but it will remain available
388 * for legacy users.
adminb0dd10f2006-08-25 17:25:49 +0000389 *
390 * @access public
391 * @param array
392 * @return void
393 */
394 function script($scripts = array())
395 {
396 if ( ! is_array($scripts))
397 {
398 $scripts = array($scripts);
399 }
400
401 foreach ($scripts as $script)
402 {
403 if (isset($this->scripts[$script]))
404 {
405 continue;
406 }
407
408 $script = strtolower(str_replace(EXT, '', $script));
409
410 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
411 {
412 show_error('Unable to load the requested script: scripts/'.$script.EXT);
413 }
414
415 include_once(APPPATH.'scripts/'.$script.EXT);
416
417 $this->scripts[$script] = TRUE;
418 }
419
420 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
421 }
adminfd2750b2006-10-06 17:29:12 +0000422
adminb0dd10f2006-08-25 17:25:49 +0000423 // --------------------------------------------------------------------
424
425 /**
426 * Loads a language file
427 *
428 * @access public
429 * @param string
430 * @return void
431 */
432 function language($file = '', $lang = '', $return = FALSE)
433 {
admin8f0a8f62006-10-07 01:17:25 +0000434 return $this->CI->lang->load($file, $lang, $return);
adminb0dd10f2006-08-25 17:25:49 +0000435 }
adminb0dd10f2006-08-25 17:25:49 +0000436
437 // --------------------------------------------------------------------
438
439 /**
440 * Loads a config file
441 *
442 * @access public
443 * @param string
444 * @return void
445 */
446 function config($file = '')
447 {
admin8f0a8f62006-10-07 01:17:25 +0000448 $this->CI->config->load($file);
adminb0dd10f2006-08-25 17:25:49 +0000449 }
admin8f0a8f62006-10-07 01:17:25 +0000450
adminb0dd10f2006-08-25 17:25:49 +0000451 // --------------------------------------------------------------------
452
453 /**
admin8f0a8f62006-10-07 01:17:25 +0000454 * Scaffolding Loader
adminb0dd10f2006-08-25 17:25:49 +0000455 *
admin8f0a8f62006-10-07 01:17:25 +0000456 * This initializing function works a bit different than the
457 * others. It doesn't load the class. Instead, it simply
458 * sets a flag indicating that scaffolding is allowed to be
459 * used. The actual scaffolding function below is
460 * called by the front controller based on whether the
461 * second segment of the URL matches the "secret" scaffolding
462 * word stored in the application/config/routes.php
463 *
464 * @access public
adminb0dd10f2006-08-25 17:25:49 +0000465 * @param string
466 * @return void
admin8f0a8f62006-10-07 01:17:25 +0000467 */
468 function scaffolding($table = '')
469 {
470 if ($table === FALSE)
471 {
472 show_error('You must include the name of the table you would like access when you initialize scaffolding');
473 }
474
475 $this->CI->_ci_scaffolding = TRUE;
476 $this->CI->_ci_scaff_table = $table;
adminb0dd10f2006-08-25 17:25:49 +0000477 }
admin8f0a8f62006-10-07 01:17:25 +0000478
adminb0dd10f2006-08-25 17:25:49 +0000479 // --------------------------------------------------------------------
admin8f0a8f62006-10-07 01:17:25 +0000480
adminb0dd10f2006-08-25 17:25:49 +0000481 /**
482 * Loader
483 *
admin8f0a8f62006-10-07 01:17:25 +0000484 * This function is used to load views and files.
adminb0dd10f2006-08-25 17:25:49 +0000485 *
486 * @access private
487 * @param array
488 * @return void
489 */
490 function _ci_load($data)
admin212a3fa2006-09-23 04:22:39 +0000491 {
adminb0dd10f2006-08-25 17:25:49 +0000492 // This allows anything loaded using $this->load (viwes, files, etc.)
493 // to become accessible from within the Controller and Model functions.
admin8f0a8f62006-10-07 01:17:25 +0000494 foreach (get_object_vars($this->CI) as $key => $var)
adminb0dd10f2006-08-25 17:25:49 +0000495 {
admine79dc712006-09-26 03:52:45 +0000496 if (is_object($var))
adminb0dd10f2006-08-25 17:25:49 +0000497 {
admin8f0a8f62006-10-07 01:17:25 +0000498 $this->$key =& $this->CI->$key;
admine79dc712006-09-26 03:52:45 +0000499 }
500 }
501
adminb0dd10f2006-08-25 17:25:49 +0000502 // Set the default data variables
503 foreach (array('view', 'vars', 'path', 'return') as $val)
504 {
505 $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
506 }
507
508 /*
admin83b05a82006-09-25 21:06:46 +0000509 * Extract and cache variables
adminb0dd10f2006-08-25 17:25:49 +0000510 *
admin212a3fa2006-09-23 04:22:39 +0000511 * You can either set variables using the dedicated $this->load_vars()
512 * function or via the second parameter of this function. We'll merge
513 * the two types and cache them so that views that are embedded within
514 * other views can have access to these variables.
adminb0dd10f2006-08-25 17:25:49 +0000515 */
adminb0dd10f2006-08-25 17:25:49 +0000516 if (is_array($vars))
517 {
518 $this->cached_vars = array_merge($this->cached_vars, $vars);
519 }
520 extract($this->cached_vars);
521
522 // Set the path to the requested file
523 if ($path == '')
524 {
525 $ext = pathinfo($view, PATHINFO_EXTENSION);
admin6ac4bea2006-09-02 17:46:15 +0000526 $file = ($ext == '') ? $view.EXT : $view;
adminb0dd10f2006-08-25 17:25:49 +0000527 $path = $this->view_path.$file;
528 }
529 else
530 {
531 $x = explode('/', $path);
532 $file = end($x);
533 }
534
535 /*
536 * Buffer the output
537 *
538 * We buffer the output for two reasons:
539 * 1. Speed. You get a significant speed boost.
540 * 2. So that the final rendered template can be
541 * post-processed by the output class. Why do we
542 * need post processing? For one thing, in order to
543 * show the elapsed page load time. Unless we
544 * can intercept the content right before it's sent to
admin212a3fa2006-09-23 04:22:39 +0000545 * the browser and then stop the timer it won't be acurate.
adminb0dd10f2006-08-25 17:25:49 +0000546 */
adminb0dd10f2006-08-25 17:25:49 +0000547 if ( ! file_exists($path))
548 {
549 show_error('Unable to load the requested file: '.$file);
550 }
551
552 ob_start();
553
554 include($path);
555 log_message('debug', 'File loaded: '.$path);
556
557 // Return the file data if requested to
558 if ($return === TRUE)
559 {
560 $buffer = ob_get_contents();
561 ob_end_clean();
562
563 return $buffer;
564 }
565
566 /*
567 * Flush the buffer... or buff the flusher?
568 *
admin212a3fa2006-09-23 04:22:39 +0000569 * In order to permit views to be nested within
adminb0dd10f2006-08-25 17:25:49 +0000570 * other views, we need to flush the content back out whenever
571 * we are beyond the first level of output buffering so that
572 * it can be seen and included properly by the first included
573 * template and any subsequent ones. Oy!
574 *
575 */
576 if (ob_get_level() > $this->ob_level + 1)
577 {
578 ob_end_flush();
579 }
580 else
581 {
admin8f0a8f62006-10-07 01:17:25 +0000582 $this->CI->output->set_output(ob_get_contents());
adminb0dd10f2006-08-25 17:25:49 +0000583 ob_end_clean();
584 }
585 }
admin8f0a8f62006-10-07 01:17:25 +0000586
587 // --------------------------------------------------------------------
588
589 /**
590 * Load class
591 *
592 * This function loads the requested class.
593 *
594 * @access private
595 * @param string the item that is being loaded
596 * @param mixed any additional parameters
597 * @return void
598 */
admin2f0bac82006-10-09 17:36:45 +0000599 function _ci_load_class($class, $varname = NULL)
admin8f0a8f62006-10-07 01:17:25 +0000600 {
601 // Prep the class name
602 $class = strtolower(str_replace(EXT, '', $class));
603
604 // Bug fix for backward compat.
605 // Kill this at some point in the future
606 if ($class == 'unit_test')
607 {
608 $class = 'unit';
609 }
610
611 // Is this a class extension request?
612 if (substr($class, 0, 3) == 'my_')
613 {
614 $class = preg_replace("/my_(.+)/", "\\1", $class);
admin10c3f412006-10-08 07:21:12 +0000615
admin8f0a8f62006-10-07 01:17:25 +0000616 // Load the requested library from the main system/libraries folder
617 if (file_exists(BASEPATH.'libraries/'.ucfirst($class).EXT))
618 {
619 include_once(BASEPATH.'libraries/'.ucfirst($class).EXT);
620 }
621
622 // Now look for a matching library
623 foreach (array(ucfirst($class), $class) as $filename)
624 {
625 if (file_exists(APPPATH.'libraries/'.$filename.EXT))
626 {
627 include_once(APPPATH.'libraries/'.$filename.EXT);
628 }
629 }
630
admin2f0bac82006-10-09 17:36:45 +0000631 return $this->_ci_init_class($filename, 'MY_', $varname);
admin8f0a8f62006-10-07 01:17:25 +0000632 }
admin10c3f412006-10-08 07:21:12 +0000633
634 // Lets search for the requested library file and load it.
635 // For backward compatibility we'll test for filenames that are
636 // both uppercase and lower.
637 foreach (array(ucfirst($class), $class) as $filename)
638 {
639 for ($i = 1; $i < 3; $i++)
admin8f0a8f62006-10-07 01:17:25 +0000640 {
admin10c3f412006-10-08 07:21:12 +0000641 $path = ($i % 2) ? APPPATH : BASEPATH;
642
643 if (file_exists($path.'libraries/'.$filename.EXT))
admin8f0a8f62006-10-07 01:17:25 +0000644 {
admin10c3f412006-10-08 07:21:12 +0000645 include_once($path.'libraries/'.$filename.EXT);
admin2f0bac82006-10-09 17:36:45 +0000646 return $this->_ci_init_class($filename, '', $varname);
admin8f0a8f62006-10-07 01:17:25 +0000647 }
648 }
649 }
650
651 // If we got this far we were unable to find the requested class
652 log_message('error', "Unable to load the requested class: ".$class);
653 show_error("Unable to load the class: ".$class);
654 }
655
656 // --------------------------------------------------------------------
657
658 /**
659 * Instantiates a class
660 *
661 * @access private
662 * @param string
663 * @param string
664 * @return null
665 */
admin2f0bac82006-10-09 17:36:45 +0000666 function _ci_init_class($class, $prefix = '', $varname = NULL)
admin8f0a8f62006-10-07 01:17:25 +0000667 {
668 // Is there an associated config file for this class?
adminb1fddc02006-10-09 21:29:07 +0000669 $config = NULL;
admin2f0bac82006-10-09 17:36:45 +0000670 if (file_exists(APPPATH.'config/'.$class.EXT))
admin8f0a8f62006-10-07 01:17:25 +0000671 {
admin2f0bac82006-10-09 17:36:45 +0000672 include_once(APPPATH.'config/'.$class.EXT);
admin8f0a8f62006-10-07 01:17:25 +0000673 }
674
675 if ($prefix == '')
676 {
admin10c3f412006-10-08 07:21:12 +0000677 $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
admin8f0a8f62006-10-07 01:17:25 +0000678 }
679 else
680 {
681 $name = $prefix.$class;
682 }
admin2f0bac82006-10-09 17:36:45 +0000683
adminb1fddc02006-10-09 21:29:07 +0000684 // Set the variable name we will assign the class to
685 if ( ! is_null($varname))
686 {
687 $classvar = $varname;
688 }
689 else
690 {
691 $class = strtolower($class);
692 $classvar = ( ! isset($this->varmap[$class])) ? $class : $this->varmap[$class];
693 }
admin8f0a8f62006-10-07 01:17:25 +0000694
695 // Instantiate the class
696 if ($config !== NULL)
697 {
admin2f0bac82006-10-09 17:36:45 +0000698 $this->CI->$classvar = new $name($config);
admin8f0a8f62006-10-07 01:17:25 +0000699 }
700 else
701 {
admin2f0bac82006-10-09 17:36:45 +0000702 $this->CI->$classvar = new $name;
admin8f0a8f62006-10-07 01:17:25 +0000703 }
704 }
adminb0dd10f2006-08-25 17:25:49 +0000705
706 // --------------------------------------------------------------------
707
708 /**
709 * Autoloader
710 *
711 * The config/autoload.php file contains an array that permits sub-systems,
admin8f0a8f62006-10-07 01:17:25 +0000712 * libraries, plugins, and helpers to be loaded automatically.
adminb0dd10f2006-08-25 17:25:49 +0000713 *
714 * @access private
715 * @param array
716 * @return void
717 */
admin8f0a8f62006-10-07 01:17:25 +0000718 function _ci_autoloader()
719 {
720 include_once(APPPATH.'config/autoload'.EXT);
721
722 if ( ! isset($autoload))
723 {
724 return FALSE;
725 }
726
727 // Load any custome config file
728 if (count($autoload['config']) > 0)
729 {
730 foreach ($autoload['config'] as $key => $val)
731 {
732 $this->CI->config->load($val);
733 }
734 }
735
736 // Load plugins, helpers, and scripts
737 foreach (array('helper', 'plugin', 'script') as $type)
738 {
739 if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
740 {
741 $this->$type($autoload[$type]);
742 }
743 }
744
745 // A little tweak to remain backward compatible
746 // The $autoload['core'] item was deprecated
747 if ( ! isset($autoload['libraries']))
748 {
749 $autoload['libraries'] = $autoload['core'];
750 }
751
752 // Load libraries
753 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
754 {
admin10c3f412006-10-08 07:21:12 +0000755 // Load the database driver.
admin8f0a8f62006-10-07 01:17:25 +0000756 if (in_array('database', $autoload['libraries']))
757 {
758 $this->database();
759 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
760 }
761
admin10c3f412006-10-08 07:21:12 +0000762 // Load the model class.
admin8f0a8f62006-10-07 01:17:25 +0000763 if (in_array('model', $autoload['libraries']))
764 {
765 $this->model();
766 $autoload['libraries'] = array_diff($autoload['libraries'], array('model'));
767 }
admin10c3f412006-10-08 07:21:12 +0000768
769 // Load scaffolding
admin8f0a8f62006-10-07 01:17:25 +0000770 if (in_array('scaffolding', $autoload['libraries']))
771 {
772 $this->scaffolding();
773 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
774 }
775
admin10c3f412006-10-08 07:21:12 +0000776 // Load all other libraries
admin8f0a8f62006-10-07 01:17:25 +0000777 foreach ($autoload['libraries'] as $item)
778 {
779 $this->library($item);
780 }
781 }
782 }
783
784 // --------------------------------------------------------------------
785
786 /**
787 * Assign to Models
788 *
789 * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
790 * will be available to modles, if any exist.
791 *
792 * @access public
793 * @param object
794 * @return array
795 */
796 function _ci_assign_to_models()
adminb0dd10f2006-08-25 17:25:49 +0000797 {
admin8f0a8f62006-10-07 01:17:25 +0000798 if (count($this->models) == 0)
adminb0dd10f2006-08-25 17:25:49 +0000799 {
800 return;
801 }
admin8f0a8f62006-10-07 01:17:25 +0000802 foreach ($this->models as $model)
803 {
804 $this->CI->$model->_assign_libraries();
805 }
806 }
adminb0dd10f2006-08-25 17:25:49 +0000807
808 // --------------------------------------------------------------------
809
810 /**
811 * Object to Array
812 *
813 * Takes an object as input and convers the class variables to array key/vals
814 *
815 * @access public
816 * @param object
817 * @return array
818 */
819 function _ci_object_to_array($object)
820 {
admin2e5872a2006-09-06 02:32:55 +0000821 return (is_object($object)) ? get_object_vars($object) : $object;
adminb0dd10f2006-08-25 17:25:49 +0000822 }
admin8f0a8f62006-10-07 01:17:25 +0000823
adminb0dd10f2006-08-25 17:25:49 +0000824
825}
adminb0dd10f2006-08-25 17:25:49 +0000826?>