blob: 88fbdb6e13c6f0e6f50fcff6faeb65ddbc61f6f4 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevd7297352012-01-07 22:53:14 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevd7297352012-01-07 22:53:14 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
Derek Allard2067d1a2008-11-13 22:59:24 +000018 *
19 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Loader Class
31 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030032 * Loads framework components.
Derek Allard2067d1a2008-11-13 22:59:24 +000033 *
34 * @package CodeIgniter
35 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @category Loader
Andrey Andreev92ebfb62012-05-17 12:49:24 +030037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/loader.html
39 */
40class CI_Loader {
41
42 // All these are set automatically. Don't mess with them.
David Behlercda768a2011-08-14 23:52:48 +020043 /**
44 * Nesting level of the output buffering mechanism
45 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030046 * @var int
David Behlercda768a2011-08-14 23:52:48 +020047 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050048 protected $_ci_ob_level;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030049
David Behlercda768a2011-08-14 23:52:48 +020050 /**
51 * List of paths to load views from
52 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030053 * @var array
David Behlercda768a2011-08-14 23:52:48 +020054 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040055 protected $_ci_view_paths = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030056
David Behlercda768a2011-08-14 23:52:48 +020057 /**
58 * List of paths to load libraries from
59 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030060 * @var array
David Behlercda768a2011-08-14 23:52:48 +020061 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040062 protected $_ci_library_paths = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030063
David Behlercda768a2011-08-14 23:52:48 +020064 /**
65 * List of paths to load models from
66 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030067 * @var array
David Behlercda768a2011-08-14 23:52:48 +020068 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040069 protected $_ci_model_paths = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030070
David Behlercda768a2011-08-14 23:52:48 +020071 /**
72 * List of paths to load helpers from
73 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030074 * @var array
David Behlercda768a2011-08-14 23:52:48 +020075 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040076 protected $_ci_helper_paths = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030077
David Behlercda768a2011-08-14 23:52:48 +020078 /**
79 * List of loaded base classes
David Behlercda768a2011-08-14 23:52:48 +020080 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030081 * @var array
David Behlercda768a2011-08-14 23:52:48 +020082 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040083 protected $_base_classes = array(); // Set by the controller class
Andrey Andreev92ebfb62012-05-17 12:49:24 +030084
David Behlercda768a2011-08-14 23:52:48 +020085 /**
86 * List of cached variables
87 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030088 * @var array
David Behlercda768a2011-08-14 23:52:48 +020089 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040090 protected $_ci_cached_vars = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030091
David Behlercda768a2011-08-14 23:52:48 +020092 /**
93 * List of loaded classes
94 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030095 * @var array
David Behlercda768a2011-08-14 23:52:48 +020096 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040097 protected $_ci_classes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030098
David Behlercda768a2011-08-14 23:52:48 +020099 /**
100 * List of loaded files
101 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300102 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200103 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400104 protected $_ci_loaded_files = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300105
David Behlercda768a2011-08-14 23:52:48 +0200106 /**
107 * List of loaded models
108 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300109 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200110 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400111 protected $_ci_models = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300112
David Behlercda768a2011-08-14 23:52:48 +0200113 /**
114 * List of loaded helpers
115 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300116 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200117 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400118 protected $_ci_helpers = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300119
David Behlercda768a2011-08-14 23:52:48 +0200120 /**
121 * List of class name mappings
122 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300123 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200124 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400125 protected $_ci_varmap = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400126 'unit_test' => 'unit',
127 'user_agent' => 'agent'
128 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000129
130 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300131 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 *
Andrey Andreevcdac2482012-11-03 18:09:01 +0200133 * Sets component load paths, gets the initial output buffering level.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300134 *
135 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 */
Greg Akerf5c84022011-04-19 17:13:03 -0500137 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +0200138 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500139 $this->_ci_ob_level = ob_get_level();
Derek Jones32bf1862010-03-02 13:46:07 -0600140 $this->_ci_library_paths = array(APPPATH, BASEPATH);
141 $this->_ci_helper_paths = array(APPPATH, BASEPATH);
142 $this->_ci_model_paths = array(APPPATH);
Joe Cianflone8eef9c72011-08-21 10:39:06 -0400143 $this->_ci_view_paths = array(VIEWPATH => TRUE);
David Behlercda768a2011-08-14 23:52:48 +0200144
Andrey Andreevd7297352012-01-07 22:53:14 +0200145 log_message('debug', 'Loader Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200149
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500150 /**
Andrey Andreevcdac2482012-11-03 18:09:01 +0200151 * Initializer
152 *
153 * @todo Figure out a way to move this to the constructor
154 * without breaking *package_path*() methods.
155 * @uses CI_Loader::_ci_autoloader()
156 * @used-by CI_Controller::__construct()
157 * @return void
158 */
159 public function initialize()
160 {
161 $this->_base_classes =& is_loaded();
162 $this->_ci_autoloader();
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500168 * Is Loaded
169 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300170 * A utility method to test if a class is in the self::$_ci_classes array.
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500171 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300172 * @used-by Mainly used by Form Helper function _get_validation_object().
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500173 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300174 * @param string $class Class name to check for
175 * @return string|bool Class object name if loaded or FALSE
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500176 */
177 public function is_loaded($class)
178 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300179 return isset($this->_ci_classes[$class]) ? $this->_ci_classes[$class] : FALSE;
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500180 }
181
182 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300185 * Library Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300187 * Loads and instantiates libraries.
188 * Designed to be called from application controllers.
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300190 * @param string $library Library name
191 * @param array $params Optional parameters to pass to the library class constructor
192 * @param string $object_name An optional object name to assign to
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200194 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500195 public function library($library = '', $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
Greg Akerce433962010-10-12 09:29:35 -0500197 if (is_array($library))
198 {
Phil Sturgeon08b51692011-04-03 18:05:42 +0100199 foreach ($library as $class)
Greg Akerce433962010-10-12 09:29:35 -0500200 {
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600201 $this->library($class, $params);
Greg Akerce433962010-10-12 09:29:35 -0500202 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000203
Greg Akerce433962010-10-12 09:29:35 -0500204 return;
205 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000206
Alex Bilbieed944a32012-06-02 11:07:47 +0100207 if ($library === '' OR isset($this->_base_classes[$library]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300209 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 }
211
Derek Jones32bf1862010-03-02 13:46:07 -0600212 if ( ! is_null($params) && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
214 $params = NULL;
215 }
216
Kellas Reeves3c6e4852011-02-09 11:57:56 -0600217 $this->_ci_load_class($library, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
219
220 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 /**
223 * Model Loader
224 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300225 * Loads and instantiates libraries.
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300227 * @param string $model Model name
228 * @param string $name An optional object name to assign to
229 * @param bool $db_conn An optional database connection configuration to initialize
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200231 */
Greg Akerf5c84022011-04-19 17:13:03 -0500232 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200233 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300234 if (empty($model))
235 {
236 return;
237 }
238 elseif (is_array($model))
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
Joel Limberg3cf174b2012-07-13 20:49:57 +0300240 foreach ($model as $class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Joel Limberg3cf174b2012-07-13 20:49:57 +0300242 $this->model($class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
244 return;
245 }
246
Derek Jones32bf1862010-03-02 13:46:07 -0600247 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600250 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
Derek Jones32bf1862010-03-02 13:46:07 -0600252 // The path is in front of the last slash
Andrey Andreevd47baab2012-01-09 16:56:46 +0200253 $path = substr($model, 0, ++$last_slash);
Derek Jones32bf1862010-03-02 13:46:07 -0600254
255 // And the model name behind it
Andrey Andreevd47baab2012-01-09 16:56:46 +0200256 $model = substr($model, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
Barry Mienydd671972010-10-04 16:33:58 +0200258
Phil Sturgeon10d78f62012-06-04 14:41:53 -0500259 if (empty($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
261 $name = $model;
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 if (in_array($name, $this->_ci_models, TRUE))
265 {
266 return;
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 $CI =& get_instance();
270 if (isset($CI->$name))
271 {
272 show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
273 }
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 $model = strtolower($model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000276
Derek Jones32bf1862010-03-02 13:46:07 -0600277 foreach ($this->_ci_model_paths as $mod_path)
278 {
Greg Aker3a746652011-04-19 10:59:47 -0500279 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
Derek Jones32bf1862010-03-02 13:46:07 -0600280 {
281 continue;
282 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000283
Andrey Andreev94af3552012-03-26 23:10:42 +0300284 if ($db_conn !== FALSE && ! class_exists('CI_DB'))
Derek Jones32bf1862010-03-02 13:46:07 -0600285 {
286 if ($db_conn === TRUE)
Pascal Kriete287781e2010-11-10 15:43:49 -0500287 {
Derek Jones32bf1862010-03-02 13:46:07 -0600288 $db_conn = '';
Pascal Kriete287781e2010-11-10 15:43:49 -0500289 }
Derek Jones32bf1862010-03-02 13:46:07 -0600290
291 $CI->load->database($db_conn, FALSE, TRUE);
292 }
293
Greg Akerbce13482010-10-11 15:37:16 -0500294 if ( ! class_exists('CI_Model'))
Derek Jones32bf1862010-03-02 13:46:07 -0600295 {
296 load_class('Model', 'core');
297 }
298
Greg Aker3a746652011-04-19 10:59:47 -0500299 require_once($mod_path.'models/'.$path.$model.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600300
301 $model = ucfirst($model);
Derek Jones32bf1862010-03-02 13:46:07 -0600302 $CI->$name = new $model();
Derek Jones32bf1862010-03-02 13:46:07 -0600303 $this->_ci_models[] = $name;
304 return;
305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Jones32bf1862010-03-02 13:46:07 -0600307 // couldn't find the model
308 show_error('Unable to locate the model you have specified: '.$model);
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 /**
314 * Database Loader
315 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300316 * @param mixed $params Database configuration options
317 * @param bool $return Whether to return the database object
318 * @param bool $query_builder Whether to enable Query Builder
319 * (overrides the configuration setting)
320 *
321 * @return void|object|bool Database object if $return is set to TRUE,
322 * FALSE on failure, void in any other case
Barry Mienydd671972010-10-04 16:33:58 +0200323 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000324 public function database($params = '', $return = FALSE, $query_builder = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
326 // Grab the super object
327 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 // Do we even need to load the database class?
Andrey Andreev9d0ab042012-10-24 21:47:39 +0300330 if ($return === FALSE && $query_builder === NULL && isset($CI->db) && is_object($CI->db) && ! empty($CI->db->conn_id))
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200333 }
334
Greg Aker3a746652011-04-19 10:59:47 -0500335 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000336
337 if ($return === TRUE)
338 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000339 return DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Andrey Andreevd7297352012-01-07 22:53:14 +0200342 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 // reference errors with some configurations
344 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // Load the DB class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000347 $CI->db =& DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // --------------------------------------------------------------------
351
352 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300353 * Load the Database Utilities Class
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300355 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200356 */
Greg Akerf5c84022011-04-19 17:13:03 -0500357 public function dbutil()
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
359 if ( ! class_exists('CI_DB'))
360 {
361 $this->database();
362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 $CI =& get_instance();
365
366 // for backwards compatibility, load dbforge so we can extend dbutils off it
367 // this use is deprecated and strongly discouraged
368 $CI->load->dbforge();
Barry Mienydd671972010-10-04 16:33:58 +0200369
Greg Aker3a746652011-04-19 10:59:47 -0500370 require_once(BASEPATH.'database/DB_utility.php');
371 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
373
Pascal Kriete58560022010-11-10 16:01:20 -0500374 $CI->dbutil = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // --------------------------------------------------------------------
378
379 /**
380 * Load the Database Forge Class
381 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300382 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200383 */
Greg Akerf5c84022011-04-19 17:13:03 -0500384 public function dbforge()
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
386 if ( ! class_exists('CI_DB'))
387 {
388 $this->database();
389 }
Barry Mienydd671972010-10-04 16:33:58 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200392
Greg Aker3a746652011-04-19 10:59:47 -0500393 require_once(BASEPATH.'database/DB_forge.php');
394 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
396
397 $CI->dbforge = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300403 * View Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300405 * Loads "view" files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300407 * @param string $view View name
408 * @param array $vars An associative array of data
409 * to be extracted for use in the view
410 * @param bool $return Whether to return the view output
411 * or leave it to the Output class
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 * @return void
413 */
Greg Akerf5c84022011-04-19 17:13:03 -0500414 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
416 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300422 * Generic File Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300424 * @param string $path File path
425 * @param bool $return Whether to return the file output
426 * @return void|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 */
Greg Akerf5c84022011-04-19 17:13:03 -0500428 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
430 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
431 }
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 /**
436 * Set Variables
437 *
438 * Once variables are set they become available within
439 * the controller class and its "view" files.
440 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300441 * @param array|object|string $vars
442 * An associative array or object containing values
443 * to be set, or a value's name if string
444 * @param string $val Value to set, only used if $vars is a string
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 * @return void
446 */
Greg Akerf5c84022011-04-19 17:13:03 -0500447 public function vars($vars = array(), $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100449 if ($val !== '' && is_string($vars))
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
451 $vars = array($vars => $val);
452 }
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200455
Andrey Andreev94af3552012-03-26 23:10:42 +0300456 if (is_array($vars) && count($vars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
458 foreach ($vars as $key => $val)
459 {
460 $this->_ci_cached_vars[$key] = $val;
461 }
462 }
463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600468 * Get Variable
469 *
470 * Check if a variable is set and retrieve it.
471 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300472 * @param string $key Variable name
473 * @return mixed The variable or NULL if not found
Phil Sturgeon8731f642011-07-22 16:11:34 -0600474 */
475 public function get_var($key)
476 {
477 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
478 }
479
480 // --------------------------------------------------------------------
481
482 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600483 * Get Variables
484 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300485 * Retrieves all loaded variables.
Shane Pearson81dd2232011-11-18 20:49:35 -0600486 *
487 * @return array
488 */
489 public function get_vars()
490 {
491 return $this->_ci_cached_vars;
492 }
493
494 // --------------------------------------------------------------------
495
496 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300497 * Helper Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300499 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 * @return void
501 */
Greg Akerf5c84022011-04-19 17:13:03 -0500502 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200503 {
Derek Jones32bf1862010-03-02 13:46:07 -0600504 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200505 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 if (isset($this->_ci_helpers[$helper]))
507 {
508 continue;
509 }
Derek Jones32bf1862010-03-02 13:46:07 -0600510
Greg Aker3a746652011-04-19 10:59:47 -0500511 $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000512
Barry Mienydd671972010-10-04 16:33:58 +0200513 // Is this a helper extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 if (file_exists($ext_helper))
515 {
Greg Aker3a746652011-04-19 10:59:47 -0500516 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 if ( ! file_exists($base_helper))
519 {
Greg Aker3a746652011-04-19 10:59:47 -0500520 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 include_once($ext_helper);
524 include_once($base_helper);
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Jones32bf1862010-03-02 13:46:07 -0600526 $this->_ci_helpers[$helper] = TRUE;
527 log_message('debug', 'Helper loaded: '.$helper);
528 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 }
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Jones32bf1862010-03-02 13:46:07 -0600531 // Try to load the helper
532 foreach ($this->_ci_helper_paths as $path)
533 {
Greg Aker3a746652011-04-19 10:59:47 -0500534 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200535 {
Greg Aker3a746652011-04-19 10:59:47 -0500536 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600537
538 $this->_ci_helpers[$helper] = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200539 log_message('debug', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600540 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 }
542 }
543
Derek Jones32bf1862010-03-02 13:46:07 -0600544 // unable to load the helper
545 if ( ! isset($this->_ci_helpers[$helper]))
546 {
Greg Aker3a746652011-04-19 10:59:47 -0500547 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 /**
555 * Load Helpers
556 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300557 * An alias for the helper() method in case the developer has
558 * written the plural form of it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300560 * @uses CI_Loader::helper()
561 * @param string|string[] $helpers Helper name(s)
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 * @return void
563 */
Greg Akerf5c84022011-04-19 17:13:03 -0500564 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 {
566 $this->helper($helpers);
567 }
Barry Mienydd671972010-10-04 16:33:58 +0200568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300572 * Language Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300574 * Loads language files.
575 *
576 * @param string|string[] $files List of language file names to load
577 * @param string Language name
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 * @return void
579 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300580 public function language($files = array(), $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
582 $CI =& get_instance();
583
Andrey Andreeved4b2582012-10-27 17:46:52 +0300584 is_array($files) OR $files = array($files);
Derek Allard2067d1a2008-11-13 22:59:24 +0000585
Andrey Andreeved4b2582012-10-27 17:46:52 +0300586 foreach ($files as $langfile)
Barry Mienydd671972010-10-04 16:33:58 +0200587 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 $CI->lang->load($langfile, $lang);
589 }
590 }
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200593
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300595 * Config Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300597 * Loads a config file (an alias for CI_Config::load()).
598 *
599 * @uses CI_Config::load()
600 * @param string $file Configuration file name
601 * @param bool $use_sections Whether configuration values should be loaded into their own section
602 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
603 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 */
Greg Akerf5c84022011-04-19 17:13:03 -0500605 public function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200606 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 $CI =& get_instance();
Andrey Andreeved4b2582012-10-27 17:46:52 +0300608 return $CI->config->load($file, $use_sections, $fail_gracefully);
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 }
610
611 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300614 * Driver Loader
Derek Jones8dca0412010-03-05 13:01:44 -0600615 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300616 * Loads a driver library.
Derek Jones8dca0412010-03-05 13:01:44 -0600617 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300618 * @param string|string[] $library Driver name(s)
619 * @param array $params Optional parameters to pass to the driver
620 * @param string $object_name An optional object name to assign to
621 *
622 * @return void|object|bool Object or FALSE on failure if $library is a string
623 * and $object_name is set. void otherwise.
Derek Jones8dca0412010-03-05 13:01:44 -0600624 */
Greg Akerf5c84022011-04-19 17:13:03 -0500625 public function driver($library = '', $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600626 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800627 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800628 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800629 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800630 {
631 $this->driver($driver);
632 }
dchill420fc3be52012-08-27 20:54:23 -0400633 return;
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800634 }
635
Alex Bilbieed944a32012-06-02 11:07:47 +0100636 if ($library === '')
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200637 {
638 return FALSE;
639 }
640
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600641 // We can save the loader some time since Drivers will *always* be in a subfolder,
642 // and typically identically named to the library
643 if ( ! strpos($library, '/'))
644 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500645 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600646 }
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Jones8dca0412010-03-05 13:01:44 -0600648 return $this->library($library, $params, $object_name);
649 }
650
651 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Jones8dca0412010-03-05 13:01:44 -0600653 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600654 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300656 * Prepends a parent path to the library, model, helper and config
657 * path arrays.
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300659 * @see CI_Loader::$_ci_library_paths
660 * @see CI_Loader::$_ci_model_paths
661 * @see CI_Loader::$_ci_helper_paths
662 * @see CI_Config::$_config_paths
663 *
664 * @param string $path Path to add
665 * @param bool $view_cascade (default: TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600667 */
Andrey Andreevcce91802012-06-12 13:25:31 +0300668 public function add_package_path($path, $view_cascade = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600669 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500670 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200671
Derek Jones32bf1862010-03-02 13:46:07 -0600672 array_unshift($this->_ci_library_paths, $path);
673 array_unshift($this->_ci_model_paths, $path);
674 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200675
Greg Akerf5c84022011-04-19 17:13:03 -0500676 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
677
Derek Jones32bf1862010-03-02 13:46:07 -0600678 // Add config file path
679 $config =& $this->_ci_get_component('config');
Korri3684d342012-04-17 00:35:08 -0400680 array_push($config->_config_paths, $path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
682
683 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600684
685 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000686 * Get Package Paths
687 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300688 * Return a list of all package paths.
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000689 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300690 * @param bool $include_base Whether to include BASEPATH (default: TRUE)
691 * @return array
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000692 */
Greg Akerf5c84022011-04-19 17:13:03 -0500693 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000694 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300695 return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000696 }
697
698 // --------------------------------------------------------------------
699
700 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600701 * Remove Package Path
702 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300703 * Remove a path from the library, model, helper and/or config
704 * path arrays if it exists. If no path is provided, the most recently
705 * added path will be removed removed.
Derek Jones32bf1862010-03-02 13:46:07 -0600706 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300707 * @param string $path Path to remove
Andrey Andreev94af3552012-03-26 23:10:42 +0300708 * @return void
Derek Jones32bf1862010-03-02 13:46:07 -0600709 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300710 public function remove_package_path($path = '')
Derek Jones32bf1862010-03-02 13:46:07 -0600711 {
712 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200713
Alex Bilbieed944a32012-06-02 11:07:47 +0100714 if ($path === '')
Derek Jones32bf1862010-03-02 13:46:07 -0600715 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200716 array_shift($this->_ci_library_paths);
717 array_shift($this->_ci_model_paths);
718 array_shift($this->_ci_helper_paths);
719 array_shift($this->_ci_view_paths);
Korri3684d342012-04-17 00:35:08 -0400720 array_pop($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600721 }
722 else
723 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500724 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600725 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
726 {
727 if (($key = array_search($path, $this->{$var})) !== FALSE)
728 {
729 unset($this->{$var}[$key]);
730 }
731 }
David Behlercda768a2011-08-14 23:52:48 +0200732
Greg Akerf5c84022011-04-19 17:13:03 -0500733 if (isset($this->_ci_view_paths[$path.'views/']))
734 {
735 unset($this->_ci_view_paths[$path.'views/']);
736 }
Barry Mienydd671972010-10-04 16:33:58 +0200737
Derek Jones32bf1862010-03-02 13:46:07 -0600738 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
739 {
740 unset($config->_config_paths[$key]);
741 }
742 }
Barry Mienydd671972010-10-04 16:33:58 +0200743
Derek Jones32bf1862010-03-02 13:46:07 -0600744 // make sure the application default paths are still in the array
745 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
746 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
747 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500748 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600749 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
750 }
751
752 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200753
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300755 * Internal CI Data Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300757 * Used to load views and files.
758 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 * Variables are prefixed with _ci_ to avoid symbol collision with
Andrey Andreeved4b2582012-10-27 17:46:52 +0300760 * variables made available to view files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300762 * @used-by CI_Loader::view()
763 * @used-by CI_Loader::file()
764 * @param array $_ci_data Data to load
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 * @return void
766 */
Greg Akerf5c84022011-04-19 17:13:03 -0500767 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 {
769 // Set the default data variables
770 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
771 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300772 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 }
David Behlercda768a2011-08-14 23:52:48 +0200774
Greg Akerf5c84022011-04-19 17:13:03 -0500775 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000776
777 // Set the path to the requested file
Alex Bilbie40bd2a72012-06-02 16:04:15 +0100778 if (is_string($_ci_path) && $_ci_path !== '')
Greg Aker8807be32011-04-21 13:06:15 -0500779 {
780 $_ci_x = explode('/', $_ci_path);
781 $_ci_file = end($_ci_x);
782 }
783 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 {
785 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Alex Bilbieed944a32012-06-02 11:07:47 +0100786 $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500787
Joe McFrederick64f470b2012-08-18 12:29:56 -0400788 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
Greg Akerf5c84022011-04-19 17:13:03 -0500789 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400790 if (file_exists($_ci_view_file.$_ci_file))
Greg Akerf5c84022011-04-19 17:13:03 -0500791 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400792 $_ci_path = $_ci_view_file.$_ci_file;
Greg Akerf5c84022011-04-19 17:13:03 -0500793 $file_exists = TRUE;
794 break;
795 }
David Behlercda768a2011-08-14 23:52:48 +0200796
Greg Akerf5c84022011-04-19 17:13:03 -0500797 if ( ! $cascade)
798 {
799 break;
David Behlercda768a2011-08-14 23:52:48 +0200800 }
Greg Akerf5c84022011-04-19 17:13:03 -0500801 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Greg Akerf5c84022011-04-19 17:13:03 -0500804 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 {
806 show_error('Unable to load the requested file: '.$_ci_file);
807 }
Barry Mienydd671972010-10-04 16:33:58 +0200808
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 // This allows anything loaded using $this->load (views, files, etc.)
810 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500811 $_ci_CI =& get_instance();
812 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500814 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500816 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 }
818 }
819
820 /*
821 * Extract and cache variables
822 *
vlakoff02506182012-07-03 07:28:50 +0200823 * You can either set variables using the dedicated $this->load->vars()
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 * function or via the second parameter of this function. We'll merge
825 * the two types and cache them so that views that are embedded within
826 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200827 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 if (is_array($_ci_vars))
829 {
830 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
831 }
832 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 /*
835 * Buffer the output
836 *
837 * We buffer the output for two reasons:
838 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200839 * 2. So that the final rendered template can be post-processed by
dchill425628ba02012-08-08 12:05:45 -0400840 * the output class. Why do we need post processing? For one thing,
841 * in order to show the elapsed page load time. Unless we can
842 * intercept the content right before it's sent to the browser and
843 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 */
845 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200846
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 // If the PHP installation does not support short tags we'll
848 // do a little string replacement, changing the short tags
849 // to standard PHP echo statements.
Alex Bilbieed944a32012-06-02 11:07:47 +0100850 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 +0000851 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200852 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 }
854 else
855 {
856 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
857 }
Barry Mienydd671972010-10-04 16:33:58 +0200858
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 log_message('debug', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 // Return the file data if requested
862 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200863 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 $buffer = ob_get_contents();
865 @ob_end_clean();
866 return $buffer;
867 }
868
869 /*
870 * Flush the buffer... or buff the flusher?
871 *
872 * In order to permit views to be nested within
873 * other views, we need to flush the content back out whenever
874 * we are beyond the first level of output buffering so that
875 * it can be seen and included properly by the first included
876 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200877 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 if (ob_get_level() > $this->_ci_ob_level + 1)
879 {
880 ob_end_flush();
881 }
882 else
883 {
Greg Aker22f1a632010-11-10 15:34:35 -0600884 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 @ob_end_clean();
886 }
887 }
888
889 // --------------------------------------------------------------------
890
891 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300892 * Internal CI Class Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300894 * @used-by CI_Loader::library()
895 * @uses CI_Loader::_ci_init_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300897 * @param string $class Class name to load
898 * @param mixed $params Optional parameters to pass to the class constructor
899 * @param string $object_name Optional object name to assign to
Barry Mienydd671972010-10-04 16:33:58 +0200900 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 */
Greg Akerf5c84022011-04-19 17:13:03 -0500902 protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200903 {
904 // Get the class name, and while we're at it trim any slashes.
905 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500907 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200908
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 // Was the path included with the class name?
910 // We look for a slash to determine this
911 $subdir = '';
Derek Jones32bf1862010-03-02 13:46:07 -0600912 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
Derek Jones32bf1862010-03-02 13:46:07 -0600914 // Extract the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200915 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200916
Derek Jones32bf1862010-03-02 13:46:07 -0600917 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200918 $class = substr($class, $last_slash);
dchill425628ba02012-08-08 12:05:45 -0400919
920 // Check for match and driver base class
dchill42aee92652012-08-26 21:45:35 -0400921 if (strtolower(trim($subdir, '/')) == strtolower($class) && ! class_exists('CI_Driver_Library'))
dchill425628ba02012-08-08 12:05:45 -0400922 {
923 // We aren't instantiating an object here, just making the base class available
924 require BASEPATH.'libraries/Driver.php';
925 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 }
927
928 // We'll test for both lowercase and capitalized versions of the file name
929 foreach (array(ucfirst($class), strtolower($class)) as $class)
930 {
Greg Aker3a746652011-04-19 10:59:47 -0500931 $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000932
Barry Mienydd671972010-10-04 16:33:58 +0200933 // Is this a class extension request?
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 if (file_exists($subclass))
935 {
Greg Aker3a746652011-04-19 10:59:47 -0500936 $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
Barry Mienydd671972010-10-04 16:33:58 +0200937
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 if ( ! file_exists($baseclass))
939 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200940 log_message('error', 'Unable to load the requested class: '.$class);
941 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 }
943
Andrey Andreevd7297352012-01-07 22:53:14 +0200944 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 if (in_array($subclass, $this->_ci_loaded_files))
946 {
947 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200948 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 // return a new instance of the object
950 if ( ! is_null($object_name))
951 {
952 $CI =& get_instance();
953 if ( ! isset($CI->$object_name))
954 {
Barry Mienydd671972010-10-04 16:33:58 +0200955 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200960 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 return;
962 }
Barry Mienydd671972010-10-04 16:33:58 +0200963
964 include_once($baseclass);
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 include_once($subclass);
966 $this->_ci_loaded_files[] = $subclass;
Barry Mienydd671972010-10-04 16:33:58 +0200967
968 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 }
Barry Mienydd671972010-10-04 16:33:58 +0200970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 // Lets search for the requested library file and load it.
Derek Jones32bf1862010-03-02 13:46:07 -0600972 $is_duplicate = FALSE;
973 foreach ($this->_ci_library_paths as $path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 {
Greg Aker3a746652011-04-19 10:59:47 -0500975 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Derek Jones32bf1862010-03-02 13:46:07 -0600976
Andrey Andreevd7297352012-01-07 22:53:14 +0200977 // Does the file exist? No? Bummer...
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 if ( ! file_exists($filepath))
979 {
980 continue;
981 }
Barry Mienydd671972010-10-04 16:33:58 +0200982
Andrey Andreevd7297352012-01-07 22:53:14 +0200983 // Safety: Was the class already loaded by a previous call?
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 if (in_array($filepath, $this->_ci_loaded_files))
985 {
986 // Before we deem this to be a duplicate request, let's see
Andrey Andreevd7297352012-01-07 22:53:14 +0200987 // if a custom object name is being supplied. If so, we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 // return a new instance of the object
989 if ( ! is_null($object_name))
990 {
991 $CI =& get_instance();
992 if ( ! isset($CI->$object_name))
993 {
994 return $this->_ci_init_class($class, '', $params, $object_name);
995 }
996 }
Barry Mienydd671972010-10-04 16:33:58 +0200997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 $is_duplicate = TRUE;
Andrey Andreevd7297352012-01-07 22:53:14 +0200999 log_message('debug', $class.' class already loaded. Second attempt ignored.');
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 return;
1001 }
Barry Mienydd671972010-10-04 16:33:58 +02001002
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 include_once($filepath);
1004 $this->_ci_loaded_files[] = $filepath;
Barry Mienydd671972010-10-04 16:33:58 +02001005 return $this->_ci_init_class($class, '', $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 }
1007 } // END FOREACH
1008
Andrey Andreevd7297352012-01-07 22:53:14 +02001009 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Alex Bilbieed944a32012-06-02 11:07:47 +01001010 if ($subdir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 {
1012 $path = strtolower($class).'/'.$class;
dchill420fc3be52012-08-27 20:54:23 -04001013 return $this->_ci_load_class($path, $params, $object_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 }
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001015 elseif (ucfirst($subdir) != $subdir)
dchill42aee92652012-08-26 21:45:35 -04001016 {
1017 // Lowercase subdir failed - retry capitalized
1018 $path = ucfirst($subdir).$class;
dchill420fc3be52012-08-27 20:54:23 -04001019 return $this->_ci_load_class($path, $params, $object_name);
dchill42aee92652012-08-26 21:45:35 -04001020 }
Barry Mienydd671972010-10-04 16:33:58 +02001021
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 // If we got this far we were unable to find the requested class.
1023 // We do not issue errors if the load call failed due to a duplicate request
Alex Bilbieed944a32012-06-02 11:07:47 +01001024 if ($is_duplicate === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001026 log_message('error', 'Unable to load the requested class: '.$class);
1027 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 }
1029 }
Barry Mienydd671972010-10-04 16:33:58 +02001030
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 // --------------------------------------------------------------------
1032
1033 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001034 * Internal CI Class Instantiator
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001036 * @used-by CI_Loader::_ci_load_class()
1037 *
1038 * @param string $class Class name
1039 * @param string $prefix Class name prefix
1040 * @param array|null|bool $config Optional configuration to pass to the class constructor:
1041 * FALSE to skip;
1042 * NULL to search in config paths;
1043 * array containing configuration data
1044 * @param string $object_name Optional object name to assign to
Andrey Andreev94af3552012-03-26 23:10:42 +03001045 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001047 protected function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001048 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001049 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 if ($config === NULL)
1051 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001052 // Fetch the config paths containing any package paths
1053 $config_component = $this->_ci_get_component('config');
1054
1055 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001057 // Break on the first found file, thus package files
1058 // are not overridden by default paths
1059 foreach ($config_component->_config_paths as $path)
1060 {
1061 // We test for both uppercase and lowercase, for servers that
joelcox1bfd9fa2011-01-16 18:49:39 +01001062 // are case-sensitive with regard to file names. Check for environment
1063 // first, global next
Andrey Andreev94af3552012-03-26 23:10:42 +03001064 if (defined('ENVIRONMENT') && file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001065 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001066 include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001067 break;
1068 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001069 elseif (defined('ENVIRONMENT') && file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
joelcox1bfd9fa2011-01-16 18:49:39 +01001070 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001071 include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
joelcox1bfd9fa2011-01-16 18:49:39 +01001072 break;
1073 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001074 elseif (file_exists($path.'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001075 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001076 include($path.'config/'.strtolower($class).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001077 break;
1078 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001079 elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001080 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001081 include($path.'config/'.ucfirst(strtolower($class)).'.php');
Eric Barnes5e16ec62011-01-04 17:25:23 -05001082 break;
1083 }
1084 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 }
1086 }
Barry Mienydd671972010-10-04 16:33:58 +02001087
Alex Bilbieed944a32012-06-02 11:07:47 +01001088 if ($prefix === '')
Barry Mienydd671972010-10-04 16:33:58 +02001089 {
1090 if (class_exists('CI_'.$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 {
1092 $name = 'CI_'.$class;
1093 }
Barry Mienydd671972010-10-04 16:33:58 +02001094 elseif (class_exists(config_item('subclass_prefix').$class))
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 {
1096 $name = config_item('subclass_prefix').$class;
1097 }
1098 else
1099 {
1100 $name = $class;
1101 }
1102 }
1103 else
1104 {
1105 $name = $prefix.$class;
1106 }
Barry Mienydd671972010-10-04 16:33:58 +02001107
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 // Is the class name valid?
1109 if ( ! class_exists($name))
1110 {
Andrey Andreevd7297352012-01-07 22:53:14 +02001111 log_message('error', 'Non-existent class: '.$name);
jonnueee2df62012-07-16 13:06:16 +01001112 show_error('Non-existent class: '.$name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 }
Barry Mienydd671972010-10-04 16:33:58 +02001114
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001116 // Was a custom class name supplied? If so we'll use it
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 $class = strtolower($class);
Barry Mienydd671972010-10-04 16:33:58 +02001118
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 if (is_null($object_name))
1120 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001121 $classvar = isset($this->_ci_varmap[$class]) ? $this->_ci_varmap[$class] : $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 }
1123 else
1124 {
1125 $classvar = $object_name;
1126 }
1127
Barry Mienydd671972010-10-04 16:33:58 +02001128 // Save the class name and object name
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 $this->_ci_classes[$class] = $classvar;
1130
Barry Mienydd671972010-10-04 16:33:58 +02001131 // Instantiate the class
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 $CI =& get_instance();
1133 if ($config !== NULL)
1134 {
1135 $CI->$classvar = new $name($config);
1136 }
1137 else
Barry Mienydd671972010-10-04 16:33:58 +02001138 {
Andrey Andreeva11b16b2012-03-28 12:22:04 +03001139 $CI->$classvar = new $name();
Barry Mienydd671972010-10-04 16:33:58 +02001140 }
1141 }
1142
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001146 * CI Autoloader
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001148 * Loads component listed in the config/autoload.php file.
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 *
Andrey Andreevcdac2482012-11-03 18:09:01 +02001150 * @used-by CI_Loader::initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 * @return void
1152 */
Shane Pearson665baec2011-08-22 18:52:19 -05001153 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001154 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001155 if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001156 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001157 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001158 }
1159 else
1160 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001161 include(APPPATH.'config/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001162 }
Barry Mienydd671972010-10-04 16:33:58 +02001163
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 if ( ! isset($autoload))
1165 {
1166 return FALSE;
1167 }
Barry Mienydd671972010-10-04 16:33:58 +02001168
Phil Sturgeon9730c752010-12-15 10:50:15 +00001169 // Autoload packages
1170 if (isset($autoload['packages']))
1171 {
1172 foreach ($autoload['packages'] as $package_path)
1173 {
1174 $this->add_package_path($package_path);
1175 }
1176 }
1177
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 // Load any custom config file
1179 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001180 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 $CI =& get_instance();
1182 foreach ($autoload['config'] as $key => $val)
1183 {
1184 $CI->config->load($val);
1185 }
Barry Mienydd671972010-10-04 16:33:58 +02001186 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001187
Derek Jonesc6da5032010-03-09 20:44:27 -06001188 // Autoload helpers and languages
1189 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001190 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001191 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 {
1193 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001194 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 }
1196
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001198 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 {
1200 // Load the database driver.
1201 if (in_array('database', $autoload['libraries']))
1202 {
1203 $this->database();
1204 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1205 }
Barry Mienydd671972010-10-04 16:33:58 +02001206
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 // Load all other libraries
1208 foreach ($autoload['libraries'] as $item)
1209 {
1210 $this->library($item);
1211 }
Barry Mienydd671972010-10-04 16:33:58 +02001212 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001213
Darren Hillc4e266b2011-08-30 15:40:27 -04001214 // Autoload drivers
1215 if (isset($autoload['drivers']))
1216 {
Darren Hillca3be1d2011-08-31 08:31:18 -04001217 foreach ($autoload['drivers'] as $item)
1218 {
1219 $this->driver($item);
1220 }
Darren Hillc4e266b2011-08-30 15:40:27 -04001221 }
1222
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 // Autoload models
1224 if (isset($autoload['model']))
1225 {
1226 $this->model($autoload['model']);
1227 }
Barry Mienydd671972010-10-04 16:33:58 +02001228 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001229
1230 // --------------------------------------------------------------------
1231
1232 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001233 * CI Object to Array translator
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001235 * Takes an object as input and converts the class variables to
1236 * an associative array with key/value pairs.
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001238 * @param object $object Object data to translate
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 * @return array
1240 */
Greg Akerf5c84022011-04-19 17:13:03 -05001241 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001243 return is_object($object) ? get_object_vars($object) : $object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 }
1245
1246 // --------------------------------------------------------------------
1247
1248 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001249 * CI Component getter
Derek Jones32bf1862010-03-02 13:46:07 -06001250 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001251 * Get a reference to a specific library or model.
1252 *
1253 * @param string $component Component name
Derek Jones32bf1862010-03-02 13:46:07 -06001254 * @return bool
1255 */
Greg Akerf5c84022011-04-19 17:13:03 -05001256 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001257 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001258 $CI =& get_instance();
1259 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001260 }
1261
1262 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001263
Derek Jones32bf1862010-03-02 13:46:07 -06001264 /**
1265 * Prep filename
1266 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001267 * This function prepares filenames of various items to
1268 * make their loading more reliable.
Derek Jones32bf1862010-03-02 13:46:07 -06001269 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001270 * @param string|string[] $filename Filename(s)
1271 * @param string $extension Filename extension
Derek Jones32bf1862010-03-02 13:46:07 -06001272 * @return array
1273 */
Greg Akerf5c84022011-04-19 17:13:03 -05001274 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001275 {
1276 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001277 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001278 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001279 }
1280 else
1281 {
1282 foreach ($filename as $key => $val)
1283 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001284 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001285 }
Barry Mienydd671972010-10-04 16:33:58 +02001286
Derek Jones32bf1862010-03-02 13:46:07 -06001287 return $filename;
1288 }
1289 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001290
Derek Allard2067d1a2008-11-13 22:59:24 +00001291}
1292
1293/* End of file Loader.php */
Andrey Andreev9438e262012-10-05 13:16:27 +03001294/* Location: ./system/core/Loader.php */