blob: 00ca35199527c55db7e0b6ba62604def1fede08a [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
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000265
Derek Jones32bf1862010-03-02 13:46:07 -0600266 foreach ($this->_ci_model_paths as $mod_path)
267 {
Greg Aker3a746652011-04-19 10:59:47 -0500268 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600269 {
270 continue;
271 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000272
Andrey Andreev94af3552012-03-26 23:10:42 +0300273 if ($db_conn !== FALSE && ! class_exists('CI_DB'))
Derek Jones32bf1862010-03-02 13:46:07 -0600274 {
275 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500276 {
Derek Jones32bf1862010-03-02 13:46:07 -0600277 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500278 }
Derek Jones32bf1862010-03-02 13:46:07 -0600279
280 $CI->load->database($db_conn, FALSE, TRUE);
281 }
282
Greg Akerbce13482010-10-11 15:37:16 -0500283 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600284 {
285 load_class('Model', 'core');
286 }
287
Greg Aker3a746652011-04-19 10:59:47 -0500288 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600289
290 $model = ucfirst($model);
Derek Jones32bf1862010-03-02 13:46:07 -0600291 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600292 $this->_ci_models[] = $name;
293 return;
294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Jones32bf1862010-03-02 13:46:07 -0600296 // couldn't find the model
297 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 /**
303 * Database Loader
304 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300305 * @param mixed $params Database configuration options
306 * @param bool $return Whether to return the database object
307 * @param bool $query_builder Whether to enable Query Builder
308 * (overrides the configuration setting)
309 *
310 * @return void|object|bool Database object if $return is set to TRUE,
311 * FALSE on failure, void in any other case
Barry Mienydd671972010-10-04 16:33:58 +0200312 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000313 public function database($params = '', $return = FALSE, $query_builder = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
315 // Grab the super object
316 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // Do we even need to load the database class?
Andrey Andreev9d0ab042012-10-24 21:47:39 +0300319 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 +0000320 {
321 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200322 }
323
Greg Aker3a746652011-04-19 10:59:47 -0500324 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000325
326 if ($return === TRUE)
327 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000328 return DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Andrey Andreevd7297352012-01-07 22:53:14 +0200331 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 // reference errors with some configurations
333 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // Load the DB class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000336 $CI->db =& DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // --------------------------------------------------------------------
340
341 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300342 * Load the Database Utilities Class
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200344 * @param object $db Database object
345 * @param bool $return Whether to return the DB Forge class object or not
346 * @return void|object
Barry Mienydd671972010-10-04 16:33:58 +0200347 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200348 public function dbutil($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 $CI =& get_instance();
351
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200352 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
353 {
354 class_exists('CI_DB', FALSE) OR $this->database();
355 $db =& $CI->db;
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Greg Aker3a746652011-04-19 10:59:47 -0500358 require_once(BASEPATH.'database/DB_utility.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200359 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_utility.php');
360 $class = 'CI_DB_'.$db->dbdriver.'_utility';
Derek Allard2067d1a2008-11-13 22:59:24 +0000361
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200362 if ($return === TRUE)
363 {
364 return new $class($db);
365 }
366
367 $CI->dbutil = new $class($db);
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 // --------------------------------------------------------------------
371
372 /**
373 * Load the Database Forge Class
374 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200375 * @param object $db Database object
376 * @param bool $return Whether to return the DB Forge class object or not
377 * @return void|object
Barry Mienydd671972010-10-04 16:33:58 +0200378 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200379 public function dbforge($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200381 $CI =& get_instance();
382 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200384 class_exists('CI_DB', FALSE) OR $this->database();
385 $db =& $CI->db;
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Greg Aker3a746652011-04-19 10:59:47 -0500388 require_once(BASEPATH.'database/DB_forge.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200389 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_forge.php');
Andrey Andreeva287a342012-11-05 23:19:59 +0200390
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200391 if ( ! empty($db->subdriver))
Andrey Andreeva287a342012-11-05 23:19:59 +0200392 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200393 $driver_path = BASEPATH.'database/drivers/'.$db->dbdriver.'/subdrivers/'.$db->dbdriver.'_'.$db->subdriver.'_forge.php';
Andrey Andreeva287a342012-11-05 23:19:59 +0200394 if (file_exists($driver_path))
395 {
396 require_once($driver_path);
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200397 $class = 'CI_DB_'.$db->dbdriver.'_'.$db->subdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200398 }
399 }
400 else
401 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200402 $class = 'CI_DB_'.$db->dbdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200403 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000404
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200405 if ($return === TRUE)
406 {
407 return new $class($db);
408 }
409
410 $CI->dbforge = new $class($db);
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300416 * View Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300418 * Loads "view" files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300420 * @param string $view View name
421 * @param array $vars An associative array of data
422 * to be extracted for use in the view
423 * @param bool $return Whether to return the view output
424 * or leave it to the Output class
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 * @return void
426 */
Greg Akerf5c84022011-04-19 17:13:03 -0500427 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
429 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300435 * Generic File Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300437 * @param string $path File path
438 * @param bool $return Whether to return the file output
439 * @return void|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 */
Greg Akerf5c84022011-04-19 17:13:03 -0500441 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
444 }
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 /**
449 * Set Variables
450 *
451 * Once variables are set they become available within
452 * the controller class and its "view" files.
453 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300454 * @param array|object|string $vars
455 * An associative array or object containing values
456 * to be set, or a value's name if string
457 * @param string $val Value to set, only used if $vars is a string
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 * @return void
459 */
Greg Akerf5c84022011-04-19 17:13:03 -0500460 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100462 if ($val !== '' && is_string($vars))
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
464 $vars = array($vars => $val);
465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200468
Andrey Andreev94af3552012-03-26 23:10:42 +0300469 if (is_array($vars) && count($vars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
471 foreach ($vars as $key => $val)
472 {
473 $this->_ci_cached_vars[$key] = $val;
474 }
475 }
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600481 * Get Variable
482 *
483 * Check if a variable is set and retrieve it.
484 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300485 * @param string $key Variable name
486 * @return mixed The variable or NULL if not found
Phil Sturgeon8731f642011-07-22 16:11:34 -0600487 */
488 public function get_var($key)
489 {
490 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
491 }
492
493 // --------------------------------------------------------------------
494
495 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600496 * Get Variables
497 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300498 * Retrieves all loaded variables.
Shane Pearson81dd2232011-11-18 20:49:35 -0600499 *
500 * @return array
501 */
502 public function get_vars()
503 {
504 return $this->_ci_cached_vars;
505 }
506
507 // --------------------------------------------------------------------
508
509 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300510 * Helper Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300512 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 * @return void
514 */
Greg Akerf5c84022011-04-19 17:13:03 -0500515 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200516 {
Derek Jones32bf1862010-03-02 13:46:07 -0600517 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200518 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 if (isset($this->_ci_helpers[$helper]))
520 {
521 continue;
522 }
Derek Jones32bf1862010-03-02 13:46:07 -0600523
Barry Mienydd671972010-10-04 16:33:58 +0200524 // Is this a helper extension request?
Andrey Andreev12d7b462012-11-12 13:42:09 +0200525 $ext_helper = config_item('subclass_prefix').$helper;
526 $ext_loaded = FALSE;
527 foreach ($this->_ci_helper_paths as $path)
528 {
529 if (file_exists($path.'helpers/'.$ext_helper.'.php'))
530 {
531 include_once($path.'helpers/'.$ext_helper.'.php');
532 $ext_loaded = TRUE;
533 }
534 }
535
536 // If we have loaded extensions - check if the base one is here
537 if ($ext_loaded === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
Greg Aker3a746652011-04-19 10:59:47 -0500539 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 if ( ! file_exists($base_helper))
541 {
Greg Aker3a746652011-04-19 10:59:47 -0500542 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 }
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 include_once($base_helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600546 $this->_ci_helpers[$helper] = TRUE;
547 log_message('debug', 'Helper loaded: '.$helper);
548 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 }
Barry Mienydd671972010-10-04 16:33:58 +0200550
Andrey Andreev12d7b462012-11-12 13:42:09 +0200551 // No extensions found ... try loading regular helpers and/or overrides
Derek Jones32bf1862010-03-02 13:46:07 -0600552 foreach ($this->_ci_helper_paths as $path)
553 {
Greg Aker3a746652011-04-19 10:59:47 -0500554 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200555 {
Greg Aker3a746652011-04-19 10:59:47 -0500556 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600557
558 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200559 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600560 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 }
562 }
563
Derek Jones32bf1862010-03-02 13:46:07 -0600564 // unable to load the helper
565 if ( ! isset($this->_ci_helpers[$helper]))
566 {
Greg Aker3a746652011-04-19 10:59:47 -0500567 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600568 }
Barry Mienydd671972010-10-04 16:33:58 +0200569 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 /**
575 * Load Helpers
576 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300577 * An alias for the helper() method in case the developer has
578 * written the plural form of it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300580 * @uses CI_Loader::helper()
581 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * @return void
583 */
Greg Akerf5c84022011-04-19 17:13:03 -0500584 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
586 $this->helper($helpers);
587 }
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300592 * Language Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300594 * Loads language files.
595 *
596 * @param string|string[] $files List of language file names to load
597 * @param string Language name
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 * @return void
599 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300600 public function language($files = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
602 $CI =& get_instance();
603
Andrey Andreeved4b2582012-10-27 17:46:52 +0300604 is_array($files) OR $files = array($files);
Derek Allard2067d1a2008-11-13 22:59:24 +0000605
Andrey Andreeved4b2582012-10-27 17:46:52 +0300606 foreach ($files as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200607 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 $CI->lang->load($langfile, $lang);
609 }
610 }
Barry Mienydd671972010-10-04 16:33:58 +0200611
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300615 * Config Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300617 * Loads a config file (an alias for CI_Config::load()).
618 *
619 * @uses CI_Config::load()
620 * @param string $file Configuration file name
621 * @param bool $use_sections Whether configuration values should be loaded into their own section
622 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
623 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 */
Greg Akerf5c84022011-04-19 17:13:03 -0500625 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200626 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 $CI =& get_instance();
Andrey Andreeved4b2582012-10-27 17:46:52 +0300628 return $CI->config->load($file, $use_sections, $fail_gracefully);
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 }
630
631 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300634 * Driver Loader
Derek Jones8dca0412010-03-05 13:01:44 -0600635 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300636 * Loads a driver library.
Derek Jones8dca0412010-03-05 13:01:44 -0600637 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300638 * @param string|string[] $library Driver name(s)
639 * @param array $params Optional parameters to pass to the driver
640 * @param string $object_name An optional object name to assign to
641 *
642 * @return void|object|bool Object or FALSE on failure if $library is a string
643 * and $object_name is set. void otherwise.
Derek Jones8dca0412010-03-05 13:01:44 -0600644 */
Greg Akerf5c84022011-04-19 17:13:03 -0500645 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600646 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800647 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800648 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800649 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800650 {
651 $this->driver($driver);
652 }
dchill420fc3be52012-08-27 20:54:23 -0400653 return;
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800654 }
655
Alex Bilbieed944a32012-06-02 11:07:47 +0100656 if ($library === '')
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200657 {
658 return FALSE;
659 }
660
dchill426262d052012-11-24 18:41:13 -0500661 if ( ! class_exists('CI_Driver_Library'))
662 {
663 // We aren't instantiating an object here, just making the base class available
664 require BASEPATH.'libraries/Driver.php';
665 }
666
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600667 // We can save the loader some time since Drivers will *always* be in a subfolder,
668 // and typically identically named to the library
669 if ( ! strpos($library, '/'))
670 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500671 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600672 }
Barry Mienydd671972010-10-04 16:33:58 +0200673
Derek Jones8dca0412010-03-05 13:01:44 -0600674 return $this->library($library, $params, $object_name);
675 }
676
677 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Jones8dca0412010-03-05 13:01:44 -0600679 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600680 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300682 * Prepends a parent path to the library, model, helper and config
683 * path arrays.
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300685 * @see CI_Loader::$_ci_library_paths
686 * @see CI_Loader::$_ci_model_paths
687 * @see CI_Loader::$_ci_helper_paths
688 * @see CI_Config::$_config_paths
689 *
690 * @param string $path Path to add
691 * @param bool $view_cascade (default: TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600693 */
Andrey Andreevcce91802012-06-12 13:25:31 +0300694 public function add_package_path($path, $view_cascade = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600695 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500696 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200697
Derek Jones32bf1862010-03-02 13:46:07 -0600698 array_unshift($this->_ci_library_paths, $path);
699 array_unshift($this->_ci_model_paths, $path);
700 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200701
Greg Akerf5c84022011-04-19 17:13:03 -0500702 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
703
Derek Jones32bf1862010-03-02 13:46:07 -0600704 // Add config file path
705 $config =& $this->_ci_get_component('config');
and-ersb3ec9422013-01-03 16:05:12 +0100706 $config->_config_paths[] = $path;
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 }
708
709 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600710
711 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000712 * Get Package Paths
713 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300714 * Return a list of all package paths.
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000715 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300716 * @param bool $include_base Whether to include BASEPATH (default: TRUE)
717 * @return array
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000718 */
Greg Akerf5c84022011-04-19 17:13:03 -0500719 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000720 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300721 return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000722 }
723
724 // --------------------------------------------------------------------
725
726 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600727 * Remove Package Path
728 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300729 * Remove a path from the library, model, helper and/or config
730 * path arrays if it exists. If no path is provided, the most recently
731 * added path will be removed removed.
Derek Jones32bf1862010-03-02 13:46:07 -0600732 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300733 * @param string $path Path to remove
Andrey Andreev94af3552012-03-26 23:10:42 +0300734 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600735 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300736 public function remove_package_path($path = '')
Derek Jones32bf1862010-03-02 13:46:07 -0600737 {
738 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200739
Alex Bilbieed944a32012-06-02 11:07:47 +0100740 if ($path === '')
Derek Jones32bf1862010-03-02 13:46:07 -0600741 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200742 array_shift($this->_ci_library_paths);
743 array_shift($this->_ci_model_paths);
744 array_shift($this->_ci_helper_paths);
745 array_shift($this->_ci_view_paths);
Korri3684d342012-04-17 00:35:08 -0400746 array_pop($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600747 }
748 else
749 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500750 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600751 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
752 {
753 if (($key = array_search($path, $this->{$var})) !== FALSE)
754 {
755 unset($this->{$var}[$key]);
756 }
757 }
David Behlercda768a2011-08-14 23:52:48 +0200758
Greg Akerf5c84022011-04-19 17:13:03 -0500759 if (isset($this->_ci_view_paths[$path.'views/']))
760 {
761 unset($this->_ci_view_paths[$path.'views/']);
762 }
Barry Mienydd671972010-10-04 16:33:58 +0200763
Derek Jones32bf1862010-03-02 13:46:07 -0600764 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
765 {
766 unset($config->_config_paths[$key]);
767 }
768 }
Barry Mienydd671972010-10-04 16:33:58 +0200769
Derek Jones32bf1862010-03-02 13:46:07 -0600770 // make sure the application default paths are still in the array
771 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
772 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
773 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500774 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600775 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
776 }
777
778 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200779
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300781 * Internal CI Data Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300783 * Used to load views and files.
784 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 * Variables are prefixed with _ci_ to avoid symbol collision with
Andrey Andreeved4b2582012-10-27 17:46:52 +0300786 * variables made available to view files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300788 * @used-by CI_Loader::view()
789 * @used-by CI_Loader::file()
790 * @param array $_ci_data Data to load
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 * @return void
792 */
Greg Akerf5c84022011-04-19 17:13:03 -0500793 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
795 // Set the default data variables
796 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
797 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300798 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 }
David Behlercda768a2011-08-14 23:52:48 +0200800
Greg Akerf5c84022011-04-19 17:13:03 -0500801 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000802
803 // Set the path to the requested file
Alex Bilbie40bd2a72012-06-02 16:04:15 +0100804 if (is_string($_ci_path) && $_ci_path !== '')
Greg Aker8807be32011-04-21 13:06:15 -0500805 {
806 $_ci_x = explode('/', $_ci_path);
807 $_ci_file = end($_ci_x);
808 }
809 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 {
811 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Alex Bilbieed944a32012-06-02 11:07:47 +0100812 $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500813
Joe McFrederick64f470b2012-08-18 12:29:56 -0400814 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
Greg Akerf5c84022011-04-19 17:13:03 -0500815 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400816 if (file_exists($_ci_view_file.$_ci_file))
Greg Akerf5c84022011-04-19 17:13:03 -0500817 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400818 $_ci_path = $_ci_view_file.$_ci_file;
Greg Akerf5c84022011-04-19 17:13:03 -0500819 $file_exists = TRUE;
820 break;
821 }
David Behlercda768a2011-08-14 23:52:48 +0200822
Greg Akerf5c84022011-04-19 17:13:03 -0500823 if ( ! $cascade)
824 {
825 break;
David Behlercda768a2011-08-14 23:52:48 +0200826 }
Greg Akerf5c84022011-04-19 17:13:03 -0500827 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 }
Barry Mienydd671972010-10-04 16:33:58 +0200829
Greg Akerf5c84022011-04-19 17:13:03 -0500830 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 {
832 show_error('Unable to load the requested file: '.$_ci_file);
833 }
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 // This allows anything loaded using $this->load (views, files, etc.)
836 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500837 $_ci_CI =& get_instance();
838 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500840 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500842 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 }
844 }
845
846 /*
847 * Extract and cache variables
848 *
vlakoff02506182012-07-03 07:28:50 +0200849 * You can either set variables using the dedicated $this->load->vars()
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 * function or via the second parameter of this function. We'll merge
851 * the two types and cache them so that views that are embedded within
852 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200853 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 if (is_array($_ci_vars))
855 {
856 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
857 }
858 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200859
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 /*
861 * Buffer the output
862 *
863 * We buffer the output for two reasons:
864 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200865 * 2. So that the final rendered template can be post-processed by
dchill425628ba02012-08-08 12:05:45 -0400866 * the output class. Why do we need post processing? For one thing,
867 * in order to show the elapsed page load time. Unless we can
868 * intercept the content right before it's sent to the browser and
869 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 */
871 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200872
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 // If the PHP installation does not support short tags we'll
874 // do a little string replacement, changing the short tags
875 // to standard PHP echo statements.
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200876 if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE
877 && config_item('rewrite_short_tags') === TRUE && function_usable('eval')
878 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200880 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 }
882 else
883 {
884 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
885 }
Barry Mienydd671972010-10-04 16:33:58 +0200886
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 // Return the file data if requested
890 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200891 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 $buffer = ob_get_contents();
893 @ob_end_clean();
894 return $buffer;
895 }
896
897 /*
898 * Flush the buffer... or buff the flusher?
899 *
900 * In order to permit views to be nested within
901 * other views, we need to flush the content back out whenever
902 * we are beyond the first level of output buffering so that
903 * it can be seen and included properly by the first included
904 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200905 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 if (ob_get_level() > $this->_ci_ob_level + 1)
907 {
908 ob_end_flush();
909 }
910 else
911 {
Greg Aker22f1a632010-11-10 15:34:35 -0600912 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 @ob_end_clean();
914 }
915 }
916
917 // --------------------------------------------------------------------
918
919 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300920 * Internal CI Class Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300922 * @used-by CI_Loader::library()
923 * @uses CI_Loader::_ci_init_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300925 * @param string $class Class name to load
926 * @param mixed $params Optional parameters to pass to the class constructor
927 * @param string $object_name Optional object name to assign to
Barry Mienydd671972010-10-04 16:33:58 +0200928 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 */
Greg Akerf5c84022011-04-19 17:13:03 -0500930 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200931 {
932 // Get the class name, and while we're at it trim any slashes.
933 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500935 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200936
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 // Was the path included with the class name?
938 // We look for a slash to determine this
Derek Jones32bf1862010-03-02 13:46:07 -0600939 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 {
Derek Jones32bf1862010-03-02 13:46:07 -0600941 // Extract the path
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200942 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200943
Derek Jones32bf1862010-03-02 13:46:07 -0600944 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200945 $class = substr($class, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 }
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200947 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200949 $subdir = '';
950 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000951
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200952 $class = ucfirst($class);
953 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
954
955 // Is this a class extension request?
956 if (file_exists($subclass))
957 {
958 $baseclass = BASEPATH.'libraries/'.$class.'.php';
959
960 if ( ! file_exists($baseclass))
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200962 log_message('error', 'Unable to load the requested class: '.$class);
963 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 }
Barry Mienydd671972010-10-04 16:33:58 +0200965
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200966 // Safety: Was the class already loaded by a previous call?
967 if (class_exists(config_item('subclass_prefix').$class, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200969 // Before we deem this to be a duplicate request, let's see
970 // if a custom object name is being supplied. If so, we'll
971 // return a new instance of the object
972 if ($object_name !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200974 $CI =& get_instance();
975 if ( ! isset($CI->$object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200977 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 }
Barry Mienydd671972010-10-04 16:33:58 +0200980
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200981 log_message('debug', $class.' class already loaded. Second attempt ignored.');
982 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 }
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200984
985 include_once($baseclass);
986 include_once($subclass);
987
988 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
989 }
990
991 // Lets search for the requested library file and load it.
992 foreach ($this->_ci_library_paths as $path)
993 {
994 $filepath = $path.'libraries/'.$subdir.$class.'.php';
995
996 // Safety: Was the class already loaded by a previous call?
997 if (class_exists($class, FALSE))
998 {
999 // Before we deem this to be a duplicate request, let's see
1000 // if a custom object name is being supplied. If so, we'll
1001 // return a new instance of the object
1002 if ($object_name !== NULL)
1003 {
1004 $CI =& get_instance();
1005 if ( ! isset($CI->$object_name))
1006 {
1007 return $this->_ci_init_class($class, '', $params, $object_name);
1008 }
1009 }
1010
1011 log_message('debug', $class.' class already loaded. Second attempt ignored.');
1012 return;
1013 }
1014 // Does the file exist? No? Bummer...
1015 elseif ( ! file_exists($filepath))
1016 {
1017 continue;
1018 }
1019
1020 include_once($filepath);
1021 return $this->_ci_init_class($class, '', $params, $object_name);
1022 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001023
Andrey Andreevd7297352012-01-07 22:53:14 +02001024 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Alex Bilbieed944a32012-06-02 11:07:47 +01001025 if ($subdir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001027 return $this->_ci_load_class($class.'/'.$class, $params, $object_name);
dchill42aee92652012-08-26 21:45:35 -04001028 }
Barry Mienydd671972010-10-04 16:33:58 +02001029
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 // If we got this far we were unable to find the requested class.
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001031 log_message('error', 'Unable to load the requested class: '.$class);
1032 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 }
Barry Mienydd671972010-10-04 16:33:58 +02001034
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 // --------------------------------------------------------------------
1036
1037 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001038 * Internal CI Class Instantiator
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001040 * @used-by CI_Loader::_ci_load_class()
1041 *
1042 * @param string $class Class name
1043 * @param string $prefix Class name prefix
1044 * @param array|null|bool $config Optional configuration to pass to the class constructor:
1045 * FALSE to skip;
1046 * NULL to search in config paths;
1047 * array containing configuration data
1048 * @param string $object_name Optional object name to assign to
Andrey Andreev94af3552012-03-26 23:10:42 +03001049 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001051 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001052 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001053 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 if ($config === NULL)
1055 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001056 // Fetch the config paths containing any package paths
1057 $config_component = $this->_ci_get_component('config');
1058
1059 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001061 // Break on the first found file, thus package files
1062 // are not overridden by default paths
1063 foreach ($config_component->_config_paths as $path)
1064 {
1065 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001066 // are case-sensitive with regard to file names. Check for environment
1067 // first, global next
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001068 if (file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001069 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001070 include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001071 break;
1072 }
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001073 elseif (file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001074 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001075 include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001076 break;
1077 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001078 elseif (file_exists($path.'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001079 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001080 include($path.'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001081 break;
1082 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001083 elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001084 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001085 include($path.'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001086 break;
1087 }
1088 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 }
1090 }
Barry Mienydd671972010-10-04 16:33:58 +02001091
Alex Bilbieed944a32012-06-02 11:07:47 +01001092 if ($prefix === '')
Barry Mienydd671972010-10-04 16:33:58 +02001093 {
1094 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 {
1096 $name = 'CI_'.$class;
1097 }
Barry Mienydd671972010-10-04 16:33:58 +02001098 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
1100 $name = config_item('subclass_prefix').$class;
1101 }
1102 else
1103 {
1104 $name = $class;
1105 }
1106 }
1107 else
1108 {
1109 $name = $prefix.$class;
1110 }
Barry Mienydd671972010-10-04 16:33:58 +02001111
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 // Is the class name valid?
1113 if ( ! class_exists($name))
1114 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001115 log_message('error', 'Non-existent class: '.$name);
jonnueee2df62012-07-16 13:06:16 +01001116 show_error('Non-existent class: '.$name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 }
Barry Mienydd671972010-10-04 16:33:58 +02001118
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001120 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001122
vlakoff1228fe22013-01-14 01:30:09 +01001123 if ($object_name === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001125 $classvar = isset($this->_ci_varmap[$class]) ? $this->_ci_varmap[$class] : $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 }
1127 else
1128 {
1129 $classvar = $object_name;
1130 }
1131
Barry Mienydd671972010-10-04 16:33:58 +02001132 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 $this->_ci_classes[$class] = $classvar;
1134
Barry Mienydd671972010-10-04 16:33:58 +02001135 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 $CI =& get_instance();
1137 if ($config !== NULL)
1138 {
1139 $CI->$classvar = new $name($config);
1140 }
1141 else
Barry Mienydd671972010-10-04 16:33:58 +02001142 {
Andrey Andreeva11b16b2012-03-28 12:22:04 +03001143 $CI->$classvar = new $name();
Barry Mienydd671972010-10-04 16:33:58 +02001144 }
1145 }
1146
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001148
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001150 * CI Autoloader
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001152 * Loads component listed in the config/autoload.php file.
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 *
Andrey Andreevcdac2482012-11-03 18:09:01 +02001154 * @used-by CI_Loader::initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 * @return void
1156 */
Shane Pearson665baec2011-08-22 18:52:19 -05001157 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001158 {
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001159 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001160 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001161 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001162 }
1163 else
1164 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001165 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001166 }
Barry Mienydd671972010-10-04 16:33:58 +02001167
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 if ( ! isset($autoload))
1169 {
1170 return FALSE;
1171 }
Barry Mienydd671972010-10-04 16:33:58 +02001172
Phil Sturgeon9730c752010-12-15 10:50:15 +00001173 // Autoload packages
1174 if (isset($autoload['packages']))
1175 {
1176 foreach ($autoload['packages'] as $package_path)
1177 {
1178 $this->add_package_path($package_path);
1179 }
1180 }
1181
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 // Load any custom config file
1183 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001184 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 $CI =& get_instance();
1186 foreach ($autoload['config'] as $key => $val)
1187 {
1188 $CI->config->load($val);
1189 }
Barry Mienydd671972010-10-04 16:33:58 +02001190 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001191
Derek Jonesc6da5032010-03-09 20:44:27 -06001192 // Autoload helpers and languages
1193 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001194 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001195 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 {
1197 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001198 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 }
1200
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001202 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 {
1204 // Load the database driver.
1205 if (in_array('database', $autoload['libraries']))
1206 {
1207 $this->database();
1208 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1209 }
Barry Mienydd671972010-10-04 16:33:58 +02001210
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 // Load all other libraries
1212 foreach ($autoload['libraries'] as $item)
1213 {
1214 $this->library($item);
1215 }
Barry Mienydd671972010-10-04 16:33:58 +02001216 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001217
Darren Hillc4e266b2011-08-30 15:40:27 -04001218 // Autoload drivers
1219 if (isset($autoload['drivers']))
1220 {
Darren Hillca3be1d2011-08-31 08:31:18 -04001221 foreach ($autoload['drivers'] as $item)
1222 {
1223 $this->driver($item);
1224 }
Darren Hillc4e266b2011-08-30 15:40:27 -04001225 }
1226
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 // Autoload models
1228 if (isset($autoload['model']))
1229 {
1230 $this->model($autoload['model']);
1231 }
Barry Mienydd671972010-10-04 16:33:58 +02001232 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001233
1234 // --------------------------------------------------------------------
1235
1236 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001237 * CI Object to Array translator
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001239 * Takes an object as input and converts the class variables to
1240 * an associative array with key/value pairs.
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001242 * @param object $object Object data to translate
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 * @return array
1244 */
Greg Akerf5c84022011-04-19 17:13:03 -05001245 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001247 return is_object($object) ? get_object_vars($object) : $object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 }
1249
1250 // --------------------------------------------------------------------
1251
1252 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001253 * CI Component getter
Derek Jones32bf1862010-03-02 13:46:07 -06001254 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001255 * Get a reference to a specific library or model.
1256 *
1257 * @param string $component Component name
Derek Jones32bf1862010-03-02 13:46:07 -06001258 * @return bool
1259 */
Greg Akerf5c84022011-04-19 17:13:03 -05001260 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001261 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001262 $CI =& get_instance();
1263 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001264 }
1265
1266 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001267
Derek Jones32bf1862010-03-02 13:46:07 -06001268 /**
1269 * Prep filename
1270 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001271 * This function prepares filenames of various items to
1272 * make their loading more reliable.
Derek Jones32bf1862010-03-02 13:46:07 -06001273 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001274 * @param string|string[] $filename Filename(s)
1275 * @param string $extension Filename extension
Derek Jones32bf1862010-03-02 13:46:07 -06001276 * @return array
1277 */
Greg Akerf5c84022011-04-19 17:13:03 -05001278 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001279 {
1280 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001281 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001282 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001283 }
1284 else
1285 {
1286 foreach ($filename as $key => $val)
1287 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001288 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001289 }
Barry Mienydd671972010-10-04 16:33:58 +02001290
Derek Jones32bf1862010-03-02 13:46:07 -06001291 return $filename;
1292 }
1293 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001294
Derek Allard2067d1a2008-11-13 22:59:24 +00001295}
1296
1297/* End of file Loader.php */
Andrey Andreev9438e262012-10-05 13:16:27 +03001298/* Location: ./system/core/Loader.php */