blob: e9d03482f021d99e7ad822c68ab8515a83b484d1 [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 Andreeved4b2582012-10-27 17:46:52 +0300133 * 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 /**
Shane Pearson6adfe632011-08-10 16:42:53 -0500151 * Initialize the Loader
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500152 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300153 * @used-by CI_Controller
154 * @uses CI_Loader::_ci_autoloader()
155 * @return object $this
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500156 */
Shane Pearson6adfe632011-08-10 16:42:53 -0500157 public function initialize()
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500158 {
Shane Pearson6adfe632011-08-10 16:42:53 -0500159 $this->_ci_classes = array();
160 $this->_ci_loaded_files = array();
161 $this->_ci_models = array();
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500162 $this->_base_classes =& is_loaded();
Shane Pearson6adfe632011-08-10 16:42:53 -0500163
164 $this->_ci_autoloader();
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500165 return $this;
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Is Loaded
172 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300173 * A utility method to test if a class is in the self::$_ci_classes array.
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500174 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300175 * @used-by Mainly used by Form Helper function _get_validation_object().
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500176 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300177 * @param string $class Class name to check for
178 * @return string|bool Class object name if loaded or FALSE
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500179 */
180 public function is_loaded($class)
181 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300182 return isset($this->_ci_classes[$class]) ? $this->_ci_classes[$class] : FALSE;
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500183 }
184
185 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300188 * Library Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300190 * Loads and instantiates libraries.
191 * Designed to be called from application controllers.
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300193 * @param string $library Library name
194 * @param array $params Optional parameters to pass to the library class constructor
195 * @param string $object_name An optional object name to assign to
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200197 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500198 public function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
Greg Akerce433962010-10-12 09:29:35 -0500200 if (is_array($library))
201 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100202 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500203 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600204 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500205 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000206
Greg Akerce433962010-10-12 09:29:35 -0500207 return;
208 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000209
Alex Bilbieed944a32012-06-02 11:07:47 +0100210 if ($library === '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300212 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 }
214
Derek Jones32bf1862010-03-02 13:46:07 -0600215 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
217 $params = NULL;
218 }
219
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600220 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
222
223 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 /**
226 * Model Loader
227 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300228 * Loads and instantiates libraries.
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300230 * @param string $model Model name
231 * @param string $name An optional object name to assign to
232 * @param bool $db_conn An optional database connection configuration to initialize
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200234 */
Greg Akerf5c84022011-04-19 17:13:03 -0500235 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200236 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300237 if (empty($model))
238 {
239 return;
240 }
241 elseif (is_array($model))
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
Joel Limberg3cf174b2012-07-13 20:49:57 +0300243 foreach ($model as $class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
Joel Limberg3cf174b2012-07-13 20:49:57 +0300245 $this->model($class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247 return;
248 }
249
Derek Jones32bf1862010-03-02 13:46:07 -0600250 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600253 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
Derek Jones32bf1862010-03-02 13:46:07 -0600255 // The path is in front of the last slash
Andrey Andreevd47baab2012-01-09 16:56:46 +0200256 $path = substr($model, 0, ++$last_slash);
Derek Jones32bf1862010-03-02 13:46:07 -0600257
258 // And the model name behind it
Andrey Andreevd47baab2012-01-09 16:56:46 +0200259 $model = substr($model, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Phil Sturgeon10d78f62012-06-04 14:41:53 -0500262 if (empty($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
264 $name = $model;
265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 if (in_array($name, $this->_ci_models, TRUE))
268 {
269 return;
270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 $CI =& get_instance();
273 if (isset($CI->$name))
274 {
275 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
276 }
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000279
Derek Jones32bf1862010-03-02 13:46:07 -0600280 foreach ($this->_ci_model_paths as $mod_path)
281 {
Greg Aker3a746652011-04-19 10:59:47 -0500282 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600283 {
284 continue;
285 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000286
Andrey Andreev94af3552012-03-26 23:10:42 +0300287 if ($db_conn !== FALSE && ! class_exists('CI_DB'))
Derek Jones32bf1862010-03-02 13:46:07 -0600288 {
289 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500290 {
Derek Jones32bf1862010-03-02 13:46:07 -0600291 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500292 }
Derek Jones32bf1862010-03-02 13:46:07 -0600293
294 $CI->load->database($db_conn, FALSE, TRUE);
295 }
296
Greg Akerbce13482010-10-11 15:37:16 -0500297 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600298 {
299 load_class('Model', 'core');
300 }
301
Greg Aker3a746652011-04-19 10:59:47 -0500302 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600303
304 $model = ucfirst($model);
Derek Jones32bf1862010-03-02 13:46:07 -0600305 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600306 $this->_ci_models[] = $name;
307 return;
308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Jones32bf1862010-03-02 13:46:07 -0600310 // couldn't find the model
311 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 /**
317 * Database Loader
318 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300319 * @param mixed $params Database configuration options
320 * @param bool $return Whether to return the database object
321 * @param bool $query_builder Whether to enable Query Builder
322 * (overrides the configuration setting)
323 *
324 * @return void|object|bool Database object if $return is set to TRUE,
325 * FALSE on failure, void in any other case
Barry Mienydd671972010-10-04 16:33:58 +0200326 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000327 public function database($params = '', $return = FALSE, $query_builder = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 // Grab the super object
330 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 // Do we even need to load the database class?
Andrey Andreev9d0ab042012-10-24 21:47:39 +0300333 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 +0000334 {
335 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200336 }
337
Greg Aker3a746652011-04-19 10:59:47 -0500338 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000339
340 if ($return === TRUE)
341 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000342 return DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 }
Barry Mienydd671972010-10-04 16:33:58 +0200344
Andrey Andreevd7297352012-01-07 22:53:14 +0200345 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // reference errors with some configurations
347 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // Load the DB class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000350 $CI->db =& DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 // --------------------------------------------------------------------
354
355 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300356 * Load the Database Utilities Class
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300358 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200359 */
Greg Akerf5c84022011-04-19 17:13:03 -0500360 public function dbutil()
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 if ( ! class_exists('CI_DB'))
363 {
364 $this->database();
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 $CI =& get_instance();
368
369 // for backwards compatibility, load dbforge so we can extend dbutils off it
370 // this use is deprecated and strongly discouraged
371 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200372
Greg Aker3a746652011-04-19 10:59:47 -0500373 require_once(BASEPATH.'database/DB_utility.php');
374 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
376
Pascal Kriete58560022010-11-10 16:01:20 -0500377 $CI->dbutil = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 }
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 // --------------------------------------------------------------------
381
382 /**
383 * Load the Database Forge Class
384 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300385 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200386 */
Greg Akerf5c84022011-04-19 17:13:03 -0500387 public function dbforge()
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
389 if ( ! class_exists('CI_DB'))
390 {
391 $this->database();
392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200395
Greg Aker3a746652011-04-19 10:59:47 -0500396 require_once(BASEPATH.'database/DB_forge.php');
397 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
399
400 $CI->dbforge = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300406 * View Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300408 * Loads "view" files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300410 * @param string $view View name
411 * @param array $vars An associative array of data
412 * to be extracted for use in the view
413 * @param bool $return Whether to return the view output
414 * or leave it to the Output class
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 * @return void
416 */
Greg Akerf5c84022011-04-19 17:13:03 -0500417 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
419 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300425 * Generic File Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300427 * @param string $path File path
428 * @param bool $return Whether to return the file output
429 * @return void|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 */
Greg Akerf5c84022011-04-19 17:13:03 -0500431 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
433 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 /**
439 * Set Variables
440 *
441 * Once variables are set they become available within
442 * the controller class and its "view" files.
443 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300444 * @param array|object|string $vars
445 * An associative array or object containing values
446 * to be set, or a value's name if string
447 * @param string $val Value to set, only used if $vars is a string
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @return void
449 */
Greg Akerf5c84022011-04-19 17:13:03 -0500450 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100452 if ($val !== '' && is_string($vars))
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
454 $vars = array($vars => $val);
455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200458
Andrey Andreev94af3552012-03-26 23:10:42 +0300459 if (is_array($vars) && count($vars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
461 foreach ($vars as $key => $val)
462 {
463 $this->_ci_cached_vars[$key] = $val;
464 }
465 }
466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600471 * Get Variable
472 *
473 * Check if a variable is set and retrieve it.
474 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300475 * @param string $key Variable name
476 * @return mixed The variable or NULL if not found
Phil Sturgeon8731f642011-07-22 16:11:34 -0600477 */
478 public function get_var($key)
479 {
480 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
481 }
482
483 // --------------------------------------------------------------------
484
485 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600486 * Get Variables
487 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300488 * Retrieves all loaded variables.
Shane Pearson81dd2232011-11-18 20:49:35 -0600489 *
490 * @return array
491 */
492 public function get_vars()
493 {
494 return $this->_ci_cached_vars;
495 }
496
497 // --------------------------------------------------------------------
498
499 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300500 * Helper Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300502 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 * @return void
504 */
Greg Akerf5c84022011-04-19 17:13:03 -0500505 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200506 {
Derek Jones32bf1862010-03-02 13:46:07 -0600507 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200508 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 if (isset($this->_ci_helpers[$helper]))
510 {
511 continue;
512 }
Derek Jones32bf1862010-03-02 13:46:07 -0600513
Greg Aker3a746652011-04-19 10:59:47 -0500514 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000515
Barry Mienydd671972010-10-04 16:33:58 +0200516 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 if (file_exists($ext_helper))
518 {
Greg Aker3a746652011-04-19 10:59:47 -0500519 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 if ( ! file_exists($base_helper))
522 {
Greg Aker3a746652011-04-19 10:59:47 -0500523 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 include_once($ext_helper);
527 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Jones32bf1862010-03-02 13:46:07 -0600529 $this->_ci_helpers[$helper] = TRUE;
530 log_message('debug', 'Helper loaded: '.$helper);
531 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Jones32bf1862010-03-02 13:46:07 -0600534 // Try to load the helper
535 foreach ($this->_ci_helper_paths as $path)
536 {
Greg Aker3a746652011-04-19 10:59:47 -0500537 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200538 {
Greg Aker3a746652011-04-19 10:59:47 -0500539 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600540
541 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200542 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600543 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 }
545 }
546
Derek Jones32bf1862010-03-02 13:46:07 -0600547 // unable to load the helper
548 if ( ! isset($this->_ci_helpers[$helper]))
549 {
Greg Aker3a746652011-04-19 10:59:47 -0500550 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 /**
558 * Load Helpers
559 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300560 * An alias for the helper() method in case the developer has
561 * written the plural form of it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300563 * @uses CI_Loader::helper()
564 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 * @return void
566 */
Greg Akerf5c84022011-04-19 17:13:03 -0500567 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
569 $this->helper($helpers);
570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300575 * Language Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300577 * Loads language files.
578 *
579 * @param string|string[] $files List of language file names to load
580 * @param string Language name
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 * @return void
582 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300583 public function language($files = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
585 $CI =& get_instance();
586
Andrey Andreeved4b2582012-10-27 17:46:52 +0300587 is_array($files) OR $files = array($files);
Derek Allard2067d1a2008-11-13 22:59:24 +0000588
Andrey Andreeved4b2582012-10-27 17:46:52 +0300589 foreach ($files as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200590 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 $CI->lang->load($langfile, $lang);
592 }
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300598 * Config Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300600 * Loads a config file (an alias for CI_Config::load()).
601 *
602 * @uses CI_Config::load()
603 * @param string $file Configuration file name
604 * @param bool $use_sections Whether configuration values should be loaded into their own section
605 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
606 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 */
Greg Akerf5c84022011-04-19 17:13:03 -0500608 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200609 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 $CI =& get_instance();
Andrey Andreeved4b2582012-10-27 17:46:52 +0300611 return $CI->config->load($file, $use_sections, $fail_gracefully);
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 }
613
614 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300617 * Driver Loader
Derek Jones8dca0412010-03-05 13:01:44 -0600618 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300619 * Loads a driver library.
Derek Jones8dca0412010-03-05 13:01:44 -0600620 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300621 * @param string|string[] $library Driver name(s)
622 * @param array $params Optional parameters to pass to the driver
623 * @param string $object_name An optional object name to assign to
624 *
625 * @return void|object|bool Object or FALSE on failure if $library is a string
626 * and $object_name is set. void otherwise.
Derek Jones8dca0412010-03-05 13:01:44 -0600627 */
Greg Akerf5c84022011-04-19 17:13:03 -0500628 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600629 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800630 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800631 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800632 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800633 {
634 $this->driver($driver);
635 }
dchill420fc3be52012-08-27 20:54:23 -0400636 return;
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800637 }
638
Alex Bilbieed944a32012-06-02 11:07:47 +0100639 if ($library === '')
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200640 {
641 return FALSE;
642 }
643
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600644 // We can save the loader some time since Drivers will *always* be in a subfolder,
645 // and typically identically named to the library
646 if ( ! strpos($library, '/'))
647 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500648 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600649 }
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Jones8dca0412010-03-05 13:01:44 -0600651 return $this->library($library, $params, $object_name);
652 }
653
654 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200655
Derek Jones8dca0412010-03-05 13:01:44 -0600656 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600657 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300659 * Prepends a parent path to the library, model, helper and config
660 * path arrays.
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300662 * @see CI_Loader::$_ci_library_paths
663 * @see CI_Loader::$_ci_model_paths
664 * @see CI_Loader::$_ci_helper_paths
665 * @see CI_Config::$_config_paths
666 *
667 * @param string $path Path to add
668 * @param bool $view_cascade (default: TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600670 */
Andrey Andreevcce91802012-06-12 13:25:31 +0300671 public function add_package_path($path, $view_cascade = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600672 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500673 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200674
Derek Jones32bf1862010-03-02 13:46:07 -0600675 array_unshift($this->_ci_library_paths, $path);
676 array_unshift($this->_ci_model_paths, $path);
677 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200678
Greg Akerf5c84022011-04-19 17:13:03 -0500679 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
680
Derek Jones32bf1862010-03-02 13:46:07 -0600681 // Add config file path
682 $config =& $this->_ci_get_component('config');
Korri3684d342012-04-17 00:35:08 -0400683 array_push($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 }
685
686 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600687
688 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000689 * Get Package Paths
690 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300691 * Return a list of all package paths.
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000692 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300693 * @param bool $include_base Whether to include BASEPATH (default: TRUE)
694 * @return array
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000695 */
Greg Akerf5c84022011-04-19 17:13:03 -0500696 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000697 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300698 return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000699 }
700
701 // --------------------------------------------------------------------
702
703 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600704 * Remove Package Path
705 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300706 * Remove a path from the library, model, helper and/or config
707 * path arrays if it exists. If no path is provided, the most recently
708 * added path will be removed removed.
Derek Jones32bf1862010-03-02 13:46:07 -0600709 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300710 * @param string $path Path to remove
Andrey Andreev94af3552012-03-26 23:10:42 +0300711 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600712 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300713 public function remove_package_path($path = '')
Derek Jones32bf1862010-03-02 13:46:07 -0600714 {
715 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200716
Alex Bilbieed944a32012-06-02 11:07:47 +0100717 if ($path === '')
Derek Jones32bf1862010-03-02 13:46:07 -0600718 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200719 array_shift($this->_ci_library_paths);
720 array_shift($this->_ci_model_paths);
721 array_shift($this->_ci_helper_paths);
722 array_shift($this->_ci_view_paths);
Korri3684d342012-04-17 00:35:08 -0400723 array_pop($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600724 }
725 else
726 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500727 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600728 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
729 {
730 if (($key = array_search($path, $this->{$var})) !== FALSE)
731 {
732 unset($this->{$var}[$key]);
733 }
734 }
David Behlercda768a2011-08-14 23:52:48 +0200735
Greg Akerf5c84022011-04-19 17:13:03 -0500736 if (isset($this->_ci_view_paths[$path.'views/']))
737 {
738 unset($this->_ci_view_paths[$path.'views/']);
739 }
Barry Mienydd671972010-10-04 16:33:58 +0200740
Derek Jones32bf1862010-03-02 13:46:07 -0600741 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
742 {
743 unset($config->_config_paths[$key]);
744 }
745 }
Barry Mienydd671972010-10-04 16:33:58 +0200746
Derek Jones32bf1862010-03-02 13:46:07 -0600747 // make sure the application default paths are still in the array
748 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
749 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
750 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500751 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600752 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
753 }
754
755 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200756
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300758 * Internal CI Data Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300760 * Used to load views and files.
761 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 * Variables are prefixed with _ci_ to avoid symbol collision with
Andrey Andreeved4b2582012-10-27 17:46:52 +0300763 * variables made available to view files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300765 * @used-by CI_Loader::view()
766 * @used-by CI_Loader::file()
767 * @param array $_ci_data Data to load
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 * @return void
769 */
Greg Akerf5c84022011-04-19 17:13:03 -0500770 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 {
772 // Set the default data variables
773 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
774 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300775 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 }
David Behlercda768a2011-08-14 23:52:48 +0200777
Greg Akerf5c84022011-04-19 17:13:03 -0500778 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000779
780 // Set the path to the requested file
Alex Bilbie40bd2a72012-06-02 16:04:15 +0100781 if (is_string($_ci_path) && $_ci_path !== '')
Greg Aker8807be32011-04-21 13:06:15 -0500782 {
783 $_ci_x = explode('/', $_ci_path);
784 $_ci_file = end($_ci_x);
785 }
786 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 {
788 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Alex Bilbieed944a32012-06-02 11:07:47 +0100789 $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500790
Joe McFrederick64f470b2012-08-18 12:29:56 -0400791 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
Greg Akerf5c84022011-04-19 17:13:03 -0500792 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400793 if (file_exists($_ci_view_file.$_ci_file))
Greg Akerf5c84022011-04-19 17:13:03 -0500794 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400795 $_ci_path = $_ci_view_file.$_ci_file;
Greg Akerf5c84022011-04-19 17:13:03 -0500796 $file_exists = TRUE;
797 break;
798 }
David Behlercda768a2011-08-14 23:52:48 +0200799
Greg Akerf5c84022011-04-19 17:13:03 -0500800 if ( ! $cascade)
801 {
802 break;
David Behlercda768a2011-08-14 23:52:48 +0200803 }
Greg Akerf5c84022011-04-19 17:13:03 -0500804 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 }
Barry Mienydd671972010-10-04 16:33:58 +0200806
Greg Akerf5c84022011-04-19 17:13:03 -0500807 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 {
809 show_error('Unable to load the requested file: '.$_ci_file);
810 }
Barry Mienydd671972010-10-04 16:33:58 +0200811
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 // This allows anything loaded using $this->load (views, files, etc.)
813 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500814 $_ci_CI =& get_instance();
815 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500817 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500819 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 }
821 }
822
823 /*
824 * Extract and cache variables
825 *
vlakoff02506182012-07-03 07:28:50 +0200826 * You can either set variables using the dedicated $this->load->vars()
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 * function or via the second parameter of this function. We'll merge
828 * the two types and cache them so that views that are embedded within
829 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200830 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 if (is_array($_ci_vars))
832 {
833 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
834 }
835 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 /*
838 * Buffer the output
839 *
840 * We buffer the output for two reasons:
841 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200842 * 2. So that the final rendered template can be post-processed by
dchill425628ba02012-08-08 12:05:45 -0400843 * the output class. Why do we need post processing? For one thing,
844 * in order to show the elapsed page load time. Unless we can
845 * intercept the content right before it's sent to the browser and
846 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 */
848 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200849
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 // If the PHP installation does not support short tags we'll
851 // do a little string replacement, changing the short tags
852 // to standard PHP echo statements.
Alex Bilbieed944a32012-06-02 11:07:47 +0100853 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 +0000854 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200855 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 }
857 else
858 {
859 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
860 }
Barry Mienydd671972010-10-04 16:33:58 +0200861
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 // Return the file data if requested
865 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200866 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 $buffer = ob_get_contents();
868 @ob_end_clean();
869 return $buffer;
870 }
871
872 /*
873 * Flush the buffer... or buff the flusher?
874 *
875 * In order to permit views to be nested within
876 * other views, we need to flush the content back out whenever
877 * we are beyond the first level of output buffering so that
878 * it can be seen and included properly by the first included
879 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200880 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 if (ob_get_level() > $this->_ci_ob_level + 1)
882 {
883 ob_end_flush();
884 }
885 else
886 {
Greg Aker22f1a632010-11-10 15:34:35 -0600887 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 @ob_end_clean();
889 }
890 }
891
892 // --------------------------------------------------------------------
893
894 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300895 * Internal CI Class Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300897 * @used-by CI_Loader::library()
898 * @uses CI_Loader::_ci_init_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300900 * @param string $class Class name to load
901 * @param mixed $params Optional parameters to pass to the class constructor
902 * @param string $object_name Optional object name to assign to
Barry Mienydd671972010-10-04 16:33:58 +0200903 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 */
Greg Akerf5c84022011-04-19 17:13:03 -0500905 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200906 {
907 // Get the class name, and while we're at it trim any slashes.
908 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500910 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200911
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 // Was the path included with the class name?
913 // We look for a slash to determine this
914 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600915 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 {
Derek Jones32bf1862010-03-02 13:46:07 -0600917 // Extract the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200918 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200919
Derek Jones32bf1862010-03-02 13:46:07 -0600920 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200921 $class = substr($class, $last_slash);
dchill425628ba02012-08-08 12:05:45 -0400922
923 // Check for match and driver base class
dchill42aee92652012-08-26 21:45:35 -0400924 if (strtolower(trim($subdir, '/')) == strtolower($class) && ! class_exists('CI_Driver_Library'))
dchill425628ba02012-08-08 12:05:45 -0400925 {
926 // We aren't instantiating an object here, just making the base class available
927 require BASEPATH.'libraries/Driver.php';
928 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 }
930
931 // We'll test for both lowercase and capitalized versions of the file name
932 foreach (array(ucfirst($class), strtolower($class)) as $class)
933 {
Greg Aker3a746652011-04-19 10:59:47 -0500934 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000935
Barry Mienydd671972010-10-04 16:33:58 +0200936 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 if (file_exists($subclass))
938 {
Greg Aker3a746652011-04-19 10:59:47 -0500939 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 if ( ! file_exists($baseclass))
942 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200943 log_message('error', 'Unable to load the requested class: '.$class);
944 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
946
Andrey Andreevd7297352012-01-07 22:53:14 +0200947 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 if (in_array($subclass, $this->_ci_loaded_files))
949 {
950 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200951 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 // return a new instance of the object
953 if ( ! is_null($object_name))
954 {
955 $CI =& get_instance();
956 if ( ! isset($CI->$object_name))
957 {
Barry Mienydd671972010-10-04 16:33:58 +0200958 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 }
960 }
Barry Mienydd671972010-10-04 16:33:58 +0200961
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200963 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 return;
965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966
967 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 include_once($subclass);
969 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200970
971 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
Barry Mienydd671972010-10-04 16:33:58 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600975 $is_duplicate = FALSE;
976 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 {
Greg Aker3a746652011-04-19 10:59:47 -0500978 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600979
Andrey Andreevd7297352012-01-07 22:53:14 +0200980 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 if ( ! file_exists($filepath))
982 {
983 continue;
984 }
Barry Mienydd671972010-10-04 16:33:58 +0200985
Andrey Andreevd7297352012-01-07 22:53:14 +0200986 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 if (in_array($filepath, $this->_ci_loaded_files))
988 {
989 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200990 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 // return a new instance of the object
992 if ( ! is_null($object_name))
993 {
994 $CI =& get_instance();
995 if ( ! isset($CI->$object_name))
996 {
997 return $this->_ci_init_class($class, '', $params, $object_name);
998 }
999 }
Barry Mienydd671972010-10-04 16:33:58 +02001000
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +02001002 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 return;
1004 }
Barry Mienydd671972010-10-04 16:33:58 +02001005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 include_once($filepath);
1007 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +02001008 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 }
1010 } // END FOREACH
1011
Andrey Andreevd7297352012-01-07 22:53:14 +02001012 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Alex Bilbieed944a32012-06-02 11:07:47 +01001013 if ($subdir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 {
1015 $path = strtolower($class).'/'.$class;
dchill420fc3be52012-08-27 20:54:23 -04001016 return $this->_ci_load_class($path, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 }
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001018 elseif (ucfirst($subdir) != $subdir)
dchill42aee92652012-08-26 21:45:35 -04001019 {
1020 // Lowercase subdir failed - retry capitalized
1021 $path = ucfirst($subdir).$class;
dchill420fc3be52012-08-27 20:54:23 -04001022 return $this->_ci_load_class($path, $params, $object_name);
dchill42aee92652012-08-26 21:45:35 -04001023 }
Barry Mienydd671972010-10-04 16:33:58 +02001024
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 // If we got this far we were unable to find the requested class.
1026 // We do not issue errors if the load call failed due to a duplicate request
Alex Bilbieed944a32012-06-02 11:07:47 +01001027 if ($is_duplicate === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001029 log_message('error', 'Unable to load the requested class: '.$class);
1030 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 }
1032 }
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 // --------------------------------------------------------------------
1035
1036 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001037 * Internal CI Class Instantiator
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001039 * @used-by CI_Loader::_ci_load_class()
1040 *
1041 * @param string $class Class name
1042 * @param string $prefix Class name prefix
1043 * @param array|null|bool $config Optional configuration to pass to the class constructor:
1044 * FALSE to skip;
1045 * NULL to search in config paths;
1046 * array containing configuration data
1047 * @param string $object_name Optional object name to assign to
Andrey Andreev94af3552012-03-26 23:10:42 +03001048 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001050 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001051 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001052 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 if ($config === NULL)
1054 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001055 // Fetch the config paths containing any package paths
1056 $config_component = $this->_ci_get_component('config');
1057
1058 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001060 // Break on the first found file, thus package files
1061 // are not overridden by default paths
1062 foreach ($config_component->_config_paths as $path)
1063 {
1064 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001065 // are case-sensitive with regard to file names. Check for environment
1066 // first, global next
Andrey Andreev94af3552012-03-26 23:10:42 +03001067 if (defined('ENVIRONMENT') && file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001068 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001069 include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001070 break;
1071 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001072 elseif (defined('ENVIRONMENT') && file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001073 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001074 include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001075 break;
1076 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001077 elseif (file_exists($path.'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001078 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001079 include($path.'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001080 break;
1081 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001082 elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001083 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001084 include($path.'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001085 break;
1086 }
1087 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 }
1089 }
Barry Mienydd671972010-10-04 16:33:58 +02001090
Alex Bilbieed944a32012-06-02 11:07:47 +01001091 if ($prefix === '')
Barry Mienydd671972010-10-04 16:33:58 +02001092 {
1093 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 {
1095 $name = 'CI_'.$class;
1096 }
Barry Mienydd671972010-10-04 16:33:58 +02001097 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
1099 $name = config_item('subclass_prefix').$class;
1100 }
1101 else
1102 {
1103 $name = $class;
1104 }
1105 }
1106 else
1107 {
1108 $name = $prefix.$class;
1109 }
Barry Mienydd671972010-10-04 16:33:58 +02001110
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 // Is the class name valid?
1112 if ( ! class_exists($name))
1113 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001114 log_message('error', 'Non-existent class: '.$name);
jonnueee2df62012-07-16 13:06:16 +01001115 show_error('Non-existent class: '.$name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 }
Barry Mienydd671972010-10-04 16:33:58 +02001117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001119 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001121
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 if (is_null($object_name))
1123 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001124 $classvar = isset($this->_ci_varmap[$class]) ? $this->_ci_varmap[$class] : $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 }
1126 else
1127 {
1128 $classvar = $object_name;
1129 }
1130
Barry Mienydd671972010-10-04 16:33:58 +02001131 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 $this->_ci_classes[$class] = $classvar;
1133
Barry Mienydd671972010-10-04 16:33:58 +02001134 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 $CI =& get_instance();
1136 if ($config !== NULL)
1137 {
1138 $CI->$classvar = new $name($config);
1139 }
1140 else
Barry Mienydd671972010-10-04 16:33:58 +02001141 {
Andrey Andreeva11b16b2012-03-28 12:22:04 +03001142 $CI->$classvar = new $name();
Barry Mienydd671972010-10-04 16:33:58 +02001143 }
1144 }
1145
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001147
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001149 * CI Autoloader
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001151 * Loads component listed in the config/autoload.php file.
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001153 * @used-by CI_Loader::initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 * @return void
1155 */
Shane Pearson665baec2011-08-22 18:52:19 -05001156 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001157 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001158 if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001159 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001160 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001161 }
1162 else
1163 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001164 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001165 }
Barry Mienydd671972010-10-04 16:33:58 +02001166
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 if ( ! isset($autoload))
1168 {
1169 return FALSE;
1170 }
Barry Mienydd671972010-10-04 16:33:58 +02001171
Phil Sturgeon9730c752010-12-15 10:50:15 +00001172 // Autoload packages
1173 if (isset($autoload['packages']))
1174 {
1175 foreach ($autoload['packages'] as $package_path)
1176 {
1177 $this->add_package_path($package_path);
1178 }
1179 }
1180
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 // Load any custom config file
1182 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001183 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 $CI =& get_instance();
1185 foreach ($autoload['config'] as $key => $val)
1186 {
1187 $CI->config->load($val);
1188 }
Barry Mienydd671972010-10-04 16:33:58 +02001189 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001190
Derek Jonesc6da5032010-03-09 20:44:27 -06001191 // Autoload helpers and languages
1192 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001193 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001194 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 {
1196 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001197 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 }
1199
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001201 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 {
1203 // Load the database driver.
1204 if (in_array('database', $autoload['libraries']))
1205 {
1206 $this->database();
1207 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1208 }
Barry Mienydd671972010-10-04 16:33:58 +02001209
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 // Load all other libraries
1211 foreach ($autoload['libraries'] as $item)
1212 {
1213 $this->library($item);
1214 }
Barry Mienydd671972010-10-04 16:33:58 +02001215 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001216
Darren Hillc4e266b2011-08-30 15:40:27 -04001217 // Autoload drivers
1218 if (isset($autoload['drivers']))
1219 {
Darren Hillca3be1d2011-08-31 08:31:18 -04001220 foreach ($autoload['drivers'] as $item)
1221 {
1222 $this->driver($item);
1223 }
Darren Hillc4e266b2011-08-30 15:40:27 -04001224 }
1225
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 // Autoload models
1227 if (isset($autoload['model']))
1228 {
1229 $this->model($autoload['model']);
1230 }
Barry Mienydd671972010-10-04 16:33:58 +02001231 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001232
1233 // --------------------------------------------------------------------
1234
1235 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001236 * CI Object to Array translator
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001238 * Takes an object as input and converts the class variables to
1239 * an associative array with key/value pairs.
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001241 * @param object $object Object data to translate
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 * @return array
1243 */
Greg Akerf5c84022011-04-19 17:13:03 -05001244 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001246 return is_object($object) ? get_object_vars($object) : $object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 }
1248
1249 // --------------------------------------------------------------------
1250
1251 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001252 * CI Component getter
Derek Jones32bf1862010-03-02 13:46:07 -06001253 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001254 * Get a reference to a specific library or model.
1255 *
1256 * @param string $component Component name
Derek Jones32bf1862010-03-02 13:46:07 -06001257 * @return bool
1258 */
Greg Akerf5c84022011-04-19 17:13:03 -05001259 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001260 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001261 $CI =& get_instance();
1262 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001263 }
1264
1265 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001266
Derek Jones32bf1862010-03-02 13:46:07 -06001267 /**
1268 * Prep filename
1269 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001270 * This function prepares filenames of various items to
1271 * make their loading more reliable.
Derek Jones32bf1862010-03-02 13:46:07 -06001272 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001273 * @param string|string[] $filename Filename(s)
1274 * @param string $extension Filename extension
Derek Jones32bf1862010-03-02 13:46:07 -06001275 * @return array
1276 */
Greg Akerf5c84022011-04-19 17:13:03 -05001277 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001278 {
1279 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001280 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001281 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001282 }
1283 else
1284 {
1285 foreach ($filename as $key => $val)
1286 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001287 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001288 }
Barry Mienydd671972010-10-04 16:33:58 +02001289
Derek Jones32bf1862010-03-02 13:46:07 -06001290 return $filename;
1291 }
1292 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001293
Derek Allard2067d1a2008-11-13 22:59:24 +00001294}
1295
1296/* End of file Loader.php */
Andrey Andreev9438e262012-10-05 13:16:27 +03001297/* Location: ./system/core/Loader.php */