1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* EEH_Autoloader |
5
|
|
|
* |
6
|
|
|
* This is a helper utility class for setting up autoloaders. |
7
|
|
|
* |
8
|
|
|
* @package Event Espresso |
9
|
|
|
* @subpackage /helpers/EEH_Autoloader.helper.php |
10
|
|
|
* @author Darren Ethier |
11
|
|
|
* |
12
|
|
|
* ------------------------------------------------------------------------ |
13
|
|
|
*/ |
14
|
|
|
class EEH_Autoloader extends EEH_Base |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* instance of the EE_System object |
20
|
|
|
* |
21
|
|
|
* @var $_instance |
22
|
|
|
* @access private |
23
|
|
|
*/ |
24
|
|
|
private static $_instance = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* $_autoloaders |
28
|
|
|
* @var array $_autoloaders |
29
|
|
|
* @access private |
30
|
|
|
*/ |
31
|
|
|
private static $_autoloaders; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* set to "paths" to display autoloader class => path mappings |
35
|
|
|
* set to "times" to display autoloader loading times |
36
|
|
|
* set to "all" to display both |
37
|
|
|
* |
38
|
|
|
* @var string $debug |
39
|
|
|
* @access private |
40
|
|
|
*/ |
41
|
|
|
public static $debug = false; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* class constructor |
46
|
|
|
* |
47
|
|
|
* @access private |
48
|
|
|
* @return \EEH_Autoloader |
|
|
|
|
49
|
|
|
* @throws Exception |
50
|
|
|
*/ |
51
|
|
|
private function __construct() |
52
|
|
|
{ |
53
|
|
|
if (self::$_autoloaders === null) { |
54
|
|
|
self::$_autoloaders = array(); |
55
|
|
|
$this->_register_custom_autoloaders(); |
56
|
|
|
spl_autoload_register(array( $this, 'espresso_autoloader' )); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @access public |
64
|
|
|
* @return EEH_Autoloader |
65
|
|
|
*/ |
66
|
|
|
public static function instance() |
67
|
|
|
{ |
68
|
|
|
// check if class object is instantiated |
69
|
|
|
if (! self::$_instance instanceof EEH_Autoloader) { |
70
|
|
|
self::$_instance = new self(); |
71
|
|
|
} |
72
|
|
|
return self::$_instance; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* espresso_autoloader |
79
|
|
|
* |
80
|
|
|
* @access public |
81
|
|
|
* @param $class_name |
82
|
|
|
* @internal param $className |
83
|
|
|
* @internal param string $class_name - simple class name ie: session |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public static function espresso_autoloader($class_name) |
87
|
|
|
{ |
88
|
|
|
if (isset(self::$_autoloaders[ $class_name ])) { |
89
|
|
|
require_once(self::$_autoloaders[ $class_name ]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* register_autoloader |
97
|
|
|
* |
98
|
|
|
* @access public |
99
|
|
|
* @param array | string $class_paths - array of key => value pairings between class names and paths |
100
|
|
|
* @param bool $read_check true if we need to check whether the file is readable or not. |
101
|
|
|
* @param bool $debug **deprecated** |
102
|
|
|
* @throws \EE_Error |
103
|
|
|
*/ |
104
|
|
|
public static function register_autoloader($class_paths, $read_check = true, $debug = false) |
105
|
|
|
{ |
106
|
|
|
$class_paths = is_array($class_paths) ? $class_paths : array( $class_paths ); |
107
|
|
|
foreach ($class_paths as $class => $path) { |
108
|
|
|
// skip all files that are not PHP |
109
|
|
|
if (substr($path, strlen($path) - 3) !== 'php') { |
110
|
|
|
continue; |
111
|
|
|
} |
112
|
|
|
// don't give up! you gotta... |
113
|
|
|
// get some class |
114
|
|
|
if (empty($class)) { |
115
|
|
|
throw new EE_Error(sprintf(__('No Class name was specified while registering an autoloader for the following path: %s.', 'event_espresso'), $path)); |
116
|
|
|
} |
117
|
|
|
// one day you will find the path young grasshopper |
118
|
|
|
if (empty($path)) { |
119
|
|
|
throw new EE_Error(sprintf(__('No path was specified while registering an autoloader for the %s class.', 'event_espresso'), $class)); |
120
|
|
|
} |
121
|
|
|
// is file readable ? |
122
|
|
|
if ($read_check && ! is_readable($path)) { |
123
|
|
|
throw new EE_Error(sprintf(__('The file for the %s class could not be found or is not readable due to file permissions. Please ensure the following path is correct: %s', 'event_espresso'), $class, $path)); |
124
|
|
|
} |
125
|
|
|
if (! isset(self::$_autoloaders[ $class ])) { |
126
|
|
|
self::$_autoloaders[ $class ] = str_replace(array( '/', '\\' ), DS, $path); |
127
|
|
|
if (EE_DEBUG && ( EEH_Autoloader::$debug === 'paths' || EEH_Autoloader::$debug === 'all' || $debug )) { |
128
|
|
|
EEH_Debug_Tools::printr(self::$_autoloaders[ $class ], $class, __FILE__, __LINE__); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* get_autoloaders |
139
|
|
|
* |
140
|
|
|
* @access public |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
public static function get_autoloaders() |
144
|
|
|
{ |
145
|
|
|
return self::$_autoloaders; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* register core, model and class 'autoloaders' |
151
|
|
|
* |
152
|
|
|
* @access private |
153
|
|
|
* @return void |
154
|
|
|
* @throws EE_Error |
155
|
|
|
*/ |
156
|
|
|
private function _register_custom_autoloaders() |
157
|
|
|
{ |
158
|
|
|
EEH_Autoloader::$debug = ''; |
159
|
|
|
\EEH_Autoloader::register_helpers_autoloaders(); |
160
|
|
|
EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'interfaces'); |
161
|
|
|
EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE); |
162
|
|
|
EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_INTERFACES, true); |
163
|
|
|
EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_MODELS, true); |
164
|
|
|
EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CLASSES); |
165
|
|
|
EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_FORM_SECTIONS, true); |
166
|
|
|
EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'messages'); |
167
|
|
|
if (EEH_Autoloader::$debug === 'times' || EEH_Autoloader::$debug === 'all') { |
168
|
|
|
EEH_Debug_Tools::instance()->show_times(); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* register core, model and class 'autoloaders' |
176
|
|
|
* |
177
|
|
|
* @access public |
178
|
|
|
*/ |
179
|
|
|
public static function register_helpers_autoloaders() |
180
|
|
|
{ |
181
|
|
|
EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_HELPERS); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* register core, model and class 'autoloaders' |
189
|
|
|
* |
190
|
|
|
* @access public |
191
|
|
|
* @return void |
192
|
|
|
*/ |
193
|
|
|
public static function register_form_sections_autoloaders() |
194
|
|
|
{ |
195
|
|
|
// EEH_Autoloader::register_autoloaders_for_each_file_in_folder( EE_FORM_SECTIONS, true ); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* register core, model and class 'autoloaders' |
201
|
|
|
* |
202
|
|
|
* @access public |
203
|
|
|
* @return void |
204
|
|
|
* @throws EE_Error |
205
|
|
|
*/ |
206
|
|
|
public static function register_line_item_display_autoloaders() |
207
|
|
|
{ |
208
|
|
|
EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'line_item_display', true); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* register core, model and class 'autoloaders' |
214
|
|
|
* |
215
|
|
|
* @access public |
216
|
|
|
* @return void |
217
|
|
|
* @throws EE_Error |
218
|
|
|
*/ |
219
|
|
|
public static function register_line_item_filter_autoloaders() |
220
|
|
|
{ |
221
|
|
|
EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'line_item_filters', true); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* register template part 'autoloaders' |
227
|
|
|
* |
228
|
|
|
* @access public |
229
|
|
|
* @return void |
230
|
|
|
* @throws EE_Error |
231
|
|
|
*/ |
232
|
|
|
public static function register_template_part_autoloaders() |
233
|
|
|
{ |
234
|
|
|
EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'template_parts', true); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return void |
240
|
|
|
* @throws EE_Error |
241
|
|
|
*/ |
242
|
|
|
public static function register_business_classes() |
243
|
|
|
{ |
244
|
|
|
EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'business'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Assumes all the files in this folder have the normal naming scheme (namely that their classname |
251
|
|
|
* is the file's name, plus ".whatever.php".) and adds each of them to the autoloader list. |
252
|
|
|
* If that's not the case, you'll need to improve this function or just use EEH_File::get_classname_from_filepath_with_standard_filename() directly. |
253
|
|
|
* Yes this has to scan the directory for files, but it only does it once -- not on EACH |
254
|
|
|
* time the autoloader is used |
255
|
|
|
* |
256
|
|
|
* @param string $folder name, with or without trailing /, doesn't matter |
257
|
|
|
* @param bool $recursive |
258
|
|
|
* @param bool $debug **deprecated** |
259
|
|
|
* @throws \EE_Error |
260
|
|
|
*/ |
261
|
|
|
public static function register_autoloaders_for_each_file_in_folder($folder, $recursive = false, $debug = false) |
262
|
|
|
{ |
263
|
|
View Code Duplication |
if (EEH_Autoloader::$debug === 'times' || EEH_Autoloader::$debug === 'all' || $debug) { |
264
|
|
|
EEH_Debug_Tools::instance()->start_timer(basename($folder)); |
|
|
|
|
265
|
|
|
} |
266
|
|
|
// make sure last char is a / |
267
|
|
|
$folder .= $folder[ strlen($folder)-1 ] !== DS ? DS : ''; |
268
|
|
|
$class_to_filepath_map = array(); |
269
|
|
|
$exclude = array( 'index' ); |
270
|
|
|
// get all the files in that folder that end in php |
271
|
|
|
$filepaths = glob($folder.'*'); |
272
|
|
|
|
273
|
|
|
if (empty($filepaths)) { |
274
|
|
|
return; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
foreach ($filepaths as $filepath) { |
278
|
|
|
if (substr($filepath, -4, 4) === '.php') { |
279
|
|
|
$class_name = EEH_File::get_classname_from_filepath_with_standard_filename($filepath); |
280
|
|
|
if (! in_array($class_name, $exclude)) { |
281
|
|
|
$class_to_filepath_map [ $class_name ] = $filepath; |
282
|
|
|
} |
283
|
|
|
} elseif ($recursive) { |
284
|
|
|
EEH_Autoloader::register_autoloaders_for_each_file_in_folder($filepath, $recursive, $debug); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
// we remove the necessity to do a is_readable() check via the $read_check flag because glob by nature will not return non_readable files/directories. |
288
|
|
|
self::register_autoloader($class_to_filepath_map, false, $debug); |
289
|
|
View Code Duplication |
if (EEH_Autoloader::$debug === 'times' || EEH_Autoloader::$debug === 'all') { |
290
|
|
|
EEH_Debug_Tools::instance()->stop_timer(basename($folder)); |
|
|
|
|
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* add_alias |
298
|
|
|
* register additional autoloader based on variation of the classname for an existing autoloader |
299
|
|
|
* |
300
|
|
|
* @access public |
301
|
|
|
* @param string $class_name - simple class name ie: EE_Session |
302
|
|
|
* @param string $alias - variation on class name ie: EE_session, session, etc |
303
|
|
|
*/ |
304
|
|
|
public static function add_alias($class_name, $alias) |
305
|
|
|
{ |
306
|
|
|
if (isset(self::$_autoloaders[ $class_name ])) { |
307
|
|
|
self::$_autoloaders[ $alias ] = self::$_autoloaders[ $class_name ]; |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.