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