blob: a9eec396c4b13634157bc9d1909180083fd1a0f9 [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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040055 protected $_ci_view_paths = array();
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 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040062 protected $_ci_library_paths = array();
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 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040069 protected $_ci_model_paths = array();
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 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040076 protected $_ci_helper_paths = array();
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();
Derek Jones32bf1862010-03-02 13:46:07 -0600140 $this->_ci_library_paths = array(APPPATH, BASEPATH);
141 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
142 $this->_ci_model_paths = array(APPPATH);
Joe Cianflone8eef9c72011-08-21 10:39:06 -0400143 $this->_ci_view_paths = array(VIEWPATH => TRUE);
David Behlercda768a2011-08-14 23:52:48 +0200144
Andrey Andreevd7297352012-01-07 22:53:14 +0200145 log_message('debug', 'Loader Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200149
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500150 /**
Andrey Andreevcdac2482012-11-03 18:09:01 +0200151 * Initializer
152 *
153 * @todo Figure out a way to move this to the constructor
154 * without breaking *package_path*() methods.
155 * @uses CI_Loader::_ci_autoloader()
156 * @used-by CI_Controller::__construct()
157 * @return void
158 */
159 public function initialize()
160 {
161 $this->_base_classes =& is_loaded();
162 $this->_ci_autoloader();
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500168 * Is Loaded
169 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300170 * A utility method to test if a class is in the self::$_ci_classes array.
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500171 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300172 * @used-by Mainly used by Form Helper function _get_validation_object().
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500173 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300174 * @param string $class Class name to check for
175 * @return string|bool Class object name if loaded or FALSE
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500176 */
177 public function is_loaded($class)
178 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300179 return isset($this->_ci_classes[$class]) ? $this->_ci_classes[$class] : FALSE;
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500180 }
181
182 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300185 * Library Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300187 * Loads and instantiates libraries.
188 * Designed to be called from application controllers.
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300190 * @param string $library Library name
191 * @param array $params Optional parameters to pass to the library class constructor
192 * @param string $object_name An optional object name to assign to
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200194 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500195 public function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
Greg Akerce433962010-10-12 09:29:35 -0500197 if (is_array($library))
198 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100199 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500200 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600201 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500202 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000203
Greg Akerce433962010-10-12 09:29:35 -0500204 return;
205 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000206
Alex Bilbieed944a32012-06-02 11:07:47 +0100207 if ($library === '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300209 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 }
211
Derek Jones32bf1862010-03-02 13:46:07 -0600212 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
214 $params = NULL;
215 }
216
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600217 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
219
220 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 /**
223 * Model Loader
224 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300225 * Loads and instantiates libraries.
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300227 * @param string $model Model name
228 * @param string $name An optional object name to assign to
229 * @param bool $db_conn An optional database connection configuration to initialize
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200231 */
Greg Akerf5c84022011-04-19 17:13:03 -0500232 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200233 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300234 if (empty($model))
235 {
236 return;
237 }
238 elseif (is_array($model))
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
Joel Limberg3cf174b2012-07-13 20:49:57 +0300240 foreach ($model as $class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Joel Limberg3cf174b2012-07-13 20:49:57 +0300242 $this->model($class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
244 return;
245 }
246
Derek Jones32bf1862010-03-02 13:46:07 -0600247 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600250 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
Derek Jones32bf1862010-03-02 13:46:07 -0600252 // The path is in front of the last slash
Andrey Andreevd47baab2012-01-09 16:56:46 +0200253 $path = substr($model, 0, ++$last_slash);
Derek Jones32bf1862010-03-02 13:46:07 -0600254
255 // And the model name behind it
Andrey Andreevd47baab2012-01-09 16:56:46 +0200256 $model = substr($model, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
Barry Mienydd671972010-10-04 16:33:58 +0200258
Phil Sturgeon10d78f62012-06-04 14:41:53 -0500259 if (empty($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
261 $name = $model;
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 if (in_array($name, $this->_ci_models, TRUE))
265 {
266 return;
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 $CI =& get_instance();
270 if (isset($CI->$name))
271 {
272 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
273 }
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 $model = 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
Andrey Andreev94af3552012-03-26 23:10:42 +0300284 if ($db_conn !== FALSE && ! class_exists('CI_DB'))
Derek Jones32bf1862010-03-02 13:46:07 -0600285 {
286 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500287 {
Derek Jones32bf1862010-03-02 13:46:07 -0600288 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500289 }
Derek Jones32bf1862010-03-02 13:46:07 -0600290
291 $CI->load->database($db_conn, FALSE, TRUE);
292 }
293
Greg Akerbce13482010-10-11 15:37:16 -0500294 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600295 {
296 load_class('Model', 'core');
297 }
298
Greg Aker3a746652011-04-19 10:59:47 -0500299 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600300
301 $model = ucfirst($model);
Derek Jones32bf1862010-03-02 13:46:07 -0600302 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600303 $this->_ci_models[] = $name;
304 return;
305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Jones32bf1862010-03-02 13:46:07 -0600307 // couldn't find the model
308 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 /**
314 * Database Loader
315 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300316 * @param mixed $params Database configuration options
317 * @param bool $return Whether to return the database object
318 * @param bool $query_builder Whether to enable Query Builder
319 * (overrides the configuration setting)
320 *
321 * @return void|object|bool Database object if $return is set to TRUE,
322 * FALSE on failure, void in any other case
Barry Mienydd671972010-10-04 16:33:58 +0200323 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000324 public function database($params = '', $return = FALSE, $query_builder = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
326 // Grab the super object
327 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 // Do we even need to load the database class?
Andrey Andreev9d0ab042012-10-24 21:47:39 +0300330 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 +0000331 {
332 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200333 }
334
Greg Aker3a746652011-04-19 10:59:47 -0500335 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000336
337 if ($return === TRUE)
338 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000339 return DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Andrey Andreevd7297352012-01-07 22:53:14 +0200342 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 // reference errors with some configurations
344 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // Load the DB class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000347 $CI->db =& DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // --------------------------------------------------------------------
351
352 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300353 * Load the Database Utilities Class
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200355 * @param object $db Database object
356 * @param bool $return Whether to return the DB Forge class object or not
357 * @return void|object
Barry Mienydd671972010-10-04 16:33:58 +0200358 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200359 public function dbutil($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 $CI =& get_instance();
362
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200363 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
364 {
365 class_exists('CI_DB', FALSE) OR $this->database();
366 $db =& $CI->db;
367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Greg Aker3a746652011-04-19 10:59:47 -0500369 require_once(BASEPATH.'database/DB_utility.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200370 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_utility.php');
371 $class = 'CI_DB_'.$db->dbdriver.'_utility';
Derek Allard2067d1a2008-11-13 22:59:24 +0000372
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200373 if ($return === TRUE)
374 {
375 return new $class($db);
376 }
377
378 $CI->dbutil = new $class($db);
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 // --------------------------------------------------------------------
382
383 /**
384 * Load the Database Forge Class
385 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200386 * @param object $db Database object
387 * @param bool $return Whether to return the DB Forge class object or not
388 * @return void|object
Barry Mienydd671972010-10-04 16:33:58 +0200389 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200390 public function dbforge($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200392 $CI =& get_instance();
393 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200395 class_exists('CI_DB', FALSE) OR $this->database();
396 $db =& $CI->db;
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Greg Aker3a746652011-04-19 10:59:47 -0500399 require_once(BASEPATH.'database/DB_forge.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200400 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_forge.php');
Andrey Andreeva287a342012-11-05 23:19:59 +0200401
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200402 if ( ! empty($db->subdriver))
Andrey Andreeva287a342012-11-05 23:19:59 +0200403 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200404 $driver_path = BASEPATH.'database/drivers/'.$db->dbdriver.'/subdrivers/'.$db->dbdriver.'_'.$db->subdriver.'_forge.php';
Andrey Andreeva287a342012-11-05 23:19:59 +0200405 if (file_exists($driver_path))
406 {
407 require_once($driver_path);
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200408 $class = 'CI_DB_'.$db->dbdriver.'_'.$db->subdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200409 }
410 }
411 else
412 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200413 $class = 'CI_DB_'.$db->dbdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200414 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000415
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200416 if ($return === TRUE)
417 {
418 return new $class($db);
419 }
420
421 $CI->dbforge = new $class($db);
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300427 * View Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300429 * Loads "view" files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300431 * @param string $view View name
432 * @param array $vars An associative array of data
433 * to be extracted for use in the view
434 * @param bool $return Whether to return the view output
435 * or leave it to the Output class
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 * @return void
437 */
Greg Akerf5c84022011-04-19 17:13:03 -0500438 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
440 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300446 * Generic File Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300448 * @param string $path File path
449 * @param bool $return Whether to return the file output
450 * @return void|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 */
Greg Akerf5c84022011-04-19 17:13:03 -0500452 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
454 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 /**
460 * Set Variables
461 *
462 * Once variables are set they become available within
463 * the controller class and its "view" files.
464 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300465 * @param array|object|string $vars
466 * An associative array or object containing values
467 * to be set, or a value's name if string
468 * @param string $val Value to set, only used if $vars is a string
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 * @return void
470 */
Greg Akerf5c84022011-04-19 17:13:03 -0500471 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100473 if ($val !== '' && is_string($vars))
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
475 $vars = array($vars => $val);
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200479
Andrey Andreev94af3552012-03-26 23:10:42 +0300480 if (is_array($vars) && count($vars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
482 foreach ($vars as $key => $val)
483 {
484 $this->_ci_cached_vars[$key] = $val;
485 }
486 }
487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600492 * Get Variable
493 *
494 * Check if a variable is set and retrieve it.
495 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300496 * @param string $key Variable name
497 * @return mixed The variable or NULL if not found
Phil Sturgeon8731f642011-07-22 16:11:34 -0600498 */
499 public function get_var($key)
500 {
501 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
502 }
503
504 // --------------------------------------------------------------------
505
506 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600507 * Get Variables
508 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300509 * Retrieves all loaded variables.
Shane Pearson81dd2232011-11-18 20:49:35 -0600510 *
511 * @return array
512 */
513 public function get_vars()
514 {
515 return $this->_ci_cached_vars;
516 }
517
518 // --------------------------------------------------------------------
519
520 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300521 * Helper Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300523 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 * @return void
525 */
Greg Akerf5c84022011-04-19 17:13:03 -0500526 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200527 {
Derek Jones32bf1862010-03-02 13:46:07 -0600528 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200529 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 if (isset($this->_ci_helpers[$helper]))
531 {
532 continue;
533 }
Derek Jones32bf1862010-03-02 13:46:07 -0600534
Greg Aker3a746652011-04-19 10:59:47 -0500535 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000536
Barry Mienydd671972010-10-04 16:33:58 +0200537 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 if (file_exists($ext_helper))
539 {
Greg Aker3a746652011-04-19 10:59:47 -0500540 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 if ( ! file_exists($base_helper))
543 {
Greg Aker3a746652011-04-19 10:59:47 -0500544 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 }
Barry Mienydd671972010-10-04 16:33:58 +0200546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 include_once($ext_helper);
548 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Jones32bf1862010-03-02 13:46:07 -0600550 $this->_ci_helpers[$helper] = TRUE;
551 log_message('debug', 'Helper loaded: '.$helper);
552 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Jones32bf1862010-03-02 13:46:07 -0600555 // Try to load the helper
556 foreach ($this->_ci_helper_paths as $path)
557 {
Greg Aker3a746652011-04-19 10:59:47 -0500558 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200559 {
Greg Aker3a746652011-04-19 10:59:47 -0500560 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600561
562 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200563 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600564 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 }
566 }
567
Derek Jones32bf1862010-03-02 13:46:07 -0600568 // unable to load the helper
569 if ( ! isset($this->_ci_helpers[$helper]))
570 {
Greg Aker3a746652011-04-19 10:59:47 -0500571 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600572 }
Barry Mienydd671972010-10-04 16:33:58 +0200573 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 /**
579 * Load Helpers
580 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300581 * An alias for the helper() method in case the developer has
582 * written the plural form of it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300584 * @uses CI_Loader::helper()
585 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 * @return void
587 */
Greg Akerf5c84022011-04-19 17:13:03 -0500588 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 {
590 $this->helper($helpers);
591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300596 * Language Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300598 * Loads language files.
599 *
600 * @param string|string[] $files List of language file names to load
601 * @param string Language name
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 * @return void
603 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300604 public function language($files = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
606 $CI =& get_instance();
607
Andrey Andreeved4b2582012-10-27 17:46:52 +0300608 is_array($files) OR $files = array($files);
Derek Allard2067d1a2008-11-13 22:59:24 +0000609
Andrey Andreeved4b2582012-10-27 17:46:52 +0300610 foreach ($files as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200611 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 $CI->lang->load($langfile, $lang);
613 }
614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300619 * Config Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300621 * Loads a config file (an alias for CI_Config::load()).
622 *
623 * @uses CI_Config::load()
624 * @param string $file Configuration file name
625 * @param bool $use_sections Whether configuration values should be loaded into their own section
626 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
627 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 */
Greg Akerf5c84022011-04-19 17:13:03 -0500629 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200630 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 $CI =& get_instance();
Andrey Andreeved4b2582012-10-27 17:46:52 +0300632 return $CI->config->load($file, $use_sections, $fail_gracefully);
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 }
634
635 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300638 * Driver Loader
Derek Jones8dca0412010-03-05 13:01:44 -0600639 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300640 * Loads a driver library.
Derek Jones8dca0412010-03-05 13:01:44 -0600641 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300642 * @param string|string[] $library Driver name(s)
643 * @param array $params Optional parameters to pass to the driver
644 * @param string $object_name An optional object name to assign to
645 *
646 * @return void|object|bool Object or FALSE on failure if $library is a string
647 * and $object_name is set. void otherwise.
Derek Jones8dca0412010-03-05 13:01:44 -0600648 */
Greg Akerf5c84022011-04-19 17:13:03 -0500649 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600650 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800651 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800652 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800653 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800654 {
655 $this->driver($driver);
656 }
dchill420fc3be52012-08-27 20:54:23 -0400657 return;
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800658 }
659
Alex Bilbieed944a32012-06-02 11:07:47 +0100660 if ($library === '')
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200661 {
662 return FALSE;
663 }
664
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600665 // We can save the loader some time since Drivers will *always* be in a subfolder,
666 // and typically identically named to the library
667 if ( ! strpos($library, '/'))
668 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500669 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Jones8dca0412010-03-05 13:01:44 -0600672 return $this->library($library, $params, $object_name);
673 }
674
675 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Jones8dca0412010-03-05 13:01:44 -0600677 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600678 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300680 * Prepends a parent path to the library, model, helper and config
681 * path arrays.
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300683 * @see CI_Loader::$_ci_library_paths
684 * @see CI_Loader::$_ci_model_paths
685 * @see CI_Loader::$_ci_helper_paths
686 * @see CI_Config::$_config_paths
687 *
688 * @param string $path Path to add
689 * @param bool $view_cascade (default: TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600691 */
Andrey Andreevcce91802012-06-12 13:25:31 +0300692 public function add_package_path($path, $view_cascade = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600693 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500694 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200695
Derek Jones32bf1862010-03-02 13:46:07 -0600696 array_unshift($this->_ci_library_paths, $path);
697 array_unshift($this->_ci_model_paths, $path);
698 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200699
Greg Akerf5c84022011-04-19 17:13:03 -0500700 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
701
Derek Jones32bf1862010-03-02 13:46:07 -0600702 // Add config file path
703 $config =& $this->_ci_get_component('config');
Korri3684d342012-04-17 00:35:08 -0400704 array_push($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 }
706
707 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600708
709 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000710 * Get Package Paths
711 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300712 * Return a list of all package paths.
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000713 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300714 * @param bool $include_base Whether to include BASEPATH (default: TRUE)
715 * @return array
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000716 */
Greg Akerf5c84022011-04-19 17:13:03 -0500717 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000718 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300719 return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000720 }
721
722 // --------------------------------------------------------------------
723
724 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600725 * Remove Package Path
726 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300727 * Remove a path from the library, model, helper and/or config
728 * path arrays if it exists. If no path is provided, the most recently
729 * added path will be removed removed.
Derek Jones32bf1862010-03-02 13:46:07 -0600730 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300731 * @param string $path Path to remove
Andrey Andreev94af3552012-03-26 23:10:42 +0300732 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600733 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300734 public function remove_package_path($path = '')
Derek Jones32bf1862010-03-02 13:46:07 -0600735 {
736 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200737
Alex Bilbieed944a32012-06-02 11:07:47 +0100738 if ($path === '')
Derek Jones32bf1862010-03-02 13:46:07 -0600739 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200740 array_shift($this->_ci_library_paths);
741 array_shift($this->_ci_model_paths);
742 array_shift($this->_ci_helper_paths);
743 array_shift($this->_ci_view_paths);
Korri3684d342012-04-17 00:35:08 -0400744 array_pop($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600745 }
746 else
747 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500748 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600749 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
750 {
751 if (($key = array_search($path, $this->{$var})) !== FALSE)
752 {
753 unset($this->{$var}[$key]);
754 }
755 }
David Behlercda768a2011-08-14 23:52:48 +0200756
Greg Akerf5c84022011-04-19 17:13:03 -0500757 if (isset($this->_ci_view_paths[$path.'views/']))
758 {
759 unset($this->_ci_view_paths[$path.'views/']);
760 }
Barry Mienydd671972010-10-04 16:33:58 +0200761
Derek Jones32bf1862010-03-02 13:46:07 -0600762 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
763 {
764 unset($config->_config_paths[$key]);
765 }
766 }
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Jones32bf1862010-03-02 13:46:07 -0600768 // make sure the application default paths are still in the array
769 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
770 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
771 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500772 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600773 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
774 }
775
776 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200777
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300779 * Internal CI Data Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300781 * Used to load views and files.
782 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 * Variables are prefixed with _ci_ to avoid symbol collision with
Andrey Andreeved4b2582012-10-27 17:46:52 +0300784 * variables made available to view files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300786 * @used-by CI_Loader::view()
787 * @used-by CI_Loader::file()
788 * @param array $_ci_data Data to load
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 * @return void
790 */
Greg Akerf5c84022011-04-19 17:13:03 -0500791 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 {
793 // Set the default data variables
794 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
795 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300796 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 }
David Behlercda768a2011-08-14 23:52:48 +0200798
Greg Akerf5c84022011-04-19 17:13:03 -0500799 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000800
801 // Set the path to the requested file
Alex Bilbie40bd2a72012-06-02 16:04:15 +0100802 if (is_string($_ci_path) && $_ci_path !== '')
Greg Aker8807be32011-04-21 13:06:15 -0500803 {
804 $_ci_x = explode('/', $_ci_path);
805 $_ci_file = end($_ci_x);
806 }
807 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 {
809 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Alex Bilbieed944a32012-06-02 11:07:47 +0100810 $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500811
Joe McFrederick64f470b2012-08-18 12:29:56 -0400812 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
Greg Akerf5c84022011-04-19 17:13:03 -0500813 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400814 if (file_exists($_ci_view_file.$_ci_file))
Greg Akerf5c84022011-04-19 17:13:03 -0500815 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400816 $_ci_path = $_ci_view_file.$_ci_file;
Greg Akerf5c84022011-04-19 17:13:03 -0500817 $file_exists = TRUE;
818 break;
819 }
David Behlercda768a2011-08-14 23:52:48 +0200820
Greg Akerf5c84022011-04-19 17:13:03 -0500821 if ( ! $cascade)
822 {
823 break;
David Behlercda768a2011-08-14 23:52:48 +0200824 }
Greg Akerf5c84022011-04-19 17:13:03 -0500825 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 }
Barry Mienydd671972010-10-04 16:33:58 +0200827
Greg Akerf5c84022011-04-19 17:13:03 -0500828 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 {
830 show_error('Unable to load the requested file: '.$_ci_file);
831 }
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 // This allows anything loaded using $this->load (views, files, etc.)
834 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500835 $_ci_CI =& get_instance();
836 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500838 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500840 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 }
842 }
843
844 /*
845 * Extract and cache variables
846 *
vlakoff02506182012-07-03 07:28:50 +0200847 * You can either set variables using the dedicated $this->load->vars()
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 * function or via the second parameter of this function. We'll merge
849 * the two types and cache them so that views that are embedded within
850 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200851 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 if (is_array($_ci_vars))
853 {
854 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
855 }
856 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200857
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 /*
859 * Buffer the output
860 *
861 * We buffer the output for two reasons:
862 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200863 * 2. So that the final rendered template can be post-processed by
dchill425628ba02012-08-08 12:05:45 -0400864 * the output class. Why do we need post processing? For one thing,
865 * in order to show the elapsed page load time. Unless we can
866 * intercept the content right before it's sent to the browser and
867 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 */
869 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200870
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 // If the PHP installation does not support short tags we'll
872 // do a little string replacement, changing the short tags
873 // to standard PHP echo statements.
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200874 if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE
875 && config_item('rewrite_short_tags') === TRUE && function_usable('eval')
876 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200878 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 }
880 else
881 {
882 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
883 }
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200886
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 // Return the file data if requested
888 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200889 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 $buffer = ob_get_contents();
891 @ob_end_clean();
892 return $buffer;
893 }
894
895 /*
896 * Flush the buffer... or buff the flusher?
897 *
898 * In order to permit views to be nested within
899 * other views, we need to flush the content back out whenever
900 * we are beyond the first level of output buffering so that
901 * it can be seen and included properly by the first included
902 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200903 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 if (ob_get_level() > $this->_ci_ob_level + 1)
905 {
906 ob_end_flush();
907 }
908 else
909 {
Greg Aker22f1a632010-11-10 15:34:35 -0600910 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 @ob_end_clean();
912 }
913 }
914
915 // --------------------------------------------------------------------
916
917 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300918 * Internal CI Class Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300920 * @used-by CI_Loader::library()
921 * @uses CI_Loader::_ci_init_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300923 * @param string $class Class name to load
924 * @param mixed $params Optional parameters to pass to the class constructor
925 * @param string $object_name Optional object name to assign to
Barry Mienydd671972010-10-04 16:33:58 +0200926 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 */
Greg Akerf5c84022011-04-19 17:13:03 -0500928 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200929 {
930 // Get the class name, and while we're at it trim any slashes.
931 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500933 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200934
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 // Was the path included with the class name?
936 // We look for a slash to determine this
937 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600938 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
Derek Jones32bf1862010-03-02 13:46:07 -0600940 // Extract the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200941 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200942
Derek Jones32bf1862010-03-02 13:46:07 -0600943 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200944 $class = substr($class, $last_slash);
dchill425628ba02012-08-08 12:05:45 -0400945
946 // Check for match and driver base class
dchill42aee92652012-08-26 21:45:35 -0400947 if (strtolower(trim($subdir, '/')) == strtolower($class) && ! class_exists('CI_Driver_Library'))
dchill425628ba02012-08-08 12:05:45 -0400948 {
949 // We aren't instantiating an object here, just making the base class available
950 require BASEPATH.'libraries/Driver.php';
951 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 }
953
954 // We'll test for both lowercase and capitalized versions of the file name
955 foreach (array(ucfirst($class), strtolower($class)) as $class)
956 {
Greg Aker3a746652011-04-19 10:59:47 -0500957 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000958
Barry Mienydd671972010-10-04 16:33:58 +0200959 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 if (file_exists($subclass))
961 {
Greg Aker3a746652011-04-19 10:59:47 -0500962 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200963
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 if ( ! file_exists($baseclass))
965 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200966 log_message('error', 'Unable to load the requested class: '.$class);
967 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 }
969
Andrey Andreevd7297352012-01-07 22:53:14 +0200970 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 if (in_array($subclass, $this->_ci_loaded_files))
972 {
973 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200974 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 // return a new instance of the object
976 if ( ! is_null($object_name))
977 {
978 $CI =& get_instance();
979 if ( ! isset($CI->$object_name))
980 {
Barry Mienydd671972010-10-04 16:33:58 +0200981 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 }
983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200986 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 return;
988 }
Barry Mienydd671972010-10-04 16:33:58 +0200989
990 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 include_once($subclass);
992 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200993
994 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 }
Barry Mienydd671972010-10-04 16:33:58 +0200996
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600998 $is_duplicate = FALSE;
999 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
Greg Aker3a746652011-04-19 10:59:47 -05001001 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -06001002
Andrey Andreevd7297352012-01-07 22:53:14 +02001003 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 if ( ! file_exists($filepath))
1005 {
1006 continue;
1007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Andrey Andreevd7297352012-01-07 22:53:14 +02001009 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 if (in_array($filepath, $this->_ci_loaded_files))
1011 {
1012 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +02001013 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 // return a new instance of the object
1015 if ( ! is_null($object_name))
1016 {
1017 $CI =& get_instance();
1018 if ( ! isset($CI->$object_name))
1019 {
1020 return $this->_ci_init_class($class, '', $params, $object_name);
1021 }
1022 }
Barry Mienydd671972010-10-04 16:33:58 +02001023
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +02001025 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 return;
1027 }
Barry Mienydd671972010-10-04 16:33:58 +02001028
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 include_once($filepath);
1030 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +02001031 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 }
1033 } // END FOREACH
1034
Andrey Andreevd7297352012-01-07 22:53:14 +02001035 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Alex Bilbieed944a32012-06-02 11:07:47 +01001036 if ($subdir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 {
1038 $path = strtolower($class).'/'.$class;
dchill420fc3be52012-08-27 20:54:23 -04001039 return $this->_ci_load_class($path, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 }
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001041 elseif (ucfirst($subdir) != $subdir)
dchill42aee92652012-08-26 21:45:35 -04001042 {
1043 // Lowercase subdir failed - retry capitalized
1044 $path = ucfirst($subdir).$class;
dchill420fc3be52012-08-27 20:54:23 -04001045 return $this->_ci_load_class($path, $params, $object_name);
dchill42aee92652012-08-26 21:45:35 -04001046 }
Barry Mienydd671972010-10-04 16:33:58 +02001047
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 // If we got this far we were unable to find the requested class.
1049 // We do not issue errors if the load call failed due to a duplicate request
Alex Bilbieed944a32012-06-02 11:07:47 +01001050 if ($is_duplicate === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001052 log_message('error', 'Unable to load the requested class: '.$class);
1053 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 }
1055 }
Barry Mienydd671972010-10-04 16:33:58 +02001056
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 // --------------------------------------------------------------------
1058
1059 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001060 * Internal CI Class Instantiator
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001062 * @used-by CI_Loader::_ci_load_class()
1063 *
1064 * @param string $class Class name
1065 * @param string $prefix Class name prefix
1066 * @param array|null|bool $config Optional configuration to pass to the class constructor:
1067 * FALSE to skip;
1068 * NULL to search in config paths;
1069 * array containing configuration data
1070 * @param string $object_name Optional object name to assign to
Andrey Andreev94af3552012-03-26 23:10:42 +03001071 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001073 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001074 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001075 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 if ($config === NULL)
1077 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001078 // Fetch the config paths containing any package paths
1079 $config_component = $this->_ci_get_component('config');
1080
1081 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001083 // Break on the first found file, thus package files
1084 // are not overridden by default paths
1085 foreach ($config_component->_config_paths as $path)
1086 {
1087 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001088 // are case-sensitive with regard to file names. Check for environment
1089 // first, global next
Andrey Andreev94af3552012-03-26 23:10:42 +03001090 if (defined('ENVIRONMENT') && file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001091 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001092 include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001093 break;
1094 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001095 elseif (defined('ENVIRONMENT') && file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001096 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001097 include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001098 break;
1099 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001100 elseif (file_exists($path.'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001101 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001102 include($path.'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001103 break;
1104 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001105 elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001106 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001107 include($path.'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001108 break;
1109 }
1110 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 }
1112 }
Barry Mienydd671972010-10-04 16:33:58 +02001113
Alex Bilbieed944a32012-06-02 11:07:47 +01001114 if ($prefix === '')
Barry Mienydd671972010-10-04 16:33:58 +02001115 {
1116 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 {
1118 $name = 'CI_'.$class;
1119 }
Barry Mienydd671972010-10-04 16:33:58 +02001120 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 {
1122 $name = config_item('subclass_prefix').$class;
1123 }
1124 else
1125 {
1126 $name = $class;
1127 }
1128 }
1129 else
1130 {
1131 $name = $prefix.$class;
1132 }
Barry Mienydd671972010-10-04 16:33:58 +02001133
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 // Is the class name valid?
1135 if ( ! class_exists($name))
1136 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001137 log_message('error', 'Non-existent class: '.$name);
jonnueee2df62012-07-16 13:06:16 +01001138 show_error('Non-existent class: '.$name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 }
Barry Mienydd671972010-10-04 16:33:58 +02001140
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001142 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 if (is_null($object_name))
1146 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001147 $classvar = isset($this->_ci_varmap[$class]) ? $this->_ci_varmap[$class] : $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 }
1149 else
1150 {
1151 $classvar = $object_name;
1152 }
1153
Barry Mienydd671972010-10-04 16:33:58 +02001154 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 $this->_ci_classes[$class] = $classvar;
1156
Barry Mienydd671972010-10-04 16:33:58 +02001157 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 $CI =& get_instance();
1159 if ($config !== NULL)
1160 {
1161 $CI->$classvar = new $name($config);
1162 }
1163 else
Barry Mienydd671972010-10-04 16:33:58 +02001164 {
Andrey Andreeva11b16b2012-03-28 12:22:04 +03001165 $CI->$classvar = new $name();
Barry Mienydd671972010-10-04 16:33:58 +02001166 }
1167 }
1168
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001170
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001172 * CI Autoloader
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001174 * Loads component listed in the config/autoload.php file.
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 *
Andrey Andreevcdac2482012-11-03 18:09:01 +02001176 * @used-by CI_Loader::initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 * @return void
1178 */
Shane Pearson665baec2011-08-22 18:52:19 -05001179 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001180 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001181 if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001182 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001183 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001184 }
1185 else
1186 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001187 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001188 }
Barry Mienydd671972010-10-04 16:33:58 +02001189
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 if ( ! isset($autoload))
1191 {
1192 return FALSE;
1193 }
Barry Mienydd671972010-10-04 16:33:58 +02001194
Phil Sturgeon9730c752010-12-15 10:50:15 +00001195 // Autoload packages
1196 if (isset($autoload['packages']))
1197 {
1198 foreach ($autoload['packages'] as $package_path)
1199 {
1200 $this->add_package_path($package_path);
1201 }
1202 }
1203
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 // Load any custom config file
1205 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001206 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 $CI =& get_instance();
1208 foreach ($autoload['config'] as $key => $val)
1209 {
1210 $CI->config->load($val);
1211 }
Barry Mienydd671972010-10-04 16:33:58 +02001212 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001213
Derek Jonesc6da5032010-03-09 20:44:27 -06001214 // Autoload helpers and languages
1215 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001216 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001217 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 {
1219 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001220 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 }
1222
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001224 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 {
1226 // Load the database driver.
1227 if (in_array('database', $autoload['libraries']))
1228 {
1229 $this->database();
1230 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1231 }
Barry Mienydd671972010-10-04 16:33:58 +02001232
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 // Load all other libraries
1234 foreach ($autoload['libraries'] as $item)
1235 {
1236 $this->library($item);
1237 }
Barry Mienydd671972010-10-04 16:33:58 +02001238 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001239
Darren Hillc4e266b2011-08-30 15:40:27 -04001240 // Autoload drivers
1241 if (isset($autoload['drivers']))
1242 {
Darren Hillca3be1d2011-08-31 08:31:18 -04001243 foreach ($autoload['drivers'] as $item)
1244 {
1245 $this->driver($item);
1246 }
Darren Hillc4e266b2011-08-30 15:40:27 -04001247 }
1248
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 // Autoload models
1250 if (isset($autoload['model']))
1251 {
1252 $this->model($autoload['model']);
1253 }
Barry Mienydd671972010-10-04 16:33:58 +02001254 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001255
1256 // --------------------------------------------------------------------
1257
1258 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001259 * CI Object to Array translator
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001261 * Takes an object as input and converts the class variables to
1262 * an associative array with key/value pairs.
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001264 * @param object $object Object data to translate
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 * @return array
1266 */
Greg Akerf5c84022011-04-19 17:13:03 -05001267 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001269 return is_object($object) ? get_object_vars($object) : $object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 }
1271
1272 // --------------------------------------------------------------------
1273
1274 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001275 * CI Component getter
Derek Jones32bf1862010-03-02 13:46:07 -06001276 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001277 * Get a reference to a specific library or model.
1278 *
1279 * @param string $component Component name
Derek Jones32bf1862010-03-02 13:46:07 -06001280 * @return bool
1281 */
Greg Akerf5c84022011-04-19 17:13:03 -05001282 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001283 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001284 $CI =& get_instance();
1285 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001286 }
1287
1288 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001289
Derek Jones32bf1862010-03-02 13:46:07 -06001290 /**
1291 * Prep filename
1292 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001293 * This function prepares filenames of various items to
1294 * make their loading more reliable.
Derek Jones32bf1862010-03-02 13:46:07 -06001295 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001296 * @param string|string[] $filename Filename(s)
1297 * @param string $extension Filename extension
Derek Jones32bf1862010-03-02 13:46:07 -06001298 * @return array
1299 */
Greg Akerf5c84022011-04-19 17:13:03 -05001300 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001301 {
1302 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001303 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001304 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001305 }
1306 else
1307 {
1308 foreach ($filename as $key => $val)
1309 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001310 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001311 }
Barry Mienydd671972010-10-04 16:33:58 +02001312
Derek Jones32bf1862010-03-02 13:46:07 -06001313 return $filename;
1314 }
1315 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001316
Derek Allard2067d1a2008-11-13 22:59:24 +00001317}
1318
1319/* End of file Loader.php */
Andrey Andreev9438e262012-10-05 13:16:27 +03001320/* Location: ./system/core/Loader.php */