Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 2 | /**
|
| 3 | * CodeIgniter
|
| 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Rick Ellis | e4a7db4 | 2008-09-12 23:35:09 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 25 | * @author ExpressionEngine Dev Team
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 26 | * @category Loader
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 27 | * @link http://codeigniter.com/user_guide/libraries/loader.html
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 28 | */
|
| 29 | class CI_Loader {
|
| 30 |
|
| 31 | // All these are set automatically. Don't mess with them.
|
| 32 | var $_ci_ob_level;
|
| 33 | var $_ci_view_path = '';
|
| 34 | var $_ci_is_php5 = FALSE;
|
| 35 | var $_ci_is_instance = FALSE; // Whether we should use $this or $CI =& get_instance()
|
| 36 | var $_ci_cached_vars = array();
|
| 37 | var $_ci_classes = array();
|
Rick Ellis | 7cdef03 | 2008-08-26 18:44:54 +0000 | [diff] [blame] | 38 | var $_ci_loaded_files = array();
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 39 | var $_ci_models = array();
|
| 40 | var $_ci_helpers = array();
|
| 41 | var $_ci_plugins = array();
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 42 | var $_ci_varmap = array('unit_test' => 'unit', 'user_agent' => 'agent');
|
| 43 |
|
| 44 |
|
| 45 | /**
|
| 46 | * Constructor
|
| 47 | *
|
| 48 | * Sets the path to the view files and gets the initial output buffering level
|
| 49 | *
|
| 50 | * @access public
|
| 51 | */
|
| 52 | function CI_Loader()
|
| 53 | {
|
| 54 | $this->_ci_is_php5 = (floor(phpversion()) >= 5) ? TRUE : FALSE;
|
| 55 | $this->_ci_view_path = APPPATH.'views/';
|
| 56 | $this->_ci_ob_level = ob_get_level();
|
| 57 |
|
| 58 | log_message('debug', "Loader Class Initialized");
|
| 59 | }
|
| 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
|
| 71 | * @param mixed the optional parameters
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 72 | * @param string an optional object name
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 73 | * @return void
|
| 74 | */
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 75 | function library($library = '', $params = NULL, $object_name = NULL)
|
Rick Ellis | 76c504e | 2008-08-26 19:11:34 +0000 | [diff] [blame] | 76 | {
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 77 | if ($library == '')
|
| 78 | {
|
| 79 | return FALSE;
|
| 80 | }
|
| 81 |
|
Rick Ellis | 65f4606 | 2008-08-21 18:55:09 +0000 | [diff] [blame] | 82 | if ( ! is_null($params) AND ! is_array($params))
|
| 83 | {
|
| 84 | $params = NULL;
|
| 85 | }
|
| 86 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 87 | if (is_array($library))
|
| 88 | {
|
| 89 | foreach ($library as $class)
|
| 90 | {
|
Rick Ellis | 6945097 | 2008-08-26 18:51:19 +0000 | [diff] [blame] | 91 | $this->_ci_load_class($class, $params, $object_name);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 92 | }
|
| 93 | }
|
| 94 | else
|
| 95 | {
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 96 | $this->_ci_load_class($library, $params, $object_name);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 97 | }
|
| 98 |
|
| 99 | $this->_ci_assign_to_models();
|
| 100 | }
|
| 101 |
|
| 102 | // --------------------------------------------------------------------
|
| 103 |
|
| 104 | /**
|
| 105 | * Model Loader
|
| 106 | *
|
| 107 | * This function lets users load and instantiate models.
|
| 108 | *
|
Rick Ellis | 76c504e | 2008-08-26 19:11:34 +0000 | [diff] [blame] | 109 | * @access public
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 110 | * @param string the name of the class
|
Derek Allard | 9736d3f | 2008-06-16 21:36:01 +0000 | [diff] [blame] | 111 | * @param string name for the model
|
| 112 | * @param bool database connection
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 113 | * @return void
|
| 114 | */
|
| 115 | function model($model, $name = '', $db_conn = FALSE)
|
| 116 | {
|
| 117 | if (is_array($model))
|
| 118 | {
|
| 119 | foreach($model as $babe)
|
| 120 | {
|
| 121 | $this->model($babe);
|
| 122 | }
|
| 123 | return;
|
| 124 | }
|
| 125 |
|
| 126 | if ($model == '')
|
| 127 | {
|
| 128 | return;
|
| 129 | }
|
| 130 |
|
| 131 | // Is the model in a sub-folder? If so, parse out the filename and path.
|
| 132 | if (strpos($model, '/') === FALSE)
|
| 133 | {
|
| 134 | $path = '';
|
| 135 | }
|
| 136 | else
|
| 137 | {
|
| 138 | $x = explode('/', $model);
|
| 139 | $model = end($x);
|
| 140 | unset($x[count($x)-1]);
|
| 141 | $path = implode('/', $x).'/';
|
| 142 | }
|
| 143 |
|
| 144 | if ($name == '')
|
| 145 | {
|
| 146 | $name = $model;
|
| 147 | }
|
| 148 |
|
| 149 | if (in_array($name, $this->_ci_models, TRUE))
|
| 150 | {
|
| 151 | return;
|
| 152 | }
|
| 153 |
|
| 154 | $CI =& get_instance();
|
| 155 | if (isset($CI->$name))
|
| 156 | {
|
| 157 | show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
|
| 158 | }
|
| 159 |
|
| 160 | $model = strtolower($model);
|
| 161 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 162 | if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 163 | {
|
| 164 | show_error('Unable to locate the model you have specified: '.$model);
|
| 165 | }
|
| 166 |
|
| 167 | if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
|
| 168 | {
|
| 169 | if ($db_conn === TRUE)
|
| 170 | $db_conn = '';
|
| 171 |
|
| 172 | $CI->load->database($db_conn, FALSE, TRUE);
|
| 173 | }
|
| 174 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 175 | if ( ! class_exists('Model'))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 176 | {
|
Derek Allard | ff943eb | 2008-01-22 18:08:10 +0000 | [diff] [blame] | 177 | load_class('Model', FALSE);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 178 | }
|
| 179 |
|
| 180 | require_once(APPPATH.'models/'.$path.$model.EXT);
|
| 181 |
|
| 182 | $model = ucfirst($model);
|
| 183 |
|
| 184 | $CI->$name = new $model();
|
| 185 | $CI->$name->_assign_libraries();
|
| 186 |
|
| 187 | $this->_ci_models[] = $name;
|
| 188 | }
|
| 189 |
|
| 190 | // --------------------------------------------------------------------
|
| 191 |
|
| 192 | /**
|
| 193 | * Database Loader
|
| 194 | *
|
| 195 | * @access public
|
| 196 | * @param string the DB credentials
|
| 197 | * @param bool whether to return the DB object
|
| 198 | * @param bool whether to enable active record (this allows us to override the config setting)
|
| 199 | * @return object
|
| 200 | */
|
| 201 | function database($params = '', $return = FALSE, $active_record = FALSE)
|
| 202 | {
|
Derek Jones | 72d6133 | 2008-01-30 20:52:22 +0000 | [diff] [blame] | 203 | // Grab the super object
|
| 204 | $CI =& get_instance();
|
| 205 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 206 | // Do we even need to load the database class?
|
Derek Jones | 72d6133 | 2008-01-30 20:52:22 +0000 | [diff] [blame] | 207 | if (class_exists('CI_DB') AND $return == FALSE AND $active_record == FALSE AND isset($CI->db) AND is_object($CI->db))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 208 | {
|
| 209 | return FALSE;
|
| 210 | }
|
| 211 |
|
| 212 | require_once(BASEPATH.'database/DB'.EXT);
|
| 213 |
|
| 214 | if ($return === TRUE)
|
| 215 | {
|
| 216 | return DB($params, $active_record);
|
| 217 | }
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 218 |
|
| 219 | // Initialize the db variable. Needed to prevent
|
| 220 | // reference errors with some configurations
|
| 221 | $CI->db = '';
|
| 222 |
|
| 223 | // Load the DB class
|
| 224 | $CI->db =& DB($params, $active_record);
|
| 225 |
|
| 226 | // Assign the DB object to any existing models
|
| 227 | $this->_ci_assign_to_models();
|
| 228 | }
|
| 229 |
|
| 230 | // --------------------------------------------------------------------
|
| 231 |
|
| 232 | /**
|
| 233 | * Load the Utilities Class
|
| 234 | *
|
| 235 | * @access public
|
| 236 | * @return string
|
| 237 | */
|
| 238 | function dbutil()
|
| 239 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 240 | if ( ! class_exists('CI_DB'))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 241 | {
|
| 242 | $this->database();
|
| 243 | }
|
| 244 |
|
| 245 | $CI =& get_instance();
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 246 |
|
| 247 | // for backwards compatibility, load dbforge so we can extend dbutils off it
|
| 248 | // this use is deprecated and strongly discouraged
|
| 249 | $CI->load->dbforge();
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 250 |
|
| 251 | require_once(BASEPATH.'database/DB_utility'.EXT);
|
| 252 | require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT);
|
| 253 | $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
|
| 254 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 255 | $CI->dbutil =& new $class();
|
| 256 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 257 | $CI->load->_ci_assign_to_models();
|
| 258 | }
|
| 259 |
|
| 260 | // --------------------------------------------------------------------
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 261 |
|
| 262 | /**
|
| 263 | * Load the Database Forge Class
|
| 264 | *
|
| 265 | * @access public
|
| 266 | * @return string
|
| 267 | */
|
| 268 | function dbforge()
|
| 269 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 270 | if ( ! class_exists('CI_DB'))
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 271 | {
|
| 272 | $this->database();
|
| 273 | }
|
| 274 |
|
| 275 | $CI =& get_instance();
|
| 276 |
|
| 277 | require_once(BASEPATH.'database/DB_forge'.EXT);
|
| 278 | require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge'.EXT);
|
| 279 | $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
|
| 280 |
|
| 281 | $CI->dbforge = new $class();
|
Derek Allard | ff390bd | 2008-05-06 00:37:12 +0000 | [diff] [blame] | 282 |
|
| 283 | $CI->load->_ci_assign_to_models();
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 284 | }
|
| 285 |
|
| 286 | // --------------------------------------------------------------------
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 287 |
|
| 288 | /**
|
| 289 | * Load View
|
| 290 | *
|
| 291 | * This function is used to load a "view" file. It has three parameters:
|
| 292 | *
|
| 293 | * 1. The name of the "view" file to be included.
|
| 294 | * 2. An associative array of data to be extracted for use in the view.
|
| 295 | * 3. TRUE/FALSE - whether to return the data or load it. In
|
| 296 | * some cases it's advantageous to be able to return data so that
|
| 297 | * a developer can process it in some way.
|
| 298 | *
|
| 299 | * @access public
|
| 300 | * @param string
|
| 301 | * @param array
|
| 302 | * @param bool
|
| 303 | * @return void
|
| 304 | */
|
| 305 | function view($view, $vars = array(), $return = FALSE)
|
| 306 | {
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 307 | return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 308 | }
|
| 309 |
|
| 310 | // --------------------------------------------------------------------
|
| 311 |
|
| 312 | /**
|
| 313 | * Load File
|
| 314 | *
|
| 315 | * This is a generic file loader
|
| 316 | *
|
| 317 | * @access public
|
| 318 | * @param string
|
| 319 | * @param bool
|
| 320 | * @return string
|
| 321 | */
|
| 322 | function file($path, $return = FALSE)
|
| 323 | {
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 324 | return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 325 | }
|
| 326 |
|
| 327 | // --------------------------------------------------------------------
|
| 328 |
|
| 329 | /**
|
| 330 | * Set Variables
|
| 331 | *
|
| 332 | * Once variables are set they become available within
|
| 333 | * the controller class and its "view" files.
|
| 334 | *
|
| 335 | * @access public
|
| 336 | * @param array
|
| 337 | * @return void
|
| 338 | */
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 339 | function vars($vars = array(), $val = '')
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 340 | {
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 341 | if ($val != '' AND is_string($vars))
|
| 342 | {
|
| 343 | $vars = array($vars => $val);
|
| 344 | }
|
| 345 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 346 | $vars = $this->_ci_object_to_array($vars);
|
| 347 |
|
| 348 | if (is_array($vars) AND count($vars) > 0)
|
| 349 | {
|
| 350 | foreach ($vars as $key => $val)
|
| 351 | {
|
| 352 | $this->_ci_cached_vars[$key] = $val;
|
| 353 | }
|
| 354 | }
|
| 355 | }
|
| 356 |
|
| 357 | // --------------------------------------------------------------------
|
| 358 |
|
| 359 | /**
|
| 360 | * Load Helper
|
| 361 | *
|
| 362 | * This function loads the specified helper file.
|
| 363 | *
|
| 364 | * @access public
|
| 365 | * @param mixed
|
| 366 | * @return void
|
| 367 | */
|
| 368 | function helper($helpers = array())
|
| 369 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 370 | if ( ! is_array($helpers))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 371 | {
|
| 372 | $helpers = array($helpers);
|
| 373 | }
|
| 374 |
|
| 375 | foreach ($helpers as $helper)
|
| 376 | {
|
| 377 | $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
|
Rick Ellis | 76c504e | 2008-08-26 19:11:34 +0000 | [diff] [blame] | 378 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 379 | if (isset($this->_ci_helpers[$helper]))
|
| 380 | {
|
| 381 | continue;
|
| 382 | }
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 383 |
|
| 384 | $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.EXT;
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 385 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 386 | // Is this a helper extension request?
|
| 387 | if (file_exists($ext_helper))
|
| 388 | {
|
| 389 | $base_helper = BASEPATH.'helpers/'.$helper.EXT;
|
| 390 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 391 | if ( ! file_exists($base_helper))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 392 | {
|
| 393 | show_error('Unable to load the requested file: helpers/'.$helper.EXT);
|
| 394 | }
|
| 395 |
|
| 396 | include_once($ext_helper);
|
| 397 | include_once($base_helper);
|
| 398 | }
|
| 399 | elseif (file_exists(APPPATH.'helpers/'.$helper.EXT))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 400 | {
|
| 401 | include_once(APPPATH.'helpers/'.$helper.EXT);
|
| 402 | }
|
| 403 | else
|
| 404 | {
|
| 405 | if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
|
| 406 | {
|
Derek Allard | d888c35 | 2008-03-18 04:04:33 +0000 | [diff] [blame] | 407 | include_once(BASEPATH.'helpers/'.$helper.EXT);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 408 | }
|
| 409 | else
|
| 410 | {
|
| 411 | show_error('Unable to load the requested file: helpers/'.$helper.EXT);
|
| 412 | }
|
| 413 | }
|
| 414 |
|
| 415 | $this->_ci_helpers[$helper] = TRUE;
|
Rick Ellis | 76c504e | 2008-08-26 19:11:34 +0000 | [diff] [blame] | 416 | log_message('debug', 'Helper loaded: '.$helper);
|
| 417 | }
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 418 | }
|
| 419 |
|
| 420 | // --------------------------------------------------------------------
|
| 421 |
|
| 422 | /**
|
| 423 | * Load Helpers
|
| 424 | *
|
| 425 | * This is simply an alias to the above function in case the
|
| 426 | * user has written the plural form of this function.
|
| 427 | *
|
| 428 | * @access public
|
| 429 | * @param array
|
| 430 | * @return void
|
| 431 | */
|
| 432 | function helpers($helpers = array())
|
| 433 | {
|
| 434 | $this->helper($helpers);
|
| 435 | }
|
| 436 |
|
| 437 | // --------------------------------------------------------------------
|
| 438 |
|
| 439 | /**
|
| 440 | * Load Plugin
|
| 441 | *
|
| 442 | * This function loads the specified plugin.
|
| 443 | *
|
| 444 | * @access public
|
| 445 | * @param array
|
| 446 | * @return void
|
| 447 | */
|
| 448 | function plugin($plugins = array())
|
| 449 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 450 | if ( ! is_array($plugins))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 451 | {
|
| 452 | $plugins = array($plugins);
|
| 453 | }
|
| 454 |
|
| 455 | foreach ($plugins as $plugin)
|
| 456 | {
|
Derek Allard | 17f7406 | 2008-01-16 01:22:44 +0000 | [diff] [blame] | 457 | $plugin = strtolower(str_replace(EXT, '', str_replace('_pi', '', $plugin)).'_pi');
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 458 |
|
| 459 | if (isset($this->_ci_plugins[$plugin]))
|
| 460 | {
|
| 461 | continue;
|
| 462 | }
|
| 463 |
|
| 464 | if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
|
| 465 | {
|
Derek Allard | d888c35 | 2008-03-18 04:04:33 +0000 | [diff] [blame] | 466 | include_once(APPPATH.'plugins/'.$plugin.EXT);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 467 | }
|
| 468 | else
|
| 469 | {
|
| 470 | if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
|
| 471 | {
|
Derek Allard | d888c35 | 2008-03-18 04:04:33 +0000 | [diff] [blame] | 472 | include_once(BASEPATH.'plugins/'.$plugin.EXT);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 473 | }
|
| 474 | else
|
| 475 | {
|
| 476 | show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
|
| 477 | }
|
| 478 | }
|
| 479 |
|
| 480 | $this->_ci_plugins[$plugin] = TRUE;
|
Rick Ellis | 76c504e | 2008-08-26 19:11:34 +0000 | [diff] [blame] | 481 | log_message('debug', 'Plugin loaded: '.$plugin);
|
| 482 | }
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 483 | }
|
| 484 |
|
| 485 | // --------------------------------------------------------------------
|
| 486 |
|
| 487 | /**
|
| 488 | * Load Plugins
|
| 489 | *
|
| 490 | * This is simply an alias to the above function in case the
|
| 491 | * user has written the plural form of this function.
|
| 492 | *
|
| 493 | * @access public
|
| 494 | * @param array
|
| 495 | * @return void
|
| 496 | */
|
| 497 | function plugins($plugins = array())
|
| 498 | {
|
| 499 | $this->plugin($plugins);
|
| 500 | }
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 501 |
|
| 502 | // --------------------------------------------------------------------
|
| 503 |
|
| 504 | /**
|
| 505 | * Loads a language file
|
| 506 | *
|
| 507 | * @access public
|
| 508 | * @param array
|
| 509 | * @param string
|
| 510 | * @return void
|
| 511 | */
|
| 512 | function language($file = array(), $lang = '')
|
| 513 | {
|
| 514 | $CI =& get_instance();
|
| 515 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 516 | if ( ! is_array($file))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 517 | {
|
| 518 | $file = array($file);
|
| 519 | }
|
| 520 |
|
| 521 | foreach ($file as $langfile)
|
| 522 | {
|
| 523 | $CI->lang->load($langfile, $lang);
|
| 524 | }
|
| 525 | }
|
| 526 |
|
| 527 | /**
|
| 528 | * Loads language files for scaffolding
|
| 529 | *
|
| 530 | * @access public
|
| 531 | * @param string
|
| 532 | * @return arra
|
| 533 | */
|
| 534 | function scaffold_language($file = '', $lang = '', $return = FALSE)
|
| 535 | {
|
| 536 | $CI =& get_instance();
|
| 537 | return $CI->lang->load($file, $lang, $return);
|
| 538 | }
|
| 539 |
|
| 540 | // --------------------------------------------------------------------
|
| 541 |
|
| 542 | /**
|
| 543 | * Loads a config file
|
| 544 | *
|
| 545 | * @access public
|
| 546 | * @param string
|
| 547 | * @return void
|
| 548 | */
|
| 549 | function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
|
| 550 | {
|
| 551 | $CI =& get_instance();
|
| 552 | $CI->config->load($file, $use_sections, $fail_gracefully);
|
| 553 | }
|
| 554 |
|
| 555 | // --------------------------------------------------------------------
|
| 556 |
|
| 557 | /**
|
| 558 | * Scaffolding Loader
|
| 559 | *
|
| 560 | * This initializing function works a bit different than the
|
| 561 | * others. It doesn't load the class. Instead, it simply
|
| 562 | * sets a flag indicating that scaffolding is allowed to be
|
| 563 | * used. The actual scaffolding function below is
|
| 564 | * called by the front controller based on whether the
|
| 565 | * second segment of the URL matches the "secret" scaffolding
|
| 566 | * word stored in the application/config/routes.php
|
| 567 | *
|
| 568 | * @access public
|
| 569 | * @param string
|
| 570 | * @return void
|
| 571 | */
|
| 572 | function scaffolding($table = '')
|
| 573 | {
|
| 574 | if ($table === FALSE)
|
| 575 | {
|
| 576 | show_error('You must include the name of the table you would like to access when you initialize scaffolding');
|
| 577 | }
|
| 578 |
|
| 579 | $CI =& get_instance();
|
| 580 | $CI->_ci_scaffolding = TRUE;
|
| 581 | $CI->_ci_scaff_table = $table;
|
| 582 | }
|
| 583 |
|
| 584 | // --------------------------------------------------------------------
|
| 585 |
|
| 586 | /**
|
| 587 | * Loader
|
| 588 | *
|
| 589 | * This function is used to load views and files.
|
Derek Jones | ca0e7fa | 2008-01-22 16:42:13 +0000 | [diff] [blame] | 590 | * Variables are prefixed with _ci_ to avoid symbol collision with
|
| 591 | * variables made available to view files
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 592 | *
|
| 593 | * @access private
|
| 594 | * @param array
|
| 595 | * @return void
|
| 596 | */
|
Derek Jones | ca0e7fa | 2008-01-22 16:42:13 +0000 | [diff] [blame] | 597 | function _ci_load($_ci_data)
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 598 | {
|
| 599 | // Set the default data variables
|
Derek Jones | ca0e7fa | 2008-01-22 16:42:13 +0000 | [diff] [blame] | 600 | foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 601 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 602 | $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 603 | }
|
| 604 |
|
| 605 | // Set the path to the requested file
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 606 | if ($_ci_path == '')
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 607 | {
|
Derek Jones | ca0e7fa | 2008-01-22 16:42:13 +0000 | [diff] [blame] | 608 | $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
|
| 609 | $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view;
|
| 610 | $_ci_path = $this->_ci_view_path.$_ci_file;
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 611 | }
|
| 612 | else
|
| 613 | {
|
Derek Jones | ca0e7fa | 2008-01-22 16:42:13 +0000 | [diff] [blame] | 614 | $_ci_x = explode('/', $_ci_path);
|
| 615 | $_ci_file = end($_ci_x);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 616 | }
|
| 617 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 618 | if ( ! file_exists($_ci_path))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 619 | {
|
Derek Jones | ca0e7fa | 2008-01-22 16:42:13 +0000 | [diff] [blame] | 620 | show_error('Unable to load the requested file: '.$_ci_file);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 621 | }
|
| 622 |
|
| 623 | // This allows anything loaded using $this->load (views, files, etc.)
|
| 624 | // to become accessible from within the Controller and Model functions.
|
| 625 | // Only needed when running PHP 5
|
| 626 |
|
| 627 | if ($this->_ci_is_instance())
|
| 628 | {
|
Derek Jones | ca0e7fa | 2008-01-22 16:42:13 +0000 | [diff] [blame] | 629 | $_ci_CI =& get_instance();
|
| 630 | foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 631 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 632 | if ( ! isset($this->$_ci_key))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 633 | {
|
Derek Jones | ca0e7fa | 2008-01-22 16:42:13 +0000 | [diff] [blame] | 634 | $this->$_ci_key =& $_ci_CI->$_ci_key;
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 635 | }
|
| 636 | }
|
| 637 | }
|
Derek Jones | ca0e7fa | 2008-01-22 16:42:13 +0000 | [diff] [blame] | 638 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 639 | /*
|
| 640 | * Extract and cache variables
|
| 641 | *
|
| 642 | * You can either set variables using the dedicated $this->load_vars()
|
| 643 | * function or via the second parameter of this function. We'll merge
|
| 644 | * the two types and cache them so that views that are embedded within
|
| 645 | * other views can have access to these variables.
|
| 646 | */
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 647 | if (is_array($_ci_vars))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 648 | {
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 649 | $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 650 | }
|
| 651 | extract($this->_ci_cached_vars);
|
| 652 |
|
| 653 | /*
|
| 654 | * Buffer the output
|
| 655 | *
|
| 656 | * We buffer the output for two reasons:
|
| 657 | * 1. Speed. You get a significant speed boost.
|
| 658 | * 2. So that the final rendered template can be
|
| 659 | * post-processed by the output class. Why do we
|
| 660 | * need post processing? For one thing, in order to
|
| 661 | * show the elapsed page load time. Unless we
|
| 662 | * can intercept the content right before it's sent to
|
| 663 | * the browser and then stop the timer it won't be accurate.
|
| 664 | */
|
| 665 | ob_start();
|
| 666 |
|
| 667 | // If the PHP installation does not support short tags we'll
|
| 668 | // do a little string replacement, changing the short tags
|
| 669 | // to standard PHP echo statements.
|
| 670 |
|
| 671 | if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
|
| 672 | {
|
Derek Jones | 454fa7e | 2008-05-13 22:26:09 +0000 | [diff] [blame] | 673 | echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 674 | }
|
| 675 | else
|
| 676 | {
|
Derek Allard | 72c82c1 | 2008-04-04 11:34:58 +0000 | [diff] [blame] | 677 | include($_ci_path); // include() vs include_once() allows for multiple views with the same name
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 678 | }
|
| 679 |
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 680 | log_message('debug', 'File loaded: '.$_ci_path);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 681 |
|
| 682 | // Return the file data if requested
|
Derek Jones | 47845f2 | 2008-01-16 23:23:02 +0000 | [diff] [blame] | 683 | if ($_ci_return === TRUE)
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 684 | {
|
| 685 | $buffer = ob_get_contents();
|
| 686 | @ob_end_clean();
|
| 687 | return $buffer;
|
| 688 | }
|
| 689 |
|
| 690 | /*
|
| 691 | * Flush the buffer... or buff the flusher?
|
| 692 | *
|
| 693 | * In order to permit views to be nested within
|
| 694 | * other views, we need to flush the content back out whenever
|
| 695 | * we are beyond the first level of output buffering so that
|
| 696 | * it can be seen and included properly by the first included
|
| 697 | * template and any subsequent ones. Oy!
|
| 698 | *
|
| 699 | */
|
| 700 | if (ob_get_level() > $this->_ci_ob_level + 1)
|
| 701 | {
|
| 702 | ob_end_flush();
|
| 703 | }
|
| 704 | else
|
| 705 | {
|
| 706 | // PHP 4 requires that we use a global
|
| 707 | global $OUT;
|
Derek Allard | 66f0724 | 2008-01-18 22:36:14 +0000 | [diff] [blame] | 708 | $OUT->append_output(ob_get_contents());
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 709 | @ob_end_clean();
|
| 710 | }
|
| 711 | }
|
| 712 |
|
| 713 | // --------------------------------------------------------------------
|
| 714 |
|
| 715 | /**
|
| 716 | * Load class
|
| 717 | *
|
| 718 | * This function loads the requested class.
|
| 719 | *
|
| 720 | * @access private
|
| 721 | * @param string the item that is being loaded
|
| 722 | * @param mixed any additional parameters
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 723 | * @param string an optional object name
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 724 | * @return void
|
| 725 | */
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 726 | function _ci_load_class($class, $params = NULL, $object_name = NULL)
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 727 | {
|
Rick Ellis | 0ea8f98 | 2008-08-12 00:39:10 +0000 | [diff] [blame] | 728 | // Get the class name, and while we're at it trim any slashes.
|
| 729 | // The directory path can be included as part of the class name,
|
| 730 | // but we don't want a leading slash
|
| 731 | $class = str_replace(EXT, '', trim($class, '/'));
|
| 732 |
|
| 733 | // Was the path included with the class name?
|
| 734 | // We look for a slash to determine this
|
| 735 | $subdir = '';
|
| 736 | if (strpos($class, '/') !== FALSE)
|
| 737 | {
|
| 738 | // explode the path so we can separate the filename from the path
|
| 739 | $x = explode('/', $class);
|
| 740 |
|
| 741 | // Reset the $class variable now that we know the actual filename
|
| 742 | $class = end($x);
|
| 743 |
|
| 744 | // Kill the filename from the array
|
| 745 | unset($x[count($x)-1]);
|
| 746 |
|
| 747 | // Glue the path back together, sans filename
|
| 748 | $subdir = implode($x, '/').'/';
|
| 749 | }
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 750 |
|
| 751 | // We'll test for both lowercase and capitalized versions of the file name
|
| 752 | foreach (array(ucfirst($class), strtolower($class)) as $class)
|
| 753 | {
|
Rick Ellis | afb056a | 2008-08-12 00:44:25 +0000 | [diff] [blame] | 754 | $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.EXT;
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 755 |
|
| 756 | // Is this a class extension request?
|
| 757 | if (file_exists($subclass))
|
| 758 | {
|
| 759 | $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
|
| 760 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 761 | if ( ! file_exists($baseclass))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 762 | {
|
| 763 | log_message('error', "Unable to load the requested class: ".$class);
|
| 764 | show_error("Unable to load the requested class: ".$class);
|
| 765 | }
|
| 766 |
|
| 767 | // Safety: Was the class already loaded by a previous call?
|
Rick Ellis | 7cdef03 | 2008-08-26 18:44:54 +0000 | [diff] [blame] | 768 | if (in_array($subclass, $this->_ci_loaded_files))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 769 | {
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 770 | // Before we deem this to be a duplicate request, let's see
|
| 771 | // if a custom object name is being supplied. If so, we'll
|
| 772 | // return a new instance of the object
|
| 773 | if ( ! is_null($object_name))
|
| 774 | {
|
| 775 | $CI =& get_instance();
|
| 776 | if ( ! isset($CI->$object_name))
|
| 777 | {
|
| 778 | return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
|
| 779 | }
|
Rick Ellis | 76c504e | 2008-08-26 19:11:34 +0000 | [diff] [blame] | 780 | }
|
| 781 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 782 | $is_duplicate = TRUE;
|
| 783 | log_message('debug', $class." class already loaded. Second attempt ignored.");
|
| 784 | return;
|
| 785 | }
|
| 786 |
|
Derek Allard | d888c35 | 2008-03-18 04:04:33 +0000 | [diff] [blame] | 787 | include_once($baseclass);
|
| 788 | include_once($subclass);
|
Rick Ellis | 7cdef03 | 2008-08-26 18:44:54 +0000 | [diff] [blame] | 789 | $this->_ci_loaded_files[] = $subclass;
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 790 |
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 791 | return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 792 | }
|
| 793 |
|
| 794 | // Lets search for the requested library file and load it.
|
| 795 | $is_duplicate = FALSE;
|
| 796 | for ($i = 1; $i < 3; $i++)
|
| 797 | {
|
| 798 | $path = ($i % 2) ? APPPATH : BASEPATH;
|
Rick Ellis | afb056a | 2008-08-12 00:44:25 +0000 | [diff] [blame] | 799 | $filepath = $path.'libraries/'.$subdir.$class.EXT;
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 800 |
|
| 801 | // Does the file exist? No? Bummer...
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 802 | if ( ! file_exists($filepath))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 803 | {
|
| 804 | continue;
|
| 805 | }
|
| 806 |
|
| 807 | // Safety: Was the class already loaded by a previous call?
|
Rick Ellis | 7cdef03 | 2008-08-26 18:44:54 +0000 | [diff] [blame] | 808 | if (in_array($filepath, $this->_ci_loaded_files))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 809 | {
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 810 | // Before we deem this to be a duplicate request, let's see
|
| 811 | // if a custom object name is being supplied. If so, we'll
|
| 812 | // return a new instance of the object
|
| 813 | if ( ! is_null($object_name))
|
| 814 | {
|
| 815 | $CI =& get_instance();
|
| 816 | if ( ! isset($CI->$object_name))
|
| 817 | {
|
| 818 | return $this->_ci_init_class($class, '', $params, $object_name);
|
| 819 | }
|
| 820 | }
|
| 821 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 822 | $is_duplicate = TRUE;
|
| 823 | log_message('debug', $class." class already loaded. Second attempt ignored.");
|
| 824 | return;
|
| 825 | }
|
| 826 |
|
Derek Allard | d888c35 | 2008-03-18 04:04:33 +0000 | [diff] [blame] | 827 | include_once($filepath);
|
Rick Ellis | 7cdef03 | 2008-08-26 18:44:54 +0000 | [diff] [blame] | 828 | $this->_ci_loaded_files[] = $filepath;
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 829 | return $this->_ci_init_class($class, '', $params, $object_name);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 830 | }
|
| 831 | } // END FOREACH
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 832 |
|
| 833 | // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
|
| 834 | if ($subdir == '')
|
| 835 | {
|
| 836 | $path = strtolower($class).'/'.$class;
|
| 837 | return $this->_ci_load_class($path, $params);
|
| 838 | }
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 839 |
|
| 840 | // If we got this far we were unable to find the requested class.
|
| 841 | // We do not issue errors if the load call failed due to a duplicate request
|
| 842 | if ($is_duplicate == FALSE)
|
| 843 | {
|
| 844 | log_message('error', "Unable to load the requested class: ".$class);
|
| 845 | show_error("Unable to load the requested class: ".$class);
|
| 846 | }
|
| 847 | }
|
| 848 |
|
| 849 | // --------------------------------------------------------------------
|
| 850 |
|
| 851 | /**
|
| 852 | * Instantiates a class
|
| 853 | *
|
| 854 | * @access private
|
| 855 | * @param string
|
| 856 | * @param string
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 857 | * @param string an optional object name
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 858 | * @return null
|
| 859 | */
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 860 | function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 861 | {
|
| 862 | // Is there an associated config file for this class?
|
| 863 | if ($config === NULL)
|
| 864 | {
|
| 865 | if (file_exists(APPPATH.'config/'.$class.EXT))
|
| 866 | {
|
Derek Allard | d888c35 | 2008-03-18 04:04:33 +0000 | [diff] [blame] | 867 | include_once(APPPATH.'config/'.$class.EXT);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 868 | }
|
| 869 | }
|
| 870 |
|
| 871 | if ($prefix == '')
|
Rick Ellis | 76c504e | 2008-08-26 19:11:34 +0000 | [diff] [blame] | 872 | {
|
| 873 | if (class_exists('CI_'.$class))
|
| 874 | {
|
| 875 | $name = 'CI_'.$class;
|
| 876 | }
|
| 877 | elseif (class_exists(config_item('subclass_prefix').$class))
|
| 878 | {
|
| 879 | $name = config_item('subclass_prefix').$class;
|
| 880 | }
|
| 881 | else
|
| 882 | {
|
| 883 | $name = $class;
|
| 884 | }
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 885 | }
|
| 886 | else
|
| 887 | {
|
| 888 | $name = $prefix.$class;
|
| 889 | }
|
| 890 |
|
Rick Ellis | 76c504e | 2008-08-26 19:11:34 +0000 | [diff] [blame] | 891 | // Is the class name valid?
|
| 892 | if ( ! class_exists($name))
|
| 893 | {
|
Derek Jones | ccfe913 | 2008-10-09 18:46:13 +0000 | [diff] [blame] | 894 | log_message('error', "Non-existent class: ".$name);
|
| 895 | show_error("Non-existent class: ".$class);
|
Rick Ellis | 76c504e | 2008-08-26 19:11:34 +0000 | [diff] [blame] | 896 | }
|
| 897 |
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 898 | // Set the variable name we will assign the class to
|
| 899 | // Was a custom class name supplied? If so we'll use it
|
Rick Ellis | 76c504e | 2008-08-26 19:11:34 +0000 | [diff] [blame] | 900 | $class = strtolower($class);
|
| 901 |
|
Rick Ellis | 6ad5329 | 2008-08-21 18:31:28 +0000 | [diff] [blame] | 902 | if (is_null($object_name))
|
| 903 | {
|
| 904 | $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
|
| 905 | }
|
| 906 | else
|
| 907 | {
|
| 908 | $classvar = $object_name;
|
| 909 | }
|
Rick Ellis | 7cdef03 | 2008-08-26 18:44:54 +0000 | [diff] [blame] | 910 |
|
| 911 | // Save the class name and object name
|
Rick Ellis | 7926943 | 2008-08-27 06:25:08 +0000 | [diff] [blame] | 912 | $this->_ci_classes[$class] = $classvar;
|
Rick Ellis | 7cdef03 | 2008-08-26 18:44:54 +0000 | [diff] [blame] | 913 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 914 | // Instantiate the class
|
| 915 | $CI =& get_instance();
|
| 916 | if ($config !== NULL)
|
| 917 | {
|
| 918 | $CI->$classvar = new $name($config);
|
| 919 | }
|
| 920 | else
|
| 921 | {
|
| 922 | $CI->$classvar = new $name;
|
| 923 | }
|
| 924 | }
|
| 925 |
|
| 926 | // --------------------------------------------------------------------
|
| 927 |
|
| 928 | /**
|
| 929 | * Autoloader
|
| 930 | *
|
| 931 | * The config/autoload.php file contains an array that permits sub-systems,
|
| 932 | * libraries, plugins, and helpers to be loaded automatically.
|
| 933 | *
|
| 934 | * @access private
|
| 935 | * @param array
|
| 936 | * @return void
|
| 937 | */
|
| 938 | function _ci_autoloader()
|
| 939 | {
|
Derek Allard | d888c35 | 2008-03-18 04:04:33 +0000 | [diff] [blame] | 940 | include_once(APPPATH.'config/autoload'.EXT);
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 941 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 942 | if ( ! isset($autoload))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 943 | {
|
| 944 | return FALSE;
|
| 945 | }
|
| 946 |
|
| 947 | // Load any custom config file
|
| 948 | if (count($autoload['config']) > 0)
|
| 949 | {
|
| 950 | $CI =& get_instance();
|
| 951 | foreach ($autoload['config'] as $key => $val)
|
| 952 | {
|
| 953 | $CI->config->load($val);
|
| 954 | }
|
| 955 | }
|
| 956 |
|
Derek Allard | 15dcf49 | 2008-05-12 21:37:04 +0000 | [diff] [blame] | 957 | // Autoload plugins, helpers and languages
|
| 958 | foreach (array('helper', 'plugin', 'language') as $type)
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 959 | {
|
| 960 | if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
|
| 961 | {
|
| 962 | $this->$type($autoload[$type]);
|
| 963 | }
|
| 964 | }
|
| 965 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 966 | // A little tweak to remain backward compatible
|
| 967 | // The $autoload['core'] item was deprecated
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 968 | if ( ! isset($autoload['libraries']))
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 969 | {
|
| 970 | $autoload['libraries'] = $autoload['core'];
|
| 971 | }
|
| 972 |
|
| 973 | // Load libraries
|
| 974 | if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
|
| 975 | {
|
| 976 | // Load the database driver.
|
| 977 | if (in_array('database', $autoload['libraries']))
|
| 978 | {
|
| 979 | $this->database();
|
| 980 | $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
|
| 981 | }
|
| 982 |
|
| 983 | // Load scaffolding
|
| 984 | if (in_array('scaffolding', $autoload['libraries']))
|
| 985 | {
|
| 986 | $this->scaffolding();
|
| 987 | $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
|
| 988 | }
|
| 989 |
|
| 990 | // Load all other libraries
|
| 991 | foreach ($autoload['libraries'] as $item)
|
| 992 | {
|
| 993 | $this->library($item);
|
| 994 | }
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 995 | }
|
Derek Allard | 22259b5 | 2008-01-22 01:47:11 +0000 | [diff] [blame] | 996 |
|
| 997 | // Autoload models
|
| 998 | if (isset($autoload['model']))
|
| 999 | {
|
| 1000 | $this->model($autoload['model']);
|
| 1001 | }
|
| 1002 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 1003 | }
|
| 1004 |
|
| 1005 | // --------------------------------------------------------------------
|
| 1006 |
|
| 1007 | /**
|
| 1008 | * Assign to Models
|
| 1009 | *
|
| 1010 | * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
|
| 1011 | * will be available to models, if any exist.
|
| 1012 | *
|
| 1013 | * @access private
|
| 1014 | * @param object
|
| 1015 | * @return array
|
| 1016 | */
|
| 1017 | function _ci_assign_to_models()
|
| 1018 | {
|
| 1019 | if (count($this->_ci_models) == 0)
|
| 1020 | {
|
| 1021 | return;
|
| 1022 | }
|
| 1023 |
|
| 1024 | if ($this->_ci_is_instance())
|
| 1025 | {
|
| 1026 | $CI =& get_instance();
|
| 1027 | foreach ($this->_ci_models as $model)
|
| 1028 | {
|
| 1029 | $CI->$model->_assign_libraries();
|
| 1030 | }
|
| 1031 | }
|
| 1032 | else
|
| 1033 | {
|
| 1034 | foreach ($this->_ci_models as $model)
|
| 1035 | {
|
| 1036 | $this->$model->_assign_libraries();
|
| 1037 | }
|
| 1038 | }
|
| 1039 | }
|
| 1040 |
|
| 1041 | // --------------------------------------------------------------------
|
| 1042 |
|
| 1043 | /**
|
| 1044 | * Object to Array
|
| 1045 | *
|
| 1046 | * Takes an object as input and converts the class variables to array key/vals
|
| 1047 | *
|
| 1048 | * @access private
|
| 1049 | * @param object
|
| 1050 | * @return array
|
| 1051 | */
|
| 1052 | function _ci_object_to_array($object)
|
| 1053 | {
|
| 1054 | return (is_object($object)) ? get_object_vars($object) : $object;
|
| 1055 | }
|
| 1056 |
|
| 1057 | // --------------------------------------------------------------------
|
| 1058 |
|
| 1059 | /**
|
| 1060 | * Determines whether we should use the CI instance or $this
|
| 1061 | *
|
| 1062 | * @access private
|
| 1063 | * @return bool
|
| 1064 | */
|
| 1065 | function _ci_is_instance()
|
| 1066 | {
|
| 1067 | if ($this->_ci_is_php5 == TRUE)
|
| 1068 | {
|
| 1069 | return TRUE;
|
| 1070 | }
|
| 1071 |
|
| 1072 | global $CI;
|
| 1073 | return (is_object($CI)) ? TRUE : FALSE;
|
| 1074 | }
|
Rick Ellis | 76c504e | 2008-08-26 19:11:34 +0000 | [diff] [blame] | 1075 |
|
Derek Allard | 0265857 | 2007-12-20 14:00:50 +0000 | [diff] [blame] | 1076 | }
|
Derek Allard | 15dcf49 | 2008-05-12 21:37:04 +0000 | [diff] [blame] | 1077 |
|
| 1078 | /* End of file Loader.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 1079 | /* Location: ./system/libraries/Loader.php */ |