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. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Loader Class |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 20 | * |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | 34f7f2d | 2006-10-10 08:06:42 +0000 | [diff] [blame] | 31 | // All these are set automatically. Don't mess with them. |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 32 | var $_ci_ob_level; |
| 33 | var $_ci_view_path = ''; |
admin | 34f7f2d | 2006-10-10 08:06:42 +0000 | [diff] [blame] | 34 | var $_ci_is_php5 = FALSE; |
admin | 1e30398 | 2006-10-10 16:35:43 +0000 | [diff] [blame] | 35 | var $_ci_is_instance = FALSE; // Whether we should use $this or $CI =& get_instance() |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 36 | var $_ci_cached_vars = array(); |
admin | 9139b0c | 2006-10-31 17:59:16 +0000 | [diff] [blame] | 37 | var $_ci_classes = array(); |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 38 | var $_ci_models = array(); |
| 39 | var $_ci_helpers = array(); |
| 40 | var $_ci_plugins = array(); |
| 41 | var $_ci_scripts = array(); |
| 42 | var $_ci_varmap = array('unit_test' => 'unit', 'user_agent' => 'agent'); |
| 43 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Constructor |
| 47 | * |
admin | 34f7f2d | 2006-10-10 08:06:42 +0000 | [diff] [blame] | 48 | * Sets the path to the view files and gets the initial output buffering level |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 49 | * |
| 50 | * @access public |
| 51 | */ |
| 52 | function CI_Loader() |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 53 | { |
admin | 34f7f2d | 2006-10-10 08:06:42 +0000 | [diff] [blame] | 54 | $this->_ci_is_php5 = (floor(phpversion()) >= 5) ? TRUE : FALSE; |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 55 | $this->_ci_view_path = APPPATH.'views/'; |
| 56 | $this->_ci_ob_level = ob_get_level(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 57 | |
| 58 | log_message('debug', "Loader Class Initialized"); |
| 59 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 60 | |
| 61 | // -------------------------------------------------------------------- |
| 62 | |
| 63 | /** |
| 64 | * Class Loader |
| 65 | * |
| 66 | * This function lets users load and instantiate classes. |
| 67 | * It is designed to be called from a user's app controllers. |
| 68 | * |
| 69 | * @access public |
| 70 | * @param string the name of the class |
admin | 80b2bd9 | 2006-10-12 18:30:36 +0000 | [diff] [blame] | 71 | * @param mixed the optional parameters |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 72 | * @return void |
| 73 | */ |
admin | be013b3 | 2006-11-04 05:07:03 +0000 | [diff] [blame] | 74 | function library($library = '', $params = NULL) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 75 | { |
admin | be013b3 | 2006-11-04 05:07:03 +0000 | [diff] [blame] | 76 | if ($library == '') |
| 77 | { |
| 78 | return FALSE; |
| 79 | } |
| 80 | |
| 81 | if (is_array($library)) |
| 82 | { |
| 83 | foreach ($library as $class) |
| 84 | { |
| 85 | $this->_ci_load_class($class, $params); |
| 86 | } |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | $this->_ci_load_class($library, $params); |
| 91 | } |
| 92 | |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 93 | $this->_ci_assign_to_models(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 94 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 95 | |
| 96 | // -------------------------------------------------------------------- |
| 97 | |
| 98 | /** |
| 99 | * Model Loader |
| 100 | * |
| 101 | * This function lets users load and instantiate models. |
| 102 | * |
| 103 | * @access public |
| 104 | * @param string the name of the class |
| 105 | * @param mixed any initialization parameters |
| 106 | * @return void |
| 107 | */ |
| 108 | function model($model, $name = '', $db_conn = FALSE) |
| 109 | { |
| 110 | if ($model == '') |
| 111 | return; |
| 112 | |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 113 | // Is the model in a sub-folder? If so, parse out the filename and path. |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 114 | if (strpos($model, '/') === FALSE) |
| 115 | { |
| 116 | $path = ''; |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | $x = explode('/', $model); |
| 121 | $model = end($x); |
| 122 | unset($x[count($x)-1]); |
| 123 | $path = implode('/', $x).'/'; |
| 124 | } |
| 125 | |
| 126 | if ($name == '') |
| 127 | { |
| 128 | $name = $model; |
| 129 | } |
| 130 | |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 131 | if (in_array($name, $this->_ci_models, TRUE)) |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 132 | { |
| 133 | return; |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 134 | } |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 135 | |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 136 | $CI =& get_instance(); |
| 137 | if (isset($CI->$name)) |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 138 | { |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 139 | show_error('The model name you are loading is the name of a resource that is already being used: '.$name); |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | $model = strtolower($model); |
| 143 | |
| 144 | if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT)) |
| 145 | { |
| 146 | show_error('Unable to locate the model you have specified: '.$model); |
| 147 | } |
| 148 | |
Rick Ellis | 6f62816 | 2006-11-24 01:17:45 +0000 | [diff] [blame^] | 149 | if ($db_conn !== FALSE AND ! class_exists('CI_DB')) |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 150 | { |
| 151 | if ($db_conn === TRUE) |
| 152 | $db_conn = ''; |
| 153 | |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 154 | $CI->load->database($db_conn, FALSE, TRUE); |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | if ( ! class_exists('Model')) |
| 158 | { |
| 159 | require_once(BASEPATH.'libraries/Model'.EXT); |
| 160 | } |
| 161 | |
| 162 | require_once(APPPATH.'models/'.$path.$model.EXT); |
| 163 | |
| 164 | $model = ucfirst($model); |
admin | 34f7f2d | 2006-10-10 08:06:42 +0000 | [diff] [blame] | 165 | |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 166 | $CI->$name = new $model(); |
| 167 | $CI->$name->_assign_libraries(); |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 168 | |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 169 | $this->_ci_models[] = $name; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 170 | } |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 171 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
| 175 | * Database Loader |
| 176 | * |
| 177 | * @access public |
| 178 | * @param string the DB credentials |
| 179 | * @param bool whether to return the DB object |
| 180 | * @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] | 181 | * @return object |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 182 | */ |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 183 | function database($params = '', $return = FALSE, $active_record = FALSE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 184 | { |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 185 | // Do we even need to load the database class? |
| 186 | if (class_exists('CI_DB') AND $return == FALSE AND $active_record == FALSE) |
| 187 | { |
| 188 | return FALSE; |
| 189 | } |
| 190 | |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 191 | require_once(BASEPATH.'database/DB'.EXT); |
admin | fd2750b | 2006-10-06 17:29:12 +0000 | [diff] [blame] | 192 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 193 | if ($return === TRUE) |
| 194 | { |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 195 | return DB($params, $active_record); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 196 | } |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 197 | |
admin | 2b47ea4 | 2006-10-24 00:15:02 +0000 | [diff] [blame] | 198 | // Grab the super object |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 199 | $CI =& get_instance(); |
admin | 2b47ea4 | 2006-10-24 00:15:02 +0000 | [diff] [blame] | 200 | |
| 201 | // Initialize the db variable. Needed to prevent |
| 202 | // reference errors with some configurations |
| 203 | $CI->db = ''; |
| 204 | |
| 205 | // Load the DB class |
| 206 | $CI->db =& DB($params, $active_record); |
| 207 | |
| 208 | // Assign the DB object to any existing models |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 209 | $this->_ci_assign_to_models(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 210 | } |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 211 | |
| 212 | // -------------------------------------------------------------------- |
| 213 | |
| 214 | /** |
| 215 | * Load the Utilities Class |
| 216 | * |
| 217 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 218 | * @return string |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 219 | */ |
| 220 | function dbutil() |
| 221 | { |
| 222 | if ( ! class_exists('CI_DB')) |
| 223 | { |
| 224 | $this->database(); |
| 225 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 226 | |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 227 | $CI =& get_instance(); |
| 228 | |
| 229 | require_once(BASEPATH.'database/DB_utility'.EXT); |
| 230 | require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT); |
| 231 | $class = 'CI_DB_'.$CI->db->dbdriver.'_utility'; |
| 232 | |
| 233 | $CI->dbutil = new $class(); |
| 234 | $CI->load->_ci_assign_to_models(); |
| 235 | } |
| 236 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 237 | // -------------------------------------------------------------------- |
| 238 | |
| 239 | /** |
| 240 | * Load View |
| 241 | * |
| 242 | * This function is used to load a "view" file. It has three parameters: |
| 243 | * |
| 244 | * 1. The name of the "view" file to be included. |
| 245 | * 2. An associative array of data to be extracted for use in the view. |
| 246 | * 3. TRUE/FALSE - whether to return the data or load it. In |
admin | b807f32 | 2006-10-20 04:55:37 +0000 | [diff] [blame] | 247 | * some cases it's advantageous to be able to return data so that |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 248 | * a developer can process it in some way. |
| 249 | * |
| 250 | * @access public |
| 251 | * @param string |
| 252 | * @param array |
| 253 | * @param bool |
| 254 | * @return void |
| 255 | */ |
| 256 | function view($view, $vars = array(), $return = FALSE) |
| 257 | { |
| 258 | return $this->_ci_load(array('view' => $view, 'vars' => $this->_ci_object_to_array($vars), 'return' => $return)); |
| 259 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 260 | |
| 261 | // -------------------------------------------------------------------- |
| 262 | |
| 263 | /** |
| 264 | * Load File |
| 265 | * |
| 266 | * This is a generic file loader |
| 267 | * |
| 268 | * @access public |
| 269 | * @param string |
| 270 | * @param bool |
| 271 | * @return string |
| 272 | */ |
| 273 | function file($path, $return = FALSE) |
| 274 | { |
| 275 | return $this->_ci_load(array('path' => $path, 'return' => $return)); |
| 276 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 277 | |
| 278 | // -------------------------------------------------------------------- |
| 279 | |
| 280 | /** |
| 281 | * Set Variables |
| 282 | * |
admin | b807f32 | 2006-10-20 04:55:37 +0000 | [diff] [blame] | 283 | * Once variables are set they become available within |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 284 | * the controller class and its "view" files. |
| 285 | * |
| 286 | * @access public |
| 287 | * @param array |
| 288 | * @return void |
| 289 | */ |
| 290 | function vars($vars = array()) |
| 291 | { |
| 292 | $vars = $this->_ci_object_to_array($vars); |
| 293 | |
| 294 | if (is_array($vars) AND count($vars) > 0) |
| 295 | { |
| 296 | foreach ($vars as $key => $val) |
| 297 | { |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 298 | $this->_ci_cached_vars[$key] = $val; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 302 | |
| 303 | // -------------------------------------------------------------------- |
| 304 | |
| 305 | /** |
| 306 | * Load Helper |
| 307 | * |
| 308 | * This function loads the specified helper file. |
| 309 | * |
| 310 | * @access public |
| 311 | * @param mixed |
| 312 | * @return void |
| 313 | */ |
| 314 | function helper($helpers = array()) |
| 315 | { |
| 316 | if ( ! is_array($helpers)) |
| 317 | { |
| 318 | $helpers = array($helpers); |
| 319 | } |
| 320 | |
| 321 | foreach ($helpers as $helper) |
admin | 9139b0c | 2006-10-31 17:59:16 +0000 | [diff] [blame] | 322 | { |
| 323 | $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper'); |
| 324 | |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 325 | if (isset($this->_ci_helpers[$helper])) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 326 | { |
| 327 | continue; |
| 328 | } |
admin | 9139b0c | 2006-10-31 17:59:16 +0000 | [diff] [blame] | 329 | |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 330 | if (file_exists(APPPATH.'helpers/'.$helper.EXT)) |
admin | 9139b0c | 2006-10-31 17:59:16 +0000 | [diff] [blame] | 331 | { |
| 332 | include_once(APPPATH.'helpers/'.$helper.EXT); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 333 | } |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 334 | else |
| 335 | { |
| 336 | if (file_exists(BASEPATH.'helpers/'.$helper.EXT)) |
| 337 | { |
admin | f3a6204 | 2006-10-29 20:24:11 +0000 | [diff] [blame] | 338 | include(BASEPATH.'helpers/'.$helper.EXT); |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 339 | } |
| 340 | else |
| 341 | { |
| 342 | show_error('Unable to load the requested file: helpers/'.$helper.EXT); |
| 343 | } |
| 344 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 345 | |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 346 | $this->_ci_helpers[$helper] = TRUE; |
admin | 9139b0c | 2006-10-31 17:59:16 +0000 | [diff] [blame] | 347 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | log_message('debug', 'Helpers loaded: '.implode(', ', $helpers)); |
| 351 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 352 | |
| 353 | // -------------------------------------------------------------------- |
| 354 | |
| 355 | /** |
| 356 | * Load Helpers |
| 357 | * |
| 358 | * This is simply an alias to the above function in case the |
| 359 | * user has written the plural form of this function. |
| 360 | * |
| 361 | * @access public |
| 362 | * @param array |
| 363 | * @return void |
| 364 | */ |
| 365 | function helpers($helpers = array()) |
| 366 | { |
| 367 | $this->helper($helpers); |
| 368 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 369 | |
| 370 | // -------------------------------------------------------------------- |
| 371 | |
| 372 | /** |
| 373 | * Load Plugin |
| 374 | * |
| 375 | * This function loads the specified plugin. |
| 376 | * |
| 377 | * @access public |
| 378 | * @param array |
| 379 | * @return void |
| 380 | */ |
| 381 | function plugin($plugins = array()) |
| 382 | { |
| 383 | if ( ! is_array($plugins)) |
| 384 | { |
| 385 | $plugins = array($plugins); |
| 386 | } |
| 387 | |
| 388 | foreach ($plugins as $plugin) |
admin | 9139b0c | 2006-10-31 17:59:16 +0000 | [diff] [blame] | 389 | { |
| 390 | $plugin = strtolower(str_replace(EXT, '', str_replace('_plugin.', '', $plugin)).'_pi'); |
| 391 | |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 392 | if (isset($this->_ci_plugins[$plugin])) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 393 | { |
| 394 | continue; |
| 395 | } |
admin | 9139b0c | 2006-10-31 17:59:16 +0000 | [diff] [blame] | 396 | |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 397 | if (file_exists(APPPATH.'plugins/'.$plugin.EXT)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 398 | { |
admin | f3a6204 | 2006-10-29 20:24:11 +0000 | [diff] [blame] | 399 | include(APPPATH.'plugins/'.$plugin.EXT); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 400 | } |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 401 | else |
| 402 | { |
| 403 | if (file_exists(BASEPATH.'plugins/'.$plugin.EXT)) |
| 404 | { |
admin | f3a6204 | 2006-10-29 20:24:11 +0000 | [diff] [blame] | 405 | include(BASEPATH.'plugins/'.$plugin.EXT); |
admin | 480da81 | 2006-09-20 21:11:04 +0000 | [diff] [blame] | 406 | } |
| 407 | else |
| 408 | { |
| 409 | show_error('Unable to load the requested file: plugins/'.$plugin.EXT); |
| 410 | } |
| 411 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 412 | |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 413 | $this->_ci_plugins[$plugin] = TRUE; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | log_message('debug', 'Plugins loaded: '.implode(', ', $plugins)); |
| 417 | } |
admin | fd2750b | 2006-10-06 17:29:12 +0000 | [diff] [blame] | 418 | |
| 419 | // -------------------------------------------------------------------- |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 420 | |
admin | fd2750b | 2006-10-06 17:29:12 +0000 | [diff] [blame] | 421 | /** |
| 422 | * Load Plugins |
| 423 | * |
| 424 | * This is simply an alias to the above function in case the |
| 425 | * user has written the plural form of this function. |
| 426 | * |
| 427 | * @access public |
| 428 | * @param array |
| 429 | * @return void |
| 430 | */ |
| 431 | function plugins($plugins = array()) |
| 432 | { |
| 433 | $this->plugin($plugins); |
| 434 | } |
| 435 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 436 | // -------------------------------------------------------------------- |
| 437 | |
| 438 | /** |
| 439 | * Load Script |
| 440 | * |
| 441 | * This function loads the specified include file from the |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 442 | * application/scripts/ folder. |
| 443 | * |
| 444 | * NOTE: This feature has been deprecated but it will remain available |
| 445 | * for legacy users. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 446 | * |
| 447 | * @access public |
| 448 | * @param array |
| 449 | * @return void |
| 450 | */ |
| 451 | function script($scripts = array()) |
| 452 | { |
| 453 | if ( ! is_array($scripts)) |
| 454 | { |
| 455 | $scripts = array($scripts); |
| 456 | } |
| 457 | |
| 458 | foreach ($scripts as $script) |
admin | 9139b0c | 2006-10-31 17:59:16 +0000 | [diff] [blame] | 459 | { |
| 460 | $script = strtolower(str_replace(EXT, '', $script)); |
| 461 | |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 462 | if (isset($this->_ci_scripts[$script])) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 463 | { |
| 464 | continue; |
| 465 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 466 | |
| 467 | if ( ! file_exists(APPPATH.'scripts/'.$script.EXT)) |
| 468 | { |
| 469 | show_error('Unable to load the requested script: scripts/'.$script.EXT); |
| 470 | } |
| 471 | |
admin | f3a6204 | 2006-10-29 20:24:11 +0000 | [diff] [blame] | 472 | include(APPPATH.'scripts/'.$script.EXT); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | log_message('debug', 'Scripts loaded: '.implode(', ', $scripts)); |
| 476 | } |
admin | fd2750b | 2006-10-06 17:29:12 +0000 | [diff] [blame] | 477 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 478 | // -------------------------------------------------------------------- |
| 479 | |
| 480 | /** |
| 481 | * Loads a language file |
| 482 | * |
| 483 | * @access public |
| 484 | * @param string |
| 485 | * @return void |
| 486 | */ |
| 487 | function language($file = '', $lang = '', $return = FALSE) |
| 488 | { |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 489 | $CI =& get_instance(); |
| 490 | return $CI->lang->load($file, $lang, $return); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 491 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 492 | |
| 493 | // -------------------------------------------------------------------- |
| 494 | |
| 495 | /** |
| 496 | * Loads a config file |
| 497 | * |
| 498 | * @access public |
| 499 | * @param string |
| 500 | * @return void |
| 501 | */ |
| 502 | function config($file = '') |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 503 | { |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 504 | $CI =& get_instance(); |
| 505 | $CI->config->load($file); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 506 | } |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 507 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 508 | // -------------------------------------------------------------------- |
| 509 | |
| 510 | /** |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 511 | * Scaffolding Loader |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 512 | * |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 513 | * This initializing function works a bit different than the |
| 514 | * others. It doesn't load the class. Instead, it simply |
| 515 | * sets a flag indicating that scaffolding is allowed to be |
| 516 | * used. The actual scaffolding function below is |
| 517 | * called by the front controller based on whether the |
| 518 | * second segment of the URL matches the "secret" scaffolding |
| 519 | * word stored in the application/config/routes.php |
| 520 | * |
| 521 | * @access public |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 522 | * @param string |
| 523 | * @return void |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 524 | */ |
| 525 | function scaffolding($table = '') |
| 526 | { |
| 527 | if ($table === FALSE) |
| 528 | { |
| 529 | show_error('You must include the name of the table you would like access when you initialize scaffolding'); |
| 530 | } |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 531 | |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 532 | $CI =& get_instance(); |
| 533 | $CI->_ci_scaffolding = TRUE; |
| 534 | $CI->_ci_scaff_table = $table; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 535 | } |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 536 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 537 | // -------------------------------------------------------------------- |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 538 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 539 | /** |
| 540 | * Loader |
| 541 | * |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 542 | * This function is used to load views and files. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 543 | * |
| 544 | * @access private |
| 545 | * @param array |
| 546 | * @return void |
| 547 | */ |
| 548 | function _ci_load($data) |
admin | 212a3fa | 2006-09-23 04:22:39 +0000 | [diff] [blame] | 549 | { |
admin | 9139b0c | 2006-10-31 17:59:16 +0000 | [diff] [blame] | 550 | // Set the default data variables |
| 551 | foreach (array('view', 'vars', 'path', 'return') as $val) |
| 552 | { |
| 553 | $$val = ( ! isset($data[$val])) ? FALSE : $data[$val]; |
| 554 | } |
| 555 | |
| 556 | // Set the path to the requested file |
| 557 | if ($path == '') |
| 558 | { |
| 559 | $ext = pathinfo($view, PATHINFO_EXTENSION); |
| 560 | $file = ($ext == '') ? $view.EXT : $view; |
| 561 | $path = $this->_ci_view_path.$file; |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | $x = explode('/', $path); |
| 566 | $file = end($x); |
| 567 | } |
| 568 | |
| 569 | if ( ! file_exists($path)) |
| 570 | { |
| 571 | show_error('Unable to load the requested file: '.$file); |
| 572 | } |
| 573 | |
admin | b807f32 | 2006-10-20 04:55:37 +0000 | [diff] [blame] | 574 | // This allows anything loaded using $this->load (views, files, etc.) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 575 | // to become accessible from within the Controller and Model functions. |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 576 | // Only needed when running PHP 5 |
| 577 | |
admin | 1e30398 | 2006-10-10 16:35:43 +0000 | [diff] [blame] | 578 | if ($this->_ci_is_instance()) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 579 | { |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 580 | $CI =& get_instance(); |
| 581 | foreach (get_object_vars($CI) as $key => $var) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 582 | { |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 583 | if ( ! isset($this->$key)) |
| 584 | { |
| 585 | $this->$key =& $CI->$key; |
| 586 | } |
admin | e79dc71 | 2006-09-26 03:52:45 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 589 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 590 | /* |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 591 | * Extract and cache variables |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 592 | * |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 593 | * You can either set variables using the dedicated $this->load_vars() |
| 594 | * function or via the second parameter of this function. We'll merge |
| 595 | * the two types and cache them so that views that are embedded within |
admin | 212a3fa | 2006-09-23 04:22:39 +0000 | [diff] [blame] | 596 | * other views can have access to these variables. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 597 | */ |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 598 | if (is_array($vars)) |
| 599 | { |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 600 | $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $vars); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 601 | } |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 602 | extract($this->_ci_cached_vars); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 603 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 604 | /* |
| 605 | * Buffer the output |
| 606 | * |
| 607 | * We buffer the output for two reasons: |
| 608 | * 1. Speed. You get a significant speed boost. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 609 | * 2. So that the final rendered template can be |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 610 | * post-processed by the output class. Why do we |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 611 | * need post processing? For one thing, in order to |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 612 | * show the elapsed page load time. Unless we |
| 613 | * can intercept the content right before it's sent to |
admin | b807f32 | 2006-10-20 04:55:37 +0000 | [diff] [blame] | 614 | * the browser and then stop the timer it won't be accurate. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 615 | */ |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 616 | ob_start(); |
admin | 30578ea | 2006-10-17 02:31:06 +0000 | [diff] [blame] | 617 | |
| 618 | // If the PHP installation does not support short tags we'll |
| 619 | // do a little string replacement, changing the short tags |
| 620 | // to standard PHP echo statements. |
admin | 30578ea | 2006-10-17 02:31:06 +0000 | [diff] [blame] | 621 | |
admin | 49439ff | 2006-10-30 04:39:12 +0000 | [diff] [blame] | 622 | if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE) |
admin | 81a3681 | 2006-10-17 03:40:51 +0000 | [diff] [blame] | 623 | { |
| 624 | echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($path))).'<?php '); |
admin | 30578ea | 2006-10-17 02:31:06 +0000 | [diff] [blame] | 625 | } |
| 626 | else |
| 627 | { |
| 628 | include($path); |
| 629 | } |
| 630 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 631 | log_message('debug', 'File loaded: '.$path); |
| 632 | |
admin | 49439ff | 2006-10-30 04:39:12 +0000 | [diff] [blame] | 633 | // Return the file data if requested |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 634 | if ($return === TRUE) |
| 635 | { |
| 636 | $buffer = ob_get_contents(); |
admin | be013b3 | 2006-11-04 05:07:03 +0000 | [diff] [blame] | 637 | @ob_end_clean(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 638 | return $buffer; |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * Flush the buffer... or buff the flusher? |
| 643 | * |
admin | 212a3fa | 2006-09-23 04:22:39 +0000 | [diff] [blame] | 644 | * In order to permit views to be nested within |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 645 | * other views, we need to flush the content back out whenever |
| 646 | * we are beyond the first level of output buffering so that |
| 647 | * it can be seen and included properly by the first included |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 648 | * template and any subsequent ones. Oy! |
| 649 | * |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 650 | */ |
| 651 | if (ob_get_level() > $this->_ci_ob_level + 1) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 652 | { |
| 653 | ob_end_flush(); |
| 654 | } |
| 655 | else |
| 656 | { |
admin | 34f7f2d | 2006-10-10 08:06:42 +0000 | [diff] [blame] | 657 | // PHP 4 requires that we use a global |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 658 | global $OUT; |
| 659 | $OUT->set_output(ob_get_contents()); |
admin | be013b3 | 2006-11-04 05:07:03 +0000 | [diff] [blame] | 660 | @ob_end_clean(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 661 | } |
| 662 | } |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 663 | |
| 664 | // -------------------------------------------------------------------- |
| 665 | |
| 666 | /** |
| 667 | * Load class |
| 668 | * |
| 669 | * This function loads the requested class. |
| 670 | * |
| 671 | * @access private |
| 672 | * @param string the item that is being loaded |
| 673 | * @param mixed any additional parameters |
| 674 | * @return void |
| 675 | */ |
admin | 5a14ea1 | 2006-10-12 20:42:55 +0000 | [diff] [blame] | 676 | function _ci_load_class($class, $params = NULL) |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 677 | { |
admin | be013b3 | 2006-11-04 05:07:03 +0000 | [diff] [blame] | 678 | // Get the class name |
| 679 | $class = str_replace(EXT, '', $class); |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 680 | |
admin | be013b3 | 2006-11-04 05:07:03 +0000 | [diff] [blame] | 681 | // We'll test for both lowercase and capitalized versions of the file name |
| 682 | foreach (array(ucfirst($class), strtolower($class)) as $class) |
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 683 | { |
admin | be013b3 | 2006-11-04 05:07:03 +0000 | [diff] [blame] | 684 | // Is this a class extension request? |
| 685 | if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT)) |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 686 | { |
admin | be013b3 | 2006-11-04 05:07:03 +0000 | [diff] [blame] | 687 | if ( ! file_exists(BASEPATH.'libraries/'.ucfirst($class).EXT)) |
| 688 | { |
| 689 | log_message('error', "Unable to load the requested class: ".$class); |
| 690 | show_error("Unable to load the requested class: ".$class); |
| 691 | } |
| 692 | |
| 693 | include(BASEPATH.'libraries/'.ucfirst($class).EXT); |
| 694 | include(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT); |
| 695 | |
| 696 | return $this->_ci_init_class($class, config_item('subclass_prefix'), $params); |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 697 | } |
admin | be013b3 | 2006-11-04 05:07:03 +0000 | [diff] [blame] | 698 | |
| 699 | // Lets search for the requested library file and load it. |
| 700 | $is_duplicate = FALSE; |
| 701 | for ($i = 1; $i < 3; $i++) |
admin | 69d3984 | 2006-10-31 18:01:00 +0000 | [diff] [blame] | 702 | { |
admin | be013b3 | 2006-11-04 05:07:03 +0000 | [diff] [blame] | 703 | $path = ($i % 2) ? APPPATH : BASEPATH; |
| 704 | $fp = $path.'libraries/'.$class.EXT; |
| 705 | |
| 706 | // Does the file exist? No? Bummer... |
| 707 | if ( ! file_exists($fp)) |
| 708 | { |
| 709 | continue; |
| 710 | } |
| 711 | |
| 712 | // Safety: Was the class already loaded by a previous call? |
| 713 | if (in_array($fp, $this->_ci_classes)) |
| 714 | { |
| 715 | $is_duplicate = TRUE; |
| 716 | continue; |
| 717 | } |
| 718 | |
| 719 | include($fp); |
| 720 | $this->_ci_classes[] = $fp; |
| 721 | return $this->_ci_init_class($class, '', $params); |
admin | 69d3984 | 2006-10-31 18:01:00 +0000 | [diff] [blame] | 722 | } |
admin | be013b3 | 2006-11-04 05:07:03 +0000 | [diff] [blame] | 723 | } // END FOREACH |
| 724 | |
admin | 9139b0c | 2006-10-31 17:59:16 +0000 | [diff] [blame] | 725 | // If we got this far we were unable to find the requested class. |
| 726 | // We do not issue errors if the load call failed due to a duplicate request |
| 727 | if ($is_duplicate == FALSE) |
| 728 | { |
| 729 | log_message('error', "Unable to load the requested class: ".$class); |
| 730 | show_error("Unable to load the requested class: ".$class); |
| 731 | } |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | // -------------------------------------------------------------------- |
| 735 | |
| 736 | /** |
| 737 | * Instantiates a class |
| 738 | * |
| 739 | * @access private |
| 740 | * @param string |
| 741 | * @param string |
| 742 | * @return null |
| 743 | */ |
admin | 30578ea | 2006-10-17 02:31:06 +0000 | [diff] [blame] | 744 | function _ci_init_class($class, $prefix = '', $config = FALSE) |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 745 | { |
| 746 | // Is there an associated config file for this class? |
admin | ca3dafb | 2006-10-23 01:24:11 +0000 | [diff] [blame] | 747 | if ($config === NULL) |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 748 | { |
admin | ca3dafb | 2006-10-23 01:24:11 +0000 | [diff] [blame] | 749 | $config = NULL; |
admin | 80b2bd9 | 2006-10-12 18:30:36 +0000 | [diff] [blame] | 750 | if (file_exists(APPPATH.'config/'.$class.EXT)) |
| 751 | { |
admin | f3a6204 | 2006-10-29 20:24:11 +0000 | [diff] [blame] | 752 | include(APPPATH.'config/'.$class.EXT); |
admin | 80b2bd9 | 2006-10-12 18:30:36 +0000 | [diff] [blame] | 753 | } |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | if ($prefix == '') |
| 757 | { |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 758 | $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class; |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 759 | } |
| 760 | else |
| 761 | { |
| 762 | $name = $prefix.$class; |
| 763 | } |
admin | 2f0bac8 | 2006-10-09 17:36:45 +0000 | [diff] [blame] | 764 | |
admin | b1fddc0 | 2006-10-09 21:29:07 +0000 | [diff] [blame] | 765 | // Set the variable name we will assign the class to |
admin | 80b2bd9 | 2006-10-12 18:30:36 +0000 | [diff] [blame] | 766 | $class = strtolower($class); |
| 767 | $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class]; |
| 768 | |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 769 | // Instantiate the class |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 770 | $CI =& get_instance(); |
| 771 | if ($config !== NULL) |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 772 | { |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 773 | $CI->$classvar = new $name($config); |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 774 | } |
| 775 | else |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 776 | { |
| 777 | $CI->$classvar = new $name; |
| 778 | } |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 779 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 780 | |
| 781 | // -------------------------------------------------------------------- |
| 782 | |
| 783 | /** |
| 784 | * Autoloader |
| 785 | * |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 786 | * The config/autoload.php file contains an array that permits sub-systems, |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 787 | * libraries, plugins, and helpers to be loaded automatically. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 788 | * |
| 789 | * @access private |
| 790 | * @param array |
| 791 | * @return void |
| 792 | */ |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 793 | function _ci_autoloader() |
| 794 | { |
admin | f3a6204 | 2006-10-29 20:24:11 +0000 | [diff] [blame] | 795 | include(APPPATH.'config/autoload'.EXT); |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 796 | |
| 797 | if ( ! isset($autoload)) |
| 798 | { |
| 799 | return FALSE; |
| 800 | } |
| 801 | |
| 802 | // Load any custome config file |
| 803 | if (count($autoload['config']) > 0) |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 804 | { |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 805 | $CI =& get_instance(); |
| 806 | foreach ($autoload['config'] as $key => $val) |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 807 | { |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 808 | $CI->config->load($val); |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
| 812 | // Load plugins, helpers, and scripts |
| 813 | foreach (array('helper', 'plugin', 'script') as $type) |
| 814 | { |
| 815 | if (isset($autoload[$type]) AND count($autoload[$type]) > 0) |
| 816 | { |
| 817 | $this->$type($autoload[$type]); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | // A little tweak to remain backward compatible |
| 822 | // The $autoload['core'] item was deprecated |
| 823 | if ( ! isset($autoload['libraries'])) |
| 824 | { |
| 825 | $autoload['libraries'] = $autoload['core']; |
| 826 | } |
| 827 | |
| 828 | // Load libraries |
| 829 | if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0) |
| 830 | { |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 831 | // Load the database driver. |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 832 | if (in_array('database', $autoload['libraries'])) |
| 833 | { |
| 834 | $this->database(); |
| 835 | $autoload['libraries'] = array_diff($autoload['libraries'], array('database')); |
| 836 | } |
| 837 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 838 | // Load the model class. |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 839 | if (in_array('model', $autoload['libraries'])) |
| 840 | { |
| 841 | $this->model(); |
| 842 | $autoload['libraries'] = array_diff($autoload['libraries'], array('model')); |
| 843 | } |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 844 | |
| 845 | // Load scaffolding |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 846 | if (in_array('scaffolding', $autoload['libraries'])) |
| 847 | { |
| 848 | $this->scaffolding(); |
| 849 | $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding')); |
| 850 | } |
| 851 | |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 852 | // Load all other libraries |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 853 | foreach ($autoload['libraries'] as $item) |
| 854 | { |
| 855 | $this->library($item); |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | // -------------------------------------------------------------------- |
| 861 | |
| 862 | /** |
| 863 | * Assign to Models |
| 864 | * |
| 865 | * Makes sure that anything loaded by the loader class (libraries, plugins, etc.) |
admin | bd6bee7 | 2006-10-21 19:39:00 +0000 | [diff] [blame] | 866 | * will be available to models, if any exist. |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 867 | * |
admin | 34f7f2d | 2006-10-10 08:06:42 +0000 | [diff] [blame] | 868 | * @access private |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 869 | * @param object |
| 870 | * @return array |
| 871 | */ |
| 872 | function _ci_assign_to_models() |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 873 | { |
admin | 2bc1385 | 2006-10-24 23:16:17 +0000 | [diff] [blame] | 874 | if (count($this->_ci_models) == 0) |
| 875 | { |
| 876 | return; |
| 877 | } |
| 878 | |
admin | 1e30398 | 2006-10-10 16:35:43 +0000 | [diff] [blame] | 879 | if ($this->_ci_is_instance()) |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 880 | { |
| 881 | $CI =& get_instance(); |
| 882 | foreach ($this->_ci_models as $model) |
| 883 | { |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 884 | $CI->$model->_assign_libraries(); |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | else |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 888 | { |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 889 | foreach ($this->_ci_models as $model) |
| 890 | { |
admin | 0aef222 | 2006-10-12 18:00:22 +0000 | [diff] [blame] | 891 | $this->$model->_assign_libraries(); |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 892 | } |
| 893 | } |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 894 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 895 | |
| 896 | // -------------------------------------------------------------------- |
| 897 | |
| 898 | /** |
| 899 | * Object to Array |
| 900 | * |
admin | bd6bee7 | 2006-10-21 19:39:00 +0000 | [diff] [blame] | 901 | * Takes an object as input and converts the class variables to array key/vals |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 902 | * |
admin | 34f7f2d | 2006-10-10 08:06:42 +0000 | [diff] [blame] | 903 | * @access private |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 904 | * @param object |
| 905 | * @return array |
| 906 | */ |
| 907 | function _ci_object_to_array($object) |
| 908 | { |
admin | 2e5872a | 2006-09-06 02:32:55 +0000 | [diff] [blame] | 909 | return (is_object($object)) ? get_object_vars($object) : $object; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 910 | } |
admin | 8f0a8f6 | 2006-10-07 01:17:25 +0000 | [diff] [blame] | 911 | |
admin | 34f7f2d | 2006-10-10 08:06:42 +0000 | [diff] [blame] | 912 | // -------------------------------------------------------------------- |
| 913 | |
| 914 | /** |
| 915 | * Determines whether we should use the CI instance or $this |
| 916 | * |
| 917 | * @access private |
| 918 | * @return bool |
| 919 | */ |
admin | 1e30398 | 2006-10-10 16:35:43 +0000 | [diff] [blame] | 920 | function _ci_is_instance() |
admin | 34f7f2d | 2006-10-10 08:06:42 +0000 | [diff] [blame] | 921 | { |
| 922 | if ($this->_ci_is_php5 == TRUE) |
| 923 | { |
| 924 | return TRUE; |
| 925 | } |
| 926 | |
| 927 | global $CI; |
| 928 | return (is_object($CI)) ? TRUE : FALSE; |
| 929 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 930 | |
| 931 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 932 | ?> |