blob: 833e376409b66d40aef93bd0f08400fb3efd0bf4 [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 */
69 function library($class, $param = FALSE)
70 {
71 if ($class == '')
72 return;
73
74 $obj =& get_instance();
75 $obj->_ci_initialize($class, $param);
76 $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();
98 $obj->_ci_load_model($model, $name, $db_conn);
99 }
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()
admina5e812c2006-09-25 02:17:30 +0000128
129 // --------------------------------------------------------------------
130
131 /**
132 * Database Utilities Loader
133 *
134 * @access public
135 * @param string the DB platform
136 * @param bool whether to return the DB object
137 * @return object
138 */
admin9cd4e8e2006-09-25 23:26:25 +0000139 function dbutil($db = '', $return = FALSE)
admina5e812c2006-09-25 02:17:30 +0000140 {
141 $obj =& get_instance();
142
143 if ( ! is_bool($return))
144 {
145 $return = FALSE;
146 }
147
admin9cd4e8e2006-09-25 23:26:25 +0000148 return $obj->_ci_init_dbutil($db, $return);
admina5e812c2006-09-25 02:17:30 +0000149 }
150 // END dbutils()
adminb0dd10f2006-08-25 17:25:49 +0000151
152 // --------------------------------------------------------------------
153
154 /**
155 * Scaffolding Loader
156 *
157 * @access public
158 * @param string
159 * @return void
160 */
161 function scaffolding($table = '')
162 {
163 if ($table == FALSE)
164 {
165 show_error('You must include the name of the table you would like access when you initialize scaffolding');
166 }
167
168 $obj =& get_instance();
169 $obj->_ci_init_scaffolding($table);
170 }
171 // END scaffolding()
172
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 }
196 // END view()
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Load File
202 *
203 * This is a generic file loader
204 *
205 * @access public
206 * @param string
207 * @param bool
208 * @return string
209 */
210 function file($path, $return = FALSE)
211 {
212 return $this->_ci_load(array('path' => $path, 'return' => $return));
213 }
214 // END file()
215
216 // --------------------------------------------------------------------
217
218 /**
219 * Set Variables
220 *
221 * Once variables are set they become availabe within
222 * the controller class and its "view" files.
223 *
224 * @access public
225 * @param array
226 * @return void
227 */
228 function vars($vars = array())
229 {
230 $vars = $this->_ci_object_to_array($vars);
231
232 if (is_array($vars) AND count($vars) > 0)
233 {
234 foreach ($vars as $key => $val)
235 {
236 $this->cached_vars[$key] = $val;
237 }
238 }
239 }
240 // END vars()
241
242 // --------------------------------------------------------------------
243
244 /**
245 * Load Helper
246 *
247 * This function loads the specified helper file.
248 *
249 * @access public
250 * @param mixed
251 * @return void
252 */
253 function helper($helpers = array())
254 {
255 if ( ! is_array($helpers))
256 {
257 $helpers = array($helpers);
258 }
259
260 foreach ($helpers as $helper)
261 {
262 if (isset($this->helpers[$helper]))
263 {
264 continue;
265 }
266
267 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
268
admin480da812006-09-20 21:11:04 +0000269 if (file_exists(APPPATH.'helpers/'.$helper.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000270 {
admin480da812006-09-20 21:11:04 +0000271 include_once(APPPATH.'helpers/'.$helper.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000272 }
admin480da812006-09-20 21:11:04 +0000273 else
274 {
275 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
276 {
277 include_once(BASEPATH.'helpers/'.$helper.EXT);
278 }
279 else
280 {
281 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
282 }
283 }
adminb0dd10f2006-08-25 17:25:49 +0000284
285 $this->helpers[$helper] = TRUE;
286 }
287
288 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
289 }
290 // END helper()
291
292 // --------------------------------------------------------------------
293
294 /**
295 * Load Helpers
296 *
297 * This is simply an alias to the above function in case the
298 * user has written the plural form of this function.
299 *
300 * @access public
301 * @param array
302 * @return void
303 */
304 function helpers($helpers = array())
305 {
306 $this->helper($helpers);
307 }
308 // END helpers()
309
310 // --------------------------------------------------------------------
311
312 /**
313 * Load Plugin
314 *
315 * This function loads the specified plugin.
316 *
317 * @access public
318 * @param array
319 * @return void
320 */
321 function plugin($plugins = array())
322 {
323 if ( ! is_array($plugins))
324 {
325 $plugins = array($plugins);
326 }
327
328 foreach ($plugins as $plugin)
329 {
330 if (isset($this->plugins[$plugin]))
331 {
332 continue;
333 }
334
admin480da812006-09-20 21:11:04 +0000335 $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi');
adminb0dd10f2006-08-25 17:25:49 +0000336
admin480da812006-09-20 21:11:04 +0000337 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000338 {
admin480da812006-09-20 21:11:04 +0000339 include_once(APPPATH.'plugins/'.$plugin.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000340 }
admin480da812006-09-20 21:11:04 +0000341 else
342 {
343 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
344 {
345 include_once(BASEPATH.'plugins/'.$plugin.EXT);
346 }
347 else
348 {
349 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
350 }
351 }
adminb0dd10f2006-08-25 17:25:49 +0000352
353 $this->plugins[$plugin] = TRUE;
354 }
355
356 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
357 }
358 // END plugin()
359
360 // --------------------------------------------------------------------
361
362 /**
363 * Load Script
364 *
365 * This function loads the specified include file from the
admin83b05a82006-09-25 21:06:46 +0000366 * application/scripts/ folder.
367 *
368 * NOTE: This feature has been deprecated but it will remain available
369 * for legacy users.
adminb0dd10f2006-08-25 17:25:49 +0000370 *
371 * @access public
372 * @param array
373 * @return void
374 */
375 function script($scripts = array())
376 {
377 if ( ! is_array($scripts))
378 {
379 $scripts = array($scripts);
380 }
381
382 foreach ($scripts as $script)
383 {
384 if (isset($this->scripts[$script]))
385 {
386 continue;
387 }
388
389 $script = strtolower(str_replace(EXT, '', $script));
390
391 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
392 {
393 show_error('Unable to load the requested script: scripts/'.$script.EXT);
394 }
395
396 include_once(APPPATH.'scripts/'.$script.EXT);
397
398 $this->scripts[$script] = TRUE;
399 }
400
401 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
402 }
403 // END script()
404
405 // --------------------------------------------------------------------
406
407 /**
408 * Load Plugins
409 *
410 * This is simply an alias to the above function in case the
411 * user has written the plural form of this function.
412 *
413 * @access public
414 * @param array
415 * @return void
416 */
417 function plugins($plugins = array())
418 {
419 $this->plugin($plugins);
420 }
421 // END plugins()
422
423 // --------------------------------------------------------------------
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 {
434 $obj =& get_instance();
435 return $obj->lang->load($file, $lang, $return);
436 }
437 // END language()
438
439 // --------------------------------------------------------------------
440
441 /**
442 * Loads a config file
443 *
444 * @access public
445 * @param string
446 * @return void
447 */
448 function config($file = '')
449 {
450 $obj =& get_instance();
451 $obj->config->load($file);
452 }
453 // END config()
454
455 // --------------------------------------------------------------------
456
457 /**
458 * Set the Path to the "views" folder
459 *
460 * @access private
461 * @param string
462 * @return void
463 */
464 function _ci_set_view_path($path)
465 {
466 $this->view_path = $path;
467 }
468 // END _ci_set_view_path()
469
470 // --------------------------------------------------------------------
471
472 /**
473 * Loader
474 *
475 * This function isn't called directly. It's called from
476 * the two functions above. It's used to load views and files
477 *
478 * @access private
479 * @param array
480 * @return void
481 */
482 function _ci_load($data)
admin212a3fa2006-09-23 04:22:39 +0000483 {
adminb0dd10f2006-08-25 17:25:49 +0000484 // This allows anything loaded using $this->load (viwes, files, etc.)
485 // to become accessible from within the Controller and Model functions.
486 $obj =& get_instance();
487 foreach ($obj->ci_is_loaded as $val)
488 {
489 if ( ! isset($this->$val))
490 {
491 $this->$val =& $obj->$val;
492 }
493 }
494
495 // Set the default data variables
496 foreach (array('view', 'vars', 'path', 'return') as $val)
497 {
498 $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
499 }
500
501 /*
admin83b05a82006-09-25 21:06:46 +0000502 * Extract and cache variables
adminb0dd10f2006-08-25 17:25:49 +0000503 *
admin212a3fa2006-09-23 04:22:39 +0000504 * You can either set variables using the dedicated $this->load_vars()
505 * function or via the second parameter of this function. We'll merge
506 * the two types and cache them so that views that are embedded within
507 * other views can have access to these variables.
adminb0dd10f2006-08-25 17:25:49 +0000508 */
adminb0dd10f2006-08-25 17:25:49 +0000509 if (is_array($vars))
510 {
511 $this->cached_vars = array_merge($this->cached_vars, $vars);
512 }
513 extract($this->cached_vars);
514
515 // Set the path to the requested file
516 if ($path == '')
517 {
518 $ext = pathinfo($view, PATHINFO_EXTENSION);
admin6ac4bea2006-09-02 17:46:15 +0000519 $file = ($ext == '') ? $view.EXT : $view;
adminb0dd10f2006-08-25 17:25:49 +0000520 $path = $this->view_path.$file;
521 }
522 else
523 {
524 $x = explode('/', $path);
525 $file = end($x);
526 }
527
528 /*
529 * Buffer the output
530 *
531 * We buffer the output for two reasons:
532 * 1. Speed. You get a significant speed boost.
533 * 2. So that the final rendered template can be
534 * post-processed by the output class. Why do we
535 * need post processing? For one thing, in order to
536 * show the elapsed page load time. Unless we
537 * can intercept the content right before it's sent to
admin212a3fa2006-09-23 04:22:39 +0000538 * the browser and then stop the timer it won't be acurate.
adminb0dd10f2006-08-25 17:25:49 +0000539 */
adminb0dd10f2006-08-25 17:25:49 +0000540 if ( ! file_exists($path))
541 {
542 show_error('Unable to load the requested file: '.$file);
543 }
544
545 ob_start();
546
547 include($path);
548 log_message('debug', 'File loaded: '.$path);
549
550 // Return the file data if requested to
551 if ($return === TRUE)
552 {
553 $buffer = ob_get_contents();
554 ob_end_clean();
555
556 return $buffer;
557 }
558
559 /*
560 * Flush the buffer... or buff the flusher?
561 *
admin212a3fa2006-09-23 04:22:39 +0000562 * In order to permit views to be nested within
adminb0dd10f2006-08-25 17:25:49 +0000563 * other views, we need to flush the content back out whenever
564 * we are beyond the first level of output buffering so that
565 * it can be seen and included properly by the first included
566 * template and any subsequent ones. Oy!
567 *
568 */
569 if (ob_get_level() > $this->ob_level + 1)
570 {
571 ob_end_flush();
572 }
573 else
574 {
admin212a3fa2006-09-23 04:22:39 +0000575 $obj->output->set_output(ob_get_contents());
adminb0dd10f2006-08-25 17:25:49 +0000576 ob_end_clean();
577 }
578 }
579 // END _load()
580
581 // --------------------------------------------------------------------
582
583 /**
584 * Autoloader
585 *
586 * The config/autoload.php file contains an array that permits sub-systems,
587 * plugins, and helpers to be loaded automatically.
588 *
589 * @access private
590 * @param array
591 * @return void
592 */
593 function _ci_autoloader($autoload)
594 {
595 if ($autoload === FALSE)
596 {
597 return;
598 }
599
600 foreach (array('helper', 'plugin', 'script') as $type)
601 {
602 if (isset($autoload[$type]))
603 {
admin2e5872a2006-09-06 02:32:55 +0000604 $this->$type($autoload[$type]);
adminb0dd10f2006-08-25 17:25:49 +0000605 }
606 }
607 }
608 // END _ci_autoloader()
609
610 // --------------------------------------------------------------------
611
612 /**
613 * Object to Array
614 *
615 * Takes an object as input and convers the class variables to array key/vals
616 *
617 * @access public
618 * @param object
619 * @return array
620 */
621 function _ci_object_to_array($object)
622 {
admin2e5872a2006-09-06 02:32:55 +0000623 return (is_object($object)) ? get_object_vars($object) : $object;
adminb0dd10f2006-08-25 17:25:49 +0000624 }
admin2e5872a2006-09-06 02:32:55 +0000625 // END _ci_object_to_array()
adminb0dd10f2006-08-25 17:25:49 +0000626
627}
adminb0dd10f2006-08-25 17:25:49 +0000628?>