Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Loader Class |
| 20 | * |
| 21 | * Loads views and files |
| 22 | * |
| 23 | * @package CodeIgniter |
| 24 | * @subpackage Libraries |
| 25 | * @author ExpressionEngine Dev Team |
| 26 | * @category Loader |
| 27 | * @link http://codeigniter.com/user_guide/libraries/loader.html |
| 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 = ''; |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 34 | var $_ci_library_paths = array(); |
| 35 | var $_ci_model_paths = array(); |
| 36 | var $_ci_helper_paths = array(); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 37 | var $_base_classes = array(); // Set by the controller class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | var $_ci_cached_vars = array(); |
| 39 | var $_ci_classes = array(); |
| 40 | var $_ci_loaded_files = array(); |
| 41 | var $_ci_models = array(); |
| 42 | var $_ci_helpers = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 43 | var $_ci_varmap = array('unit_test' => 'unit', 'user_agent' => 'agent'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 44 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Constructor |
| 48 | * |
| 49 | * Sets the path to the view files and gets the initial output buffering level |
| 50 | * |
| 51 | * @access public |
| 52 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 53 | function __construct() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 54 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 55 | $this->_ci_view_path = APPPATH.'views/'; |
| 56 | $this->_ci_ob_level = ob_get_level(); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 57 | $this->_ci_library_paths = array(APPPATH, BASEPATH); |
| 58 | $this->_ci_helper_paths = array(APPPATH, BASEPATH); |
| 59 | $this->_ci_model_paths = array(APPPATH); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 60 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | log_message('debug', "Loader Class Initialized"); |
| 62 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 63 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 65 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | /** |
| 67 | * Class Loader |
| 68 | * |
| 69 | * This function lets users load and instantiate classes. |
| 70 | * It is designed to be called from a user's app controllers. |
| 71 | * |
| 72 | * @access public |
| 73 | * @param string the name of the class |
| 74 | * @param mixed the optional parameters |
| 75 | * @param string an optional object name |
| 76 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 77 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | function library($library = '', $params = NULL, $object_name = NULL) |
| 79 | { |
Greg Aker | ce43396 | 2010-10-12 09:29:35 -0500 | [diff] [blame] | 80 | if (is_array($library)) |
| 81 | { |
| 82 | foreach($library as $read) |
| 83 | { |
| 84 | $this->library($read); |
| 85 | } |
| 86 | |
| 87 | return; |
| 88 | } |
| 89 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 90 | if ($library == '' OR isset($this->_base_classes[$library])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | { |
| 92 | return FALSE; |
| 93 | } |
| 94 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 95 | if ( ! is_null($params) && ! is_array($params)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | { |
| 97 | $params = NULL; |
| 98 | } |
| 99 | |
| 100 | if (is_array($library)) |
| 101 | { |
| 102 | foreach ($library as $class) |
| 103 | { |
| 104 | $this->_ci_load_class($class, $params, $object_name); |
| 105 | } |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | $this->_ci_load_class($library, $params, $object_name); |
| 110 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 114 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | /** |
| 116 | * Model Loader |
| 117 | * |
| 118 | * This function lets users load and instantiate models. |
| 119 | * |
| 120 | * @access public |
| 121 | * @param string the name of the class |
| 122 | * @param string name for the model |
| 123 | * @param bool database connection |
| 124 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | function model($model, $name = '', $db_conn = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 127 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | if (is_array($model)) |
| 129 | { |
| 130 | foreach($model as $babe) |
| 131 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 132 | $this->model($babe); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | } |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | if ($model == '') |
| 138 | { |
| 139 | return; |
| 140 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 141 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 142 | $path = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 143 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | // Is the model in a sub-folder? If so, parse out the filename and path. |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 145 | if (($last_slash = strrpos($model, '/')) !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | { |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 147 | // The path is in front of the last slash |
| 148 | $path = substr($model, 0, $last_slash + 1); |
| 149 | |
| 150 | // And the model name behind it |
| 151 | $model = substr($model, $last_slash + 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 153 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | if ($name == '') |
| 155 | { |
| 156 | $name = $model; |
| 157 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | if (in_array($name, $this->_ci_models, TRUE)) |
| 160 | { |
| 161 | return; |
| 162 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | $CI =& get_instance(); |
| 165 | if (isset($CI->$name)) |
| 166 | { |
| 167 | show_error('The model name you are loading is the name of a resource that is already being used: '.$name); |
| 168 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 169 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | $model = strtolower($model); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 172 | foreach ($this->_ci_model_paths as $mod_path) |
| 173 | { |
| 174 | if ( ! file_exists($mod_path.'models/'.$path.$model.EXT)) |
| 175 | { |
| 176 | continue; |
| 177 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 179 | if ($db_conn !== FALSE AND ! class_exists('CI_DB')) |
| 180 | { |
| 181 | if ($db_conn === TRUE) |
Pascal Kriete | 287781e | 2010-11-10 15:43:49 -0500 | [diff] [blame] | 182 | { |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 183 | $db_conn = ''; |
Pascal Kriete | 287781e | 2010-11-10 15:43:49 -0500 | [diff] [blame] | 184 | } |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 185 | |
| 186 | $CI->load->database($db_conn, FALSE, TRUE); |
| 187 | } |
| 188 | |
Greg Aker | bce1348 | 2010-10-11 15:37:16 -0500 | [diff] [blame] | 189 | if ( ! class_exists('CI_Model')) |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 190 | { |
| 191 | load_class('Model', 'core'); |
| 192 | } |
| 193 | |
| 194 | require_once($mod_path.'models/'.$path.$model.EXT); |
| 195 | |
| 196 | $model = ucfirst($model); |
| 197 | |
| 198 | $CI->$name = new $model(); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 199 | |
| 200 | $this->_ci_models[] = $name; |
| 201 | return; |
| 202 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 203 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 204 | // couldn't find the model |
| 205 | show_error('Unable to locate the model you have specified: '.$model); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 207 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 209 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | /** |
| 211 | * Database Loader |
| 212 | * |
| 213 | * @access public |
| 214 | * @param string the DB credentials |
| 215 | * @param bool whether to return the DB object |
| 216 | * @param bool whether to enable active record (this allows us to override the config setting) |
| 217 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 218 | */ |
Derek Jones | ffca6c2 | 2009-12-05 15:31:44 +0000 | [diff] [blame] | 219 | function database($params = '', $return = FALSE, $active_record = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | { |
| 221 | // Grab the super object |
| 222 | $CI =& get_instance(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | // Do we even need to load the database class? |
Derek Jones | 9fb6dd1 | 2009-12-05 15:32:48 +0000 | [diff] [blame] | 225 | if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | { |
| 227 | return FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | require_once(BASEPATH.'database/DB'.EXT); |
| 231 | |
| 232 | if ($return === TRUE) |
| 233 | { |
| 234 | return DB($params, $active_record); |
| 235 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 236 | |
| 237 | // Initialize the db variable. Needed to prevent |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 238 | // reference errors with some configurations |
| 239 | $CI->db = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 240 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 241 | // Load the DB class |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 242 | $CI->db =& DB($params, $active_record); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 244 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 245 | // -------------------------------------------------------------------- |
| 246 | |
| 247 | /** |
| 248 | * Load the Utilities Class |
| 249 | * |
| 250 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 251 | * @return string |
| 252 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | function dbutil() |
| 254 | { |
| 255 | if ( ! class_exists('CI_DB')) |
| 256 | { |
| 257 | $this->database(); |
| 258 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 259 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | $CI =& get_instance(); |
| 261 | |
| 262 | // for backwards compatibility, load dbforge so we can extend dbutils off it |
| 263 | // this use is deprecated and strongly discouraged |
| 264 | $CI->load->dbforge(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 265 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | require_once(BASEPATH.'database/DB_utility'.EXT); |
| 267 | require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT); |
| 268 | $class = 'CI_DB_'.$CI->db->dbdriver.'_utility'; |
| 269 | |
Pascal Kriete | 5856002 | 2010-11-10 16:01:20 -0500 | [diff] [blame] | 270 | $CI->dbutil = new $class(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 272 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | // -------------------------------------------------------------------- |
| 274 | |
| 275 | /** |
| 276 | * Load the Database Forge Class |
| 277 | * |
| 278 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 279 | * @return string |
| 280 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | function dbforge() |
| 282 | { |
| 283 | if ( ! class_exists('CI_DB')) |
| 284 | { |
| 285 | $this->database(); |
| 286 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 287 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | $CI =& get_instance(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 289 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | require_once(BASEPATH.'database/DB_forge'.EXT); |
| 291 | require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge'.EXT); |
| 292 | $class = 'CI_DB_'.$CI->db->dbdriver.'_forge'; |
| 293 | |
| 294 | $CI->dbforge = new $class(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 295 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 296 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 298 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 299 | /** |
| 300 | * Load View |
| 301 | * |
| 302 | * This function is used to load a "view" file. It has three parameters: |
| 303 | * |
| 304 | * 1. The name of the "view" file to be included. |
| 305 | * 2. An associative array of data to be extracted for use in the view. |
| 306 | * 3. TRUE/FALSE - whether to return the data or load it. In |
| 307 | * some cases it's advantageous to be able to return data so that |
| 308 | * a developer can process it in some way. |
| 309 | * |
| 310 | * @access public |
| 311 | * @param string |
| 312 | * @param array |
| 313 | * @param bool |
| 314 | * @return void |
| 315 | */ |
| 316 | function view($view, $vars = array(), $return = FALSE) |
| 317 | { |
| 318 | return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return)); |
| 319 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 320 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 322 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | /** |
| 324 | * Load File |
| 325 | * |
| 326 | * This is a generic file loader |
| 327 | * |
| 328 | * @access public |
| 329 | * @param string |
| 330 | * @param bool |
| 331 | * @return string |
| 332 | */ |
| 333 | function file($path, $return = FALSE) |
| 334 | { |
| 335 | return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return)); |
| 336 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 337 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | /** |
| 341 | * Set Variables |
| 342 | * |
| 343 | * Once variables are set they become available within |
| 344 | * the controller class and its "view" files. |
| 345 | * |
| 346 | * @access public |
| 347 | * @param array |
| 348 | * @return void |
| 349 | */ |
| 350 | function vars($vars = array(), $val = '') |
| 351 | { |
| 352 | if ($val != '' AND is_string($vars)) |
| 353 | { |
| 354 | $vars = array($vars => $val); |
| 355 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 356 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | $vars = $this->_ci_object_to_array($vars); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 358 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | if (is_array($vars) AND count($vars) > 0) |
| 360 | { |
| 361 | foreach ($vars as $key => $val) |
| 362 | { |
| 363 | $this->_ci_cached_vars[$key] = $val; |
| 364 | } |
| 365 | } |
| 366 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 367 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 369 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | /** |
| 371 | * Load Helper |
| 372 | * |
| 373 | * This function loads the specified helper file. |
| 374 | * |
| 375 | * @access public |
| 376 | * @param mixed |
| 377 | * @return void |
| 378 | */ |
| 379 | function helper($helpers = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 380 | { |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 381 | foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 382 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | if (isset($this->_ci_helpers[$helper])) |
| 384 | { |
| 385 | continue; |
| 386 | } |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 387 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.EXT; |
| 389 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 390 | // Is this a helper extension request? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 391 | if (file_exists($ext_helper)) |
| 392 | { |
| 393 | $base_helper = BASEPATH.'helpers/'.$helper.EXT; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 394 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | if ( ! file_exists($base_helper)) |
| 396 | { |
| 397 | show_error('Unable to load the requested file: helpers/'.$helper.EXT); |
| 398 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 399 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | include_once($ext_helper); |
| 401 | include_once($base_helper); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 402 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 403 | $this->_ci_helpers[$helper] = TRUE; |
| 404 | log_message('debug', 'Helper loaded: '.$helper); |
| 405 | continue; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 407 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 408 | // Try to load the helper |
| 409 | foreach ($this->_ci_helper_paths as $path) |
| 410 | { |
| 411 | if (file_exists($path.'helpers/'.$helper.EXT)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 412 | { |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 413 | include_once($path.'helpers/'.$helper.EXT); |
| 414 | |
| 415 | $this->_ci_helpers[$helper] = TRUE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 416 | log_message('debug', 'Helper loaded: '.$helper); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 417 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 421 | // unable to load the helper |
| 422 | if ( ! isset($this->_ci_helpers[$helper])) |
| 423 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 424 | show_error('Unable to load the requested file: helpers/'.$helper.EXT); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 425 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 426 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 428 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 430 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | /** |
| 432 | * Load Helpers |
| 433 | * |
| 434 | * This is simply an alias to the above function in case the |
| 435 | * user has written the plural form of this function. |
| 436 | * |
| 437 | * @access public |
| 438 | * @param array |
| 439 | * @return void |
| 440 | */ |
| 441 | function helpers($helpers = array()) |
| 442 | { |
| 443 | $this->helper($helpers); |
| 444 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 445 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 447 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | * Loads a language file |
| 450 | * |
| 451 | * @access public |
| 452 | * @param array |
| 453 | * @param string |
| 454 | * @return void |
| 455 | */ |
| 456 | function language($file = array(), $lang = '') |
| 457 | { |
| 458 | $CI =& get_instance(); |
| 459 | |
| 460 | if ( ! is_array($file)) |
| 461 | { |
| 462 | $file = array($file); |
| 463 | } |
| 464 | |
| 465 | foreach ($file as $langfile) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 466 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | $CI->lang->load($langfile, $lang); |
| 468 | } |
| 469 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 470 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 472 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 473 | /** |
| 474 | * Loads a config file |
| 475 | * |
| 476 | * @access public |
| 477 | * @param string |
| 478 | * @return void |
| 479 | */ |
| 480 | function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 481 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | $CI =& get_instance(); |
| 483 | $CI->config->load($file, $use_sections, $fail_gracefully); |
| 484 | } |
| 485 | |
| 486 | // -------------------------------------------------------------------- |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 487 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | /** |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 489 | * Driver |
| 490 | * |
| 491 | * Loads a driver library |
| 492 | * |
| 493 | * @param string the name of the class |
| 494 | * @param mixed the optional parameters |
| 495 | * @param string an optional object name |
| 496 | * @return void |
| 497 | */ |
| 498 | function driver($library = '', $params = NULL, $object_name = NULL) |
| 499 | { |
| 500 | if ( ! class_exists('CI_Driver_Library')) |
| 501 | { |
| 502 | // we aren't instantiating an object here, that'll be done by the Library itself |
Derek Jones | d5e0cb5 | 2010-03-09 20:20:46 -0600 | [diff] [blame] | 503 | require BASEPATH.'libraries/Driver'.EXT; |
| 504 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 505 | |
Derek Jones | d5e0cb5 | 2010-03-09 20:20:46 -0600 | [diff] [blame] | 506 | // We can save the loader some time since Drivers will *always* be in a subfolder, |
| 507 | // and typically identically named to the library |
| 508 | if ( ! strpos($library, '/')) |
| 509 | { |
Greg Aker | d25e66a | 2010-03-28 01:07:09 -0500 | [diff] [blame] | 510 | $library = ucfirst($library).'/'.$library; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 511 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 512 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 513 | return $this->library($library, $params, $object_name); |
| 514 | } |
| 515 | |
| 516 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 517 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 518 | /** |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 519 | * Add Package Path |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 520 | * |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 521 | * Prepends a parent path to the library, model, helper, and config path arrays |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 522 | * |
| 523 | * @access public |
| 524 | * @param string |
| 525 | * @return void |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 526 | */ |
| 527 | function add_package_path($path) |
| 528 | { |
Pascal Kriete | 6b6c274 | 2010-11-09 13:12:22 -0500 | [diff] [blame] | 529 | $path = rtrim($path, '/').'/'; |
| 530 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 531 | array_unshift($this->_ci_library_paths, $path); |
| 532 | array_unshift($this->_ci_model_paths, $path); |
| 533 | array_unshift($this->_ci_helper_paths, $path); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 534 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 535 | // Add config file path |
| 536 | $config =& $this->_ci_get_component('config'); |
| 537 | array_unshift($config->_config_paths, $path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | // -------------------------------------------------------------------- |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 541 | |
| 542 | /** |
| 543 | * Remove Package Path |
| 544 | * |
| 545 | * Remove a path from the library, model, and helper path arrays if it exists |
| 546 | * If no path is provided, the most recently added path is removed. |
| 547 | * |
| 548 | * @access public |
| 549 | * @param type |
| 550 | * @return type |
| 551 | */ |
| 552 | function remove_package_path($path = '', $remove_config_path = TRUE) |
| 553 | { |
| 554 | $config =& $this->_ci_get_component('config'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 555 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 556 | if ($path == '') |
| 557 | { |
| 558 | $void = array_shift($this->_ci_library_paths); |
| 559 | $void = array_shift($this->_ci_model_paths); |
| 560 | $void = array_shift($this->_ci_helper_paths); |
| 561 | $void = array_shift($config->_config_paths); |
| 562 | } |
| 563 | else |
| 564 | { |
Pascal Kriete | 6b6c274 | 2010-11-09 13:12:22 -0500 | [diff] [blame] | 565 | $path = rtrim($path, '/').'/'; |
| 566 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 567 | foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var) |
| 568 | { |
| 569 | if (($key = array_search($path, $this->{$var})) !== FALSE) |
| 570 | { |
| 571 | unset($this->{$var}[$key]); |
| 572 | } |
| 573 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 574 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 575 | if (($key = array_search($path, $config->_config_paths)) !== FALSE) |
| 576 | { |
| 577 | unset($config->_config_paths[$key]); |
| 578 | } |
| 579 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 580 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 581 | // make sure the application default paths are still in the array |
| 582 | $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH))); |
| 583 | $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH))); |
| 584 | $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH))); |
| 585 | $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH))); |
| 586 | } |
| 587 | |
| 588 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 589 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 590 | /** |
| 591 | * Loader |
| 592 | * |
| 593 | * This function is used to load views and files. |
| 594 | * Variables are prefixed with _ci_ to avoid symbol collision with |
| 595 | * variables made available to view files |
| 596 | * |
| 597 | * @access private |
| 598 | * @param array |
| 599 | * @return void |
| 600 | */ |
| 601 | function _ci_load($_ci_data) |
| 602 | { |
| 603 | // Set the default data variables |
| 604 | foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val) |
| 605 | { |
| 606 | $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val]; |
| 607 | } |
| 608 | |
| 609 | // Set the path to the requested file |
| 610 | if ($_ci_path == '') |
| 611 | { |
| 612 | $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION); |
| 613 | $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view; |
| 614 | $_ci_path = $this->_ci_view_path.$_ci_file; |
| 615 | } |
| 616 | else |
| 617 | { |
| 618 | $_ci_x = explode('/', $_ci_path); |
| 619 | $_ci_file = end($_ci_x); |
| 620 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 621 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 622 | if ( ! file_exists($_ci_path)) |
| 623 | { |
| 624 | show_error('Unable to load the requested file: '.$_ci_file); |
| 625 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 626 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 627 | // This allows anything loaded using $this->load (views, files, etc.) |
| 628 | // to become accessible from within the Controller and Model functions. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 629 | |
Pascal Kriete | 89ace43 | 2010-11-10 15:49:10 -0500 | [diff] [blame] | 630 | $_ci_CI =& get_instance(); |
| 631 | foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | { |
Pascal Kriete | 89ace43 | 2010-11-10 15:49:10 -0500 | [diff] [blame] | 633 | if ( ! isset($this->$_ci_key)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 634 | { |
Pascal Kriete | 89ace43 | 2010-11-10 15:49:10 -0500 | [diff] [blame] | 635 | $this->$_ci_key =& $_ci_CI->$_ci_key; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
| 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. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 646 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 647 | if (is_array($_ci_vars)) |
| 648 | { |
| 649 | $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars); |
| 650 | } |
| 651 | extract($this->_ci_cached_vars); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 652 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 666 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 670 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 671 | if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE) |
| 672 | { |
| 673 | echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path)))); |
| 674 | } |
| 675 | else |
| 676 | { |
| 677 | include($_ci_path); // include() vs include_once() allows for multiple views with the same name |
| 678 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 679 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 680 | log_message('debug', 'File loaded: '.$_ci_path); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 681 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 682 | // Return the file data if requested |
| 683 | if ($_ci_return === TRUE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 684 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 699 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 700 | if (ob_get_level() > $this->_ci_ob_level + 1) |
| 701 | { |
| 702 | ob_end_flush(); |
| 703 | } |
| 704 | else |
| 705 | { |
Greg Aker | 22f1a63 | 2010-11-10 15:34:35 -0600 | [diff] [blame^] | 706 | $_ci_CI->output->append_output(ob_get_contents()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 707 | @ob_end_clean(); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | // -------------------------------------------------------------------- |
| 712 | |
| 713 | /** |
| 714 | * Load class |
| 715 | * |
| 716 | * This function loads the requested class. |
| 717 | * |
| 718 | * @access private |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 719 | * @param string the item that is being loaded |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 720 | * @param mixed any additional parameters |
| 721 | * @param string an optional object name |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 722 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 723 | */ |
| 724 | function _ci_load_class($class, $params = NULL, $object_name = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 725 | { |
| 726 | // Get the class name, and while we're at it trim any slashes. |
| 727 | // The directory path can be included as part of the class name, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 728 | // but we don't want a leading slash |
| 729 | $class = str_replace(EXT, '', trim($class, '/')); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 730 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 731 | // Was the path included with the class name? |
| 732 | // We look for a slash to determine this |
| 733 | $subdir = ''; |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 734 | if (($last_slash = strrpos($class, '/')) !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 735 | { |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 736 | // Extract the path |
| 737 | $subdir = substr($class, 0, $last_slash + 1); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 738 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 739 | // Get the filename from the path |
| 740 | $class = substr($class, $last_slash + 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | // We'll test for both lowercase and capitalized versions of the file name |
| 744 | foreach (array(ucfirst($class), strtolower($class)) as $class) |
| 745 | { |
| 746 | $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.EXT; |
| 747 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 748 | // Is this a class extension request? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 749 | if (file_exists($subclass)) |
| 750 | { |
| 751 | $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 752 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 753 | if ( ! file_exists($baseclass)) |
| 754 | { |
| 755 | log_message('error', "Unable to load the requested class: ".$class); |
| 756 | show_error("Unable to load the requested class: ".$class); |
| 757 | } |
| 758 | |
| 759 | // Safety: Was the class already loaded by a previous call? |
| 760 | if (in_array($subclass, $this->_ci_loaded_files)) |
| 761 | { |
| 762 | // Before we deem this to be a duplicate request, let's see |
| 763 | // if a custom object name is being supplied. If so, we'll |
| 764 | // return a new instance of the object |
| 765 | if ( ! is_null($object_name)) |
| 766 | { |
| 767 | $CI =& get_instance(); |
| 768 | if ( ! isset($CI->$object_name)) |
| 769 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 770 | return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 771 | } |
| 772 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 773 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 774 | $is_duplicate = TRUE; |
| 775 | log_message('debug', $class." class already loaded. Second attempt ignored."); |
| 776 | return; |
| 777 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 778 | |
| 779 | include_once($baseclass); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 780 | include_once($subclass); |
| 781 | $this->_ci_loaded_files[] = $subclass; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 782 | |
| 783 | return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 784 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 785 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 786 | // Lets search for the requested library file and load it. |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 787 | $is_duplicate = FALSE; |
| 788 | foreach ($this->_ci_library_paths as $path) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 789 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 790 | $filepath = $path.'libraries/'.$subdir.$class.EXT; |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 791 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 792 | // Does the file exist? No? Bummer... |
| 793 | if ( ! file_exists($filepath)) |
| 794 | { |
| 795 | continue; |
| 796 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 797 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 798 | // Safety: Was the class already loaded by a previous call? |
| 799 | if (in_array($filepath, $this->_ci_loaded_files)) |
| 800 | { |
| 801 | // Before we deem this to be a duplicate request, let's see |
| 802 | // if a custom object name is being supplied. If so, we'll |
| 803 | // return a new instance of the object |
| 804 | if ( ! is_null($object_name)) |
| 805 | { |
| 806 | $CI =& get_instance(); |
| 807 | if ( ! isset($CI->$object_name)) |
| 808 | { |
| 809 | return $this->_ci_init_class($class, '', $params, $object_name); |
| 810 | } |
| 811 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 812 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 813 | $is_duplicate = TRUE; |
| 814 | log_message('debug', $class." class already loaded. Second attempt ignored."); |
| 815 | return; |
| 816 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 817 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 818 | include_once($filepath); |
| 819 | $this->_ci_loaded_files[] = $filepath; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 820 | return $this->_ci_init_class($class, '', $params, $object_name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 821 | } |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 822 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 823 | } // END FOREACH |
| 824 | |
| 825 | // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified? |
| 826 | if ($subdir == '') |
| 827 | { |
| 828 | $path = strtolower($class).'/'.$class; |
| 829 | return $this->_ci_load_class($path, $params); |
| 830 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 831 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 832 | // If we got this far we were unable to find the requested class. |
| 833 | // We do not issue errors if the load call failed due to a duplicate request |
| 834 | if ($is_duplicate == FALSE) |
| 835 | { |
| 836 | log_message('error', "Unable to load the requested class: ".$class); |
| 837 | show_error("Unable to load the requested class: ".$class); |
| 838 | } |
| 839 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 840 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 841 | // -------------------------------------------------------------------- |
| 842 | |
| 843 | /** |
| 844 | * Instantiates a class |
| 845 | * |
| 846 | * @access private |
| 847 | * @param string |
| 848 | * @param string |
| 849 | * @param string an optional object name |
| 850 | * @return null |
| 851 | */ |
| 852 | function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 853 | { |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 854 | // Is there an associated config file for this class? Note: these should always be lowercase |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 855 | if ($config === NULL) |
| 856 | { |
| 857 | // We test for both uppercase and lowercase, for servers that |
| 858 | // are case-sensitive with regard to file names |
| 859 | if (file_exists(APPPATH.'config/'.strtolower($class).EXT)) |
| 860 | { |
| 861 | include_once(APPPATH.'config/'.strtolower($class).EXT); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 862 | } |
Derek Jones | c8dddd9 | 2009-07-10 18:58:03 +0000 | [diff] [blame] | 863 | elseif (file_exists(APPPATH.'config/'.ucfirst(strtolower($class)).EXT)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 864 | { |
Derek Jones | c8dddd9 | 2009-07-10 18:58:03 +0000 | [diff] [blame] | 865 | include_once(APPPATH.'config/'.ucfirst(strtolower($class)).EXT); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 866 | } |
| 867 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 868 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 869 | if ($prefix == '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 870 | { |
| 871 | if (class_exists('CI_'.$class)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 872 | { |
| 873 | $name = 'CI_'.$class; |
| 874 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 875 | elseif (class_exists(config_item('subclass_prefix').$class)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 876 | { |
| 877 | $name = config_item('subclass_prefix').$class; |
| 878 | } |
| 879 | else |
| 880 | { |
| 881 | $name = $class; |
| 882 | } |
| 883 | } |
| 884 | else |
| 885 | { |
| 886 | $name = $prefix.$class; |
| 887 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 888 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 889 | // Is the class name valid? |
| 890 | if ( ! class_exists($name)) |
| 891 | { |
| 892 | log_message('error', "Non-existent class: ".$name); |
| 893 | show_error("Non-existent class: ".$class); |
| 894 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 895 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 896 | // Set the variable name we will assign the class to |
| 897 | // Was a custom class name supplied? If so we'll use it |
| 898 | $class = strtolower($class); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 899 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 900 | if (is_null($object_name)) |
| 901 | { |
| 902 | $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class]; |
| 903 | } |
| 904 | else |
| 905 | { |
| 906 | $classvar = $object_name; |
| 907 | } |
| 908 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 909 | // Save the class name and object name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 910 | $this->_ci_classes[$class] = $classvar; |
| 911 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 912 | // Instantiate the class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 913 | $CI =& get_instance(); |
| 914 | if ($config !== NULL) |
| 915 | { |
| 916 | $CI->$classvar = new $name($config); |
| 917 | } |
| 918 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 919 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 920 | $CI->$classvar = new $name; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 924 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 925 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 926 | /** |
| 927 | * Autoloader |
| 928 | * |
| 929 | * The config/autoload.php file contains an array that permits sub-systems, |
Derek Jones | c6da503 | 2010-03-09 20:44:27 -0600 | [diff] [blame] | 930 | * libraries, and helpers to be loaded automatically. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 931 | * |
| 932 | * @access private |
| 933 | * @param array |
| 934 | * @return void |
| 935 | */ |
| 936 | function _ci_autoloader() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 937 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 938 | include_once(APPPATH.'config/autoload'.EXT); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 939 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 940 | if ( ! isset($autoload)) |
| 941 | { |
| 942 | return FALSE; |
| 943 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 944 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 945 | // Load any custom config file |
| 946 | if (count($autoload['config']) > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 947 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 948 | $CI =& get_instance(); |
| 949 | foreach ($autoload['config'] as $key => $val) |
| 950 | { |
| 951 | $CI->config->load($val); |
| 952 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 953 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 954 | |
Derek Jones | c6da503 | 2010-03-09 20:44:27 -0600 | [diff] [blame] | 955 | // Autoload helpers and languages |
| 956 | foreach (array('helper', 'language') as $type) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 957 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 958 | if (isset($autoload[$type]) AND count($autoload[$type]) > 0) |
| 959 | { |
| 960 | $this->$type($autoload[$type]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 961 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | // A little tweak to remain backward compatible |
| 965 | // The $autoload['core'] item was deprecated |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 966 | if ( ! isset($autoload['libraries']) AND isset($autoload['core'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 967 | { |
| 968 | $autoload['libraries'] = $autoload['core']; |
| 969 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 970 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 971 | // Load libraries |
| 972 | if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0) |
| 973 | { |
| 974 | // Load the database driver. |
| 975 | if (in_array('database', $autoload['libraries'])) |
| 976 | { |
| 977 | $this->database(); |
| 978 | $autoload['libraries'] = array_diff($autoload['libraries'], array('database')); |
| 979 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 980 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 981 | // Load all other libraries |
| 982 | foreach ($autoload['libraries'] as $item) |
| 983 | { |
| 984 | $this->library($item); |
| 985 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 986 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 987 | |
| 988 | // Autoload models |
| 989 | if (isset($autoload['model'])) |
| 990 | { |
| 991 | $this->model($autoload['model']); |
| 992 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 993 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 994 | |
| 995 | // -------------------------------------------------------------------- |
| 996 | |
| 997 | /** |
| 998 | * Object to Array |
| 999 | * |
| 1000 | * Takes an object as input and converts the class variables to array key/vals |
| 1001 | * |
| 1002 | * @access private |
| 1003 | * @param object |
| 1004 | * @return array |
| 1005 | */ |
| 1006 | function _ci_object_to_array($object) |
| 1007 | { |
| 1008 | return (is_object($object)) ? get_object_vars($object) : $object; |
| 1009 | } |
| 1010 | |
| 1011 | // -------------------------------------------------------------------- |
| 1012 | |
| 1013 | /** |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 1014 | * Get a reference to a specific library or model |
| 1015 | * |
| 1016 | * @access private |
| 1017 | * @return bool |
| 1018 | */ |
| 1019 | function &_ci_get_component($component) |
| 1020 | { |
Pascal Kriete | 89ace43 | 2010-11-10 15:49:10 -0500 | [diff] [blame] | 1021 | $CI =& get_instance(); |
| 1022 | return $CI->$component; |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1026 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 1027 | /** |
| 1028 | * Prep filename |
| 1029 | * |
| 1030 | * This function preps the name of various items to make loading them more reliable. |
| 1031 | * |
| 1032 | * @access private |
| 1033 | * @param mixed |
| 1034 | * @return array |
| 1035 | */ |
| 1036 | function _ci_prep_filename($filename, $extension) |
| 1037 | { |
| 1038 | if ( ! is_array($filename)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1039 | { |
| 1040 | return array(strtolower(str_replace(EXT, '', str_replace($extension, '', $filename)).$extension)); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 1041 | } |
| 1042 | else |
| 1043 | { |
| 1044 | foreach ($filename as $key => $val) |
| 1045 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1046 | $filename[$key] = strtolower(str_replace(EXT, '', str_replace($extension, '', $val)).$extension); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 1047 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1048 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 1049 | return $filename; |
| 1050 | } |
| 1051 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1052 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1053 | |
| 1054 | } |
| 1055 | |
| 1056 | /* End of file Loader.php */ |
Derek Jones | c68dfbf | 2010-03-02 12:59:23 -0600 | [diff] [blame] | 1057 | /* Location: ./system/core/Loader.php */ |