blob: 4d0cd09a229c23c0cf09f7584d27d5987a61e08e [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevd7297352012-01-07 22:53:14 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevd7297352012-01-07 22:53:14 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
Derek Allard2067d1a2008-11-13 22:59:24 +000018 *
19 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Loader Class
31 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030032 * Loads framework components.
Derek Allard2067d1a2008-11-13 22:59:24 +000033 *
34 * @package CodeIgniter
35 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @category Loader
Andrey Andreev92ebfb62012-05-17 12:49:24 +030037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/loader.html
39 */
40class CI_Loader {
41
42 // All these are set automatically. Don't mess with them.
David Behlercda768a2011-08-14 23:52:48 +020043 /**
44 * Nesting level of the output buffering mechanism
45 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030046 * @var int
David Behlercda768a2011-08-14 23:52:48 +020047 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050048 protected $_ci_ob_level;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030049
David Behlercda768a2011-08-14 23:52:48 +020050 /**
51 * List of paths to load views from
52 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030053 * @var array
David Behlercda768a2011-08-14 23:52:48 +020054 */
Ted Wood4c223642013-01-05 16:50:31 -080055 protected $_ci_view_paths = array(VIEWPATH => TRUE);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030056
David Behlercda768a2011-08-14 23:52:48 +020057 /**
58 * List of paths to load libraries from
59 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030060 * @var array
David Behlercda768a2011-08-14 23:52:48 +020061 */
Ted Wood4c223642013-01-05 16:50:31 -080062 protected $_ci_library_paths = array(APPPATH, BASEPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030063
David Behlercda768a2011-08-14 23:52:48 +020064 /**
65 * List of paths to load models from
66 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030067 * @var array
David Behlercda768a2011-08-14 23:52:48 +020068 */
Ted Wood4c223642013-01-05 16:50:31 -080069 protected $_ci_model_paths = array(APPPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030070
David Behlercda768a2011-08-14 23:52:48 +020071 /**
72 * List of paths to load helpers from
73 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030074 * @var array
David Behlercda768a2011-08-14 23:52:48 +020075 */
Ted Wood4c223642013-01-05 16:50:31 -080076 protected $_ci_helper_paths = array(APPPATH, BASEPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030077
David Behlercda768a2011-08-14 23:52:48 +020078 /**
79 * List of loaded base classes
David Behlercda768a2011-08-14 23:52:48 +020080 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030081 * @var array
David Behlercda768a2011-08-14 23:52:48 +020082 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040083 protected $_base_classes = array(); // Set by the controller class
Andrey Andreev92ebfb62012-05-17 12:49:24 +030084
David Behlercda768a2011-08-14 23:52:48 +020085 /**
86 * List of cached variables
87 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030088 * @var array
David Behlercda768a2011-08-14 23:52:48 +020089 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040090 protected $_ci_cached_vars = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030091
David Behlercda768a2011-08-14 23:52:48 +020092 /**
93 * List of loaded classes
94 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030095 * @var array
David Behlercda768a2011-08-14 23:52:48 +020096 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040097 protected $_ci_classes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030098
David Behlercda768a2011-08-14 23:52:48 +020099 /**
David Behlercda768a2011-08-14 23:52:48 +0200100 * List of loaded models
101 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300102 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200103 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400104 protected $_ci_models = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300105
David Behlercda768a2011-08-14 23:52:48 +0200106 /**
107 * List of loaded helpers
108 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300109 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200110 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400111 protected $_ci_helpers = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300112
David Behlercda768a2011-08-14 23:52:48 +0200113 /**
114 * List of class name mappings
115 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300116 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200117 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400118 protected $_ci_varmap = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400119 'unit_test' => 'unit',
120 'user_agent' => 'agent'
121 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000122
123 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300124 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 *
Andrey Andreevcdac2482012-11-03 18:09:01 +0200126 * Sets component load paths, gets the initial output buffering level.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300127 *
128 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 */
Greg Akerf5c84022011-04-19 17:13:03 -0500130 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +0200131 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500132 $this->_ci_ob_level = ob_get_level();
David Behlercda768a2011-08-14 23:52:48 +0200133
Andrey Andreevd7297352012-01-07 22:53:14 +0200134 log_message('debug', 'Loader Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 }
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200138
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500139 /**
Andrey Andreevcdac2482012-11-03 18:09:01 +0200140 * Initializer
141 *
142 * @todo Figure out a way to move this to the constructor
143 * without breaking *package_path*() methods.
144 * @uses CI_Loader::_ci_autoloader()
145 * @used-by CI_Controller::__construct()
146 * @return void
147 */
148 public function initialize()
149 {
150 $this->_base_classes =& is_loaded();
151 $this->_ci_autoloader();
152 }
153
154 // --------------------------------------------------------------------
155
156 /**
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500157 * Is Loaded
158 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300159 * A utility method to test if a class is in the self::$_ci_classes array.
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500160 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300161 * @used-by Mainly used by Form Helper function _get_validation_object().
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500162 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300163 * @param string $class Class name to check for
164 * @return string|bool Class object name if loaded or FALSE
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500165 */
166 public function is_loaded($class)
167 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300168 return isset($this->_ci_classes[$class]) ? $this->_ci_classes[$class] : FALSE;
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500169 }
170
171 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300174 * Library Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300176 * Loads and instantiates libraries.
177 * Designed to be called from application controllers.
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300179 * @param string $library Library name
180 * @param array $params Optional parameters to pass to the library class constructor
181 * @param string $object_name An optional object name to assign to
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200183 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500184 public function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Greg Akerce433962010-10-12 09:29:35 -0500186 if (is_array($library))
187 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100188 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500189 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600190 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500191 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000192
Greg Akerce433962010-10-12 09:29:35 -0500193 return;
194 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000195
Alex Bilbieed944a32012-06-02 11:07:47 +0100196 if ($library === '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300198 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
200
vlakoff1228fe22013-01-14 01:30:09 +0100201 if ($params !== NULL && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
203 $params = NULL;
204 }
205
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600206 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
208
209 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 /**
212 * Model Loader
213 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300214 * Loads and instantiates libraries.
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300216 * @param string $model Model name
217 * @param string $name An optional object name to assign to
218 * @param bool $db_conn An optional database connection configuration to initialize
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200220 */
Greg Akerf5c84022011-04-19 17:13:03 -0500221 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200222 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300223 if (empty($model))
224 {
225 return;
226 }
227 elseif (is_array($model))
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
Andrey Andreev5a519db2013-01-12 04:19:19 +0200229 foreach ($model as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
Andrey Andreev5a519db2013-01-12 04:19:19 +0200231 $this->model(is_int($key) ? $value : $key, $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
233 return;
234 }
235
Derek Jones32bf1862010-03-02 13:46:07 -0600236 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600239 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
Derek Jones32bf1862010-03-02 13:46:07 -0600241 // The path is in front of the last slash
Andrey Andreevd47baab2012-01-09 16:56:46 +0200242 $path = substr($model, 0, ++$last_slash);
Derek Jones32bf1862010-03-02 13:46:07 -0600243
244 // And the model name behind it
Andrey Andreevd47baab2012-01-09 16:56:46 +0200245 $model = substr($model, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Phil Sturgeon10d78f62012-06-04 14:41:53 -0500248 if (empty($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
250 $name = $model;
251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 if (in_array($name, $this->_ci_models, TRUE))
254 {
255 return;
256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 $CI =& get_instance();
259 if (isset($CI->$name))
260 {
261 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Andrey Andreev20292312013-07-22 14:29:10 +0300264 if ($db_conn !== FALSE && ! class_exists('CI_DB', FALSE))
265 {
266 if ($db_conn === TRUE)
267 {
268 $db_conn = '';
269 }
270
271 $CI->load->database($db_conn, FALSE, TRUE);
272 }
273
274 if ( ! class_exists('CI_Model', FALSE))
275 {
276 load_class('Model', 'core');
277 }
278
279 $model = ucfirst(strtolower($model));
Derek Allard2067d1a2008-11-13 22:59:24 +0000280
Derek Jones32bf1862010-03-02 13:46:07 -0600281 foreach ($this->_ci_model_paths as $mod_path)
282 {
Greg Aker3a746652011-04-19 10:59:47 -0500283 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600284 {
285 continue;
286 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000287
Greg Aker3a746652011-04-19 10:59:47 -0500288 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600289
Derek Jones32bf1862010-03-02 13:46:07 -0600290 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600291 $this->_ci_models[] = $name;
292 return;
293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Jones32bf1862010-03-02 13:46:07 -0600295 // couldn't find the model
296 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 /**
302 * Database Loader
303 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300304 * @param mixed $params Database configuration options
305 * @param bool $return Whether to return the database object
306 * @param bool $query_builder Whether to enable Query Builder
307 * (overrides the configuration setting)
308 *
309 * @return void|object|bool Database object if $return is set to TRUE,
310 * FALSE on failure, void in any other case
Barry Mienydd671972010-10-04 16:33:58 +0200311 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000312 public function database($params = '', $return = FALSE, $query_builder = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
314 // Grab the super object
315 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // Do we even need to load the database class?
Andrey Andreev9d0ab042012-10-24 21:47:39 +0300318 if ($return === FALSE && $query_builder === NULL && isset($CI->db) && is_object($CI->db) && ! empty($CI->db->conn_id))
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
320 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200321 }
322
Greg Aker3a746652011-04-19 10:59:47 -0500323 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000324
325 if ($return === TRUE)
326 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000327 return DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Andrey Andreevd7297352012-01-07 22:53:14 +0200330 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // reference errors with some configurations
332 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // Load the DB class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000335 $CI->db =& DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // --------------------------------------------------------------------
339
340 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300341 * Load the Database Utilities Class
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200343 * @param object $db Database object
344 * @param bool $return Whether to return the DB Forge class object or not
345 * @return void|object
Barry Mienydd671972010-10-04 16:33:58 +0200346 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200347 public function dbutil($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 $CI =& get_instance();
350
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200351 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
352 {
353 class_exists('CI_DB', FALSE) OR $this->database();
354 $db =& $CI->db;
355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Greg Aker3a746652011-04-19 10:59:47 -0500357 require_once(BASEPATH.'database/DB_utility.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200358 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_utility.php');
359 $class = 'CI_DB_'.$db->dbdriver.'_utility';
Derek Allard2067d1a2008-11-13 22:59:24 +0000360
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200361 if ($return === TRUE)
362 {
363 return new $class($db);
364 }
365
366 $CI->dbutil = new $class($db);
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // --------------------------------------------------------------------
370
371 /**
372 * Load the Database Forge Class
373 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200374 * @param object $db Database object
375 * @param bool $return Whether to return the DB Forge class object or not
376 * @return void|object
Barry Mienydd671972010-10-04 16:33:58 +0200377 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200378 public function dbforge($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200380 $CI =& get_instance();
381 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200383 class_exists('CI_DB', FALSE) OR $this->database();
384 $db =& $CI->db;
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Greg Aker3a746652011-04-19 10:59:47 -0500387 require_once(BASEPATH.'database/DB_forge.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200388 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_forge.php');
Andrey Andreeva287a342012-11-05 23:19:59 +0200389
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200390 if ( ! empty($db->subdriver))
Andrey Andreeva287a342012-11-05 23:19:59 +0200391 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200392 $driver_path = BASEPATH.'database/drivers/'.$db->dbdriver.'/subdrivers/'.$db->dbdriver.'_'.$db->subdriver.'_forge.php';
Andrey Andreeva287a342012-11-05 23:19:59 +0200393 if (file_exists($driver_path))
394 {
395 require_once($driver_path);
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200396 $class = 'CI_DB_'.$db->dbdriver.'_'.$db->subdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200397 }
398 }
399 else
400 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200401 $class = 'CI_DB_'.$db->dbdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200402 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000403
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200404 if ($return === TRUE)
405 {
406 return new $class($db);
407 }
408
409 $CI->dbforge = new $class($db);
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300415 * View Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300417 * Loads "view" files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300419 * @param string $view View name
420 * @param array $vars An associative array of data
421 * to be extracted for use in the view
422 * @param bool $return Whether to return the view output
423 * or leave it to the Output class
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 * @return void
425 */
Greg Akerf5c84022011-04-19 17:13:03 -0500426 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
428 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300434 * Generic File Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300436 * @param string $path File path
437 * @param bool $return Whether to return the file output
438 * @return void|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 */
Greg Akerf5c84022011-04-19 17:13:03 -0500440 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
442 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
443 }
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 /**
448 * Set Variables
449 *
450 * Once variables are set they become available within
451 * the controller class and its "view" files.
452 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300453 * @param array|object|string $vars
454 * An associative array or object containing values
455 * to be set, or a value's name if string
456 * @param string $val Value to set, only used if $vars is a string
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 * @return void
458 */
Greg Akerf5c84022011-04-19 17:13:03 -0500459 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
vlakoffa5e0ea82013-02-27 18:17:35 +0100461 if (is_string($vars))
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
463 $vars = array($vars => $val);
464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200467
Andrey Andreev94af3552012-03-26 23:10:42 +0300468 if (is_array($vars) && count($vars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
470 foreach ($vars as $key => $val)
471 {
472 $this->_ci_cached_vars[$key] = $val;
473 }
474 }
475 }
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600480 * Get Variable
481 *
482 * Check if a variable is set and retrieve it.
483 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300484 * @param string $key Variable name
485 * @return mixed The variable or NULL if not found
Phil Sturgeon8731f642011-07-22 16:11:34 -0600486 */
487 public function get_var($key)
488 {
489 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
490 }
491
492 // --------------------------------------------------------------------
493
494 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600495 * Get Variables
496 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300497 * Retrieves all loaded variables.
Shane Pearson81dd2232011-11-18 20:49:35 -0600498 *
499 * @return array
500 */
501 public function get_vars()
502 {
503 return $this->_ci_cached_vars;
504 }
505
506 // --------------------------------------------------------------------
507
508 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300509 * Helper Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300511 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 * @return void
513 */
Greg Akerf5c84022011-04-19 17:13:03 -0500514 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200515 {
Derek Jones32bf1862010-03-02 13:46:07 -0600516 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200517 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 if (isset($this->_ci_helpers[$helper]))
519 {
520 continue;
521 }
Derek Jones32bf1862010-03-02 13:46:07 -0600522
Barry Mienydd671972010-10-04 16:33:58 +0200523 // Is this a helper extension request?
Andrey Andreev12d7b462012-11-12 13:42:09 +0200524 $ext_helper = config_item('subclass_prefix').$helper;
525 $ext_loaded = FALSE;
526 foreach ($this->_ci_helper_paths as $path)
527 {
528 if (file_exists($path.'helpers/'.$ext_helper.'.php'))
529 {
530 include_once($path.'helpers/'.$ext_helper.'.php');
531 $ext_loaded = TRUE;
532 }
533 }
534
535 // If we have loaded extensions - check if the base one is here
536 if ($ext_loaded === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 {
Greg Aker3a746652011-04-19 10:59:47 -0500538 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 if ( ! file_exists($base_helper))
540 {
Greg Aker3a746652011-04-19 10:59:47 -0500541 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 }
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 include_once($base_helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600545 $this->_ci_helpers[$helper] = TRUE;
546 log_message('debug', 'Helper loaded: '.$helper);
547 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Andrey Andreev12d7b462012-11-12 13:42:09 +0200550 // No extensions found ... try loading regular helpers and/or overrides
Derek Jones32bf1862010-03-02 13:46:07 -0600551 foreach ($this->_ci_helper_paths as $path)
552 {
Greg Aker3a746652011-04-19 10:59:47 -0500553 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200554 {
Greg Aker3a746652011-04-19 10:59:47 -0500555 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600556
557 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200558 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600559 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 }
561 }
562
Derek Jones32bf1862010-03-02 13:46:07 -0600563 // unable to load the helper
564 if ( ! isset($this->_ci_helpers[$helper]))
565 {
Greg Aker3a746652011-04-19 10:59:47 -0500566 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600567 }
Barry Mienydd671972010-10-04 16:33:58 +0200568 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 }
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 /**
574 * Load Helpers
575 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300576 * An alias for the helper() method in case the developer has
577 * written the plural form of it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300579 * @uses CI_Loader::helper()
580 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 * @return void
582 */
Greg Akerf5c84022011-04-19 17:13:03 -0500583 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
585 $this->helper($helpers);
586 }
Barry Mienydd671972010-10-04 16:33:58 +0200587
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300591 * Language Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300593 * Loads language files.
594 *
595 * @param string|string[] $files List of language file names to load
596 * @param string Language name
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 * @return void
598 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300599 public function language($files = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
601 $CI =& get_instance();
602
Andrey Andreeved4b2582012-10-27 17:46:52 +0300603 is_array($files) OR $files = array($files);
Derek Allard2067d1a2008-11-13 22:59:24 +0000604
Andrey Andreeved4b2582012-10-27 17:46:52 +0300605 foreach ($files as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200606 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 $CI->lang->load($langfile, $lang);
608 }
609 }
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300614 * Config Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300616 * Loads a config file (an alias for CI_Config::load()).
617 *
618 * @uses CI_Config::load()
619 * @param string $file Configuration file name
620 * @param bool $use_sections Whether configuration values should be loaded into their own section
621 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
622 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 */
Greg Akerf5c84022011-04-19 17:13:03 -0500624 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200625 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 $CI =& get_instance();
Andrey Andreeved4b2582012-10-27 17:46:52 +0300627 return $CI->config->load($file, $use_sections, $fail_gracefully);
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 }
629
630 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300633 * Driver Loader
Derek Jones8dca0412010-03-05 13:01:44 -0600634 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300635 * Loads a driver library.
Derek Jones8dca0412010-03-05 13:01:44 -0600636 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300637 * @param string|string[] $library Driver name(s)
638 * @param array $params Optional parameters to pass to the driver
639 * @param string $object_name An optional object name to assign to
640 *
641 * @return void|object|bool Object or FALSE on failure if $library is a string
642 * and $object_name is set. void otherwise.
Derek Jones8dca0412010-03-05 13:01:44 -0600643 */
Greg Akerf5c84022011-04-19 17:13:03 -0500644 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600645 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800646 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800647 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800648 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800649 {
650 $this->driver($driver);
651 }
dchill420fc3be52012-08-27 20:54:23 -0400652 return;
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800653 }
654
Alex Bilbieed944a32012-06-02 11:07:47 +0100655 if ($library === '')
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200656 {
657 return FALSE;
658 }
659
vlakofffadb8222013-05-12 10:57:09 +0200660 if ( ! class_exists('CI_Driver_Library', FALSE))
dchill426262d052012-11-24 18:41:13 -0500661 {
662 // We aren't instantiating an object here, just making the base class available
663 require BASEPATH.'libraries/Driver.php';
664 }
665
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600666 // We can save the loader some time since Drivers will *always* be in a subfolder,
667 // and typically identically named to the library
668 if ( ! strpos($library, '/'))
669 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500670 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600671 }
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Jones8dca0412010-03-05 13:01:44 -0600673 return $this->library($library, $params, $object_name);
674 }
675
676 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200677
Derek Jones8dca0412010-03-05 13:01:44 -0600678 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600679 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300681 * Prepends a parent path to the library, model, helper and config
682 * path arrays.
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300684 * @see CI_Loader::$_ci_library_paths
685 * @see CI_Loader::$_ci_model_paths
686 * @see CI_Loader::$_ci_helper_paths
687 * @see CI_Config::$_config_paths
688 *
689 * @param string $path Path to add
690 * @param bool $view_cascade (default: TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600692 */
Andrey Andreevcce91802012-06-12 13:25:31 +0300693 public function add_package_path($path, $view_cascade = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600694 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500695 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200696
Derek Jones32bf1862010-03-02 13:46:07 -0600697 array_unshift($this->_ci_library_paths, $path);
698 array_unshift($this->_ci_model_paths, $path);
699 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200700
Greg Akerf5c84022011-04-19 17:13:03 -0500701 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
702
Derek Jones32bf1862010-03-02 13:46:07 -0600703 // Add config file path
704 $config =& $this->_ci_get_component('config');
and-ersb3ec9422013-01-03 16:05:12 +0100705 $config->_config_paths[] = $path;
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 }
707
708 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600709
710 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000711 * Get Package Paths
712 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300713 * Return a list of all package paths.
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000714 *
vlakoffcdc61132013-05-10 16:47:47 +0200715 * @param bool $include_base Whether to include BASEPATH (default: FALSE)
Andrey Andreeved4b2582012-10-27 17:46:52 +0300716 * @return array
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000717 */
Greg Akerf5c84022011-04-19 17:13:03 -0500718 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000719 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300720 return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000721 }
722
723 // --------------------------------------------------------------------
724
725 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600726 * Remove Package Path
727 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300728 * Remove a path from the library, model, helper and/or config
729 * path arrays if it exists. If no path is provided, the most recently
730 * added path will be removed removed.
Derek Jones32bf1862010-03-02 13:46:07 -0600731 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300732 * @param string $path Path to remove
Andrey Andreev94af3552012-03-26 23:10:42 +0300733 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600734 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300735 public function remove_package_path($path = '')
Derek Jones32bf1862010-03-02 13:46:07 -0600736 {
737 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200738
Alex Bilbieed944a32012-06-02 11:07:47 +0100739 if ($path === '')
Derek Jones32bf1862010-03-02 13:46:07 -0600740 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200741 array_shift($this->_ci_library_paths);
742 array_shift($this->_ci_model_paths);
743 array_shift($this->_ci_helper_paths);
744 array_shift($this->_ci_view_paths);
Korri3684d342012-04-17 00:35:08 -0400745 array_pop($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600746 }
747 else
748 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500749 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600750 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
751 {
752 if (($key = array_search($path, $this->{$var})) !== FALSE)
753 {
754 unset($this->{$var}[$key]);
755 }
756 }
David Behlercda768a2011-08-14 23:52:48 +0200757
Greg Akerf5c84022011-04-19 17:13:03 -0500758 if (isset($this->_ci_view_paths[$path.'views/']))
759 {
760 unset($this->_ci_view_paths[$path.'views/']);
761 }
Barry Mienydd671972010-10-04 16:33:58 +0200762
Derek Jones32bf1862010-03-02 13:46:07 -0600763 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
764 {
765 unset($config->_config_paths[$key]);
766 }
767 }
Barry Mienydd671972010-10-04 16:33:58 +0200768
Derek Jones32bf1862010-03-02 13:46:07 -0600769 // make sure the application default paths are still in the array
770 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
771 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
772 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500773 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600774 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
775 }
776
777 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200778
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300780 * Internal CI Data Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300782 * Used to load views and files.
783 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 * Variables are prefixed with _ci_ to avoid symbol collision with
Andrey Andreeved4b2582012-10-27 17:46:52 +0300785 * variables made available to view files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300787 * @used-by CI_Loader::view()
788 * @used-by CI_Loader::file()
789 * @param array $_ci_data Data to load
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 * @return void
791 */
Greg Akerf5c84022011-04-19 17:13:03 -0500792 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 {
794 // Set the default data variables
795 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
796 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300797 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 }
David Behlercda768a2011-08-14 23:52:48 +0200799
Greg Akerf5c84022011-04-19 17:13:03 -0500800 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000801
802 // Set the path to the requested file
Alex Bilbie40bd2a72012-06-02 16:04:15 +0100803 if (is_string($_ci_path) && $_ci_path !== '')
Greg Aker8807be32011-04-21 13:06:15 -0500804 {
805 $_ci_x = explode('/', $_ci_path);
806 $_ci_file = end($_ci_x);
807 }
808 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 {
810 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Alex Bilbieed944a32012-06-02 11:07:47 +0100811 $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500812
Joe McFrederick64f470b2012-08-18 12:29:56 -0400813 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
Greg Akerf5c84022011-04-19 17:13:03 -0500814 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400815 if (file_exists($_ci_view_file.$_ci_file))
Greg Akerf5c84022011-04-19 17:13:03 -0500816 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400817 $_ci_path = $_ci_view_file.$_ci_file;
Greg Akerf5c84022011-04-19 17:13:03 -0500818 $file_exists = TRUE;
819 break;
820 }
David Behlercda768a2011-08-14 23:52:48 +0200821
Greg Akerf5c84022011-04-19 17:13:03 -0500822 if ( ! $cascade)
823 {
824 break;
David Behlercda768a2011-08-14 23:52:48 +0200825 }
Greg Akerf5c84022011-04-19 17:13:03 -0500826 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 }
Barry Mienydd671972010-10-04 16:33:58 +0200828
Greg Akerf5c84022011-04-19 17:13:03 -0500829 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 {
831 show_error('Unable to load the requested file: '.$_ci_file);
832 }
Barry Mienydd671972010-10-04 16:33:58 +0200833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 // This allows anything loaded using $this->load (views, files, etc.)
835 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500836 $_ci_CI =& get_instance();
837 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500839 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500841 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 }
843 }
844
845 /*
846 * Extract and cache variables
847 *
vlakoff02506182012-07-03 07:28:50 +0200848 * You can either set variables using the dedicated $this->load->vars()
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 * function or via the second parameter of this function. We'll merge
850 * the two types and cache them so that views that are embedded within
851 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200852 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 if (is_array($_ci_vars))
854 {
855 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
856 }
857 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200858
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 /*
860 * Buffer the output
861 *
862 * We buffer the output for two reasons:
863 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200864 * 2. So that the final rendered template can be post-processed by
dchill425628ba02012-08-08 12:05:45 -0400865 * the output class. Why do we need post processing? For one thing,
866 * in order to show the elapsed page load time. Unless we can
867 * intercept the content right before it's sent to the browser and
868 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 */
870 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200871
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 // If the PHP installation does not support short tags we'll
873 // do a little string replacement, changing the short tags
874 // to standard PHP echo statements.
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200875 if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE
876 && config_item('rewrite_short_tags') === TRUE && function_usable('eval')
877 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200879 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 }
881 else
882 {
883 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
884 }
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200887
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 // Return the file data if requested
889 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200890 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 $buffer = ob_get_contents();
892 @ob_end_clean();
893 return $buffer;
894 }
895
896 /*
897 * Flush the buffer... or buff the flusher?
898 *
899 * In order to permit views to be nested within
900 * other views, we need to flush the content back out whenever
901 * we are beyond the first level of output buffering so that
902 * it can be seen and included properly by the first included
903 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200904 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 if (ob_get_level() > $this->_ci_ob_level + 1)
906 {
907 ob_end_flush();
908 }
909 else
910 {
Greg Aker22f1a632010-11-10 15:34:35 -0600911 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 @ob_end_clean();
913 }
914 }
915
916 // --------------------------------------------------------------------
917
918 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300919 * Internal CI Class Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300921 * @used-by CI_Loader::library()
922 * @uses CI_Loader::_ci_init_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300924 * @param string $class Class name to load
925 * @param mixed $params Optional parameters to pass to the class constructor
926 * @param string $object_name Optional object name to assign to
Barry Mienydd671972010-10-04 16:33:58 +0200927 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 */
Greg Akerf5c84022011-04-19 17:13:03 -0500929 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200930 {
931 // Get the class name, and while we're at it trim any slashes.
932 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500934 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 // Was the path included with the class name?
937 // We look for a slash to determine this
Derek Jones32bf1862010-03-02 13:46:07 -0600938 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
Derek Jones32bf1862010-03-02 13:46:07 -0600940 // Extract the path
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200941 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200942
Derek Jones32bf1862010-03-02 13:46:07 -0600943 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200944 $class = substr($class, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200946 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200948 $subdir = '';
949 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000950
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200951 $class = ucfirst($class);
952 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
953
954 // Is this a class extension request?
955 if (file_exists($subclass))
956 {
Daniel1d3752c2013-04-14 16:41:57 -0400957 $baseclass = BASEPATH.'libraries/'.$subdir.$class.'.php';
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200958
959 if ( ! file_exists($baseclass))
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200961 log_message('error', 'Unable to load the requested class: '.$class);
962 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 }
Barry Mienydd671972010-10-04 16:33:58 +0200964
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200965 // Safety: Was the class already loaded by a previous call?
966 if (class_exists(config_item('subclass_prefix').$class, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200968 // Before we deem this to be a duplicate request, let's see
969 // if a custom object name is being supplied. If so, we'll
970 // return a new instance of the object
971 if ($object_name !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200973 $CI =& get_instance();
974 if ( ! isset($CI->$object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200976 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 }
Barry Mienydd671972010-10-04 16:33:58 +0200979
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200980 log_message('debug', $class.' class already loaded. Second attempt ignored.');
981 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 }
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200983
984 include_once($baseclass);
985 include_once($subclass);
986
987 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
988 }
989
vlakoff35672462013-02-15 01:36:04 +0100990 // Let's search for the requested library file and load it.
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200991 foreach ($this->_ci_library_paths as $path)
992 {
993 $filepath = $path.'libraries/'.$subdir.$class.'.php';
994
995 // Safety: Was the class already loaded by a previous call?
996 if (class_exists($class, FALSE))
997 {
998 // Before we deem this to be a duplicate request, let's see
999 // if a custom object name is being supplied. If so, we'll
1000 // return a new instance of the object
1001 if ($object_name !== NULL)
1002 {
1003 $CI =& get_instance();
1004 if ( ! isset($CI->$object_name))
1005 {
1006 return $this->_ci_init_class($class, '', $params, $object_name);
1007 }
1008 }
1009
1010 log_message('debug', $class.' class already loaded. Second attempt ignored.');
1011 return;
1012 }
1013 // Does the file exist? No? Bummer...
1014 elseif ( ! file_exists($filepath))
1015 {
1016 continue;
1017 }
1018
1019 include_once($filepath);
1020 return $this->_ci_init_class($class, '', $params, $object_name);
1021 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001022
Andrey Andreevd7297352012-01-07 22:53:14 +02001023 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Alex Bilbieed944a32012-06-02 11:07:47 +01001024 if ($subdir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001026 return $this->_ci_load_class($class.'/'.$class, $params, $object_name);
dchill42aee92652012-08-26 21:45:35 -04001027 }
Barry Mienydd671972010-10-04 16:33:58 +02001028
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 // If we got this far we were unable to find the requested class.
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001030 log_message('error', 'Unable to load the requested class: '.$class);
1031 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 }
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 // --------------------------------------------------------------------
1035
1036 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001037 * Internal CI Class Instantiator
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001039 * @used-by CI_Loader::_ci_load_class()
1040 *
1041 * @param string $class Class name
1042 * @param string $prefix Class name prefix
1043 * @param array|null|bool $config Optional configuration to pass to the class constructor:
1044 * FALSE to skip;
1045 * NULL to search in config paths;
1046 * array containing configuration data
1047 * @param string $object_name Optional object name to assign to
Andrey Andreev94af3552012-03-26 23:10:42 +03001048 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001050 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001051 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001052 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 if ($config === NULL)
1054 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001055 // Fetch the config paths containing any package paths
1056 $config_component = $this->_ci_get_component('config');
1057
1058 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001060 // Break on the first found file, thus package files
1061 // are not overridden by default paths
1062 foreach ($config_component->_config_paths as $path)
1063 {
1064 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001065 // are case-sensitive with regard to file names. Check for environment
1066 // first, global next
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001067 if (file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001068 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001069 include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001070 break;
1071 }
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001072 elseif (file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001073 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001074 include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001075 break;
1076 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001077 elseif (file_exists($path.'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001078 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001079 include($path.'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001080 break;
1081 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001082 elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001083 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001084 include($path.'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001085 break;
1086 }
1087 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 }
1089 }
Barry Mienydd671972010-10-04 16:33:58 +02001090
Alex Bilbieed944a32012-06-02 11:07:47 +01001091 if ($prefix === '')
Barry Mienydd671972010-10-04 16:33:58 +02001092 {
Andrey Andreev49e68de2013-02-21 16:30:55 +02001093 if (class_exists('CI_'.$class, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 {
1095 $name = 'CI_'.$class;
1096 }
Andrey Andreev49e68de2013-02-21 16:30:55 +02001097 elseif (class_exists(config_item('subclass_prefix').$class, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
1099 $name = config_item('subclass_prefix').$class;
1100 }
1101 else
1102 {
1103 $name = $class;
1104 }
1105 }
1106 else
1107 {
1108 $name = $prefix.$class;
1109 }
Barry Mienydd671972010-10-04 16:33:58 +02001110
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 // Is the class name valid?
Andrey Andreev49e68de2013-02-21 16:30:55 +02001112 if ( ! class_exists($name, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001114 log_message('error', 'Non-existent class: '.$name);
jonnueee2df62012-07-16 13:06:16 +01001115 show_error('Non-existent class: '.$name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 }
Barry Mienydd671972010-10-04 16:33:58 +02001117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001119 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001121
vlakoff1228fe22013-01-14 01:30:09 +01001122 if ($object_name === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001124 $classvar = isset($this->_ci_varmap[$class]) ? $this->_ci_varmap[$class] : $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 }
1126 else
1127 {
1128 $classvar = $object_name;
1129 }
1130
Barry Mienydd671972010-10-04 16:33:58 +02001131 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 $this->_ci_classes[$class] = $classvar;
1133
Barry Mienydd671972010-10-04 16:33:58 +02001134 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 $CI =& get_instance();
1136 if ($config !== NULL)
1137 {
1138 $CI->$classvar = new $name($config);
1139 }
1140 else
Barry Mienydd671972010-10-04 16:33:58 +02001141 {
Andrey Andreeva11b16b2012-03-28 12:22:04 +03001142 $CI->$classvar = new $name();
Barry Mienydd671972010-10-04 16:33:58 +02001143 }
1144 }
1145
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001147
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001149 * CI Autoloader
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001151 * Loads component listed in the config/autoload.php file.
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 *
Andrey Andreevcdac2482012-11-03 18:09:01 +02001153 * @used-by CI_Loader::initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 * @return void
1155 */
Shane Pearson665baec2011-08-22 18:52:19 -05001156 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001157 {
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001158 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001159 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001160 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001161 }
1162 else
1163 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001164 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001165 }
Barry Mienydd671972010-10-04 16:33:58 +02001166
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 if ( ! isset($autoload))
1168 {
1169 return FALSE;
1170 }
Barry Mienydd671972010-10-04 16:33:58 +02001171
Phil Sturgeon9730c752010-12-15 10:50:15 +00001172 // Autoload packages
1173 if (isset($autoload['packages']))
1174 {
1175 foreach ($autoload['packages'] as $package_path)
1176 {
1177 $this->add_package_path($package_path);
1178 }
1179 }
1180
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 // Load any custom config file
1182 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001183 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 $CI =& get_instance();
1185 foreach ($autoload['config'] as $key => $val)
1186 {
1187 $CI->config->load($val);
1188 }
Barry Mienydd671972010-10-04 16:33:58 +02001189 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001190
Derek Jonesc6da5032010-03-09 20:44:27 -06001191 // Autoload helpers and languages
1192 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001193 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001194 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 {
1196 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001197 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 }
1199
Andrey Andreev9ab70a82013-07-23 00:09:26 +03001200 // Autoload drivers
1201 if (isset($autoload['drivers']))
1202 {
1203 foreach ($autoload['drivers'] as $item)
1204 {
1205 $this->driver($item);
1206 }
1207 }
1208
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001210 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 {
1212 // Load the database driver.
1213 if (in_array('database', $autoload['libraries']))
1214 {
1215 $this->database();
1216 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1217 }
Barry Mienydd671972010-10-04 16:33:58 +02001218
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 // Load all other libraries
1220 foreach ($autoload['libraries'] as $item)
1221 {
1222 $this->library($item);
1223 }
Barry Mienydd671972010-10-04 16:33:58 +02001224 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001225
1226 // Autoload models
1227 if (isset($autoload['model']))
1228 {
1229 $this->model($autoload['model']);
1230 }
Barry Mienydd671972010-10-04 16:33:58 +02001231 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001232
1233 // --------------------------------------------------------------------
1234
1235 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001236 * CI Object to Array translator
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001238 * Takes an object as input and converts the class variables to
1239 * an associative array with key/value pairs.
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001241 * @param object $object Object data to translate
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 * @return array
1243 */
Greg Akerf5c84022011-04-19 17:13:03 -05001244 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001246 return is_object($object) ? get_object_vars($object) : $object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 }
1248
1249 // --------------------------------------------------------------------
1250
1251 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001252 * CI Component getter
Derek Jones32bf1862010-03-02 13:46:07 -06001253 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001254 * Get a reference to a specific library or model.
1255 *
1256 * @param string $component Component name
Derek Jones32bf1862010-03-02 13:46:07 -06001257 * @return bool
1258 */
Greg Akerf5c84022011-04-19 17:13:03 -05001259 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001260 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001261 $CI =& get_instance();
1262 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001263 }
1264
1265 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001266
Derek Jones32bf1862010-03-02 13:46:07 -06001267 /**
1268 * Prep filename
1269 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001270 * This function prepares filenames of various items to
1271 * make their loading more reliable.
Derek Jones32bf1862010-03-02 13:46:07 -06001272 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001273 * @param string|string[] $filename Filename(s)
1274 * @param string $extension Filename extension
Derek Jones32bf1862010-03-02 13:46:07 -06001275 * @return array
1276 */
Greg Akerf5c84022011-04-19 17:13:03 -05001277 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001278 {
1279 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001280 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001281 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001282 }
1283 else
1284 {
1285 foreach ($filename as $key => $val)
1286 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001287 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001288 }
Barry Mienydd671972010-10-04 16:33:58 +02001289
Derek Jones32bf1862010-03-02 13:46:07 -06001290 return $filename;
1291 }
1292 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001293
Derek Allard2067d1a2008-11-13 22:59:24 +00001294}
1295
1296/* End of file Loader.php */
Andrey Andreev9438e262012-10-05 13:16:27 +03001297/* Location: ./system/core/Loader.php */