blob: 2534e69651fb039623dbb01b23792a4916103f01 [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();
admin33de9a12006-09-28 06:50:16 +000074 $obj->_ci_load_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 }
admin6e00bab2006-09-26 08:13:06 +0000138
139 // --------------------------------------------------------------------
140
141 /**
adminb0dd10f2006-08-25 17:25:49 +0000142 * Scaffolding Loader
143 *
144 * @access public
145 * @param string
146 * @return void
147 */
148 function scaffolding($table = '')
149 {
150 if ($table == FALSE)
151 {
152 show_error('You must include the name of the table you would like access when you initialize scaffolding');
153 }
154
155 $obj =& get_instance();
156 $obj->_ci_init_scaffolding($table);
157 }
adminb0dd10f2006-08-25 17:25:49 +0000158
159 // --------------------------------------------------------------------
160
161 /**
162 * Load View
163 *
164 * This function is used to load a "view" file. It has three parameters:
165 *
166 * 1. The name of the "view" file to be included.
167 * 2. An associative array of data to be extracted for use in the view.
168 * 3. TRUE/FALSE - whether to return the data or load it. In
169 * some cases it's advantageous to be able to retun data so that
170 * a developer can process it in some way.
171 *
172 * @access public
173 * @param string
174 * @param array
175 * @param bool
176 * @return void
177 */
178 function view($view, $vars = array(), $return = FALSE)
179 {
180 return $this->_ci_load(array('view' => $view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return));
181 }
adminb0dd10f2006-08-25 17:25:49 +0000182
183 // --------------------------------------------------------------------
184
185 /**
186 * Load File
187 *
188 * This is a generic file loader
189 *
190 * @access public
191 * @param string
192 * @param bool
193 * @return string
194 */
195 function file($path, $return = FALSE)
196 {
197 return $this->_ci_load(array('path' => $path, 'return' => $return));
198 }
adminb0dd10f2006-08-25 17:25:49 +0000199
200 // --------------------------------------------------------------------
201
202 /**
203 * Set Variables
204 *
205 * Once variables are set they become availabe within
206 * the controller class and its "view" files.
207 *
208 * @access public
209 * @param array
210 * @return void
211 */
212 function vars($vars = array())
213 {
214 $vars = $this->_ci_object_to_array($vars);
215
216 if (is_array($vars) AND count($vars) > 0)
217 {
218 foreach ($vars as $key => $val)
219 {
220 $this->cached_vars[$key] = $val;
221 }
222 }
223 }
adminb0dd10f2006-08-25 17:25:49 +0000224
225 // --------------------------------------------------------------------
226
227 /**
228 * Load Helper
229 *
230 * This function loads the specified helper file.
231 *
232 * @access public
233 * @param mixed
234 * @return void
235 */
236 function helper($helpers = array())
237 {
238 if ( ! is_array($helpers))
239 {
240 $helpers = array($helpers);
241 }
242
243 foreach ($helpers as $helper)
244 {
245 if (isset($this->helpers[$helper]))
246 {
247 continue;
248 }
249
250 $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
251
admin480da812006-09-20 21:11:04 +0000252 if (file_exists(APPPATH.'helpers/'.$helper.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000253 {
admin480da812006-09-20 21:11:04 +0000254 include_once(APPPATH.'helpers/'.$helper.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000255 }
admin480da812006-09-20 21:11:04 +0000256 else
257 {
258 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
259 {
260 include_once(BASEPATH.'helpers/'.$helper.EXT);
261 }
262 else
263 {
264 show_error('Unable to load the requested file: helpers/'.$helper.EXT);
265 }
266 }
adminb0dd10f2006-08-25 17:25:49 +0000267
268 $this->helpers[$helper] = TRUE;
269 }
270
271 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
272 }
adminb0dd10f2006-08-25 17:25:49 +0000273
274 // --------------------------------------------------------------------
275
276 /**
277 * Load Helpers
278 *
279 * This is simply an alias to the above function in case the
280 * user has written the plural form of this function.
281 *
282 * @access public
283 * @param array
284 * @return void
285 */
286 function helpers($helpers = array())
287 {
288 $this->helper($helpers);
289 }
adminb0dd10f2006-08-25 17:25:49 +0000290
291 // --------------------------------------------------------------------
292
293 /**
294 * Load Plugin
295 *
296 * This function loads the specified plugin.
297 *
298 * @access public
299 * @param array
300 * @return void
301 */
302 function plugin($plugins = array())
303 {
304 if ( ! is_array($plugins))
305 {
306 $plugins = array($plugins);
307 }
308
309 foreach ($plugins as $plugin)
310 {
311 if (isset($this->plugins[$plugin]))
312 {
313 continue;
314 }
315
admin480da812006-09-20 21:11:04 +0000316 $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi');
adminb0dd10f2006-08-25 17:25:49 +0000317
admin480da812006-09-20 21:11:04 +0000318 if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
adminb0dd10f2006-08-25 17:25:49 +0000319 {
admin480da812006-09-20 21:11:04 +0000320 include_once(APPPATH.'plugins/'.$plugin.EXT);
adminb0dd10f2006-08-25 17:25:49 +0000321 }
admin480da812006-09-20 21:11:04 +0000322 else
323 {
324 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
325 {
326 include_once(BASEPATH.'plugins/'.$plugin.EXT);
327 }
328 else
329 {
330 show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
331 }
332 }
adminb0dd10f2006-08-25 17:25:49 +0000333
334 $this->plugins[$plugin] = TRUE;
335 }
336
337 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
338 }
adminb0dd10f2006-08-25 17:25:49 +0000339
340 // --------------------------------------------------------------------
341
342 /**
343 * Load Script
344 *
345 * This function loads the specified include file from the
admin83b05a82006-09-25 21:06:46 +0000346 * application/scripts/ folder.
347 *
348 * NOTE: This feature has been deprecated but it will remain available
349 * for legacy users.
adminb0dd10f2006-08-25 17:25:49 +0000350 *
351 * @access public
352 * @param array
353 * @return void
354 */
355 function script($scripts = array())
356 {
357 if ( ! is_array($scripts))
358 {
359 $scripts = array($scripts);
360 }
361
362 foreach ($scripts as $script)
363 {
364 if (isset($this->scripts[$script]))
365 {
366 continue;
367 }
368
369 $script = strtolower(str_replace(EXT, '', $script));
370
371 if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
372 {
373 show_error('Unable to load the requested script: scripts/'.$script.EXT);
374 }
375
376 include_once(APPPATH.'scripts/'.$script.EXT);
377
378 $this->scripts[$script] = TRUE;
379 }
380
381 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
382 }
adminb0dd10f2006-08-25 17:25:49 +0000383
384 // --------------------------------------------------------------------
385
386 /**
387 * Load Plugins
388 *
389 * This is simply an alias to the above function in case the
390 * user has written the plural form of this function.
391 *
392 * @access public
393 * @param array
394 * @return void
395 */
396 function plugins($plugins = array())
397 {
398 $this->plugin($plugins);
399 }
adminb0dd10f2006-08-25 17:25:49 +0000400
401 // --------------------------------------------------------------------
402
403 /**
404 * Loads a language file
405 *
406 * @access public
407 * @param string
408 * @return void
409 */
410 function language($file = '', $lang = '', $return = FALSE)
411 {
412 $obj =& get_instance();
413 return $obj->lang->load($file, $lang, $return);
414 }
adminb0dd10f2006-08-25 17:25:49 +0000415
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 }
adminb0dd10f2006-08-25 17:25:49 +0000430
431 // --------------------------------------------------------------------
432
433 /**
434 * Set the Path to the "views" folder
435 *
436 * @access private
437 * @param string
438 * @return void
439 */
440 function _ci_set_view_path($path)
441 {
442 $this->view_path = $path;
443 }
adminb0dd10f2006-08-25 17:25:49 +0000444
445 // --------------------------------------------------------------------
446
447 /**
448 * Loader
449 *
450 * This function isn't called directly. It's called from
451 * the two functions above. It's used to load views and files
452 *
453 * @access private
454 * @param array
455 * @return void
456 */
457 function _ci_load($data)
admin212a3fa2006-09-23 04:22:39 +0000458 {
adminb0dd10f2006-08-25 17:25:49 +0000459 // This allows anything loaded using $this->load (viwes, files, etc.)
460 // to become accessible from within the Controller and Model functions.
461 $obj =& get_instance();
admine79dc712006-09-26 03:52:45 +0000462 foreach (get_object_vars($obj) as $key => $var)
adminb0dd10f2006-08-25 17:25:49 +0000463 {
admine79dc712006-09-26 03:52:45 +0000464 if (is_object($var))
adminb0dd10f2006-08-25 17:25:49 +0000465 {
admine79dc712006-09-26 03:52:45 +0000466 $this->$key =& $obj->$key;
467 }
468 }
469
adminb0dd10f2006-08-25 17:25:49 +0000470 // Set the default data variables
471 foreach (array('view', 'vars', 'path', 'return') as $val)
472 {
473 $$val = ( ! isset($data[$val])) ? FALSE : $data[$val];
474 }
475
476 /*
admin83b05a82006-09-25 21:06:46 +0000477 * Extract and cache variables
adminb0dd10f2006-08-25 17:25:49 +0000478 *
admin212a3fa2006-09-23 04:22:39 +0000479 * You can either set variables using the dedicated $this->load_vars()
480 * function or via the second parameter of this function. We'll merge
481 * the two types and cache them so that views that are embedded within
482 * other views can have access to these variables.
adminb0dd10f2006-08-25 17:25:49 +0000483 */
adminb0dd10f2006-08-25 17:25:49 +0000484 if (is_array($vars))
485 {
486 $this->cached_vars = array_merge($this->cached_vars, $vars);
487 }
488 extract($this->cached_vars);
489
490 // Set the path to the requested file
491 if ($path == '')
492 {
493 $ext = pathinfo($view, PATHINFO_EXTENSION);
admin6ac4bea2006-09-02 17:46:15 +0000494 $file = ($ext == '') ? $view.EXT : $view;
adminb0dd10f2006-08-25 17:25:49 +0000495 $path = $this->view_path.$file;
496 }
497 else
498 {
499 $x = explode('/', $path);
500 $file = end($x);
501 }
502
503 /*
504 * Buffer the output
505 *
506 * We buffer the output for two reasons:
507 * 1. Speed. You get a significant speed boost.
508 * 2. So that the final rendered template can be
509 * post-processed by the output class. Why do we
510 * need post processing? For one thing, in order to
511 * show the elapsed page load time. Unless we
512 * can intercept the content right before it's sent to
admin212a3fa2006-09-23 04:22:39 +0000513 * the browser and then stop the timer it won't be acurate.
adminb0dd10f2006-08-25 17:25:49 +0000514 */
adminb0dd10f2006-08-25 17:25:49 +0000515 if ( ! file_exists($path))
516 {
517 show_error('Unable to load the requested file: '.$file);
518 }
519
520 ob_start();
521
522 include($path);
523 log_message('debug', 'File loaded: '.$path);
524
525 // Return the file data if requested to
526 if ($return === TRUE)
527 {
528 $buffer = ob_get_contents();
529 ob_end_clean();
530
531 return $buffer;
532 }
533
534 /*
535 * Flush the buffer... or buff the flusher?
536 *
admin212a3fa2006-09-23 04:22:39 +0000537 * In order to permit views to be nested within
adminb0dd10f2006-08-25 17:25:49 +0000538 * other views, we need to flush the content back out whenever
539 * we are beyond the first level of output buffering so that
540 * it can be seen and included properly by the first included
541 * template and any subsequent ones. Oy!
542 *
543 */
544 if (ob_get_level() > $this->ob_level + 1)
545 {
546 ob_end_flush();
547 }
548 else
549 {
admin212a3fa2006-09-23 04:22:39 +0000550 $obj->output->set_output(ob_get_contents());
adminb0dd10f2006-08-25 17:25:49 +0000551 ob_end_clean();
552 }
553 }
adminb0dd10f2006-08-25 17:25:49 +0000554
555 // --------------------------------------------------------------------
556
557 /**
558 * Autoloader
559 *
560 * The config/autoload.php file contains an array that permits sub-systems,
561 * plugins, and helpers to be loaded automatically.
562 *
563 * @access private
564 * @param array
565 * @return void
566 */
567 function _ci_autoloader($autoload)
568 {
569 if ($autoload === FALSE)
570 {
571 return;
572 }
573
574 foreach (array('helper', 'plugin', 'script') as $type)
575 {
576 if (isset($autoload[$type]))
577 {
admin2e5872a2006-09-06 02:32:55 +0000578 $this->$type($autoload[$type]);
adminb0dd10f2006-08-25 17:25:49 +0000579 }
580 }
581 }
adminb0dd10f2006-08-25 17:25:49 +0000582
583 // --------------------------------------------------------------------
584
585 /**
586 * Object to Array
587 *
588 * Takes an object as input and convers the class variables to array key/vals
589 *
590 * @access public
591 * @param object
592 * @return array
593 */
594 function _ci_object_to_array($object)
595 {
admin2e5872a2006-09-06 02:32:55 +0000596 return (is_object($object)) ? get_object_vars($object) : $object;
adminb0dd10f2006-08-25 17:25:49 +0000597 }
598
599}
adminb0dd10f2006-08-25 17:25:49 +0000600?>