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