blob: 9813e99a939024808326069dcaf0714277865c98 [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 *
Master Yodada60e9b2016-12-31 08:46:18 -08009 * Copyright (c) 2014 - 2017, 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Master Yodada60e9b2016-12-31 08:46:18 -080032 * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @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
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/libraries/loader.html
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
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 Andreev9bceb9c2017-07-06 15:12:09 +0300185 * @param mixed $library Library name
Andrey Andreeved4b2582012-10-27 17:46:52 +0300186 * @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 Andreev6cfc6a92017-11-03 13:38:13 +0200229 * @param mixed $model Model name
Andrey Andreeved4b2582012-10-27 17:46:52 +0300230 * @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
Andrey Andreev0b1efb32016-01-04 12:34:14 +0200288 // Note: All of the code under this condition used to be just:
289 //
290 // load_class('Model', 'core');
291 //
292 // However, load_class() instantiates classes
293 // to cache them for later use and that prevents
294 // MY_Model from being an abstract class and is
295 // sub-optimal otherwise anyway.
Andrey Andreev20292312013-07-22 14:29:10 +0300296 if ( ! class_exists('CI_Model', FALSE))
297 {
Andrey Andreev0b1efb32016-01-04 12:34:14 +0200298 $app_path = APPPATH.'core'.DIRECTORY_SEPARATOR;
299 if (file_exists($app_path.'Model.php'))
300 {
301 require_once($app_path.'Model.php');
302 if ( ! class_exists('CI_Model', FALSE))
303 {
304 throw new RuntimeException($app_path."Model.php exists, but doesn't declare class CI_Model");
305 }
Andrey Andreevc9420842017-11-10 15:12:04 +0200306
307 log_message('info', 'CI_Model class loaded');
Andrey Andreev0b1efb32016-01-04 12:34:14 +0200308 }
309 elseif ( ! class_exists('CI_Model', FALSE))
310 {
311 require_once(BASEPATH.'core'.DIRECTORY_SEPARATOR.'Model.php');
312 }
313
314 $class = config_item('subclass_prefix').'Model';
315 if (file_exists($app_path.$class.'.php'))
316 {
317 require_once($app_path.$class.'.php');
318 if ( ! class_exists($class, FALSE))
319 {
320 throw new RuntimeException($app_path.$class.".php exists, but doesn't declare class ".$class);
321 }
Andrey Andreevc9420842017-11-10 15:12:04 +0200322
323 log_message('info', config_item('subclass_prefix').'Model class loaded');
Andrey Andreev0b1efb32016-01-04 12:34:14 +0200324 }
Andrey Andreev20292312013-07-22 14:29:10 +0300325 }
326
Andrey Andreev825fab72015-08-17 09:52:42 +0300327 $model = ucfirst($model);
Andrey Andreev738b9e32016-02-24 12:14:10 +0200328 if ( ! class_exists($model, FALSE))
Derek Jones32bf1862010-03-02 13:46:07 -0600329 {
Andrey Andreevb63dc192015-07-22 13:14:50 +0300330 foreach ($this->_ci_model_paths as $mod_path)
Derek Jones32bf1862010-03-02 13:46:07 -0600331 {
Andrey Andreevb63dc192015-07-22 13:14:50 +0300332 if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
333 {
334 continue;
335 }
336
337 require_once($mod_path.'models/'.$path.$model.'.php');
338 if ( ! class_exists($model, FALSE))
339 {
340 throw new RuntimeException($mod_path."models/".$path.$model.".php exists, but doesn't declare class ".$model);
341 }
342
343 break;
Derek Jones32bf1862010-03-02 13:46:07 -0600344 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000345
Andrey Andreevb63dc192015-07-22 13:14:50 +0300346 if ( ! class_exists($model, FALSE))
347 {
348 throw new RuntimeException('Unable to locate the model you have specified: '.$model);
349 }
350 }
351 elseif ( ! is_subclass_of($model, 'CI_Model'))
352 {
353 throw new RuntimeException("Class ".$model." already exists and doesn't extend CI_Model");
Derek Jones32bf1862010-03-02 13:46:07 -0600354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Andrey Andreevb63dc192015-07-22 13:14:50 +0300356 $this->_ci_models[] = $name;
Andrey Andreevc9420842017-11-10 15:12:04 +0200357 $model = new $model();
358 $CI->$name = $model;
359 log_message('info', 'Model "'.get_class($model).'" initialized');
Andrey Andreevb63dc192015-07-22 13:14:50 +0300360 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 /**
366 * Database Loader
367 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300368 * @param mixed $params Database configuration options
369 * @param bool $return Whether to return the database object
370 * @param bool $query_builder Whether to enable Query Builder
371 * (overrides the configuration setting)
372 *
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200373 * @return object|bool Database object if $return is set to TRUE,
374 * FALSE on failure, CI_Loader instance in any other case
Barry Mienydd671972010-10-04 16:33:58 +0200375 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000376 public function database($params = '', $return = FALSE, $query_builder = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 // Grab the super object
379 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 // Do we even need to load the database class?
Andrey Andreev9d0ab042012-10-24 21:47:39 +0300382 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 +0000383 {
384 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200385 }
386
Greg Aker3a746652011-04-19 10:59:47 -0500387 require_once(BASEPATH.'database/DB.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000388
389 if ($return === TRUE)
390 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000391 return DB($params, $query_builder);
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Andrey Andreevd7297352012-01-07 22:53:14 +0200394 // Initialize the db variable. Needed to prevent
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 // reference errors with some configurations
396 $CI->db = '';
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 // Load the DB class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000399 $CI->db =& DB($params, $query_builder);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200400 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 // --------------------------------------------------------------------
404
405 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300406 * Load the Database Utilities Class
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200408 * @param object $db Database object
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200409 * @param bool $return Whether to return the DB Utilities class object or not
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200410 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200411 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200412 public function dbutil($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 $CI =& get_instance();
415
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200416 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
417 {
418 class_exists('CI_DB', FALSE) OR $this->database();
419 $db =& $CI->db;
420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Greg Aker3a746652011-04-19 10:59:47 -0500422 require_once(BASEPATH.'database/DB_utility.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200423 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_utility.php');
424 $class = 'CI_DB_'.$db->dbdriver.'_utility';
Derek Allard2067d1a2008-11-13 22:59:24 +0000425
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200426 if ($return === TRUE)
427 {
428 return new $class($db);
429 }
430
431 $CI->dbutil = new $class($db);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200432 return $this;
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
437 /**
438 * Load the Database Forge Class
439 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200440 * @param object $db Database object
441 * @param bool $return Whether to return the DB Forge class object or not
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200442 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200443 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200444 public function dbforge($db = NULL, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200446 $CI =& get_instance();
447 if ( ! is_object($db) OR ! ($db instanceof CI_DB))
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200449 class_exists('CI_DB', FALSE) OR $this->database();
450 $db =& $CI->db;
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Greg Aker3a746652011-04-19 10:59:47 -0500453 require_once(BASEPATH.'database/DB_forge.php');
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200454 require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_forge.php');
Andrey Andreeva287a342012-11-05 23:19:59 +0200455
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200456 if ( ! empty($db->subdriver))
Andrey Andreeva287a342012-11-05 23:19:59 +0200457 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200458 $driver_path = BASEPATH.'database/drivers/'.$db->dbdriver.'/subdrivers/'.$db->dbdriver.'_'.$db->subdriver.'_forge.php';
Andrey Andreeva287a342012-11-05 23:19:59 +0200459 if (file_exists($driver_path))
460 {
461 require_once($driver_path);
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200462 $class = 'CI_DB_'.$db->dbdriver.'_'.$db->subdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200463 }
464 }
465 else
466 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200467 $class = 'CI_DB_'.$db->dbdriver.'_forge';
Andrey Andreeva287a342012-11-05 23:19:59 +0200468 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000469
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200470 if ($return === TRUE)
471 {
472 return new $class($db);
473 }
474
475 $CI->dbforge = new $class($db);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200476 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 }
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300482 * View Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300484 * Loads "view" files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300486 * @param string $view View name
487 * @param array $vars An associative array of data
488 * to be extracted for use in the view
489 * @param bool $return Whether to return the view output
490 * or leave it to the Output class
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200491 * @return object|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 */
Greg Akerf5c84022011-04-19 17:13:03 -0500493 public function view($view, $vars = array(), $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 {
Andrey Andreevec8dbbb2017-01-04 17:01:44 +0200495 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return));
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300501 * Generic File Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300503 * @param string $path File path
504 * @param bool $return Whether to return the file output
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200505 * @return object|string
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 */
Greg Akerf5c84022011-04-19 17:13:03 -0500507 public function file($path, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
509 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 /**
515 * Set Variables
516 *
517 * Once variables are set they become available within
518 * the controller class and its "view" files.
519 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300520 * @param array|object|string $vars
521 * An associative array or object containing values
522 * to be set, or a value's name if string
523 * @param string $val Value to set, only used if $vars is a string
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200524 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200526 public function vars($vars, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 {
Andrey Andreevec8dbbb2017-01-04 17:01:44 +0200528 $vars = is_string($vars)
529 ? array($vars => $val)
530 : $this->_ci_prepare_view_vars($vars);
Barry Mienydd671972010-10-04 16:33:58 +0200531
Andrey Andreevec8dbbb2017-01-04 17:01:44 +0200532 foreach ($vars as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Andrey Andreevec8dbbb2017-01-04 17:01:44 +0200534 $this->_ci_cached_vars[$key] = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200536
537 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 /**
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200543 * Clear Cached Variables
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200544 *
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200545 * Clears the cached variables.
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200546 *
Andrey Andreev10fb7d12015-08-03 10:05:29 +0300547 * @return CI_Loader
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200548 */
549 public function clear_vars()
550 {
551 $this->_ci_cached_vars = array();
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200552 return $this;
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200553 }
Andrey Andreevaf01fb12014-01-03 16:57:44 +0200554
Tomaz Lovreca81a5ef2013-10-16 11:29:57 +0200555 // --------------------------------------------------------------------
556
557 /**
Phil Sturgeon8731f642011-07-22 16:11:34 -0600558 * Get Variable
559 *
560 * Check if a variable is set and retrieve it.
561 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300562 * @param string $key Variable name
563 * @return mixed The variable or NULL if not found
Phil Sturgeon8731f642011-07-22 16:11:34 -0600564 */
565 public function get_var($key)
566 {
567 return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
568 }
569
570 // --------------------------------------------------------------------
571
572 /**
Shane Pearson81dd2232011-11-18 20:49:35 -0600573 * Get Variables
574 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300575 * Retrieves all loaded variables.
Shane Pearson81dd2232011-11-18 20:49:35 -0600576 *
577 * @return array
578 */
579 public function get_vars()
580 {
581 return $this->_ci_cached_vars;
582 }
583
584 // --------------------------------------------------------------------
585
586 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300587 * Helper Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300589 * @param string|string[] $helpers Helper name(s)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200590 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 */
Greg Akerf5c84022011-04-19 17:13:03 -0500592 public function helper($helpers = array())
Barry Mienydd671972010-10-04 16:33:58 +0200593 {
Andrey Andreev4015f9b2016-11-14 10:22:59 +0200594 is_array($helpers) OR $helpers = array($helpers);
595 foreach ($helpers as &$helper)
Barry Mienydd671972010-10-04 16:33:58 +0200596 {
Andrey Andreev4015f9b2016-11-14 10:22:59 +0200597 $filename = basename($helper);
598 $filepath = ($filename === $helper) ? '' : substr($helper, 0, strlen($helper) - strlen($filename));
Andrey Andreev99d05f02017-01-11 17:04:06 +0200599 $filename = strtolower(preg_replace('#(_helper)?(\.php)?$#i', '', $filename)).'_helper';
Andrey Andreev4015f9b2016-11-14 10:22:59 +0200600 $helper = $filepath.$filename;
601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 if (isset($this->_ci_helpers[$helper]))
603 {
604 continue;
605 }
Derek Jones32bf1862010-03-02 13:46:07 -0600606
Barry Mienydd671972010-10-04 16:33:58 +0200607 // Is this a helper extension request?
Andrey Andreev4015f9b2016-11-14 10:22:59 +0200608 $ext_helper = config_item('subclass_prefix').$filename;
Andrey Andreev12d7b462012-11-12 13:42:09 +0200609 $ext_loaded = FALSE;
610 foreach ($this->_ci_helper_paths as $path)
611 {
612 if (file_exists($path.'helpers/'.$ext_helper.'.php'))
613 {
614 include_once($path.'helpers/'.$ext_helper.'.php');
615 $ext_loaded = TRUE;
616 }
617 }
618
619 // If we have loaded extensions - check if the base one is here
620 if ($ext_loaded === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 {
Greg Aker3a746652011-04-19 10:59:47 -0500622 $base_helper = BASEPATH.'helpers/'.$helper.'.php';
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 if ( ! file_exists($base_helper))
624 {
Greg Aker3a746652011-04-19 10:59:47 -0500625 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 }
Barry Mienydd671972010-10-04 16:33:58 +0200627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 include_once($base_helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600629 $this->_ci_helpers[$helper] = TRUE;
Andrey Andreev90726b82015-01-20 12:39:22 +0200630 log_message('info', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600631 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 }
Barry Mienydd671972010-10-04 16:33:58 +0200633
Andrey Andreev12d7b462012-11-12 13:42:09 +0200634 // No extensions found ... try loading regular helpers and/or overrides
Derek Jones32bf1862010-03-02 13:46:07 -0600635 foreach ($this->_ci_helper_paths as $path)
636 {
Greg Aker3a746652011-04-19 10:59:47 -0500637 if (file_exists($path.'helpers/'.$helper.'.php'))
Barry Mienydd671972010-10-04 16:33:58 +0200638 {
Greg Aker3a746652011-04-19 10:59:47 -0500639 include_once($path.'helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600640
641 $this->_ci_helpers[$helper] = TRUE;
Andrey Andreev90726b82015-01-20 12:39:22 +0200642 log_message('info', 'Helper loaded: '.$helper);
Derek Jones32bf1862010-03-02 13:46:07 -0600643 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 }
645 }
646
Derek Jones32bf1862010-03-02 13:46:07 -0600647 // unable to load the helper
648 if ( ! isset($this->_ci_helpers[$helper]))
649 {
Greg Aker3a746652011-04-19 10:59:47 -0500650 show_error('Unable to load the requested file: helpers/'.$helper.'.php');
Derek Jones32bf1862010-03-02 13:46:07 -0600651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200653
654 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 }
Barry Mienydd671972010-10-04 16:33:58 +0200656
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 /**
660 * Load Helpers
661 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300662 * An alias for the helper() method in case the developer has
663 * written the plural form of it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300665 * @uses CI_Loader::helper()
666 * @param string|string[] $helpers Helper name(s)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200667 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 */
Greg Akerf5c84022011-04-19 17:13:03 -0500669 public function helpers($helpers = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 {
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200671 return $this->helper($helpers);
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 }
Barry Mienydd671972010-10-04 16:33:58 +0200673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200675
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300677 * Language Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300679 * Loads language files.
680 *
681 * @param string|string[] $files List of language file names to load
682 * @param string Language name
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200683 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200685 public function language($files, $lang = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 {
Andrey Andreevd8e31ec2014-11-07 12:17:50 +0200687 get_instance()->lang->load($files, $lang);
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200688 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 }
Barry Mienydd671972010-10-04 16:33:58 +0200690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300694 * Config Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300696 * Loads a config file (an alias for CI_Config::load()).
697 *
698 * @uses CI_Config::load()
699 * @param string $file Configuration file name
700 * @param bool $use_sections Whether configuration values should be loaded into their own section
701 * @param bool $fail_gracefully Whether to just return FALSE or display an error message
702 * @return bool TRUE if the file was loaded correctly or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200704 public function config($file, $use_sections = FALSE, $fail_gracefully = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200705 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200706 return get_instance()->config->load($file, $use_sections, $fail_gracefully);
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 }
708
709 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600710
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300712 * Driver Loader
Derek Jones8dca0412010-03-05 13:01:44 -0600713 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300714 * Loads a driver library.
Derek Jones8dca0412010-03-05 13:01:44 -0600715 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300716 * @param string|string[] $library Driver name(s)
717 * @param array $params Optional parameters to pass to the driver
718 * @param string $object_name An optional object name to assign to
719 *
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200720 * @return object|bool Object or FALSE on failure if $library is a string
721 * and $object_name is set. CI_Loader instance otherwise.
Derek Jones8dca0412010-03-05 13:01:44 -0600722 */
Andrey Andreev9c12b3f2014-01-06 12:32:30 +0200723 public function driver($library, $params = NULL, $object_name = NULL)
Derek Jones8dca0412010-03-05 13:01:44 -0600724 {
Christopher Guineyb54d3552012-03-10 08:38:10 -0800725 if (is_array($library))
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800726 {
Andrey Andreev44d3b182016-02-15 14:37:14 +0200727 foreach ($library as $key => $value)
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800728 {
Andrey Andreev44d3b182016-02-15 14:37:14 +0200729 if (is_int($key))
730 {
731 $this->driver($value, $params);
732 }
733 else
734 {
735 $this->driver($key, $params, $value);
736 }
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800737 }
Christopher Guiney9929d6f2012-03-09 19:53:24 -0800738
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200739 return $this;
740 }
741 elseif (empty($library))
Tom Klingenberg6a15b2d2011-10-07 20:03:30 +0200742 {
743 return FALSE;
744 }
745
vlakofffadb8222013-05-12 10:57:09 +0200746 if ( ! class_exists('CI_Driver_Library', FALSE))
dchill426262d052012-11-24 18:41:13 -0500747 {
748 // We aren't instantiating an object here, just making the base class available
749 require BASEPATH.'libraries/Driver.php';
750 }
751
Derek Jonesd5e0cb52010-03-09 20:20:46 -0600752 // We can save the loader some time since Drivers will *always* be in a subfolder,
753 // and typically identically named to the library
754 if ( ! strpos($library, '/'))
755 {
Greg Akerd25e66a2010-03-28 01:07:09 -0500756 $library = ucfirst($library).'/'.$library;
Derek Jones8dca0412010-03-05 13:01:44 -0600757 }
Barry Mienydd671972010-10-04 16:33:58 +0200758
Derek Jones8dca0412010-03-05 13:01:44 -0600759 return $this->library($library, $params, $object_name);
760 }
761
762 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200763
Derek Jones8dca0412010-03-05 13:01:44 -0600764 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600765 * Add Package Path
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300767 * Prepends a parent path to the library, model, helper and config
768 * path arrays.
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300770 * @see CI_Loader::$_ci_library_paths
771 * @see CI_Loader::$_ci_model_paths
772 * @see CI_Loader::$_ci_helper_paths
773 * @see CI_Config::$_config_paths
774 *
775 * @param string $path Path to add
776 * @param bool $view_cascade (default: TRUE)
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200777 * @return object
Derek Jones32bf1862010-03-02 13:46:07 -0600778 */
Andrey Andreevcce91802012-06-12 13:25:31 +0300779 public function add_package_path($path, $view_cascade = TRUE)
Derek Jones32bf1862010-03-02 13:46:07 -0600780 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500781 $path = rtrim($path, '/').'/';
David Behlercda768a2011-08-14 23:52:48 +0200782
Derek Jones32bf1862010-03-02 13:46:07 -0600783 array_unshift($this->_ci_library_paths, $path);
784 array_unshift($this->_ci_model_paths, $path);
785 array_unshift($this->_ci_helper_paths, $path);
Barry Mienydd671972010-10-04 16:33:58 +0200786
Greg Akerf5c84022011-04-19 17:13:03 -0500787 $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths;
788
Derek Jones32bf1862010-03-02 13:46:07 -0600789 // Add config file path
790 $config =& $this->_ci_get_component('config');
and-ersb3ec9422013-01-03 16:05:12 +0100791 $config->_config_paths[] = $path;
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200792
793 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 }
795
796 // --------------------------------------------------------------------
Derek Jones32bf1862010-03-02 13:46:07 -0600797
798 /**
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000799 * Get Package Paths
800 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300801 * Return a list of all package paths.
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000802 *
vlakoffcdc61132013-05-10 16:47:47 +0200803 * @param bool $include_base Whether to include BASEPATH (default: FALSE)
Andrey Andreeved4b2582012-10-27 17:46:52 +0300804 * @return array
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000805 */
Greg Akerf5c84022011-04-19 17:13:03 -0500806 public function get_package_paths($include_base = FALSE)
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000807 {
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300808 return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths;
Phil Sturgeonde3dbc32010-12-27 17:41:02 +0000809 }
810
811 // --------------------------------------------------------------------
812
813 /**
Derek Jones32bf1862010-03-02 13:46:07 -0600814 * Remove Package Path
815 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300816 * Remove a path from the library, model, helper and/or config
817 * path arrays if it exists. If no path is provided, the most recently
818 * added path will be removed removed.
Derek Jones32bf1862010-03-02 13:46:07 -0600819 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300820 * @param string $path Path to remove
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200821 * @return object
Derek Jones32bf1862010-03-02 13:46:07 -0600822 */
Andrey Andreeved4b2582012-10-27 17:46:52 +0300823 public function remove_package_path($path = '')
Derek Jones32bf1862010-03-02 13:46:07 -0600824 {
825 $config =& $this->_ci_get_component('config');
Barry Mienydd671972010-10-04 16:33:58 +0200826
Alex Bilbieed944a32012-06-02 11:07:47 +0100827 if ($path === '')
Derek Jones32bf1862010-03-02 13:46:07 -0600828 {
Andrey Andreevd7297352012-01-07 22:53:14 +0200829 array_shift($this->_ci_library_paths);
830 array_shift($this->_ci_model_paths);
831 array_shift($this->_ci_helper_paths);
832 array_shift($this->_ci_view_paths);
Korri3684d342012-04-17 00:35:08 -0400833 array_pop($config->_config_paths);
Derek Jones32bf1862010-03-02 13:46:07 -0600834 }
835 else
836 {
Pascal Kriete6b6c2742010-11-09 13:12:22 -0500837 $path = rtrim($path, '/').'/';
Derek Jones32bf1862010-03-02 13:46:07 -0600838 foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
839 {
840 if (($key = array_search($path, $this->{$var})) !== FALSE)
841 {
842 unset($this->{$var}[$key]);
843 }
844 }
David Behlercda768a2011-08-14 23:52:48 +0200845
Greg Akerf5c84022011-04-19 17:13:03 -0500846 if (isset($this->_ci_view_paths[$path.'views/']))
847 {
848 unset($this->_ci_view_paths[$path.'views/']);
849 }
Barry Mienydd671972010-10-04 16:33:58 +0200850
Derek Jones32bf1862010-03-02 13:46:07 -0600851 if (($key = array_search($path, $config->_config_paths)) !== FALSE)
852 {
853 unset($config->_config_paths[$key]);
854 }
855 }
Barry Mienydd671972010-10-04 16:33:58 +0200856
Derek Jones32bf1862010-03-02 13:46:07 -0600857 // make sure the application default paths are still in the array
858 $this->_ci_library_paths = array_unique(array_merge($this->_ci_library_paths, array(APPPATH, BASEPATH)));
859 $this->_ci_helper_paths = array_unique(array_merge($this->_ci_helper_paths, array(APPPATH, BASEPATH)));
860 $this->_ci_model_paths = array_unique(array_merge($this->_ci_model_paths, array(APPPATH)));
Greg Akerf5c84022011-04-19 17:13:03 -0500861 $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE));
Derek Jones32bf1862010-03-02 13:46:07 -0600862 $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH)));
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200863
864 return $this;
Derek Jones32bf1862010-03-02 13:46:07 -0600865 }
866
867 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200868
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +0300870 * Internal CI Data Loader
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300872 * Used to load views and files.
873 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 * Variables are prefixed with _ci_ to avoid symbol collision with
Andrey Andreeved4b2582012-10-27 17:46:52 +0300875 * variables made available to view files.
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 *
Andrey Andreeved4b2582012-10-27 17:46:52 +0300877 * @used-by CI_Loader::view()
878 * @used-by CI_Loader::file()
879 * @param array $_ci_data Data to load
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200880 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 */
Greg Akerf5c84022011-04-19 17:13:03 -0500882 protected function _ci_load($_ci_data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 {
884 // Set the default data variables
885 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
886 {
Andrey Andreev94af3552012-03-26 23:10:42 +0300887 $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 }
David Behlercda768a2011-08-14 23:52:48 +0200889
Greg Akerf5c84022011-04-19 17:13:03 -0500890 $file_exists = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000891
892 // Set the path to the requested file
Alex Bilbie40bd2a72012-06-02 16:04:15 +0100893 if (is_string($_ci_path) && $_ci_path !== '')
Greg Aker8807be32011-04-21 13:06:15 -0500894 {
895 $_ci_x = explode('/', $_ci_path);
896 $_ci_file = end($_ci_x);
897 }
898 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 {
900 $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
Alex Bilbieed944a32012-06-02 11:07:47 +0100901 $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
Greg Akerf5c84022011-04-19 17:13:03 -0500902
Joe McFrederick64f470b2012-08-18 12:29:56 -0400903 foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
Greg Akerf5c84022011-04-19 17:13:03 -0500904 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400905 if (file_exists($_ci_view_file.$_ci_file))
Greg Akerf5c84022011-04-19 17:13:03 -0500906 {
Joe McFrederick64f470b2012-08-18 12:29:56 -0400907 $_ci_path = $_ci_view_file.$_ci_file;
Greg Akerf5c84022011-04-19 17:13:03 -0500908 $file_exists = TRUE;
909 break;
910 }
David Behlercda768a2011-08-14 23:52:48 +0200911
Greg Akerf5c84022011-04-19 17:13:03 -0500912 if ( ! $cascade)
913 {
914 break;
David Behlercda768a2011-08-14 23:52:48 +0200915 }
Greg Akerf5c84022011-04-19 17:13:03 -0500916 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 }
Barry Mienydd671972010-10-04 16:33:58 +0200918
Greg Akerf5c84022011-04-19 17:13:03 -0500919 if ( ! $file_exists && ! file_exists($_ci_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 {
921 show_error('Unable to load the requested file: '.$_ci_file);
922 }
Barry Mienydd671972010-10-04 16:33:58 +0200923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 // This allows anything loaded using $this->load (views, files, etc.)
925 // to become accessible from within the Controller and Model functions.
Pascal Kriete89ace432010-11-10 15:49:10 -0500926 $_ci_CI =& get_instance();
927 foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500929 if ( ! isset($this->$_ci_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 {
Pascal Kriete89ace432010-11-10 15:49:10 -0500931 $this->$_ci_key =& $_ci_CI->$_ci_key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 }
933 }
934
935 /*
936 * Extract and cache variables
937 *
vlakoff02506182012-07-03 07:28:50 +0200938 * You can either set variables using the dedicated $this->load->vars()
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 * function or via the second parameter of this function. We'll merge
940 * the two types and cache them so that views that are embedded within
941 * other views can have access to these variables.
Barry Mienydd671972010-10-04 16:33:58 +0200942 */
Andrey Andreevec8dbbb2017-01-04 17:01:44 +0200943 empty($_ci_vars) OR $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 extract($this->_ci_cached_vars);
Barry Mienydd671972010-10-04 16:33:58 +0200945
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 /*
947 * Buffer the output
948 *
949 * We buffer the output for two reasons:
950 * 1. Speed. You get a significant speed boost.
Andrey Andreevd7297352012-01-07 22:53:14 +0200951 * 2. So that the final rendered template can be post-processed by
dchill425628ba02012-08-08 12:05:45 -0400952 * the output class. Why do we need post processing? For one thing,
953 * in order to show the elapsed page load time. Unless we can
954 * intercept the content right before it's sent to the browser and
955 * then stop the timer it won't be accurate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 */
957 ob_start();
Barry Mienydd671972010-10-04 16:33:58 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 // If the PHP installation does not support short tags we'll
960 // do a little string replacement, changing the short tags
961 // to standard PHP echo statements.
Andrey Andreev07355da2015-07-22 12:46:16 +0300962 if ( ! is_php('5.4') && ! ini_get('short_open_tag') && config_item('rewrite_short_tags') === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
Andrey Andreevd47baab2012-01-09 16:56:46 +0200964 echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
966 else
967 {
968 include($_ci_path); // include() vs include_once() allows for multiple views with the same name
969 }
Barry Mienydd671972010-10-04 16:33:58 +0200970
Andrey Andreev90726b82015-01-20 12:39:22 +0200971 log_message('info', 'File loaded: '.$_ci_path);
Barry Mienydd671972010-10-04 16:33:58 +0200972
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 // Return the file data if requested
974 if ($_ci_return === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200975 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 $buffer = ob_get_contents();
977 @ob_end_clean();
978 return $buffer;
979 }
980
981 /*
982 * Flush the buffer... or buff the flusher?
983 *
984 * In order to permit views to be nested within
985 * other views, we need to flush the content back out whenever
986 * we are beyond the first level of output buffering so that
987 * it can be seen and included properly by the first included
988 * template and any subsequent ones. Oy!
Barry Mienydd671972010-10-04 16:33:58 +0200989 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 if (ob_get_level() > $this->_ci_ob_level + 1)
991 {
992 ob_end_flush();
993 }
994 else
995 {
Greg Aker22f1a632010-11-10 15:34:35 -0600996 $_ci_CI->output->append_output(ob_get_contents());
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 @ob_end_clean();
998 }
Andrey Andreev61a7b8f2014-01-07 13:36:50 +0200999
1000 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 }
1002
1003 // --------------------------------------------------------------------
1004
1005 /**
Andrey Andreevdb669f12015-01-21 16:51:51 +02001006 * Internal CI Library Loader
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001008 * @used-by CI_Loader::library()
Andrey Andreevdb669f12015-01-21 16:51:51 +02001009 * @uses CI_Loader::_ci_init_library()
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001011 * @param string $class Class name to load
1012 * @param mixed $params Optional parameters to pass to the class constructor
1013 * @param string $object_name Optional object name to assign to
Barry Mienydd671972010-10-04 16:33:58 +02001014 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 */
Andrey Andreevdb669f12015-01-21 16:51:51 +02001016 protected function _ci_load_library($class, $params = NULL, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001017 {
1018 // Get the class name, and while we're at it trim any slashes.
1019 // The directory path can be included as part of the class name,
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 // but we don't want a leading slash
Greg Aker3a746652011-04-19 10:59:47 -05001021 $class = str_replace('.php', '', trim($class, '/'));
Barry Mienydd671972010-10-04 16:33:58 +02001022
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 // Was the path included with the class name?
1024 // We look for a slash to determine this
Derek Jones32bf1862010-03-02 13:46:07 -06001025 if (($last_slash = strrpos($class, '/')) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 {
Derek Jones32bf1862010-03-02 13:46:07 -06001027 // Extract the path
Andrey Andreevc26d34f2013-01-28 21:46:08 +02001028 $subdir = substr($class, 0, ++$last_slash);
Barry Mienydd671972010-10-04 16:33:58 +02001029
Derek Jones32bf1862010-03-02 13:46:07 -06001030 // Get the filename from the path
Andrey Andreevd7297352012-01-07 22:53:14 +02001031 $class = substr($class, $last_slash);
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 }
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001033 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 {
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001035 $subdir = '';
1036 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001037
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001038 $class = ucfirst($class);
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001039
Andrey Andreevdb669f12015-01-21 16:51:51 +02001040 // Is this a stock library? There are a few special conditions if so ...
1041 if (file_exists(BASEPATH.'libraries/'.$subdir.$class.'.php'))
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001042 {
Andrey Andreevdb669f12015-01-21 16:51:51 +02001043 return $this->_ci_load_stock_library($class, $subdir, $params, $object_name);
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001044 }
1045
Andrey Andreev894a3f22017-06-27 16:31:17 +03001046 // Safety: Was the class already loaded by a previous call?
1047 if (class_exists($class, FALSE))
1048 {
1049 $property = $object_name;
Andrey Andreevbe0280d2017-06-28 11:49:01 +03001050 if (empty($property))
Andrey Andreev894a3f22017-06-27 16:31:17 +03001051 {
1052 $property = strtolower($class);
Andrey Andreevdb89c202017-06-28 11:18:24 +03001053 isset($this->_ci_varmap[$property]) && $property = $this->_ci_varmap[$property];
Andrey Andreev894a3f22017-06-27 16:31:17 +03001054 }
1055
1056 $CI =& get_instance();
1057 if (isset($CI->$property))
1058 {
1059 log_message('debug', $class.' class already loaded. Second attempt ignored.');
1060 return;
1061 }
1062
1063 return $this->_ci_init_library($class, '', $params, $object_name);
1064 }
1065
vlakoff35672462013-02-15 01:36:04 +01001066 // Let's search for the requested library file and load it.
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001067 foreach ($this->_ci_library_paths as $path)
1068 {
Andrey Andreevdb669f12015-01-21 16:51:51 +02001069 // BASEPATH has already been checked for
1070 if ($path === BASEPATH)
1071 {
1072 continue;
1073 }
1074
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001075 $filepath = $path.'libraries/'.$subdir.$class.'.php';
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001076 // Does the file exist? No? Bummer...
Andrey Andreev894a3f22017-06-27 16:31:17 +03001077 if ( ! file_exists($filepath))
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001078 {
1079 continue;
1080 }
1081
1082 include_once($filepath);
Andrey Andreevdb669f12015-01-21 16:51:51 +02001083 return $this->_ci_init_library($class, '', $params, $object_name);
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001084 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001085
Andrey Andreevd7297352012-01-07 22:53:14 +02001086 // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
Alex Bilbieed944a32012-06-02 11:07:47 +01001087 if ($subdir === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 {
Andrey Andreevdb669f12015-01-21 16:51:51 +02001089 return $this->_ci_load_library($class.'/'.$class, $params, $object_name);
dchill42aee92652012-08-26 21:45:35 -04001090 }
Barry Mienydd671972010-10-04 16:33:58 +02001091
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 // If we got this far we were unable to find the requested class.
Andrey Andreev3608e1a2013-01-28 16:27:30 +02001093 log_message('error', 'Unable to load the requested class: '.$class);
1094 show_error('Unable to load the requested class: '.$class);
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 }
Barry Mienydd671972010-10-04 16:33:58 +02001096
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 // --------------------------------------------------------------------
1098
1099 /**
Andrey Andreevdb669f12015-01-21 16:51:51 +02001100 * Internal CI Stock Library Loader
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 *
Andrey Andreevdb669f12015-01-21 16:51:51 +02001102 * @used-by CI_Loader::_ci_load_library()
1103 * @uses CI_Loader::_ci_init_library()
1104 *
Andrey Andreeve13fa9f2016-05-20 17:30:07 +03001105 * @param string $library_name Library name to load
Andrey Andreevdb669f12015-01-21 16:51:51 +02001106 * @param string $file_path Path to the library filename, relative to libraries/
1107 * @param mixed $params Optional parameters to pass to the class constructor
1108 * @param string $object_name Optional object name to assign to
1109 * @return void
1110 */
1111 protected function _ci_load_stock_library($library_name, $file_path, $params, $object_name)
1112 {
1113 $prefix = 'CI_';
1114
1115 if (class_exists($prefix.$library_name, FALSE))
1116 {
1117 if (class_exists(config_item('subclass_prefix').$library_name, FALSE))
1118 {
1119 $prefix = config_item('subclass_prefix');
1120 }
1121
Andrey Andreevbe0280d2017-06-28 11:49:01 +03001122 $property = $object_name;
1123 if (empty($property))
Andrey Andreevdb669f12015-01-21 16:51:51 +02001124 {
Andrey Andreevbe0280d2017-06-28 11:49:01 +03001125 $property = strtolower($library_name);
1126 isset($this->_ci_varmap[$property]) && $property = $this->_ci_varmap[$property];
1127 }
1128
1129 $CI =& get_instance();
1130 if ( ! isset($CI->$property))
1131 {
1132 return $this->_ci_init_library($library_name, $prefix, $params, $object_name);
Andrey Andreevdb669f12015-01-21 16:51:51 +02001133 }
1134
1135 log_message('debug', $library_name.' class already loaded. Second attempt ignored.');
1136 return;
1137 }
Andrey Andreev8f5c1782015-03-25 13:41:02 +02001138
1139 $paths = $this->_ci_library_paths;
1140 array_pop($paths); // BASEPATH
1141 array_pop($paths); // APPPATH (needs to be the first path checked)
1142 array_unshift($paths, APPPATH);
1143
1144 foreach ($paths as $path)
Andrey Andreevdb669f12015-01-21 16:51:51 +02001145 {
Andrey Andreev8f5c1782015-03-25 13:41:02 +02001146 if (file_exists($path = $path.'libraries/'.$file_path.$library_name.'.php'))
Andrey Andreevdb669f12015-01-21 16:51:51 +02001147 {
Andrey Andreev8f5c1782015-03-25 13:41:02 +02001148 // Override
1149 include_once($path);
1150 if (class_exists($prefix.$library_name, FALSE))
1151 {
1152 return $this->_ci_init_library($library_name, $prefix, $params, $object_name);
1153 }
Andrey Andreevc58b0052017-06-28 11:50:27 +03001154
1155 log_message('debug', $path.' exists, but does not declare '.$prefix.$library_name);
Andrey Andreevdb669f12015-01-21 16:51:51 +02001156 }
1157 }
1158
1159 include_once(BASEPATH.'libraries/'.$file_path.$library_name.'.php');
1160
1161 // Check for extensions
1162 $subclass = config_item('subclass_prefix').$library_name;
Andrey Andreev8f5c1782015-03-25 13:41:02 +02001163 foreach ($paths as $path)
Andrey Andreevdb669f12015-01-21 16:51:51 +02001164 {
Andrey Andreev8f5c1782015-03-25 13:41:02 +02001165 if (file_exists($path = $path.'libraries/'.$file_path.$subclass.'.php'))
Andrey Andreevdb669f12015-01-21 16:51:51 +02001166 {
Andrey Andreev8f5c1782015-03-25 13:41:02 +02001167 include_once($path);
1168 if (class_exists($subclass, FALSE))
1169 {
1170 $prefix = config_item('subclass_prefix');
1171 break;
1172 }
Andrey Andreevc58b0052017-06-28 11:50:27 +03001173
1174 log_message('debug', $path.' exists, but does not declare '.$subclass);
Andrey Andreevdb669f12015-01-21 16:51:51 +02001175 }
1176 }
1177
1178 return $this->_ci_init_library($library_name, $prefix, $params, $object_name);
1179 }
1180
1181 // --------------------------------------------------------------------
1182
1183 /**
1184 * Internal CI Library Instantiator
1185 *
1186 * @used-by CI_Loader::_ci_load_stock_library()
1187 * @used-by CI_Loader::_ci_load_library()
Andrey Andreeved4b2582012-10-27 17:46:52 +03001188 *
1189 * @param string $class Class name
1190 * @param string $prefix Class name prefix
1191 * @param array|null|bool $config Optional configuration to pass to the class constructor:
1192 * FALSE to skip;
1193 * NULL to search in config paths;
1194 * array containing configuration data
1195 * @param string $object_name Optional object name to assign to
Andrey Andreev94af3552012-03-26 23:10:42 +03001196 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 */
Andrey Andreevdb669f12015-01-21 16:51:51 +02001198 protected function _ci_init_library($class, $prefix, $config = FALSE, $object_name = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001199 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001200 // Is there an associated config file for this class? Note: these should always be lowercase
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 if ($config === NULL)
1202 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001203 // Fetch the config paths containing any package paths
1204 $config_component = $this->_ci_get_component('config');
1205
1206 if (is_array($config_component->_config_paths))
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 {
Andrey Andreev95c31ad2014-12-17 19:01:31 +02001208 $found = FALSE;
Eric Barnes5e16ec62011-01-04 17:25:23 -05001209 foreach ($config_component->_config_paths as $path)
1210 {
1211 // We test for both uppercase and lowercase, for servers that
Andrey Andreev95c31ad2014-12-17 19:01:31 +02001212 // are case-sensitive with regard to file names. Load global first,
1213 // override with environment next
1214 if (file_exists($path.'config/'.strtolower($class).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001215 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001216 include($path.'config/'.strtolower($class).'.php');
Andrey Andreev95c31ad2014-12-17 19:01:31 +02001217 $found = TRUE;
Eric Barnes5e16ec62011-01-04 17:25:23 -05001218 }
Andrey Andreev94af3552012-03-26 23:10:42 +03001219 elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php'))
Eric Barnes5e16ec62011-01-04 17:25:23 -05001220 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001221 include($path.'config/'.ucfirst(strtolower($class)).'.php');
Andrey Andreev95c31ad2014-12-17 19:01:31 +02001222 $found = TRUE;
1223 }
1224
1225 if (file_exists($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php'))
1226 {
1227 include($path.'config/'.ENVIRONMENT.'/'.strtolower($class).'.php');
1228 $found = TRUE;
1229 }
1230 elseif (file_exists($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php'))
1231 {
1232 include($path.'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).'.php');
1233 $found = TRUE;
1234 }
1235
1236 // Break on the first found configuration, thus package
1237 // files are not overridden by default paths
1238 if ($found === TRUE)
1239 {
Eric Barnes5e16ec62011-01-04 17:25:23 -05001240 break;
1241 }
1242 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 }
1244 }
Barry Mienydd671972010-10-04 16:33:58 +02001245
Andrey Andreevdb669f12015-01-21 16:51:51 +02001246 $class_name = $prefix.$class;
Barry Mienydd671972010-10-04 16:33:58 +02001247
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 // Is the class name valid?
Andrey Andreevdb669f12015-01-21 16:51:51 +02001249 if ( ! class_exists($class_name, FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 {
Andrey Andreevdb669f12015-01-21 16:51:51 +02001251 log_message('error', 'Non-existent class: '.$class_name);
1252 show_error('Non-existent class: '.$class_name);
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 }
Barry Mienydd671972010-10-04 16:33:58 +02001254
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 // Set the variable name we will assign the class to
Andrey Andreevd7297352012-01-07 22:53:14 +02001256 // Was a custom class name supplied? If so we'll use it
Andrey Andreev519f87a2013-07-23 17:16:10 +03001257 if (empty($object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 {
Andrey Andreev519f87a2013-07-23 17:16:10 +03001259 $object_name = strtolower($class);
1260 if (isset($this->_ci_varmap[$object_name]))
1261 {
1262 $object_name = $this->_ci_varmap[$object_name];
1263 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 }
Andrey Andreev519f87a2013-07-23 17:16:10 +03001265
1266 // Don't overwrite existing properties
1267 $CI =& get_instance();
1268 if (isset($CI->$object_name))
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 {
Andrey Andreevdb669f12015-01-21 16:51:51 +02001270 if ($CI->$object_name instanceof $class_name)
Andrey Andreev519f87a2013-07-23 17:16:10 +03001271 {
Andrey Andreevdb669f12015-01-21 16:51:51 +02001272 log_message('debug', $class_name." has already been instantiated as '".$object_name."'. Second attempt aborted.");
Andrey Andreev519f87a2013-07-23 17:16:10 +03001273 return;
1274 }
1275
Andrey Andreevdb669f12015-01-21 16:51:51 +02001276 show_error("Resource '".$object_name."' already exists and is not a ".$class_name." instance.");
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 }
1278
Barry Mienydd671972010-10-04 16:33:58 +02001279 // Save the class name and object name
Andrey Andreev519f87a2013-07-23 17:16:10 +03001280 $this->_ci_classes[$object_name] = $class;
Derek Allard2067d1a2008-11-13 22:59:24 +00001281
Barry Mienydd671972010-10-04 16:33:58 +02001282 // Instantiate the class
Andrey Andreev519f87a2013-07-23 17:16:10 +03001283 $CI->$object_name = isset($config)
Andrey Andreevdb669f12015-01-21 16:51:51 +02001284 ? new $class_name($config)
1285 : new $class_name();
Barry Mienydd671972010-10-04 16:33:58 +02001286 }
1287
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001289
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001291 * CI Autoloader
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001293 * Loads component listed in the config/autoload.php file.
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 *
Andrey Andreevcdac2482012-11-03 18:09:01 +02001295 * @used-by CI_Loader::initialize()
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 * @return void
1297 */
Shane Pearson665baec2011-08-22 18:52:19 -05001298 protected function _ci_autoloader()
Barry Mienydd671972010-10-04 16:33:58 +02001299 {
Andrey Andreev4b6469d2015-01-22 16:42:17 +02001300 if (file_exists(APPPATH.'config/autoload.php'))
Andrey Andreevbd6a8142015-01-22 16:41:25 +02001301 {
1302 include(APPPATH.'config/autoload.php');
1303 }
1304
Andrey Andreevdb529ca2013-01-28 11:00:02 +02001305 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
bubbafoley0ea04142011-03-17 14:55:41 -05001306 {
Shane Pearson6adfe632011-08-10 16:42:53 -05001307 include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
bubbafoley0ea04142011-03-17 14:55:41 -05001308 }
Barry Mienydd671972010-10-04 16:33:58 +02001309
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 if ( ! isset($autoload))
1311 {
Gabriel Potkányd9287a02015-02-04 08:47:56 +01001312 return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 }
Barry Mienydd671972010-10-04 16:33:58 +02001314
Phil Sturgeon9730c752010-12-15 10:50:15 +00001315 // Autoload packages
1316 if (isset($autoload['packages']))
1317 {
1318 foreach ($autoload['packages'] as $package_path)
1319 {
1320 $this->add_package_path($package_path);
1321 }
1322 }
1323
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 // Load any custom config file
1325 if (count($autoload['config']) > 0)
Barry Mienydd671972010-10-04 16:33:58 +02001326 {
Andrey Andreev623227f2014-03-24 12:46:25 +02001327 foreach ($autoload['config'] as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 {
Andrey Andreev689d41c2014-03-24 17:28:40 +02001329 $this->config($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 }
Barry Mienydd671972010-10-04 16:33:58 +02001331 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001332
Derek Jonesc6da5032010-03-09 20:44:27 -06001333 // Autoload helpers and languages
1334 foreach (array('helper', 'language') as $type)
Barry Mienydd671972010-10-04 16:33:58 +02001335 {
Andrey Andreev94af3552012-03-26 23:10:42 +03001336 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 {
1338 $this->$type($autoload[$type]);
Barry Mienydd671972010-10-04 16:33:58 +02001339 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 }
1341
Andrey Andreev9ab70a82013-07-23 00:09:26 +03001342 // Autoload drivers
1343 if (isset($autoload['drivers']))
1344 {
Andrey Andreev44d3b182016-02-15 14:37:14 +02001345 $this->driver($autoload['drivers']);
Andrey Andreev9ab70a82013-07-23 00:09:26 +03001346 }
1347
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 // Load libraries
Andrey Andreev94af3552012-03-26 23:10:42 +03001349 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 {
1351 // Load the database driver.
1352 if (in_array('database', $autoload['libraries']))
1353 {
1354 $this->database();
1355 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
1356 }
Barry Mienydd671972010-10-04 16:33:58 +02001357
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 // Load all other libraries
Andrey Andreeve36d0482015-04-04 21:55:09 +03001359 $this->library($autoload['libraries']);
Barry Mienydd671972010-10-04 16:33:58 +02001360 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001361
1362 // Autoload models
1363 if (isset($autoload['model']))
1364 {
1365 $this->model($autoload['model']);
1366 }
Barry Mienydd671972010-10-04 16:33:58 +02001367 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001368
1369 // --------------------------------------------------------------------
1370
1371 /**
Andrey Andreevec8dbbb2017-01-04 17:01:44 +02001372 * Prepare variables for _ci_vars, to be later extract()-ed inside views
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 *
Andrey Andreevec8dbbb2017-01-04 17:01:44 +02001374 * Converts objects to associative arrays and filters-out internal
Chris Faulknerb9ac1a12017-01-10 16:02:40 +10301375 * variable names (i.e. keys prefixed with '_ci_').
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 *
Andrey Andreevec8dbbb2017-01-04 17:01:44 +02001377 * @param mixed $vars
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 * @return array
1379 */
Andrey Andreevec8dbbb2017-01-04 17:01:44 +02001380 protected function _ci_prepare_view_vars($vars)
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 {
Andrey Andreevec8dbbb2017-01-04 17:01:44 +02001382 if ( ! is_array($vars))
1383 {
1384 $vars = is_object($vars)
Chris Faulknerb9ac1a12017-01-10 16:02:40 +10301385 ? get_object_vars($vars)
Andrey Andreevec8dbbb2017-01-04 17:01:44 +02001386 : array();
1387 }
1388
1389 foreach (array_keys($vars) as $key)
1390 {
1391 if (strncmp($key, '_ci_', 4) === 0)
1392 {
1393 unset($vars[$key]);
1394 }
1395 }
1396
1397 return $vars;
Derek Allard2067d1a2008-11-13 22:59:24 +00001398 }
1399
1400 // --------------------------------------------------------------------
1401
1402 /**
Andrey Andreeved4b2582012-10-27 17:46:52 +03001403 * CI Component getter
Derek Jones32bf1862010-03-02 13:46:07 -06001404 *
Andrey Andreeved4b2582012-10-27 17:46:52 +03001405 * Get a reference to a specific library or model.
1406 *
1407 * @param string $component Component name
Derek Jones32bf1862010-03-02 13:46:07 -06001408 * @return bool
1409 */
Greg Akerf5c84022011-04-19 17:13:03 -05001410 protected function &_ci_get_component($component)
Derek Jones32bf1862010-03-02 13:46:07 -06001411 {
Pascal Kriete89ace432010-11-10 15:49:10 -05001412 $CI =& get_instance();
1413 return $CI->$component;
Derek Jones32bf1862010-03-02 13:46:07 -06001414 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001415}