admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?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 | */ |
| 29 | class 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) |
| 111 | * @return mixed |
| 112 | */ |
| 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 | |
| 246 | if ( ! file_exists(BASEPATH.'helpers/'.$helper.EXT)) |
| 247 | { |
| 248 | show_error('Unable to load the requested file: helpers/'.$helper.EXT); |
| 249 | } |
| 250 | |
| 251 | include_once(BASEPATH.'helpers/'.$helper.EXT); |
| 252 | |
| 253 | $this->helpers[$helper] = TRUE; |
| 254 | } |
| 255 | |
| 256 | log_message('debug', 'Helpers loaded: '.implode(', ', $helpers)); |
| 257 | } |
| 258 | // END helper() |
| 259 | |
| 260 | // -------------------------------------------------------------------- |
| 261 | |
| 262 | /** |
| 263 | * Load Helpers |
| 264 | * |
| 265 | * This is simply an alias to the above function in case the |
| 266 | * user has written the plural form of this function. |
| 267 | * |
| 268 | * @access public |
| 269 | * @param array |
| 270 | * @return void |
| 271 | */ |
| 272 | function helpers($helpers = array()) |
| 273 | { |
| 274 | $this->helper($helpers); |
| 275 | } |
| 276 | // END helpers() |
| 277 | |
| 278 | // -------------------------------------------------------------------- |
| 279 | |
| 280 | /** |
| 281 | * Load Plugin |
| 282 | * |
| 283 | * This function loads the specified plugin. |
| 284 | * |
| 285 | * @access public |
| 286 | * @param array |
| 287 | * @return void |
| 288 | */ |
| 289 | function plugin($plugins = array()) |
| 290 | { |
| 291 | if ( ! is_array($plugins)) |
| 292 | { |
| 293 | $plugins = array($plugins); |
| 294 | } |
| 295 | |
| 296 | foreach ($plugins as $plugin) |
| 297 | { |
| 298 | if (isset($this->plugins[$plugin])) |
| 299 | { |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi'); |
| 304 | |
| 305 | if ( ! file_exists(BASEPATH.'plugins/'.$plugin.EXT)) |
| 306 | { |
| 307 | show_error('Unable to load the requested file: plugins/'.$plugin.EXT); |
| 308 | } |
| 309 | |
| 310 | include_once(BASEPATH.'plugins/'.$plugin.EXT); |
| 311 | |
| 312 | $this->plugins[$plugin] = TRUE; |
| 313 | } |
| 314 | |
| 315 | log_message('debug', 'Plugins loaded: '.implode(', ', $plugins)); |
| 316 | } |
| 317 | // END plugin() |
| 318 | |
| 319 | // -------------------------------------------------------------------- |
| 320 | |
| 321 | /** |
| 322 | * Load Script |
| 323 | * |
| 324 | * This function loads the specified include file from the |
| 325 | * application/scripts/ folder |
| 326 | * |
| 327 | * @access public |
| 328 | * @param array |
| 329 | * @return void |
| 330 | */ |
| 331 | function script($scripts = array()) |
| 332 | { |
| 333 | if ( ! is_array($scripts)) |
| 334 | { |
| 335 | $scripts = array($scripts); |
| 336 | } |
| 337 | |
| 338 | foreach ($scripts as $script) |
| 339 | { |
| 340 | if (isset($this->scripts[$script])) |
| 341 | { |
| 342 | continue; |
| 343 | } |
| 344 | |
| 345 | $script = strtolower(str_replace(EXT, '', $script)); |
| 346 | |
| 347 | if ( ! file_exists(APPPATH.'scripts/'.$script.EXT)) |
| 348 | { |
| 349 | show_error('Unable to load the requested script: scripts/'.$script.EXT); |
| 350 | } |
| 351 | |
| 352 | include_once(APPPATH.'scripts/'.$script.EXT); |
| 353 | |
| 354 | $this->scripts[$script] = TRUE; |
| 355 | } |
| 356 | |
| 357 | log_message('debug', 'Scripts loaded: '.implode(', ', $scripts)); |
| 358 | } |
| 359 | // END script() |
| 360 | |
| 361 | // -------------------------------------------------------------------- |
| 362 | |
| 363 | /** |
| 364 | * Load Plugins |
| 365 | * |
| 366 | * This is simply an alias to the above function in case the |
| 367 | * user has written the plural form of this function. |
| 368 | * |
| 369 | * @access public |
| 370 | * @param array |
| 371 | * @return void |
| 372 | */ |
| 373 | function plugins($plugins = array()) |
| 374 | { |
| 375 | $this->plugin($plugins); |
| 376 | } |
| 377 | // END plugins() |
| 378 | |
| 379 | // -------------------------------------------------------------------- |
| 380 | |
| 381 | /** |
| 382 | * Loads a language file |
| 383 | * |
| 384 | * @access public |
| 385 | * @param string |
| 386 | * @return void |
| 387 | */ |
| 388 | function language($file = '', $lang = '', $return = FALSE) |
| 389 | { |
| 390 | $obj =& get_instance(); |
| 391 | return $obj->lang->load($file, $lang, $return); |
| 392 | } |
| 393 | // END language() |
| 394 | |
| 395 | // -------------------------------------------------------------------- |
| 396 | |
| 397 | /** |
| 398 | * Loads a config file |
| 399 | * |
| 400 | * @access public |
| 401 | * @param string |
| 402 | * @return void |
| 403 | */ |
| 404 | function config($file = '') |
| 405 | { |
| 406 | $obj =& get_instance(); |
| 407 | $obj->config->load($file); |
| 408 | } |
| 409 | // END config() |
| 410 | |
| 411 | // -------------------------------------------------------------------- |
| 412 | |
| 413 | /** |
| 414 | * Set the Path to the "views" folder |
| 415 | * |
| 416 | * @access private |
| 417 | * @param string |
| 418 | * @return void |
| 419 | */ |
| 420 | function _ci_set_view_path($path) |
| 421 | { |
| 422 | $this->view_path = $path; |
| 423 | } |
| 424 | // END _ci_set_view_path() |
| 425 | |
| 426 | // -------------------------------------------------------------------- |
| 427 | |
| 428 | /** |
| 429 | * Loader |
| 430 | * |
| 431 | * This function isn't called directly. It's called from |
| 432 | * the two functions above. It's used to load views and files |
| 433 | * |
| 434 | * @access private |
| 435 | * @param array |
| 436 | * @return void |
| 437 | */ |
| 438 | function _ci_load($data) |
| 439 | { |
| 440 | $OUT =& _load_class('CI_Output'); |
| 441 | |
| 442 | // This allows anything loaded using $this->load (viwes, files, etc.) |
| 443 | // to become accessible from within the Controller and Model functions. |
| 444 | $obj =& get_instance(); |
| 445 | foreach ($obj->ci_is_loaded as $val) |
| 446 | { |
| 447 | if ( ! isset($this->$val)) |
| 448 | { |
| 449 | $this->$val =& $obj->$val; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | // Set the default data variables |
| 454 | foreach (array('view', 'vars', 'path', 'return') as $val) |
| 455 | { |
| 456 | $$val = ( ! isset($data[$val])) ? FALSE : $data[$val]; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Extract and cached variables |
| 461 | * |
| 462 | * You can either set variables using the dedicated |
| 463 | * $this->load_vars() function or via the second |
| 464 | * parameter of this function. We'll |
| 465 | * merge the two types and cache them so that |
| 466 | * views that are embedded within other views |
| 467 | * can have access to these variables. |
| 468 | * |
| 469 | */ |
| 470 | |
| 471 | if (is_array($vars)) |
| 472 | { |
| 473 | $this->cached_vars = array_merge($this->cached_vars, $vars); |
| 474 | } |
| 475 | extract($this->cached_vars); |
| 476 | |
| 477 | // Set the path to the requested file |
| 478 | if ($path == '') |
| 479 | { |
| 480 | $ext = pathinfo($view, PATHINFO_EXTENSION); |
admin | 6ac4bea | 2006-09-02 17:46:15 +0000 | [diff] [blame] | 481 | $file = ($ext == '') ? $view.EXT : $view; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 482 | $path = $this->view_path.$file; |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | $x = explode('/', $path); |
| 487 | $file = end($x); |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * Buffer the output |
| 492 | * |
| 493 | * We buffer the output for two reasons: |
| 494 | * 1. Speed. You get a significant speed boost. |
| 495 | * 2. So that the final rendered template can be |
| 496 | * post-processed by the output class. Why do we |
| 497 | * need post processing? For one thing, in order to |
| 498 | * show the elapsed page load time. Unless we |
| 499 | * can intercept the content right before it's sent to |
| 500 | * the browser and then stop the timer, it won't be acurate. |
| 501 | * |
| 502 | */ |
| 503 | |
| 504 | if ( ! file_exists($path)) |
| 505 | { |
| 506 | show_error('Unable to load the requested file: '.$file); |
| 507 | } |
| 508 | |
| 509 | ob_start(); |
| 510 | |
| 511 | include($path); |
| 512 | log_message('debug', 'File loaded: '.$path); |
| 513 | |
| 514 | // Return the file data if requested to |
| 515 | if ($return === TRUE) |
| 516 | { |
| 517 | $buffer = ob_get_contents(); |
| 518 | ob_end_clean(); |
| 519 | |
| 520 | return $buffer; |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * Flush the buffer... or buff the flusher? |
| 525 | * |
| 526 | * In order to permit templates (views) to be nested within |
| 527 | * other views, we need to flush the content back out whenever |
| 528 | * we are beyond the first level of output buffering so that |
| 529 | * it can be seen and included properly by the first included |
| 530 | * template and any subsequent ones. Oy! |
| 531 | * |
| 532 | */ |
| 533 | if (ob_get_level() > $this->ob_level + 1) |
| 534 | { |
| 535 | ob_end_flush(); |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | $OUT->set_output(ob_get_contents()); |
| 540 | ob_end_clean(); |
| 541 | } |
| 542 | } |
| 543 | // END _load() |
| 544 | |
| 545 | // -------------------------------------------------------------------- |
| 546 | |
| 547 | /** |
| 548 | * Autoloader |
| 549 | * |
| 550 | * The config/autoload.php file contains an array that permits sub-systems, |
| 551 | * plugins, and helpers to be loaded automatically. |
| 552 | * |
| 553 | * @access private |
| 554 | * @param array |
| 555 | * @return void |
| 556 | */ |
| 557 | function _ci_autoloader($autoload) |
| 558 | { |
| 559 | if ($autoload === FALSE) |
| 560 | { |
| 561 | return; |
| 562 | } |
| 563 | |
| 564 | foreach (array('helper', 'plugin', 'script') as $type) |
| 565 | { |
| 566 | if (isset($autoload[$type])) |
| 567 | { |
admin | 2e5872a | 2006-09-06 02:32:55 +0000 | [diff] [blame] | 568 | $this->$type($autoload[$type]); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | } |
| 572 | // END _ci_autoloader() |
| 573 | |
| 574 | // -------------------------------------------------------------------- |
| 575 | |
| 576 | /** |
| 577 | * Object to Array |
| 578 | * |
| 579 | * Takes an object as input and convers the class variables to array key/vals |
| 580 | * |
| 581 | * @access public |
| 582 | * @param object |
| 583 | * @return array |
| 584 | */ |
| 585 | function _ci_object_to_array($object) |
| 586 | { |
admin | 2e5872a | 2006-09-06 02:32:55 +0000 | [diff] [blame] | 587 | return (is_object($object)) ? get_object_vars($object) : $object; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 588 | } |
admin | 2e5872a | 2006-09-06 02:32:55 +0000 | [diff] [blame] | 589 | // END _ci_object_to_array() |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 590 | |
| 591 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 592 | ?> |