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 | |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 31 | var $CI; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 32 | var $ob_level; |
| 33 | var $cached_vars = array(); |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame^] | 34 | var $models = array(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 35 | var $helpers = array(); |
| 36 | var $plugins = array(); |
| 37 | var $scripts = array(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 38 | var $view_path = ''; |
| 39 | |
| 40 | /** |
| 41 | * Constructor |
| 42 | * |
| 43 | * Sets the path to the view files and gets the initial output |
| 44 | * buffering level |
| 45 | * |
| 46 | * @access public |
| 47 | */ |
| 48 | function CI_Loader() |
| 49 | { |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 50 | $this->CI =& get_instance(); |
| 51 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 52 | $this->view_path = APPPATH.'views/'; |
| 53 | $this->ob_level = ob_get_level(); |
| 54 | |
| 55 | log_message('debug', "Loader Class Initialized"); |
| 56 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 57 | |
| 58 | // -------------------------------------------------------------------- |
| 59 | |
| 60 | /** |
| 61 | * Class Loader |
| 62 | * |
| 63 | * This function lets users load and instantiate classes. |
| 64 | * It is designed to be called from a user's app controllers. |
| 65 | * |
| 66 | * @access public |
| 67 | * @param string the name of the class |
| 68 | * @param mixed any initialization parameters |
| 69 | * @return void |
| 70 | */ |
admin | 7981a9a | 2006-09-26 07:52:09 +0000 | [diff] [blame] | 71 | function library($class, $param = NULL) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 72 | { |
| 73 | if ($class == '') |
| 74 | return; |
| 75 | |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 76 | $this->_ci_load_class($class, $param); |
| 77 | $this->_ci_assign_to_models(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 78 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 97 | // Is the model in a sub-folder? |
| 98 | // If so, parse out the filename and path. |
| 99 | if (strpos($model, '/') === FALSE) |
| 100 | { |
| 101 | $path = ''; |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | $x = explode('/', $model); |
| 106 | $model = end($x); |
| 107 | unset($x[count($x)-1]); |
| 108 | $path = implode('/', $x).'/'; |
| 109 | } |
| 110 | |
| 111 | if ($name == '') |
| 112 | { |
| 113 | $name = $model; |
| 114 | } |
| 115 | |
| 116 | if (in_array($name, $this->models, TRUE)) |
| 117 | { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | if (isset($this->CI->$name)) |
| 122 | { |
| 123 | show_error('The model name you are loading is the name of a resource that is already being used: '.$name); |
| 124 | } |
| 125 | |
| 126 | $model = strtolower($model); |
| 127 | |
| 128 | if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT)) |
| 129 | { |
| 130 | show_error('Unable to locate the model you have specified: '.$model); |
| 131 | } |
| 132 | |
| 133 | if ($db_conn !== FALSE) |
| 134 | { |
| 135 | if ($db_conn === TRUE) |
| 136 | $db_conn = ''; |
| 137 | |
| 138 | $this->CI->load->database($db_conn, FALSE, TRUE); |
| 139 | } |
| 140 | |
| 141 | if ( ! class_exists('Model')) |
| 142 | { |
| 143 | require_once(BASEPATH.'libraries/Model'.EXT); |
| 144 | } |
| 145 | |
| 146 | require_once(APPPATH.'models/'.$path.$model.EXT); |
| 147 | |
| 148 | $model = ucfirst($model); |
| 149 | $this->CI->$name = new $model(); |
| 150 | $this->models[] = $name; |
| 151 | $this->_ci_assign_to_models(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 152 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 153 | |
| 154 | // -------------------------------------------------------------------- |
| 155 | |
| 156 | /** |
| 157 | * Database Loader |
| 158 | * |
| 159 | * @access public |
| 160 | * @param string the DB credentials |
| 161 | * @param bool whether to return the DB object |
| 162 | * @param bool whether to enable active record (this allows us to override the config setting) |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 163 | * @return object |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 164 | */ |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 165 | function database($params = '', $return = FALSE, $active_record = FALSE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 166 | { |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 167 | require_once(BASEPATH.'database/DB'.EXT); |
admin | fd2750b | 2006-10-06 17:29:12 +0000 | [diff] [blame] | 168 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 169 | if ($return === TRUE) |
| 170 | { |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 171 | return DB($params, $return, $active_record); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 172 | } |
| 173 | else |
| 174 | { |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 175 | DB($params, $return, $active_record); |
| 176 | $this->_ci_assign_to_models(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 179 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 180 | // -------------------------------------------------------------------- |
| 181 | |
| 182 | /** |
| 183 | * Load View |
| 184 | * |
| 185 | * This function is used to load a "view" file. It has three parameters: |
| 186 | * |
| 187 | * 1. The name of the "view" file to be included. |
| 188 | * 2. An associative array of data to be extracted for use in the view. |
| 189 | * 3. TRUE/FALSE - whether to return the data or load it. In |
| 190 | * some cases it's advantageous to be able to retun data so that |
| 191 | * a developer can process it in some way. |
| 192 | * |
| 193 | * @access public |
| 194 | * @param string |
| 195 | * @param array |
| 196 | * @param bool |
| 197 | * @return void |
| 198 | */ |
| 199 | function view($view, $vars = array(), $return = FALSE) |
| 200 | { |
| 201 | return $this->_ci_load(array('view' => $view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return)); |
| 202 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 203 | |
| 204 | // -------------------------------------------------------------------- |
| 205 | |
| 206 | /** |
| 207 | * Load File |
| 208 | * |
| 209 | * This is a generic file loader |
| 210 | * |
| 211 | * @access public |
| 212 | * @param string |
| 213 | * @param bool |
| 214 | * @return string |
| 215 | */ |
| 216 | function file($path, $return = FALSE) |
| 217 | { |
| 218 | return $this->_ci_load(array('path' => $path, 'return' => $return)); |
| 219 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 220 | |
| 221 | // -------------------------------------------------------------------- |
| 222 | |
| 223 | /** |
| 224 | * Set Variables |
| 225 | * |
| 226 | * Once variables are set they become availabe within |
| 227 | * the controller class and its "view" files. |
| 228 | * |
| 229 | * @access public |
| 230 | * @param array |
| 231 | * @return void |
| 232 | */ |
| 233 | function vars($vars = array()) |
| 234 | { |
| 235 | $vars = $this->_ci_object_to_array($vars); |
| 236 | |
| 237 | if (is_array($vars) AND count($vars) > 0) |
| 238 | { |
| 239 | foreach ($vars as $key => $val) |
| 240 | { |
| 241 | $this->cached_vars[$key] = $val; |
| 242 | } |
| 243 | } |
| 244 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 245 | |
| 246 | // -------------------------------------------------------------------- |
| 247 | |
| 248 | /** |
| 249 | * Load Helper |
| 250 | * |
| 251 | * This function loads the specified helper file. |
| 252 | * |
| 253 | * @access public |
| 254 | * @param mixed |
| 255 | * @return void |
| 256 | */ |
| 257 | function helper($helpers = array()) |
| 258 | { |
| 259 | if ( ! is_array($helpers)) |
| 260 | { |
| 261 | $helpers = array($helpers); |
| 262 | } |
| 263 | |
| 264 | foreach ($helpers as $helper) |
| 265 | { |
| 266 | if (isset($this->helpers[$helper])) |
| 267 | { |
| 268 | continue; |
| 269 | } |
| 270 | |
| 271 | $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper'); |
| 272 | |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 273 | if (file_exists(APPPATH.'helpers/'.$helper.EXT)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 274 | { |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 275 | include_once(APPPATH.'helpers/'.$helper.EXT); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 276 | } |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 277 | else |
| 278 | { |
| 279 | if (file_exists(BASEPATH.'helpers/'.$helper.EXT)) |
| 280 | { |
| 281 | include_once(BASEPATH.'helpers/'.$helper.EXT); |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | show_error('Unable to load the requested file: helpers/'.$helper.EXT); |
| 286 | } |
| 287 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 288 | |
| 289 | $this->helpers[$helper] = TRUE; |
| 290 | } |
| 291 | |
| 292 | log_message('debug', 'Helpers loaded: '.implode(', ', $helpers)); |
| 293 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 294 | |
| 295 | // -------------------------------------------------------------------- |
| 296 | |
| 297 | /** |
| 298 | * Load Helpers |
| 299 | * |
| 300 | * This is simply an alias to the above function in case the |
| 301 | * user has written the plural form of this function. |
| 302 | * |
| 303 | * @access public |
| 304 | * @param array |
| 305 | * @return void |
| 306 | */ |
| 307 | function helpers($helpers = array()) |
| 308 | { |
| 309 | $this->helper($helpers); |
| 310 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 311 | |
| 312 | // -------------------------------------------------------------------- |
| 313 | |
| 314 | /** |
| 315 | * Load Plugin |
| 316 | * |
| 317 | * This function loads the specified plugin. |
| 318 | * |
| 319 | * @access public |
| 320 | * @param array |
| 321 | * @return void |
| 322 | */ |
| 323 | function plugin($plugins = array()) |
| 324 | { |
| 325 | if ( ! is_array($plugins)) |
| 326 | { |
| 327 | $plugins = array($plugins); |
| 328 | } |
| 329 | |
| 330 | foreach ($plugins as $plugin) |
| 331 | { |
| 332 | if (isset($this->plugins[$plugin])) |
| 333 | { |
| 334 | continue; |
| 335 | } |
| 336 | |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 337 | $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 338 | |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 339 | if (file_exists(APPPATH.'plugins/'.$plugin.EXT)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 340 | { |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 341 | include_once(APPPATH.'plugins/'.$plugin.EXT); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 342 | } |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 343 | else |
| 344 | { |
| 345 | if (file_exists(BASEPATH.'plugins/'.$plugin.EXT)) |
| 346 | { |
| 347 | include_once(BASEPATH.'plugins/'.$plugin.EXT); |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | show_error('Unable to load the requested file: plugins/'.$plugin.EXT); |
| 352 | } |
| 353 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 354 | |
| 355 | $this->plugins[$plugin] = TRUE; |
| 356 | } |
| 357 | |
| 358 | log_message('debug', 'Plugins loaded: '.implode(', ', $plugins)); |
| 359 | } |
admin | fd2750b | 2006-10-06 17:29:12 +0000 | [diff] [blame] | 360 | |
| 361 | // -------------------------------------------------------------------- |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 362 | |
admin | fd2750b | 2006-10-06 17:29:12 +0000 | [diff] [blame] | 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 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 378 | // -------------------------------------------------------------------- |
| 379 | |
| 380 | /** |
| 381 | * Load Script |
| 382 | * |
| 383 | * This function loads the specified include file from the |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 384 | * application/scripts/ folder. |
| 385 | * |
| 386 | * NOTE: This feature has been deprecated but it will remain available |
| 387 | * for legacy users. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 388 | * |
| 389 | * @access public |
| 390 | * @param array |
| 391 | * @return void |
| 392 | */ |
| 393 | function script($scripts = array()) |
| 394 | { |
| 395 | if ( ! is_array($scripts)) |
| 396 | { |
| 397 | $scripts = array($scripts); |
| 398 | } |
| 399 | |
| 400 | foreach ($scripts as $script) |
| 401 | { |
| 402 | if (isset($this->scripts[$script])) |
| 403 | { |
| 404 | continue; |
| 405 | } |
| 406 | |
| 407 | $script = strtolower(str_replace(EXT, '', $script)); |
| 408 | |
| 409 | if ( ! file_exists(APPPATH.'scripts/'.$script.EXT)) |
| 410 | { |
| 411 | show_error('Unable to load the requested script: scripts/'.$script.EXT); |
| 412 | } |
| 413 | |
| 414 | include_once(APPPATH.'scripts/'.$script.EXT); |
| 415 | |
| 416 | $this->scripts[$script] = TRUE; |
| 417 | } |
| 418 | |
| 419 | log_message('debug', 'Scripts loaded: '.implode(', ', $scripts)); |
| 420 | } |
admin | fd2750b | 2006-10-06 17:29:12 +0000 | [diff] [blame] | 421 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 422 | // -------------------------------------------------------------------- |
| 423 | |
| 424 | /** |
| 425 | * Loads a language file |
| 426 | * |
| 427 | * @access public |
| 428 | * @param string |
| 429 | * @return void |
| 430 | */ |
| 431 | function language($file = '', $lang = '', $return = FALSE) |
| 432 | { |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 433 | return $this->CI->lang->load($file, $lang, $return); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 434 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | { |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 447 | $this->CI->config->load($file); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 448 | } |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 449 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 450 | // -------------------------------------------------------------------- |
| 451 | |
| 452 | /** |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 453 | * Scaffolding Loader |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 454 | * |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 455 | * This initializing function works a bit different than the |
| 456 | * others. It doesn't load the class. Instead, it simply |
| 457 | * sets a flag indicating that scaffolding is allowed to be |
| 458 | * used. The actual scaffolding function below is |
| 459 | * called by the front controller based on whether the |
| 460 | * second segment of the URL matches the "secret" scaffolding |
| 461 | * word stored in the application/config/routes.php |
| 462 | * |
| 463 | * @access public |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 464 | * @param string |
| 465 | * @return void |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 466 | */ |
| 467 | function scaffolding($table = '') |
| 468 | { |
| 469 | if ($table === FALSE) |
| 470 | { |
| 471 | show_error('You must include the name of the table you would like access when you initialize scaffolding'); |
| 472 | } |
| 473 | |
| 474 | $this->CI->_ci_scaffolding = TRUE; |
| 475 | $this->CI->_ci_scaff_table = $table; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 476 | } |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 477 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 478 | // -------------------------------------------------------------------- |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 479 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 480 | /** |
| 481 | * Loader |
| 482 | * |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 483 | * This function is used to load views and files. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 484 | * |
| 485 | * @access private |
| 486 | * @param array |
| 487 | * @return void |
| 488 | */ |
| 489 | function _ci_load($data) |
admin | 212a3fa | 2006-09-23 04:22:39 +0000 | [diff] [blame] | 490 | { |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 491 | // This allows anything loaded using $this->load (viwes, files, etc.) |
| 492 | // to become accessible from within the Controller and Model functions. |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 493 | foreach (get_object_vars($this->CI) as $key => $var) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 494 | { |
admin | e79dc71 | 2006-09-26 03:52:45 +0000 | [diff] [blame] | 495 | if (is_object($var)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 496 | { |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 497 | $this->$key =& $this->CI->$key; |
admin | e79dc71 | 2006-09-26 03:52:45 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 501 | // Set the default data variables |
| 502 | foreach (array('view', 'vars', 'path', 'return') as $val) |
| 503 | { |
| 504 | $$val = ( ! isset($data[$val])) ? FALSE : $data[$val]; |
| 505 | } |
| 506 | |
| 507 | /* |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 508 | * Extract and cache variables |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 509 | * |
admin | 212a3fa | 2006-09-23 04:22:39 +0000 | [diff] [blame] | 510 | * You can either set variables using the dedicated $this->load_vars() |
| 511 | * function or via the second parameter of this function. We'll merge |
| 512 | * the two types and cache them so that views that are embedded within |
| 513 | * other views can have access to these variables. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 514 | */ |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 515 | if (is_array($vars)) |
| 516 | { |
| 517 | $this->cached_vars = array_merge($this->cached_vars, $vars); |
| 518 | } |
| 519 | extract($this->cached_vars); |
| 520 | |
| 521 | // Set the path to the requested file |
| 522 | if ($path == '') |
| 523 | { |
| 524 | $ext = pathinfo($view, PATHINFO_EXTENSION); |
admin | 6ac4bea | 2006-09-02 17:46:15 +0000 | [diff] [blame] | 525 | $file = ($ext == '') ? $view.EXT : $view; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 526 | $path = $this->view_path.$file; |
| 527 | } |
| 528 | else |
| 529 | { |
| 530 | $x = explode('/', $path); |
| 531 | $file = end($x); |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Buffer the output |
| 536 | * |
| 537 | * We buffer the output for two reasons: |
| 538 | * 1. Speed. You get a significant speed boost. |
| 539 | * 2. So that the final rendered template can be |
| 540 | * post-processed by the output class. Why do we |
| 541 | * need post processing? For one thing, in order to |
| 542 | * show the elapsed page load time. Unless we |
| 543 | * can intercept the content right before it's sent to |
admin | 212a3fa | 2006-09-23 04:22:39 +0000 | [diff] [blame] | 544 | * the browser and then stop the timer it won't be acurate. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 545 | */ |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 546 | if ( ! file_exists($path)) |
| 547 | { |
| 548 | show_error('Unable to load the requested file: '.$file); |
| 549 | } |
| 550 | |
| 551 | ob_start(); |
| 552 | |
| 553 | include($path); |
| 554 | log_message('debug', 'File loaded: '.$path); |
| 555 | |
| 556 | // Return the file data if requested to |
| 557 | if ($return === TRUE) |
| 558 | { |
| 559 | $buffer = ob_get_contents(); |
| 560 | ob_end_clean(); |
| 561 | |
| 562 | return $buffer; |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * Flush the buffer... or buff the flusher? |
| 567 | * |
admin | 212a3fa | 2006-09-23 04:22:39 +0000 | [diff] [blame] | 568 | * In order to permit views to be nested within |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 569 | * other views, we need to flush the content back out whenever |
| 570 | * we are beyond the first level of output buffering so that |
| 571 | * it can be seen and included properly by the first included |
| 572 | * template and any subsequent ones. Oy! |
| 573 | * |
| 574 | */ |
| 575 | if (ob_get_level() > $this->ob_level + 1) |
| 576 | { |
| 577 | ob_end_flush(); |
| 578 | } |
| 579 | else |
| 580 | { |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 581 | $this->CI->output->set_output(ob_get_contents()); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 582 | ob_end_clean(); |
| 583 | } |
| 584 | } |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 585 | |
| 586 | // -------------------------------------------------------------------- |
| 587 | |
| 588 | /** |
| 589 | * Load class |
| 590 | * |
| 591 | * This function loads the requested class. |
| 592 | * |
| 593 | * @access private |
| 594 | * @param string the item that is being loaded |
| 595 | * @param mixed any additional parameters |
| 596 | * @return void |
| 597 | */ |
| 598 | function _ci_load_class($class, $params = NULL) |
| 599 | { |
| 600 | // Prep the class name |
| 601 | $class = strtolower(str_replace(EXT, '', $class)); |
| 602 | |
| 603 | // Bug fix for backward compat. |
| 604 | // Kill this at some point in the future |
| 605 | if ($class == 'unit_test') |
| 606 | { |
| 607 | $class = 'unit'; |
| 608 | } |
| 609 | |
| 610 | // Is this a class extension request? |
| 611 | if (substr($class, 0, 3) == 'my_') |
| 612 | { |
| 613 | $class = preg_replace("/my_(.+)/", "\\1", $class); |
| 614 | $extend = TRUE; |
| 615 | } |
| 616 | else |
| 617 | { |
| 618 | $extend = FALSE; |
| 619 | } |
| 620 | |
| 621 | // Are we extending one of the base classes? |
| 622 | if ($extend == TRUE) |
| 623 | { |
| 624 | // Load the requested library from the main system/libraries folder |
| 625 | if (file_exists(BASEPATH.'libraries/'.ucfirst($class).EXT)) |
| 626 | { |
| 627 | include_once(BASEPATH.'libraries/'.ucfirst($class).EXT); |
| 628 | } |
| 629 | |
| 630 | // Now look for a matching library |
| 631 | foreach (array(ucfirst($class), $class) as $filename) |
| 632 | { |
| 633 | if (file_exists(APPPATH.'libraries/'.$filename.EXT)) |
| 634 | { |
| 635 | include_once(APPPATH.'libraries/'.$filename.EXT); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | return $this->_ci_init_class($filename, 'MY_', $params); |
| 640 | } |
| 641 | else |
| 642 | { |
| 643 | // Lets search for the requested library file and load it. |
| 644 | // For backward compatibility we'll test for filenames that are |
| 645 | // both uppercase and lower. |
| 646 | foreach (array(ucfirst($class), $class) as $filename) |
| 647 | { |
| 648 | for ($i = 1; $i < 3; $i++) |
| 649 | { |
| 650 | $path = ($i % 2) ? APPPATH : BASEPATH; |
| 651 | |
| 652 | if (file_exists($path.'libraries/'.$filename.EXT)) |
| 653 | { |
| 654 | include_once($path.'libraries/'.$filename.EXT); |
| 655 | return $this->_ci_init_class($filename, '', $params); |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | // If we got this far we were unable to find the requested class |
| 662 | log_message('error', "Unable to load the requested class: ".$class); |
| 663 | show_error("Unable to load the class: ".$class); |
| 664 | } |
| 665 | |
| 666 | // -------------------------------------------------------------------- |
| 667 | |
| 668 | /** |
| 669 | * Instantiates a class |
| 670 | * |
| 671 | * @access private |
| 672 | * @param string |
| 673 | * @param string |
| 674 | * @return null |
| 675 | */ |
| 676 | function _ci_init_class($class, $prefix = '', $config = NULL) |
| 677 | { |
| 678 | // Is there an associated config file for this class? |
| 679 | if ($config == NULL) |
| 680 | { |
| 681 | if (file_exists(APPPATH.'config/'.$class.EXT)) |
| 682 | { |
| 683 | include_once(APPPATH.'config/'.$class.EXT); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | if ($prefix == '') |
| 688 | { |
| 689 | $name = ( ! class_exists($class)) ? 'CI_'.$class : $class; |
| 690 | } |
| 691 | else |
| 692 | { |
| 693 | $name = $prefix.$class; |
| 694 | } |
| 695 | |
| 696 | $varname = ( ! isset($remap[$class])) ? $class : $remap[$class]; |
| 697 | $varname = strtolower($varname); |
| 698 | |
| 699 | // Instantiate the class |
| 700 | if ($config !== NULL) |
| 701 | { |
| 702 | $this->CI->$varname = new $name($config); |
| 703 | } |
| 704 | else |
| 705 | { |
| 706 | $this->CI->$varname = new $name; |
| 707 | } |
| 708 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 709 | |
| 710 | // -------------------------------------------------------------------- |
| 711 | |
| 712 | /** |
| 713 | * Autoloader |
| 714 | * |
| 715 | * The config/autoload.php file contains an array that permits sub-systems, |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 716 | * libraries, plugins, and helpers to be loaded automatically. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 717 | * |
| 718 | * @access private |
| 719 | * @param array |
| 720 | * @return void |
| 721 | */ |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 722 | function _ci_autoloader() |
| 723 | { |
| 724 | include_once(APPPATH.'config/autoload'.EXT); |
| 725 | |
| 726 | if ( ! isset($autoload)) |
| 727 | { |
| 728 | return FALSE; |
| 729 | } |
| 730 | |
| 731 | // Load any custome config file |
| 732 | if (count($autoload['config']) > 0) |
| 733 | { |
| 734 | foreach ($autoload['config'] as $key => $val) |
| 735 | { |
| 736 | $this->CI->config->load($val); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | // Load plugins, helpers, and scripts |
| 741 | foreach (array('helper', 'plugin', 'script') as $type) |
| 742 | { |
| 743 | if (isset($autoload[$type]) AND count($autoload[$type]) > 0) |
| 744 | { |
| 745 | $this->$type($autoload[$type]); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | // A little tweak to remain backward compatible |
| 750 | // The $autoload['core'] item was deprecated |
| 751 | if ( ! isset($autoload['libraries'])) |
| 752 | { |
| 753 | $autoload['libraries'] = $autoload['core']; |
| 754 | } |
| 755 | |
| 756 | // Load libraries |
| 757 | if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0) |
| 758 | { |
| 759 | if (in_array('database', $autoload['libraries'])) |
| 760 | { |
| 761 | $this->database(); |
| 762 | $autoload['libraries'] = array_diff($autoload['libraries'], array('database')); |
| 763 | } |
| 764 | |
| 765 | if (in_array('model', $autoload['libraries'])) |
| 766 | { |
| 767 | $this->model(); |
| 768 | $autoload['libraries'] = array_diff($autoload['libraries'], array('model')); |
| 769 | } |
| 770 | |
| 771 | if (in_array('scaffolding', $autoload['libraries'])) |
| 772 | { |
| 773 | $this->scaffolding(); |
| 774 | $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding')); |
| 775 | } |
| 776 | |
| 777 | foreach ($autoload['libraries'] as $item) |
| 778 | { |
| 779 | $this->library($item); |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | // -------------------------------------------------------------------- |
| 785 | |
| 786 | /** |
| 787 | * Assign to Models |
| 788 | * |
| 789 | * Makes sure that anything loaded by the loader class (libraries, plugins, etc.) |
| 790 | * will be available to modles, if any exist. |
| 791 | * |
| 792 | * @access public |
| 793 | * @param object |
| 794 | * @return array |
| 795 | */ |
| 796 | function _ci_assign_to_models() |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 797 | { |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 798 | if (count($this->models) == 0) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 799 | { |
| 800 | return; |
| 801 | } |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 802 | foreach ($this->models as $model) |
| 803 | { |
| 804 | $this->CI->$model->_assign_libraries(); |
| 805 | } |
| 806 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 807 | |
| 808 | // -------------------------------------------------------------------- |
| 809 | |
| 810 | /** |
| 811 | * Object to Array |
| 812 | * |
| 813 | * Takes an object as input and convers the class variables to array key/vals |
| 814 | * |
| 815 | * @access public |
| 816 | * @param object |
| 817 | * @return array |
| 818 | */ |
| 819 | function _ci_object_to_array($object) |
| 820 | { |
admin | 2e5872a | 2006-09-06 02:32:55 +0000 | [diff] [blame] | 821 | return (is_object($object)) ? get_object_vars($object) : $object; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 822 | } |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 823 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 824 | |
| 825 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 826 | ?> |