blob: db56ab3ae57b01e475df247072f7930d73e84dc3 [file] [log] [blame]
Andrey Andreevd7297352012-01-07 22:53:14 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Loader Class
30 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030031 * Loads framework components.
Derek Allard2067d1a2008-11-13 22:59:24 +000032 *
33 * @package CodeIgniter
34 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @category Loader
Andrey Andreev92ebfb62012-05-17 12:49:24 +030036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/loader.html
38 */
39class CI_Loader {
40
41 // All these are set automatically. Don't mess with them.
David Behlercda768a2011-08-14 23:52:48 +020042 /**
43 * Nesting level of the output buffering mechanism
44 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030045 * @var int
David Behlercda768a2011-08-14 23:52:48 +020046 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050047 protected $_ci_ob_level;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030048
David Behlercda768a2011-08-14 23:52:48 +020049 /**
50 * List of paths to load views from
51 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030052 * @var array
David Behlercda768a2011-08-14 23:52:48 +020053 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040054 protected $_ci_view_paths = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030055
David Behlercda768a2011-08-14 23:52:48 +020056 /**
57 * List of paths to load libraries from
58 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030059 * @var array
David Behlercda768a2011-08-14 23:52:48 +020060 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040061 protected $_ci_library_paths = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030062
David Behlercda768a2011-08-14 23:52:48 +020063 /**
64 * List of paths to load models from
65 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030066 * @var array
David Behlercda768a2011-08-14 23:52:48 +020067 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040068 protected $_ci_model_paths = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030069
David Behlercda768a2011-08-14 23:52:48 +020070 /**
71 * List of paths to load helpers from
72 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030073 * @var array
David Behlercda768a2011-08-14 23:52:48 +020074 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040075 protected $_ci_helper_paths = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030076
David Behlercda768a2011-08-14 23:52:48 +020077 /**
78 * List of loaded base classes
David Behlercda768a2011-08-14 23:52:48 +020079 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030080 * @var array
David Behlercda768a2011-08-14 23:52:48 +020081 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040082 protected $_base_classes = array(); // Set by the controller class
Andrey Andreev92ebfb62012-05-17 12:49:24 +030083
David Behlercda768a2011-08-14 23:52:48 +020084 /**
85 * List of cached variables
86 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030087 * @var array
David Behlercda768a2011-08-14 23:52:48 +020088 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040089 protected $_ci_cached_vars = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030090
David Behlercda768a2011-08-14 23:52:48 +020091 /**
92 * List of loaded classes
93 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030094 * @var array
David Behlercda768a2011-08-14 23:52:48 +020095 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040096 protected $_ci_classes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030097
David Behlercda768a2011-08-14 23:52:48 +020098 /**
99 * List of loaded files
100 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300101 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200102 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400103 protected $_ci_loaded_files = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300104
David Behlercda768a2011-08-14 23:52:48 +0200105 /**
106 * List of loaded models
107 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300108 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200109 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400110 protected $_ci_models = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300111
David Behlercda768a2011-08-14 23:52:48 +0200112 /**
113 * List of loaded helpers
114 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300115 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200116 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400117 protected $_ci_helpers = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300118
David Behlercda768a2011-08-14 23:52:48 +0200119 /**
120 * List of class name mappings
121 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300122 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200123 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400124 protected $_ci_varmap = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400125 'unit_test' => 'unit',
126 'user_agent' => 'agent'
127 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000128
129 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300130 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300132 * Sets component load paths gets the initial output buffering level.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300133 *
134 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 */
Greg Akerf5c84022011-04-19 17:13:03 -0500136 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +0200137 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500138 $this->_ci_ob_level = ob_get_level();
Derek Jones32bf1862010-03-02 13:46:07 -0600139 $this->_ci_library_paths = array(APPPATH, BASEPATH);
140 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
141 $this->_ci_model_paths = array(APPPATH);
Joe Cianflone8eef9c72011-08-21 10:39:06 -0400142 $this->_ci_view_paths = array(VIEWPATH => TRUE);
David Behlercda768a2011-08-14 23:52:48 +0200143
Andrey Andreevd7297352012-01-07 22:53:14 +0200144 log_message('debug', 'Loader Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200148
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500149 /**
Shane Pearson6adfe632011-08-10 16:42:53 -0500150 * Initialize the Loader
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500151 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300152 * @used-by CI_Controller
153 * @uses CI_Loader::_ci_autoloader()
154 * @return object $this
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500155 */
Shane Pearson6adfe632011-08-10 16:42:53 -0500156 public function initialize()
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500157 {
Shane Pearson6adfe632011-08-10 16:42:53 -0500158 $this->_ci_classes = array();
159 $this->_ci_loaded_files = array();
160 $this->_ci_models = array();
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500161 $this->_base_classes =& is_loaded();
Shane Pearson6adfe632011-08-10 16:42:53 -0500162
163 $this->_ci_autoloader();
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500164 return $this;
165 }
166
167 // --------------------------------------------------------------------
168
169 /**
170 * Is Loaded
171 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300172 * A utility method to test if a class is in the self::$_ci_classes array.
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500173 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300174 * @used-by Mainly used by Form Helper function _get_validation_object().
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500175 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300176 * @param string $class Class name to check for
177 * @return string|bool Class object name if loaded or FALSE
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500178 */
179 public function is_loaded($class)
180 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300181 return isset($this->_ci_classes[$class]) ? $this->_ci_classes[$class] : FALSE;
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500182 }
183
184 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300187 * Library Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300189 * Loads and instantiates libraries.
190 * Designed to be called from application controllers.
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300192 * @param string $library Library name
193 * @param array $params Optional parameters to pass to the library class constructor
194 * @param string $object_name An optional object name to assign to
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200196 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500197 public function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Greg Akerce433962010-10-12 09:29:35 -0500199 if (is_array($library))
200 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100201 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500202 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600203 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500204 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000205
Greg Akerce433962010-10-12 09:29:35 -0500206 return;
207 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000208
Alex Bilbieed944a32012-06-02 11:07:47 +0100209 if ($library === '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300211 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
213
Derek Jones32bf1862010-03-02 13:46:07 -0600214 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
216 $params = NULL;
217 }
218
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600219 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
221
222 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 /**
225 * Model Loader
226 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300227 * Loads and instantiates libraries.
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300229 * @param string $model Model name
230 * @param string $name An optional object name to assign to
231 * @param bool $db_conn An optional database connection configuration to initialize
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200233 */
Greg Akerf5c84022011-04-19 17:13:03 -0500234 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200235 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300236 if (empty($model))
237 {
238 return;
239 }
240 elseif (is_array($model))
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Joel Limberg3cf174b2012-07-13 20:49:57 +0300242 foreach ($model as $class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
Joel Limberg3cf174b2012-07-13 20:49:57 +0300244 $this->model($class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
246 return;
247 }
248
Derek Jones32bf1862010-03-02 13:46:07 -0600249 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600252 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
Derek Jones32bf1862010-03-02 13:46:07 -0600254 // The path is in front of the last slash
Andrey Andreevd47baab2012-01-09 16:56:46 +0200255 $path = substr($model, 0, ++$last_slash);
Derek Jones32bf1862010-03-02 13:46:07 -0600256
257 // And the model name behind it
Andrey Andreevd47baab2012-01-09 16:56:46 +0200258 $model = substr($model, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
Barry Mienydd671972010-10-04 16:33:58 +0200260
Phil Sturgeon10d78f62012-06-04 14:41:53 -0500261 if (empty($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
263 $name = $model;
264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 if (in_array($name, $this->_ci_models, TRUE))
267 {
268 return;
269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 $CI =& get_instance();
272 if (isset($CI->$name))
273 {
274 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
275 }
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000278
Derek Jones32bf1862010-03-02 13:46:07 -0600279 foreach ($this->_ci_model_paths as $mod_path)
280 {
Greg Aker3a746652011-04-19 10:59:47 -0500281 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600282 {
283 continue;
284 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000285
Andrey Andreev94af3552012-03-26 23:10:42 +0300286 if ($db_conn !== FALSE && ! class_exists('CI_DB'))
Derek Jones32bf1862010-03-02 13:46:07 -0600287 {
288 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500289 {
Derek Jones32bf1862010-03-02 13:46:07 -0600290 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500291 }
Derek Jones32bf1862010-03-02 13:46:07 -0600292
293 $CI->load->database($db_conn, FALSE, TRUE);
294 }
295
Greg Akerbce13482010-10-11 15:37:16 -0500296 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600297 {
298 load_class('Model', 'core');
299 }
300
Greg Aker3a746652011-04-19 10:59:47 -0500301 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600302
303 $model = ucfirst($model);
Derek Jones32bf1862010-03-02 13:46:07 -0600304 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600305 $this->_ci_models[] = $name;
306 return;
307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Jones32bf1862010-03-02 13:46:07 -0600309 // couldn't find the model
310 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 /**
316 * Database Loader
317 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300318 * @param mixed $params Database configuration options
319 * @param bool $return Whether to return the database object
320 * @param bool $query_builder Whether to enable Query Builder
321 * (overrides the configuration setting)
322 *
323 * @return void|object|bool Database object if $return is set to TRUE,
324 * FALSE on failure, void in any other case
Barry Mienydd671972010-10-04 16:33:58 +0200325 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000326 public function database($params = '', $return = FALSE, $query_builder = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
328 // Grab the super object
329 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // Do we even need to load the database class?
Andrey Andreev9d0ab042012-10-24 21:47:39 +0300332 if ($return === FALSE && $query_builder === NULL && isset($CI->db) && is_object($CI->db) && ! empty($CI->db->conn_id))
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200335 }
336
Greg Aker3a746652011-04-19 10:59:47 -0500337 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000338
339 if ($return === TRUE)
340 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000341 return DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Andrey Andreevd7297352012-01-07 22:53:14 +0200344 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 // reference errors with some configurations
346 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // Load the DB class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000349 $CI->db =& DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 // --------------------------------------------------------------------
353
354 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300355 * Load the Database Utilities Class
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300357 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200358 */
Greg Akerf5c84022011-04-19 17:13:03 -0500359 public function dbutil()
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
361 if ( ! class_exists('CI_DB'))
362 {
363 $this->database();
364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 $CI =& get_instance();
367
368 // for backwards compatibility, load dbforge so we can extend dbutils off it
369 // this use is deprecated and strongly discouraged
370 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200371
Greg Aker3a746652011-04-19 10:59:47 -0500372 require_once(BASEPATH.'database/DB_utility.php');
373 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
375
Pascal Kriete58560022010-11-10 16:01:20 -0500376 $CI->dbutil = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // --------------------------------------------------------------------
380
381 /**
382 * Load the Database Forge Class
383 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300384 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200385 */
Greg Akerf5c84022011-04-19 17:13:03 -0500386 public function dbforge()
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
388 if ( ! class_exists('CI_DB'))
389 {
390 $this->database();
391 }
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200394
Greg Aker3a746652011-04-19 10:59:47 -0500395 require_once(BASEPATH.'database/DB_forge.php');
396 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
398
399 $CI->dbforge = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300405 * View Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300407 * Loads "view" files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300409 * @param string $view View name
410 * @param array $vars An associative array of data
411 * to be extracted for use in the view
412 * @param bool $return Whether to return the view output
413 * or leave it to the Output class
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 * @return void
415 */
Greg Akerf5c84022011-04-19 17:13:03 -0500416 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
418 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300424 * Generic File Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300426 * @param string $path File path
427 * @param bool $return Whether to return the file output
428 * @return void|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 */
Greg Akerf5c84022011-04-19 17:13:03 -0500430 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
432 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 /**
438 * Set Variables
439 *
440 * Once variables are set they become available within
441 * the controller class and its "view" files.
442 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300443 * @param array|object|string $vars
444 * An associative array or object containing values
445 * to be set, or a value's name if string
446 * @param string $val Value to set, only used if $vars is a string
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * @return void
448 */
Greg Akerf5c84022011-04-19 17:13:03 -0500449 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100451 if ($val !== '' && is_string($vars))
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
453 $vars = array($vars => $val);
454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200457
Andrey Andreev94af3552012-03-26 23:10:42 +0300458 if (is_array($vars) && count($vars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
460 foreach ($vars as $key => $val)
461 {
462 $this->_ci_cached_vars[$key] = $val;
463 }
464 }
465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600470 * Get Variable
471 *
472 * Check if a variable is set and retrieve it.
473 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300474 * @param string $key Variable name
475 * @return mixed The variable or NULL if not found
Phil Sturgeon8731f642011-07-22 16:11:34 -0600476 */
477 public function get_var($key)
478 {
479 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
480 }
481
482 // --------------------------------------------------------------------
483
484 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600485 * Get Variables
486 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300487 * Retrieves all loaded variables.
Shane Pearson81dd2232011-11-18 20:49:35 -0600488 *
489 * @return array
490 */
491 public function get_vars()
492 {
493 return $this->_ci_cached_vars;
494 }
495
496 // --------------------------------------------------------------------
497
498 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300499 * Helper Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300501 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 * @return void
503 */
Greg Akerf5c84022011-04-19 17:13:03 -0500504 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200505 {
Derek Jones32bf1862010-03-02 13:46:07 -0600506 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200507 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 if (isset($this->_ci_helpers[$helper]))
509 {
510 continue;
511 }
Derek Jones32bf1862010-03-02 13:46:07 -0600512
Greg Aker3a746652011-04-19 10:59:47 -0500513 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000514
Barry Mienydd671972010-10-04 16:33:58 +0200515 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 if (file_exists($ext_helper))
517 {
Greg Aker3a746652011-04-19 10:59:47 -0500518 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 if ( ! file_exists($base_helper))
521 {
Greg Aker3a746652011-04-19 10:59:47 -0500522 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 }
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 include_once($ext_helper);
526 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Jones32bf1862010-03-02 13:46:07 -0600528 $this->_ci_helpers[$helper] = TRUE;
529 log_message('debug', 'Helper loaded: '.$helper);
530 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Jones32bf1862010-03-02 13:46:07 -0600533 // Try to load the helper
534 foreach ($this->_ci_helper_paths as $path)
535 {
Greg Aker3a746652011-04-19 10:59:47 -0500536 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200537 {
Greg Aker3a746652011-04-19 10:59:47 -0500538 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600539
540 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200541 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600542 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 }
544 }
545
Derek Jones32bf1862010-03-02 13:46:07 -0600546 // unable to load the helper
547 if ( ! isset($this->_ci_helpers[$helper]))
548 {
Greg Aker3a746652011-04-19 10:59:47 -0500549 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 /**
557 * Load Helpers
558 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300559 * An alias for the helper() method in case the developer has
560 * written the plural form of it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300562 * @uses CI_Loader::helper()
563 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 * @return void
565 */
Greg Akerf5c84022011-04-19 17:13:03 -0500566 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
568 $this->helper($helpers);
569 }
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300574 * Language Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300576 * Loads language files.
577 *
578 * @param string|string[] $files List of language file names to load
579 * @param string Language name
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 * @return void
581 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300582 public function language($files = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 {
584 $CI =& get_instance();
585
Andrey Andreeved4b2582012-10-27 17:46:52 +0300586 is_array($files) OR $files = array($files);
Derek Allard2067d1a2008-11-13 22:59:24 +0000587
Andrey Andreeved4b2582012-10-27 17:46:52 +0300588 foreach ($files as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200589 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 $CI->lang->load($langfile, $lang);
591 }
592 }
Barry Mienydd671972010-10-04 16:33:58 +0200593
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300597 * Config Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300599 * Loads a config file (an alias for CI_Config::load()).
600 *
601 * @uses CI_Config::load()
602 * @param string $file Configuration file name
603 * @param bool $use_sections Whether configuration values should be loaded into their own section
604 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
605 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 */
Greg Akerf5c84022011-04-19 17:13:03 -0500607 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200608 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 $CI =& get_instance();
Andrey Andreeved4b2582012-10-27 17:46:52 +0300610 return $CI->config->load($file, $use_sections, $fail_gracefully);
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 }
612
613 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300616 * Driver Loader
Derek Jones8dca0412010-03-05 13:01:44 -0600617 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300618 * Loads a driver library.
Derek Jones8dca0412010-03-05 13:01:44 -0600619 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300620 * @param string|string[] $library Driver name(s)
621 * @param array $params Optional parameters to pass to the driver
622 * @param string $object_name An optional object name to assign to
623 *
624 * @return void|object|bool Object or FALSE on failure if $library is a string
625 * and $object_name is set. void otherwise.
Derek Jones8dca0412010-03-05 13:01:44 -0600626 */
Greg Akerf5c84022011-04-19 17:13:03 -0500627 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600628 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800629 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800630 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800631 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800632 {
633 $this->driver($driver);
634 }
dchill420fc3be52012-08-27 20:54:23 -0400635 return;
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800636 }
637
Alex Bilbieed944a32012-06-02 11:07:47 +0100638 if ($library === '')
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200639 {
640 return FALSE;
641 }
642
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600643 // We can save the loader some time since Drivers will *always* be in a subfolder,
644 // and typically identically named to the library
645 if ( ! strpos($library, '/'))
646 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500647 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600648 }
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Jones8dca0412010-03-05 13:01:44 -0600650 return $this->library($library, $params, $object_name);
651 }
652
653 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200654
Derek Jones8dca0412010-03-05 13:01:44 -0600655 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600656 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300658 * Prepends a parent path to the library, model, helper and config
659 * path arrays.
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300661 * @see CI_Loader::$_ci_library_paths
662 * @see CI_Loader::$_ci_model_paths
663 * @see CI_Loader::$_ci_helper_paths
664 * @see CI_Config::$_config_paths
665 *
666 * @param string $path Path to add
667 * @param bool $view_cascade (default: TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600669 */
Andrey Andreevcce91802012-06-12 13:25:31 +0300670 public function add_package_path($path, $view_cascade = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600671 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500672 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200673
Derek Jones32bf1862010-03-02 13:46:07 -0600674 array_unshift($this->_ci_library_paths, $path);
675 array_unshift($this->_ci_model_paths, $path);
676 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200677
Greg Akerf5c84022011-04-19 17:13:03 -0500678 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
679
Derek Jones32bf1862010-03-02 13:46:07 -0600680 // Add config file path
681 $config =& $this->_ci_get_component('config');
Korri3684d342012-04-17 00:35:08 -0400682 array_push($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 }
684
685 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600686
687 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000688 * Get Package Paths
689 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300690 * Return a list of all package paths.
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000691 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300692 * @param bool $include_base Whether to include BASEPATH (default: TRUE)
693 * @return array
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000694 */
Greg Akerf5c84022011-04-19 17:13:03 -0500695 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000696 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300697 return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000698 }
699
700 // --------------------------------------------------------------------
701
702 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600703 * Remove Package Path
704 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300705 * Remove a path from the library, model, helper and/or config
706 * path arrays if it exists. If no path is provided, the most recently
707 * added path will be removed removed.
Derek Jones32bf1862010-03-02 13:46:07 -0600708 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300709 * @param string $path Path to remove
Andrey Andreev94af3552012-03-26 23:10:42 +0300710 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600711 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300712 public function remove_package_path($path = '')
Derek Jones32bf1862010-03-02 13:46:07 -0600713 {
714 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200715
Alex Bilbieed944a32012-06-02 11:07:47 +0100716 if ($path === '')
Derek Jones32bf1862010-03-02 13:46:07 -0600717 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200718 array_shift($this->_ci_library_paths);
719 array_shift($this->_ci_model_paths);
720 array_shift($this->_ci_helper_paths);
721 array_shift($this->_ci_view_paths);
Korri3684d342012-04-17 00:35:08 -0400722 array_pop($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600723 }
724 else
725 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500726 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600727 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
728 {
729 if (($key = array_search($path, $this->{$var})) !== FALSE)
730 {
731 unset($this->{$var}[$key]);
732 }
733 }
David Behlercda768a2011-08-14 23:52:48 +0200734
Greg Akerf5c84022011-04-19 17:13:03 -0500735 if (isset($this->_ci_view_paths[$path.'views/']))
736 {
737 unset($this->_ci_view_paths[$path.'views/']);
738 }
Barry Mienydd671972010-10-04 16:33:58 +0200739
Derek Jones32bf1862010-03-02 13:46:07 -0600740 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
741 {
742 unset($config->_config_paths[$key]);
743 }
744 }
Barry Mienydd671972010-10-04 16:33:58 +0200745
Derek Jones32bf1862010-03-02 13:46:07 -0600746 // make sure the application default paths are still in the array
747 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
748 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
749 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500750 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600751 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
752 }
753
754 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200755
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300757 * Internal CI Data Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300759 * Used to load views and files.
760 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 * Variables are prefixed with _ci_ to avoid symbol collision with
Andrey Andreeved4b2582012-10-27 17:46:52 +0300762 * variables made available to view files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300764 * @used-by CI_Loader::view()
765 * @used-by CI_Loader::file()
766 * @param array $_ci_data Data to load
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 * @return void
768 */
Greg Akerf5c84022011-04-19 17:13:03 -0500769 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 {
771 // Set the default data variables
772 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
773 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300774 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 }
David Behlercda768a2011-08-14 23:52:48 +0200776
Greg Akerf5c84022011-04-19 17:13:03 -0500777 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000778
779 // Set the path to the requested file
Alex Bilbie40bd2a72012-06-02 16:04:15 +0100780 if (is_string($_ci_path) && $_ci_path !== '')
Greg Aker8807be32011-04-21 13:06:15 -0500781 {
782 $_ci_x = explode('/', $_ci_path);
783 $_ci_file = end($_ci_x);
784 }
785 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 {
787 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Alex Bilbieed944a32012-06-02 11:07:47 +0100788 $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500789
Joe McFrederick64f470b2012-08-18 12:29:56 -0400790 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
Greg Akerf5c84022011-04-19 17:13:03 -0500791 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400792 if (file_exists($_ci_view_file.$_ci_file))
Greg Akerf5c84022011-04-19 17:13:03 -0500793 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400794 $_ci_path = $_ci_view_file.$_ci_file;
Greg Akerf5c84022011-04-19 17:13:03 -0500795 $file_exists = TRUE;
796 break;
797 }
David Behlercda768a2011-08-14 23:52:48 +0200798
Greg Akerf5c84022011-04-19 17:13:03 -0500799 if ( ! $cascade)
800 {
801 break;
David Behlercda768a2011-08-14 23:52:48 +0200802 }
Greg Akerf5c84022011-04-19 17:13:03 -0500803 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 }
Barry Mienydd671972010-10-04 16:33:58 +0200805
Greg Akerf5c84022011-04-19 17:13:03 -0500806 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 {
808 show_error('Unable to load the requested file: '.$_ci_file);
809 }
Barry Mienydd671972010-10-04 16:33:58 +0200810
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 // This allows anything loaded using $this->load (views, files, etc.)
812 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500813 $_ci_CI =& get_instance();
814 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500816 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500818 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 }
820 }
821
822 /*
823 * Extract and cache variables
824 *
vlakoff02506182012-07-03 07:28:50 +0200825 * You can either set variables using the dedicated $this->load->vars()
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 * function or via the second parameter of this function. We'll merge
827 * the two types and cache them so that views that are embedded within
828 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200829 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 if (is_array($_ci_vars))
831 {
832 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
833 }
834 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 /*
837 * Buffer the output
838 *
839 * We buffer the output for two reasons:
840 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200841 * 2. So that the final rendered template can be post-processed by
dchill425628ba02012-08-08 12:05:45 -0400842 * the output class. Why do we need post processing? For one thing,
843 * in order to show the elapsed page load time. Unless we can
844 * intercept the content right before it's sent to the browser and
845 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 */
847 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200848
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 // If the PHP installation does not support short tags we'll
850 // do a little string replacement, changing the short tags
851 // to standard PHP echo statements.
Alex Bilbieed944a32012-06-02 11:07:47 +0100852 if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE && config_item('rewrite_short_tags') === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200854 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 }
856 else
857 {
858 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 // Return the file data if requested
864 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200865 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 $buffer = ob_get_contents();
867 @ob_end_clean();
868 return $buffer;
869 }
870
871 /*
872 * Flush the buffer... or buff the flusher?
873 *
874 * In order to permit views to be nested within
875 * other views, we need to flush the content back out whenever
876 * we are beyond the first level of output buffering so that
877 * it can be seen and included properly by the first included
878 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200879 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 if (ob_get_level() > $this->_ci_ob_level + 1)
881 {
882 ob_end_flush();
883 }
884 else
885 {
Greg Aker22f1a632010-11-10 15:34:35 -0600886 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 @ob_end_clean();
888 }
889 }
890
891 // --------------------------------------------------------------------
892
893 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300894 * Internal CI Class Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300896 * @used-by CI_Loader::library()
897 * @uses CI_Loader::_ci_init_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300899 * @param string $class Class name to load
900 * @param mixed $params Optional parameters to pass to the class constructor
901 * @param string $object_name Optional object name to assign to
Barry Mienydd671972010-10-04 16:33:58 +0200902 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 */
Greg Akerf5c84022011-04-19 17:13:03 -0500904 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200905 {
906 // Get the class name, and while we're at it trim any slashes.
907 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500909 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 // Was the path included with the class name?
912 // We look for a slash to determine this
913 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600914 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
Derek Jones32bf1862010-03-02 13:46:07 -0600916 // Extract the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200917 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200918
Derek Jones32bf1862010-03-02 13:46:07 -0600919 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200920 $class = substr($class, $last_slash);
dchill425628ba02012-08-08 12:05:45 -0400921
922 // Check for match and driver base class
dchill42aee92652012-08-26 21:45:35 -0400923 if (strtolower(trim($subdir, '/')) == strtolower($class) && ! class_exists('CI_Driver_Library'))
dchill425628ba02012-08-08 12:05:45 -0400924 {
925 // We aren't instantiating an object here, just making the base class available
926 require BASEPATH.'libraries/Driver.php';
927 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 }
929
930 // We'll test for both lowercase and capitalized versions of the file name
931 foreach (array(ucfirst($class), strtolower($class)) as $class)
932 {
Greg Aker3a746652011-04-19 10:59:47 -0500933 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000934
Barry Mienydd671972010-10-04 16:33:58 +0200935 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 if (file_exists($subclass))
937 {
Greg Aker3a746652011-04-19 10:59:47 -0500938 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 if ( ! file_exists($baseclass))
941 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200942 log_message('error', 'Unable to load the requested class: '.$class);
943 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 }
945
Andrey Andreevd7297352012-01-07 22:53:14 +0200946 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 if (in_array($subclass, $this->_ci_loaded_files))
948 {
949 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200950 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 // return a new instance of the object
952 if ( ! is_null($object_name))
953 {
954 $CI =& get_instance();
955 if ( ! isset($CI->$object_name))
956 {
Barry Mienydd671972010-10-04 16:33:58 +0200957 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 }
959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200962 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 return;
964 }
Barry Mienydd671972010-10-04 16:33:58 +0200965
966 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 include_once($subclass);
968 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200969
970 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 }
Barry Mienydd671972010-10-04 16:33:58 +0200972
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600974 $is_duplicate = FALSE;
975 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
Greg Aker3a746652011-04-19 10:59:47 -0500977 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600978
Andrey Andreevd7297352012-01-07 22:53:14 +0200979 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 if ( ! file_exists($filepath))
981 {
982 continue;
983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Andrey Andreevd7297352012-01-07 22:53:14 +0200985 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 if (in_array($filepath, $this->_ci_loaded_files))
987 {
988 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200989 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 // return a new instance of the object
991 if ( ! is_null($object_name))
992 {
993 $CI =& get_instance();
994 if ( ! isset($CI->$object_name))
995 {
996 return $this->_ci_init_class($class, '', $params, $object_name);
997 }
998 }
Barry Mienydd671972010-10-04 16:33:58 +0200999
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +02001001 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 return;
1003 }
Barry Mienydd671972010-10-04 16:33:58 +02001004
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 include_once($filepath);
1006 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +02001007 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 }
1009 } // END FOREACH
1010
Andrey Andreevd7297352012-01-07 22:53:14 +02001011 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Alex Bilbieed944a32012-06-02 11:07:47 +01001012 if ($subdir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 {
1014 $path = strtolower($class).'/'.$class;
dchill420fc3be52012-08-27 20:54:23 -04001015 return $this->_ci_load_class($path, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 }
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001017 elseif (ucfirst($subdir) != $subdir)
dchill42aee92652012-08-26 21:45:35 -04001018 {
1019 // Lowercase subdir failed - retry capitalized
1020 $path = ucfirst($subdir).$class;
dchill420fc3be52012-08-27 20:54:23 -04001021 return $this->_ci_load_class($path, $params, $object_name);
dchill42aee92652012-08-26 21:45:35 -04001022 }
Barry Mienydd671972010-10-04 16:33:58 +02001023
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 // If we got this far we were unable to find the requested class.
1025 // We do not issue errors if the load call failed due to a duplicate request
Alex Bilbieed944a32012-06-02 11:07:47 +01001026 if ($is_duplicate === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001028 log_message('error', 'Unable to load the requested class: '.$class);
1029 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 }
1031 }
Barry Mienydd671972010-10-04 16:33:58 +02001032
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 // --------------------------------------------------------------------
1034
1035 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001036 * Internal CI Class Instantiator
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001038 * @used-by CI_Loader::_ci_load_class()
1039 *
1040 * @param string $class Class name
1041 * @param string $prefix Class name prefix
1042 * @param array|null|bool $config Optional configuration to pass to the class constructor:
1043 * FALSE to skip;
1044 * NULL to search in config paths;
1045 * array containing configuration data
1046 * @param string $object_name Optional object name to assign to
Andrey Andreev94af3552012-03-26 23:10:42 +03001047 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001049 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001050 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001051 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 if ($config === NULL)
1053 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001054 // Fetch the config paths containing any package paths
1055 $config_component = $this->_ci_get_component('config');
1056
1057 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001059 // Break on the first found file, thus package files
1060 // are not overridden by default paths
1061 foreach ($config_component->_config_paths as $path)
1062 {
1063 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001064 // are case-sensitive with regard to file names. Check for environment
1065 // first, global next
Andrey Andreev94af3552012-03-26 23:10:42 +03001066 if (defined('ENVIRONMENT') && file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001067 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001068 include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001069 break;
1070 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001071 elseif (defined('ENVIRONMENT') && file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001072 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001073 include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001074 break;
1075 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001076 elseif (file_exists($path.'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001077 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001078 include($path.'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001079 break;
1080 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001081 elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001082 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001083 include($path.'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001084 break;
1085 }
1086 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 }
1088 }
Barry Mienydd671972010-10-04 16:33:58 +02001089
Alex Bilbieed944a32012-06-02 11:07:47 +01001090 if ($prefix === '')
Barry Mienydd671972010-10-04 16:33:58 +02001091 {
1092 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 {
1094 $name = 'CI_'.$class;
1095 }
Barry Mienydd671972010-10-04 16:33:58 +02001096 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 {
1098 $name = config_item('subclass_prefix').$class;
1099 }
1100 else
1101 {
1102 $name = $class;
1103 }
1104 }
1105 else
1106 {
1107 $name = $prefix.$class;
1108 }
Barry Mienydd671972010-10-04 16:33:58 +02001109
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 // Is the class name valid?
1111 if ( ! class_exists($name))
1112 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001113 log_message('error', 'Non-existent class: '.$name);
jonnueee2df62012-07-16 13:06:16 +01001114 show_error('Non-existent class: '.$name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 }
Barry Mienydd671972010-10-04 16:33:58 +02001116
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001118 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001120
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 if (is_null($object_name))
1122 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001123 $classvar = isset($this->_ci_varmap[$class]) ? $this->_ci_varmap[$class] : $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 }
1125 else
1126 {
1127 $classvar = $object_name;
1128 }
1129
Barry Mienydd671972010-10-04 16:33:58 +02001130 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 $this->_ci_classes[$class] = $classvar;
1132
Barry Mienydd671972010-10-04 16:33:58 +02001133 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 $CI =& get_instance();
1135 if ($config !== NULL)
1136 {
1137 $CI->$classvar = new $name($config);
1138 }
1139 else
Barry Mienydd671972010-10-04 16:33:58 +02001140 {
Andrey Andreeva11b16b2012-03-28 12:22:04 +03001141 $CI->$classvar = new $name();
Barry Mienydd671972010-10-04 16:33:58 +02001142 }
1143 }
1144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001146
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001148 * CI Autoloader
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001150 * Loads component listed in the config/autoload.php file.
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001152 * @used-by CI_Loader::initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 * @return void
1154 */
Shane Pearson665baec2011-08-22 18:52:19 -05001155 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001156 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001157 if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001158 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001159 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001160 }
1161 else
1162 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001163 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001164 }
Barry Mienydd671972010-10-04 16:33:58 +02001165
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 if ( ! isset($autoload))
1167 {
1168 return FALSE;
1169 }
Barry Mienydd671972010-10-04 16:33:58 +02001170
Phil Sturgeon9730c752010-12-15 10:50:15 +00001171 // Autoload packages
1172 if (isset($autoload['packages']))
1173 {
1174 foreach ($autoload['packages'] as $package_path)
1175 {
1176 $this->add_package_path($package_path);
1177 }
1178 }
1179
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 // Load any custom config file
1181 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001182 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 $CI =& get_instance();
1184 foreach ($autoload['config'] as $key => $val)
1185 {
1186 $CI->config->load($val);
1187 }
Barry Mienydd671972010-10-04 16:33:58 +02001188 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001189
Derek Jonesc6da5032010-03-09 20:44:27 -06001190 // Autoload helpers and languages
1191 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001192 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001193 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 {
1195 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001196 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 }
1198
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001200 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 {
1202 // Load the database driver.
1203 if (in_array('database', $autoload['libraries']))
1204 {
1205 $this->database();
1206 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1207 }
Barry Mienydd671972010-10-04 16:33:58 +02001208
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 // Load all other libraries
1210 foreach ($autoload['libraries'] as $item)
1211 {
1212 $this->library($item);
1213 }
Barry Mienydd671972010-10-04 16:33:58 +02001214 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001215
Darren Hillc4e266b2011-08-30 15:40:27 -04001216 // Autoload drivers
1217 if (isset($autoload['drivers']))
1218 {
Darren Hillca3be1d2011-08-31 08:31:18 -04001219 foreach ($autoload['drivers'] as $item)
1220 {
1221 $this->driver($item);
1222 }
Darren Hillc4e266b2011-08-30 15:40:27 -04001223 }
1224
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 // Autoload models
1226 if (isset($autoload['model']))
1227 {
1228 $this->model($autoload['model']);
1229 }
Barry Mienydd671972010-10-04 16:33:58 +02001230 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001231
1232 // --------------------------------------------------------------------
1233
1234 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001235 * CI Object to Array translator
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001237 * Takes an object as input and converts the class variables to
1238 * an associative array with key/value pairs.
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001240 * @param object $object Object data to translate
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 * @return array
1242 */
Greg Akerf5c84022011-04-19 17:13:03 -05001243 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001245 return is_object($object) ? get_object_vars($object) : $object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 }
1247
1248 // --------------------------------------------------------------------
1249
1250 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001251 * CI Component getter
Derek Jones32bf1862010-03-02 13:46:07 -06001252 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001253 * Get a reference to a specific library or model.
1254 *
1255 * @param string $component Component name
Derek Jones32bf1862010-03-02 13:46:07 -06001256 * @return bool
1257 */
Greg Akerf5c84022011-04-19 17:13:03 -05001258 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001259 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001260 $CI =& get_instance();
1261 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001262 }
1263
1264 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001265
Derek Jones32bf1862010-03-02 13:46:07 -06001266 /**
1267 * Prep filename
1268 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001269 * This function prepares filenames of various items to
1270 * make their loading more reliable.
Derek Jones32bf1862010-03-02 13:46:07 -06001271 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001272 * @param string|string[] $filename Filename(s)
1273 * @param string $extension Filename extension
Derek Jones32bf1862010-03-02 13:46:07 -06001274 * @return array
1275 */
Greg Akerf5c84022011-04-19 17:13:03 -05001276 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001277 {
1278 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001279 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001280 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001281 }
1282 else
1283 {
1284 foreach ($filename as $key => $val)
1285 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001286 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001287 }
Barry Mienydd671972010-10-04 16:33:58 +02001288
Derek Jones32bf1862010-03-02 13:46:07 -06001289 return $filename;
1290 }
1291 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001292
Derek Allard2067d1a2008-11-13 22:59:24 +00001293}
1294
1295/* End of file Loader.php */
Andrey Andreev9438e262012-10-05 13:16:27 +03001296/* Location: ./system/core/Loader.php */