blob: 5747b7918436a4969242a48e6b7de6a8c77ad7e4 [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 /**
David Behlercda768a2011-08-14 23:52:48 +020079 * List of cached variables
80 *
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 $_ci_cached_vars = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030084
David Behlercda768a2011-08-14 23:52:48 +020085 /**
86 * List of loaded classes
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_classes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030091
David Behlercda768a2011-08-14 23:52:48 +020092 /**
David Behlercda768a2011-08-14 23:52:48 +020093 * List of loaded models
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_models = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030098
David Behlercda768a2011-08-14 23:52:48 +020099 /**
100 * List of loaded helpers
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_helpers = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300105
David Behlercda768a2011-08-14 23:52:48 +0200106 /**
107 * List of class name mappings
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_varmap = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400112 'unit_test' => 'unit',
113 'user_agent' => 'agent'
114 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000115
Andrey Andreev519f87a2013-07-23 17:16:10 +0300116 // --------------------------------------------------------------------
117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300119 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 *
Andrey Andreevcdac2482012-11-03 18:09:01 +0200121 * Sets component load paths, gets the initial output buffering level.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300122 *
123 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 */
Greg Akerf5c84022011-04-19 17:13:03 -0500125 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +0200126 {
Andrey Andreev519f87a2013-07-23 17:16:10 +0300127 $this->_ci_ob_level = ob_get_level();
128 $this->_ci_classes =& is_loaded();
David Behlercda768a2011-08-14 23:52:48 +0200129
Andrey Andreevd7297352012-01-07 22:53:14 +0200130 log_message('debug', 'Loader Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200134
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500135 /**
Andrey Andreevcdac2482012-11-03 18:09:01 +0200136 * Initializer
137 *
138 * @todo Figure out a way to move this to the constructor
139 * without breaking *package_path*() methods.
140 * @uses CI_Loader::_ci_autoloader()
141 * @used-by CI_Controller::__construct()
142 * @return void
143 */
144 public function initialize()
145 {
Andrey Andreevcdac2482012-11-03 18:09:01 +0200146 $this->_ci_autoloader();
147 }
148
149 // --------------------------------------------------------------------
150
151 /**
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500152 * Is Loaded
153 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300154 * A utility method to test if a class is in the self::$_ci_classes array.
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500155 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300156 * @used-by Mainly used by Form Helper function _get_validation_object().
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500157 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300158 * @param string $class Class name to check for
159 * @return string|bool Class object name if loaded or FALSE
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500160 */
161 public function is_loaded($class)
162 {
Andrey Andreev519f87a2013-07-23 17:16:10 +0300163 return array_search(ucfirst($class), $this->_ci_classes, TRUE);
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500164 }
165
166 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300169 * Library Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300171 * Loads and instantiates libraries.
172 * Designed to be called from application controllers.
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300174 * @param string $library Library name
175 * @param array $params Optional parameters to pass to the library class constructor
176 * @param string $object_name An optional object name to assign to
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200177 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200178 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200179 public function library($library, $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreev519f87a2013-07-23 17:16:10 +0300181 if (empty($library))
182 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200183 return $this;
Andrey Andreev519f87a2013-07-23 17:16:10 +0300184 }
185 elseif (is_array($library))
Greg Akerce433962010-10-12 09:29:35 -0500186 {
Andrey Andreev88cf55b2014-01-17 15:38:30 +0200187 foreach ($library as $key => $value)
Greg Akerce433962010-10-12 09:29:35 -0500188 {
Andrey Andreev88cf55b2014-01-17 15:38:30 +0200189 if (is_int($key))
190 {
191 $this->library($value, $params);
192 }
193 else
194 {
195 $this->library($key, $params, $value);
196 }
Greg Akerce433962010-10-12 09:29:35 -0500197 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000198
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200199 return $this;
Greg Akerce433962010-10-12 09:29:35 -0500200 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000201
vlakoff1228fe22013-01-14 01:30:09 +0100202 if ($params !== NULL && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
204 $params = NULL;
205 }
206
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600207 $this->_ci_load_class($library, $params, $object_name);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200208 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
210
211 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 /**
214 * Model Loader
215 *
James7b272ff2014-03-20 19:02:37 +0000216 * Loads and instantiates models.
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300218 * @param string $model Model name
219 * @param string $name An optional object name to assign to
220 * @param bool $db_conn An optional database connection configuration to initialize
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200221 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200222 */
Greg Akerf5c84022011-04-19 17:13:03 -0500223 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200224 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300225 if (empty($model))
226 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200227 return $this;
Andrey Andreeved4b2582012-10-27 17:46:52 +0300228 }
229 elseif (is_array($model))
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
Andrey Andreev5a519db2013-01-12 04:19:19 +0200231 foreach ($model as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
Ahmad Anbar4005e3c2013-09-12 23:33:28 +0300233 is_int($key) ? $this->model($value, '', $db_conn) : $this->model($key, $value, $db_conn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200235
236 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 }
238
Derek Jones32bf1862010-03-02 13:46:07 -0600239 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600242 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
Derek Jones32bf1862010-03-02 13:46:07 -0600244 // The path is in front of the last slash
Andrey Andreevd47baab2012-01-09 16:56:46 +0200245 $path = substr($model, 0, ++$last_slash);
Derek Jones32bf1862010-03-02 13:46:07 -0600246
247 // And the model name behind it
Andrey Andreevd47baab2012-01-09 16:56:46 +0200248 $model = substr($model, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Phil Sturgeon10d78f62012-06-04 14:41:53 -0500251 if (empty($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
253 $name = $model;
254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 if (in_array($name, $this->_ci_models, TRUE))
257 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200258 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 $CI =& get_instance();
262 if (isset($CI->$name))
263 {
264 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Andrey Andreev20292312013-07-22 14:29:10 +0300267 if ($db_conn !== FALSE && ! class_exists('CI_DB', FALSE))
268 {
269 if ($db_conn === TRUE)
270 {
271 $db_conn = '';
272 }
273
274 $CI->load->database($db_conn, FALSE, TRUE);
275 }
276
277 if ( ! class_exists('CI_Model', FALSE))
278 {
279 load_class('Model', 'core');
280 }
281
282 $model = ucfirst(strtolower($model));
Derek Allard2067d1a2008-11-13 22:59:24 +0000283
Derek Jones32bf1862010-03-02 13:46:07 -0600284 foreach ($this->_ci_model_paths as $mod_path)
285 {
Greg Aker3a746652011-04-19 10:59:47 -0500286 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600287 {
288 continue;
289 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000290
Greg Aker3a746652011-04-19 10:59:47 -0500291 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600292
Derek Jones32bf1862010-03-02 13:46:07 -0600293 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600294 $this->_ci_models[] = $name;
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200295 return $this;
Derek Jones32bf1862010-03-02 13:46:07 -0600296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Jones32bf1862010-03-02 13:46:07 -0600298 // couldn't find the model
299 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 /**
305 * Database Loader
306 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300307 * @param mixed $params Database configuration options
308 * @param bool $return Whether to return the database object
309 * @param bool $query_builder Whether to enable Query Builder
310 * (overrides the configuration setting)
311 *
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200312 * @return object|bool Database object if $return is set to TRUE,
313 * FALSE on failure, CI_Loader instance in any other case
Barry Mienydd671972010-10-04 16:33:58 +0200314 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000315 public function database($params = '', $return = FALSE, $query_builder = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
317 // Grab the super object
318 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // Do we even need to load the database class?
Andrey Andreev9d0ab042012-10-24 21:47:39 +0300321 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 +0000322 {
323 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200324 }
325
Greg Aker3a746652011-04-19 10:59:47 -0500326 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000327
328 if ($return === TRUE)
329 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000330 return DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Andrey Andreevd7297352012-01-07 22:53:14 +0200333 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // reference errors with some configurations
335 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // Load the DB class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000338 $CI->db =& DB($params, $query_builder);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200339 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // --------------------------------------------------------------------
343
344 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300345 * Load the Database Utilities Class
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200347 * @param object $db Database object
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200348 * @param bool $return Whether to return the DB Utilities class object or not
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200349 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200350 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200351 public function dbutil($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 $CI =& get_instance();
354
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200355 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
356 {
357 class_exists('CI_DB', FALSE) OR $this->database();
358 $db =& $CI->db;
359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Greg Aker3a746652011-04-19 10:59:47 -0500361 require_once(BASEPATH.'database/DB_utility.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200362 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_utility.php');
363 $class = 'CI_DB_'.$db->dbdriver.'_utility';
Derek Allard2067d1a2008-11-13 22:59:24 +0000364
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200365 if ($return === TRUE)
366 {
367 return new $class($db);
368 }
369
370 $CI->dbutil = new $class($db);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200371 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 // --------------------------------------------------------------------
375
376 /**
377 * Load the Database Forge Class
378 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200379 * @param object $db Database object
380 * @param bool $return Whether to return the DB Forge class object or not
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200381 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200382 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200383 public function dbforge($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200385 $CI =& get_instance();
386 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200388 class_exists('CI_DB', FALSE) OR $this->database();
389 $db =& $CI->db;
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 }
Barry Mienydd671972010-10-04 16:33:58 +0200391
Greg Aker3a746652011-04-19 10:59:47 -0500392 require_once(BASEPATH.'database/DB_forge.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200393 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_forge.php');
Andrey Andreeva287a342012-11-05 23:19:59 +0200394
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200395 if ( ! empty($db->subdriver))
Andrey Andreeva287a342012-11-05 23:19:59 +0200396 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200397 $driver_path = BASEPATH.'database/drivers/'.$db->dbdriver.'/subdrivers/'.$db->dbdriver.'_'.$db->subdriver.'_forge.php';
Andrey Andreeva287a342012-11-05 23:19:59 +0200398 if (file_exists($driver_path))
399 {
400 require_once($driver_path);
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200401 $class = 'CI_DB_'.$db->dbdriver.'_'.$db->subdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200402 }
403 }
404 else
405 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200406 $class = 'CI_DB_'.$db->dbdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200407 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000408
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200409 if ($return === TRUE)
410 {
411 return new $class($db);
412 }
413
414 $CI->dbforge = new $class($db);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200415 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300421 * View Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300423 * Loads "view" files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300425 * @param string $view View name
426 * @param array $vars An associative array of data
427 * to be extracted for use in the view
428 * @param bool $return Whether to return the view output
429 * or leave it to the Output class
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200430 * @return object|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 */
Greg Akerf5c84022011-04-19 17:13:03 -0500432 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300440 * Generic File Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300442 * @param string $path File path
443 * @param bool $return Whether to return the file output
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200444 * @return object|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 */
Greg Akerf5c84022011-04-19 17:13:03 -0500446 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
448 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 /**
454 * Set Variables
455 *
456 * Once variables are set they become available within
457 * the controller class and its "view" files.
458 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300459 * @param array|object|string $vars
460 * An associative array or object containing values
461 * to be set, or a value's name if string
462 * @param string $val Value to set, only used if $vars is a string
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200463 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200465 public function vars($vars, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
vlakoffa5e0ea82013-02-27 18:17:35 +0100467 if (is_string($vars))
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
469 $vars = array($vars => $val);
470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200473
Andrey Andreev94af3552012-03-26 23:10:42 +0300474 if (is_array($vars) && count($vars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
476 foreach ($vars as $key => $val)
477 {
478 $this->_ci_cached_vars[$key] = $val;
479 }
480 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200481
482 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 /**
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200488 * Clear Cached Variables
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200489 *
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200490 * Clears the cached variables.
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200491 *
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200492 * @return object
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200493 */
494 public function clear_vars()
495 {
496 $this->_ci_cached_vars = array();
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200497 return $this;
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200498 }
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200499
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200500 // --------------------------------------------------------------------
501
502 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600503 * Get Variable
504 *
505 * Check if a variable is set and retrieve it.
506 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300507 * @param string $key Variable name
508 * @return mixed The variable or NULL if not found
Phil Sturgeon8731f642011-07-22 16:11:34 -0600509 */
510 public function get_var($key)
511 {
512 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
513 }
514
515 // --------------------------------------------------------------------
516
517 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600518 * Get Variables
519 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300520 * Retrieves all loaded variables.
Shane Pearson81dd2232011-11-18 20:49:35 -0600521 *
522 * @return array
523 */
524 public function get_vars()
525 {
526 return $this->_ci_cached_vars;
527 }
528
529 // --------------------------------------------------------------------
530
531 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300532 * Helper Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300534 * @param string|string[] $helpers Helper name(s)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200535 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 */
Greg Akerf5c84022011-04-19 17:13:03 -0500537 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200538 {
Derek Jones32bf1862010-03-02 13:46:07 -0600539 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200540 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 if (isset($this->_ci_helpers[$helper]))
542 {
543 continue;
544 }
Derek Jones32bf1862010-03-02 13:46:07 -0600545
Barry Mienydd671972010-10-04 16:33:58 +0200546 // Is this a helper extension request?
Andrey Andreev12d7b462012-11-12 13:42:09 +0200547 $ext_helper = config_item('subclass_prefix').$helper;
548 $ext_loaded = FALSE;
549 foreach ($this->_ci_helper_paths as $path)
550 {
551 if (file_exists($path.'helpers/'.$ext_helper.'.php'))
552 {
553 include_once($path.'helpers/'.$ext_helper.'.php');
554 $ext_loaded = TRUE;
555 }
556 }
557
558 // If we have loaded extensions - check if the base one is here
559 if ($ext_loaded === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
Greg Aker3a746652011-04-19 10:59:47 -0500561 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 if ( ! file_exists($base_helper))
563 {
Greg Aker3a746652011-04-19 10:59:47 -0500564 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 }
Barry Mienydd671972010-10-04 16:33:58 +0200566
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 include_once($base_helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600568 $this->_ci_helpers[$helper] = TRUE;
569 log_message('debug', 'Helper loaded: '.$helper);
570 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 }
Barry Mienydd671972010-10-04 16:33:58 +0200572
Andrey Andreev12d7b462012-11-12 13:42:09 +0200573 // No extensions found ... try loading regular helpers and/or overrides
Derek Jones32bf1862010-03-02 13:46:07 -0600574 foreach ($this->_ci_helper_paths as $path)
575 {
Greg Aker3a746652011-04-19 10:59:47 -0500576 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200577 {
Greg Aker3a746652011-04-19 10:59:47 -0500578 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600579
580 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200581 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600582 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 }
584 }
585
Derek Jones32bf1862010-03-02 13:46:07 -0600586 // unable to load the helper
587 if ( ! isset($this->_ci_helpers[$helper]))
588 {
Greg Aker3a746652011-04-19 10:59:47 -0500589 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600590 }
Barry Mienydd671972010-10-04 16:33:58 +0200591 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200592
593 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 }
Barry Mienydd671972010-10-04 16:33:58 +0200595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 /**
599 * Load Helpers
600 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300601 * An alias for the helper() method in case the developer has
602 * written the plural form of it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300604 * @uses CI_Loader::helper()
605 * @param string|string[] $helpers Helper name(s)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200606 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 */
Greg Akerf5c84022011-04-19 17:13:03 -0500608 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200610 return $this->helper($helpers);
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 }
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300616 * Language Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300618 * Loads language files.
619 *
620 * @param string|string[] $files List of language file names to load
621 * @param string Language name
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200622 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200624 public function language($files, $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
626 $CI =& get_instance();
Andrey Andreeved4b2582012-10-27 17:46:52 +0300627 is_array($files) OR $files = array($files);
Derek Allard2067d1a2008-11-13 22:59:24 +0000628
Andrey Andreeved4b2582012-10-27 17:46:52 +0300629 foreach ($files as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200630 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 $CI->lang->load($langfile, $lang);
632 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200633
634 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 }
Barry Mienydd671972010-10-04 16:33:58 +0200636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300640 * Config Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300642 * Loads a config file (an alias for CI_Config::load()).
643 *
644 * @uses CI_Config::load()
645 * @param string $file Configuration file name
646 * @param bool $use_sections Whether configuration values should be loaded into their own section
647 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
648 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200650 public function config($file, $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200651 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200652 return get_instance()->config->load($file, $use_sections, $fail_gracefully);
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 }
654
655 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600656
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300658 * Driver Loader
Derek Jones8dca0412010-03-05 13:01:44 -0600659 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300660 * Loads a driver library.
Derek Jones8dca0412010-03-05 13:01:44 -0600661 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300662 * @param string|string[] $library Driver name(s)
663 * @param array $params Optional parameters to pass to the driver
664 * @param string $object_name An optional object name to assign to
665 *
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200666 * @return object|bool Object or FALSE on failure if $library is a string
667 * and $object_name is set. CI_Loader instance otherwise.
Derek Jones8dca0412010-03-05 13:01:44 -0600668 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200669 public function driver($library, $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600670 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800671 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800672 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800673 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800674 {
675 $this->driver($driver);
676 }
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800677
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200678 return $this;
679 }
680 elseif (empty($library))
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200681 {
682 return FALSE;
683 }
684
vlakofffadb8222013-05-12 10:57:09 +0200685 if ( ! class_exists('CI_Driver_Library', FALSE))
dchill426262d052012-11-24 18:41:13 -0500686 {
687 // We aren't instantiating an object here, just making the base class available
688 require BASEPATH.'libraries/Driver.php';
689 }
690
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600691 // We can save the loader some time since Drivers will *always* be in a subfolder,
692 // and typically identically named to the library
693 if ( ! strpos($library, '/'))
694 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500695 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600696 }
Barry Mienydd671972010-10-04 16:33:58 +0200697
Derek Jones8dca0412010-03-05 13:01:44 -0600698 return $this->library($library, $params, $object_name);
699 }
700
701 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200702
Derek Jones8dca0412010-03-05 13:01:44 -0600703 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600704 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300706 * Prepends a parent path to the library, model, helper and config
707 * path arrays.
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300709 * @see CI_Loader::$_ci_library_paths
710 * @see CI_Loader::$_ci_model_paths
711 * @see CI_Loader::$_ci_helper_paths
712 * @see CI_Config::$_config_paths
713 *
714 * @param string $path Path to add
715 * @param bool $view_cascade (default: TRUE)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200716 * @return object
Derek Jones32bf1862010-03-02 13:46:07 -0600717 */
Andrey Andreevcce91802012-06-12 13:25:31 +0300718 public function add_package_path($path, $view_cascade = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600719 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500720 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200721
Derek Jones32bf1862010-03-02 13:46:07 -0600722 array_unshift($this->_ci_library_paths, $path);
723 array_unshift($this->_ci_model_paths, $path);
724 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200725
Greg Akerf5c84022011-04-19 17:13:03 -0500726 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
727
Derek Jones32bf1862010-03-02 13:46:07 -0600728 // Add config file path
729 $config =& $this->_ci_get_component('config');
and-ersb3ec9422013-01-03 16:05:12 +0100730 $config->_config_paths[] = $path;
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200731
732 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 }
734
735 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600736
737 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000738 * Get Package Paths
739 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300740 * Return a list of all package paths.
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000741 *
vlakoffcdc61132013-05-10 16:47:47 +0200742 * @param bool $include_base Whether to include BASEPATH (default: FALSE)
Andrey Andreeved4b2582012-10-27 17:46:52 +0300743 * @return array
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000744 */
Greg Akerf5c84022011-04-19 17:13:03 -0500745 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000746 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300747 return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000748 }
749
750 // --------------------------------------------------------------------
751
752 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600753 * Remove Package Path
754 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300755 * Remove a path from the library, model, helper and/or config
756 * path arrays if it exists. If no path is provided, the most recently
757 * added path will be removed removed.
Derek Jones32bf1862010-03-02 13:46:07 -0600758 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300759 * @param string $path Path to remove
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200760 * @return object
Derek Jones32bf1862010-03-02 13:46:07 -0600761 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300762 public function remove_package_path($path = '')
Derek Jones32bf1862010-03-02 13:46:07 -0600763 {
764 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200765
Alex Bilbieed944a32012-06-02 11:07:47 +0100766 if ($path === '')
Derek Jones32bf1862010-03-02 13:46:07 -0600767 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200768 array_shift($this->_ci_library_paths);
769 array_shift($this->_ci_model_paths);
770 array_shift($this->_ci_helper_paths);
771 array_shift($this->_ci_view_paths);
Korri3684d342012-04-17 00:35:08 -0400772 array_pop($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600773 }
774 else
775 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500776 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600777 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
778 {
779 if (($key = array_search($path, $this->{$var})) !== FALSE)
780 {
781 unset($this->{$var}[$key]);
782 }
783 }
David Behlercda768a2011-08-14 23:52:48 +0200784
Greg Akerf5c84022011-04-19 17:13:03 -0500785 if (isset($this->_ci_view_paths[$path.'views/']))
786 {
787 unset($this->_ci_view_paths[$path.'views/']);
788 }
Barry Mienydd671972010-10-04 16:33:58 +0200789
Derek Jones32bf1862010-03-02 13:46:07 -0600790 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
791 {
792 unset($config->_config_paths[$key]);
793 }
794 }
Barry Mienydd671972010-10-04 16:33:58 +0200795
Derek Jones32bf1862010-03-02 13:46:07 -0600796 // make sure the application default paths are still in the array
797 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
798 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
799 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500800 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600801 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200802
803 return $this;
Derek Jones32bf1862010-03-02 13:46:07 -0600804 }
805
806 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200807
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300809 * Internal CI Data Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300811 * Used to load views and files.
812 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 * Variables are prefixed with _ci_ to avoid symbol collision with
Andrey Andreeved4b2582012-10-27 17:46:52 +0300814 * variables made available to view files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300816 * @used-by CI_Loader::view()
817 * @used-by CI_Loader::file()
818 * @param array $_ci_data Data to load
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200819 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 */
Greg Akerf5c84022011-04-19 17:13:03 -0500821 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 {
823 // Set the default data variables
824 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
825 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300826 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 }
David Behlercda768a2011-08-14 23:52:48 +0200828
Greg Akerf5c84022011-04-19 17:13:03 -0500829 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000830
831 // Set the path to the requested file
Alex Bilbie40bd2a72012-06-02 16:04:15 +0100832 if (is_string($_ci_path) && $_ci_path !== '')
Greg Aker8807be32011-04-21 13:06:15 -0500833 {
834 $_ci_x = explode('/', $_ci_path);
835 $_ci_file = end($_ci_x);
836 }
837 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 {
839 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Alex Bilbieed944a32012-06-02 11:07:47 +0100840 $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500841
Joe McFrederick64f470b2012-08-18 12:29:56 -0400842 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
Greg Akerf5c84022011-04-19 17:13:03 -0500843 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400844 if (file_exists($_ci_view_file.$_ci_file))
Greg Akerf5c84022011-04-19 17:13:03 -0500845 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400846 $_ci_path = $_ci_view_file.$_ci_file;
Greg Akerf5c84022011-04-19 17:13:03 -0500847 $file_exists = TRUE;
848 break;
849 }
David Behlercda768a2011-08-14 23:52:48 +0200850
Greg Akerf5c84022011-04-19 17:13:03 -0500851 if ( ! $cascade)
852 {
853 break;
David Behlercda768a2011-08-14 23:52:48 +0200854 }
Greg Akerf5c84022011-04-19 17:13:03 -0500855 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 }
Barry Mienydd671972010-10-04 16:33:58 +0200857
Greg Akerf5c84022011-04-19 17:13:03 -0500858 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 {
860 show_error('Unable to load the requested file: '.$_ci_file);
861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 // This allows anything loaded using $this->load (views, files, etc.)
864 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500865 $_ci_CI =& get_instance();
866 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500868 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500870 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 }
872 }
873
874 /*
875 * Extract and cache variables
876 *
vlakoff02506182012-07-03 07:28:50 +0200877 * You can either set variables using the dedicated $this->load->vars()
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 * function or via the second parameter of this function. We'll merge
879 * the two types and cache them so that views that are embedded within
880 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200881 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 if (is_array($_ci_vars))
883 {
884 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
885 }
886 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200887
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 /*
889 * Buffer the output
890 *
891 * We buffer the output for two reasons:
892 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200893 * 2. So that the final rendered template can be post-processed by
dchill425628ba02012-08-08 12:05:45 -0400894 * the output class. Why do we need post processing? For one thing,
895 * in order to show the elapsed page load time. Unless we can
896 * intercept the content right before it's sent to the browser and
897 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 */
899 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 // If the PHP installation does not support short tags we'll
902 // do a little string replacement, changing the short tags
903 // to standard PHP echo statements.
Andrey Andreevf6274742014-02-20 18:05:58 +0200904 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 +0000905 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200906 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 }
908 else
909 {
910 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
911 }
Barry Mienydd671972010-10-04 16:33:58 +0200912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200914
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 // Return the file data if requested
916 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200917 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 $buffer = ob_get_contents();
919 @ob_end_clean();
920 return $buffer;
921 }
922
923 /*
924 * Flush the buffer... or buff the flusher?
925 *
926 * In order to permit views to be nested within
927 * other views, we need to flush the content back out whenever
928 * we are beyond the first level of output buffering so that
929 * it can be seen and included properly by the first included
930 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200931 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 if (ob_get_level() > $this->_ci_ob_level + 1)
933 {
934 ob_end_flush();
935 }
936 else
937 {
Greg Aker22f1a632010-11-10 15:34:35 -0600938 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 @ob_end_clean();
940 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200941
942 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 }
944
945 // --------------------------------------------------------------------
946
947 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300948 * Internal CI Class Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300950 * @used-by CI_Loader::library()
951 * @uses CI_Loader::_ci_init_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300953 * @param string $class Class name to load
954 * @param mixed $params Optional parameters to pass to the class constructor
955 * @param string $object_name Optional object name to assign to
Barry Mienydd671972010-10-04 16:33:58 +0200956 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 */
Greg Akerf5c84022011-04-19 17:13:03 -0500958 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200959 {
960 // Get the class name, and while we're at it trim any slashes.
961 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500963 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 // Was the path included with the class name?
966 // We look for a slash to determine this
Derek Jones32bf1862010-03-02 13:46:07 -0600967 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 {
Derek Jones32bf1862010-03-02 13:46:07 -0600969 // Extract the path
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200970 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200971
Derek Jones32bf1862010-03-02 13:46:07 -0600972 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200973 $class = substr($class, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 }
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200975 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200977 $subdir = '';
978 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000979
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200980 $class = ucfirst($class);
981 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
982
983 // Is this a class extension request?
984 if (file_exists($subclass))
985 {
Daniel1d3752c2013-04-14 16:41:57 -0400986 $baseclass = BASEPATH.'libraries/'.$subdir.$class.'.php';
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200987
988 if ( ! file_exists($baseclass))
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200990 log_message('error', 'Unable to load the requested class: '.$class);
991 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 }
Barry Mienydd671972010-10-04 16:33:58 +0200993
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200994 // Safety: Was the class already loaded by a previous call?
995 if (class_exists(config_item('subclass_prefix').$class, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200997 // Before we deem this to be a duplicate request, let's see
998 // if a custom object name is being supplied. If so, we'll
999 // return a new instance of the object
1000 if ($object_name !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001002 $CI =& get_instance();
1003 if ( ! isset($CI->$object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001005 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001009 log_message('debug', $class.' class already loaded. Second attempt ignored.');
1010 return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 }
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001012
1013 include_once($baseclass);
1014 include_once($subclass);
1015
1016 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
1017 }
1018
vlakoff35672462013-02-15 01:36:04 +01001019 // Let's search for the requested library file and load it.
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001020 foreach ($this->_ci_library_paths as $path)
1021 {
1022 $filepath = $path.'libraries/'.$subdir.$class.'.php';
1023
1024 // Safety: Was the class already loaded by a previous call?
1025 if (class_exists($class, FALSE))
1026 {
1027 // Before we deem this to be a duplicate request, let's see
1028 // if a custom object name is being supplied. If so, we'll
1029 // return a new instance of the object
1030 if ($object_name !== NULL)
1031 {
1032 $CI =& get_instance();
1033 if ( ! isset($CI->$object_name))
1034 {
1035 return $this->_ci_init_class($class, '', $params, $object_name);
1036 }
1037 }
1038
1039 log_message('debug', $class.' class already loaded. Second attempt ignored.');
1040 return;
1041 }
1042 // Does the file exist? No? Bummer...
1043 elseif ( ! file_exists($filepath))
1044 {
1045 continue;
1046 }
1047
1048 include_once($filepath);
1049 return $this->_ci_init_class($class, '', $params, $object_name);
1050 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001051
Andrey Andreevd7297352012-01-07 22:53:14 +02001052 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Alex Bilbieed944a32012-06-02 11:07:47 +01001053 if ($subdir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001055 return $this->_ci_load_class($class.'/'.$class, $params, $object_name);
dchill42aee92652012-08-26 21:45:35 -04001056 }
Barry Mienydd671972010-10-04 16:33:58 +02001057
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 // If we got this far we were unable to find the requested class.
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001059 log_message('error', 'Unable to load the requested class: '.$class);
1060 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 }
Barry Mienydd671972010-10-04 16:33:58 +02001062
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 // --------------------------------------------------------------------
1064
1065 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001066 * Internal CI Class Instantiator
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001068 * @used-by CI_Loader::_ci_load_class()
1069 *
1070 * @param string $class Class name
1071 * @param string $prefix Class name prefix
1072 * @param array|null|bool $config Optional configuration to pass to the class constructor:
1073 * FALSE to skip;
1074 * NULL to search in config paths;
1075 * array containing configuration data
1076 * @param string $object_name Optional object name to assign to
Andrey Andreev94af3552012-03-26 23:10:42 +03001077 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001079 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001080 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001081 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 if ($config === NULL)
1083 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001084 // Fetch the config paths containing any package paths
1085 $config_component = $this->_ci_get_component('config');
1086
1087 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001089 // Break on the first found file, thus package files
1090 // are not overridden by default paths
1091 foreach ($config_component->_config_paths as $path)
1092 {
1093 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001094 // are case-sensitive with regard to file names. Check for environment
1095 // first, global next
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001096 if (file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001097 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001098 include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001099 break;
1100 }
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001101 elseif (file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001102 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001103 include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001104 break;
1105 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001106 elseif (file_exists($path.'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001107 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001108 include($path.'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001109 break;
1110 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001111 elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001112 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001113 include($path.'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001114 break;
1115 }
1116 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 }
1118 }
Barry Mienydd671972010-10-04 16:33:58 +02001119
Alex Bilbieed944a32012-06-02 11:07:47 +01001120 if ($prefix === '')
Barry Mienydd671972010-10-04 16:33:58 +02001121 {
Andrey Andreev49e68de2013-02-21 16:30:55 +02001122 if (class_exists('CI_'.$class, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
1124 $name = 'CI_'.$class;
1125 }
Andrey Andreev49e68de2013-02-21 16:30:55 +02001126 elseif (class_exists(config_item('subclass_prefix').$class, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 {
1128 $name = config_item('subclass_prefix').$class;
1129 }
1130 else
1131 {
1132 $name = $class;
1133 }
1134 }
1135 else
1136 {
1137 $name = $prefix.$class;
1138 }
Barry Mienydd671972010-10-04 16:33:58 +02001139
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 // Is the class name valid?
Andrey Andreev49e68de2013-02-21 16:30:55 +02001141 if ( ! class_exists($name, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001143 log_message('error', 'Non-existent class: '.$name);
jonnueee2df62012-07-16 13:06:16 +01001144 show_error('Non-existent class: '.$name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 }
Barry Mienydd671972010-10-04 16:33:58 +02001146
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001148 // Was a custom class name supplied? If so we'll use it
Andrey Andreev519f87a2013-07-23 17:16:10 +03001149 if (empty($object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 {
Andrey Andreev519f87a2013-07-23 17:16:10 +03001151 $object_name = strtolower($class);
1152 if (isset($this->_ci_varmap[$object_name]))
1153 {
1154 $object_name = $this->_ci_varmap[$object_name];
1155 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 }
Andrey Andreev519f87a2013-07-23 17:16:10 +03001157
1158 // Don't overwrite existing properties
1159 $CI =& get_instance();
1160 if (isset($CI->$object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 {
Andrey Andreev519f87a2013-07-23 17:16:10 +03001162 if ($CI->$object_name instanceof $name)
1163 {
1164 log_message('debug', $class." has already been instantiated as '".$object_name."'. Second attempt aborted.");
1165 return;
1166 }
1167
1168 show_error("Resource '".$object_name."' already exists and is not a ".$class." instance.");
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 }
1170
Barry Mienydd671972010-10-04 16:33:58 +02001171 // Save the class name and object name
Andrey Andreev519f87a2013-07-23 17:16:10 +03001172 $this->_ci_classes[$object_name] = $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001173
Barry Mienydd671972010-10-04 16:33:58 +02001174 // Instantiate the class
Andrey Andreev519f87a2013-07-23 17:16:10 +03001175 $CI->$object_name = isset($config)
1176 ? new $name($config)
1177 : new $name();
Barry Mienydd671972010-10-04 16:33:58 +02001178 }
1179
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001181
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001183 * CI Autoloader
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001185 * Loads component listed in the config/autoload.php file.
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 *
Andrey Andreevcdac2482012-11-03 18:09:01 +02001187 * @used-by CI_Loader::initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 * @return void
1189 */
Shane Pearson665baec2011-08-22 18:52:19 -05001190 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001191 {
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001192 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001193 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001194 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001195 }
1196 else
1197 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001198 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001199 }
Barry Mienydd671972010-10-04 16:33:58 +02001200
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 if ( ! isset($autoload))
1202 {
1203 return FALSE;
1204 }
Barry Mienydd671972010-10-04 16:33:58 +02001205
Phil Sturgeon9730c752010-12-15 10:50:15 +00001206 // Autoload packages
1207 if (isset($autoload['packages']))
1208 {
1209 foreach ($autoload['packages'] as $package_path)
1210 {
1211 $this->add_package_path($package_path);
1212 }
1213 }
1214
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 // Load any custom config file
1216 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001217 {
Andrey Andreev623227f2014-03-24 12:46:25 +02001218 $CFG =& load_class('Config', 'core');
1219 foreach ($autoload['config'] as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 {
Andrey Andreev623227f2014-03-24 12:46:25 +02001221 $CFG->load($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 }
Barry Mienydd671972010-10-04 16:33:58 +02001223 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001224
Derek Jonesc6da5032010-03-09 20:44:27 -06001225 // Autoload helpers and languages
1226 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001227 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001228 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 {
1230 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001231 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 }
1233
Andrey Andreev9ab70a82013-07-23 00:09:26 +03001234 // Autoload drivers
1235 if (isset($autoload['drivers']))
1236 {
1237 foreach ($autoload['drivers'] as $item)
1238 {
1239 $this->driver($item);
1240 }
1241 }
1242
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001244 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 {
1246 // Load the database driver.
1247 if (in_array('database', $autoload['libraries']))
1248 {
1249 $this->database();
1250 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1251 }
Barry Mienydd671972010-10-04 16:33:58 +02001252
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 // Load all other libraries
1254 foreach ($autoload['libraries'] as $item)
1255 {
1256 $this->library($item);
1257 }
Barry Mienydd671972010-10-04 16:33:58 +02001258 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001259
1260 // Autoload models
1261 if (isset($autoload['model']))
1262 {
1263 $this->model($autoload['model']);
1264 }
Barry Mienydd671972010-10-04 16:33:58 +02001265 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001266
1267 // --------------------------------------------------------------------
1268
1269 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001270 * CI Object to Array translator
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001272 * Takes an object as input and converts the class variables to
1273 * an associative array with key/value pairs.
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001275 * @param object $object Object data to translate
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 * @return array
1277 */
Greg Akerf5c84022011-04-19 17:13:03 -05001278 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001280 return is_object($object) ? get_object_vars($object) : $object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 }
1282
1283 // --------------------------------------------------------------------
1284
1285 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001286 * CI Component getter
Derek Jones32bf1862010-03-02 13:46:07 -06001287 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001288 * Get a reference to a specific library or model.
1289 *
1290 * @param string $component Component name
Derek Jones32bf1862010-03-02 13:46:07 -06001291 * @return bool
1292 */
Greg Akerf5c84022011-04-19 17:13:03 -05001293 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001294 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001295 $CI =& get_instance();
1296 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001297 }
1298
1299 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001300
Derek Jones32bf1862010-03-02 13:46:07 -06001301 /**
1302 * Prep filename
1303 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001304 * This function prepares filenames of various items to
1305 * make their loading more reliable.
Derek Jones32bf1862010-03-02 13:46:07 -06001306 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001307 * @param string|string[] $filename Filename(s)
1308 * @param string $extension Filename extension
Derek Jones32bf1862010-03-02 13:46:07 -06001309 * @return array
1310 */
Greg Akerf5c84022011-04-19 17:13:03 -05001311 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001312 {
1313 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001314 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001315 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001316 }
1317 else
1318 {
1319 foreach ($filename as $key => $val)
1320 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001321 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001322 }
Barry Mienydd671972010-10-04 16:33:58 +02001323
Derek Jones32bf1862010-03-02 13:46:07 -06001324 return $filename;
1325 }
1326 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001327
Derek Allard2067d1a2008-11-13 22:59:24 +00001328}
1329
1330/* End of file Loader.php */
Tomaz Lovrec8bb20a12013-10-16 12:34:36 +02001331/* Location: ./system/core/Loader.php */