blob: 2a78f4153fb174d72c316919f8a0e19de79fd7fc [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 Andreev679525d2012-11-03 00:35:48 +0200133 * Sets component load paths, gets the initial output buffering level
134 * and calls the autoloader.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300135 *
Andrey Andreev679525d2012-11-03 00:35:48 +0200136 * @uses CI_Loader::_ci_autoloader()
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300137 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 */
Greg Akerf5c84022011-04-19 17:13:03 -0500139 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +0200140 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500141 $this->_ci_ob_level = ob_get_level();
Derek Jones32bf1862010-03-02 13:46:07 -0600142 $this->_ci_library_paths = array(APPPATH, BASEPATH);
143 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
144 $this->_ci_model_paths = array(APPPATH);
Joe Cianflone8eef9c72011-08-21 10:39:06 -0400145 $this->_ci_view_paths = array(VIEWPATH => TRUE);
Andrey Andreev679525d2012-11-03 00:35:48 +0200146 $this->_base_classes =& is_loaded();
147 $this->_ci_autoloader();
David Behlercda768a2011-08-14 23:52:48 +0200148
Andrey Andreevd7297352012-01-07 22:53:14 +0200149 log_message('debug', 'Loader Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200153
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500154 /**
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500155 * Is Loaded
156 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300157 * A utility method to test if a class is in the self::$_ci_classes array.
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500158 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300159 * @used-by Mainly used by Form Helper function _get_validation_object().
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500160 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300161 * @param string $class Class name to check for
162 * @return string|bool Class object name if loaded or FALSE
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500163 */
164 public function is_loaded($class)
165 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300166 return isset($this->_ci_classes[$class]) ? $this->_ci_classes[$class] : FALSE;
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500167 }
168
169 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300172 * Library Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300174 * Loads and instantiates libraries.
175 * Designed to be called from application controllers.
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300177 * @param string $library Library name
178 * @param array $params Optional parameters to pass to the library class constructor
179 * @param string $object_name An optional object name to assign to
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200181 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500182 public function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
Greg Akerce433962010-10-12 09:29:35 -0500184 if (is_array($library))
185 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100186 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500187 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600188 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500189 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000190
Greg Akerce433962010-10-12 09:29:35 -0500191 return;
192 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000193
Alex Bilbieed944a32012-06-02 11:07:47 +0100194 if ($library === '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300196 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
198
Derek Jones32bf1862010-03-02 13:46:07 -0600199 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
201 $params = NULL;
202 }
203
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600204 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
206
207 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 /**
210 * Model Loader
211 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300212 * Loads and instantiates libraries.
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300214 * @param string $model Model name
215 * @param string $name An optional object name to assign to
216 * @param bool $db_conn An optional database connection configuration to initialize
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200218 */
Greg Akerf5c84022011-04-19 17:13:03 -0500219 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200220 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300221 if (empty($model))
222 {
223 return;
224 }
225 elseif (is_array($model))
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
Joel Limberg3cf174b2012-07-13 20:49:57 +0300227 foreach ($model as $class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
Joel Limberg3cf174b2012-07-13 20:49:57 +0300229 $this->model($class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
231 return;
232 }
233
Derek Jones32bf1862010-03-02 13:46:07 -0600234 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600237 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
Derek Jones32bf1862010-03-02 13:46:07 -0600239 // The path is in front of the last slash
Andrey Andreevd47baab2012-01-09 16:56:46 +0200240 $path = substr($model, 0, ++$last_slash);
Derek Jones32bf1862010-03-02 13:46:07 -0600241
242 // And the model name behind it
Andrey Andreevd47baab2012-01-09 16:56:46 +0200243 $model = substr($model, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Phil Sturgeon10d78f62012-06-04 14:41:53 -0500246 if (empty($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
248 $name = $model;
249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 if (in_array($name, $this->_ci_models, TRUE))
252 {
253 return;
254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 $CI =& get_instance();
257 if (isset($CI->$name))
258 {
259 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000263
Derek Jones32bf1862010-03-02 13:46:07 -0600264 foreach ($this->_ci_model_paths as $mod_path)
265 {
Greg Aker3a746652011-04-19 10:59:47 -0500266 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600267 {
268 continue;
269 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000270
Andrey Andreev94af3552012-03-26 23:10:42 +0300271 if ($db_conn !== FALSE && ! class_exists('CI_DB'))
Derek Jones32bf1862010-03-02 13:46:07 -0600272 {
273 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500274 {
Derek Jones32bf1862010-03-02 13:46:07 -0600275 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500276 }
Derek Jones32bf1862010-03-02 13:46:07 -0600277
278 $CI->load->database($db_conn, FALSE, TRUE);
279 }
280
Greg Akerbce13482010-10-11 15:37:16 -0500281 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600282 {
283 load_class('Model', 'core');
284 }
285
Greg Aker3a746652011-04-19 10:59:47 -0500286 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600287
288 $model = ucfirst($model);
Derek Jones32bf1862010-03-02 13:46:07 -0600289 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600290 $this->_ci_models[] = $name;
291 return;
292 }
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Jones32bf1862010-03-02 13:46:07 -0600294 // couldn't find the model
295 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 /**
301 * Database Loader
302 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300303 * @param mixed $params Database configuration options
304 * @param bool $return Whether to return the database object
305 * @param bool $query_builder Whether to enable Query Builder
306 * (overrides the configuration setting)
307 *
308 * @return void|object|bool Database object if $return is set to TRUE,
309 * FALSE on failure, void in any other case
Barry Mienydd671972010-10-04 16:33:58 +0200310 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000311 public function database($params = '', $return = FALSE, $query_builder = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
313 // Grab the super object
314 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 // Do we even need to load the database class?
Andrey Andreev9d0ab042012-10-24 21:47:39 +0300317 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 +0000318 {
319 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200320 }
321
Greg Aker3a746652011-04-19 10:59:47 -0500322 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000323
324 if ($return === TRUE)
325 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000326 return DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
Barry Mienydd671972010-10-04 16:33:58 +0200328
Andrey Andreevd7297352012-01-07 22:53:14 +0200329 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // reference errors with some configurations
331 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 // Load the DB class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000334 $CI->db =& DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // --------------------------------------------------------------------
338
339 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300340 * Load the Database Utilities Class
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300342 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200343 */
Greg Akerf5c84022011-04-19 17:13:03 -0500344 public function dbutil()
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
346 if ( ! class_exists('CI_DB'))
347 {
348 $this->database();
349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 $CI =& get_instance();
352
353 // for backwards compatibility, load dbforge so we can extend dbutils off it
354 // this use is deprecated and strongly discouraged
355 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200356
Greg Aker3a746652011-04-19 10:59:47 -0500357 require_once(BASEPATH.'database/DB_utility.php');
358 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
360
Pascal Kriete58560022010-11-10 16:01:20 -0500361 $CI->dbutil = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 // --------------------------------------------------------------------
365
366 /**
367 * Load the Database Forge Class
368 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300369 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200370 */
Greg Akerf5c84022011-04-19 17:13:03 -0500371 public function dbforge()
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
373 if ( ! class_exists('CI_DB'))
374 {
375 $this->database();
376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200379
Greg Aker3a746652011-04-19 10:59:47 -0500380 require_once(BASEPATH.'database/DB_forge.php');
381 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
383
384 $CI->dbforge = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300390 * View Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300392 * Loads "view" files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300394 * @param string $view View name
395 * @param array $vars An associative array of data
396 * to be extracted for use in the view
397 * @param bool $return Whether to return the view output
398 * or leave it to the Output class
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 * @return void
400 */
Greg Akerf5c84022011-04-19 17:13:03 -0500401 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
403 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300409 * Generic File Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300411 * @param string $path File path
412 * @param bool $return Whether to return the file output
413 * @return void|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 */
Greg Akerf5c84022011-04-19 17:13:03 -0500415 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
417 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 /**
423 * Set Variables
424 *
425 * Once variables are set they become available within
426 * the controller class and its "view" files.
427 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300428 * @param array|object|string $vars
429 * An associative array or object containing values
430 * to be set, or a value's name if string
431 * @param string $val Value to set, only used if $vars is a string
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @return void
433 */
Greg Akerf5c84022011-04-19 17:13:03 -0500434 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100436 if ($val !== '' && is_string($vars))
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
438 $vars = array($vars => $val);
439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200442
Andrey Andreev94af3552012-03-26 23:10:42 +0300443 if (is_array($vars) && count($vars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
445 foreach ($vars as $key => $val)
446 {
447 $this->_ci_cached_vars[$key] = $val;
448 }
449 }
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600455 * Get Variable
456 *
457 * Check if a variable is set and retrieve it.
458 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300459 * @param string $key Variable name
460 * @return mixed The variable or NULL if not found
Phil Sturgeon8731f642011-07-22 16:11:34 -0600461 */
462 public function get_var($key)
463 {
464 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
465 }
466
467 // --------------------------------------------------------------------
468
469 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600470 * Get Variables
471 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300472 * Retrieves all loaded variables.
Shane Pearson81dd2232011-11-18 20:49:35 -0600473 *
474 * @return array
475 */
476 public function get_vars()
477 {
478 return $this->_ci_cached_vars;
479 }
480
481 // --------------------------------------------------------------------
482
483 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300484 * Helper Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300486 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 * @return void
488 */
Greg Akerf5c84022011-04-19 17:13:03 -0500489 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200490 {
Derek Jones32bf1862010-03-02 13:46:07 -0600491 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200492 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 if (isset($this->_ci_helpers[$helper]))
494 {
495 continue;
496 }
Derek Jones32bf1862010-03-02 13:46:07 -0600497
Greg Aker3a746652011-04-19 10:59:47 -0500498 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000499
Barry Mienydd671972010-10-04 16:33:58 +0200500 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 if (file_exists($ext_helper))
502 {
Greg Aker3a746652011-04-19 10:59:47 -0500503 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 if ( ! file_exists($base_helper))
506 {
Greg Aker3a746652011-04-19 10:59:47 -0500507 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 }
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 include_once($ext_helper);
511 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Jones32bf1862010-03-02 13:46:07 -0600513 $this->_ci_helpers[$helper] = TRUE;
514 log_message('debug', 'Helper loaded: '.$helper);
515 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Jones32bf1862010-03-02 13:46:07 -0600518 // Try to load the helper
519 foreach ($this->_ci_helper_paths as $path)
520 {
Greg Aker3a746652011-04-19 10:59:47 -0500521 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200522 {
Greg Aker3a746652011-04-19 10:59:47 -0500523 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600524
525 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200526 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600527 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
529 }
530
Derek Jones32bf1862010-03-02 13:46:07 -0600531 // unable to load the helper
532 if ( ! isset($this->_ci_helpers[$helper]))
533 {
Greg Aker3a746652011-04-19 10:59:47 -0500534 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600535 }
Barry Mienydd671972010-10-04 16:33:58 +0200536 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 /**
542 * Load Helpers
543 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300544 * An alias for the helper() method in case the developer has
545 * written the plural form of it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300547 * @uses CI_Loader::helper()
548 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 * @return void
550 */
Greg Akerf5c84022011-04-19 17:13:03 -0500551 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 {
553 $this->helper($helpers);
554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300559 * Language Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300561 * Loads language files.
562 *
563 * @param string|string[] $files List of language file names to load
564 * @param string Language name
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 * @return void
566 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300567 public function language($files = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
569 $CI =& get_instance();
570
Andrey Andreeved4b2582012-10-27 17:46:52 +0300571 is_array($files) OR $files = array($files);
Derek Allard2067d1a2008-11-13 22:59:24 +0000572
Andrey Andreeved4b2582012-10-27 17:46:52 +0300573 foreach ($files as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200574 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 $CI->lang->load($langfile, $lang);
576 }
577 }
Barry Mienydd671972010-10-04 16:33:58 +0200578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300582 * Config Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300584 * Loads a config file (an alias for CI_Config::load()).
585 *
586 * @uses CI_Config::load()
587 * @param string $file Configuration file name
588 * @param bool $use_sections Whether configuration values should be loaded into their own section
589 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
590 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 */
Greg Akerf5c84022011-04-19 17:13:03 -0500592 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200593 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 $CI =& get_instance();
Andrey Andreeved4b2582012-10-27 17:46:52 +0300595 return $CI->config->load($file, $use_sections, $fail_gracefully);
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 }
597
598 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300601 * Driver Loader
Derek Jones8dca0412010-03-05 13:01:44 -0600602 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300603 * Loads a driver library.
Derek Jones8dca0412010-03-05 13:01:44 -0600604 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300605 * @param string|string[] $library Driver name(s)
606 * @param array $params Optional parameters to pass to the driver
607 * @param string $object_name An optional object name to assign to
608 *
609 * @return void|object|bool Object or FALSE on failure if $library is a string
610 * and $object_name is set. void otherwise.
Derek Jones8dca0412010-03-05 13:01:44 -0600611 */
Greg Akerf5c84022011-04-19 17:13:03 -0500612 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600613 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800614 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800615 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800616 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800617 {
618 $this->driver($driver);
619 }
dchill420fc3be52012-08-27 20:54:23 -0400620 return;
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800621 }
622
Alex Bilbieed944a32012-06-02 11:07:47 +0100623 if ($library === '')
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200624 {
625 return FALSE;
626 }
627
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600628 // We can save the loader some time since Drivers will *always* be in a subfolder,
629 // and typically identically named to the library
630 if ( ! strpos($library, '/'))
631 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500632 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600633 }
Barry Mienydd671972010-10-04 16:33:58 +0200634
Derek Jones8dca0412010-03-05 13:01:44 -0600635 return $this->library($library, $params, $object_name);
636 }
637
638 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200639
Derek Jones8dca0412010-03-05 13:01:44 -0600640 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600641 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300643 * Prepends a parent path to the library, model, helper and config
644 * path arrays.
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300646 * @see CI_Loader::$_ci_library_paths
647 * @see CI_Loader::$_ci_model_paths
648 * @see CI_Loader::$_ci_helper_paths
649 * @see CI_Config::$_config_paths
650 *
651 * @param string $path Path to add
652 * @param bool $view_cascade (default: TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600654 */
Andrey Andreevcce91802012-06-12 13:25:31 +0300655 public function add_package_path($path, $view_cascade = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600656 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500657 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200658
Derek Jones32bf1862010-03-02 13:46:07 -0600659 array_unshift($this->_ci_library_paths, $path);
660 array_unshift($this->_ci_model_paths, $path);
661 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200662
Greg Akerf5c84022011-04-19 17:13:03 -0500663 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
664
Derek Jones32bf1862010-03-02 13:46:07 -0600665 // Add config file path
666 $config =& $this->_ci_get_component('config');
Korri3684d342012-04-17 00:35:08 -0400667 array_push($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 }
669
670 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600671
672 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000673 * Get Package Paths
674 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300675 * Return a list of all package paths.
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000676 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300677 * @param bool $include_base Whether to include BASEPATH (default: TRUE)
678 * @return array
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000679 */
Greg Akerf5c84022011-04-19 17:13:03 -0500680 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000681 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300682 return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000683 }
684
685 // --------------------------------------------------------------------
686
687 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600688 * Remove Package Path
689 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300690 * Remove a path from the library, model, helper and/or config
691 * path arrays if it exists. If no path is provided, the most recently
692 * added path will be removed removed.
Derek Jones32bf1862010-03-02 13:46:07 -0600693 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300694 * @param string $path Path to remove
Andrey Andreev94af3552012-03-26 23:10:42 +0300695 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600696 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300697 public function remove_package_path($path = '')
Derek Jones32bf1862010-03-02 13:46:07 -0600698 {
699 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200700
Alex Bilbieed944a32012-06-02 11:07:47 +0100701 if ($path === '')
Derek Jones32bf1862010-03-02 13:46:07 -0600702 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200703 array_shift($this->_ci_library_paths);
704 array_shift($this->_ci_model_paths);
705 array_shift($this->_ci_helper_paths);
706 array_shift($this->_ci_view_paths);
Korri3684d342012-04-17 00:35:08 -0400707 array_pop($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600708 }
709 else
710 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500711 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600712 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
713 {
714 if (($key = array_search($path, $this->{$var})) !== FALSE)
715 {
716 unset($this->{$var}[$key]);
717 }
718 }
David Behlercda768a2011-08-14 23:52:48 +0200719
Greg Akerf5c84022011-04-19 17:13:03 -0500720 if (isset($this->_ci_view_paths[$path.'views/']))
721 {
722 unset($this->_ci_view_paths[$path.'views/']);
723 }
Barry Mienydd671972010-10-04 16:33:58 +0200724
Derek Jones32bf1862010-03-02 13:46:07 -0600725 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
726 {
727 unset($config->_config_paths[$key]);
728 }
729 }
Barry Mienydd671972010-10-04 16:33:58 +0200730
Derek Jones32bf1862010-03-02 13:46:07 -0600731 // make sure the application default paths are still in the array
732 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
733 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
734 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500735 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600736 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
737 }
738
739 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200740
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300742 * Internal CI Data Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300744 * Used to load views and files.
745 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 * Variables are prefixed with _ci_ to avoid symbol collision with
Andrey Andreeved4b2582012-10-27 17:46:52 +0300747 * variables made available to view files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300749 * @used-by CI_Loader::view()
750 * @used-by CI_Loader::file()
751 * @param array $_ci_data Data to load
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 * @return void
753 */
Greg Akerf5c84022011-04-19 17:13:03 -0500754 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 {
756 // Set the default data variables
757 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
758 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300759 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 }
David Behlercda768a2011-08-14 23:52:48 +0200761
Greg Akerf5c84022011-04-19 17:13:03 -0500762 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000763
764 // Set the path to the requested file
Alex Bilbie40bd2a72012-06-02 16:04:15 +0100765 if (is_string($_ci_path) && $_ci_path !== '')
Greg Aker8807be32011-04-21 13:06:15 -0500766 {
767 $_ci_x = explode('/', $_ci_path);
768 $_ci_file = end($_ci_x);
769 }
770 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 {
772 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Alex Bilbieed944a32012-06-02 11:07:47 +0100773 $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500774
Joe McFrederick64f470b2012-08-18 12:29:56 -0400775 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
Greg Akerf5c84022011-04-19 17:13:03 -0500776 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400777 if (file_exists($_ci_view_file.$_ci_file))
Greg Akerf5c84022011-04-19 17:13:03 -0500778 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400779 $_ci_path = $_ci_view_file.$_ci_file;
Greg Akerf5c84022011-04-19 17:13:03 -0500780 $file_exists = TRUE;
781 break;
782 }
David Behlercda768a2011-08-14 23:52:48 +0200783
Greg Akerf5c84022011-04-19 17:13:03 -0500784 if ( ! $cascade)
785 {
786 break;
David Behlercda768a2011-08-14 23:52:48 +0200787 }
Greg Akerf5c84022011-04-19 17:13:03 -0500788 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 }
Barry Mienydd671972010-10-04 16:33:58 +0200790
Greg Akerf5c84022011-04-19 17:13:03 -0500791 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 {
793 show_error('Unable to load the requested file: '.$_ci_file);
794 }
Barry Mienydd671972010-10-04 16:33:58 +0200795
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 // This allows anything loaded using $this->load (views, files, etc.)
797 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500798 $_ci_CI =& get_instance();
799 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500801 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500803 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 }
805 }
806
807 /*
808 * Extract and cache variables
809 *
vlakoff02506182012-07-03 07:28:50 +0200810 * You can either set variables using the dedicated $this->load->vars()
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 * function or via the second parameter of this function. We'll merge
812 * the two types and cache them so that views that are embedded within
813 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200814 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 if (is_array($_ci_vars))
816 {
817 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
818 }
819 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 /*
822 * Buffer the output
823 *
824 * We buffer the output for two reasons:
825 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200826 * 2. So that the final rendered template can be post-processed by
dchill425628ba02012-08-08 12:05:45 -0400827 * the output class. Why do we need post processing? For one thing,
828 * in order to show the elapsed page load time. Unless we can
829 * intercept the content right before it's sent to the browser and
830 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 */
832 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 // If the PHP installation does not support short tags we'll
835 // do a little string replacement, changing the short tags
836 // to standard PHP echo statements.
Alex Bilbieed944a32012-06-02 11:07:47 +0100837 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 +0000838 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200839 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 }
841 else
842 {
843 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
844 }
Barry Mienydd671972010-10-04 16:33:58 +0200845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200847
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 // Return the file data if requested
849 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200850 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 $buffer = ob_get_contents();
852 @ob_end_clean();
853 return $buffer;
854 }
855
856 /*
857 * Flush the buffer... or buff the flusher?
858 *
859 * In order to permit views to be nested within
860 * other views, we need to flush the content back out whenever
861 * we are beyond the first level of output buffering so that
862 * it can be seen and included properly by the first included
863 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200864 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 if (ob_get_level() > $this->_ci_ob_level + 1)
866 {
867 ob_end_flush();
868 }
869 else
870 {
Greg Aker22f1a632010-11-10 15:34:35 -0600871 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 @ob_end_clean();
873 }
874 }
875
876 // --------------------------------------------------------------------
877
878 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300879 * Internal CI Class Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300881 * @used-by CI_Loader::library()
882 * @uses CI_Loader::_ci_init_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300884 * @param string $class Class name to load
885 * @param mixed $params Optional parameters to pass to the class constructor
886 * @param string $object_name Optional object name to assign to
Barry Mienydd671972010-10-04 16:33:58 +0200887 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 */
Greg Akerf5c84022011-04-19 17:13:03 -0500889 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200890 {
891 // Get the class name, and while we're at it trim any slashes.
892 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500894 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 // Was the path included with the class name?
897 // We look for a slash to determine this
898 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600899 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 {
Derek Jones32bf1862010-03-02 13:46:07 -0600901 // Extract the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200902 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200903
Derek Jones32bf1862010-03-02 13:46:07 -0600904 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200905 $class = substr($class, $last_slash);
dchill425628ba02012-08-08 12:05:45 -0400906
907 // Check for match and driver base class
dchill42aee92652012-08-26 21:45:35 -0400908 if (strtolower(trim($subdir, '/')) == strtolower($class) && ! class_exists('CI_Driver_Library'))
dchill425628ba02012-08-08 12:05:45 -0400909 {
910 // We aren't instantiating an object here, just making the base class available
911 require BASEPATH.'libraries/Driver.php';
912 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 }
914
915 // We'll test for both lowercase and capitalized versions of the file name
916 foreach (array(ucfirst($class), strtolower($class)) as $class)
917 {
Greg Aker3a746652011-04-19 10:59:47 -0500918 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000919
Barry Mienydd671972010-10-04 16:33:58 +0200920 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 if (file_exists($subclass))
922 {
Greg Aker3a746652011-04-19 10:59:47 -0500923 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200924
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 if ( ! file_exists($baseclass))
926 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200927 log_message('error', 'Unable to load the requested class: '.$class);
928 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 }
930
Andrey Andreevd7297352012-01-07 22:53:14 +0200931 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 if (in_array($subclass, $this->_ci_loaded_files))
933 {
934 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200935 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 // return a new instance of the object
937 if ( ! is_null($object_name))
938 {
939 $CI =& get_instance();
940 if ( ! isset($CI->$object_name))
941 {
Barry Mienydd671972010-10-04 16:33:58 +0200942 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 }
944 }
Barry Mienydd671972010-10-04 16:33:58 +0200945
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200947 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 return;
949 }
Barry Mienydd671972010-10-04 16:33:58 +0200950
951 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 include_once($subclass);
953 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200954
955 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
Barry Mienydd671972010-10-04 16:33:58 +0200957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600959 $is_duplicate = FALSE;
960 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 {
Greg Aker3a746652011-04-19 10:59:47 -0500962 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600963
Andrey Andreevd7297352012-01-07 22:53:14 +0200964 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 if ( ! file_exists($filepath))
966 {
967 continue;
968 }
Barry Mienydd671972010-10-04 16:33:58 +0200969
Andrey Andreevd7297352012-01-07 22:53:14 +0200970 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 if (in_array($filepath, $this->_ci_loaded_files))
972 {
973 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200974 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 // return a new instance of the object
976 if ( ! is_null($object_name))
977 {
978 $CI =& get_instance();
979 if ( ! isset($CI->$object_name))
980 {
981 return $this->_ci_init_class($class, '', $params, $object_name);
982 }
983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200986 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 return;
988 }
Barry Mienydd671972010-10-04 16:33:58 +0200989
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 include_once($filepath);
991 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +0200992 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 }
994 } // END FOREACH
995
Andrey Andreevd7297352012-01-07 22:53:14 +0200996 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Alex Bilbieed944a32012-06-02 11:07:47 +0100997 if ($subdir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 {
999 $path = strtolower($class).'/'.$class;
dchill420fc3be52012-08-27 20:54:23 -04001000 return $this->_ci_load_class($path, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 }
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001002 elseif (ucfirst($subdir) != $subdir)
dchill42aee92652012-08-26 21:45:35 -04001003 {
1004 // Lowercase subdir failed - retry capitalized
1005 $path = ucfirst($subdir).$class;
dchill420fc3be52012-08-27 20:54:23 -04001006 return $this->_ci_load_class($path, $params, $object_name);
dchill42aee92652012-08-26 21:45:35 -04001007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 // If we got this far we were unable to find the requested class.
1010 // We do not issue errors if the load call failed due to a duplicate request
Alex Bilbieed944a32012-06-02 11:07:47 +01001011 if ($is_duplicate === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001013 log_message('error', 'Unable to load the requested class: '.$class);
1014 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 }
1016 }
Barry Mienydd671972010-10-04 16:33:58 +02001017
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 // --------------------------------------------------------------------
1019
1020 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001021 * Internal CI Class Instantiator
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001023 * @used-by CI_Loader::_ci_load_class()
1024 *
1025 * @param string $class Class name
1026 * @param string $prefix Class name prefix
1027 * @param array|null|bool $config Optional configuration to pass to the class constructor:
1028 * FALSE to skip;
1029 * NULL to search in config paths;
1030 * array containing configuration data
1031 * @param string $object_name Optional object name to assign to
Andrey Andreev94af3552012-03-26 23:10:42 +03001032 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001034 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001035 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001036 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 if ($config === NULL)
1038 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001039 // Fetch the config paths containing any package paths
1040 $config_component = $this->_ci_get_component('config');
1041
1042 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001044 // Break on the first found file, thus package files
1045 // are not overridden by default paths
1046 foreach ($config_component->_config_paths as $path)
1047 {
1048 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001049 // are case-sensitive with regard to file names. Check for environment
1050 // first, global next
Andrey Andreev94af3552012-03-26 23:10:42 +03001051 if (defined('ENVIRONMENT') && file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001052 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001053 include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001054 break;
1055 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001056 elseif (defined('ENVIRONMENT') && file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001057 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001058 include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001059 break;
1060 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001061 elseif (file_exists($path.'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001062 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001063 include($path.'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001064 break;
1065 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001066 elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001067 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001068 include($path.'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001069 break;
1070 }
1071 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 }
1073 }
Barry Mienydd671972010-10-04 16:33:58 +02001074
Alex Bilbieed944a32012-06-02 11:07:47 +01001075 if ($prefix === '')
Barry Mienydd671972010-10-04 16:33:58 +02001076 {
1077 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 {
1079 $name = 'CI_'.$class;
1080 }
Barry Mienydd671972010-10-04 16:33:58 +02001081 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 {
1083 $name = config_item('subclass_prefix').$class;
1084 }
1085 else
1086 {
1087 $name = $class;
1088 }
1089 }
1090 else
1091 {
1092 $name = $prefix.$class;
1093 }
Barry Mienydd671972010-10-04 16:33:58 +02001094
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 // Is the class name valid?
1096 if ( ! class_exists($name))
1097 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001098 log_message('error', 'Non-existent class: '.$name);
jonnueee2df62012-07-16 13:06:16 +01001099 show_error('Non-existent class: '.$name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 }
Barry Mienydd671972010-10-04 16:33:58 +02001101
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001103 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001105
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 if (is_null($object_name))
1107 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001108 $classvar = isset($this->_ci_varmap[$class]) ? $this->_ci_varmap[$class] : $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 }
1110 else
1111 {
1112 $classvar = $object_name;
1113 }
1114
Barry Mienydd671972010-10-04 16:33:58 +02001115 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 $this->_ci_classes[$class] = $classvar;
1117
Barry Mienydd671972010-10-04 16:33:58 +02001118 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 $CI =& get_instance();
1120 if ($config !== NULL)
1121 {
1122 $CI->$classvar = new $name($config);
1123 }
1124 else
Barry Mienydd671972010-10-04 16:33:58 +02001125 {
Andrey Andreeva11b16b2012-03-28 12:22:04 +03001126 $CI->$classvar = new $name();
Barry Mienydd671972010-10-04 16:33:58 +02001127 }
1128 }
1129
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001131
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001133 * CI Autoloader
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001135 * Loads component listed in the config/autoload.php file.
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 *
Andrey Andreev679525d2012-11-03 00:35:48 +02001137 * @used-by CI_Loader::__construct()
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 * @return void
1139 */
Shane Pearson665baec2011-08-22 18:52:19 -05001140 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001141 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001142 if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001143 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001144 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001145 }
1146 else
1147 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001148 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001149 }
Barry Mienydd671972010-10-04 16:33:58 +02001150
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 if ( ! isset($autoload))
1152 {
1153 return FALSE;
1154 }
Barry Mienydd671972010-10-04 16:33:58 +02001155
Phil Sturgeon9730c752010-12-15 10:50:15 +00001156 // Autoload packages
1157 if (isset($autoload['packages']))
1158 {
1159 foreach ($autoload['packages'] as $package_path)
1160 {
1161 $this->add_package_path($package_path);
1162 }
1163 }
1164
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 // Load any custom config file
1166 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001167 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 $CI =& get_instance();
1169 foreach ($autoload['config'] as $key => $val)
1170 {
1171 $CI->config->load($val);
1172 }
Barry Mienydd671972010-10-04 16:33:58 +02001173 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001174
Derek Jonesc6da5032010-03-09 20:44:27 -06001175 // Autoload helpers and languages
1176 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001177 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001178 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 {
1180 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001181 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 }
1183
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001185 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 {
1187 // Load the database driver.
1188 if (in_array('database', $autoload['libraries']))
1189 {
1190 $this->database();
1191 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1192 }
Barry Mienydd671972010-10-04 16:33:58 +02001193
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 // Load all other libraries
1195 foreach ($autoload['libraries'] as $item)
1196 {
1197 $this->library($item);
1198 }
Barry Mienydd671972010-10-04 16:33:58 +02001199 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001200
Darren Hillc4e266b2011-08-30 15:40:27 -04001201 // Autoload drivers
1202 if (isset($autoload['drivers']))
1203 {
Darren Hillca3be1d2011-08-31 08:31:18 -04001204 foreach ($autoload['drivers'] as $item)
1205 {
1206 $this->driver($item);
1207 }
Darren Hillc4e266b2011-08-30 15:40:27 -04001208 }
1209
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 // Autoload models
1211 if (isset($autoload['model']))
1212 {
1213 $this->model($autoload['model']);
1214 }
Barry Mienydd671972010-10-04 16:33:58 +02001215 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001216
1217 // --------------------------------------------------------------------
1218
1219 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001220 * CI Object to Array translator
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001222 * Takes an object as input and converts the class variables to
1223 * an associative array with key/value pairs.
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001225 * @param object $object Object data to translate
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 * @return array
1227 */
Greg Akerf5c84022011-04-19 17:13:03 -05001228 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001230 return is_object($object) ? get_object_vars($object) : $object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 }
1232
1233 // --------------------------------------------------------------------
1234
1235 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001236 * CI Component getter
Derek Jones32bf1862010-03-02 13:46:07 -06001237 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001238 * Get a reference to a specific library or model.
1239 *
1240 * @param string $component Component name
Derek Jones32bf1862010-03-02 13:46:07 -06001241 * @return bool
1242 */
Greg Akerf5c84022011-04-19 17:13:03 -05001243 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001244 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001245 $CI =& get_instance();
1246 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001247 }
1248
1249 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001250
Derek Jones32bf1862010-03-02 13:46:07 -06001251 /**
1252 * Prep filename
1253 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001254 * This function prepares filenames of various items to
1255 * make their loading more reliable.
Derek Jones32bf1862010-03-02 13:46:07 -06001256 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001257 * @param string|string[] $filename Filename(s)
1258 * @param string $extension Filename extension
Derek Jones32bf1862010-03-02 13:46:07 -06001259 * @return array
1260 */
Greg Akerf5c84022011-04-19 17:13:03 -05001261 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001262 {
1263 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001264 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001265 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001266 }
1267 else
1268 {
1269 foreach ($filename as $key => $val)
1270 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001271 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001272 }
Barry Mienydd671972010-10-04 16:33:58 +02001273
Derek Jones32bf1862010-03-02 13:46:07 -06001274 return $filename;
1275 }
1276 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001277
Derek Allard2067d1a2008-11-13 22:59:24 +00001278}
1279
1280/* End of file Loader.php */
Andrey Andreev9438e262012-10-05 13:16:27 +03001281/* Location: ./system/core/Loader.php */