blob: 5b119854b3c8366b44b2967afbd199c3e7999abe [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 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevd7297352012-01-07 22:53:14 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreevd7297352012-01-07 22:53:14 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Allard2067d1a2008-11-13 22:59:24 +000017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Loader Class
42 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030043 * Loads framework components.
Derek Allard2067d1a2008-11-13 22:59:24 +000044 *
45 * @package CodeIgniter
46 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @category Loader
Andrey Andreev92ebfb62012-05-17 12:49:24 +030048 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * @link http://codeigniter.com/user_guide/libraries/loader.html
50 */
51class CI_Loader {
52
53 // All these are set automatically. Don't mess with them.
David Behlercda768a2011-08-14 23:52:48 +020054 /**
55 * Nesting level of the output buffering mechanism
56 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030057 * @var int
David Behlercda768a2011-08-14 23:52:48 +020058 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050059 protected $_ci_ob_level;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030060
David Behlercda768a2011-08-14 23:52:48 +020061 /**
62 * List of paths to load views from
63 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030064 * @var array
David Behlercda768a2011-08-14 23:52:48 +020065 */
Ted Wood4c223642013-01-05 16:50:31 -080066 protected $_ci_view_paths = array(VIEWPATH => TRUE);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030067
David Behlercda768a2011-08-14 23:52:48 +020068 /**
69 * List of paths to load libraries from
70 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030071 * @var array
David Behlercda768a2011-08-14 23:52:48 +020072 */
Ted Wood4c223642013-01-05 16:50:31 -080073 protected $_ci_library_paths = array(APPPATH, BASEPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030074
David Behlercda768a2011-08-14 23:52:48 +020075 /**
76 * List of paths to load models from
77 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030078 * @var array
David Behlercda768a2011-08-14 23:52:48 +020079 */
Ted Wood4c223642013-01-05 16:50:31 -080080 protected $_ci_model_paths = array(APPPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030081
David Behlercda768a2011-08-14 23:52:48 +020082 /**
83 * List of paths to load helpers from
84 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030085 * @var array
David Behlercda768a2011-08-14 23:52:48 +020086 */
Ted Wood4c223642013-01-05 16:50:31 -080087 protected $_ci_helper_paths = array(APPPATH, BASEPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030088
David Behlercda768a2011-08-14 23:52:48 +020089 /**
David Behlercda768a2011-08-14 23:52:48 +020090 * List of cached variables
91 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030092 * @var array
David Behlercda768a2011-08-14 23:52:48 +020093 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040094 protected $_ci_cached_vars = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030095
David Behlercda768a2011-08-14 23:52:48 +020096 /**
97 * List of loaded classes
98 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030099 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200100 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400101 protected $_ci_classes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300102
David Behlercda768a2011-08-14 23:52:48 +0200103 /**
David Behlercda768a2011-08-14 23:52:48 +0200104 * List of loaded models
105 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300106 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200107 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400108 protected $_ci_models = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300109
David Behlercda768a2011-08-14 23:52:48 +0200110 /**
111 * List of loaded helpers
112 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300113 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200114 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400115 protected $_ci_helpers = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300116
David Behlercda768a2011-08-14 23:52:48 +0200117 /**
118 * List of class name mappings
119 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300120 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200121 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400122 protected $_ci_varmap = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400123 'unit_test' => 'unit',
124 'user_agent' => 'agent'
125 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000126
Andrey Andreev519f87a2013-07-23 17:16:10 +0300127 // --------------------------------------------------------------------
128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300130 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 *
Andrey Andreevcdac2482012-11-03 18:09:01 +0200132 * Sets component load paths, gets the initial output buffering level.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300133 *
134 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 */
Greg Akerf5c84022011-04-19 17:13:03 -0500136 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +0200137 {
Andrey Andreev519f87a2013-07-23 17:16:10 +0300138 $this->_ci_ob_level = ob_get_level();
139 $this->_ci_classes =& is_loaded();
David Behlercda768a2011-08-14 23:52:48 +0200140
Andrey Andreevd7297352012-01-07 22:53:14 +0200141 log_message('debug', 'Loader Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200145
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500146 /**
Andrey Andreevcdac2482012-11-03 18:09:01 +0200147 * Initializer
148 *
149 * @todo Figure out a way to move this to the constructor
150 * without breaking *package_path*() methods.
151 * @uses CI_Loader::_ci_autoloader()
152 * @used-by CI_Controller::__construct()
153 * @return void
154 */
155 public function initialize()
156 {
Andrey Andreevcdac2482012-11-03 18:09:01 +0200157 $this->_ci_autoloader();
158 }
159
160 // --------------------------------------------------------------------
161
162 /**
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500163 * Is Loaded
164 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300165 * A utility method to test if a class is in the self::$_ci_classes array.
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500166 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300167 * @used-by Mainly used by Form Helper function _get_validation_object().
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500168 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300169 * @param string $class Class name to check for
170 * @return string|bool Class object name if loaded or FALSE
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500171 */
172 public function is_loaded($class)
173 {
Andrey Andreev519f87a2013-07-23 17:16:10 +0300174 return array_search(ucfirst($class), $this->_ci_classes, TRUE);
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500175 }
176
177 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300180 * Library Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300182 * Loads and instantiates libraries.
183 * Designed to be called from application controllers.
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300185 * @param string $library Library name
186 * @param array $params Optional parameters to pass to the library class constructor
187 * @param string $object_name An optional object name to assign to
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200188 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200189 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200190 public function library($library, $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
Andrey Andreev519f87a2013-07-23 17:16:10 +0300192 if (empty($library))
193 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200194 return $this;
Andrey Andreev519f87a2013-07-23 17:16:10 +0300195 }
196 elseif (is_array($library))
Greg Akerce433962010-10-12 09:29:35 -0500197 {
Andrey Andreev88cf55b2014-01-17 15:38:30 +0200198 foreach ($library as $key => $value)
Greg Akerce433962010-10-12 09:29:35 -0500199 {
Andrey Andreev88cf55b2014-01-17 15:38:30 +0200200 if (is_int($key))
201 {
202 $this->library($value, $params);
203 }
204 else
205 {
206 $this->library($key, $params, $value);
207 }
Greg Akerce433962010-10-12 09:29:35 -0500208 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000209
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200210 return $this;
Greg Akerce433962010-10-12 09:29:35 -0500211 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000212
vlakoff1228fe22013-01-14 01:30:09 +0100213 if ($params !== NULL && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
215 $params = NULL;
216 }
217
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600218 $this->_ci_load_class($library, $params, $object_name);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200219 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
221
222 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 /**
225 * Model Loader
226 *
James7b272ff2014-03-20 19:02:37 +0000227 * Loads and instantiates models.
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300229 * @param string $model Model name
230 * @param string $name An optional object name to assign to
231 * @param bool $db_conn An optional database connection configuration to initialize
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200232 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200233 */
Greg Akerf5c84022011-04-19 17:13:03 -0500234 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200235 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300236 if (empty($model))
237 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200238 return $this;
Andrey Andreeved4b2582012-10-27 17:46:52 +0300239 }
240 elseif (is_array($model))
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Andrey Andreev5a519db2013-01-12 04:19:19 +0200242 foreach ($model as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
Ahmad Anbar4005e3c2013-09-12 23:33:28 +0300244 is_int($key) ? $this->model($value, '', $db_conn) : $this->model($key, $value, $db_conn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200246
247 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
249
Derek Jones32bf1862010-03-02 13:46:07 -0600250 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600253 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
Derek Jones32bf1862010-03-02 13:46:07 -0600255 // The path is in front of the last slash
Andrey Andreevd47baab2012-01-09 16:56:46 +0200256 $path = substr($model, 0, ++$last_slash);
Derek Jones32bf1862010-03-02 13:46:07 -0600257
258 // And the model name behind it
Andrey Andreevd47baab2012-01-09 16:56:46 +0200259 $model = substr($model, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Phil Sturgeon10d78f62012-06-04 14:41:53 -0500262 if (empty($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
264 $name = $model;
265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 if (in_array($name, $this->_ci_models, TRUE))
268 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200269 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 $CI =& get_instance();
273 if (isset($CI->$name))
274 {
275 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
276 }
Barry Mienydd671972010-10-04 16:33:58 +0200277
Andrey Andreev20292312013-07-22 14:29:10 +0300278 if ($db_conn !== FALSE && ! class_exists('CI_DB', FALSE))
279 {
280 if ($db_conn === TRUE)
281 {
282 $db_conn = '';
283 }
284
Andrey Andreev5f839692014-03-24 17:34:54 +0200285 $this->database($db_conn, FALSE, TRUE);
Andrey Andreev20292312013-07-22 14:29:10 +0300286 }
287
288 if ( ! class_exists('CI_Model', FALSE))
289 {
290 load_class('Model', 'core');
291 }
292
293 $model = ucfirst(strtolower($model));
Derek Allard2067d1a2008-11-13 22:59:24 +0000294
Derek Jones32bf1862010-03-02 13:46:07 -0600295 foreach ($this->_ci_model_paths as $mod_path)
296 {
Greg Aker3a746652011-04-19 10:59:47 -0500297 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600298 {
299 continue;
300 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000301
Greg Aker3a746652011-04-19 10:59:47 -0500302 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600303
Derek Jones32bf1862010-03-02 13:46:07 -0600304 $this->_ci_models[] = $name;
Andrey Andreev1c77aab2014-10-08 00:35:29 +0300305 $CI->$name = new $model();
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200306 return $this;
Derek Jones32bf1862010-03-02 13:46:07 -0600307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Jones32bf1862010-03-02 13:46:07 -0600309 // couldn't find the model
310 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 /**
316 * Database Loader
317 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300318 * @param mixed $params Database configuration options
319 * @param bool $return Whether to return the database object
320 * @param bool $query_builder Whether to enable Query Builder
321 * (overrides the configuration setting)
322 *
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200323 * @return object|bool Database object if $return is set to TRUE,
324 * FALSE on failure, CI_Loader instance in any other case
Barry Mienydd671972010-10-04 16:33:58 +0200325 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000326 public function database($params = '', $return = FALSE, $query_builder = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
328 // Grab the super object
329 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // Do we even need to load the database class?
Andrey Andreev9d0ab042012-10-24 21:47:39 +0300332 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 +0000333 {
334 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200335 }
336
Greg Aker3a746652011-04-19 10:59:47 -0500337 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000338
339 if ($return === TRUE)
340 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000341 return DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Andrey Andreevd7297352012-01-07 22:53:14 +0200344 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 // reference errors with some configurations
346 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // Load the DB class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000349 $CI->db =& DB($params, $query_builder);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200350 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 // --------------------------------------------------------------------
354
355 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300356 * Load the Database Utilities Class
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200358 * @param object $db Database object
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200359 * @param bool $return Whether to return the DB Utilities class object or not
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200360 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200361 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200362 public function dbutil($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 $CI =& get_instance();
365
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200366 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
367 {
368 class_exists('CI_DB', FALSE) OR $this->database();
369 $db =& $CI->db;
370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371
Greg Aker3a746652011-04-19 10:59:47 -0500372 require_once(BASEPATH.'database/DB_utility.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200373 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_utility.php');
374 $class = 'CI_DB_'.$db->dbdriver.'_utility';
Derek Allard2067d1a2008-11-13 22:59:24 +0000375
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200376 if ($return === TRUE)
377 {
378 return new $class($db);
379 }
380
381 $CI->dbutil = new $class($db);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200382 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // --------------------------------------------------------------------
386
387 /**
388 * Load the Database Forge Class
389 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200390 * @param object $db Database object
391 * @param bool $return Whether to return the DB Forge class object or not
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200392 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200393 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200394 public function dbforge($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200396 $CI =& get_instance();
397 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200399 class_exists('CI_DB', FALSE) OR $this->database();
400 $db =& $CI->db;
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Greg Aker3a746652011-04-19 10:59:47 -0500403 require_once(BASEPATH.'database/DB_forge.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200404 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_forge.php');
Andrey Andreeva287a342012-11-05 23:19:59 +0200405
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200406 if ( ! empty($db->subdriver))
Andrey Andreeva287a342012-11-05 23:19:59 +0200407 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200408 $driver_path = BASEPATH.'database/drivers/'.$db->dbdriver.'/subdrivers/'.$db->dbdriver.'_'.$db->subdriver.'_forge.php';
Andrey Andreeva287a342012-11-05 23:19:59 +0200409 if (file_exists($driver_path))
410 {
411 require_once($driver_path);
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200412 $class = 'CI_DB_'.$db->dbdriver.'_'.$db->subdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200413 }
414 }
415 else
416 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200417 $class = 'CI_DB_'.$db->dbdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200418 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000419
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200420 if ($return === TRUE)
421 {
422 return new $class($db);
423 }
424
425 $CI->dbforge = new $class($db);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200426 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300432 * View Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300434 * Loads "view" files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300436 * @param string $view View name
437 * @param array $vars An associative array of data
438 * to be extracted for use in the view
439 * @param bool $return Whether to return the view output
440 * or leave it to the Output class
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200441 * @return object|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 */
Greg Akerf5c84022011-04-19 17:13:03 -0500443 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
445 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300451 * Generic File Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300453 * @param string $path File path
454 * @param bool $return Whether to return the file output
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200455 * @return object|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 */
Greg Akerf5c84022011-04-19 17:13:03 -0500457 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
459 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 /**
465 * Set Variables
466 *
467 * Once variables are set they become available within
468 * the controller class and its "view" files.
469 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300470 * @param array|object|string $vars
471 * An associative array or object containing values
472 * to be set, or a value's name if string
473 * @param string $val Value to set, only used if $vars is a string
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200474 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200476 public function vars($vars, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
vlakoffa5e0ea82013-02-27 18:17:35 +0100478 if (is_string($vars))
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
480 $vars = array($vars => $val);
481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200484
Andrey Andreev94af3552012-03-26 23:10:42 +0300485 if (is_array($vars) && count($vars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
487 foreach ($vars as $key => $val)
488 {
489 $this->_ci_cached_vars[$key] = $val;
490 }
491 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200492
493 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 /**
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200499 * Clear Cached Variables
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200500 *
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200501 * Clears the cached variables.
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200502 *
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200503 * @return object
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200504 */
505 public function clear_vars()
506 {
507 $this->_ci_cached_vars = array();
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200508 return $this;
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200509 }
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200510
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200511 // --------------------------------------------------------------------
512
513 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600514 * Get Variable
515 *
516 * Check if a variable is set and retrieve it.
517 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300518 * @param string $key Variable name
519 * @return mixed The variable or NULL if not found
Phil Sturgeon8731f642011-07-22 16:11:34 -0600520 */
521 public function get_var($key)
522 {
523 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
524 }
525
526 // --------------------------------------------------------------------
527
528 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600529 * Get Variables
530 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300531 * Retrieves all loaded variables.
Shane Pearson81dd2232011-11-18 20:49:35 -0600532 *
533 * @return array
534 */
535 public function get_vars()
536 {
537 return $this->_ci_cached_vars;
538 }
539
540 // --------------------------------------------------------------------
541
542 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300543 * Helper Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300545 * @param string|string[] $helpers Helper name(s)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200546 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 */
Greg Akerf5c84022011-04-19 17:13:03 -0500548 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200549 {
Derek Jones32bf1862010-03-02 13:46:07 -0600550 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200551 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 if (isset($this->_ci_helpers[$helper]))
553 {
554 continue;
555 }
Derek Jones32bf1862010-03-02 13:46:07 -0600556
Barry Mienydd671972010-10-04 16:33:58 +0200557 // Is this a helper extension request?
Andrey Andreev12d7b462012-11-12 13:42:09 +0200558 $ext_helper = config_item('subclass_prefix').$helper;
559 $ext_loaded = FALSE;
560 foreach ($this->_ci_helper_paths as $path)
561 {
562 if (file_exists($path.'helpers/'.$ext_helper.'.php'))
563 {
564 include_once($path.'helpers/'.$ext_helper.'.php');
565 $ext_loaded = TRUE;
566 }
567 }
568
569 // If we have loaded extensions - check if the base one is here
570 if ($ext_loaded === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
Greg Aker3a746652011-04-19 10:59:47 -0500572 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 if ( ! file_exists($base_helper))
574 {
Greg Aker3a746652011-04-19 10:59:47 -0500575 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 include_once($base_helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600579 $this->_ci_helpers[$helper] = TRUE;
580 log_message('debug', 'Helper loaded: '.$helper);
581 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 }
Barry Mienydd671972010-10-04 16:33:58 +0200583
Andrey Andreev12d7b462012-11-12 13:42:09 +0200584 // No extensions found ... try loading regular helpers and/or overrides
Derek Jones32bf1862010-03-02 13:46:07 -0600585 foreach ($this->_ci_helper_paths as $path)
586 {
Greg Aker3a746652011-04-19 10:59:47 -0500587 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200588 {
Greg Aker3a746652011-04-19 10:59:47 -0500589 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600590
591 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200592 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600593 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 }
595 }
596
Derek Jones32bf1862010-03-02 13:46:07 -0600597 // unable to load the helper
598 if ( ! isset($this->_ci_helpers[$helper]))
599 {
Greg Aker3a746652011-04-19 10:59:47 -0500600 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600601 }
Barry Mienydd671972010-10-04 16:33:58 +0200602 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200603
604 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 }
Barry Mienydd671972010-10-04 16:33:58 +0200606
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200608
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 /**
610 * Load Helpers
611 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300612 * An alias for the helper() method in case the developer has
613 * written the plural form of it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300615 * @uses CI_Loader::helper()
616 * @param string|string[] $helpers Helper name(s)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200617 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 */
Greg Akerf5c84022011-04-19 17:13:03 -0500619 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200621 return $this->helper($helpers);
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 }
Barry Mienydd671972010-10-04 16:33:58 +0200623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300627 * Language Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300629 * Loads language files.
630 *
631 * @param string|string[] $files List of language file names to load
632 * @param string Language name
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200633 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200635 public function language($files, $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
Andrey Andreev5f839692014-03-24 17:34:54 +0200637 $LNG =& get_instance()->lang;
Andrey Andreeved4b2582012-10-27 17:46:52 +0300638 is_array($files) OR $files = array($files);
Derek Allard2067d1a2008-11-13 22:59:24 +0000639
Andrey Andreeved4b2582012-10-27 17:46:52 +0300640 foreach ($files as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200641 {
Andrey Andreev5f839692014-03-24 17:34:54 +0200642 $LNG->load($langfile, $lang);
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200644
645 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 }
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300651 * Config Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300653 * Loads a config file (an alias for CI_Config::load()).
654 *
655 * @uses CI_Config::load()
656 * @param string $file Configuration file name
657 * @param bool $use_sections Whether configuration values should be loaded into their own section
658 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
659 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200661 public function config($file, $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200662 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200663 return get_instance()->config->load($file, $use_sections, $fail_gracefully);
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 }
665
666 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300669 * Driver Loader
Derek Jones8dca0412010-03-05 13:01:44 -0600670 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300671 * Loads a driver library.
Derek Jones8dca0412010-03-05 13:01:44 -0600672 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300673 * @param string|string[] $library Driver name(s)
674 * @param array $params Optional parameters to pass to the driver
675 * @param string $object_name An optional object name to assign to
676 *
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200677 * @return object|bool Object or FALSE on failure if $library is a string
678 * and $object_name is set. CI_Loader instance otherwise.
Derek Jones8dca0412010-03-05 13:01:44 -0600679 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200680 public function driver($library, $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600681 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800682 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800683 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800684 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800685 {
686 $this->driver($driver);
687 }
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800688
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200689 return $this;
690 }
691 elseif (empty($library))
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200692 {
693 return FALSE;
694 }
695
vlakofffadb8222013-05-12 10:57:09 +0200696 if ( ! class_exists('CI_Driver_Library', FALSE))
dchill426262d052012-11-24 18:41:13 -0500697 {
698 // We aren't instantiating an object here, just making the base class available
699 require BASEPATH.'libraries/Driver.php';
700 }
701
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600702 // We can save the loader some time since Drivers will *always* be in a subfolder,
703 // and typically identically named to the library
704 if ( ! strpos($library, '/'))
705 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500706 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600707 }
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Jones8dca0412010-03-05 13:01:44 -0600709 return $this->library($library, $params, $object_name);
710 }
711
712 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Jones8dca0412010-03-05 13:01:44 -0600714 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600715 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300717 * Prepends a parent path to the library, model, helper and config
718 * path arrays.
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300720 * @see CI_Loader::$_ci_library_paths
721 * @see CI_Loader::$_ci_model_paths
722 * @see CI_Loader::$_ci_helper_paths
723 * @see CI_Config::$_config_paths
724 *
725 * @param string $path Path to add
726 * @param bool $view_cascade (default: TRUE)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200727 * @return object
Derek Jones32bf1862010-03-02 13:46:07 -0600728 */
Andrey Andreevcce91802012-06-12 13:25:31 +0300729 public function add_package_path($path, $view_cascade = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600730 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500731 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200732
Derek Jones32bf1862010-03-02 13:46:07 -0600733 array_unshift($this->_ci_library_paths, $path);
734 array_unshift($this->_ci_model_paths, $path);
735 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200736
Greg Akerf5c84022011-04-19 17:13:03 -0500737 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
738
Derek Jones32bf1862010-03-02 13:46:07 -0600739 // Add config file path
740 $config =& $this->_ci_get_component('config');
and-ersb3ec9422013-01-03 16:05:12 +0100741 $config->_config_paths[] = $path;
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200742
743 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 }
745
746 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600747
748 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000749 * Get Package Paths
750 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300751 * Return a list of all package paths.
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000752 *
vlakoffcdc61132013-05-10 16:47:47 +0200753 * @param bool $include_base Whether to include BASEPATH (default: FALSE)
Andrey Andreeved4b2582012-10-27 17:46:52 +0300754 * @return array
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000755 */
Greg Akerf5c84022011-04-19 17:13:03 -0500756 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000757 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300758 return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000759 }
760
761 // --------------------------------------------------------------------
762
763 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600764 * Remove Package Path
765 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300766 * Remove a path from the library, model, helper and/or config
767 * path arrays if it exists. If no path is provided, the most recently
768 * added path will be removed removed.
Derek Jones32bf1862010-03-02 13:46:07 -0600769 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300770 * @param string $path Path to remove
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200771 * @return object
Derek Jones32bf1862010-03-02 13:46:07 -0600772 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300773 public function remove_package_path($path = '')
Derek Jones32bf1862010-03-02 13:46:07 -0600774 {
775 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200776
Alex Bilbieed944a32012-06-02 11:07:47 +0100777 if ($path === '')
Derek Jones32bf1862010-03-02 13:46:07 -0600778 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200779 array_shift($this->_ci_library_paths);
780 array_shift($this->_ci_model_paths);
781 array_shift($this->_ci_helper_paths);
782 array_shift($this->_ci_view_paths);
Korri3684d342012-04-17 00:35:08 -0400783 array_pop($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600784 }
785 else
786 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500787 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600788 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
789 {
790 if (($key = array_search($path, $this->{$var})) !== FALSE)
791 {
792 unset($this->{$var}[$key]);
793 }
794 }
David Behlercda768a2011-08-14 23:52:48 +0200795
Greg Akerf5c84022011-04-19 17:13:03 -0500796 if (isset($this->_ci_view_paths[$path.'views/']))
797 {
798 unset($this->_ci_view_paths[$path.'views/']);
799 }
Barry Mienydd671972010-10-04 16:33:58 +0200800
Derek Jones32bf1862010-03-02 13:46:07 -0600801 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
802 {
803 unset($config->_config_paths[$key]);
804 }
805 }
Barry Mienydd671972010-10-04 16:33:58 +0200806
Derek Jones32bf1862010-03-02 13:46:07 -0600807 // make sure the application default paths are still in the array
808 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
809 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
810 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500811 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600812 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200813
814 return $this;
Derek Jones32bf1862010-03-02 13:46:07 -0600815 }
816
817 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200818
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300820 * Internal CI Data Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300822 * Used to load views and files.
823 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 * Variables are prefixed with _ci_ to avoid symbol collision with
Andrey Andreeved4b2582012-10-27 17:46:52 +0300825 * variables made available to view files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300827 * @used-by CI_Loader::view()
828 * @used-by CI_Loader::file()
829 * @param array $_ci_data Data to load
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200830 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 */
Greg Akerf5c84022011-04-19 17:13:03 -0500832 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 {
834 // Set the default data variables
835 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
836 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300837 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 }
David Behlercda768a2011-08-14 23:52:48 +0200839
Greg Akerf5c84022011-04-19 17:13:03 -0500840 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000841
842 // Set the path to the requested file
Alex Bilbie40bd2a72012-06-02 16:04:15 +0100843 if (is_string($_ci_path) && $_ci_path !== '')
Greg Aker8807be32011-04-21 13:06:15 -0500844 {
845 $_ci_x = explode('/', $_ci_path);
846 $_ci_file = end($_ci_x);
847 }
848 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 {
850 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Alex Bilbieed944a32012-06-02 11:07:47 +0100851 $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500852
Joe McFrederick64f470b2012-08-18 12:29:56 -0400853 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
Greg Akerf5c84022011-04-19 17:13:03 -0500854 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400855 if (file_exists($_ci_view_file.$_ci_file))
Greg Akerf5c84022011-04-19 17:13:03 -0500856 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400857 $_ci_path = $_ci_view_file.$_ci_file;
Greg Akerf5c84022011-04-19 17:13:03 -0500858 $file_exists = TRUE;
859 break;
860 }
David Behlercda768a2011-08-14 23:52:48 +0200861
Greg Akerf5c84022011-04-19 17:13:03 -0500862 if ( ! $cascade)
863 {
864 break;
David Behlercda768a2011-08-14 23:52:48 +0200865 }
Greg Akerf5c84022011-04-19 17:13:03 -0500866 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 }
Barry Mienydd671972010-10-04 16:33:58 +0200868
Greg Akerf5c84022011-04-19 17:13:03 -0500869 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 {
871 show_error('Unable to load the requested file: '.$_ci_file);
872 }
Barry Mienydd671972010-10-04 16:33:58 +0200873
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 // This allows anything loaded using $this->load (views, files, etc.)
875 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500876 $_ci_CI =& get_instance();
877 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500879 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500881 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 }
883 }
884
885 /*
886 * Extract and cache variables
887 *
vlakoff02506182012-07-03 07:28:50 +0200888 * You can either set variables using the dedicated $this->load->vars()
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 * function or via the second parameter of this function. We'll merge
890 * the two types and cache them so that views that are embedded within
891 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200892 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 if (is_array($_ci_vars))
894 {
895 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
896 }
897 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200898
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 /*
900 * Buffer the output
901 *
902 * We buffer the output for two reasons:
903 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200904 * 2. So that the final rendered template can be post-processed by
dchill425628ba02012-08-08 12:05:45 -0400905 * the output class. Why do we need post processing? For one thing,
906 * in order to show the elapsed page load time. Unless we can
907 * intercept the content right before it's sent to the browser and
908 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 */
910 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200911
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 // If the PHP installation does not support short tags we'll
913 // do a little string replacement, changing the short tags
914 // to standard PHP echo statements.
Andrey Andreevf6274742014-02-20 18:05:58 +0200915 if ( ! is_php('5.4') && ! ini_get('short_open_tag') && config_item('rewrite_short_tags') === TRUE && function_usable('eval'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200917 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 }
919 else
920 {
921 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
922 }
Barry Mienydd671972010-10-04 16:33:58 +0200923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200925
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 // Return the file data if requested
927 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200928 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 $buffer = ob_get_contents();
930 @ob_end_clean();
931 return $buffer;
932 }
933
934 /*
935 * Flush the buffer... or buff the flusher?
936 *
937 * In order to permit views to be nested within
938 * other views, we need to flush the content back out whenever
939 * we are beyond the first level of output buffering so that
940 * it can be seen and included properly by the first included
941 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200942 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 if (ob_get_level() > $this->_ci_ob_level + 1)
944 {
945 ob_end_flush();
946 }
947 else
948 {
Greg Aker22f1a632010-11-10 15:34:35 -0600949 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 @ob_end_clean();
951 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200952
953 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 }
955
956 // --------------------------------------------------------------------
957
958 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300959 * Internal CI Class Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300961 * @used-by CI_Loader::library()
962 * @uses CI_Loader::_ci_init_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300964 * @param string $class Class name to load
965 * @param mixed $params Optional parameters to pass to the class constructor
966 * @param string $object_name Optional object name to assign to
Barry Mienydd671972010-10-04 16:33:58 +0200967 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 */
Greg Akerf5c84022011-04-19 17:13:03 -0500969 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200970 {
971 // Get the class name, and while we're at it trim any slashes.
972 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500974 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200975
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 // Was the path included with the class name?
977 // We look for a slash to determine this
Derek Jones32bf1862010-03-02 13:46:07 -0600978 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 {
Derek Jones32bf1862010-03-02 13:46:07 -0600980 // Extract the path
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200981 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200982
Derek Jones32bf1862010-03-02 13:46:07 -0600983 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200984 $class = substr($class, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 }
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200986 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200988 $subdir = '';
989 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000990
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200991 $class = ucfirst($class);
992 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
993
994 // Is this a class extension request?
995 if (file_exists($subclass))
996 {
Daniel1d3752c2013-04-14 16:41:57 -0400997 $baseclass = BASEPATH.'libraries/'.$subdir.$class.'.php';
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200998
999 if ( ! file_exists($baseclass))
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001001 log_message('error', 'Unable to load the requested class: '.$class);
1002 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
Barry Mienydd671972010-10-04 16:33:58 +02001004
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001005 // Safety: Was the class already loaded by a previous call?
1006 if (class_exists(config_item('subclass_prefix').$class, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001008 // Before we deem this to be a duplicate request, let's see
1009 // if a custom object name is being supplied. If so, we'll
1010 // return a new instance of the object
1011 if ($object_name !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001013 $CI =& get_instance();
1014 if ( ! isset($CI->$object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001016 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 }
Barry Mienydd671972010-10-04 16:33:58 +02001019
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001020 log_message('debug', $class.' class already loaded. Second attempt ignored.');
1021 return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 }
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001023
1024 include_once($baseclass);
1025 include_once($subclass);
1026
1027 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
1028 }
1029
vlakoff35672462013-02-15 01:36:04 +01001030 // Let's search for the requested library file and load it.
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001031 foreach ($this->_ci_library_paths as $path)
1032 {
1033 $filepath = $path.'libraries/'.$subdir.$class.'.php';
1034
1035 // Safety: Was the class already loaded by a previous call?
1036 if (class_exists($class, FALSE))
1037 {
1038 // Before we deem this to be a duplicate request, let's see
1039 // if a custom object name is being supplied. If so, we'll
1040 // return a new instance of the object
1041 if ($object_name !== NULL)
1042 {
1043 $CI =& get_instance();
1044 if ( ! isset($CI->$object_name))
1045 {
1046 return $this->_ci_init_class($class, '', $params, $object_name);
1047 }
1048 }
1049
1050 log_message('debug', $class.' class already loaded. Second attempt ignored.');
1051 return;
1052 }
1053 // Does the file exist? No? Bummer...
1054 elseif ( ! file_exists($filepath))
1055 {
1056 continue;
1057 }
1058
1059 include_once($filepath);
1060 return $this->_ci_init_class($class, '', $params, $object_name);
1061 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001062
Andrey Andreevd7297352012-01-07 22:53:14 +02001063 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Alex Bilbieed944a32012-06-02 11:07:47 +01001064 if ($subdir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001066 return $this->_ci_load_class($class.'/'.$class, $params, $object_name);
dchill42aee92652012-08-26 21:45:35 -04001067 }
Barry Mienydd671972010-10-04 16:33:58 +02001068
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 // If we got this far we were unable to find the requested class.
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001070 log_message('error', 'Unable to load the requested class: '.$class);
1071 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 }
Barry Mienydd671972010-10-04 16:33:58 +02001073
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 // --------------------------------------------------------------------
1075
1076 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001077 * Internal CI Class Instantiator
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001079 * @used-by CI_Loader::_ci_load_class()
1080 *
1081 * @param string $class Class name
1082 * @param string $prefix Class name prefix
1083 * @param array|null|bool $config Optional configuration to pass to the class constructor:
1084 * FALSE to skip;
1085 * NULL to search in config paths;
1086 * array containing configuration data
1087 * @param string $object_name Optional object name to assign to
Andrey Andreev94af3552012-03-26 23:10:42 +03001088 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001090 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001091 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001092 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 if ($config === NULL)
1094 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001095 // Fetch the config paths containing any package paths
1096 $config_component = $this->_ci_get_component('config');
1097
1098 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001100 // Break on the first found file, thus package files
1101 // are not overridden by default paths
1102 foreach ($config_component->_config_paths as $path)
1103 {
1104 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001105 // are case-sensitive with regard to file names. Check for environment
1106 // first, global next
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001107 if (file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001108 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001109 include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001110 break;
1111 }
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001112 elseif (file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001113 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001114 include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001115 break;
1116 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001117 elseif (file_exists($path.'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001118 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001119 include($path.'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001120 break;
1121 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001122 elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001123 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001124 include($path.'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001125 break;
1126 }
1127 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 }
1129 }
Barry Mienydd671972010-10-04 16:33:58 +02001130
Alex Bilbieed944a32012-06-02 11:07:47 +01001131 if ($prefix === '')
Barry Mienydd671972010-10-04 16:33:58 +02001132 {
Andrey Andreev49e68de2013-02-21 16:30:55 +02001133 if (class_exists('CI_'.$class, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 {
1135 $name = 'CI_'.$class;
1136 }
Andrey Andreev49e68de2013-02-21 16:30:55 +02001137 elseif (class_exists(config_item('subclass_prefix').$class, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 {
1139 $name = config_item('subclass_prefix').$class;
1140 }
1141 else
1142 {
1143 $name = $class;
1144 }
1145 }
1146 else
1147 {
1148 $name = $prefix.$class;
1149 }
Barry Mienydd671972010-10-04 16:33:58 +02001150
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 // Is the class name valid?
Andrey Andreev49e68de2013-02-21 16:30:55 +02001152 if ( ! class_exists($name, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001154 log_message('error', 'Non-existent class: '.$name);
jonnueee2df62012-07-16 13:06:16 +01001155 show_error('Non-existent class: '.$name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 }
Barry Mienydd671972010-10-04 16:33:58 +02001157
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001159 // Was a custom class name supplied? If so we'll use it
Andrey Andreev519f87a2013-07-23 17:16:10 +03001160 if (empty($object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 {
Andrey Andreev519f87a2013-07-23 17:16:10 +03001162 $object_name = strtolower($class);
1163 if (isset($this->_ci_varmap[$object_name]))
1164 {
1165 $object_name = $this->_ci_varmap[$object_name];
1166 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 }
Andrey Andreev519f87a2013-07-23 17:16:10 +03001168
1169 // Don't overwrite existing properties
1170 $CI =& get_instance();
1171 if (isset($CI->$object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 {
Andrey Andreev519f87a2013-07-23 17:16:10 +03001173 if ($CI->$object_name instanceof $name)
1174 {
1175 log_message('debug', $class." has already been instantiated as '".$object_name."'. Second attempt aborted.");
1176 return;
1177 }
1178
1179 show_error("Resource '".$object_name."' already exists and is not a ".$class." instance.");
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 }
1181
Barry Mienydd671972010-10-04 16:33:58 +02001182 // Save the class name and object name
Andrey Andreev519f87a2013-07-23 17:16:10 +03001183 $this->_ci_classes[$object_name] = $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001184
Barry Mienydd671972010-10-04 16:33:58 +02001185 // Instantiate the class
Andrey Andreev519f87a2013-07-23 17:16:10 +03001186 $CI->$object_name = isset($config)
1187 ? new $name($config)
1188 : new $name();
Barry Mienydd671972010-10-04 16:33:58 +02001189 }
1190
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001192
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001194 * CI Autoloader
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001196 * Loads component listed in the config/autoload.php file.
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 *
Andrey Andreevcdac2482012-11-03 18:09:01 +02001198 * @used-by CI_Loader::initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 * @return void
1200 */
Shane Pearson665baec2011-08-22 18:52:19 -05001201 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001202 {
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001203 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001204 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001205 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001206 }
1207 else
1208 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001209 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001210 }
Barry Mienydd671972010-10-04 16:33:58 +02001211
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 if ( ! isset($autoload))
1213 {
1214 return FALSE;
1215 }
Barry Mienydd671972010-10-04 16:33:58 +02001216
Phil Sturgeon9730c752010-12-15 10:50:15 +00001217 // Autoload packages
1218 if (isset($autoload['packages']))
1219 {
1220 foreach ($autoload['packages'] as $package_path)
1221 {
1222 $this->add_package_path($package_path);
1223 }
1224 }
1225
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 // Load any custom config file
1227 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001228 {
Andrey Andreev623227f2014-03-24 12:46:25 +02001229 foreach ($autoload['config'] as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 {
Andrey Andreev689d41c2014-03-24 17:28:40 +02001231 $this->config($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 }
Barry Mienydd671972010-10-04 16:33:58 +02001233 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001234
Derek Jonesc6da5032010-03-09 20:44:27 -06001235 // Autoload helpers and languages
1236 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001237 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001238 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 {
1240 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001241 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 }
1243
Andrey Andreev9ab70a82013-07-23 00:09:26 +03001244 // Autoload drivers
1245 if (isset($autoload['drivers']))
1246 {
1247 foreach ($autoload['drivers'] as $item)
1248 {
1249 $this->driver($item);
1250 }
1251 }
1252
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001254 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 {
1256 // Load the database driver.
1257 if (in_array('database', $autoload['libraries']))
1258 {
1259 $this->database();
1260 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1261 }
Barry Mienydd671972010-10-04 16:33:58 +02001262
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 // Load all other libraries
1264 foreach ($autoload['libraries'] as $item)
1265 {
1266 $this->library($item);
1267 }
Barry Mienydd671972010-10-04 16:33:58 +02001268 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001269
1270 // Autoload models
1271 if (isset($autoload['model']))
1272 {
1273 $this->model($autoload['model']);
1274 }
Barry Mienydd671972010-10-04 16:33:58 +02001275 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001276
1277 // --------------------------------------------------------------------
1278
1279 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001280 * CI Object to Array translator
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001282 * Takes an object as input and converts the class variables to
1283 * an associative array with key/value pairs.
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001285 * @param object $object Object data to translate
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 * @return array
1287 */
Greg Akerf5c84022011-04-19 17:13:03 -05001288 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001290 return is_object($object) ? get_object_vars($object) : $object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 }
1292
1293 // --------------------------------------------------------------------
1294
1295 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001296 * CI Component getter
Derek Jones32bf1862010-03-02 13:46:07 -06001297 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001298 * Get a reference to a specific library or model.
1299 *
1300 * @param string $component Component name
Derek Jones32bf1862010-03-02 13:46:07 -06001301 * @return bool
1302 */
Greg Akerf5c84022011-04-19 17:13:03 -05001303 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001304 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001305 $CI =& get_instance();
1306 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001307 }
1308
1309 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001310
Derek Jones32bf1862010-03-02 13:46:07 -06001311 /**
1312 * Prep filename
1313 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001314 * This function prepares filenames of various items to
1315 * make their loading more reliable.
Derek Jones32bf1862010-03-02 13:46:07 -06001316 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001317 * @param string|string[] $filename Filename(s)
1318 * @param string $extension Filename extension
Derek Jones32bf1862010-03-02 13:46:07 -06001319 * @return array
1320 */
Greg Akerf5c84022011-04-19 17:13:03 -05001321 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001322 {
1323 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001324 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001325 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001326 }
1327 else
1328 {
1329 foreach ($filename as $key => $val)
1330 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001331 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001332 }
Barry Mienydd671972010-10-04 16:33:58 +02001333
Derek Jones32bf1862010-03-02 13:46:07 -06001334 return $filename;
1335 }
1336 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001337
Derek Allard2067d1a2008-11-13 22:59:24 +00001338}
1339
1340/* End of file Loader.php */
Tomaz Lovrec8bb20a12013-10-16 12:34:36 +02001341/* Location: ./system/core/Loader.php */