blob: 18e4c5287a9f9b8862d3e154e0d1fc8ff2959a3d [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevd7297352012-01-07 22:53:14 +02008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Andrey Andreevd7297352012-01-07 22:53:14 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Allard2067d1a2008-11-13 22:59:24 +000017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Loader Class
42 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030043 * Loads framework components.
Derek Allard2067d1a2008-11-13 22:59:24 +000044 *
45 * @package CodeIgniter
46 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @category Loader
Andrey Andreev92ebfb62012-05-17 12:49:24 +030048 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * @link http://codeigniter.com/user_guide/libraries/loader.html
50 */
51class CI_Loader {
52
53 // All these are set automatically. Don't mess with them.
David Behlercda768a2011-08-14 23:52:48 +020054 /**
55 * Nesting level of the output buffering mechanism
56 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030057 * @var int
David Behlercda768a2011-08-14 23:52:48 +020058 */
Greg Aker0c9ee4a2011-04-20 09:40:17 -050059 protected $_ci_ob_level;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030060
David Behlercda768a2011-08-14 23:52:48 +020061 /**
62 * List of paths to load views from
63 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030064 * @var array
David Behlercda768a2011-08-14 23:52:48 +020065 */
Ted Wood4c223642013-01-05 16:50:31 -080066 protected $_ci_view_paths = array(VIEWPATH => TRUE);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030067
David Behlercda768a2011-08-14 23:52:48 +020068 /**
69 * List of paths to load libraries from
70 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030071 * @var array
David Behlercda768a2011-08-14 23:52:48 +020072 */
Ted Wood4c223642013-01-05 16:50:31 -080073 protected $_ci_library_paths = array(APPPATH, BASEPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030074
David Behlercda768a2011-08-14 23:52:48 +020075 /**
76 * List of paths to load models from
77 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030078 * @var array
David Behlercda768a2011-08-14 23:52:48 +020079 */
Ted Wood4c223642013-01-05 16:50:31 -080080 protected $_ci_model_paths = array(APPPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030081
David Behlercda768a2011-08-14 23:52:48 +020082 /**
83 * List of paths to load helpers from
84 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030085 * @var array
David Behlercda768a2011-08-14 23:52:48 +020086 */
Ted Wood4c223642013-01-05 16:50:31 -080087 protected $_ci_helper_paths = array(APPPATH, BASEPATH);
Andrey Andreev92ebfb62012-05-17 12:49:24 +030088
David Behlercda768a2011-08-14 23:52:48 +020089 /**
David Behlercda768a2011-08-14 23:52:48 +020090 * List of cached variables
91 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030092 * @var array
David Behlercda768a2011-08-14 23:52:48 +020093 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040094 protected $_ci_cached_vars = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030095
David Behlercda768a2011-08-14 23:52:48 +020096 /**
97 * List of loaded classes
98 *
Andrey Andreeved4b2582012-10-27 17:46:52 +030099 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200100 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400101 protected $_ci_classes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300102
David Behlercda768a2011-08-14 23:52:48 +0200103 /**
David Behlercda768a2011-08-14 23:52:48 +0200104 * List of loaded models
105 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300106 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200107 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400108 protected $_ci_models = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300109
David Behlercda768a2011-08-14 23:52:48 +0200110 /**
111 * List of loaded helpers
112 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300113 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200114 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400115 protected $_ci_helpers = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300116
David Behlercda768a2011-08-14 23:52:48 +0200117 /**
118 * List of class name mappings
119 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300120 * @var array
David Behlercda768a2011-08-14 23:52:48 +0200121 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400122 protected $_ci_varmap = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400123 'unit_test' => 'unit',
124 'user_agent' => 'agent'
125 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000126
Andrey Andreev519f87a2013-07-23 17:16:10 +0300127 // --------------------------------------------------------------------
128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300130 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 *
Andrey Andreevcdac2482012-11-03 18:09:01 +0200132 * Sets component load paths, gets the initial output buffering level.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300133 *
134 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 */
Greg Akerf5c84022011-04-19 17:13:03 -0500136 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +0200137 {
Andrey Andreev519f87a2013-07-23 17:16:10 +0300138 $this->_ci_ob_level = ob_get_level();
139 $this->_ci_classes =& is_loaded();
David Behlercda768a2011-08-14 23:52:48 +0200140
Andrey Andreev90726b82015-01-20 12:39:22 +0200141 log_message('info', 'Loader Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 // --------------------------------------------------------------------
David Behlercda768a2011-08-14 23:52:48 +0200145
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500146 /**
Andrey Andreevcdac2482012-11-03 18:09:01 +0200147 * Initializer
148 *
149 * @todo Figure out a way to move this to the constructor
150 * without breaking *package_path*() methods.
151 * @uses CI_Loader::_ci_autoloader()
152 * @used-by CI_Controller::__construct()
153 * @return void
154 */
155 public function initialize()
156 {
Andrey Andreevcdac2482012-11-03 18:09:01 +0200157 $this->_ci_autoloader();
158 }
159
160 // --------------------------------------------------------------------
161
162 /**
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500163 * Is Loaded
164 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300165 * A utility method to test if a class is in the self::$_ci_classes array.
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500166 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300167 * @used-by Mainly used by Form Helper function _get_validation_object().
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500168 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300169 * @param string $class Class name to check for
170 * @return string|bool Class object name if loaded or FALSE
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500171 */
172 public function is_loaded($class)
173 {
Andrey Andreev519f87a2013-07-23 17:16:10 +0300174 return array_search(ucfirst($class), $this->_ci_classes, TRUE);
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500175 }
176
177 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300180 * Library Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300182 * Loads and instantiates libraries.
183 * Designed to be called from application controllers.
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300185 * @param string $library Library name
186 * @param array $params Optional parameters to pass to the library class constructor
187 * @param string $object_name An optional object name to assign to
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200188 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200189 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200190 public function library($library, $params = NULL, $object_name = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
Andrey Andreev519f87a2013-07-23 17:16:10 +0300192 if (empty($library))
193 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200194 return $this;
Andrey Andreev519f87a2013-07-23 17:16:10 +0300195 }
196 elseif (is_array($library))
Greg Akerce433962010-10-12 09:29:35 -0500197 {
Andrey Andreev88cf55b2014-01-17 15:38:30 +0200198 foreach ($library as $key => $value)
Greg Akerce433962010-10-12 09:29:35 -0500199 {
Andrey Andreev88cf55b2014-01-17 15:38:30 +0200200 if (is_int($key))
201 {
202 $this->library($value, $params);
203 }
204 else
205 {
206 $this->library($key, $params, $value);
207 }
Greg Akerce433962010-10-12 09:29:35 -0500208 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000209
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200210 return $this;
Greg Akerce433962010-10-12 09:29:35 -0500211 }
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000212
vlakoff1228fe22013-01-14 01:30:09 +0100213 if ($params !== NULL && ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
215 $params = NULL;
216 }
217
Andrey Andreevdb669f12015-01-21 16:51:51 +0200218 $this->_ci_load_library($library, $params, $object_name);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200219 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
221
222 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 /**
225 * Model Loader
226 *
James7b272ff2014-03-20 19:02:37 +0000227 * Loads and instantiates models.
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300229 * @param string $model Model name
230 * @param string $name An optional object name to assign to
231 * @param bool $db_conn An optional database connection configuration to initialize
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200232 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200233 */
Greg Akerf5c84022011-04-19 17:13:03 -0500234 public function model($model, $name = '', $db_conn = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200235 {
Andrey Andreeved4b2582012-10-27 17:46:52 +0300236 if (empty($model))
237 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200238 return $this;
Andrey Andreeved4b2582012-10-27 17:46:52 +0300239 }
240 elseif (is_array($model))
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Andrey Andreev5a519db2013-01-12 04:19:19 +0200242 foreach ($model as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
Ahmad Anbar4005e3c2013-09-12 23:33:28 +0300244 is_int($key) ? $this->model($value, '', $db_conn) : $this->model($key, $value, $db_conn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200246
247 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
249
Derek Jones32bf1862010-03-02 13:46:07 -0600250 $path = '';
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 // Is the model in a sub-folder? If so, parse out the filename and path.
Derek Jones32bf1862010-03-02 13:46:07 -0600253 if (($last_slash = strrpos($model, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
Derek Jones32bf1862010-03-02 13:46:07 -0600255 // The path is in front of the last slash
Andrey Andreevd47baab2012-01-09 16:56:46 +0200256 $path = substr($model, 0, ++$last_slash);
Derek Jones32bf1862010-03-02 13:46:07 -0600257
258 // And the model name behind it
Andrey Andreevd47baab2012-01-09 16:56:46 +0200259 $model = substr($model, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Phil Sturgeon10d78f62012-06-04 14:41:53 -0500262 if (empty($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
264 $name = $model;
265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 if (in_array($name, $this->_ci_models, TRUE))
268 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200269 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 $CI =& get_instance();
273 if (isset($CI->$name))
274 {
Andrey Andreevb63dc192015-07-22 13:14:50 +0300275 throw new RuntimeException('The model name you are loading is the name of a resource that is already being used: '.$name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 }
Barry Mienydd671972010-10-04 16:33:58 +0200277
Andrey Andreev20292312013-07-22 14:29:10 +0300278 if ($db_conn !== FALSE && ! class_exists('CI_DB', FALSE))
279 {
280 if ($db_conn === TRUE)
281 {
282 $db_conn = '';
283 }
284
Andrey Andreev5f839692014-03-24 17:34:54 +0200285 $this->database($db_conn, FALSE, TRUE);
Andrey Andreev20292312013-07-22 14:29:10 +0300286 }
287
288 if ( ! class_exists('CI_Model', FALSE))
289 {
290 load_class('Model', 'core');
291 }
292
Andrey Andreev825fab72015-08-17 09:52:42 +0300293 $model = ucfirst($model);
Andrey Andreevb63dc192015-07-22 13:14:50 +0300294 if ( ! class_exists($model))
Derek Jones32bf1862010-03-02 13:46:07 -0600295 {
Andrey Andreevb63dc192015-07-22 13:14:50 +0300296 foreach ($this->_ci_model_paths as $mod_path)
Derek Jones32bf1862010-03-02 13:46:07 -0600297 {
Andrey Andreevb63dc192015-07-22 13:14:50 +0300298 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
299 {
300 continue;
301 }
302
303 require_once($mod_path.'models/'.$path.$model.'.php');
304 if ( ! class_exists($model, FALSE))
305 {
306 throw new RuntimeException($mod_path."models/".$path.$model.".php exists, but doesn't declare class ".$model);
307 }
308
309 break;
Derek Jones32bf1862010-03-02 13:46:07 -0600310 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000311
Andrey Andreevb63dc192015-07-22 13:14:50 +0300312 if ( ! class_exists($model, FALSE))
313 {
314 throw new RuntimeException('Unable to locate the model you have specified: '.$model);
315 }
316 }
317 elseif ( ! is_subclass_of($model, 'CI_Model'))
318 {
319 throw new RuntimeException("Class ".$model." already exists and doesn't extend CI_Model");
Derek Jones32bf1862010-03-02 13:46:07 -0600320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Andrey Andreevb63dc192015-07-22 13:14:50 +0300322 $this->_ci_models[] = $name;
323 $CI->$name = new $model();
324 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 /**
330 * Database Loader
331 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300332 * @param mixed $params Database configuration options
333 * @param bool $return Whether to return the database object
334 * @param bool $query_builder Whether to enable Query Builder
335 * (overrides the configuration setting)
336 *
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200337 * @return object|bool Database object if $return is set to TRUE,
338 * FALSE on failure, CI_Loader instance in any other case
Barry Mienydd671972010-10-04 16:33:58 +0200339 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000340 public function database($params = '', $return = FALSE, $query_builder = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
342 // Grab the super object
343 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 // Do we even need to load the database class?
Andrey Andreev9d0ab042012-10-24 21:47:39 +0300346 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 +0000347 {
348 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200349 }
350
Greg Aker3a746652011-04-19 10:59:47 -0500351 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000352
353 if ($return === TRUE)
354 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000355 return DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Andrey Andreevd7297352012-01-07 22:53:14 +0200358 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // reference errors with some configurations
360 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // Load the DB class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000363 $CI->db =& DB($params, $query_builder);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200364 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // --------------------------------------------------------------------
368
369 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300370 * Load the Database Utilities Class
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200372 * @param object $db Database object
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200373 * @param bool $return Whether to return the DB Utilities class object or not
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200374 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200375 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200376 public function dbutil($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 $CI =& get_instance();
379
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200380 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
381 {
382 class_exists('CI_DB', FALSE) OR $this->database();
383 $db =& $CI->db;
384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Greg Aker3a746652011-04-19 10:59:47 -0500386 require_once(BASEPATH.'database/DB_utility.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200387 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_utility.php');
388 $class = 'CI_DB_'.$db->dbdriver.'_utility';
Derek Allard2067d1a2008-11-13 22:59:24 +0000389
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200390 if ($return === TRUE)
391 {
392 return new $class($db);
393 }
394
395 $CI->dbutil = new $class($db);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200396 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 // --------------------------------------------------------------------
400
401 /**
402 * Load the Database Forge Class
403 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200404 * @param object $db Database object
405 * @param bool $return Whether to return the DB Forge class object or not
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200406 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200407 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200408 public function dbforge($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200410 $CI =& get_instance();
411 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200413 class_exists('CI_DB', FALSE) OR $this->database();
414 $db =& $CI->db;
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Greg Aker3a746652011-04-19 10:59:47 -0500417 require_once(BASEPATH.'database/DB_forge.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200418 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_forge.php');
Andrey Andreeva287a342012-11-05 23:19:59 +0200419
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200420 if ( ! empty($db->subdriver))
Andrey Andreeva287a342012-11-05 23:19:59 +0200421 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200422 $driver_path = BASEPATH.'database/drivers/'.$db->dbdriver.'/subdrivers/'.$db->dbdriver.'_'.$db->subdriver.'_forge.php';
Andrey Andreeva287a342012-11-05 23:19:59 +0200423 if (file_exists($driver_path))
424 {
425 require_once($driver_path);
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200426 $class = 'CI_DB_'.$db->dbdriver.'_'.$db->subdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200427 }
428 }
429 else
430 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200431 $class = 'CI_DB_'.$db->dbdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200432 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000433
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200434 if ($return === TRUE)
435 {
436 return new $class($db);
437 }
438
439 $CI->dbforge = new $class($db);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200440 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300446 * View Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300448 * Loads "view" files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300450 * @param string $view View name
451 * @param array $vars An associative array of data
452 * to be extracted for use in the view
453 * @param bool $return Whether to return the view output
454 * or leave it to the Output class
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200455 * @return object|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 */
Greg Akerf5c84022011-04-19 17:13:03 -0500457 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
459 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300465 * Generic File Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300467 * @param string $path File path
468 * @param bool $return Whether to return the file output
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200469 * @return object|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 */
Greg Akerf5c84022011-04-19 17:13:03 -0500471 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
473 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 /**
479 * Set Variables
480 *
481 * Once variables are set they become available within
482 * the controller class and its "view" files.
483 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300484 * @param array|object|string $vars
485 * An associative array or object containing values
486 * to be set, or a value's name if string
487 * @param string $val Value to set, only used if $vars is a string
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200488 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200490 public function vars($vars, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
vlakoffa5e0ea82013-02-27 18:17:35 +0100492 if (is_string($vars))
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
494 $vars = array($vars => $val);
495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 $vars = $this->_ci_object_to_array($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200498
Andrey Andreev94af3552012-03-26 23:10:42 +0300499 if (is_array($vars) && count($vars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
501 foreach ($vars as $key => $val)
502 {
503 $this->_ci_cached_vars[$key] = $val;
504 }
505 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200506
507 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 }
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 /**
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200513 * Clear Cached Variables
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200514 *
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200515 * Clears the cached variables.
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200516 *
Andrey Andreev10fb7d12015-08-03 10:05:29 +0300517 * @return CI_Loader
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200518 */
519 public function clear_vars()
520 {
521 $this->_ci_cached_vars = array();
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200522 return $this;
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200523 }
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200524
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200525 // --------------------------------------------------------------------
526
527 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600528 * Get Variable
529 *
530 * Check if a variable is set and retrieve it.
531 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300532 * @param string $key Variable name
533 * @return mixed The variable or NULL if not found
Phil Sturgeon8731f642011-07-22 16:11:34 -0600534 */
535 public function get_var($key)
536 {
537 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
538 }
539
540 // --------------------------------------------------------------------
541
542 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600543 * Get Variables
544 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300545 * Retrieves all loaded variables.
Shane Pearson81dd2232011-11-18 20:49:35 -0600546 *
547 * @return array
548 */
549 public function get_vars()
550 {
551 return $this->_ci_cached_vars;
552 }
553
554 // --------------------------------------------------------------------
555
556 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300557 * Helper Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300559 * @param string|string[] $helpers Helper name(s)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200560 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 */
Greg Akerf5c84022011-04-19 17:13:03 -0500562 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200563 {
Derek Jones32bf1862010-03-02 13:46:07 -0600564 foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
Barry Mienydd671972010-10-04 16:33:58 +0200565 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 if (isset($this->_ci_helpers[$helper]))
567 {
568 continue;
569 }
Derek Jones32bf1862010-03-02 13:46:07 -0600570
Barry Mienydd671972010-10-04 16:33:58 +0200571 // Is this a helper extension request?
Andrey Andreev12d7b462012-11-12 13:42:09 +0200572 $ext_helper = config_item('subclass_prefix').$helper;
573 $ext_loaded = FALSE;
574 foreach ($this->_ci_helper_paths as $path)
575 {
576 if (file_exists($path.'helpers/'.$ext_helper.'.php'))
577 {
578 include_once($path.'helpers/'.$ext_helper.'.php');
579 $ext_loaded = TRUE;
580 }
581 }
582
583 // If we have loaded extensions - check if the base one is here
584 if ($ext_loaded === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
Greg Aker3a746652011-04-19 10:59:47 -0500586 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 if ( ! file_exists($base_helper))
588 {
Greg Aker3a746652011-04-19 10:59:47 -0500589 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 }
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 include_once($base_helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600593 $this->_ci_helpers[$helper] = TRUE;
Andrey Andreev90726b82015-01-20 12:39:22 +0200594 log_message('info', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600595 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Andrey Andreev12d7b462012-11-12 13:42:09 +0200598 // No extensions found ... try loading regular helpers and/or overrides
Derek Jones32bf1862010-03-02 13:46:07 -0600599 foreach ($this->_ci_helper_paths as $path)
600 {
Greg Aker3a746652011-04-19 10:59:47 -0500601 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200602 {
Greg Aker3a746652011-04-19 10:59:47 -0500603 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600604
605 $this->_ci_helpers[$helper] = TRUE;
Andrey Andreev90726b82015-01-20 12:39:22 +0200606 log_message('info', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600607 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 }
609 }
610
Derek Jones32bf1862010-03-02 13:46:07 -0600611 // unable to load the helper
612 if ( ! isset($this->_ci_helpers[$helper]))
613 {
Greg Aker3a746652011-04-19 10:59:47 -0500614 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600615 }
Barry Mienydd671972010-10-04 16:33:58 +0200616 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200617
618 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200622
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 /**
624 * Load Helpers
625 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300626 * An alias for the helper() method in case the developer has
627 * written the plural form of it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300629 * @uses CI_Loader::helper()
630 * @param string|string[] $helpers Helper name(s)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200631 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 */
Greg Akerf5c84022011-04-19 17:13:03 -0500633 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200635 return $this->helper($helpers);
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 }
Barry Mienydd671972010-10-04 16:33:58 +0200637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300641 * Language Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300643 * Loads language files.
644 *
645 * @param string|string[] $files List of language file names to load
646 * @param string Language name
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200647 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200649 public function language($files, $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 {
Andrey Andreevd8e31ec2014-11-07 12:17:50 +0200651 get_instance()->lang->load($files, $lang);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200652 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 }
Barry Mienydd671972010-10-04 16:33:58 +0200654
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200656
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300658 * Config Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300660 * Loads a config file (an alias for CI_Config::load()).
661 *
662 * @uses CI_Config::load()
663 * @param string $file Configuration file name
664 * @param bool $use_sections Whether configuration values should be loaded into their own section
665 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
666 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200668 public function config($file, $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200669 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200670 return get_instance()->config->load($file, $use_sections, $fail_gracefully);
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 }
672
673 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300676 * Driver Loader
Derek Jones8dca0412010-03-05 13:01:44 -0600677 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300678 * Loads a driver library.
Derek Jones8dca0412010-03-05 13:01:44 -0600679 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300680 * @param string|string[] $library Driver name(s)
681 * @param array $params Optional parameters to pass to the driver
682 * @param string $object_name An optional object name to assign to
683 *
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200684 * @return object|bool Object or FALSE on failure if $library is a string
685 * and $object_name is set. CI_Loader instance otherwise.
Derek Jones8dca0412010-03-05 13:01:44 -0600686 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200687 public function driver($library, $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600688 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800689 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800690 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800691 foreach ($library as $driver)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800692 {
693 $this->driver($driver);
694 }
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800695
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200696 return $this;
697 }
698 elseif (empty($library))
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200699 {
700 return FALSE;
701 }
702
vlakofffadb8222013-05-12 10:57:09 +0200703 if ( ! class_exists('CI_Driver_Library', FALSE))
dchill426262d052012-11-24 18:41:13 -0500704 {
705 // We aren't instantiating an object here, just making the base class available
706 require BASEPATH.'libraries/Driver.php';
707 }
708
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600709 // We can save the loader some time since Drivers will *always* be in a subfolder,
710 // and typically identically named to the library
711 if ( ! strpos($library, '/'))
712 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500713 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600714 }
Barry Mienydd671972010-10-04 16:33:58 +0200715
Derek Jones8dca0412010-03-05 13:01:44 -0600716 return $this->library($library, $params, $object_name);
717 }
718
719 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200720
Derek Jones8dca0412010-03-05 13:01:44 -0600721 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600722 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300724 * Prepends a parent path to the library, model, helper and config
725 * path arrays.
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300727 * @see CI_Loader::$_ci_library_paths
728 * @see CI_Loader::$_ci_model_paths
729 * @see CI_Loader::$_ci_helper_paths
730 * @see CI_Config::$_config_paths
731 *
732 * @param string $path Path to add
733 * @param bool $view_cascade (default: TRUE)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200734 * @return object
Derek Jones32bf1862010-03-02 13:46:07 -0600735 */
Andrey Andreevcce91802012-06-12 13:25:31 +0300736 public function add_package_path($path, $view_cascade = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600737 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500738 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200739
Derek Jones32bf1862010-03-02 13:46:07 -0600740 array_unshift($this->_ci_library_paths, $path);
741 array_unshift($this->_ci_model_paths, $path);
742 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200743
Greg Akerf5c84022011-04-19 17:13:03 -0500744 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
745
Derek Jones32bf1862010-03-02 13:46:07 -0600746 // Add config file path
747 $config =& $this->_ci_get_component('config');
and-ersb3ec9422013-01-03 16:05:12 +0100748 $config->_config_paths[] = $path;
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200749
750 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 }
752
753 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600754
755 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000756 * Get Package Paths
757 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300758 * Return a list of all package paths.
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000759 *
vlakoffcdc61132013-05-10 16:47:47 +0200760 * @param bool $include_base Whether to include BASEPATH (default: FALSE)
Andrey Andreeved4b2582012-10-27 17:46:52 +0300761 * @return array
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000762 */
Greg Akerf5c84022011-04-19 17:13:03 -0500763 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000764 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300765 return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000766 }
767
768 // --------------------------------------------------------------------
769
770 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600771 * Remove Package Path
772 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300773 * Remove a path from the library, model, helper and/or config
774 * path arrays if it exists. If no path is provided, the most recently
775 * added path will be removed removed.
Derek Jones32bf1862010-03-02 13:46:07 -0600776 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300777 * @param string $path Path to remove
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200778 * @return object
Derek Jones32bf1862010-03-02 13:46:07 -0600779 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300780 public function remove_package_path($path = '')
Derek Jones32bf1862010-03-02 13:46:07 -0600781 {
782 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200783
Alex Bilbieed944a32012-06-02 11:07:47 +0100784 if ($path === '')
Derek Jones32bf1862010-03-02 13:46:07 -0600785 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200786 array_shift($this->_ci_library_paths);
787 array_shift($this->_ci_model_paths);
788 array_shift($this->_ci_helper_paths);
789 array_shift($this->_ci_view_paths);
Korri3684d342012-04-17 00:35:08 -0400790 array_pop($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600791 }
792 else
793 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500794 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600795 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
796 {
797 if (($key = array_search($path, $this->{$var})) !== FALSE)
798 {
799 unset($this->{$var}[$key]);
800 }
801 }
David Behlercda768a2011-08-14 23:52:48 +0200802
Greg Akerf5c84022011-04-19 17:13:03 -0500803 if (isset($this->_ci_view_paths[$path.'views/']))
804 {
805 unset($this->_ci_view_paths[$path.'views/']);
806 }
Barry Mienydd671972010-10-04 16:33:58 +0200807
Derek Jones32bf1862010-03-02 13:46:07 -0600808 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
809 {
810 unset($config->_config_paths[$key]);
811 }
812 }
Barry Mienydd671972010-10-04 16:33:58 +0200813
Derek Jones32bf1862010-03-02 13:46:07 -0600814 // make sure the application default paths are still in the array
815 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
816 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
817 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500818 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600819 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200820
821 return $this;
Derek Jones32bf1862010-03-02 13:46:07 -0600822 }
823
824 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300827 * Internal CI Data Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300829 * Used to load views and files.
830 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 * Variables are prefixed with _ci_ to avoid symbol collision with
Andrey Andreeved4b2582012-10-27 17:46:52 +0300832 * variables made available to view files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300834 * @used-by CI_Loader::view()
835 * @used-by CI_Loader::file()
836 * @param array $_ci_data Data to load
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200837 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 */
Greg Akerf5c84022011-04-19 17:13:03 -0500839 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 {
841 // Set the default data variables
842 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
843 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300844 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 }
David Behlercda768a2011-08-14 23:52:48 +0200846
Greg Akerf5c84022011-04-19 17:13:03 -0500847 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000848
849 // Set the path to the requested file
Alex Bilbie40bd2a72012-06-02 16:04:15 +0100850 if (is_string($_ci_path) && $_ci_path !== '')
Greg Aker8807be32011-04-21 13:06:15 -0500851 {
852 $_ci_x = explode('/', $_ci_path);
853 $_ci_file = end($_ci_x);
854 }
855 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 {
857 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Alex Bilbieed944a32012-06-02 11:07:47 +0100858 $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500859
Joe McFrederick64f470b2012-08-18 12:29:56 -0400860 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
Greg Akerf5c84022011-04-19 17:13:03 -0500861 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400862 if (file_exists($_ci_view_file.$_ci_file))
Greg Akerf5c84022011-04-19 17:13:03 -0500863 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400864 $_ci_path = $_ci_view_file.$_ci_file;
Greg Akerf5c84022011-04-19 17:13:03 -0500865 $file_exists = TRUE;
866 break;
867 }
David Behlercda768a2011-08-14 23:52:48 +0200868
Greg Akerf5c84022011-04-19 17:13:03 -0500869 if ( ! $cascade)
870 {
871 break;
David Behlercda768a2011-08-14 23:52:48 +0200872 }
Greg Akerf5c84022011-04-19 17:13:03 -0500873 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 }
Barry Mienydd671972010-10-04 16:33:58 +0200875
Greg Akerf5c84022011-04-19 17:13:03 -0500876 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 {
878 show_error('Unable to load the requested file: '.$_ci_file);
879 }
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 // This allows anything loaded using $this->load (views, files, etc.)
882 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500883 $_ci_CI =& get_instance();
884 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500886 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500888 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 }
890 }
891
892 /*
893 * Extract and cache variables
894 *
vlakoff02506182012-07-03 07:28:50 +0200895 * You can either set variables using the dedicated $this->load->vars()
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 * function or via the second parameter of this function. We'll merge
897 * the two types and cache them so that views that are embedded within
898 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200899 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 if (is_array($_ci_vars))
901 {
902 $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
903 }
904 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200905
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 /*
907 * Buffer the output
908 *
909 * We buffer the output for two reasons:
910 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200911 * 2. So that the final rendered template can be post-processed by
dchill425628ba02012-08-08 12:05:45 -0400912 * the output class. Why do we need post processing? For one thing,
913 * in order to show the elapsed page load time. Unless we can
914 * intercept the content right before it's sent to the browser and
915 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 */
917 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200918
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 // If the PHP installation does not support short tags we'll
920 // do a little string replacement, changing the short tags
921 // to standard PHP echo statements.
Andrey Andreev07355da2015-07-22 12:46:16 +0300922 if ( ! is_php('5.4') && ! ini_get('short_open_tag') && config_item('rewrite_short_tags') === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200924 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 }
926 else
927 {
928 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
929 }
Barry Mienydd671972010-10-04 16:33:58 +0200930
Andrey Andreev90726b82015-01-20 12:39:22 +0200931 log_message('info', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 // Return the file data if requested
934 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200935 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 $buffer = ob_get_contents();
937 @ob_end_clean();
938 return $buffer;
939 }
940
941 /*
942 * Flush the buffer... or buff the flusher?
943 *
944 * In order to permit views to be nested within
945 * other views, we need to flush the content back out whenever
946 * we are beyond the first level of output buffering so that
947 * it can be seen and included properly by the first included
948 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200949 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 if (ob_get_level() > $this->_ci_ob_level + 1)
951 {
952 ob_end_flush();
953 }
954 else
955 {
Greg Aker22f1a632010-11-10 15:34:35 -0600956 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 @ob_end_clean();
958 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200959
960 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 }
962
963 // --------------------------------------------------------------------
964
965 /**
Andrey Andreevdb669f12015-01-21 16:51:51 +0200966 * Internal CI Library Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300968 * @used-by CI_Loader::library()
Andrey Andreevdb669f12015-01-21 16:51:51 +0200969 * @uses CI_Loader::_ci_init_library()
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300971 * @param string $class Class name to load
972 * @param mixed $params Optional parameters to pass to the class constructor
973 * @param string $object_name Optional object name to assign to
Barry Mienydd671972010-10-04 16:33:58 +0200974 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 */
Andrey Andreevdb669f12015-01-21 16:51:51 +0200976 protected function _ci_load_library($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200977 {
978 // Get the class name, and while we're at it trim any slashes.
979 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -0500981 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +0200982
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 // Was the path included with the class name?
984 // We look for a slash to determine this
Derek Jones32bf1862010-03-02 13:46:07 -0600985 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 {
Derek Jones32bf1862010-03-02 13:46:07 -0600987 // Extract the path
Andrey Andreevc26d34f2013-01-28 21:46:08 +0200988 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +0200989
Derek Jones32bf1862010-03-02 13:46:07 -0600990 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +0200991 $class = substr($class, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 }
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200993 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200995 $subdir = '';
996 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000997
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200998 $class = ucfirst($class);
Andrey Andreev3608e1a2013-01-28 16:27:30 +0200999
Andrey Andreevdb669f12015-01-21 16:51:51 +02001000 // Is this a stock library? There are a few special conditions if so ...
1001 if (file_exists(BASEPATH.'libraries/'.$subdir.$class.'.php'))
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001002 {
Andrey Andreevdb669f12015-01-21 16:51:51 +02001003 return $this->_ci_load_stock_library($class, $subdir, $params, $object_name);
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001004 }
1005
vlakoff35672462013-02-15 01:36:04 +01001006 // Let's search for the requested library file and load it.
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001007 foreach ($this->_ci_library_paths as $path)
1008 {
Andrey Andreevdb669f12015-01-21 16:51:51 +02001009 // BASEPATH has already been checked for
1010 if ($path === BASEPATH)
1011 {
1012 continue;
1013 }
1014
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001015 $filepath = $path.'libraries/'.$subdir.$class.'.php';
1016
1017 // Safety: Was the class already loaded by a previous call?
1018 if (class_exists($class, FALSE))
1019 {
1020 // Before we deem this to be a duplicate request, let's see
1021 // if a custom object name is being supplied. If so, we'll
1022 // return a new instance of the object
1023 if ($object_name !== NULL)
1024 {
1025 $CI =& get_instance();
1026 if ( ! isset($CI->$object_name))
1027 {
Andrey Andreevdb669f12015-01-21 16:51:51 +02001028 return $this->_ci_init_library($class, '', $params, $object_name);
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001029 }
1030 }
1031
1032 log_message('debug', $class.' class already loaded. Second attempt ignored.');
1033 return;
1034 }
1035 // Does the file exist? No? Bummer...
1036 elseif ( ! file_exists($filepath))
1037 {
1038 continue;
1039 }
1040
1041 include_once($filepath);
Andrey Andreevdb669f12015-01-21 16:51:51 +02001042 return $this->_ci_init_library($class, '', $params, $object_name);
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001043 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001044
Andrey Andreevd7297352012-01-07 22:53:14 +02001045 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Alex Bilbieed944a32012-06-02 11:07:47 +01001046 if ($subdir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 {
Andrey Andreevdb669f12015-01-21 16:51:51 +02001048 return $this->_ci_load_library($class.'/'.$class, $params, $object_name);
dchill42aee92652012-08-26 21:45:35 -04001049 }
Barry Mienydd671972010-10-04 16:33:58 +02001050
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 // If we got this far we were unable to find the requested class.
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001052 log_message('error', 'Unable to load the requested class: '.$class);
1053 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 }
Barry Mienydd671972010-10-04 16:33:58 +02001055
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 // --------------------------------------------------------------------
1057
1058 /**
Andrey Andreevdb669f12015-01-21 16:51:51 +02001059 * Internal CI Stock Library Loader
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 *
Andrey Andreevdb669f12015-01-21 16:51:51 +02001061 * @used-by CI_Loader::_ci_load_library()
1062 * @uses CI_Loader::_ci_init_library()
1063 *
1064 * @param string $library Library name to load
1065 * @param string $file_path Path to the library filename, relative to libraries/
1066 * @param mixed $params Optional parameters to pass to the class constructor
1067 * @param string $object_name Optional object name to assign to
1068 * @return void
1069 */
1070 protected function _ci_load_stock_library($library_name, $file_path, $params, $object_name)
1071 {
1072 $prefix = 'CI_';
1073
1074 if (class_exists($prefix.$library_name, FALSE))
1075 {
1076 if (class_exists(config_item('subclass_prefix').$library_name, FALSE))
1077 {
1078 $prefix = config_item('subclass_prefix');
1079 }
1080
1081 // Before we deem this to be a duplicate request, let's see
1082 // if a custom object name is being supplied. If so, we'll
1083 // return a new instance of the object
1084 if ($object_name !== NULL)
1085 {
1086 $CI =& get_instance();
1087 if ( ! isset($CI->$object_name))
1088 {
1089 return $this->_ci_init_library($library_name, $prefix, $params, $object_name);
1090 }
1091 }
1092
1093 log_message('debug', $library_name.' class already loaded. Second attempt ignored.');
1094 return;
1095 }
Andrey Andreev8f5c1782015-03-25 13:41:02 +02001096
1097 $paths = $this->_ci_library_paths;
1098 array_pop($paths); // BASEPATH
1099 array_pop($paths); // APPPATH (needs to be the first path checked)
1100 array_unshift($paths, APPPATH);
1101
1102 foreach ($paths as $path)
Andrey Andreevdb669f12015-01-21 16:51:51 +02001103 {
Andrey Andreev8f5c1782015-03-25 13:41:02 +02001104 if (file_exists($path = $path.'libraries/'.$file_path.$library_name.'.php'))
Andrey Andreevdb669f12015-01-21 16:51:51 +02001105 {
Andrey Andreev8f5c1782015-03-25 13:41:02 +02001106 // Override
1107 include_once($path);
1108 if (class_exists($prefix.$library_name, FALSE))
1109 {
1110 return $this->_ci_init_library($library_name, $prefix, $params, $object_name);
1111 }
1112 else
1113 {
1114 log_message('debug', $path.' exists, but does not declare '.$prefix.$library_name);
1115 }
Andrey Andreevdb669f12015-01-21 16:51:51 +02001116 }
1117 }
1118
1119 include_once(BASEPATH.'libraries/'.$file_path.$library_name.'.php');
1120
1121 // Check for extensions
1122 $subclass = config_item('subclass_prefix').$library_name;
Andrey Andreev8f5c1782015-03-25 13:41:02 +02001123 foreach ($paths as $path)
Andrey Andreevdb669f12015-01-21 16:51:51 +02001124 {
Andrey Andreev8f5c1782015-03-25 13:41:02 +02001125 if (file_exists($path = $path.'libraries/'.$file_path.$subclass.'.php'))
Andrey Andreevdb669f12015-01-21 16:51:51 +02001126 {
Andrey Andreev8f5c1782015-03-25 13:41:02 +02001127 include_once($path);
1128 if (class_exists($subclass, FALSE))
1129 {
1130 $prefix = config_item('subclass_prefix');
1131 break;
1132 }
1133 else
1134 {
mwhitneysdsue04f4f72015-03-30 12:38:22 -07001135 log_message('debug', $path.' exists, but does not declare '.$subclass);
Andrey Andreev8f5c1782015-03-25 13:41:02 +02001136 }
Andrey Andreevdb669f12015-01-21 16:51:51 +02001137 }
1138 }
1139
1140 return $this->_ci_init_library($library_name, $prefix, $params, $object_name);
1141 }
1142
1143 // --------------------------------------------------------------------
1144
1145 /**
1146 * Internal CI Library Instantiator
1147 *
1148 * @used-by CI_Loader::_ci_load_stock_library()
1149 * @used-by CI_Loader::_ci_load_library()
Andrey Andreeved4b2582012-10-27 17:46:52 +03001150 *
1151 * @param string $class Class name
1152 * @param string $prefix Class name prefix
1153 * @param array|null|bool $config Optional configuration to pass to the class constructor:
1154 * FALSE to skip;
1155 * NULL to search in config paths;
1156 * array containing configuration data
1157 * @param string $object_name Optional object name to assign to
Andrey Andreev94af3552012-03-26 23:10:42 +03001158 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 */
Andrey Andreevdb669f12015-01-21 16:51:51 +02001160 protected function _ci_init_library($class, $prefix, $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001161 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001162 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 if ($config === NULL)
1164 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001165 // Fetch the config paths containing any package paths
1166 $config_component = $this->_ci_get_component('config');
1167
1168 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 {
Andrey Andreev95c31ad2014-12-17 19:01:31 +02001170 $found = FALSE;
Eric Barnes5e16ec62011-01-04 17:25:23 -05001171 foreach ($config_component->_config_paths as $path)
1172 {
1173 // We test for both uppercase and lowercase, for servers that
Andrey Andreev95c31ad2014-12-17 19:01:31 +02001174 // are case-sensitive with regard to file names. Load global first,
1175 // override with environment next
1176 if (file_exists($path.'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001177 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001178 include($path.'config/'.strtolower($class).'.php');
Andrey Andreev95c31ad2014-12-17 19:01:31 +02001179 $found = TRUE;
Eric Barnes5e16ec62011-01-04 17:25:23 -05001180 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001181 elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001182 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001183 include($path.'config/'.ucfirst(strtolower($class)).'.php');
Andrey Andreev95c31ad2014-12-17 19:01:31 +02001184 $found = TRUE;
1185 }
1186
1187 if (file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
1188 {
1189 include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
1190 $found = TRUE;
1191 }
1192 elseif (file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
1193 {
1194 include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
1195 $found = TRUE;
1196 }
1197
1198 // Break on the first found configuration, thus package
1199 // files are not overridden by default paths
1200 if ($found === TRUE)
1201 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001202 break;
1203 }
1204 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 }
1206 }
Barry Mienydd671972010-10-04 16:33:58 +02001207
Andrey Andreevdb669f12015-01-21 16:51:51 +02001208 $class_name = $prefix.$class;
Barry Mienydd671972010-10-04 16:33:58 +02001209
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 // Is the class name valid?
Andrey Andreevdb669f12015-01-21 16:51:51 +02001211 if ( ! class_exists($class_name, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 {
Andrey Andreevdb669f12015-01-21 16:51:51 +02001213 log_message('error', 'Non-existent class: '.$class_name);
1214 show_error('Non-existent class: '.$class_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 }
Barry Mienydd671972010-10-04 16:33:58 +02001216
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001218 // Was a custom class name supplied? If so we'll use it
Andrey Andreev519f87a2013-07-23 17:16:10 +03001219 if (empty($object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 {
Andrey Andreev519f87a2013-07-23 17:16:10 +03001221 $object_name = strtolower($class);
1222 if (isset($this->_ci_varmap[$object_name]))
1223 {
1224 $object_name = $this->_ci_varmap[$object_name];
1225 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 }
Andrey Andreev519f87a2013-07-23 17:16:10 +03001227
1228 // Don't overwrite existing properties
1229 $CI =& get_instance();
1230 if (isset($CI->$object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 {
Andrey Andreevdb669f12015-01-21 16:51:51 +02001232 if ($CI->$object_name instanceof $class_name)
Andrey Andreev519f87a2013-07-23 17:16:10 +03001233 {
Andrey Andreevdb669f12015-01-21 16:51:51 +02001234 log_message('debug', $class_name." has already been instantiated as '".$object_name."'. Second attempt aborted.");
Andrey Andreev519f87a2013-07-23 17:16:10 +03001235 return;
1236 }
1237
Andrey Andreevdb669f12015-01-21 16:51:51 +02001238 show_error("Resource '".$object_name."' already exists and is not a ".$class_name." instance.");
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 }
1240
Barry Mienydd671972010-10-04 16:33:58 +02001241 // Save the class name and object name
Andrey Andreev519f87a2013-07-23 17:16:10 +03001242 $this->_ci_classes[$object_name] = $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001243
Barry Mienydd671972010-10-04 16:33:58 +02001244 // Instantiate the class
Andrey Andreev519f87a2013-07-23 17:16:10 +03001245 $CI->$object_name = isset($config)
Andrey Andreevdb669f12015-01-21 16:51:51 +02001246 ? new $class_name($config)
1247 : new $class_name();
Barry Mienydd671972010-10-04 16:33:58 +02001248 }
1249
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001251
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001253 * CI Autoloader
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001255 * Loads component listed in the config/autoload.php file.
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 *
Andrey Andreevcdac2482012-11-03 18:09:01 +02001257 * @used-by CI_Loader::initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 * @return void
1259 */
Shane Pearson665baec2011-08-22 18:52:19 -05001260 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001261 {
Andrey Andreev4b6469d2015-01-22 16:42:17 +02001262 if (file_exists(APPPATH.'config/autoload.php'))
Andrey Andreevbd6a8142015-01-22 16:41:25 +02001263 {
1264 include(APPPATH.'config/autoload.php');
1265 }
1266
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001267 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001268 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001269 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001270 }
Barry Mienydd671972010-10-04 16:33:58 +02001271
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 if ( ! isset($autoload))
1273 {
Gabriel Potkányd9287a02015-02-04 08:47:56 +01001274 return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 }
Barry Mienydd671972010-10-04 16:33:58 +02001276
Phil Sturgeon9730c752010-12-15 10:50:15 +00001277 // Autoload packages
1278 if (isset($autoload['packages']))
1279 {
1280 foreach ($autoload['packages'] as $package_path)
1281 {
1282 $this->add_package_path($package_path);
1283 }
1284 }
1285
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 // Load any custom config file
1287 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001288 {
Andrey Andreev623227f2014-03-24 12:46:25 +02001289 foreach ($autoload['config'] as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 {
Andrey Andreev689d41c2014-03-24 17:28:40 +02001291 $this->config($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 }
Barry Mienydd671972010-10-04 16:33:58 +02001293 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001294
Derek Jonesc6da5032010-03-09 20:44:27 -06001295 // Autoload helpers and languages
1296 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001297 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001298 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 {
1300 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001301 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 }
1303
Andrey Andreev9ab70a82013-07-23 00:09:26 +03001304 // Autoload drivers
1305 if (isset($autoload['drivers']))
1306 {
1307 foreach ($autoload['drivers'] as $item)
1308 {
1309 $this->driver($item);
1310 }
1311 }
1312
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001314 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 {
1316 // Load the database driver.
1317 if (in_array('database', $autoload['libraries']))
1318 {
1319 $this->database();
1320 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1321 }
Barry Mienydd671972010-10-04 16:33:58 +02001322
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 // Load all other libraries
Andrey Andreeve36d0482015-04-04 21:55:09 +03001324 $this->library($autoload['libraries']);
Barry Mienydd671972010-10-04 16:33:58 +02001325 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001326
1327 // Autoload models
1328 if (isset($autoload['model']))
1329 {
1330 $this->model($autoload['model']);
1331 }
Barry Mienydd671972010-10-04 16:33:58 +02001332 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001333
1334 // --------------------------------------------------------------------
1335
1336 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001337 * CI Object to Array translator
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001339 * Takes an object as input and converts the class variables to
1340 * an associative array with key/value pairs.
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001342 * @param object $object Object data to translate
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 * @return array
1344 */
Greg Akerf5c84022011-04-19 17:13:03 -05001345 protected function _ci_object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001347 return is_object($object) ? get_object_vars($object) : $object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 }
1349
1350 // --------------------------------------------------------------------
1351
1352 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001353 * CI Component getter
Derek Jones32bf1862010-03-02 13:46:07 -06001354 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001355 * Get a reference to a specific library or model.
1356 *
1357 * @param string $component Component name
Derek Jones32bf1862010-03-02 13:46:07 -06001358 * @return bool
1359 */
Greg Akerf5c84022011-04-19 17:13:03 -05001360 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001361 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001362 $CI =& get_instance();
1363 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001364 }
1365
1366 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001367
Derek Jones32bf1862010-03-02 13:46:07 -06001368 /**
1369 * Prep filename
1370 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001371 * This function prepares filenames of various items to
1372 * make their loading more reliable.
Derek Jones32bf1862010-03-02 13:46:07 -06001373 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001374 * @param string|string[] $filename Filename(s)
1375 * @param string $extension Filename extension
Derek Jones32bf1862010-03-02 13:46:07 -06001376 * @return array
1377 */
Greg Akerf5c84022011-04-19 17:13:03 -05001378 protected function _ci_prep_filename($filename, $extension)
Derek Jones32bf1862010-03-02 13:46:07 -06001379 {
1380 if ( ! is_array($filename))
Barry Mienydd671972010-10-04 16:33:58 +02001381 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001382 return array(strtolower(str_replace(array($extension, '.php'), '', $filename).$extension));
Derek Jones32bf1862010-03-02 13:46:07 -06001383 }
1384 else
1385 {
1386 foreach ($filename as $key => $val)
1387 {
Andrey Andreevd47baab2012-01-09 16:56:46 +02001388 $filename[$key] = strtolower(str_replace(array($extension, '.php'), '', $val).$extension);
Derek Jones32bf1862010-03-02 13:46:07 -06001389 }
Barry Mienydd671972010-10-04 16:33:58 +02001390
Derek Jones32bf1862010-03-02 13:46:07 -06001391 return $filename;
1392 }
1393 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001394
Derek Allard2067d1a2008-11-13 22:59:24 +00001395}