Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | d729735 | 2012-01-07 22:53:14 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | d729735 | 2012-01-07 22:53:14 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * 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 Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Loader Class |
| 42 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 43 | * Loads framework components. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | * |
| 45 | * @package CodeIgniter |
| 46 | * @subpackage Libraries |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | * @category Loader |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 48 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 49 | * @link https://codeigniter.com/user_guide/libraries/loader.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | */ |
| 51 | class CI_Loader { |
| 52 | |
| 53 | // All these are set automatically. Don't mess with them. |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 54 | /** |
| 55 | * Nesting level of the output buffering mechanism |
| 56 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 57 | * @var int |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 58 | */ |
Greg Aker | 0c9ee4a | 2011-04-20 09:40:17 -0500 | [diff] [blame] | 59 | protected $_ci_ob_level; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 60 | |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 61 | /** |
| 62 | * List of paths to load views from |
| 63 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 64 | * @var array |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 65 | */ |
Ted Wood | 4c22364 | 2013-01-05 16:50:31 -0800 | [diff] [blame] | 66 | protected $_ci_view_paths = array(VIEWPATH => TRUE); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 67 | |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 68 | /** |
| 69 | * List of paths to load libraries from |
| 70 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 71 | * @var array |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 72 | */ |
Ted Wood | 4c22364 | 2013-01-05 16:50:31 -0800 | [diff] [blame] | 73 | protected $_ci_library_paths = array(APPPATH, BASEPATH); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 74 | |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 75 | /** |
| 76 | * List of paths to load models from |
| 77 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 78 | * @var array |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 79 | */ |
Ted Wood | 4c22364 | 2013-01-05 16:50:31 -0800 | [diff] [blame] | 80 | protected $_ci_model_paths = array(APPPATH); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 81 | |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 82 | /** |
| 83 | * List of paths to load helpers from |
| 84 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 85 | * @var array |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 86 | */ |
Ted Wood | 4c22364 | 2013-01-05 16:50:31 -0800 | [diff] [blame] | 87 | protected $_ci_helper_paths = array(APPPATH, BASEPATH); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 88 | |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 89 | /** |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 90 | * List of cached variables |
| 91 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 92 | * @var array |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 93 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 94 | protected $_ci_cached_vars = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 95 | |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 96 | /** |
| 97 | * List of loaded classes |
| 98 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 99 | * @var array |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 100 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 101 | protected $_ci_classes = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 102 | |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 103 | /** |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 104 | * List of loaded models |
| 105 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 106 | * @var array |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 107 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 108 | protected $_ci_models = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 109 | |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 110 | /** |
| 111 | * List of loaded helpers |
| 112 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 113 | * @var array |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 114 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 115 | protected $_ci_helpers = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 116 | |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 117 | /** |
| 118 | * List of class name mappings |
| 119 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 120 | * @var array |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 121 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 122 | protected $_ci_varmap = array( |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 123 | 'unit_test' => 'unit', |
| 124 | 'user_agent' => 'agent' |
| 125 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | |
Andrey Andreev | 519f87a | 2013-07-23 17:16:10 +0300 | [diff] [blame] | 127 | // -------------------------------------------------------------------- |
| 128 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | /** |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 130 | * Class constructor |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | * |
Andrey Andreev | cdac248 | 2012-11-03 18:09:01 +0200 | [diff] [blame] | 132 | * Sets component load paths, gets the initial output buffering level. |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 133 | * |
| 134 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | */ |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 136 | public function __construct() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 137 | { |
Andrey Andreev | 519f87a | 2013-07-23 17:16:10 +0300 | [diff] [blame] | 138 | $this->_ci_ob_level = ob_get_level(); |
| 139 | $this->_ci_classes =& is_loaded(); |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 140 | |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 141 | log_message('info', 'Loader Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 143 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | // -------------------------------------------------------------------- |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 145 | |
Greg Aker | 0c9ee4a | 2011-04-20 09:40:17 -0500 | [diff] [blame] | 146 | /** |
Andrey Andreev | cdac248 | 2012-11-03 18:09:01 +0200 | [diff] [blame] | 147 | * 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 Andreev | cdac248 | 2012-11-03 18:09:01 +0200 | [diff] [blame] | 157 | $this->_ci_autoloader(); |
| 158 | } |
| 159 | |
| 160 | // -------------------------------------------------------------------- |
| 161 | |
| 162 | /** |
Greg Aker | 0c9ee4a | 2011-04-20 09:40:17 -0500 | [diff] [blame] | 163 | * Is Loaded |
| 164 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 165 | * A utility method to test if a class is in the self::$_ci_classes array. |
Greg Aker | 0c9ee4a | 2011-04-20 09:40:17 -0500 | [diff] [blame] | 166 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 167 | * @used-by Mainly used by Form Helper function _get_validation_object(). |
Greg Aker | 0c9ee4a | 2011-04-20 09:40:17 -0500 | [diff] [blame] | 168 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 169 | * @param string $class Class name to check for |
| 170 | * @return string|bool Class object name if loaded or FALSE |
Greg Aker | 0c9ee4a | 2011-04-20 09:40:17 -0500 | [diff] [blame] | 171 | */ |
| 172 | public function is_loaded($class) |
| 173 | { |
Andrey Andreev | 519f87a | 2013-07-23 17:16:10 +0300 | [diff] [blame] | 174 | return array_search(ucfirst($class), $this->_ci_classes, TRUE); |
Greg Aker | 0c9ee4a | 2011-04-20 09:40:17 -0500 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 178 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | /** |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 180 | * Library Loader |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 182 | * Loads and instantiates libraries. |
| 183 | * Designed to be called from application controllers. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | * |
Andrey Andreev | 9bceb9c | 2017-07-06 15:12:09 +0300 | [diff] [blame] | 185 | * @param mixed $library Library name |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 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 Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 188 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | */ |
Andrey Andreev | 9c12b3f | 2014-01-06 12:32:30 +0200 | [diff] [blame] | 190 | public function library($library, $params = NULL, $object_name = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | { |
Andrey Andreev | 519f87a | 2013-07-23 17:16:10 +0300 | [diff] [blame] | 192 | if (empty($library)) |
| 193 | { |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 194 | return $this; |
Andrey Andreev | 519f87a | 2013-07-23 17:16:10 +0300 | [diff] [blame] | 195 | } |
| 196 | elseif (is_array($library)) |
Greg Aker | ce43396 | 2010-10-12 09:29:35 -0500 | [diff] [blame] | 197 | { |
Andrey Andreev | 88cf55b | 2014-01-17 15:38:30 +0200 | [diff] [blame] | 198 | foreach ($library as $key => $value) |
Greg Aker | ce43396 | 2010-10-12 09:29:35 -0500 | [diff] [blame] | 199 | { |
Andrey Andreev | 88cf55b | 2014-01-17 15:38:30 +0200 | [diff] [blame] | 200 | if (is_int($key)) |
| 201 | { |
| 202 | $this->library($value, $params); |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | $this->library($key, $params, $value); |
| 207 | } |
Greg Aker | ce43396 | 2010-10-12 09:29:35 -0500 | [diff] [blame] | 208 | } |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 209 | |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 210 | return $this; |
Greg Aker | ce43396 | 2010-10-12 09:29:35 -0500 | [diff] [blame] | 211 | } |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 212 | |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 213 | if ($params !== NULL && ! is_array($params)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | { |
| 215 | $params = NULL; |
| 216 | } |
| 217 | |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 218 | $this->_ci_load_library($library, $params, $object_name); |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 219 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | /** |
| 225 | * Model Loader |
| 226 | * |
James | 7b272ff | 2014-03-20 19:02:37 +0000 | [diff] [blame] | 227 | * Loads and instantiates models. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | * |
Andrey Andreev | 6cfc6a9 | 2017-11-03 13:38:13 +0200 | [diff] [blame] | 229 | * @param mixed $model Model name |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 230 | * @param string $name An optional object name to assign to |
| 231 | * @param bool $db_conn An optional database connection configuration to initialize |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 232 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 233 | */ |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 234 | public function model($model, $name = '', $db_conn = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 235 | { |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 236 | if (empty($model)) |
| 237 | { |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 238 | return $this; |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 239 | } |
| 240 | elseif (is_array($model)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 241 | { |
Andrey Andreev | 5a519db | 2013-01-12 04:19:19 +0200 | [diff] [blame] | 242 | foreach ($model as $key => $value) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | { |
Ahmad Anbar | 4005e3c | 2013-09-12 23:33:28 +0300 | [diff] [blame] | 244 | is_int($key) ? $this->model($value, '', $db_conn) : $this->model($key, $value, $db_conn); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 245 | } |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 246 | |
| 247 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 250 | $path = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 251 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | // Is the model in a sub-folder? If so, parse out the filename and path. |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 253 | if (($last_slash = strrpos($model, '/')) !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | { |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 255 | // The path is in front of the last slash |
Andrey Andreev | d47baab | 2012-01-09 16:56:46 +0200 | [diff] [blame] | 256 | $path = substr($model, 0, ++$last_slash); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 257 | |
| 258 | // And the model name behind it |
Andrey Andreev | d47baab | 2012-01-09 16:56:46 +0200 | [diff] [blame] | 259 | $model = substr($model, $last_slash); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 261 | |
Phil Sturgeon | 10d78f6 | 2012-06-04 14:41:53 -0500 | [diff] [blame] | 262 | if (empty($name)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | { |
| 264 | $name = $model; |
| 265 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 266 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | if (in_array($name, $this->_ci_models, TRUE)) |
| 268 | { |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 269 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 271 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | $CI =& get_instance(); |
| 273 | if (isset($CI->$name)) |
| 274 | { |
Andrey Andreev | b63dc19 | 2015-07-22 13:14:50 +0300 | [diff] [blame] | 275 | throw new RuntimeException('The model name you are loading is the name of a resource that is already being used: '.$name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 277 | |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 278 | if ($db_conn !== FALSE && ! class_exists('CI_DB', FALSE)) |
| 279 | { |
| 280 | if ($db_conn === TRUE) |
| 281 | { |
| 282 | $db_conn = ''; |
| 283 | } |
| 284 | |
Andrey Andreev | 5f83969 | 2014-03-24 17:34:54 +0200 | [diff] [blame] | 285 | $this->database($db_conn, FALSE, TRUE); |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 286 | } |
| 287 | |
Andrey Andreev | 0b1efb3 | 2016-01-04 12:34:14 +0200 | [diff] [blame] | 288 | // 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 Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 296 | if ( ! class_exists('CI_Model', FALSE)) |
| 297 | { |
Andrey Andreev | 0b1efb3 | 2016-01-04 12:34:14 +0200 | [diff] [blame] | 298 | $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 Andreev | c942084 | 2017-11-10 15:12:04 +0200 | [diff] [blame] | 306 | |
| 307 | log_message('info', 'CI_Model class loaded'); |
Andrey Andreev | 0b1efb3 | 2016-01-04 12:34:14 +0200 | [diff] [blame] | 308 | } |
| 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 Andreev | c942084 | 2017-11-10 15:12:04 +0200 | [diff] [blame] | 322 | |
| 323 | log_message('info', config_item('subclass_prefix').'Model class loaded'); |
Andrey Andreev | 0b1efb3 | 2016-01-04 12:34:14 +0200 | [diff] [blame] | 324 | } |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 325 | } |
| 326 | |
Andrey Andreev | 825fab7 | 2015-08-17 09:52:42 +0300 | [diff] [blame] | 327 | $model = ucfirst($model); |
Andrey Andreev | 738b9e3 | 2016-02-24 12:14:10 +0200 | [diff] [blame] | 328 | if ( ! class_exists($model, FALSE)) |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 329 | { |
Andrey Andreev | b63dc19 | 2015-07-22 13:14:50 +0300 | [diff] [blame] | 330 | foreach ($this->_ci_model_paths as $mod_path) |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 331 | { |
Andrey Andreev | b63dc19 | 2015-07-22 13:14:50 +0300 | [diff] [blame] | 332 | 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 Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 344 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | |
Andrey Andreev | b63dc19 | 2015-07-22 13:14:50 +0300 | [diff] [blame] | 346 | 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 Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 354 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 355 | |
Andrey Andreev | b63dc19 | 2015-07-22 13:14:50 +0300 | [diff] [blame] | 356 | $this->_ci_models[] = $name; |
Andrey Andreev | c942084 | 2017-11-10 15:12:04 +0200 | [diff] [blame] | 357 | $model = new $model(); |
| 358 | $CI->$name = $model; |
| 359 | log_message('info', 'Model "'.get_class($model).'" initialized'); |
Andrey Andreev | b63dc19 | 2015-07-22 13:14:50 +0300 | [diff] [blame] | 360 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 362 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 364 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | /** |
| 366 | * Database Loader |
| 367 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 368 | * @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 Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 373 | * @return object|bool Database object if $return is set to TRUE, |
| 374 | * FALSE on failure, CI_Loader instance in any other case |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 375 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 376 | public function database($params = '', $return = FALSE, $query_builder = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | { |
| 378 | // Grab the super object |
| 379 | $CI =& get_instance(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 380 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 381 | // Do we even need to load the database class? |
Andrey Andreev | 9d0ab04 | 2012-10-24 21:47:39 +0300 | [diff] [blame] | 382 | if ($return === FALSE && $query_builder === NULL && isset($CI->db) && is_object($CI->db) && ! empty($CI->db->conn_id)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | { |
| 384 | return FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 385 | } |
| 386 | |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 387 | require_once(BASEPATH.'database/DB.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | |
| 389 | if ($return === TRUE) |
| 390 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 391 | return DB($params, $query_builder); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 393 | |
Andrey Andreev | d729735 | 2012-01-07 22:53:14 +0200 | [diff] [blame] | 394 | // Initialize the db variable. Needed to prevent |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | // reference errors with some configurations |
| 396 | $CI->db = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 397 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | // Load the DB class |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 399 | $CI->db =& DB($params, $query_builder); |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 400 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 402 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | // -------------------------------------------------------------------- |
| 404 | |
| 405 | /** |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 406 | * Load the Database Utilities Class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | * |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 408 | * @param object $db Database object |
Andrey Andreev | 9c12b3f | 2014-01-06 12:32:30 +0200 | [diff] [blame] | 409 | * @param bool $return Whether to return the DB Utilities class object or not |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 410 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 411 | */ |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 412 | public function dbutil($db = NULL, $return = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | $CI =& get_instance(); |
| 415 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 416 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 421 | |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 422 | require_once(BASEPATH.'database/DB_utility.php'); |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 423 | require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_utility.php'); |
| 424 | $class = 'CI_DB_'.$db->dbdriver.'_utility'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 426 | if ($return === TRUE) |
| 427 | { |
| 428 | return new $class($db); |
| 429 | } |
| 430 | |
| 431 | $CI->dbutil = new $class($db); |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 432 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 434 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | // -------------------------------------------------------------------- |
| 436 | |
| 437 | /** |
| 438 | * Load the Database Forge Class |
| 439 | * |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 440 | * @param object $db Database object |
| 441 | * @param bool $return Whether to return the DB Forge class object or not |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 442 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 443 | */ |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 444 | public function dbforge($db = NULL, $return = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | { |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 446 | $CI =& get_instance(); |
| 447 | if ( ! is_object($db) OR ! ($db instanceof CI_DB)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | { |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 449 | class_exists('CI_DB', FALSE) OR $this->database(); |
| 450 | $db =& $CI->db; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 452 | |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 453 | require_once(BASEPATH.'database/DB_forge.php'); |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 454 | require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_forge.php'); |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 455 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 456 | if ( ! empty($db->subdriver)) |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 457 | { |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 458 | $driver_path = BASEPATH.'database/drivers/'.$db->dbdriver.'/subdrivers/'.$db->dbdriver.'_'.$db->subdriver.'_forge.php'; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 459 | if (file_exists($driver_path)) |
| 460 | { |
| 461 | require_once($driver_path); |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 462 | $class = 'CI_DB_'.$db->dbdriver.'_'.$db->subdriver.'_forge'; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | else |
| 466 | { |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 467 | $class = 'CI_DB_'.$db->dbdriver.'_forge'; |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 468 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 469 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 470 | if ($return === TRUE) |
| 471 | { |
| 472 | return new $class($db); |
| 473 | } |
| 474 | |
| 475 | $CI->dbforge = new $class($db); |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 476 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 477 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 478 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 479 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 480 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | /** |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 482 | * View Loader |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 483 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 484 | * Loads "view" files. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 485 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 486 | * @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 Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 491 | * @return object|string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 492 | */ |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 493 | public function view($view, $vars = array(), $return = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 494 | { |
Andrey Andreev | ec8dbbb | 2017-01-04 17:01:44 +0200 | [diff] [blame] | 495 | return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 496 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 497 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 498 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 499 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | /** |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 501 | * Generic File Loader |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 503 | * @param string $path File path |
| 504 | * @param bool $return Whether to return the file output |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 505 | * @return object|string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 506 | */ |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 507 | public function file($path, $return = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | { |
| 509 | return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return)); |
| 510 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 511 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 512 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 513 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 514 | /** |
| 515 | * Set Variables |
| 516 | * |
| 517 | * Once variables are set they become available within |
| 518 | * the controller class and its "view" files. |
| 519 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 520 | * @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 Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 524 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 525 | */ |
Andrey Andreev | 9c12b3f | 2014-01-06 12:32:30 +0200 | [diff] [blame] | 526 | public function vars($vars, $val = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 527 | { |
Andrey Andreev | ec8dbbb | 2017-01-04 17:01:44 +0200 | [diff] [blame] | 528 | $vars = is_string($vars) |
| 529 | ? array($vars => $val) |
| 530 | : $this->_ci_prepare_view_vars($vars); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 531 | |
Andrey Andreev | ec8dbbb | 2017-01-04 17:01:44 +0200 | [diff] [blame] | 532 | foreach ($vars as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 533 | { |
Andrey Andreev | ec8dbbb | 2017-01-04 17:01:44 +0200 | [diff] [blame] | 534 | $this->_ci_cached_vars[$key] = $val; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | } |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 536 | |
| 537 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 538 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 539 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 540 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 541 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 542 | /** |
Tomaz Lovrec | a81a5ef | 2013-10-16 11:29:57 +0200 | [diff] [blame] | 543 | * Clear Cached Variables |
Andrey Andreev | af01fb1 | 2014-01-03 16:57:44 +0200 | [diff] [blame] | 544 | * |
Tomaz Lovrec | a81a5ef | 2013-10-16 11:29:57 +0200 | [diff] [blame] | 545 | * Clears the cached variables. |
Andrey Andreev | af01fb1 | 2014-01-03 16:57:44 +0200 | [diff] [blame] | 546 | * |
Andrey Andreev | 10fb7d1 | 2015-08-03 10:05:29 +0300 | [diff] [blame] | 547 | * @return CI_Loader |
Tomaz Lovrec | a81a5ef | 2013-10-16 11:29:57 +0200 | [diff] [blame] | 548 | */ |
| 549 | public function clear_vars() |
| 550 | { |
| 551 | $this->_ci_cached_vars = array(); |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 552 | return $this; |
Tomaz Lovrec | a81a5ef | 2013-10-16 11:29:57 +0200 | [diff] [blame] | 553 | } |
Andrey Andreev | af01fb1 | 2014-01-03 16:57:44 +0200 | [diff] [blame] | 554 | |
Tomaz Lovrec | a81a5ef | 2013-10-16 11:29:57 +0200 | [diff] [blame] | 555 | // -------------------------------------------------------------------- |
| 556 | |
| 557 | /** |
Phil Sturgeon | 8731f64 | 2011-07-22 16:11:34 -0600 | [diff] [blame] | 558 | * Get Variable |
| 559 | * |
| 560 | * Check if a variable is set and retrieve it. |
| 561 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 562 | * @param string $key Variable name |
| 563 | * @return mixed The variable or NULL if not found |
Phil Sturgeon | 8731f64 | 2011-07-22 16:11:34 -0600 | [diff] [blame] | 564 | */ |
| 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 Pearson | 81dd223 | 2011-11-18 20:49:35 -0600 | [diff] [blame] | 573 | * Get Variables |
| 574 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 575 | * Retrieves all loaded variables. |
Shane Pearson | 81dd223 | 2011-11-18 20:49:35 -0600 | [diff] [blame] | 576 | * |
| 577 | * @return array |
| 578 | */ |
| 579 | public function get_vars() |
| 580 | { |
| 581 | return $this->_ci_cached_vars; |
| 582 | } |
| 583 | |
| 584 | // -------------------------------------------------------------------- |
| 585 | |
| 586 | /** |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 587 | * Helper Loader |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 588 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 589 | * @param string|string[] $helpers Helper name(s) |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 590 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 591 | */ |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 592 | public function helper($helpers = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 593 | { |
Andrey Andreev | 4015f9b | 2016-11-14 10:22:59 +0200 | [diff] [blame] | 594 | is_array($helpers) OR $helpers = array($helpers); |
| 595 | foreach ($helpers as &$helper) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 596 | { |
Andrey Andreev | 4015f9b | 2016-11-14 10:22:59 +0200 | [diff] [blame] | 597 | $filename = basename($helper); |
| 598 | $filepath = ($filename === $helper) ? '' : substr($helper, 0, strlen($helper) - strlen($filename)); |
Andrey Andreev | 99d05f0 | 2017-01-11 17:04:06 +0200 | [diff] [blame] | 599 | $filename = strtolower(preg_replace('#(_helper)?(\.php)?$#i', '', $filename)).'_helper'; |
Andrey Andreev | 4015f9b | 2016-11-14 10:22:59 +0200 | [diff] [blame] | 600 | $helper = $filepath.$filename; |
| 601 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 602 | if (isset($this->_ci_helpers[$helper])) |
| 603 | { |
| 604 | continue; |
| 605 | } |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 606 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 607 | // Is this a helper extension request? |
Andrey Andreev | 4015f9b | 2016-11-14 10:22:59 +0200 | [diff] [blame] | 608 | $ext_helper = config_item('subclass_prefix').$filename; |
Andrey Andreev | 12d7b46 | 2012-11-12 13:42:09 +0200 | [diff] [blame] | 609 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 621 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 622 | $base_helper = BASEPATH.'helpers/'.$helper.'.php'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 623 | if ( ! file_exists($base_helper)) |
| 624 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 625 | show_error('Unable to load the requested file: helpers/'.$helper.'.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 626 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 627 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 628 | include_once($base_helper); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 629 | $this->_ci_helpers[$helper] = TRUE; |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 630 | log_message('info', 'Helper loaded: '.$helper); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 631 | continue; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 633 | |
Andrey Andreev | 12d7b46 | 2012-11-12 13:42:09 +0200 | [diff] [blame] | 634 | // No extensions found ... try loading regular helpers and/or overrides |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 635 | foreach ($this->_ci_helper_paths as $path) |
| 636 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 637 | if (file_exists($path.'helpers/'.$helper.'.php')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 638 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 639 | include_once($path.'helpers/'.$helper.'.php'); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 640 | |
| 641 | $this->_ci_helpers[$helper] = TRUE; |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 642 | log_message('info', 'Helper loaded: '.$helper); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 643 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 647 | // unable to load the helper |
| 648 | if ( ! isset($this->_ci_helpers[$helper])) |
| 649 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 650 | show_error('Unable to load the requested file: helpers/'.$helper.'.php'); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 651 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 652 | } |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 653 | |
| 654 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 655 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 656 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 657 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 658 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 659 | /** |
| 660 | * Load Helpers |
| 661 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 662 | * An alias for the helper() method in case the developer has |
| 663 | * written the plural form of it. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 664 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 665 | * @uses CI_Loader::helper() |
| 666 | * @param string|string[] $helpers Helper name(s) |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 667 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 668 | */ |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 669 | public function helpers($helpers = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 670 | { |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 671 | return $this->helper($helpers); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 672 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 673 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 674 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 675 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 676 | /** |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 677 | * Language Loader |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 678 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 679 | * Loads language files. |
| 680 | * |
| 681 | * @param string|string[] $files List of language file names to load |
| 682 | * @param string Language name |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 683 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 684 | */ |
Andrey Andreev | 9c12b3f | 2014-01-06 12:32:30 +0200 | [diff] [blame] | 685 | public function language($files, $lang = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 686 | { |
Andrey Andreev | d8e31ec | 2014-11-07 12:17:50 +0200 | [diff] [blame] | 687 | get_instance()->lang->load($files, $lang); |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 688 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 689 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 690 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 691 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 692 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 693 | /** |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 694 | * Config Loader |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 695 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 696 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 703 | */ |
Andrey Andreev | 9c12b3f | 2014-01-06 12:32:30 +0200 | [diff] [blame] | 704 | public function config($file, $use_sections = FALSE, $fail_gracefully = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 705 | { |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 706 | return get_instance()->config->load($file, $use_sections, $fail_gracefully); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | // -------------------------------------------------------------------- |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 710 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 711 | /** |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 712 | * Driver Loader |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 713 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 714 | * Loads a driver library. |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 715 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 716 | * @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 Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 720 | * @return object|bool Object or FALSE on failure if $library is a string |
| 721 | * and $object_name is set. CI_Loader instance otherwise. |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 722 | */ |
Andrey Andreev | 9c12b3f | 2014-01-06 12:32:30 +0200 | [diff] [blame] | 723 | public function driver($library, $params = NULL, $object_name = NULL) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 724 | { |
Christopher Guiney | b54d355 | 2012-03-10 08:38:10 -0800 | [diff] [blame] | 725 | if (is_array($library)) |
Christopher Guiney | 9929d6f | 2012-03-09 19:53:24 -0800 | [diff] [blame] | 726 | { |
Andrey Andreev | 44d3b18 | 2016-02-15 14:37:14 +0200 | [diff] [blame] | 727 | foreach ($library as $key => $value) |
Christopher Guiney | 9929d6f | 2012-03-09 19:53:24 -0800 | [diff] [blame] | 728 | { |
Andrey Andreev | 44d3b18 | 2016-02-15 14:37:14 +0200 | [diff] [blame] | 729 | if (is_int($key)) |
| 730 | { |
| 731 | $this->driver($value, $params); |
| 732 | } |
| 733 | else |
| 734 | { |
| 735 | $this->driver($key, $params, $value); |
| 736 | } |
Christopher Guiney | 9929d6f | 2012-03-09 19:53:24 -0800 | [diff] [blame] | 737 | } |
Christopher Guiney | 9929d6f | 2012-03-09 19:53:24 -0800 | [diff] [blame] | 738 | |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 739 | return $this; |
| 740 | } |
| 741 | elseif (empty($library)) |
Tom Klingenberg | 6a15b2d | 2011-10-07 20:03:30 +0200 | [diff] [blame] | 742 | { |
| 743 | return FALSE; |
| 744 | } |
| 745 | |
vlakoff | fadb822 | 2013-05-12 10:57:09 +0200 | [diff] [blame] | 746 | if ( ! class_exists('CI_Driver_Library', FALSE)) |
dchill42 | 6262d05 | 2012-11-24 18:41:13 -0500 | [diff] [blame] | 747 | { |
| 748 | // We aren't instantiating an object here, just making the base class available |
| 749 | require BASEPATH.'libraries/Driver.php'; |
| 750 | } |
| 751 | |
Derek Jones | d5e0cb5 | 2010-03-09 20:20:46 -0600 | [diff] [blame] | 752 | // 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 Aker | d25e66a | 2010-03-28 01:07:09 -0500 | [diff] [blame] | 756 | $library = ucfirst($library).'/'.$library; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 757 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 758 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 759 | return $this->library($library, $params, $object_name); |
| 760 | } |
| 761 | |
| 762 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 763 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 764 | /** |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 765 | * Add Package Path |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 766 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 767 | * Prepends a parent path to the library, model, helper and config |
| 768 | * path arrays. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 769 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 770 | * @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 Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 777 | * @return object |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 778 | */ |
Andrey Andreev | cce9180 | 2012-06-12 13:25:31 +0300 | [diff] [blame] | 779 | public function add_package_path($path, $view_cascade = TRUE) |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 780 | { |
Pascal Kriete | 6b6c274 | 2010-11-09 13:12:22 -0500 | [diff] [blame] | 781 | $path = rtrim($path, '/').'/'; |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 782 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 783 | array_unshift($this->_ci_library_paths, $path); |
| 784 | array_unshift($this->_ci_model_paths, $path); |
| 785 | array_unshift($this->_ci_helper_paths, $path); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 786 | |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 787 | $this->_ci_view_paths = array($path.'views/' => $view_cascade) + $this->_ci_view_paths; |
| 788 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 789 | // Add config file path |
| 790 | $config =& $this->_ci_get_component('config'); |
and-ers | b3ec942 | 2013-01-03 16:05:12 +0100 | [diff] [blame] | 791 | $config->_config_paths[] = $path; |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 792 | |
| 793 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | // -------------------------------------------------------------------- |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 797 | |
| 798 | /** |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 799 | * Get Package Paths |
| 800 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 801 | * Return a list of all package paths. |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 802 | * |
vlakoff | cdc6113 | 2013-05-10 16:47:47 +0200 | [diff] [blame] | 803 | * @param bool $include_base Whether to include BASEPATH (default: FALSE) |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 804 | * @return array |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 805 | */ |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 806 | public function get_package_paths($include_base = FALSE) |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 807 | { |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 808 | return ($include_base === TRUE) ? $this->_ci_library_paths : $this->_ci_model_paths; |
Phil Sturgeon | de3dbc3 | 2010-12-27 17:41:02 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | // -------------------------------------------------------------------- |
| 812 | |
| 813 | /** |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 814 | * Remove Package Path |
| 815 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 816 | * 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 Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 819 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 820 | * @param string $path Path to remove |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 821 | * @return object |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 822 | */ |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 823 | public function remove_package_path($path = '') |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 824 | { |
| 825 | $config =& $this->_ci_get_component('config'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 826 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 827 | if ($path === '') |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 828 | { |
Andrey Andreev | d729735 | 2012-01-07 22:53:14 +0200 | [diff] [blame] | 829 | 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); |
Korri | 3684d34 | 2012-04-17 00:35:08 -0400 | [diff] [blame] | 833 | array_pop($config->_config_paths); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 834 | } |
| 835 | else |
| 836 | { |
Pascal Kriete | 6b6c274 | 2010-11-09 13:12:22 -0500 | [diff] [blame] | 837 | $path = rtrim($path, '/').'/'; |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 838 | 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 Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 845 | |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 846 | if (isset($this->_ci_view_paths[$path.'views/'])) |
| 847 | { |
| 848 | unset($this->_ci_view_paths[$path.'views/']); |
| 849 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 850 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 851 | if (($key = array_search($path, $config->_config_paths)) !== FALSE) |
| 852 | { |
| 853 | unset($config->_config_paths[$key]); |
| 854 | } |
| 855 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 856 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 857 | // 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 Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 861 | $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH.'views/' => TRUE)); |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 862 | $config->_config_paths = array_unique(array_merge($config->_config_paths, array(APPPATH))); |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 863 | |
| 864 | return $this; |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 868 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 869 | /** |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 870 | * Internal CI Data Loader |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 871 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 872 | * Used to load views and files. |
| 873 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 874 | * Variables are prefixed with _ci_ to avoid symbol collision with |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 875 | * variables made available to view files. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 876 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 877 | * @used-by CI_Loader::view() |
| 878 | * @used-by CI_Loader::file() |
| 879 | * @param array $_ci_data Data to load |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 880 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 881 | */ |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 882 | protected function _ci_load($_ci_data) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 883 | { |
| 884 | // Set the default data variables |
| 885 | foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val) |
| 886 | { |
Andrey Andreev | 94af355 | 2012-03-26 23:10:42 +0300 | [diff] [blame] | 887 | $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 888 | } |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 889 | |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 890 | $file_exists = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 891 | |
| 892 | // Set the path to the requested file |
Alex Bilbie | 40bd2a7 | 2012-06-02 16:04:15 +0100 | [diff] [blame] | 893 | if (is_string($_ci_path) && $_ci_path !== '') |
Greg Aker | 8807be3 | 2011-04-21 13:06:15 -0500 | [diff] [blame] | 894 | { |
| 895 | $_ci_x = explode('/', $_ci_path); |
| 896 | $_ci_file = end($_ci_x); |
| 897 | } |
| 898 | else |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 899 | { |
| 900 | $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION); |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 901 | $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view; |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 902 | |
Joe McFrederick | 64f470b | 2012-08-18 12:29:56 -0400 | [diff] [blame] | 903 | foreach ($this->_ci_view_paths as $_ci_view_file => $cascade) |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 904 | { |
Joe McFrederick | 64f470b | 2012-08-18 12:29:56 -0400 | [diff] [blame] | 905 | if (file_exists($_ci_view_file.$_ci_file)) |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 906 | { |
Joe McFrederick | 64f470b | 2012-08-18 12:29:56 -0400 | [diff] [blame] | 907 | $_ci_path = $_ci_view_file.$_ci_file; |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 908 | $file_exists = TRUE; |
| 909 | break; |
| 910 | } |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 911 | |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 912 | if ( ! $cascade) |
| 913 | { |
| 914 | break; |
David Behler | cda768a | 2011-08-14 23:52:48 +0200 | [diff] [blame] | 915 | } |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 916 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 917 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 918 | |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 919 | if ( ! $file_exists && ! file_exists($_ci_path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 920 | { |
| 921 | show_error('Unable to load the requested file: '.$_ci_file); |
| 922 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 923 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 924 | // This allows anything loaded using $this->load (views, files, etc.) |
| 925 | // to become accessible from within the Controller and Model functions. |
Pascal Kriete | 89ace43 | 2010-11-10 15:49:10 -0500 | [diff] [blame] | 926 | $_ci_CI =& get_instance(); |
| 927 | foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 928 | { |
Pascal Kriete | 89ace43 | 2010-11-10 15:49:10 -0500 | [diff] [blame] | 929 | if ( ! isset($this->$_ci_key)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 930 | { |
Pascal Kriete | 89ace43 | 2010-11-10 15:49:10 -0500 | [diff] [blame] | 931 | $this->$_ci_key =& $_ci_CI->$_ci_key; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | * Extract and cache variables |
| 937 | * |
vlakoff | 0250618 | 2012-07-03 07:28:50 +0200 | [diff] [blame] | 938 | * You can either set variables using the dedicated $this->load->vars() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 939 | * 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 942 | */ |
Andrey Andreev | ec8dbbb | 2017-01-04 17:01:44 +0200 | [diff] [blame] | 943 | empty($_ci_vars) OR $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 944 | extract($this->_ci_cached_vars); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 945 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 946 | /* |
| 947 | * Buffer the output |
| 948 | * |
| 949 | * We buffer the output for two reasons: |
| 950 | * 1. Speed. You get a significant speed boost. |
Andrey Andreev | d729735 | 2012-01-07 22:53:14 +0200 | [diff] [blame] | 951 | * 2. So that the final rendered template can be post-processed by |
dchill42 | 5628ba0 | 2012-08-08 12:05:45 -0400 | [diff] [blame] | 952 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 956 | */ |
| 957 | ob_start(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 958 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 959 | // 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 Andreev | 07355da | 2015-07-22 12:46:16 +0300 | [diff] [blame] | 962 | if ( ! is_php('5.4') && ! ini_get('short_open_tag') && config_item('rewrite_short_tags') === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 963 | { |
Andrey Andreev | d47baab | 2012-01-09 16:56:46 +0200 | [diff] [blame] | 964 | echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path)))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 965 | } |
| 966 | else |
| 967 | { |
| 968 | include($_ci_path); // include() vs include_once() allows for multiple views with the same name |
| 969 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 970 | |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 971 | log_message('info', 'File loaded: '.$_ci_path); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 972 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 973 | // Return the file data if requested |
| 974 | if ($_ci_return === TRUE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 975 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 976 | $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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 989 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 990 | if (ob_get_level() > $this->_ci_ob_level + 1) |
| 991 | { |
| 992 | ob_end_flush(); |
| 993 | } |
| 994 | else |
| 995 | { |
Greg Aker | 22f1a63 | 2010-11-10 15:34:35 -0600 | [diff] [blame] | 996 | $_ci_CI->output->append_output(ob_get_contents()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 997 | @ob_end_clean(); |
| 998 | } |
Andrey Andreev | 61a7b8f | 2014-01-07 13:36:50 +0200 | [diff] [blame] | 999 | |
| 1000 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | // -------------------------------------------------------------------- |
| 1004 | |
| 1005 | /** |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1006 | * Internal CI Library Loader |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1007 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 1008 | * @used-by CI_Loader::library() |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1009 | * @uses CI_Loader::_ci_init_library() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1010 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 1011 | * @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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1014 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1015 | */ |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1016 | protected function _ci_load_library($class, $params = NULL, $object_name = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1017 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1020 | // but we don't want a leading slash |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 1021 | $class = str_replace('.php', '', trim($class, '/')); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1022 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1023 | // Was the path included with the class name? |
| 1024 | // We look for a slash to determine this |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 1025 | if (($last_slash = strrpos($class, '/')) !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1026 | { |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 1027 | // Extract the path |
Andrey Andreev | c26d34f | 2013-01-28 21:46:08 +0200 | [diff] [blame] | 1028 | $subdir = substr($class, 0, ++$last_slash); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1029 | |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 1030 | // Get the filename from the path |
Andrey Andreev | d729735 | 2012-01-07 22:53:14 +0200 | [diff] [blame] | 1031 | $class = substr($class, $last_slash); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1032 | } |
Andrey Andreev | 3608e1a | 2013-01-28 16:27:30 +0200 | [diff] [blame] | 1033 | else |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1034 | { |
Andrey Andreev | 3608e1a | 2013-01-28 16:27:30 +0200 | [diff] [blame] | 1035 | $subdir = ''; |
| 1036 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1037 | |
Andrey Andreev | 3608e1a | 2013-01-28 16:27:30 +0200 | [diff] [blame] | 1038 | $class = ucfirst($class); |
Andrey Andreev | 3608e1a | 2013-01-28 16:27:30 +0200 | [diff] [blame] | 1039 | |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1040 | // Is this a stock library? There are a few special conditions if so ... |
| 1041 | if (file_exists(BASEPATH.'libraries/'.$subdir.$class.'.php')) |
Andrey Andreev | 3608e1a | 2013-01-28 16:27:30 +0200 | [diff] [blame] | 1042 | { |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1043 | return $this->_ci_load_stock_library($class, $subdir, $params, $object_name); |
Andrey Andreev | 3608e1a | 2013-01-28 16:27:30 +0200 | [diff] [blame] | 1044 | } |
| 1045 | |
Andrey Andreev | 894a3f2 | 2017-06-27 16:31:17 +0300 | [diff] [blame] | 1046 | // Safety: Was the class already loaded by a previous call? |
| 1047 | if (class_exists($class, FALSE)) |
| 1048 | { |
| 1049 | $property = $object_name; |
Andrey Andreev | be0280d | 2017-06-28 11:49:01 +0300 | [diff] [blame] | 1050 | if (empty($property)) |
Andrey Andreev | 894a3f2 | 2017-06-27 16:31:17 +0300 | [diff] [blame] | 1051 | { |
| 1052 | $property = strtolower($class); |
Andrey Andreev | db89c20 | 2017-06-28 11:18:24 +0300 | [diff] [blame] | 1053 | isset($this->_ci_varmap[$property]) && $property = $this->_ci_varmap[$property]; |
Andrey Andreev | 894a3f2 | 2017-06-27 16:31:17 +0300 | [diff] [blame] | 1054 | } |
| 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 | |
vlakoff | 3567246 | 2013-02-15 01:36:04 +0100 | [diff] [blame] | 1066 | // Let's search for the requested library file and load it. |
Andrey Andreev | 3608e1a | 2013-01-28 16:27:30 +0200 | [diff] [blame] | 1067 | foreach ($this->_ci_library_paths as $path) |
| 1068 | { |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1069 | // BASEPATH has already been checked for |
| 1070 | if ($path === BASEPATH) |
| 1071 | { |
| 1072 | continue; |
| 1073 | } |
| 1074 | |
Andrey Andreev | 3608e1a | 2013-01-28 16:27:30 +0200 | [diff] [blame] | 1075 | $filepath = $path.'libraries/'.$subdir.$class.'.php'; |
Andrey Andreev | 3608e1a | 2013-01-28 16:27:30 +0200 | [diff] [blame] | 1076 | // Does the file exist? No? Bummer... |
Andrey Andreev | 894a3f2 | 2017-06-27 16:31:17 +0300 | [diff] [blame] | 1077 | if ( ! file_exists($filepath)) |
Andrey Andreev | 3608e1a | 2013-01-28 16:27:30 +0200 | [diff] [blame] | 1078 | { |
| 1079 | continue; |
| 1080 | } |
| 1081 | |
| 1082 | include_once($filepath); |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1083 | return $this->_ci_init_library($class, '', $params, $object_name); |
Andrey Andreev | 3608e1a | 2013-01-28 16:27:30 +0200 | [diff] [blame] | 1084 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1085 | |
Andrey Andreev | d729735 | 2012-01-07 22:53:14 +0200 | [diff] [blame] | 1086 | // One last attempt. Maybe the library is in a subdirectory, but it wasn't specified? |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 1087 | if ($subdir === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1088 | { |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1089 | return $this->_ci_load_library($class.'/'.$class, $params, $object_name); |
dchill42 | aee9265 | 2012-08-26 21:45:35 -0400 | [diff] [blame] | 1090 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1091 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1092 | // If we got this far we were unable to find the requested class. |
Andrey Andreev | 3608e1a | 2013-01-28 16:27:30 +0200 | [diff] [blame] | 1093 | log_message('error', 'Unable to load the requested class: '.$class); |
| 1094 | show_error('Unable to load the requested class: '.$class); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1095 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1096 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1097 | // -------------------------------------------------------------------- |
| 1098 | |
| 1099 | /** |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1100 | * Internal CI Stock Library Loader |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1101 | * |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1102 | * @used-by CI_Loader::_ci_load_library() |
| 1103 | * @uses CI_Loader::_ci_init_library() |
| 1104 | * |
Andrey Andreev | e13fa9f | 2016-05-20 17:30:07 +0300 | [diff] [blame] | 1105 | * @param string $library_name Library name to load |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1106 | * @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 Andreev | be0280d | 2017-06-28 11:49:01 +0300 | [diff] [blame] | 1122 | $property = $object_name; |
| 1123 | if (empty($property)) |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1124 | { |
Andrey Andreev | be0280d | 2017-06-28 11:49:01 +0300 | [diff] [blame] | 1125 | $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 Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | log_message('debug', $library_name.' class already loaded. Second attempt ignored.'); |
| 1136 | return; |
| 1137 | } |
Andrey Andreev | 8f5c178 | 2015-03-25 13:41:02 +0200 | [diff] [blame] | 1138 | |
| 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 Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1145 | { |
Andrey Andreev | 8f5c178 | 2015-03-25 13:41:02 +0200 | [diff] [blame] | 1146 | if (file_exists($path = $path.'libraries/'.$file_path.$library_name.'.php')) |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1147 | { |
Andrey Andreev | 8f5c178 | 2015-03-25 13:41:02 +0200 | [diff] [blame] | 1148 | // 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 Andreev | c58b005 | 2017-06-28 11:50:27 +0300 | [diff] [blame] | 1154 | |
| 1155 | log_message('debug', $path.' exists, but does not declare '.$prefix.$library_name); |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1156 | } |
| 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 Andreev | 8f5c178 | 2015-03-25 13:41:02 +0200 | [diff] [blame] | 1163 | foreach ($paths as $path) |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1164 | { |
Andrey Andreev | 8f5c178 | 2015-03-25 13:41:02 +0200 | [diff] [blame] | 1165 | if (file_exists($path = $path.'libraries/'.$file_path.$subclass.'.php')) |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1166 | { |
Andrey Andreev | 8f5c178 | 2015-03-25 13:41:02 +0200 | [diff] [blame] | 1167 | include_once($path); |
| 1168 | if (class_exists($subclass, FALSE)) |
| 1169 | { |
| 1170 | $prefix = config_item('subclass_prefix'); |
| 1171 | break; |
| 1172 | } |
Andrey Andreev | c58b005 | 2017-06-28 11:50:27 +0300 | [diff] [blame] | 1173 | |
| 1174 | log_message('debug', $path.' exists, but does not declare '.$subclass); |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1175 | } |
| 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 Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 1188 | * |
| 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 Andreev | 94af355 | 2012-03-26 23:10:42 +0300 | [diff] [blame] | 1196 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1197 | */ |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1198 | protected function _ci_init_library($class, $prefix, $config = FALSE, $object_name = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1199 | { |
Andrey Andreev | 94af355 | 2012-03-26 23:10:42 +0300 | [diff] [blame] | 1200 | // Is there an associated config file for this class? Note: these should always be lowercase |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1201 | if ($config === NULL) |
| 1202 | { |
Eric Barnes | 5e16ec6 | 2011-01-04 17:25:23 -0500 | [diff] [blame] | 1203 | // 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1207 | { |
Andrey Andreev | 95c31ad | 2014-12-17 19:01:31 +0200 | [diff] [blame] | 1208 | $found = FALSE; |
Eric Barnes | 5e16ec6 | 2011-01-04 17:25:23 -0500 | [diff] [blame] | 1209 | foreach ($config_component->_config_paths as $path) |
| 1210 | { |
| 1211 | // We test for both uppercase and lowercase, for servers that |
Andrey Andreev | 95c31ad | 2014-12-17 19:01:31 +0200 | [diff] [blame] | 1212 | // 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 Barnes | 5e16ec6 | 2011-01-04 17:25:23 -0500 | [diff] [blame] | 1215 | { |
Andrey Andreev | 94af355 | 2012-03-26 23:10:42 +0300 | [diff] [blame] | 1216 | include($path.'config/'.strtolower($class).'.php'); |
Andrey Andreev | 95c31ad | 2014-12-17 19:01:31 +0200 | [diff] [blame] | 1217 | $found = TRUE; |
Eric Barnes | 5e16ec6 | 2011-01-04 17:25:23 -0500 | [diff] [blame] | 1218 | } |
Andrey Andreev | 94af355 | 2012-03-26 23:10:42 +0300 | [diff] [blame] | 1219 | elseif (file_exists($path.'config/'.ucfirst(strtolower($class)).'.php')) |
Eric Barnes | 5e16ec6 | 2011-01-04 17:25:23 -0500 | [diff] [blame] | 1220 | { |
Andrey Andreev | 94af355 | 2012-03-26 23:10:42 +0300 | [diff] [blame] | 1221 | include($path.'config/'.ucfirst(strtolower($class)).'.php'); |
Andrey Andreev | 95c31ad | 2014-12-17 19:01:31 +0200 | [diff] [blame] | 1222 | $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 Barnes | 5e16ec6 | 2011-01-04 17:25:23 -0500 | [diff] [blame] | 1240 | break; |
| 1241 | } |
| 1242 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1243 | } |
| 1244 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1245 | |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1246 | $class_name = $prefix.$class; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1247 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1248 | // Is the class name valid? |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1249 | if ( ! class_exists($class_name, FALSE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1250 | { |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1251 | log_message('error', 'Non-existent class: '.$class_name); |
| 1252 | show_error('Non-existent class: '.$class_name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1253 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1254 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1255 | // Set the variable name we will assign the class to |
Andrey Andreev | d729735 | 2012-01-07 22:53:14 +0200 | [diff] [blame] | 1256 | // Was a custom class name supplied? If so we'll use it |
Andrey Andreev | 519f87a | 2013-07-23 17:16:10 +0300 | [diff] [blame] | 1257 | if (empty($object_name)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1258 | { |
Andrey Andreev | 519f87a | 2013-07-23 17:16:10 +0300 | [diff] [blame] | 1259 | $object_name = strtolower($class); |
| 1260 | if (isset($this->_ci_varmap[$object_name])) |
| 1261 | { |
| 1262 | $object_name = $this->_ci_varmap[$object_name]; |
| 1263 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1264 | } |
Andrey Andreev | 519f87a | 2013-07-23 17:16:10 +0300 | [diff] [blame] | 1265 | |
| 1266 | // Don't overwrite existing properties |
| 1267 | $CI =& get_instance(); |
| 1268 | if (isset($CI->$object_name)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1269 | { |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1270 | if ($CI->$object_name instanceof $class_name) |
Andrey Andreev | 519f87a | 2013-07-23 17:16:10 +0300 | [diff] [blame] | 1271 | { |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1272 | log_message('debug', $class_name." has already been instantiated as '".$object_name."'. Second attempt aborted."); |
Andrey Andreev | 519f87a | 2013-07-23 17:16:10 +0300 | [diff] [blame] | 1273 | return; |
| 1274 | } |
| 1275 | |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1276 | show_error("Resource '".$object_name."' already exists and is not a ".$class_name." instance."); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1279 | // Save the class name and object name |
Andrey Andreev | 519f87a | 2013-07-23 17:16:10 +0300 | [diff] [blame] | 1280 | $this->_ci_classes[$object_name] = $class; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1281 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1282 | // Instantiate the class |
Andrey Andreev | 519f87a | 2013-07-23 17:16:10 +0300 | [diff] [blame] | 1283 | $CI->$object_name = isset($config) |
Andrey Andreev | db669f1 | 2015-01-21 16:51:51 +0200 | [diff] [blame] | 1284 | ? new $class_name($config) |
| 1285 | : new $class_name(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1286 | } |
| 1287 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1288 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1289 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1290 | /** |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 1291 | * CI Autoloader |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1292 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 1293 | * Loads component listed in the config/autoload.php file. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1294 | * |
Andrey Andreev | cdac248 | 2012-11-03 18:09:01 +0200 | [diff] [blame] | 1295 | * @used-by CI_Loader::initialize() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1296 | * @return void |
| 1297 | */ |
Shane Pearson | 665baec | 2011-08-22 18:52:19 -0500 | [diff] [blame] | 1298 | protected function _ci_autoloader() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1299 | { |
Andrey Andreev | 4b6469d | 2015-01-22 16:42:17 +0200 | [diff] [blame] | 1300 | if (file_exists(APPPATH.'config/autoload.php')) |
Andrey Andreev | bd6a814 | 2015-01-22 16:41:25 +0200 | [diff] [blame] | 1301 | { |
| 1302 | include(APPPATH.'config/autoload.php'); |
| 1303 | } |
| 1304 | |
Andrey Andreev | db529ca | 2013-01-28 11:00:02 +0200 | [diff] [blame] | 1305 | if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php')) |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 1306 | { |
Shane Pearson | 6adfe63 | 2011-08-10 16:42:53 -0500 | [diff] [blame] | 1307 | include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'); |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 1308 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1309 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1310 | if ( ! isset($autoload)) |
| 1311 | { |
Gabriel Potkány | d9287a0 | 2015-02-04 08:47:56 +0100 | [diff] [blame] | 1312 | return; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1313 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1314 | |
Phil Sturgeon | 9730c75 | 2010-12-15 10:50:15 +0000 | [diff] [blame] | 1315 | // 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1324 | // Load any custom config file |
| 1325 | if (count($autoload['config']) > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1326 | { |
Andrey Andreev | 623227f | 2014-03-24 12:46:25 +0200 | [diff] [blame] | 1327 | foreach ($autoload['config'] as $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1328 | { |
Andrey Andreev | 689d41c | 2014-03-24 17:28:40 +0200 | [diff] [blame] | 1329 | $this->config($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1330 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1331 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1332 | |
Derek Jones | c6da503 | 2010-03-09 20:44:27 -0600 | [diff] [blame] | 1333 | // Autoload helpers and languages |
| 1334 | foreach (array('helper', 'language') as $type) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1335 | { |
Andrey Andreev | 94af355 | 2012-03-26 23:10:42 +0300 | [diff] [blame] | 1336 | if (isset($autoload[$type]) && count($autoload[$type]) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1337 | { |
| 1338 | $this->$type($autoload[$type]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1339 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Andrey Andreev | 9ab70a8 | 2013-07-23 00:09:26 +0300 | [diff] [blame] | 1342 | // Autoload drivers |
| 1343 | if (isset($autoload['drivers'])) |
| 1344 | { |
Andrey Andreev | 44d3b18 | 2016-02-15 14:37:14 +0200 | [diff] [blame] | 1345 | $this->driver($autoload['drivers']); |
Andrey Andreev | 9ab70a8 | 2013-07-23 00:09:26 +0300 | [diff] [blame] | 1346 | } |
| 1347 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1348 | // Load libraries |
Andrey Andreev | 94af355 | 2012-03-26 23:10:42 +0300 | [diff] [blame] | 1349 | if (isset($autoload['libraries']) && count($autoload['libraries']) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1350 | { |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1357 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1358 | // Load all other libraries |
Andrey Andreev | e36d048 | 2015-04-04 21:55:09 +0300 | [diff] [blame] | 1359 | $this->library($autoload['libraries']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1360 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1361 | |
| 1362 | // Autoload models |
| 1363 | if (isset($autoload['model'])) |
| 1364 | { |
| 1365 | $this->model($autoload['model']); |
| 1366 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1367 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1368 | |
| 1369 | // -------------------------------------------------------------------- |
| 1370 | |
| 1371 | /** |
Andrey Andreev | ec8dbbb | 2017-01-04 17:01:44 +0200 | [diff] [blame] | 1372 | * Prepare variables for _ci_vars, to be later extract()-ed inside views |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1373 | * |
Andrey Andreev | ec8dbbb | 2017-01-04 17:01:44 +0200 | [diff] [blame] | 1374 | * Converts objects to associative arrays and filters-out internal |
Chris Faulkner | b9ac1a1 | 2017-01-10 16:02:40 +1030 | [diff] [blame] | 1375 | * variable names (i.e. keys prefixed with '_ci_'). |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1376 | * |
Andrey Andreev | ec8dbbb | 2017-01-04 17:01:44 +0200 | [diff] [blame] | 1377 | * @param mixed $vars |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1378 | * @return array |
| 1379 | */ |
Andrey Andreev | ec8dbbb | 2017-01-04 17:01:44 +0200 | [diff] [blame] | 1380 | protected function _ci_prepare_view_vars($vars) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1381 | { |
Andrey Andreev | ec8dbbb | 2017-01-04 17:01:44 +0200 | [diff] [blame] | 1382 | if ( ! is_array($vars)) |
| 1383 | { |
| 1384 | $vars = is_object($vars) |
Chris Faulkner | b9ac1a1 | 2017-01-10 16:02:40 +1030 | [diff] [blame] | 1385 | ? get_object_vars($vars) |
Andrey Andreev | ec8dbbb | 2017-01-04 17:01:44 +0200 | [diff] [blame] | 1386 | : 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | // -------------------------------------------------------------------- |
| 1401 | |
| 1402 | /** |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 1403 | * CI Component getter |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 1404 | * |
Andrey Andreev | ed4b258 | 2012-10-27 17:46:52 +0300 | [diff] [blame] | 1405 | * Get a reference to a specific library or model. |
| 1406 | * |
| 1407 | * @param string $component Component name |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 1408 | * @return bool |
| 1409 | */ |
Greg Aker | f5c8402 | 2011-04-19 17:13:03 -0500 | [diff] [blame] | 1410 | protected function &_ci_get_component($component) |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 1411 | { |
Pascal Kriete | 89ace43 | 2010-11-10 15:49:10 -0500 | [diff] [blame] | 1412 | $CI =& get_instance(); |
| 1413 | return $CI->$component; |
Derek Jones | 32bf186 | 2010-03-02 13:46:07 -0600 | [diff] [blame] | 1414 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1415 | } |