blob: daf326f25a836270b750e29d6d7517a4a88a73d5 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevd7297352012-01-07 22:53:14 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevd7297352012-01-07 22:53:14 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
Derek Allard2067d1a2008-11-13 22:59:24 +000018 *
19 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Loader Class
31 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030032 * Loads framework components.
Derek Allard2067d1a2008-11-13 22:59:24 +000033 *
34 * @package CodeIgniter
35 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @category Loader
Andrey Andreev92ebfb62012-05-17 12:49:24 +030037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/loader.html
39 */
40class CI_Loader {
41
42 // All these are set automatically. Don't mess with them.
David Behlercda768a2011-08-14 23:52:48 +020043 /**
44 * Nesting level of the output buffering mechanism
45 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030046 * @var int
David Behlercda768a2011-08-14 23:52:48 +020047 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050048 protected $_ci_ob_level;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030049
David Behlercda768a2011-08-14 23:52:48 +020050 /**
51 * List of paths to load views from
52 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030053 * @var array
David Behlercda768a2011-08-14 23:52:48 +020054 */
Ted Wood4c223642013-01-05 16:50:31 -080055 protected $_ci_view_paths = array(VIEWPATH => TRUE);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030056
David Behlercda768a2011-08-14 23:52:48 +020057 /**
58 * List of paths to load libraries from
59 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030060 * @var array
David Behlercda768a2011-08-14 23:52:48 +020061 */
Ted Wood4c223642013-01-05 16:50:31 -080062 protected $_ci_library_paths = array(APPPATH, BASEPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030063
David Behlercda768a2011-08-14 23:52:48 +020064 /**
65 * List of paths to load models from
66 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030067 * @var array
David Behlercda768a2011-08-14 23:52:48 +020068 */
Ted Wood4c223642013-01-05 16:50:31 -080069 protected $_ci_model_paths = array(APPPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030070
David Behlercda768a2011-08-14 23:52:48 +020071 /**
72 * List of paths to load helpers from
73 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030074 * @var array
David Behlercda768a2011-08-14 23:52:48 +020075 */
Ted Wood4c223642013-01-05 16:50:31 -080076 protected $_ci_helper_paths = array(APPPATH, BASEPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030077
David Behlercda768a2011-08-14 23:52:48 +020078 /**
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 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100187 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500188 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600189 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500190 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000191
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200192 return $this;
Greg Akerce433962010-10-12 09:29:35 -0500193 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000194
vlakoff1228fe22013-01-14 01:30:09 +0100195 if ($params !== NULL && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
197 $params = NULL;
198 }
199
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600200 $this->_ci_load_class($library, $params, $object_name);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200201 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203
204 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 /**
207 * Model Loader
208 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300209 * Loads and instantiates libraries.
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300211 * @param string $model Model name
212 * @param string $name An optional object name to assign to
213 * @param bool $db_conn An optional database connection configuration to initialize
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200214 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200215 */
Greg Akerf5c84022011-04-19 17:13:03 -0500216 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200217 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300218 if (empty($model))
219 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200220 return $this;
Andrey Andreeved4b2582012-10-27 17:46:52 +0300221 }
222 elseif (is_array($model))
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Andrey Andreev5a519db2013-01-12 04:19:19 +0200224 foreach ($model as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
Ahmad Anbar4005e3c2013-09-12 23:33:28 +0300226 is_int($key) ? $this->model($value, '', $db_conn) : $this->model($key, $value, $db_conn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200228
229 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
231
Derek Jones32bf1862010-03-02 13:46:07 -0600232 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600235 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Derek Jones32bf1862010-03-02 13:46:07 -0600237 // The path is in front of the last slash
Andrey Andreevd47baab2012-01-09 16:56:46 +0200238 $path = substr($model, 0, ++$last_slash);
Derek Jones32bf1862010-03-02 13:46:07 -0600239
240 // And the model name behind it
Andrey Andreevd47baab2012-01-09 16:56:46 +0200241 $model = substr($model, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Phil Sturgeon10d78f62012-06-04 14:41:53 -0500244 if (empty($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
246 $name = $model;
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 if (in_array($name, $this->_ci_models, TRUE))
250 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200251 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 $CI =& get_instance();
255 if (isset($CI->$name))
256 {
257 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Andrey Andreev20292312013-07-22 14:29:10 +0300260 if ($db_conn !== FALSE && ! class_exists('CI_DB', FALSE))
261 {
262 if ($db_conn === TRUE)
263 {
264 $db_conn = '';
265 }
266
267 $CI->load->database($db_conn, FALSE, TRUE);
268 }
269
270 if ( ! class_exists('CI_Model', FALSE))
271 {
272 load_class('Model', 'core');
273 }
274
275 $model = ucfirst(strtolower($model));
Derek Allard2067d1a2008-11-13 22:59:24 +0000276
Derek Jones32bf1862010-03-02 13:46:07 -0600277 foreach ($this->_ci_model_paths as $mod_path)
278 {
Greg Aker3a746652011-04-19 10:59:47 -0500279 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600280 {
281 continue;
282 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000283
Greg Aker3a746652011-04-19 10:59:47 -0500284 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600285
Derek Jones32bf1862010-03-02 13:46:07 -0600286 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600287 $this->_ci_models[] = $name;
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200288 return $this;
Derek Jones32bf1862010-03-02 13:46:07 -0600289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Jones32bf1862010-03-02 13:46:07 -0600291 // couldn't find the model
292 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 /**
298 * Database Loader
299 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300300 * @param mixed $params Database configuration options
301 * @param bool $return Whether to return the database object
302 * @param bool $query_builder Whether to enable Query Builder
303 * (overrides the configuration setting)
304 *
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200305 * @return object|bool Database object if $return is set to TRUE,
306 * FALSE on failure, CI_Loader instance in any other case
Barry Mienydd671972010-10-04 16:33:58 +0200307 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000308 public function database($params = '', $return = FALSE, $query_builder = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
310 // Grab the super object
311 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // Do we even need to load the database class?
Andrey Andreev9d0ab042012-10-24 21:47:39 +0300314 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 +0000315 {
316 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200317 }
318
Greg Aker3a746652011-04-19 10:59:47 -0500319 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000320
321 if ($return === TRUE)
322 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000323 return DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Andrey Andreevd7297352012-01-07 22:53:14 +0200326 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // reference errors with some configurations
328 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // Load the DB class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000331 $CI->db =& DB($params, $query_builder);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200332 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // --------------------------------------------------------------------
336
337 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300338 * Load the Database Utilities Class
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200340 * @param object $db Database object
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200341 * @param bool $return Whether to return the DB Utilities class object or not
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200342 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200343 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200344 public function dbutil($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 $CI =& get_instance();
347
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200348 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
349 {
350 class_exists('CI_DB', FALSE) OR $this->database();
351 $db =& $CI->db;
352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Greg Aker3a746652011-04-19 10:59:47 -0500354 require_once(BASEPATH.'database/DB_utility.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200355 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_utility.php');
356 $class = 'CI_DB_'.$db->dbdriver.'_utility';
Derek Allard2067d1a2008-11-13 22:59:24 +0000357
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200358 if ($return === TRUE)
359 {
360 return new $class($db);
361 }
362
363 $CI->dbutil = new $class($db);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200364 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // --------------------------------------------------------------------
368
369 /**
370 * Load the Database Forge Class
371 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200372 * @param object $db Database object
373 * @param bool $return Whether to return the DB Forge class object or not
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200374 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200375 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200376 public function dbforge($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200378 $CI =& get_instance();
379 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200381 class_exists('CI_DB', FALSE) OR $this->database();
382 $db =& $CI->db;
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Greg Aker3a746652011-04-19 10:59:47 -0500385 require_once(BASEPATH.'database/DB_forge.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200386 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_forge.php');
Andrey Andreeva287a342012-11-05 23:19:59 +0200387
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200388 if ( ! empty($db->subdriver))
Andrey Andreeva287a342012-11-05 23:19:59 +0200389 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200390 $driver_path = BASEPATH.'database/drivers/'.$db->dbdriver.'/subdrivers/'.$db->dbdriver.'_'.$db->subdriver.'_forge.php';
Andrey Andreeva287a342012-11-05 23:19:59 +0200391 if (file_exists($driver_path))
392 {
393 require_once($driver_path);
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200394 $class = 'CI_DB_'.$db->dbdriver.'_'.$db->subdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200395 }
396 }
397 else
398 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200399 $class = 'CI_DB_'.$db->dbdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200400 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000401
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200402 if ($return === TRUE)
403 {
404 return new $class($db);
405 }
406
407 $CI->dbforge = new $class($db);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200408 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300414 * View Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300416 * Loads "view" files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300418 * @param string $view View name
419 * @param array $vars An associative array of data
420 * to be extracted for use in the view
421 * @param bool $return Whether to return the view output
422 * or leave it to the Output class
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200423 * @return object|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 */
Greg Akerf5c84022011-04-19 17:13:03 -0500425 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
427 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
428 }
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300433 * Generic File Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300435 * @param string $path File path
436 * @param bool $return Whether to return the file output
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200437 * @return object|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 */
Greg Akerf5c84022011-04-19 17:13:03 -0500439 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
441 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
442 }
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 /**
447 * Set Variables
448 *
449 * Once variables are set they become available within
450 * the controller class and its "view" files.
451 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300452 * @param array|object|string $vars
453 * An associative array or object containing values
454 * to be set, or a value's name if string
455 * @param string $val Value to set, only used if $vars is a string
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200456 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200458 public function vars($vars, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
vlakoffa5e0ea82013-02-27 18:17:35 +0100460 if (is_string($vars))
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 $vars = array($vars => $val);
463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200466
Andrey Andreev94af3552012-03-26 23:10:42 +0300467 if (is_array($vars) && count($vars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
469 foreach ($vars as $key => $val)
470 {
471 $this->_ci_cached_vars[$key] = $val;
472 }
473 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200474
475 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 /**
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200481 * Clear Cached Variables
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200482 *
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200483 * Clears the cached variables.
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200484 *
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200485 * @return object
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200486 */
487 public function clear_vars()
488 {
489 $this->_ci_cached_vars = array();
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200490 return $this;
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200491 }
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200492
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200493 // --------------------------------------------------------------------
494
495 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600496 * Get Variable
497 *
498 * Check if a variable is set and retrieve it.
499 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300500 * @param string $key Variable name
501 * @return mixed The variable or NULL if not found
Phil Sturgeon8731f642011-07-22 16:11:34 -0600502 */
503 public function get_var($key)
504 {
505 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
506 }
507
508 // --------------------------------------------------------------------
509
510 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600511 * Get Variables
512 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300513 * Retrieves all loaded variables.
Shane Pearson81dd2232011-11-18 20:49:35 -0600514 *
515 * @return array
516 */
517 public function get_vars()
518 {
519 return $this->_ci_cached_vars;
520 }
521
522 // --------------------------------------------------------------------
523
524 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300525 * Helper Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300527 * @param string|string[] $helpers Helper name(s)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200528 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 */
Greg Akerf5c84022011-04-19 17:13:03 -0500530 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200531 {
Derek Jones32bf1862010-03-02 13:46:07 -0600532 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200533 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 if (isset($this->_ci_helpers[$helper]))
535 {
536 continue;
537 }
Derek Jones32bf1862010-03-02 13:46:07 -0600538
Barry Mienydd671972010-10-04 16:33:58 +0200539 // Is this a helper extension request?
Andrey Andreev12d7b462012-11-12 13:42:09 +0200540 $ext_helper = config_item('subclass_prefix').$helper;
541 $ext_loaded = FALSE;
542 foreach ($this->_ci_helper_paths as $path)
543 {
544 if (file_exists($path.'helpers/'.$ext_helper.'.php'))
545 {
546 include_once($path.'helpers/'.$ext_helper.'.php');
547 $ext_loaded = TRUE;
548 }
549 }
550
551 // If we have loaded extensions - check if the base one is here
552 if ($ext_loaded === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 {
Greg Aker3a746652011-04-19 10:59:47 -0500554 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 if ( ! file_exists($base_helper))
556 {
Greg Aker3a746652011-04-19 10:59:47 -0500557 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 }
Barry Mienydd671972010-10-04 16:33:58 +0200559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 include_once($base_helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600561 $this->_ci_helpers[$helper] = TRUE;
562 log_message('debug', 'Helper loaded: '.$helper);
563 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 }
Barry Mienydd671972010-10-04 16:33:58 +0200565
Andrey Andreev12d7b462012-11-12 13:42:09 +0200566 // No extensions found ... try loading regular helpers and/or overrides
Derek Jones32bf1862010-03-02 13:46:07 -0600567 foreach ($this->_ci_helper_paths as $path)
568 {
Greg Aker3a746652011-04-19 10:59:47 -0500569 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200570 {
Greg Aker3a746652011-04-19 10:59:47 -0500571 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600572
573 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200574 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600575 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 }
577 }
578
Derek Jones32bf1862010-03-02 13:46:07 -0600579 // unable to load the helper
580 if ( ! isset($this->_ci_helpers[$helper]))
581 {
Greg Aker3a746652011-04-19 10:59:47 -0500582 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200585
586 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 }
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 /**
592 * Load Helpers
593 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300594 * An alias for the helper() method in case the developer has
595 * written the plural form of it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300597 * @uses CI_Loader::helper()
598 * @param string|string[] $helpers Helper name(s)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200599 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 */
Greg Akerf5c84022011-04-19 17:13:03 -0500601 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200603 return $this->helper($helpers);
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 }
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200607
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300609 * Language Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300611 * Loads language files.
612 *
613 * @param string|string[] $files List of language file names to load
614 * @param string Language name
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200615 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200617 public function language($files, $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
619 $CI =& get_instance();
Andrey Andreeved4b2582012-10-27 17:46:52 +0300620 is_array($files) OR $files = array($files);
Derek Allard2067d1a2008-11-13 22:59:24 +0000621
Andrey Andreeved4b2582012-10-27 17:46:52 +0300622 foreach ($files as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200623 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 $CI->lang->load($langfile, $lang);
625 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200626
627 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 }
Barry Mienydd671972010-10-04 16:33:58 +0200629
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300633 * Config Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300635 * Loads a config file (an alias for CI_Config::load()).
636 *
637 * @uses CI_Config::load()
638 * @param string $file Configuration file name
639 * @param bool $use_sections Whether configuration values should be loaded into their own section
640 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
641 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200643 public function config($file, $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200644 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 $CI =& get_instance();
Andrey Andreeved4b2582012-10-27 17:46:52 +0300646 return $CI->config->load($file, $use_sections, $fail_gracefully);
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 }
648
649 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300652 * Driver Loader
Derek Jones8dca0412010-03-05 13:01:44 -0600653 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300654 * Loads a driver library.
Derek Jones8dca0412010-03-05 13:01:44 -0600655 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300656 * @param string|string[] $library Driver name(s)
657 * @param array $params Optional parameters to pass to the driver
658 * @param string $object_name An optional object name to assign to
659 *
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200660 * @return object|bool Object or FALSE on failure if $library is a string
661 * and $object_name is set. CI_Loader instance otherwise.
Derek Jones8dca0412010-03-05 13:01:44 -0600662 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200663 public function driver($library, $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600664 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800665 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800666 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800667 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800668 {
669 $this->driver($driver);
670 }
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800671
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200672 return $this;
673 }
674 elseif (empty($library))
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200675 {
676 return FALSE;
677 }
678
vlakofffadb8222013-05-12 10:57:09 +0200679 if ( ! class_exists('CI_Driver_Library', FALSE))
dchill426262d052012-11-24 18:41:13 -0500680 {
681 // We aren't instantiating an object here, just making the base class available
682 require BASEPATH.'libraries/Driver.php';
683 }
684
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600685 // We can save the loader some time since Drivers will *always* be in a subfolder,
686 // and typically identically named to the library
687 if ( ! strpos($library, '/'))
688 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500689 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600690 }
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Jones8dca0412010-03-05 13:01:44 -0600692 return $this->library($library, $params, $object_name);
693 }
694
695 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200696
Derek Jones8dca0412010-03-05 13:01:44 -0600697 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600698 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300700 * Prepends a parent path to the library, model, helper and config
701 * path arrays.
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300703 * @see CI_Loader::$_ci_library_paths
704 * @see CI_Loader::$_ci_model_paths
705 * @see CI_Loader::$_ci_helper_paths
706 * @see CI_Config::$_config_paths
707 *
708 * @param string $path Path to add
709 * @param bool $view_cascade (default: TRUE)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200710 * @return object
Derek Jones32bf1862010-03-02 13:46:07 -0600711 */
Andrey Andreevcce91802012-06-12 13:25:31 +0300712 public function add_package_path($path, $view_cascade = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600713 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500714 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200715
Derek Jones32bf1862010-03-02 13:46:07 -0600716 array_unshift($this->_ci_library_paths, $path);
717 array_unshift($this->_ci_model_paths, $path);
718 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200719
Greg Akerf5c84022011-04-19 17:13:03 -0500720 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
721
Derek Jones32bf1862010-03-02 13:46:07 -0600722 // Add config file path
723 $config =& $this->_ci_get_component('config');
and-ersb3ec9422013-01-03 16:05:12 +0100724 $config->_config_paths[] = $path;
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200725
726 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 }
728
729 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600730
731 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000732 * Get Package Paths
733 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300734 * Return a list of all package paths.
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000735 *
vlakoffcdc61132013-05-10 16:47:47 +0200736 * @param bool $include_base Whether to include BASEPATH (default: FALSE)
Andrey Andreeved4b2582012-10-27 17:46:52 +0300737 * @return array
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000738 */
Greg Akerf5c84022011-04-19 17:13:03 -0500739 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000740 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300741 return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000742 }
743
744 // --------------------------------------------------------------------
745
746 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600747 * Remove Package Path
748 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300749 * Remove a path from the library, model, helper and/or config
750 * path arrays if it exists. If no path is provided, the most recently
751 * added path will be removed removed.
Derek Jones32bf1862010-03-02 13:46:07 -0600752 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300753 * @param string $path Path to remove
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200754 * @return object
Derek Jones32bf1862010-03-02 13:46:07 -0600755 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300756 public function remove_package_path($path = '')
Derek Jones32bf1862010-03-02 13:46:07 -0600757 {
758 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200759
Alex Bilbieed944a32012-06-02 11:07:47 +0100760 if ($path === '')
Derek Jones32bf1862010-03-02 13:46:07 -0600761 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200762 array_shift($this->_ci_library_paths);
763 array_shift($this->_ci_model_paths);
764 array_shift($this->_ci_helper_paths);
765 array_shift($this->_ci_view_paths);
Korri3684d342012-04-17 00:35:08 -0400766 array_pop($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600767 }
768 else
769 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500770 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600771 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
772 {
773 if (($key = array_search($path, $this->{$var})) !== FALSE)
774 {
775 unset($this->{$var}[$key]);
776 }
777 }
David Behlercda768a2011-08-14 23:52:48 +0200778
Greg Akerf5c84022011-04-19 17:13:03 -0500779 if (isset($this->_ci_view_paths[$path.'views/']))
780 {
781 unset($this->_ci_view_paths[$path.'views/']);
782 }
Barry Mienydd671972010-10-04 16:33:58 +0200783
Derek Jones32bf1862010-03-02 13:46:07 -0600784 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
785 {
786 unset($config->_config_paths[$key]);
787 }
788 }
Barry Mienydd671972010-10-04 16:33:58 +0200789
Derek Jones32bf1862010-03-02 13:46:07 -0600790 // make sure the application default paths are still in the array
791 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
792 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
793 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500794 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600795 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200796
797 return $this;
Derek Jones32bf1862010-03-02 13:46:07 -0600798 }
799
800 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200801
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300803 * Internal CI Data Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300805 * Used to load views and files.
806 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 * Variables are prefixed with _ci_ to avoid symbol collision with
Andrey Andreeved4b2582012-10-27 17:46:52 +0300808 * variables made available to view files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300810 * @used-by CI_Loader::view()
811 * @used-by CI_Loader::file()
812 * @param array $_ci_data Data to load
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200813 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 */
Greg Akerf5c84022011-04-19 17:13:03 -0500815 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
817 // Set the default data variables
818 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
819 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300820 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 }
David Behlercda768a2011-08-14 23:52:48 +0200822
Greg Akerf5c84022011-04-19 17:13:03 -0500823 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000824
825 // Set the path to the requested file
Alex Bilbie40bd2a72012-06-02 16:04:15 +0100826 if (is_string($_ci_path) && $_ci_path !== '')
Greg Aker8807be32011-04-21 13:06:15 -0500827 {
828 $_ci_x = explode('/', $_ci_path);
829 $_ci_file = end($_ci_x);
830 }
831 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 {
833 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Alex Bilbieed944a32012-06-02 11:07:47 +0100834 $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500835
Joe McFrederick64f470b2012-08-18 12:29:56 -0400836 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
Greg Akerf5c84022011-04-19 17:13:03 -0500837 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400838 if (file_exists($_ci_view_file.$_ci_file))
Greg Akerf5c84022011-04-19 17:13:03 -0500839 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400840 $_ci_path = $_ci_view_file.$_ci_file;
Greg Akerf5c84022011-04-19 17:13:03 -0500841 $file_exists = TRUE;
842 break;
843 }
David Behlercda768a2011-08-14 23:52:48 +0200844
Greg Akerf5c84022011-04-19 17:13:03 -0500845 if ( ! $cascade)
846 {
847 break;
David Behlercda768a2011-08-14 23:52:48 +0200848 }
Greg Akerf5c84022011-04-19 17:13:03 -0500849 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 }
Barry Mienydd671972010-10-04 16:33:58 +0200851
Greg Akerf5c84022011-04-19 17:13:03 -0500852 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
854 show_error('Unable to load the requested file: '.$_ci_file);
855 }
Barry Mienydd671972010-10-04 16:33:58 +0200856
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 // This allows anything loaded using $this->load (views, files, etc.)
858 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500859 $_ci_CI =& get_instance();
860 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500862 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500864 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 }
866 }
867
868 /*
869 * Extract and cache variables
870 *
vlakoff02506182012-07-03 07:28:50 +0200871 * You can either set variables using the dedicated $this->load->vars()
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 * function or via the second parameter of this function. We'll merge
873 * the two types and cache them so that views that are embedded within
874 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200875 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 if (is_array($_ci_vars))
877 {
878 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
879 }
880 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200881
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 /*
883 * Buffer the output
884 *
885 * We buffer the output for two reasons:
886 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200887 * 2. So that the final rendered template can be post-processed by
dchill425628ba02012-08-08 12:05:45 -0400888 * the output class. Why do we need post processing? For one thing,
889 * in order to show the elapsed page load time. Unless we can
890 * intercept the content right before it's sent to the browser and
891 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 */
893 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200894
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 // If the PHP installation does not support short tags we'll
896 // do a little string replacement, changing the short tags
897 // to standard PHP echo statements.
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200898 if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE
899 && config_item('rewrite_short_tags') === TRUE && function_usable('eval')
900 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200902 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 }
904 else
905 {
906 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
907 }
Barry Mienydd671972010-10-04 16:33:58 +0200908
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 // Return the file data if requested
912 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200913 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 $buffer = ob_get_contents();
915 @ob_end_clean();
916 return $buffer;
917 }
918
919 /*
920 * Flush the buffer... or buff the flusher?
921 *
922 * In order to permit views to be nested within
923 * other views, we need to flush the content back out whenever
924 * we are beyond the first level of output buffering so that
925 * it can be seen and included properly by the first included
926 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200927 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 if (ob_get_level() > $this->_ci_ob_level + 1)
929 {
930 ob_end_flush();
931 }
932 else
933 {
Greg Aker22f1a632010-11-10 15:34:35 -0600934 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 @ob_end_clean();
936 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200937
938 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 }
940
941 // --------------------------------------------------------------------
942
943 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300944 * Internal CI Class Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300946 * @used-by CI_Loader::library()
947 * @uses CI_Loader::_ci_init_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300949 * @param string $class Class name to load
950 * @param mixed $params Optional parameters to pass to the class constructor
951 * @param string $object_name Optional object name to assign to
Barry Mienydd671972010-10-04 16:33:58 +0200952 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 */
Greg Akerf5c84022011-04-19 17:13:03 -0500954 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200955 {
956 // Get the class name, and while we're at it trim any slashes.
957 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500959 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 // Was the path included with the class name?
962 // We look for a slash to determine this
Derek Jones32bf1862010-03-02 13:46:07 -0600963 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 {
Derek Jones32bf1862010-03-02 13:46:07 -0600965 // Extract the path
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200966 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200967
Derek Jones32bf1862010-03-02 13:46:07 -0600968 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200969 $class = substr($class, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 }
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200971 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200973 $subdir = '';
974 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000975
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200976 $class = ucfirst($class);
977 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
978
979 // Is this a class extension request?
980 if (file_exists($subclass))
981 {
Daniel1d3752c2013-04-14 16:41:57 -0400982 $baseclass = BASEPATH.'libraries/'.$subdir.$class.'.php';
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200983
984 if ( ! file_exists($baseclass))
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200986 log_message('error', 'Unable to load the requested class: '.$class);
987 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 }
Barry Mienydd671972010-10-04 16:33:58 +0200989
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200990 // Safety: Was the class already loaded by a previous call?
991 if (class_exists(config_item('subclass_prefix').$class, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200993 // Before we deem this to be a duplicate request, let's see
994 // if a custom object name is being supplied. If so, we'll
995 // return a new instance of the object
996 if ($object_name !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200998 $CI =& get_instance();
999 if ( ! isset($CI->$object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001001 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
Barry Mienydd671972010-10-04 16:33:58 +02001004
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001005 log_message('debug', $class.' class already loaded. Second attempt ignored.');
1006 return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 }
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001008
1009 include_once($baseclass);
1010 include_once($subclass);
1011
1012 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
1013 }
1014
vlakoff35672462013-02-15 01:36:04 +01001015 // Let's search for the requested library file and load it.
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001016 foreach ($this->_ci_library_paths as $path)
1017 {
1018 $filepath = $path.'libraries/'.$subdir.$class.'.php';
1019
1020 // Safety: Was the class already loaded by a previous call?
1021 if (class_exists($class, FALSE))
1022 {
1023 // Before we deem this to be a duplicate request, let's see
1024 // if a custom object name is being supplied. If so, we'll
1025 // return a new instance of the object
1026 if ($object_name !== NULL)
1027 {
1028 $CI =& get_instance();
1029 if ( ! isset($CI->$object_name))
1030 {
1031 return $this->_ci_init_class($class, '', $params, $object_name);
1032 }
1033 }
1034
1035 log_message('debug', $class.' class already loaded. Second attempt ignored.');
1036 return;
1037 }
1038 // Does the file exist? No? Bummer...
1039 elseif ( ! file_exists($filepath))
1040 {
1041 continue;
1042 }
1043
1044 include_once($filepath);
1045 return $this->_ci_init_class($class, '', $params, $object_name);
1046 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001047
Andrey Andreevd7297352012-01-07 22:53:14 +02001048 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Alex Bilbieed944a32012-06-02 11:07:47 +01001049 if ($subdir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001051 return $this->_ci_load_class($class.'/'.$class, $params, $object_name);
dchill42aee92652012-08-26 21:45:35 -04001052 }
Barry Mienydd671972010-10-04 16:33:58 +02001053
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 // If we got this far we were unable to find the requested class.
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001055 log_message('error', 'Unable to load the requested class: '.$class);
1056 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 }
Barry Mienydd671972010-10-04 16:33:58 +02001058
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 // --------------------------------------------------------------------
1060
1061 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001062 * Internal CI Class Instantiator
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001064 * @used-by CI_Loader::_ci_load_class()
1065 *
1066 * @param string $class Class name
1067 * @param string $prefix Class name prefix
1068 * @param array|null|bool $config Optional configuration to pass to the class constructor:
1069 * FALSE to skip;
1070 * NULL to search in config paths;
1071 * array containing configuration data
1072 * @param string $object_name Optional object name to assign to
Andrey Andreev94af3552012-03-26 23:10:42 +03001073 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001075 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001076 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001077 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 if ($config === NULL)
1079 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001080 // Fetch the config paths containing any package paths
1081 $config_component = $this->_ci_get_component('config');
1082
1083 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001085 // Break on the first found file, thus package files
1086 // are not overridden by default paths
1087 foreach ($config_component->_config_paths as $path)
1088 {
1089 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001090 // are case-sensitive with regard to file names. Check for environment
1091 // first, global next
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001092 if (file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001093 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001094 include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001095 break;
1096 }
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001097 elseif (file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001098 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001099 include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001100 break;
1101 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001102 elseif (file_exists($path.'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001103 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001104 include($path.'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001105 break;
1106 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001107 elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001108 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001109 include($path.'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001110 break;
1111 }
1112 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 }
1114 }
Barry Mienydd671972010-10-04 16:33:58 +02001115
Alex Bilbieed944a32012-06-02 11:07:47 +01001116 if ($prefix === '')
Barry Mienydd671972010-10-04 16:33:58 +02001117 {
Andrey Andreev49e68de2013-02-21 16:30:55 +02001118 if (class_exists('CI_'.$class, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 {
1120 $name = 'CI_'.$class;
1121 }
Andrey Andreev49e68de2013-02-21 16:30:55 +02001122 elseif (class_exists(config_item('subclass_prefix').$class, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
1124 $name = config_item('subclass_prefix').$class;
1125 }
1126 else
1127 {
1128 $name = $class;
1129 }
1130 }
1131 else
1132 {
1133 $name = $prefix.$class;
1134 }
Barry Mienydd671972010-10-04 16:33:58 +02001135
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 // Is the class name valid?
Andrey Andreev49e68de2013-02-21 16:30:55 +02001137 if ( ! class_exists($name, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001139 log_message('error', 'Non-existent class: '.$name);
jonnueee2df62012-07-16 13:06:16 +01001140 show_error('Non-existent class: '.$name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 }
Barry Mienydd671972010-10-04 16:33:58 +02001142
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001144 // Was a custom class name supplied? If so we'll use it
Andrey Andreev519f87a2013-07-23 17:16:10 +03001145 if (empty($object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 {
Andrey Andreev519f87a2013-07-23 17:16:10 +03001147 $object_name = strtolower($class);
1148 if (isset($this->_ci_varmap[$object_name]))
1149 {
1150 $object_name = $this->_ci_varmap[$object_name];
1151 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 }
Andrey Andreev519f87a2013-07-23 17:16:10 +03001153
1154 // Don't overwrite existing properties
1155 $CI =& get_instance();
1156 if (isset($CI->$object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 {
Andrey Andreev519f87a2013-07-23 17:16:10 +03001158 if ($CI->$object_name instanceof $name)
1159 {
1160 log_message('debug', $class." has already been instantiated as '".$object_name."'. Second attempt aborted.");
1161 return;
1162 }
1163
1164 show_error("Resource '".$object_name."' already exists and is not a ".$class." instance.");
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 }
1166
Barry Mienydd671972010-10-04 16:33:58 +02001167 // Save the class name and object name
Andrey Andreev519f87a2013-07-23 17:16:10 +03001168 $this->_ci_classes[$object_name] = $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001169
Barry Mienydd671972010-10-04 16:33:58 +02001170 // Instantiate the class
Andrey Andreev519f87a2013-07-23 17:16:10 +03001171 $CI->$object_name = isset($config)
1172 ? new $name($config)
1173 : new $name();
Barry Mienydd671972010-10-04 16:33:58 +02001174 }
1175
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001177
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001179 * CI Autoloader
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001181 * Loads component listed in the config/autoload.php file.
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 *
Andrey Andreevcdac2482012-11-03 18:09:01 +02001183 * @used-by CI_Loader::initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 * @return void
1185 */
Shane Pearson665baec2011-08-22 18:52:19 -05001186 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001187 {
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001188 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001189 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001190 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001191 }
1192 else
1193 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001194 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001195 }
Barry Mienydd671972010-10-04 16:33:58 +02001196
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 if ( ! isset($autoload))
1198 {
1199 return FALSE;
1200 }
Barry Mienydd671972010-10-04 16:33:58 +02001201
Phil Sturgeon9730c752010-12-15 10:50:15 +00001202 // Autoload packages
1203 if (isset($autoload['packages']))
1204 {
1205 foreach ($autoload['packages'] as $package_path)
1206 {
1207 $this->add_package_path($package_path);
1208 }
1209 }
1210
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 // Load any custom config file
1212 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001213 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 $CI =& get_instance();
1215 foreach ($autoload['config'] as $key => $val)
1216 {
1217 $CI->config->load($val);
1218 }
Barry Mienydd671972010-10-04 16:33:58 +02001219 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001220
Derek Jonesc6da5032010-03-09 20:44:27 -06001221 // Autoload helpers and languages
1222 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001223 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001224 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 {
1226 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001227 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 }
1229
Andrey Andreev9ab70a82013-07-23 00:09:26 +03001230 // Autoload drivers
1231 if (isset($autoload['drivers']))
1232 {
1233 foreach ($autoload['drivers'] as $item)
1234 {
1235 $this->driver($item);
1236 }
1237 }
1238
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001240 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 {
1242 // Load the database driver.
1243 if (in_array('database', $autoload['libraries']))
1244 {
1245 $this->database();
1246 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1247 }
Barry Mienydd671972010-10-04 16:33:58 +02001248
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 // Load all other libraries
1250 foreach ($autoload['libraries'] as $item)
1251 {
1252 $this->library($item);
1253 }
Barry Mienydd671972010-10-04 16:33:58 +02001254 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001255
1256 // Autoload models
1257 if (isset($autoload['model']))
1258 {
1259 $this->model($autoload['model']);
1260 }
Barry Mienydd671972010-10-04 16:33:58 +02001261 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001262
1263 // --------------------------------------------------------------------
1264
1265 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001266 * CI Object to Array translator
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001268 * Takes an object as input and converts the class variables to
1269 * an associative array with key/value pairs.
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001271 * @param object $object Object data to translate
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 * @return array
1273 */
Greg Akerf5c84022011-04-19 17:13:03 -05001274 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001276 return is_object($object) ? get_object_vars($object) : $object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 }
1278
1279 // --------------------------------------------------------------------
1280
1281 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001282 * CI Component getter
Derek Jones32bf1862010-03-02 13:46:07 -06001283 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001284 * Get a reference to a specific library or model.
1285 *
1286 * @param string $component Component name
Derek Jones32bf1862010-03-02 13:46:07 -06001287 * @return bool
1288 */
Greg Akerf5c84022011-04-19 17:13:03 -05001289 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001290 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001291 $CI =& get_instance();
1292 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001293 }
1294
1295 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001296
Derek Jones32bf1862010-03-02 13:46:07 -06001297 /**
1298 * Prep filename
1299 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001300 * This function prepares filenames of various items to
1301 * make their loading more reliable.
Derek Jones32bf1862010-03-02 13:46:07 -06001302 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001303 * @param string|string[] $filename Filename(s)
1304 * @param string $extension Filename extension
Derek Jones32bf1862010-03-02 13:46:07 -06001305 * @return array
1306 */
Greg Akerf5c84022011-04-19 17:13:03 -05001307 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001308 {
1309 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001310 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001311 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001312 }
1313 else
1314 {
1315 foreach ($filename as $key => $val)
1316 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001317 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001318 }
Barry Mienydd671972010-10-04 16:33:58 +02001319
Derek Jones32bf1862010-03-02 13:46:07 -06001320 return $filename;
1321 }
1322 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001323
Derek Allard2067d1a2008-11-13 22:59:24 +00001324}
1325
1326/* End of file Loader.php */
Tomaz Lovrec8bb20a12013-10-16 12:34:36 +02001327/* Location: ./system/core/Loader.php */