blob: f4a9f821d082bc5cdd9ff6b411f91657e7b37f72 [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 */
139 function dbutils($db = '', $return = FALSE)
140 {
141 $obj =& get_instance();
142
143 if ( ! is_bool($return))
144 {
145 $return = FALSE;
146 }
147
148 return $obj->_ci_init_dbutils($db, $return);
149 }
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
366 * application/scripts/ folder
367 *
368 * @access public
369 * @param array
370 * @return void
371 */
372 function script($scripts = array())
373 {
374 if ( ! is_array($scripts))
375 {
376 $scripts = array($scripts);
377 }
378
379 foreach ($scripts as $script)
380 {
381 if (isset($this->scripts[$script]))
382 {
383 continue;
384 }
385
386 $script = strtolower(str_replace(EXT, '', $script));
387
388 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
389 {
390 show_error('Unable to load the requested script: scripts/'.$script.EXT);
391 }
392
393 include_once(APPPATH.'scripts/'.$script.EXT);
394
395 $this->scripts[$script] = TRUE;
396 }
397
398 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
399 }
400 // END script()
401
402 // --------------------------------------------------------------------
403
404 /**
405 * Load Plugins
406 *
407 * This is simply an alias to the above function in case the
408 * user has written the plural form of this function.
409 *
410 * @access public
411 * @param array
412 * @return void
413 */
414 function plugins($plugins = array())
415 {
416 $this->plugin($plugins);
417 }
418 // END plugins()
419
420 // --------------------------------------------------------------------
421
422 /**
423 * Loads a language file
424 *
425 * @access public
426 * @param string
427 * @return void
428 */
429 function language($file = '', $lang = '', $return = FALSE)
430 {
431 $obj =& get_instance();
432 return $obj->lang->load($file, $lang, $return);
433 }
434 // END language()
435
436 // --------------------------------------------------------------------
437
438 /**
439 * Loads a config file
440 *
441 * @access public
442 * @param string
443 * @return void
444 */
445 function config($file = '')
446 {
447 $obj =& get_instance();
448 $obj->config->load($file);
449 }
450 // END config()
451
452 // --------------------------------------------------------------------
453
454 /**
455 * Set the Path to the "views" folder
456 *
457 * @access private
458 * @param string
459 * @return void
460 */
461 function _ci_set_view_path($path)
462 {
463 $this->view_path = $path;
464 }
465 // END _ci_set_view_path()
466
467 // --------------------------------------------------------------------
468
469 /**
470 * Loader
471 *
472 * This function isn't called directly. It's called from
473 * the two functions above. It's used to load views and files
474 *
475 * @access private
476 * @param array
477 * @return void
478 */
479 function _ci_load($data)
admin212a3fa2006-09-23 04:22:39 +0000480 {
adminb0dd10f2006-08-25 17:25:49 +0000481 // This allows anything loaded using $this->load (viwes, files, etc.)
482 // to become accessible from within the Controller and Model functions.
483 $obj =& get_instance();
484 foreach ($obj->ci_is_loaded as $val)
485 {
486 if ( ! isset($this->$val))
487 {
488 $this->$val =& $obj->$val;
489 }
490 }
491
492 // Set the default data variables
493 foreach (array('view', 'vars', 'path', 'return') as $val)
494 {
495 $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
496 }
497
498 /*
499 * Extract and cached variables
500 *
admin212a3fa2006-09-23 04:22:39 +0000501 * You can either set variables using the dedicated $this->load_vars()
502 * function or via the second parameter of this function. We'll merge
503 * the two types and cache them so that views that are embedded within
504 * other views can have access to these variables.
adminb0dd10f2006-08-25 17:25:49 +0000505 */
adminb0dd10f2006-08-25 17:25:49 +0000506 if (is_array($vars))
507 {
508 $this->cached_vars = array_merge($this->cached_vars, $vars);
509 }
510 extract($this->cached_vars);
511
512 // Set the path to the requested file
513 if ($path == '')
514 {
515 $ext = pathinfo($view, PATHINFO_EXTENSION);
admin6ac4bea2006-09-02 17:46:15 +0000516 $file = ($ext == '') ? $view.EXT : $view;
adminb0dd10f2006-08-25 17:25:49 +0000517 $path = $this->view_path.$file;
518 }
519 else
520 {
521 $x = explode('/', $path);
522 $file = end($x);
523 }
524
525 /*
526 * Buffer the output
527 *
528 * We buffer the output for two reasons:
529 * 1. Speed. You get a significant speed boost.
530 * 2. So that the final rendered template can be
531 * post-processed by the output class. Why do we
532 * need post processing? For one thing, in order to
533 * show the elapsed page load time. Unless we
534 * can intercept the content right before it's sent to
admin212a3fa2006-09-23 04:22:39 +0000535 * the browser and then stop the timer it won't be acurate.
adminb0dd10f2006-08-25 17:25:49 +0000536 */
adminb0dd10f2006-08-25 17:25:49 +0000537 if ( ! file_exists($path))
538 {
539 show_error('Unable to load the requested file: '.$file);
540 }
541
542 ob_start();
543
544 include($path);
545 log_message('debug', 'File loaded: '.$path);
546
547 // Return the file data if requested to
548 if ($return === TRUE)
549 {
550 $buffer = ob_get_contents();
551 ob_end_clean();
552
553 return $buffer;
554 }
555
556 /*
557 * Flush the buffer... or buff the flusher?
558 *
admin212a3fa2006-09-23 04:22:39 +0000559 * In order to permit views to be nested within
adminb0dd10f2006-08-25 17:25:49 +0000560 * other views, we need to flush the content back out whenever
561 * we are beyond the first level of output buffering so that
562 * it can be seen and included properly by the first included
563 * template and any subsequent ones. Oy!
564 *
565 */
566 if (ob_get_level() > $this->ob_level + 1)
567 {
568 ob_end_flush();
569 }
570 else
571 {
admin212a3fa2006-09-23 04:22:39 +0000572 $obj->output->set_output(ob_get_contents());
adminb0dd10f2006-08-25 17:25:49 +0000573 ob_end_clean();
574 }
575 }
576 // END _load()
577
578 // --------------------------------------------------------------------
579
580 /**
581 * Autoloader
582 *
583 * The config/autoload.php file contains an array that permits sub-systems,
584 * plugins, and helpers to be loaded automatically.
585 *
586 * @access private
587 * @param array
588 * @return void
589 */
590 function _ci_autoloader($autoload)
591 {
592 if ($autoload === FALSE)
593 {
594 return;
595 }
596
597 foreach (array('helper', 'plugin', 'script') as $type)
598 {
599 if (isset($autoload[$type]))
600 {
admin2e5872a2006-09-06 02:32:55 +0000601 $this->$type($autoload[$type]);
adminb0dd10f2006-08-25 17:25:49 +0000602 }
603 }
604 }
605 // END _ci_autoloader()
606
607 // --------------------------------------------------------------------
608
609 /**
610 * Object to Array
611 *
612 * Takes an object as input and convers the class variables to array key/vals
613 *
614 * @access public
615 * @param object
616 * @return array
617 */
618 function _ci_object_to_array($object)
619 {
admin2e5872a2006-09-06 02:32:55 +0000620 return (is_object($object)) ? get_object_vars($object) : $object;
adminb0dd10f2006-08-25 17:25:49 +0000621 }
admin2e5872a2006-09-06 02:32:55 +0000622 // END _ci_object_to_array()
adminb0dd10f2006-08-25 17:25:49 +0000623
624}
adminb0dd10f2006-08-25 17:25:49 +0000625?>