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