blob: bbd7a84b60503fb9ec2e20bd9edc5c993b1fdd62 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevd7297352012-01-07 22:53:14 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevd7297352012-01-07 22:53:14 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
Derek Allard2067d1a2008-11-13 22:59:24 +000018 *
19 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Loader Class
31 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030032 * Loads framework components.
Derek Allard2067d1a2008-11-13 22:59:24 +000033 *
34 * @package CodeIgniter
35 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @category Loader
Andrey Andreev92ebfb62012-05-17 12:49:24 +030037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/loader.html
39 */
40class CI_Loader {
41
42 // All these are set automatically. Don't mess with them.
David Behlercda768a2011-08-14 23:52:48 +020043 /**
44 * Nesting level of the output buffering mechanism
45 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030046 * @var int
David Behlercda768a2011-08-14 23:52:48 +020047 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050048 protected $_ci_ob_level;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030049
David Behlercda768a2011-08-14 23:52:48 +020050 /**
51 * List of paths to load views from
52 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030053 * @var array
David Behlercda768a2011-08-14 23:52:48 +020054 */
Ted Wood4c223642013-01-05 16:50:31 -080055 protected $_ci_view_paths = array(VIEWPATH => TRUE);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030056
David Behlercda768a2011-08-14 23:52:48 +020057 /**
58 * List of paths to load libraries from
59 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030060 * @var array
David Behlercda768a2011-08-14 23:52:48 +020061 */
Ted Wood4c223642013-01-05 16:50:31 -080062 protected $_ci_library_paths = array(APPPATH, BASEPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030063
David Behlercda768a2011-08-14 23:52:48 +020064 /**
65 * List of paths to load models from
66 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030067 * @var array
David Behlercda768a2011-08-14 23:52:48 +020068 */
Ted Wood4c223642013-01-05 16:50:31 -080069 protected $_ci_model_paths = array(APPPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030070
David Behlercda768a2011-08-14 23:52:48 +020071 /**
72 * List of paths to load helpers from
73 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030074 * @var array
David Behlercda768a2011-08-14 23:52:48 +020075 */
Ted Wood4c223642013-01-05 16:50:31 -080076 protected $_ci_helper_paths = array(APPPATH, BASEPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030077
David Behlercda768a2011-08-14 23:52:48 +020078 /**
79 * List of loaded base classes
David Behlercda768a2011-08-14 23:52:48 +020080 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030081 * @var array
David Behlercda768a2011-08-14 23:52:48 +020082 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040083 protected $_base_classes = array(); // Set by the controller class
Andrey Andreev92ebfb62012-05-17 12:49:24 +030084
David Behlercda768a2011-08-14 23:52:48 +020085 /**
86 * List of cached variables
87 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030088 * @var array
David Behlercda768a2011-08-14 23:52:48 +020089 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040090 protected $_ci_cached_vars = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030091
David Behlercda768a2011-08-14 23:52:48 +020092 /**
93 * List of loaded classes
94 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030095 * @var array
David Behlercda768a2011-08-14 23:52:48 +020096 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040097 protected $_ci_classes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030098
David Behlercda768a2011-08-14 23:52:48 +020099 /**
100 * List of loaded files
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_loaded_files = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300105
David Behlercda768a2011-08-14 23:52:48 +0200106 /**
107 * List of loaded models
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_models = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300112
David Behlercda768a2011-08-14 23:52:48 +0200113 /**
114 * List of loaded helpers
115 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300116 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200117 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400118 protected $_ci_helpers = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300119
David Behlercda768a2011-08-14 23:52:48 +0200120 /**
121 * List of class name mappings
122 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300123 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200124 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400125 protected $_ci_varmap = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400126 'unit_test' => 'unit',
127 'user_agent' => 'agent'
128 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000129
130 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300131 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 *
Andrey Andreevcdac2482012-11-03 18:09:01 +0200133 * Sets component load paths, gets the initial output buffering level.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300134 *
135 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 */
Greg Akerf5c84022011-04-19 17:13:03 -0500137 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +0200138 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500139 $this->_ci_ob_level = ob_get_level();
David Behlercda768a2011-08-14 23:52:48 +0200140
Andrey Andreevd7297352012-01-07 22:53:14 +0200141 log_message('debug', 'Loader Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200145
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500146 /**
Andrey Andreevcdac2482012-11-03 18:09:01 +0200147 * Initializer
148 *
149 * @todo Figure out a way to move this to the constructor
150 * without breaking *package_path*() methods.
151 * @uses CI_Loader::_ci_autoloader()
152 * @used-by CI_Controller::__construct()
153 * @return void
154 */
155 public function initialize()
156 {
157 $this->_base_classes =& is_loaded();
158 $this->_ci_autoloader();
159 }
160
161 // --------------------------------------------------------------------
162
163 /**
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500164 * Is Loaded
165 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300166 * A utility method to test if a class is in the self::$_ci_classes array.
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500167 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300168 * @used-by Mainly used by Form Helper function _get_validation_object().
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500169 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300170 * @param string $class Class name to check for
171 * @return string|bool Class object name if loaded or FALSE
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500172 */
173 public function is_loaded($class)
174 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300175 return isset($this->_ci_classes[$class]) ? $this->_ci_classes[$class] : FALSE;
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500176 }
177
178 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300181 * Library Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300183 * Loads and instantiates libraries.
184 * Designed to be called from application controllers.
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300186 * @param string $library Library name
187 * @param array $params Optional parameters to pass to the library class constructor
188 * @param string $object_name An optional object name to assign to
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200190 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500191 public function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
Greg Akerce433962010-10-12 09:29:35 -0500193 if (is_array($library))
194 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100195 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500196 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600197 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500198 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000199
Greg Akerce433962010-10-12 09:29:35 -0500200 return;
201 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000202
Alex Bilbieed944a32012-06-02 11:07:47 +0100203 if ($library === '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300205 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
207
vlakoff1228fe22013-01-14 01:30:09 +0100208 if ($params !== NULL && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
210 $params = NULL;
211 }
212
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600213 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
215
216 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 /**
219 * Model Loader
220 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300221 * Loads and instantiates libraries.
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300223 * @param string $model Model name
224 * @param string $name An optional object name to assign to
225 * @param bool $db_conn An optional database connection configuration to initialize
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200227 */
Greg Akerf5c84022011-04-19 17:13:03 -0500228 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200229 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300230 if (empty($model))
231 {
232 return;
233 }
234 elseif (is_array($model))
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
Andrey Andreev5a519db2013-01-12 04:19:19 +0200236 foreach ($model as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Andrey Andreev5a519db2013-01-12 04:19:19 +0200238 $this->model(is_int($key) ? $value : $key, $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
240 return;
241 }
242
Derek Jones32bf1862010-03-02 13:46:07 -0600243 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600246 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
Derek Jones32bf1862010-03-02 13:46:07 -0600248 // The path is in front of the last slash
Andrey Andreevd47baab2012-01-09 16:56:46 +0200249 $path = substr($model, 0, ++$last_slash);
Derek Jones32bf1862010-03-02 13:46:07 -0600250
251 // And the model name behind it
Andrey Andreevd47baab2012-01-09 16:56:46 +0200252 $model = substr($model, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
Barry Mienydd671972010-10-04 16:33:58 +0200254
Phil Sturgeon10d78f62012-06-04 14:41:53 -0500255 if (empty($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
257 $name = $model;
258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 if (in_array($name, $this->_ci_models, TRUE))
261 {
262 return;
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 $CI =& get_instance();
266 if (isset($CI->$name))
267 {
268 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000272
Derek Jones32bf1862010-03-02 13:46:07 -0600273 foreach ($this->_ci_model_paths as $mod_path)
274 {
Greg Aker3a746652011-04-19 10:59:47 -0500275 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600276 {
277 continue;
278 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000279
Andrey Andreev94af3552012-03-26 23:10:42 +0300280 if ($db_conn !== FALSE && ! class_exists('CI_DB'))
Derek Jones32bf1862010-03-02 13:46:07 -0600281 {
282 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500283 {
Derek Jones32bf1862010-03-02 13:46:07 -0600284 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500285 }
Derek Jones32bf1862010-03-02 13:46:07 -0600286
287 $CI->load->database($db_conn, FALSE, TRUE);
288 }
289
Greg Akerbce13482010-10-11 15:37:16 -0500290 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600291 {
292 load_class('Model', 'core');
293 }
294
Greg Aker3a746652011-04-19 10:59:47 -0500295 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600296
297 $model = ucfirst($model);
Derek Jones32bf1862010-03-02 13:46:07 -0600298 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600299 $this->_ci_models[] = $name;
300 return;
301 }
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Jones32bf1862010-03-02 13:46:07 -0600303 // couldn't find the model
304 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 /**
310 * Database Loader
311 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300312 * @param mixed $params Database configuration options
313 * @param bool $return Whether to return the database object
314 * @param bool $query_builder Whether to enable Query Builder
315 * (overrides the configuration setting)
316 *
317 * @return void|object|bool Database object if $return is set to TRUE,
318 * FALSE on failure, void in any other case
Barry Mienydd671972010-10-04 16:33:58 +0200319 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000320 public function database($params = '', $return = FALSE, $query_builder = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
322 // Grab the super object
323 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // Do we even need to load the database class?
Andrey Andreev9d0ab042012-10-24 21:47:39 +0300326 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 +0000327 {
328 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200329 }
330
Greg Aker3a746652011-04-19 10:59:47 -0500331 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000332
333 if ($return === TRUE)
334 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000335 return DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Andrey Andreevd7297352012-01-07 22:53:14 +0200338 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // reference errors with some configurations
340 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // Load the DB class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000343 $CI->db =& DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
347
348 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300349 * Load the Database Utilities Class
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200351 * @param object $db Database object
352 * @param bool $return Whether to return the DB Forge class object or not
353 * @return void|object
Barry Mienydd671972010-10-04 16:33:58 +0200354 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200355 public function dbutil($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 $CI =& get_instance();
358
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200359 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
360 {
361 class_exists('CI_DB', FALSE) OR $this->database();
362 $db =& $CI->db;
363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364
Greg Aker3a746652011-04-19 10:59:47 -0500365 require_once(BASEPATH.'database/DB_utility.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200366 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_utility.php');
367 $class = 'CI_DB_'.$db->dbdriver.'_utility';
Derek Allard2067d1a2008-11-13 22:59:24 +0000368
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200369 if ($return === TRUE)
370 {
371 return new $class($db);
372 }
373
374 $CI->dbutil = new $class($db);
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // --------------------------------------------------------------------
378
379 /**
380 * Load the Database Forge Class
381 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200382 * @param object $db Database object
383 * @param bool $return Whether to return the DB Forge class object or not
384 * @return void|object
Barry Mienydd671972010-10-04 16:33:58 +0200385 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200386 public function dbforge($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200388 $CI =& get_instance();
389 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200391 class_exists('CI_DB', FALSE) OR $this->database();
392 $db =& $CI->db;
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Greg Aker3a746652011-04-19 10:59:47 -0500395 require_once(BASEPATH.'database/DB_forge.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200396 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_forge.php');
Andrey Andreeva287a342012-11-05 23:19:59 +0200397
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200398 if ( ! empty($db->subdriver))
Andrey Andreeva287a342012-11-05 23:19:59 +0200399 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200400 $driver_path = BASEPATH.'database/drivers/'.$db->dbdriver.'/subdrivers/'.$db->dbdriver.'_'.$db->subdriver.'_forge.php';
Andrey Andreeva287a342012-11-05 23:19:59 +0200401 if (file_exists($driver_path))
402 {
403 require_once($driver_path);
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200404 $class = 'CI_DB_'.$db->dbdriver.'_'.$db->subdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200405 }
406 }
407 else
408 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200409 $class = 'CI_DB_'.$db->dbdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200410 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000411
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200412 if ($return === TRUE)
413 {
414 return new $class($db);
415 }
416
417 $CI->dbforge = new $class($db);
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300423 * View Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300425 * Loads "view" files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300427 * @param string $view View name
428 * @param array $vars An associative array of data
429 * to be extracted for use in the view
430 * @param bool $return Whether to return the view output
431 * or leave it to the Output class
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @return void
433 */
Greg Akerf5c84022011-04-19 17:13:03 -0500434 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
436 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300442 * Generic File Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300444 * @param string $path File path
445 * @param bool $return Whether to return the file output
446 * @return void|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 */
Greg Akerf5c84022011-04-19 17:13:03 -0500448 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
450 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 /**
456 * Set Variables
457 *
458 * Once variables are set they become available within
459 * the controller class and its "view" files.
460 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300461 * @param array|object|string $vars
462 * An associative array or object containing values
463 * to be set, or a value's name if string
464 * @param string $val Value to set, only used if $vars is a string
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 * @return void
466 */
Greg Akerf5c84022011-04-19 17:13:03 -0500467 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100469 if ($val !== '' && is_string($vars))
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
471 $vars = array($vars => $val);
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200475
Andrey Andreev94af3552012-03-26 23:10:42 +0300476 if (is_array($vars) && count($vars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
478 foreach ($vars as $key => $val)
479 {
480 $this->_ci_cached_vars[$key] = $val;
481 }
482 }
483 }
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 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600488 * Get Variable
489 *
490 * Check if a variable is set and retrieve it.
491 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300492 * @param string $key Variable name
493 * @return mixed The variable or NULL if not found
Phil Sturgeon8731f642011-07-22 16:11:34 -0600494 */
495 public function get_var($key)
496 {
497 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
498 }
499
500 // --------------------------------------------------------------------
501
502 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600503 * Get Variables
504 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300505 * Retrieves all loaded variables.
Shane Pearson81dd2232011-11-18 20:49:35 -0600506 *
507 * @return array
508 */
509 public function get_vars()
510 {
511 return $this->_ci_cached_vars;
512 }
513
514 // --------------------------------------------------------------------
515
516 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300517 * Helper Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300519 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * @return void
521 */
Greg Akerf5c84022011-04-19 17:13:03 -0500522 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200523 {
Derek Jones32bf1862010-03-02 13:46:07 -0600524 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200525 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 if (isset($this->_ci_helpers[$helper]))
527 {
528 continue;
529 }
Derek Jones32bf1862010-03-02 13:46:07 -0600530
Barry Mienydd671972010-10-04 16:33:58 +0200531 // Is this a helper extension request?
Andrey Andreev12d7b462012-11-12 13:42:09 +0200532 $ext_helper = config_item('subclass_prefix').$helper;
533 $ext_loaded = FALSE;
534 foreach ($this->_ci_helper_paths as $path)
535 {
536 if (file_exists($path.'helpers/'.$ext_helper.'.php'))
537 {
538 include_once($path.'helpers/'.$ext_helper.'.php');
539 $ext_loaded = TRUE;
540 }
541 }
542
543 // If we have loaded extensions - check if the base one is here
544 if ($ext_loaded === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
Greg Aker3a746652011-04-19 10:59:47 -0500546 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 if ( ! file_exists($base_helper))
548 {
Greg Aker3a746652011-04-19 10:59:47 -0500549 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 include_once($base_helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600553 $this->_ci_helpers[$helper] = TRUE;
554 log_message('debug', 'Helper loaded: '.$helper);
555 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 }
Barry Mienydd671972010-10-04 16:33:58 +0200557
Andrey Andreev12d7b462012-11-12 13:42:09 +0200558 // No extensions found ... try loading regular helpers and/or overrides
Derek Jones32bf1862010-03-02 13:46:07 -0600559 foreach ($this->_ci_helper_paths as $path)
560 {
Greg Aker3a746652011-04-19 10:59:47 -0500561 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200562 {
Greg Aker3a746652011-04-19 10:59:47 -0500563 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600564
565 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200566 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600567 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 }
569 }
570
Derek Jones32bf1862010-03-02 13:46:07 -0600571 // unable to load the helper
572 if ( ! isset($this->_ci_helpers[$helper]))
573 {
Greg Aker3a746652011-04-19 10:59:47 -0500574 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600575 }
Barry Mienydd671972010-10-04 16:33:58 +0200576 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 }
Barry Mienydd671972010-10-04 16:33:58 +0200578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 /**
582 * Load Helpers
583 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300584 * An alias for the helper() method in case the developer has
585 * written the plural form of it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300587 * @uses CI_Loader::helper()
588 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 * @return void
590 */
Greg Akerf5c84022011-04-19 17:13:03 -0500591 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 $this->helper($helpers);
594 }
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 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300599 * Language Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300601 * Loads language files.
602 *
603 * @param string|string[] $files List of language file names to load
604 * @param string Language name
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 * @return void
606 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300607 public function language($files = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
609 $CI =& get_instance();
610
Andrey Andreeved4b2582012-10-27 17:46:52 +0300611 is_array($files) OR $files = array($files);
Derek Allard2067d1a2008-11-13 22:59:24 +0000612
Andrey Andreeved4b2582012-10-27 17:46:52 +0300613 foreach ($files as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200614 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 $CI->lang->load($langfile, $lang);
616 }
617 }
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300622 * Config Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300624 * Loads a config file (an alias for CI_Config::load()).
625 *
626 * @uses CI_Config::load()
627 * @param string $file Configuration file name
628 * @param bool $use_sections Whether configuration values should be loaded into their own section
629 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
630 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 */
Greg Akerf5c84022011-04-19 17:13:03 -0500632 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200633 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 $CI =& get_instance();
Andrey Andreeved4b2582012-10-27 17:46:52 +0300635 return $CI->config->load($file, $use_sections, $fail_gracefully);
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 }
637
638 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300641 * Driver Loader
Derek Jones8dca0412010-03-05 13:01:44 -0600642 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300643 * Loads a driver library.
Derek Jones8dca0412010-03-05 13:01:44 -0600644 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300645 * @param string|string[] $library Driver name(s)
646 * @param array $params Optional parameters to pass to the driver
647 * @param string $object_name An optional object name to assign to
648 *
649 * @return void|object|bool Object or FALSE on failure if $library is a string
650 * and $object_name is set. void otherwise.
Derek Jones8dca0412010-03-05 13:01:44 -0600651 */
Greg Akerf5c84022011-04-19 17:13:03 -0500652 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600653 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800654 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800655 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800656 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800657 {
658 $this->driver($driver);
659 }
dchill420fc3be52012-08-27 20:54:23 -0400660 return;
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800661 }
662
Alex Bilbieed944a32012-06-02 11:07:47 +0100663 if ($library === '')
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200664 {
665 return FALSE;
666 }
667
dchill426262d052012-11-24 18:41:13 -0500668 if ( ! class_exists('CI_Driver_Library'))
669 {
670 // We aren't instantiating an object here, just making the base class available
671 require BASEPATH.'libraries/Driver.php';
672 }
673
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600674 // We can save the loader some time since Drivers will *always* be in a subfolder,
675 // and typically identically named to the library
676 if ( ! strpos($library, '/'))
677 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500678 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600679 }
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Jones8dca0412010-03-05 13:01:44 -0600681 return $this->library($library, $params, $object_name);
682 }
683
684 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200685
Derek Jones8dca0412010-03-05 13:01:44 -0600686 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600687 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300689 * Prepends a parent path to the library, model, helper and config
690 * path arrays.
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300692 * @see CI_Loader::$_ci_library_paths
693 * @see CI_Loader::$_ci_model_paths
694 * @see CI_Loader::$_ci_helper_paths
695 * @see CI_Config::$_config_paths
696 *
697 * @param string $path Path to add
698 * @param bool $view_cascade (default: TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600700 */
Andrey Andreevcce91802012-06-12 13:25:31 +0300701 public function add_package_path($path, $view_cascade = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600702 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500703 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200704
Derek Jones32bf1862010-03-02 13:46:07 -0600705 array_unshift($this->_ci_library_paths, $path);
706 array_unshift($this->_ci_model_paths, $path);
707 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200708
Greg Akerf5c84022011-04-19 17:13:03 -0500709 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
710
Derek Jones32bf1862010-03-02 13:46:07 -0600711 // Add config file path
712 $config =& $this->_ci_get_component('config');
and-ersb3ec9422013-01-03 16:05:12 +0100713 $config->_config_paths[] = $path;
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 }
715
716 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600717
718 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000719 * Get Package Paths
720 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300721 * Return a list of all package paths.
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000722 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300723 * @param bool $include_base Whether to include BASEPATH (default: TRUE)
724 * @return array
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000725 */
Greg Akerf5c84022011-04-19 17:13:03 -0500726 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000727 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300728 return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000729 }
730
731 // --------------------------------------------------------------------
732
733 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600734 * Remove Package Path
735 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300736 * Remove a path from the library, model, helper and/or config
737 * path arrays if it exists. If no path is provided, the most recently
738 * added path will be removed removed.
Derek Jones32bf1862010-03-02 13:46:07 -0600739 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300740 * @param string $path Path to remove
Andrey Andreev94af3552012-03-26 23:10:42 +0300741 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600742 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300743 public function remove_package_path($path = '')
Derek Jones32bf1862010-03-02 13:46:07 -0600744 {
745 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200746
Alex Bilbieed944a32012-06-02 11:07:47 +0100747 if ($path === '')
Derek Jones32bf1862010-03-02 13:46:07 -0600748 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200749 array_shift($this->_ci_library_paths);
750 array_shift($this->_ci_model_paths);
751 array_shift($this->_ci_helper_paths);
752 array_shift($this->_ci_view_paths);
Korri3684d342012-04-17 00:35:08 -0400753 array_pop($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600754 }
755 else
756 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500757 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600758 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
759 {
760 if (($key = array_search($path, $this->{$var})) !== FALSE)
761 {
762 unset($this->{$var}[$key]);
763 }
764 }
David Behlercda768a2011-08-14 23:52:48 +0200765
Greg Akerf5c84022011-04-19 17:13:03 -0500766 if (isset($this->_ci_view_paths[$path.'views/']))
767 {
768 unset($this->_ci_view_paths[$path.'views/']);
769 }
Barry Mienydd671972010-10-04 16:33:58 +0200770
Derek Jones32bf1862010-03-02 13:46:07 -0600771 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
772 {
773 unset($config->_config_paths[$key]);
774 }
775 }
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Jones32bf1862010-03-02 13:46:07 -0600777 // make sure the application default paths are still in the array
778 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
779 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
780 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500781 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600782 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
783 }
784
785 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200786
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300788 * Internal CI Data Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300790 * Used to load views and files.
791 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 * Variables are prefixed with _ci_ to avoid symbol collision with
Andrey Andreeved4b2582012-10-27 17:46:52 +0300793 * variables made available to view files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300795 * @used-by CI_Loader::view()
796 * @used-by CI_Loader::file()
797 * @param array $_ci_data Data to load
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 * @return void
799 */
Greg Akerf5c84022011-04-19 17:13:03 -0500800 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
802 // Set the default data variables
803 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
804 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300805 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 }
David Behlercda768a2011-08-14 23:52:48 +0200807
Greg Akerf5c84022011-04-19 17:13:03 -0500808 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000809
810 // Set the path to the requested file
Alex Bilbie40bd2a72012-06-02 16:04:15 +0100811 if (is_string($_ci_path) && $_ci_path !== '')
Greg Aker8807be32011-04-21 13:06:15 -0500812 {
813 $_ci_x = explode('/', $_ci_path);
814 $_ci_file = end($_ci_x);
815 }
816 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 {
818 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Alex Bilbieed944a32012-06-02 11:07:47 +0100819 $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500820
Joe McFrederick64f470b2012-08-18 12:29:56 -0400821 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
Greg Akerf5c84022011-04-19 17:13:03 -0500822 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400823 if (file_exists($_ci_view_file.$_ci_file))
Greg Akerf5c84022011-04-19 17:13:03 -0500824 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400825 $_ci_path = $_ci_view_file.$_ci_file;
Greg Akerf5c84022011-04-19 17:13:03 -0500826 $file_exists = TRUE;
827 break;
828 }
David Behlercda768a2011-08-14 23:52:48 +0200829
Greg Akerf5c84022011-04-19 17:13:03 -0500830 if ( ! $cascade)
831 {
832 break;
David Behlercda768a2011-08-14 23:52:48 +0200833 }
Greg Akerf5c84022011-04-19 17:13:03 -0500834 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 }
Barry Mienydd671972010-10-04 16:33:58 +0200836
Greg Akerf5c84022011-04-19 17:13:03 -0500837 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 {
839 show_error('Unable to load the requested file: '.$_ci_file);
840 }
Barry Mienydd671972010-10-04 16:33:58 +0200841
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 // This allows anything loaded using $this->load (views, files, etc.)
843 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500844 $_ci_CI =& get_instance();
845 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500847 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500849 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 }
851 }
852
853 /*
854 * Extract and cache variables
855 *
vlakoff02506182012-07-03 07:28:50 +0200856 * You can either set variables using the dedicated $this->load->vars()
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 * function or via the second parameter of this function. We'll merge
858 * the two types and cache them so that views that are embedded within
859 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200860 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 if (is_array($_ci_vars))
862 {
863 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
864 }
865 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 /*
868 * Buffer the output
869 *
870 * We buffer the output for two reasons:
871 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200872 * 2. So that the final rendered template can be post-processed by
dchill425628ba02012-08-08 12:05:45 -0400873 * the output class. Why do we need post processing? For one thing,
874 * in order to show the elapsed page load time. Unless we can
875 * intercept the content right before it's sent to the browser and
876 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 */
878 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200879
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 // If the PHP installation does not support short tags we'll
881 // do a little string replacement, changing the short tags
882 // to standard PHP echo statements.
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200883 if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE
884 && config_item('rewrite_short_tags') === TRUE && function_usable('eval')
885 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200887 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 }
889 else
890 {
891 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
892 }
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 // Return the file data if requested
897 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200898 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 $buffer = ob_get_contents();
900 @ob_end_clean();
901 return $buffer;
902 }
903
904 /*
905 * Flush the buffer... or buff the flusher?
906 *
907 * In order to permit views to be nested within
908 * other views, we need to flush the content back out whenever
909 * we are beyond the first level of output buffering so that
910 * it can be seen and included properly by the first included
911 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200912 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 if (ob_get_level() > $this->_ci_ob_level + 1)
914 {
915 ob_end_flush();
916 }
917 else
918 {
Greg Aker22f1a632010-11-10 15:34:35 -0600919 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 @ob_end_clean();
921 }
922 }
923
924 // --------------------------------------------------------------------
925
926 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300927 * Internal CI Class Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300929 * @used-by CI_Loader::library()
930 * @uses CI_Loader::_ci_init_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300932 * @param string $class Class name to load
933 * @param mixed $params Optional parameters to pass to the class constructor
934 * @param string $object_name Optional object name to assign to
Barry Mienydd671972010-10-04 16:33:58 +0200935 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 */
Greg Akerf5c84022011-04-19 17:13:03 -0500937 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200938 {
939 // Get the class name, and while we're at it trim any slashes.
940 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500942 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200943
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 // Was the path included with the class name?
945 // We look for a slash to determine this
946 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600947 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
Derek Jones32bf1862010-03-02 13:46:07 -0600949 // Extract the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200950 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200951
Derek Jones32bf1862010-03-02 13:46:07 -0600952 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200953 $class = substr($class, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 }
955
956 // We'll test for both lowercase and capitalized versions of the file name
957 foreach (array(ucfirst($class), strtolower($class)) as $class)
958 {
Greg Aker3a746652011-04-19 10:59:47 -0500959 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000960
Barry Mienydd671972010-10-04 16:33:58 +0200961 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 if (file_exists($subclass))
963 {
Greg Aker3a746652011-04-19 10:59:47 -0500964 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200965
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 if ( ! file_exists($baseclass))
967 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200968 log_message('error', 'Unable to load the requested class: '.$class);
969 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 }
971
Andrey Andreevd7297352012-01-07 22:53:14 +0200972 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 if (in_array($subclass, $this->_ci_loaded_files))
974 {
975 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200976 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 // return a new instance of the object
vlakoff1228fe22013-01-14 01:30:09 +0100978 if ($object_name !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 {
980 $CI =& get_instance();
981 if ( ! isset($CI->$object_name))
982 {
Barry Mienydd671972010-10-04 16:33:58 +0200983 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 }
985 }
Barry Mienydd671972010-10-04 16:33:58 +0200986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200988 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 return;
990 }
Barry Mienydd671972010-10-04 16:33:58 +0200991
992 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 include_once($subclass);
994 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200995
996 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 }
Barry Mienydd671972010-10-04 16:33:58 +0200998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -06001000 $is_duplicate = FALSE;
1001 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 {
Greg Aker3a746652011-04-19 10:59:47 -05001003 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -06001004
Andrey Andreevd7297352012-01-07 22:53:14 +02001005 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 if ( ! file_exists($filepath))
1007 {
1008 continue;
1009 }
Barry Mienydd671972010-10-04 16:33:58 +02001010
Andrey Andreevd7297352012-01-07 22:53:14 +02001011 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 if (in_array($filepath, $this->_ci_loaded_files))
1013 {
1014 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +02001015 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 // return a new instance of the object
vlakoff1228fe22013-01-14 01:30:09 +01001017 if ($object_name !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 {
1019 $CI =& get_instance();
1020 if ( ! isset($CI->$object_name))
1021 {
1022 return $this->_ci_init_class($class, '', $params, $object_name);
1023 }
1024 }
Barry Mienydd671972010-10-04 16:33:58 +02001025
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +02001027 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 return;
1029 }
Barry Mienydd671972010-10-04 16:33:58 +02001030
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 include_once($filepath);
1032 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +02001033 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 }
1035 } // END FOREACH
1036
Andrey Andreevd7297352012-01-07 22:53:14 +02001037 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Alex Bilbieed944a32012-06-02 11:07:47 +01001038 if ($subdir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 {
1040 $path = strtolower($class).'/'.$class;
dchill420fc3be52012-08-27 20:54:23 -04001041 return $this->_ci_load_class($path, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 }
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001043 elseif (ucfirst($subdir) != $subdir)
dchill42aee92652012-08-26 21:45:35 -04001044 {
1045 // Lowercase subdir failed - retry capitalized
1046 $path = ucfirst($subdir).$class;
dchill420fc3be52012-08-27 20:54:23 -04001047 return $this->_ci_load_class($path, $params, $object_name);
dchill42aee92652012-08-26 21:45:35 -04001048 }
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 // If we got this far we were unable to find the requested class.
1051 // We do not issue errors if the load call failed due to a duplicate request
Alex Bilbieed944a32012-06-02 11:07:47 +01001052 if ($is_duplicate === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001054 log_message('error', 'Unable to load the requested class: '.$class);
1055 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 }
1057 }
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 {
1118 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 {
1120 $name = 'CI_'.$class;
1121 }
Barry Mienydd671972010-10-04 16:33:58 +02001122 elseif (class_exists(config_item('subclass_prefix').$class))
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?
1137 if ( ! class_exists($name))
1138 {
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
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001146
vlakoff1228fe22013-01-14 01:30:09 +01001147 if ($object_name === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001149 $classvar = isset($this->_ci_varmap[$class]) ? $this->_ci_varmap[$class] : $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 }
1151 else
1152 {
1153 $classvar = $object_name;
1154 }
1155
Barry Mienydd671972010-10-04 16:33:58 +02001156 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 $this->_ci_classes[$class] = $classvar;
1158
Barry Mienydd671972010-10-04 16:33:58 +02001159 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 $CI =& get_instance();
1161 if ($config !== NULL)
1162 {
1163 $CI->$classvar = new $name($config);
1164 }
1165 else
Barry Mienydd671972010-10-04 16:33:58 +02001166 {
Andrey Andreeva11b16b2012-03-28 12:22:04 +03001167 $CI->$classvar = new $name();
Barry Mienydd671972010-10-04 16:33:58 +02001168 }
1169 }
1170
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001172
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001174 * CI Autoloader
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001176 * Loads component listed in the config/autoload.php file.
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 *
Andrey Andreevcdac2482012-11-03 18:09:01 +02001178 * @used-by CI_Loader::initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 * @return void
1180 */
Shane Pearson665baec2011-08-22 18:52:19 -05001181 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001182 {
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001183 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001184 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001185 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001186 }
1187 else
1188 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001189 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001190 }
Barry Mienydd671972010-10-04 16:33:58 +02001191
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 if ( ! isset($autoload))
1193 {
1194 return FALSE;
1195 }
Barry Mienydd671972010-10-04 16:33:58 +02001196
Phil Sturgeon9730c752010-12-15 10:50:15 +00001197 // Autoload packages
1198 if (isset($autoload['packages']))
1199 {
1200 foreach ($autoload['packages'] as $package_path)
1201 {
1202 $this->add_package_path($package_path);
1203 }
1204 }
1205
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 // Load any custom config file
1207 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001208 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 $CI =& get_instance();
1210 foreach ($autoload['config'] as $key => $val)
1211 {
1212 $CI->config->load($val);
1213 }
Barry Mienydd671972010-10-04 16:33:58 +02001214 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001215
Derek Jonesc6da5032010-03-09 20:44:27 -06001216 // Autoload helpers and languages
1217 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001218 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001219 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 {
1221 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001222 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 }
1224
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001226 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 {
1228 // Load the database driver.
1229 if (in_array('database', $autoload['libraries']))
1230 {
1231 $this->database();
1232 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1233 }
Barry Mienydd671972010-10-04 16:33:58 +02001234
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 // Load all other libraries
1236 foreach ($autoload['libraries'] as $item)
1237 {
1238 $this->library($item);
1239 }
Barry Mienydd671972010-10-04 16:33:58 +02001240 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001241
Darren Hillc4e266b2011-08-30 15:40:27 -04001242 // Autoload drivers
1243 if (isset($autoload['drivers']))
1244 {
Darren Hillca3be1d2011-08-31 08:31:18 -04001245 foreach ($autoload['drivers'] as $item)
1246 {
1247 $this->driver($item);
1248 }
Darren Hillc4e266b2011-08-30 15:40:27 -04001249 }
1250
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 // Autoload models
1252 if (isset($autoload['model']))
1253 {
1254 $this->model($autoload['model']);
1255 }
Barry Mienydd671972010-10-04 16:33:58 +02001256 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001257
1258 // --------------------------------------------------------------------
1259
1260 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001261 * CI Object to Array translator
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001263 * Takes an object as input and converts the class variables to
1264 * an associative array with key/value pairs.
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001266 * @param object $object Object data to translate
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 * @return array
1268 */
Greg Akerf5c84022011-04-19 17:13:03 -05001269 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001271 return is_object($object) ? get_object_vars($object) : $object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 }
1273
1274 // --------------------------------------------------------------------
1275
1276 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001277 * CI Component getter
Derek Jones32bf1862010-03-02 13:46:07 -06001278 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001279 * Get a reference to a specific library or model.
1280 *
1281 * @param string $component Component name
Derek Jones32bf1862010-03-02 13:46:07 -06001282 * @return bool
1283 */
Greg Akerf5c84022011-04-19 17:13:03 -05001284 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001285 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001286 $CI =& get_instance();
1287 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001288 }
1289
1290 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001291
Derek Jones32bf1862010-03-02 13:46:07 -06001292 /**
1293 * Prep filename
1294 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001295 * This function prepares filenames of various items to
1296 * make their loading more reliable.
Derek Jones32bf1862010-03-02 13:46:07 -06001297 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001298 * @param string|string[] $filename Filename(s)
1299 * @param string $extension Filename extension
Derek Jones32bf1862010-03-02 13:46:07 -06001300 * @return array
1301 */
Greg Akerf5c84022011-04-19 17:13:03 -05001302 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001303 {
1304 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001305 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001306 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001307 }
1308 else
1309 {
1310 foreach ($filename as $key => $val)
1311 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001312 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001313 }
Barry Mienydd671972010-10-04 16:33:58 +02001314
Derek Jones32bf1862010-03-02 13:46:07 -06001315 return $filename;
1316 }
1317 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001318
Derek Allard2067d1a2008-11-13 22:59:24 +00001319}
1320
1321/* End of file Loader.php */
Andrey Andreev9438e262012-10-05 13:16:27 +03001322/* Location: ./system/core/Loader.php */