blob: 90d824049f6fbc949a8c188af2ecf5b1be5fd79c [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
31 var $ob_level;
32 var $cached_vars = array();
33 var $helpers = array();
34 var $plugins = array();
35 var $scripts = array();
36 var $languages = array();
37 var $view_path = '';
38
39 /**
40 * Constructor
41 *
42 * Sets the path to the view files and gets the initial output
43 * buffering level
44 *
45 * @access public
46 */
47 function CI_Loader()
48 {
49 $this->view_path = APPPATH.'views/';
50 $this->ob_level = ob_get_level();
51
52 log_message('debug', "Loader Class Initialized");
53 }
adminb0dd10f2006-08-25 17:25:49 +000054
55 // --------------------------------------------------------------------
56
57 /**
58 * Class Loader
59 *
60 * This function lets users load and instantiate classes.
61 * It is designed to be called from a user's app controllers.
62 *
63 * @access public
64 * @param string the name of the class
65 * @param mixed any initialization parameters
66 * @return void
67 */
admin7981a9a2006-09-26 07:52:09 +000068 function library($class, $param = NULL)
adminb0dd10f2006-08-25 17:25:49 +000069 {
70 if ($class == '')
71 return;
72
73 $obj =& get_instance();
admin7981a9a2006-09-26 07:52:09 +000074 $obj->_ci_init_class($class, $param);
adminb0dd10f2006-08-25 17:25:49 +000075 $obj->_ci_assign_to_models();
76 }
adminb0dd10f2006-08-25 17:25:49 +000077
78 // --------------------------------------------------------------------
79
80 /**
81 * Model Loader
82 *
83 * This function lets users load and instantiate models.
84 *
85 * @access public
86 * @param string the name of the class
87 * @param mixed any initialization parameters
88 * @return void
89 */
90 function model($model, $name = '', $db_conn = FALSE)
91 {
92 if ($model == '')
93 return;
94
95 $obj =& get_instance();
admin7981a9a2006-09-26 07:52:09 +000096 $obj->_ci_init_model($model, $name, $db_conn);
adminb0dd10f2006-08-25 17:25:49 +000097 }
adminb0dd10f2006-08-25 17:25:49 +000098
99 // --------------------------------------------------------------------
100
101 /**
102 * Database Loader
103 *
104 * @access public
105 * @param string the DB credentials
106 * @param bool whether to return the DB object
107 * @param bool whether to enable active record (this allows us to override the config setting)
admina5e812c2006-09-25 02:17:30 +0000108 * @return object
adminb0dd10f2006-08-25 17:25:49 +0000109 */
110 function database($db = '', $return = FALSE, $active_record = FALSE)
111 {
112 $obj =& get_instance();
113
114 if ($return === TRUE)
115 {
116 return $obj->_ci_init_database($db, TRUE, $active_record);
117 }
118 else
119 {
120 $obj->_ci_init_database($db, FALSE, $active_record);
121 $obj->_ci_assign_to_models();
122 }
123 }
adminb0dd10f2006-08-25 17:25:49 +0000124
125 // --------------------------------------------------------------------
126
127 /**
admin6e00bab2006-09-26 08:13:06 +0000128 * Database Utiliy Loader
129 *
130 * @access public
131 * @return object
132 */
133 function dbutil()
134 {
135 $obj =& get_instance();
136 $obj->_ci_init_dbextra('dbutil');
137 }
138
139 // --------------------------------------------------------------------
140
141 /**
142 * Database Export Loader
143 *
144 * @access public
145 * @return object
146 */
147 function dbexport()
148 {
149 $obj =& get_instance();
150 $obj->_ci_init_dbextra('dbexport');
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
adminb0dd10f2006-08-25 17:25:49 +0000156 * Scaffolding Loader
157 *
158 * @access public
159 * @param string
160 * @return void
161 */
162 function scaffolding($table = '')
163 {
164 if ($table == FALSE)
165 {
166 show_error('You must include the name of the table you would like access when you initialize scaffolding');
167 }
168
169 $obj =& get_instance();
170 $obj->_ci_init_scaffolding($table);
171 }
adminb0dd10f2006-08-25 17:25:49 +0000172
173 // --------------------------------------------------------------------
174
175 /**
176 * Load View
177 *
178 * This function is used to load a "view" file. It has three parameters:
179 *
180 * 1. The name of the "view" file to be included.
181 * 2. An associative array of data to be extracted for use in the view.
182 * 3. TRUE/FALSE - whether to return the data or load it. In
183 * some cases it's advantageous to be able to retun data so that
184 * a developer can process it in some way.
185 *
186 * @access public
187 * @param string
188 * @param array
189 * @param bool
190 * @return void
191 */
192 function view($view, $vars = array(), $return = FALSE)
193 {
194 return $this->_ci_load(array('view' => $view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return));
195 }
adminb0dd10f2006-08-25 17:25:49 +0000196
197 // --------------------------------------------------------------------
198
199 /**
200 * Load File
201 *
202 * This is a generic file loader
203 *
204 * @access public
205 * @param string
206 * @param bool
207 * @return string
208 */
209 function file($path, $return = FALSE)
210 {
211 return $this->_ci_load(array('path' => $path, 'return' => $return));
212 }
adminb0dd10f2006-08-25 17:25:49 +0000213
214 // --------------------------------------------------------------------
215
216 /**
217 * Set Variables
218 *
219 * Once variables are set they become availabe within
220 * the controller class and its "view" files.
221 *
222 * @access public
223 * @param array
224 * @return void
225 */
226 function vars($vars = array())
227 {
228 $vars = $this->_ci_object_to_array($vars);
229
230 if (is_array($vars) AND count($vars) > 0)
231 {
232 foreach ($vars as $key => $val)
233 {
234 $this->cached_vars[$key] = $val;
235 }
236 }
237 }
adminb0dd10f2006-08-25 17:25:49 +0000238
239 // --------------------------------------------------------------------
240
241 /**
242 * Load Helper
243 *
244 * This function loads the specified helper file.
245 *
246 * @access public
247 * @param mixed
248 * @return void
249 */
250 function helper($helpers = array())
251 {
252 if ( ! is_array($helpers))
253 {
254 $helpers = array($helpers);
255 }
256
257 foreach ($helpers as $helper)
258 {
259 if (isset($this->helpers[$helper]))
260 {
261 continue;
262 }
263
264 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
265
admin480da812006-09-20 21:11:04 +0000266 if (file_exists(APPPATH.'helpers/'.$helper.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000267 {
admin480da812006-09-20 21:11:04 +0000268 include_once(APPPATH.'helpers/'.$helper.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000269 }
admin480da812006-09-20 21:11:04 +0000270 else
271 {
272 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
273 {
274 include_once(BASEPATH.'helpers/'.$helper.EXT);
275 }
276 else
277 {
278 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
279 }
280 }
adminb0dd10f2006-08-25 17:25:49 +0000281
282 $this->helpers[$helper] = TRUE;
283 }
284
285 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
286 }
adminb0dd10f2006-08-25 17:25:49 +0000287
288 // --------------------------------------------------------------------
289
290 /**
291 * Load Helpers
292 *
293 * This is simply an alias to the above function in case the
294 * user has written the plural form of this function.
295 *
296 * @access public
297 * @param array
298 * @return void
299 */
300 function helpers($helpers = array())
301 {
302 $this->helper($helpers);
303 }
adminb0dd10f2006-08-25 17:25:49 +0000304
305 // --------------------------------------------------------------------
306
307 /**
308 * Load Plugin
309 *
310 * This function loads the specified plugin.
311 *
312 * @access public
313 * @param array
314 * @return void
315 */
316 function plugin($plugins = array())
317 {
318 if ( ! is_array($plugins))
319 {
320 $plugins = array($plugins);
321 }
322
323 foreach ($plugins as $plugin)
324 {
325 if (isset($this->plugins[$plugin]))
326 {
327 continue;
328 }
329
admin480da812006-09-20 21:11:04 +0000330 $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi');
adminb0dd10f2006-08-25 17:25:49 +0000331
admin480da812006-09-20 21:11:04 +0000332 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000333 {
admin480da812006-09-20 21:11:04 +0000334 include_once(APPPATH.'plugins/'.$plugin.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000335 }
admin480da812006-09-20 21:11:04 +0000336 else
337 {
338 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
339 {
340 include_once(BASEPATH.'plugins/'.$plugin.EXT);
341 }
342 else
343 {
344 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
345 }
346 }
adminb0dd10f2006-08-25 17:25:49 +0000347
348 $this->plugins[$plugin] = TRUE;
349 }
350
351 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
352 }
adminb0dd10f2006-08-25 17:25:49 +0000353
354 // --------------------------------------------------------------------
355
356 /**
357 * Load Script
358 *
359 * This function loads the specified include file from the
admin83b05a82006-09-25 21:06:46 +0000360 * application/scripts/ folder.
361 *
362 * NOTE: This feature has been deprecated but it will remain available
363 * for legacy users.
adminb0dd10f2006-08-25 17:25:49 +0000364 *
365 * @access public
366 * @param array
367 * @return void
368 */
369 function script($scripts = array())
370 {
371 if ( ! is_array($scripts))
372 {
373 $scripts = array($scripts);
374 }
375
376 foreach ($scripts as $script)
377 {
378 if (isset($this->scripts[$script]))
379 {
380 continue;
381 }
382
383 $script = strtolower(str_replace(EXT, '', $script));
384
385 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
386 {
387 show_error('Unable to load the requested script: scripts/'.$script.EXT);
388 }
389
390 include_once(APPPATH.'scripts/'.$script.EXT);
391
392 $this->scripts[$script] = TRUE;
393 }
394
395 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
396 }
adminb0dd10f2006-08-25 17:25:49 +0000397
398 // --------------------------------------------------------------------
399
400 /**
401 * Load Plugins
402 *
403 * This is simply an alias to the above function in case the
404 * user has written the plural form of this function.
405 *
406 * @access public
407 * @param array
408 * @return void
409 */
410 function plugins($plugins = array())
411 {
412 $this->plugin($plugins);
413 }
adminb0dd10f2006-08-25 17:25:49 +0000414
415 // --------------------------------------------------------------------
416
417 /**
418 * Loads a language file
419 *
420 * @access public
421 * @param string
422 * @return void
423 */
424 function language($file = '', $lang = '', $return = FALSE)
425 {
426 $obj =& get_instance();
427 return $obj->lang->load($file, $lang, $return);
428 }
adminb0dd10f2006-08-25 17:25:49 +0000429
430 // --------------------------------------------------------------------
431
432 /**
433 * Loads a config file
434 *
435 * @access public
436 * @param string
437 * @return void
438 */
439 function config($file = '')
440 {
441 $obj =& get_instance();
442 $obj->config->load($file);
443 }
adminb0dd10f2006-08-25 17:25:49 +0000444
445 // --------------------------------------------------------------------
446
447 /**
448 * Set the Path to the "views" folder
449 *
450 * @access private
451 * @param string
452 * @return void
453 */
454 function _ci_set_view_path($path)
455 {
456 $this->view_path = $path;
457 }
adminb0dd10f2006-08-25 17:25:49 +0000458
459 // --------------------------------------------------------------------
460
461 /**
462 * Loader
463 *
464 * This function isn't called directly. It's called from
465 * the two functions above. It's used to load views and files
466 *
467 * @access private
468 * @param array
469 * @return void
470 */
471 function _ci_load($data)
admin212a3fa2006-09-23 04:22:39 +0000472 {
adminb0dd10f2006-08-25 17:25:49 +0000473 // This allows anything loaded using $this->load (viwes, files, etc.)
474 // to become accessible from within the Controller and Model functions.
475 $obj =& get_instance();
admine79dc712006-09-26 03:52:45 +0000476 foreach (get_object_vars($obj) as $key => $var)
adminb0dd10f2006-08-25 17:25:49 +0000477 {
admine79dc712006-09-26 03:52:45 +0000478 if (is_object($var))
adminb0dd10f2006-08-25 17:25:49 +0000479 {
admine79dc712006-09-26 03:52:45 +0000480 $this->$key =& $obj->$key;
481 }
482 }
483
adminb0dd10f2006-08-25 17:25:49 +0000484 // Set the default data variables
485 foreach (array('view', 'vars', 'path', 'return') as $val)
486 {
487 $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
488 }
489
490 /*
admin83b05a82006-09-25 21:06:46 +0000491 * Extract and cache variables
adminb0dd10f2006-08-25 17:25:49 +0000492 *
admin212a3fa2006-09-23 04:22:39 +0000493 * You can either set variables using the dedicated $this->load_vars()
494 * function or via the second parameter of this function. We'll merge
495 * the two types and cache them so that views that are embedded within
496 * other views can have access to these variables.
adminb0dd10f2006-08-25 17:25:49 +0000497 */
adminb0dd10f2006-08-25 17:25:49 +0000498 if (is_array($vars))
499 {
500 $this->cached_vars = array_merge($this->cached_vars, $vars);
501 }
502 extract($this->cached_vars);
503
504 // Set the path to the requested file
505 if ($path == '')
506 {
507 $ext = pathinfo($view, PATHINFO_EXTENSION);
admin6ac4bea2006-09-02 17:46:15 +0000508 $file = ($ext == '') ? $view.EXT : $view;
adminb0dd10f2006-08-25 17:25:49 +0000509 $path = $this->view_path.$file;
510 }
511 else
512 {
513 $x = explode('/', $path);
514 $file = end($x);
515 }
516
517 /*
518 * Buffer the output
519 *
520 * We buffer the output for two reasons:
521 * 1. Speed. You get a significant speed boost.
522 * 2. So that the final rendered template can be
523 * post-processed by the output class. Why do we
524 * need post processing? For one thing, in order to
525 * show the elapsed page load time. Unless we
526 * can intercept the content right before it's sent to
admin212a3fa2006-09-23 04:22:39 +0000527 * the browser and then stop the timer it won't be acurate.
adminb0dd10f2006-08-25 17:25:49 +0000528 */
adminb0dd10f2006-08-25 17:25:49 +0000529 if ( ! file_exists($path))
530 {
531 show_error('Unable to load the requested file: '.$file);
532 }
533
534 ob_start();
535
536 include($path);
537 log_message('debug', 'File loaded: '.$path);
538
539 // Return the file data if requested to
540 if ($return === TRUE)
541 {
542 $buffer = ob_get_contents();
543 ob_end_clean();
544
545 return $buffer;
546 }
547
548 /*
549 * Flush the buffer... or buff the flusher?
550 *
admin212a3fa2006-09-23 04:22:39 +0000551 * In order to permit views to be nested within
adminb0dd10f2006-08-25 17:25:49 +0000552 * other views, we need to flush the content back out whenever
553 * we are beyond the first level of output buffering so that
554 * it can be seen and included properly by the first included
555 * template and any subsequent ones. Oy!
556 *
557 */
558 if (ob_get_level() > $this->ob_level + 1)
559 {
560 ob_end_flush();
561 }
562 else
563 {
admin212a3fa2006-09-23 04:22:39 +0000564 $obj->output->set_output(ob_get_contents());
adminb0dd10f2006-08-25 17:25:49 +0000565 ob_end_clean();
566 }
567 }
adminb0dd10f2006-08-25 17:25:49 +0000568
569 // --------------------------------------------------------------------
570
571 /**
572 * Autoloader
573 *
574 * The config/autoload.php file contains an array that permits sub-systems,
575 * plugins, and helpers to be loaded automatically.
576 *
577 * @access private
578 * @param array
579 * @return void
580 */
581 function _ci_autoloader($autoload)
582 {
583 if ($autoload === FALSE)
584 {
585 return;
586 }
587
588 foreach (array('helper', 'plugin', 'script') as $type)
589 {
590 if (isset($autoload[$type]))
591 {
admin2e5872a2006-09-06 02:32:55 +0000592 $this->$type($autoload[$type]);
adminb0dd10f2006-08-25 17:25:49 +0000593 }
594 }
595 }
adminb0dd10f2006-08-25 17:25:49 +0000596
597 // --------------------------------------------------------------------
598
599 /**
600 * Object to Array
601 *
602 * Takes an object as input and convers the class variables to array key/vals
603 *
604 * @access public
605 * @param object
606 * @return array
607 */
608 function _ci_object_to_array($object)
609 {
admin2e5872a2006-09-06 02:32:55 +0000610 return (is_object($object)) ? get_object_vars($object) : $object;
adminb0dd10f2006-08-25 17:25:49 +0000611 }
612
613}
adminb0dd10f2006-08-25 17:25:49 +0000614?>