blob: df067cbc1e6440e9ca6a4b84a37a4c887d4dddb7 [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 }
54 // END CI_Loader()
55
56 // --------------------------------------------------------------------
57
58 /**
59 * Class Loader
60 *
61 * This function lets users load and instantiate classes.
62 * It is designed to be called from a user's app controllers.
63 *
64 * @access public
65 * @param string the name of the class
66 * @param mixed any initialization parameters
67 * @return void
68 */
admin7981a9a2006-09-26 07:52:09 +000069 function library($class, $param = NULL)
adminb0dd10f2006-08-25 17:25:49 +000070 {
71 if ($class == '')
72 return;
73
74 $obj =& get_instance();
admin7981a9a2006-09-26 07:52:09 +000075 $obj->_ci_init_class($class, $param);
adminb0dd10f2006-08-25 17:25:49 +000076 $obj->_ci_assign_to_models();
77 }
78 // END library()
79
80 // --------------------------------------------------------------------
81
82 /**
83 * Model Loader
84 *
85 * This function lets users load and instantiate models.
86 *
87 * @access public
88 * @param string the name of the class
89 * @param mixed any initialization parameters
90 * @return void
91 */
92 function model($model, $name = '', $db_conn = FALSE)
93 {
94 if ($model == '')
95 return;
96
97 $obj =& get_instance();
admin7981a9a2006-09-26 07:52:09 +000098 $obj->_ci_init_model($model, $name, $db_conn);
adminb0dd10f2006-08-25 17:25:49 +000099 }
100 // END library()
101
102 // --------------------------------------------------------------------
103
104 /**
105 * Database Loader
106 *
107 * @access public
108 * @param string the DB credentials
109 * @param bool whether to return the DB object
110 * @param bool whether to enable active record (this allows us to override the config setting)
admina5e812c2006-09-25 02:17:30 +0000111 * @return object
adminb0dd10f2006-08-25 17:25:49 +0000112 */
113 function database($db = '', $return = FALSE, $active_record = FALSE)
114 {
115 $obj =& get_instance();
116
117 if ($return === TRUE)
118 {
119 return $obj->_ci_init_database($db, TRUE, $active_record);
120 }
121 else
122 {
123 $obj->_ci_init_database($db, FALSE, $active_record);
124 $obj->_ci_assign_to_models();
125 }
126 }
127 // END database()
128
129 // --------------------------------------------------------------------
130
131 /**
132 * Scaffolding Loader
133 *
134 * @access public
135 * @param string
136 * @return void
137 */
138 function scaffolding($table = '')
139 {
140 if ($table == FALSE)
141 {
142 show_error('You must include the name of the table you would like access when you initialize scaffolding');
143 }
144
145 $obj =& get_instance();
146 $obj->_ci_init_scaffolding($table);
147 }
148 // END scaffolding()
149
150 // --------------------------------------------------------------------
151
152 /**
153 * Load View
154 *
155 * This function is used to load a "view" file. It has three parameters:
156 *
157 * 1. The name of the "view" file to be included.
158 * 2. An associative array of data to be extracted for use in the view.
159 * 3. TRUE/FALSE - whether to return the data or load it. In
160 * some cases it's advantageous to be able to retun data so that
161 * a developer can process it in some way.
162 *
163 * @access public
164 * @param string
165 * @param array
166 * @param bool
167 * @return void
168 */
169 function view($view, $vars = array(), $return = FALSE)
170 {
171 return $this->_ci_load(array('view' => $view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return));
172 }
173 // END view()
174
175 // --------------------------------------------------------------------
176
177 /**
178 * Load File
179 *
180 * This is a generic file loader
181 *
182 * @access public
183 * @param string
184 * @param bool
185 * @return string
186 */
187 function file($path, $return = FALSE)
188 {
189 return $this->_ci_load(array('path' => $path, 'return' => $return));
190 }
191 // END file()
192
193 // --------------------------------------------------------------------
194
195 /**
196 * Set Variables
197 *
198 * Once variables are set they become availabe within
199 * the controller class and its "view" files.
200 *
201 * @access public
202 * @param array
203 * @return void
204 */
205 function vars($vars = array())
206 {
207 $vars = $this->_ci_object_to_array($vars);
208
209 if (is_array($vars) AND count($vars) > 0)
210 {
211 foreach ($vars as $key => $val)
212 {
213 $this->cached_vars[$key] = $val;
214 }
215 }
216 }
217 // END vars()
218
219 // --------------------------------------------------------------------
220
221 /**
222 * Load Helper
223 *
224 * This function loads the specified helper file.
225 *
226 * @access public
227 * @param mixed
228 * @return void
229 */
230 function helper($helpers = array())
231 {
232 if ( ! is_array($helpers))
233 {
234 $helpers = array($helpers);
235 }
236
237 foreach ($helpers as $helper)
238 {
239 if (isset($this->helpers[$helper]))
240 {
241 continue;
242 }
243
244 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
245
admin480da812006-09-20 21:11:04 +0000246 if (file_exists(APPPATH.'helpers/'.$helper.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000247 {
admin480da812006-09-20 21:11:04 +0000248 include_once(APPPATH.'helpers/'.$helper.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000249 }
admin480da812006-09-20 21:11:04 +0000250 else
251 {
252 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
253 {
254 include_once(BASEPATH.'helpers/'.$helper.EXT);
255 }
256 else
257 {
258 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
259 }
260 }
adminb0dd10f2006-08-25 17:25:49 +0000261
262 $this->helpers[$helper] = TRUE;
263 }
264
265 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
266 }
267 // END helper()
268
269 // --------------------------------------------------------------------
270
271 /**
272 * Load Helpers
273 *
274 * This is simply an alias to the above function in case the
275 * user has written the plural form of this function.
276 *
277 * @access public
278 * @param array
279 * @return void
280 */
281 function helpers($helpers = array())
282 {
283 $this->helper($helpers);
284 }
285 // END helpers()
286
287 // --------------------------------------------------------------------
288
289 /**
290 * Load Plugin
291 *
292 * This function loads the specified plugin.
293 *
294 * @access public
295 * @param array
296 * @return void
297 */
298 function plugin($plugins = array())
299 {
300 if ( ! is_array($plugins))
301 {
302 $plugins = array($plugins);
303 }
304
305 foreach ($plugins as $plugin)
306 {
307 if (isset($this->plugins[$plugin]))
308 {
309 continue;
310 }
311
admin480da812006-09-20 21:11:04 +0000312 $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi');
adminb0dd10f2006-08-25 17:25:49 +0000313
admin480da812006-09-20 21:11:04 +0000314 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000315 {
admin480da812006-09-20 21:11:04 +0000316 include_once(APPPATH.'plugins/'.$plugin.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000317 }
admin480da812006-09-20 21:11:04 +0000318 else
319 {
320 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
321 {
322 include_once(BASEPATH.'plugins/'.$plugin.EXT);
323 }
324 else
325 {
326 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
327 }
328 }
adminb0dd10f2006-08-25 17:25:49 +0000329
330 $this->plugins[$plugin] = TRUE;
331 }
332
333 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
334 }
335 // END plugin()
336
337 // --------------------------------------------------------------------
338
339 /**
340 * Load Script
341 *
342 * This function loads the specified include file from the
admin83b05a82006-09-25 21:06:46 +0000343 * application/scripts/ folder.
344 *
345 * NOTE: This feature has been deprecated but it will remain available
346 * for legacy users.
adminb0dd10f2006-08-25 17:25:49 +0000347 *
348 * @access public
349 * @param array
350 * @return void
351 */
352 function script($scripts = array())
353 {
354 if ( ! is_array($scripts))
355 {
356 $scripts = array($scripts);
357 }
358
359 foreach ($scripts as $script)
360 {
361 if (isset($this->scripts[$script]))
362 {
363 continue;
364 }
365
366 $script = strtolower(str_replace(EXT, '', $script));
367
368 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
369 {
370 show_error('Unable to load the requested script: scripts/'.$script.EXT);
371 }
372
373 include_once(APPPATH.'scripts/'.$script.EXT);
374
375 $this->scripts[$script] = TRUE;
376 }
377
378 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
379 }
380 // END script()
381
382 // --------------------------------------------------------------------
383
384 /**
385 * Load Plugins
386 *
387 * This is simply an alias to the above function in case the
388 * user has written the plural form of this function.
389 *
390 * @access public
391 * @param array
392 * @return void
393 */
394 function plugins($plugins = array())
395 {
396 $this->plugin($plugins);
397 }
398 // END plugins()
399
400 // --------------------------------------------------------------------
401
402 /**
403 * Loads a language file
404 *
405 * @access public
406 * @param string
407 * @return void
408 */
409 function language($file = '', $lang = '', $return = FALSE)
410 {
411 $obj =& get_instance();
412 return $obj->lang->load($file, $lang, $return);
413 }
414 // END language()
415
416 // --------------------------------------------------------------------
417
418 /**
419 * Loads a config file
420 *
421 * @access public
422 * @param string
423 * @return void
424 */
425 function config($file = '')
426 {
427 $obj =& get_instance();
428 $obj->config->load($file);
429 }
430 // END config()
431
432 // --------------------------------------------------------------------
433
434 /**
435 * Set the Path to the "views" folder
436 *
437 * @access private
438 * @param string
439 * @return void
440 */
441 function _ci_set_view_path($path)
442 {
443 $this->view_path = $path;
444 }
445 // END _ci_set_view_path()
446
447 // --------------------------------------------------------------------
448
449 /**
450 * Loader
451 *
452 * This function isn't called directly. It's called from
453 * the two functions above. It's used to load views and files
454 *
455 * @access private
456 * @param array
457 * @return void
458 */
459 function _ci_load($data)
admin212a3fa2006-09-23 04:22:39 +0000460 {
adminb0dd10f2006-08-25 17:25:49 +0000461 // This allows anything loaded using $this->load (viwes, files, etc.)
462 // to become accessible from within the Controller and Model functions.
463 $obj =& get_instance();
admine79dc712006-09-26 03:52:45 +0000464 foreach (get_object_vars($obj) as $key => $var)
adminb0dd10f2006-08-25 17:25:49 +0000465 {
admine79dc712006-09-26 03:52:45 +0000466 if (is_object($var))
adminb0dd10f2006-08-25 17:25:49 +0000467 {
admine79dc712006-09-26 03:52:45 +0000468 $this->$key =& $obj->$key;
469 }
470 }
471
adminb0dd10f2006-08-25 17:25:49 +0000472 // Set the default data variables
473 foreach (array('view', 'vars', 'path', 'return') as $val)
474 {
475 $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
476 }
477
478 /*
admin83b05a82006-09-25 21:06:46 +0000479 * Extract and cache variables
adminb0dd10f2006-08-25 17:25:49 +0000480 *
admin212a3fa2006-09-23 04:22:39 +0000481 * You can either set variables using the dedicated $this->load_vars()
482 * function or via the second parameter of this function. We'll merge
483 * the two types and cache them so that views that are embedded within
484 * other views can have access to these variables.
adminb0dd10f2006-08-25 17:25:49 +0000485 */
adminb0dd10f2006-08-25 17:25:49 +0000486 if (is_array($vars))
487 {
488 $this->cached_vars = array_merge($this->cached_vars, $vars);
489 }
490 extract($this->cached_vars);
491
492 // Set the path to the requested file
493 if ($path == '')
494 {
495 $ext = pathinfo($view, PATHINFO_EXTENSION);
admin6ac4bea2006-09-02 17:46:15 +0000496 $file = ($ext == '') ? $view.EXT : $view;
adminb0dd10f2006-08-25 17:25:49 +0000497 $path = $this->view_path.$file;
498 }
499 else
500 {
501 $x = explode('/', $path);
502 $file = end($x);
503 }
504
505 /*
506 * Buffer the output
507 *
508 * We buffer the output for two reasons:
509 * 1. Speed. You get a significant speed boost.
510 * 2. So that the final rendered template can be
511 * post-processed by the output class. Why do we
512 * need post processing? For one thing, in order to
513 * show the elapsed page load time. Unless we
514 * can intercept the content right before it's sent to
admin212a3fa2006-09-23 04:22:39 +0000515 * the browser and then stop the timer it won't be acurate.
adminb0dd10f2006-08-25 17:25:49 +0000516 */
adminb0dd10f2006-08-25 17:25:49 +0000517 if ( ! file_exists($path))
518 {
519 show_error('Unable to load the requested file: '.$file);
520 }
521
522 ob_start();
523
524 include($path);
525 log_message('debug', 'File loaded: '.$path);
526
527 // Return the file data if requested to
528 if ($return === TRUE)
529 {
530 $buffer = ob_get_contents();
531 ob_end_clean();
532
533 return $buffer;
534 }
535
536 /*
537 * Flush the buffer... or buff the flusher?
538 *
admin212a3fa2006-09-23 04:22:39 +0000539 * In order to permit views to be nested within
adminb0dd10f2006-08-25 17:25:49 +0000540 * other views, we need to flush the content back out whenever
541 * we are beyond the first level of output buffering so that
542 * it can be seen and included properly by the first included
543 * template and any subsequent ones. Oy!
544 *
545 */
546 if (ob_get_level() > $this->ob_level + 1)
547 {
548 ob_end_flush();
549 }
550 else
551 {
admin212a3fa2006-09-23 04:22:39 +0000552 $obj->output->set_output(ob_get_contents());
adminb0dd10f2006-08-25 17:25:49 +0000553 ob_end_clean();
554 }
555 }
556 // END _load()
557
558 // --------------------------------------------------------------------
559
560 /**
561 * Autoloader
562 *
563 * The config/autoload.php file contains an array that permits sub-systems,
564 * plugins, and helpers to be loaded automatically.
565 *
566 * @access private
567 * @param array
568 * @return void
569 */
570 function _ci_autoloader($autoload)
571 {
572 if ($autoload === FALSE)
573 {
574 return;
575 }
576
577 foreach (array('helper', 'plugin', 'script') as $type)
578 {
579 if (isset($autoload[$type]))
580 {
admin2e5872a2006-09-06 02:32:55 +0000581 $this->$type($autoload[$type]);
adminb0dd10f2006-08-25 17:25:49 +0000582 }
583 }
584 }
585 // END _ci_autoloader()
586
587 // --------------------------------------------------------------------
588
589 /**
590 * Object to Array
591 *
592 * Takes an object as input and convers the class variables to array key/vals
593 *
594 * @access public
595 * @param object
596 * @return array
597 */
598 function _ci_object_to_array($object)
599 {
admin2e5872a2006-09-06 02:32:55 +0000600 return (is_object($object)) ? get_object_vars($object) : $object;
adminb0dd10f2006-08-25 17:25:49 +0000601 }
admin2e5872a2006-09-06 02:32:55 +0000602 // END _ci_object_to_array()
adminb0dd10f2006-08-25 17:25:49 +0000603
604}
adminb0dd10f2006-08-25 17:25:49 +0000605?>