blob: 7449fa34a046ee43514f27ed26b187ce8a66cf50 [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 */
admine79dc712006-09-26 03:52:45 +0000139 function dbutil()
admina5e812c2006-09-25 02:17:30 +0000140 {
141 $obj =& get_instance();
admin9cd4e8e2006-09-25 23:26:25 +0000142 return $obj->_ci_init_dbutil($db, $return);
admina5e812c2006-09-25 02:17:30 +0000143 }
144 // END dbutils()
adminb0dd10f2006-08-25 17:25:49 +0000145
146 // --------------------------------------------------------------------
147
148 /**
149 * Scaffolding Loader
150 *
151 * @access public
152 * @param string
153 * @return void
154 */
155 function scaffolding($table = '')
156 {
157 if ($table == FALSE)
158 {
159 show_error('You must include the name of the table you would like access when you initialize scaffolding');
160 }
161
162 $obj =& get_instance();
163 $obj->_ci_init_scaffolding($table);
164 }
165 // END scaffolding()
166
167 // --------------------------------------------------------------------
168
169 /**
170 * Load View
171 *
172 * This function is used to load a "view" file. It has three parameters:
173 *
174 * 1. The name of the "view" file to be included.
175 * 2. An associative array of data to be extracted for use in the view.
176 * 3. TRUE/FALSE - whether to return the data or load it. In
177 * some cases it's advantageous to be able to retun data so that
178 * a developer can process it in some way.
179 *
180 * @access public
181 * @param string
182 * @param array
183 * @param bool
184 * @return void
185 */
186 function view($view, $vars = array(), $return = FALSE)
187 {
188 return $this->_ci_load(array('view' => $view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return));
189 }
190 // END view()
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Load File
196 *
197 * This is a generic file loader
198 *
199 * @access public
200 * @param string
201 * @param bool
202 * @return string
203 */
204 function file($path, $return = FALSE)
205 {
206 return $this->_ci_load(array('path' => $path, 'return' => $return));
207 }
208 // END file()
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Set Variables
214 *
215 * Once variables are set they become availabe within
216 * the controller class and its "view" files.
217 *
218 * @access public
219 * @param array
220 * @return void
221 */
222 function vars($vars = array())
223 {
224 $vars = $this->_ci_object_to_array($vars);
225
226 if (is_array($vars) AND count($vars) > 0)
227 {
228 foreach ($vars as $key => $val)
229 {
230 $this->cached_vars[$key] = $val;
231 }
232 }
233 }
234 // END vars()
235
236 // --------------------------------------------------------------------
237
238 /**
239 * Load Helper
240 *
241 * This function loads the specified helper file.
242 *
243 * @access public
244 * @param mixed
245 * @return void
246 */
247 function helper($helpers = array())
248 {
249 if ( ! is_array($helpers))
250 {
251 $helpers = array($helpers);
252 }
253
254 foreach ($helpers as $helper)
255 {
256 if (isset($this->helpers[$helper]))
257 {
258 continue;
259 }
260
261 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
262
admin480da812006-09-20 21:11:04 +0000263 if (file_exists(APPPATH.'helpers/'.$helper.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000264 {
admin480da812006-09-20 21:11:04 +0000265 include_once(APPPATH.'helpers/'.$helper.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000266 }
admin480da812006-09-20 21:11:04 +0000267 else
268 {
269 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
270 {
271 include_once(BASEPATH.'helpers/'.$helper.EXT);
272 }
273 else
274 {
275 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
276 }
277 }
adminb0dd10f2006-08-25 17:25:49 +0000278
279 $this->helpers[$helper] = TRUE;
280 }
281
282 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
283 }
284 // END helper()
285
286 // --------------------------------------------------------------------
287
288 /**
289 * Load Helpers
290 *
291 * This is simply an alias to the above function in case the
292 * user has written the plural form of this function.
293 *
294 * @access public
295 * @param array
296 * @return void
297 */
298 function helpers($helpers = array())
299 {
300 $this->helper($helpers);
301 }
302 // END helpers()
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Load Plugin
308 *
309 * This function loads the specified plugin.
310 *
311 * @access public
312 * @param array
313 * @return void
314 */
315 function plugin($plugins = array())
316 {
317 if ( ! is_array($plugins))
318 {
319 $plugins = array($plugins);
320 }
321
322 foreach ($plugins as $plugin)
323 {
324 if (isset($this->plugins[$plugin]))
325 {
326 continue;
327 }
328
admin480da812006-09-20 21:11:04 +0000329 $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi');
adminb0dd10f2006-08-25 17:25:49 +0000330
admin480da812006-09-20 21:11:04 +0000331 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000332 {
admin480da812006-09-20 21:11:04 +0000333 include_once(APPPATH.'plugins/'.$plugin.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000334 }
admin480da812006-09-20 21:11:04 +0000335 else
336 {
337 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
338 {
339 include_once(BASEPATH.'plugins/'.$plugin.EXT);
340 }
341 else
342 {
343 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
344 }
345 }
adminb0dd10f2006-08-25 17:25:49 +0000346
347 $this->plugins[$plugin] = TRUE;
348 }
349
350 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
351 }
352 // END plugin()
353
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 }
397 // END script()
398
399 // --------------------------------------------------------------------
400
401 /**
402 * Load Plugins
403 *
404 * This is simply an alias to the above function in case the
405 * user has written the plural form of this function.
406 *
407 * @access public
408 * @param array
409 * @return void
410 */
411 function plugins($plugins = array())
412 {
413 $this->plugin($plugins);
414 }
415 // END plugins()
416
417 // --------------------------------------------------------------------
418
419 /**
420 * Loads a language file
421 *
422 * @access public
423 * @param string
424 * @return void
425 */
426 function language($file = '', $lang = '', $return = FALSE)
427 {
428 $obj =& get_instance();
429 return $obj->lang->load($file, $lang, $return);
430 }
431 // END language()
432
433 // --------------------------------------------------------------------
434
435 /**
436 * Loads a config file
437 *
438 * @access public
439 * @param string
440 * @return void
441 */
442 function config($file = '')
443 {
444 $obj =& get_instance();
445 $obj->config->load($file);
446 }
447 // END config()
448
449 // --------------------------------------------------------------------
450
451 /**
452 * Set the Path to the "views" folder
453 *
454 * @access private
455 * @param string
456 * @return void
457 */
458 function _ci_set_view_path($path)
459 {
460 $this->view_path = $path;
461 }
462 // END _ci_set_view_path()
463
464 // --------------------------------------------------------------------
465
466 /**
467 * Loader
468 *
469 * This function isn't called directly. It's called from
470 * the two functions above. It's used to load views and files
471 *
472 * @access private
473 * @param array
474 * @return void
475 */
476 function _ci_load($data)
admin212a3fa2006-09-23 04:22:39 +0000477 {
adminb0dd10f2006-08-25 17:25:49 +0000478 // This allows anything loaded using $this->load (viwes, files, etc.)
479 // to become accessible from within the Controller and Model functions.
480 $obj =& get_instance();
admine79dc712006-09-26 03:52:45 +0000481 foreach (get_object_vars($obj) as $key => $var)
adminb0dd10f2006-08-25 17:25:49 +0000482 {
admine79dc712006-09-26 03:52:45 +0000483 if (is_object($var))
adminb0dd10f2006-08-25 17:25:49 +0000484 {
admine79dc712006-09-26 03:52:45 +0000485 $this->$key =& $obj->$key;
486 }
487 }
488
adminb0dd10f2006-08-25 17:25:49 +0000489 // Set the default data variables
490 foreach (array('view', 'vars', 'path', 'return') as $val)
491 {
492 $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
493 }
494
495 /*
admin83b05a82006-09-25 21:06:46 +0000496 * Extract and cache variables
adminb0dd10f2006-08-25 17:25:49 +0000497 *
admin212a3fa2006-09-23 04:22:39 +0000498 * You can either set variables using the dedicated $this->load_vars()
499 * function or via the second parameter of this function. We'll merge
500 * the two types and cache them so that views that are embedded within
501 * other views can have access to these variables.
adminb0dd10f2006-08-25 17:25:49 +0000502 */
adminb0dd10f2006-08-25 17:25:49 +0000503 if (is_array($vars))
504 {
505 $this->cached_vars = array_merge($this->cached_vars, $vars);
506 }
507 extract($this->cached_vars);
508
509 // Set the path to the requested file
510 if ($path == '')
511 {
512 $ext = pathinfo($view, PATHINFO_EXTENSION);
admin6ac4bea2006-09-02 17:46:15 +0000513 $file = ($ext == '') ? $view.EXT : $view;
adminb0dd10f2006-08-25 17:25:49 +0000514 $path = $this->view_path.$file;
515 }
516 else
517 {
518 $x = explode('/', $path);
519 $file = end($x);
520 }
521
522 /*
523 * Buffer the output
524 *
525 * We buffer the output for two reasons:
526 * 1. Speed. You get a significant speed boost.
527 * 2. So that the final rendered template can be
528 * post-processed by the output class. Why do we
529 * need post processing? For one thing, in order to
530 * show the elapsed page load time. Unless we
531 * can intercept the content right before it's sent to
admin212a3fa2006-09-23 04:22:39 +0000532 * the browser and then stop the timer it won't be acurate.
adminb0dd10f2006-08-25 17:25:49 +0000533 */
adminb0dd10f2006-08-25 17:25:49 +0000534 if ( ! file_exists($path))
535 {
536 show_error('Unable to load the requested file: '.$file);
537 }
538
539 ob_start();
540
541 include($path);
542 log_message('debug', 'File loaded: '.$path);
543
544 // Return the file data if requested to
545 if ($return === TRUE)
546 {
547 $buffer = ob_get_contents();
548 ob_end_clean();
549
550 return $buffer;
551 }
552
553 /*
554 * Flush the buffer... or buff the flusher?
555 *
admin212a3fa2006-09-23 04:22:39 +0000556 * In order to permit views to be nested within
adminb0dd10f2006-08-25 17:25:49 +0000557 * other views, we need to flush the content back out whenever
558 * we are beyond the first level of output buffering so that
559 * it can be seen and included properly by the first included
560 * template and any subsequent ones. Oy!
561 *
562 */
563 if (ob_get_level() > $this->ob_level + 1)
564 {
565 ob_end_flush();
566 }
567 else
568 {
admin212a3fa2006-09-23 04:22:39 +0000569 $obj->output->set_output(ob_get_contents());
adminb0dd10f2006-08-25 17:25:49 +0000570 ob_end_clean();
571 }
572 }
573 // END _load()
574
575 // --------------------------------------------------------------------
576
577 /**
578 * Autoloader
579 *
580 * The config/autoload.php file contains an array that permits sub-systems,
581 * plugins, and helpers to be loaded automatically.
582 *
583 * @access private
584 * @param array
585 * @return void
586 */
587 function _ci_autoloader($autoload)
588 {
589 if ($autoload === FALSE)
590 {
591 return;
592 }
593
594 foreach (array('helper', 'plugin', 'script') as $type)
595 {
596 if (isset($autoload[$type]))
597 {
admin2e5872a2006-09-06 02:32:55 +0000598 $this->$type($autoload[$type]);
adminb0dd10f2006-08-25 17:25:49 +0000599 }
600 }
601 }
602 // END _ci_autoloader()
603
604 // --------------------------------------------------------------------
605
606 /**
607 * Object to Array
608 *
609 * Takes an object as input and convers the class variables to array key/vals
610 *
611 * @access public
612 * @param object
613 * @return array
614 */
615 function _ci_object_to_array($object)
616 {
admin2e5872a2006-09-06 02:32:55 +0000617 return (is_object($object)) ? get_object_vars($object) : $object;
adminb0dd10f2006-08-25 17:25:49 +0000618 }
admin2e5872a2006-09-06 02:32:55 +0000619 // END _ci_object_to_array()
adminb0dd10f2006-08-25 17:25:49 +0000620
621}
adminb0dd10f2006-08-25 17:25:49 +0000622?>