-
Notifications
You must be signed in to change notification settings - Fork 16
/
autoload.php
202 lines (154 loc) · 5.58 KB
/
autoload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
spl_autoload_register('_common_autoloader');
/**
* Automatically loads classes in PHP5 way, using SPL.
* @param string $class The class name, no namespaces are supported.
* @return bool TRUE if a class have been found and loaded, FALSE otherwise.
* @author Ivan Tcholakov, 2013-2016
* @license The MIT License
*/
function _common_autoloader($class) {
static $locations = null;
if (!isset($locations)) {
$locations = array();
$autoload = null;
_autoload_classes_read_config($autoload, COMMONPATH.'config/autoload_classes.php');
_autoload_classes_read_config($autoload, COMMONPATH.'config/'.ENVIRONMENT.'/autoload_classes.php');
_autoload_classes_read_config($autoload, APPPATH.'config/autoload_classes.php');
_autoload_classes_read_config($autoload, APPPATH.'config/'.ENVIRONMENT.'/autoload_classes.php');
if (isset($autoload) && is_array($autoload) && isset($autoload['classes']) && is_array($autoload['classes'])) {
$locations = $autoload['classes'];
}
}
$class = (string) $class;
// No class name? Abort.
if ($class == '') {
return false;
}
// Scanning for classes in specific directories (see the array above).
if (isset($locations[$class])) {
require $locations[$class];
return true;
}
// Autoload CodeIgniter classes.
if (strpos($class, 'CI_') === 0) {
if (is_file($location = BASEPATH.'core/'.substr($class, 3).'.php')) {
require $location;
return true;
}
if (is_file($location = BASEPATH.'libraries/'.substr($class, 3).'.php')) {
require $location;
return true;
}
if (is_file($location = BASEPATH.'libraries/'.substr($class, 3).'/'.substr($class, 3).'.php')) {
require $location;
return true;
}
}
// Autoload core classes.
global $RTR;
$module = null;
if (is_object($RTR)) {
$module = $RTR->fetch_module();
}
if ($module != '') {
if (is_file($location = APPPATH."modules/$module/core/$class.php")) {
require $location;
return true;
}
if (is_file($location = APPPATH."modules/$module/controllers/$class.php")) {
require $location;
return true;
}
if (is_file($location = COMMONPATH."modules/$module/core/$class.php")) {
require $location;
return true;
}
if (is_file($location = COMMONPATH."modules/$module/controllers/$class.php")) {
require $location;
return true;
}
}
if (is_file($location = APPPATH."core/$class.php")) {
require $location;
return true;
}
if (is_file($location = APPPATH."controllers/$class.php")) {
require $location;
return true;
}
if (is_file($location = COMMONPATH."core/$class.php")) {
require $location;
return true;
}
if (is_file($location = COMMONPATH."controllers/$class.php")) {
require $location;
return true;
}
// Autoload Modular Extensions MX core classes.
if (strpos($class, 'MX_') === 0 && is_file($location = COMMONPATH.'third_party/MX/'.substr($class, 3).'.php')) {
require $location;
return true;
}
// Autoload library classes.
if (is_file($location = APPPATH."libraries/$class.php")) {
require $location;
return true;
}
if (is_file($location = COMMONPATH."libraries/$class.php")) {
require $location;
return true;
}
// Autoload models (that are extended by other models).
if (is_file($location = APPPATH."models/$class.php")) {
require $location;
return true;
}
if (is_file($location = COMMONPATH."models/$class.php")) {
require $location;
return true;
}
// Autoload custom classes, non-standard way.
if (is_file($location = APPPATH."classes/$class.php")) {
require $location;
return true;
}
if (is_file($location = COMMONPATH."classes/$class.php")) {
require $location;
return true;
}
// PSR-0 autoloading.
if (is_file($location = APPPATH.'classes/'.str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $class).'.php')) {
require $location;
return true;
}
if (is_file($location = COMMONPATH.'classes/'.str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $class).'.php')) {
require $location;
return true;
}
// Autoload PEAR packages that are integrated in this platform.
if (is_file($location = COMMONPATH.'third_party/pear/'.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php')) {
require $location;
return true;
}
return false;
}
/**
* Merges information from autoload_classes.php configuration files in different locations.
* @param null|array $config_output The result array (output parameter).
* @param string $config_file The current configuration file autoload_classes.php (a full path).
* @return void
* @author Ivan Tcholakov, 2013
* @license The MIT License
*/
function _autoload_classes_read_config(& $config_output, $config_file) {
if (file_exists($config_file)) {
include($config_file);
if (isset($autoload) && is_array($autoload)) {
if (!isset($config_output) || !is_array($config_output)) {
$config_output = array();
}
$config_output = array_merge_recursive_distinct($config_output, $autoload);
}
}
}