|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use EventEspresso\core\interfaces\InterminableInterface; |
|
4
|
|
|
use EventEspresso\core\interfaces\ResettableInterface; |
|
5
|
|
|
use EventEspresso\core\services\assets\Registry; |
|
6
|
|
|
use EventEspresso\core\services\commands\CommandBusInterface; |
|
7
|
|
|
|
|
8
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* EE_Registry Class |
|
14
|
|
|
* Centralized Application Data Storage and Management |
|
15
|
|
|
* |
|
16
|
|
|
* @package Event Espresso |
|
17
|
|
|
* @subpackage core |
|
18
|
|
|
* @author Brent Christensen |
|
19
|
|
|
*/ |
|
20
|
|
|
class EE_Registry implements ResettableInterface |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var EE_Registry $_instance |
|
25
|
|
|
*/ |
|
26
|
|
|
private static $_instance; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var EE_Dependency_Map $_dependency_map |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $_dependency_map; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array $_class_abbreviations |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $_class_abbreviations = array(); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var CommandBusInterface $BUS |
|
40
|
|
|
*/ |
|
41
|
|
|
public $BUS; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var EE_Cart $CART |
|
45
|
|
|
*/ |
|
46
|
|
|
public $CART; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var EE_Config $CFG |
|
50
|
|
|
*/ |
|
51
|
|
|
public $CFG; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var EE_Network_Config $NET_CFG |
|
55
|
|
|
*/ |
|
56
|
|
|
public $NET_CFG; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* StdClass object for storing library classes in |
|
60
|
|
|
* |
|
61
|
|
|
* @var StdClass $LIB |
|
62
|
|
|
*/ |
|
63
|
|
|
public $LIB; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var EE_Request_Handler $REQ |
|
67
|
|
|
*/ |
|
68
|
|
|
public $REQ; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var EE_Session $SSN |
|
72
|
|
|
*/ |
|
73
|
|
|
public $SSN; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @since 4.5.0 |
|
77
|
|
|
* @var EE_Capabilities $CAP |
|
78
|
|
|
*/ |
|
79
|
|
|
public $CAP; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @since 4.9.0 |
|
83
|
|
|
* @var EE_Message_Resource_Manager $MRM |
|
84
|
|
|
*/ |
|
85
|
|
|
public $MRM; |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @var Registry $AssetsRegistry |
|
90
|
|
|
*/ |
|
91
|
|
|
public $AssetsRegistry; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* StdClass object for holding addons which have registered themselves to work with EE core |
|
95
|
|
|
* |
|
96
|
|
|
* @var EE_Addon[] $addons |
|
97
|
|
|
*/ |
|
98
|
|
|
public $addons; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
102
|
|
|
* |
|
103
|
|
|
* @var EEM_Base[] $models |
|
104
|
|
|
*/ |
|
105
|
|
|
public $models = array(); |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @var EED_Module[] $modules |
|
109
|
|
|
*/ |
|
110
|
|
|
public $modules; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @var EES_Shortcode[] $shortcodes |
|
114
|
|
|
*/ |
|
115
|
|
|
public $shortcodes; |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @var WP_Widget[] $widgets |
|
119
|
|
|
*/ |
|
120
|
|
|
public $widgets; |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
124
|
|
|
* which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
125
|
|
|
* Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
126
|
|
|
* classnames (eg "EEM_Event") |
|
127
|
|
|
* |
|
128
|
|
|
* @var array $non_abstract_db_models |
|
129
|
|
|
*/ |
|
130
|
|
|
public $non_abstract_db_models = array(); |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* internationalization for JS strings |
|
135
|
|
|
* usage: EE_Registry::i18n_js_strings['string_key'] = esc_html__( 'string to translate.', 'event_espresso' ); |
|
136
|
|
|
* in js file: var translatedString = eei18n.string_key; |
|
137
|
|
|
* |
|
138
|
|
|
* @var array $i18n_js_strings |
|
139
|
|
|
*/ |
|
140
|
|
|
public static $i18n_js_strings = array(); |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* $main_file - path to espresso.php |
|
145
|
|
|
* |
|
146
|
|
|
* @var array $main_file |
|
147
|
|
|
*/ |
|
148
|
|
|
public $main_file; |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* array of ReflectionClass objects where the key is the class name |
|
152
|
|
|
* |
|
153
|
|
|
* @var ReflectionClass[] $_reflectors |
|
154
|
|
|
*/ |
|
155
|
|
|
public $_reflectors; |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
159
|
|
|
* |
|
160
|
|
|
* @var boolean $_cache_on |
|
161
|
|
|
*/ |
|
162
|
|
|
protected $_cache_on = true; |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @singleton method used to instantiate class object |
|
168
|
|
|
* @param EE_Dependency_Map $dependency_map |
|
169
|
|
|
* @return EE_Registry instance |
|
170
|
|
|
*/ |
|
171
|
|
|
public static function instance(EE_Dependency_Map $dependency_map = null) |
|
172
|
|
|
{ |
|
173
|
|
|
// check if class object is instantiated |
|
174
|
|
|
if (! self::$_instance instanceof EE_Registry) { |
|
175
|
|
|
self::$_instance = new self($dependency_map); |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
return self::$_instance; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* protected constructor to prevent direct creation |
|
184
|
|
|
* |
|
185
|
|
|
* @Constructor |
|
186
|
|
|
* @param EE_Dependency_Map $dependency_map |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function __construct(EE_Dependency_Map $dependency_map) |
|
189
|
|
|
{ |
|
190
|
|
|
$this->_dependency_map = $dependency_map; |
|
191
|
|
|
$this->LIB = new stdClass(); |
|
192
|
|
|
$this->addons = new stdClass(); |
|
193
|
|
|
$this->modules = new stdClass(); |
|
194
|
|
|
$this->shortcodes = new stdClass(); |
|
195
|
|
|
$this->widgets = new stdClass(); |
|
196
|
|
|
add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* initialize |
|
203
|
|
|
* |
|
204
|
|
|
* @throws EE_Error |
|
205
|
|
|
* @throws ReflectionException |
|
206
|
|
|
*/ |
|
207
|
|
|
public function initialize() |
|
208
|
|
|
{ |
|
209
|
|
|
$this->_class_abbreviations = apply_filters( |
|
210
|
|
|
'FHEE__EE_Registry____construct___class_abbreviations', |
|
211
|
|
|
array( |
|
212
|
|
|
'EE_Config' => 'CFG', |
|
213
|
|
|
'EE_Session' => 'SSN', |
|
214
|
|
|
'EE_Capabilities' => 'CAP', |
|
215
|
|
|
'EE_Cart' => 'CART', |
|
216
|
|
|
'EE_Network_Config' => 'NET_CFG', |
|
217
|
|
|
'EE_Request_Handler' => 'REQ', |
|
218
|
|
|
'EE_Message_Resource_Manager' => 'MRM', |
|
219
|
|
|
'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
220
|
|
|
'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
221
|
|
|
) |
|
222
|
|
|
); |
|
223
|
|
|
$this->load_core('Base', array(), true); |
|
224
|
|
|
// add our request and response objects to the cache |
|
225
|
|
|
$request_loader = $this->_dependency_map->class_loader('EE_Request'); |
|
226
|
|
|
$this->_set_cached_class( |
|
227
|
|
|
$request_loader(), |
|
228
|
|
|
'EE_Request' |
|
229
|
|
|
); |
|
230
|
|
|
$response_loader = $this->_dependency_map->class_loader('EE_Response'); |
|
231
|
|
|
$this->_set_cached_class( |
|
232
|
|
|
$response_loader(), |
|
233
|
|
|
'EE_Response' |
|
234
|
|
|
); |
|
235
|
|
|
add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @return void |
|
242
|
|
|
*/ |
|
243
|
|
|
public function init() |
|
244
|
|
|
{ |
|
245
|
|
|
// Get current page protocol |
|
246
|
|
|
$protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
247
|
|
|
// Output admin-ajax.php URL with same protocol as current page |
|
248
|
|
|
self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
249
|
|
|
self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* localize_i18n_js_strings |
|
256
|
|
|
* |
|
257
|
|
|
* @return string |
|
258
|
|
|
*/ |
|
259
|
|
|
public static function localize_i18n_js_strings() |
|
260
|
|
|
{ |
|
261
|
|
|
$i18n_js_strings = (array)self::$i18n_js_strings; |
|
262
|
|
|
foreach ($i18n_js_strings as $key => $value) { |
|
263
|
|
|
if (is_scalar($value)) { |
|
264
|
|
|
$i18n_js_strings[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8'); |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
|
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @param $module |
|
274
|
|
|
* @throws EE_Error |
|
275
|
|
|
* @throws ReflectionException |
|
276
|
|
|
*/ |
|
277
|
|
|
public function add_module($module) |
|
278
|
|
|
{ |
|
279
|
|
|
if ($module instanceof EED_Module) { |
|
280
|
|
|
$module_class = get_class($module); |
|
281
|
|
|
$this->modules->{$module_class} = $module; |
|
282
|
|
|
} else { |
|
283
|
|
|
if (! class_exists('EE_Module_Request_Router')) { |
|
284
|
|
|
$this->load_core('Module_Request_Router'); |
|
285
|
|
|
} |
|
286
|
|
|
EE_Module_Request_Router::module_factory($module); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
|
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* @param string $module_name |
|
294
|
|
|
* @return mixed EED_Module | NULL |
|
295
|
|
|
*/ |
|
296
|
|
|
public function get_module($module_name = '') |
|
297
|
|
|
{ |
|
298
|
|
|
return isset($this->modules->{$module_name}) |
|
299
|
|
|
? $this->modules->{$module_name} |
|
300
|
|
|
: null; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* loads core classes - must be singletons |
|
307
|
|
|
* |
|
308
|
|
|
* @param string $class_name - simple class name ie: session |
|
309
|
|
|
* @param mixed $arguments |
|
310
|
|
|
* @param bool $load_only |
|
311
|
|
|
* @return mixed |
|
312
|
|
|
* @throws EE_Error |
|
313
|
|
|
* @throws ReflectionException |
|
314
|
|
|
*/ |
|
315
|
|
|
public function load_core($class_name, $arguments = array(), $load_only = false) |
|
316
|
|
|
{ |
|
317
|
|
|
$core_paths = apply_filters( |
|
318
|
|
|
'FHEE__EE_Registry__load_core__core_paths', |
|
319
|
|
|
array( |
|
320
|
|
|
EE_CORE, |
|
321
|
|
|
EE_ADMIN, |
|
322
|
|
|
EE_CPTS, |
|
323
|
|
|
EE_CORE . 'data_migration_scripts' . DS, |
|
324
|
|
|
EE_CORE . 'capabilities' . DS, |
|
325
|
|
|
EE_CORE . 'request_stack' . DS, |
|
326
|
|
|
EE_CORE . 'middleware' . DS, |
|
327
|
|
|
) |
|
328
|
|
|
); |
|
329
|
|
|
// retrieve instantiated class |
|
330
|
|
|
return $this->_load( |
|
331
|
|
|
$core_paths, |
|
332
|
|
|
'EE_', |
|
333
|
|
|
$class_name, |
|
334
|
|
|
'core', |
|
335
|
|
|
$arguments, |
|
336
|
|
|
false, |
|
337
|
|
|
true, |
|
338
|
|
|
$load_only |
|
339
|
|
|
); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
|
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* loads service classes |
|
346
|
|
|
* |
|
347
|
|
|
* @param string $class_name - simple class name ie: session |
|
348
|
|
|
* @param mixed $arguments |
|
349
|
|
|
* @param bool $load_only |
|
350
|
|
|
* @return mixed |
|
351
|
|
|
* @throws EE_Error |
|
352
|
|
|
* @throws ReflectionException |
|
353
|
|
|
*/ |
|
354
|
|
|
public function load_service($class_name, $arguments = array(), $load_only = false) |
|
355
|
|
|
{ |
|
356
|
|
|
$service_paths = apply_filters( |
|
357
|
|
|
'FHEE__EE_Registry__load_service__service_paths', |
|
358
|
|
|
array( |
|
359
|
|
|
EE_CORE . 'services' . DS, |
|
360
|
|
|
) |
|
361
|
|
|
); |
|
362
|
|
|
// retrieve instantiated class |
|
363
|
|
|
return $this->_load( |
|
364
|
|
|
$service_paths, |
|
365
|
|
|
'EE_', |
|
366
|
|
|
$class_name, |
|
367
|
|
|
'class', |
|
368
|
|
|
$arguments, |
|
369
|
|
|
false, |
|
370
|
|
|
true, |
|
371
|
|
|
$load_only |
|
372
|
|
|
); |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
|
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* loads data_migration_scripts |
|
379
|
|
|
* |
|
380
|
|
|
* @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
381
|
|
|
* @param mixed $arguments |
|
382
|
|
|
* @return EE_Data_Migration_Script_Base|mixed |
|
383
|
|
|
* @throws EE_Error |
|
384
|
|
|
* @throws ReflectionException |
|
385
|
|
|
*/ |
|
386
|
|
|
public function load_dms($class_name, $arguments = array()) |
|
387
|
|
|
{ |
|
388
|
|
|
// retrieve instantiated class |
|
389
|
|
|
return $this->_load( |
|
390
|
|
|
EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), |
|
391
|
|
|
'EE_DMS_', |
|
392
|
|
|
$class_name, |
|
393
|
|
|
'dms', |
|
394
|
|
|
$arguments, |
|
395
|
|
|
false, |
|
396
|
|
|
false |
|
397
|
|
|
); |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
|
|
401
|
|
|
|
|
402
|
|
|
/** |
|
403
|
|
|
* loads object creating classes - must be singletons |
|
404
|
|
|
* |
|
405
|
|
|
* @param string $class_name - simple class name ie: attendee |
|
406
|
|
|
* @param mixed $arguments - an array of arguments to pass to the class |
|
407
|
|
|
* @param bool $from_db - some classes are instantiated from the db and thus call a different method to |
|
408
|
|
|
* instantiate |
|
409
|
|
|
* @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then |
|
410
|
|
|
* set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
411
|
|
|
* @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate |
|
412
|
|
|
* (default) |
|
413
|
|
|
* @return EE_Base_Class | bool |
|
414
|
|
|
* @throws EE_Error |
|
415
|
|
|
* @throws ReflectionException |
|
416
|
|
|
*/ |
|
417
|
|
|
public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
418
|
|
|
{ |
|
419
|
|
|
$paths = apply_filters( |
|
420
|
|
|
'FHEE__EE_Registry__load_class__paths', array( |
|
421
|
|
|
EE_CORE, |
|
422
|
|
|
EE_CLASSES, |
|
423
|
|
|
EE_BUSINESS, |
|
424
|
|
|
) |
|
425
|
|
|
); |
|
426
|
|
|
// retrieve instantiated class |
|
427
|
|
|
return $this->_load( |
|
428
|
|
|
$paths, |
|
429
|
|
|
'EE_', |
|
430
|
|
|
$class_name, |
|
431
|
|
|
'class', |
|
432
|
|
|
$arguments, |
|
433
|
|
|
$from_db, |
|
434
|
|
|
$cache, |
|
435
|
|
|
$load_only |
|
436
|
|
|
); |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
|
|
440
|
|
|
|
|
441
|
|
|
/** |
|
442
|
|
|
* loads helper classes - must be singletons |
|
443
|
|
|
* |
|
444
|
|
|
* @param string $class_name - simple class name ie: price |
|
445
|
|
|
* @param mixed $arguments |
|
446
|
|
|
* @param bool $load_only |
|
447
|
|
|
* @return EEH_Base | bool |
|
448
|
|
|
* @throws EE_Error |
|
449
|
|
|
* @throws ReflectionException |
|
450
|
|
|
*/ |
|
451
|
|
|
public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
452
|
|
|
{ |
|
453
|
|
|
// todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
454
|
|
|
$helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
455
|
|
|
// retrieve instantiated class |
|
456
|
|
|
return $this->_load( |
|
457
|
|
|
$helper_paths, |
|
458
|
|
|
'EEH_', |
|
459
|
|
|
$class_name, |
|
460
|
|
|
'helper', |
|
461
|
|
|
$arguments, |
|
462
|
|
|
false, |
|
463
|
|
|
true, |
|
464
|
|
|
$load_only |
|
465
|
|
|
); |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
|
|
469
|
|
|
|
|
470
|
|
|
/** |
|
471
|
|
|
* loads core classes - must be singletons |
|
472
|
|
|
* |
|
473
|
|
|
* @param string $class_name - simple class name ie: session |
|
474
|
|
|
* @param mixed $arguments |
|
475
|
|
|
* @param bool $load_only |
|
476
|
|
|
* @param bool $cache whether to cache the object or not. |
|
477
|
|
|
* @return mixed |
|
478
|
|
|
* @throws EE_Error |
|
479
|
|
|
* @throws ReflectionException |
|
480
|
|
|
*/ |
|
481
|
|
|
public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
482
|
|
|
{ |
|
483
|
|
|
$paths = array( |
|
484
|
|
|
EE_LIBRARIES, |
|
485
|
|
|
EE_LIBRARIES . 'messages' . DS, |
|
486
|
|
|
EE_LIBRARIES . 'shortcodes' . DS, |
|
487
|
|
|
EE_LIBRARIES . 'qtips' . DS, |
|
488
|
|
|
EE_LIBRARIES . 'payment_methods' . DS, |
|
489
|
|
|
); |
|
490
|
|
|
// retrieve instantiated class |
|
491
|
|
|
return $this->_load( |
|
492
|
|
|
$paths, |
|
493
|
|
|
'EE_', |
|
494
|
|
|
$class_name, |
|
495
|
|
|
'lib', |
|
496
|
|
|
$arguments, |
|
497
|
|
|
false, |
|
498
|
|
|
$cache, |
|
499
|
|
|
$load_only |
|
500
|
|
|
); |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
|
|
504
|
|
|
|
|
505
|
|
|
/** |
|
506
|
|
|
* loads model classes - must be singletons |
|
507
|
|
|
* |
|
508
|
|
|
* @param string $class_name - simple class name ie: price |
|
509
|
|
|
* @param mixed $arguments |
|
510
|
|
|
* @param bool $load_only |
|
511
|
|
|
* @return EEM_Base | bool |
|
512
|
|
|
* @throws EE_Error |
|
513
|
|
|
* @throws ReflectionException |
|
514
|
|
|
*/ |
|
515
|
|
|
public function load_model($class_name, $arguments = array(), $load_only = false) |
|
516
|
|
|
{ |
|
517
|
|
|
$paths = apply_filters( |
|
518
|
|
|
'FHEE__EE_Registry__load_model__paths', array( |
|
519
|
|
|
EE_MODELS, |
|
520
|
|
|
EE_CORE, |
|
521
|
|
|
) |
|
522
|
|
|
); |
|
523
|
|
|
// retrieve instantiated class |
|
524
|
|
|
return $this->_load( |
|
525
|
|
|
$paths, |
|
526
|
|
|
'EEM_', |
|
527
|
|
|
$class_name, |
|
528
|
|
|
'model', |
|
529
|
|
|
$arguments, |
|
530
|
|
|
false, |
|
531
|
|
|
true, |
|
532
|
|
|
$load_only |
|
533
|
|
|
); |
|
534
|
|
|
} |
|
535
|
|
|
|
|
536
|
|
|
|
|
537
|
|
|
|
|
538
|
|
|
/** |
|
539
|
|
|
* loads model classes - must be singletons |
|
540
|
|
|
* |
|
541
|
|
|
* @param string $class_name - simple class name ie: price |
|
542
|
|
|
* @param mixed $arguments |
|
543
|
|
|
* @param bool $load_only |
|
544
|
|
|
* @return mixed | bool |
|
545
|
|
|
* @throws EE_Error |
|
546
|
|
|
* @throws ReflectionException |
|
547
|
|
|
*/ |
|
548
|
|
|
public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
549
|
|
|
{ |
|
550
|
|
|
$paths = array( |
|
551
|
|
|
EE_MODELS . 'fields' . DS, |
|
552
|
|
|
EE_MODELS . 'helpers' . DS, |
|
553
|
|
|
EE_MODELS . 'relations' . DS, |
|
554
|
|
|
EE_MODELS . 'strategies' . DS, |
|
555
|
|
|
); |
|
556
|
|
|
// retrieve instantiated class |
|
557
|
|
|
return $this->_load( |
|
558
|
|
|
$paths, |
|
559
|
|
|
'EE_', |
|
560
|
|
|
$class_name, |
|
561
|
|
|
'', |
|
562
|
|
|
$arguments, |
|
563
|
|
|
false, |
|
564
|
|
|
true, |
|
565
|
|
|
$load_only |
|
566
|
|
|
); |
|
567
|
|
|
} |
|
568
|
|
|
|
|
569
|
|
|
|
|
570
|
|
|
|
|
571
|
|
|
/** |
|
572
|
|
|
* Determines if $model_name is the name of an actual EE model. |
|
573
|
|
|
* |
|
574
|
|
|
* @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
575
|
|
|
* @return boolean |
|
576
|
|
|
*/ |
|
577
|
|
|
public function is_model_name($model_name) |
|
578
|
|
|
{ |
|
579
|
|
|
return isset($this->models[$model_name]); |
|
580
|
|
|
} |
|
581
|
|
|
|
|
582
|
|
|
|
|
583
|
|
|
|
|
584
|
|
|
/** |
|
585
|
|
|
* generic class loader |
|
586
|
|
|
* |
|
587
|
|
|
* @param string $path_to_file - directory path to file location, not including filename |
|
588
|
|
|
* @param string $file_name - file name ie: my_file.php, including extension |
|
589
|
|
|
* @param string $type - file type - core? class? helper? model? |
|
590
|
|
|
* @param mixed $arguments |
|
591
|
|
|
* @param bool $load_only |
|
592
|
|
|
* @return mixed |
|
593
|
|
|
* @throws EE_Error |
|
594
|
|
|
* @throws ReflectionException |
|
595
|
|
|
*/ |
|
596
|
|
|
public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
597
|
|
|
{ |
|
598
|
|
|
// retrieve instantiated class |
|
599
|
|
|
return $this->_load( |
|
600
|
|
|
$path_to_file, |
|
601
|
|
|
'', |
|
602
|
|
|
$file_name, |
|
603
|
|
|
$type, |
|
604
|
|
|
$arguments, |
|
605
|
|
|
false, |
|
606
|
|
|
true, |
|
607
|
|
|
$load_only |
|
608
|
|
|
); |
|
609
|
|
|
} |
|
610
|
|
|
|
|
611
|
|
|
|
|
612
|
|
|
|
|
613
|
|
|
/** |
|
614
|
|
|
* @param string $path_to_file - directory path to file location, not including filename |
|
615
|
|
|
* @param string $class_name - full class name ie: My_Class |
|
616
|
|
|
* @param string $type - file type - core? class? helper? model? |
|
617
|
|
|
* @param mixed $arguments |
|
618
|
|
|
* @param bool $load_only |
|
619
|
|
|
* @return bool|EE_Addon|object |
|
620
|
|
|
* @throws EE_Error |
|
621
|
|
|
* @throws ReflectionException |
|
622
|
|
|
*/ |
|
623
|
|
|
public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
624
|
|
|
{ |
|
625
|
|
|
// retrieve instantiated class |
|
626
|
|
|
return $this->_load( |
|
627
|
|
|
$path_to_file, |
|
628
|
|
|
'addon', |
|
629
|
|
|
$class_name, |
|
630
|
|
|
$type, |
|
631
|
|
|
$arguments, |
|
632
|
|
|
false, |
|
633
|
|
|
true, |
|
634
|
|
|
$load_only |
|
635
|
|
|
); |
|
636
|
|
|
} |
|
637
|
|
|
|
|
638
|
|
|
|
|
639
|
|
|
|
|
640
|
|
|
/** |
|
641
|
|
|
* instantiates, caches, and automatically resolves dependencies |
|
642
|
|
|
* for classes that use a Fully Qualified Class Name. |
|
643
|
|
|
* if the class is not capable of being loaded using PSR-4 autoloading, |
|
644
|
|
|
* then you need to use one of the existing load_*() methods |
|
645
|
|
|
* which can resolve the classname and filepath from the passed arguments |
|
646
|
|
|
* |
|
647
|
|
|
* @param bool|string $class_name Fully Qualified Class Name |
|
648
|
|
|
* @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
649
|
|
|
* @param bool $cache whether to cache the instantiated object for reuse |
|
650
|
|
|
* @param bool $from_db some classes are instantiated from the db |
|
651
|
|
|
* and thus call a different method to instantiate |
|
652
|
|
|
* @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
653
|
|
|
* @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
654
|
|
|
* @return bool|null|object null = failure to load or instantiate class object. |
|
655
|
|
|
* object = class loaded and instantiated successfully. |
|
656
|
|
|
* bool = fail or success when $load_only is true |
|
657
|
|
|
* @throws EE_Error |
|
658
|
|
|
* @throws ReflectionException |
|
659
|
|
|
*/ |
|
660
|
|
|
public function create( |
|
661
|
|
|
$class_name = false, |
|
662
|
|
|
$arguments = array(), |
|
663
|
|
|
$cache = false, |
|
664
|
|
|
$from_db = false, |
|
665
|
|
|
$load_only = false, |
|
666
|
|
|
$addon = false |
|
667
|
|
|
) { |
|
668
|
|
|
$class_name = ltrim($class_name, '\\'); |
|
669
|
|
|
$class_name = $this->_dependency_map->get_alias($class_name); |
|
670
|
|
|
if (! class_exists($class_name)) { |
|
671
|
|
|
// maybe the class is registered with a preceding \ |
|
672
|
|
|
$class_name = strpos($class_name, '\\') !== 0 |
|
673
|
|
|
? '\\' . $class_name |
|
674
|
|
|
: $class_name; |
|
675
|
|
|
// still doesn't exist ? |
|
676
|
|
|
if (! class_exists($class_name)) { |
|
677
|
|
|
return null; |
|
678
|
|
|
} |
|
679
|
|
|
} |
|
680
|
|
|
// if we're only loading the class and it already exists, then let's just return true immediately |
|
681
|
|
|
if ($load_only) { |
|
682
|
|
|
return true; |
|
683
|
|
|
} |
|
684
|
|
|
$addon = $addon |
|
685
|
|
|
? 'addon' |
|
686
|
|
|
: ''; |
|
687
|
|
|
// $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
688
|
|
|
// $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
689
|
|
|
// $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
690
|
|
View Code Duplication |
if ($this->_cache_on && $cache && ! $load_only) { |
|
691
|
|
|
// return object if it's already cached |
|
692
|
|
|
$cached_class = $this->_get_cached_class($class_name, $addon); |
|
693
|
|
|
if ($cached_class !== null) { |
|
694
|
|
|
return $cached_class; |
|
695
|
|
|
} |
|
696
|
|
|
} |
|
697
|
|
|
// instantiate the requested object |
|
698
|
|
|
$class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
699
|
|
|
if ($this->_cache_on && $cache) { |
|
700
|
|
|
// save it for later... kinda like gum { : $ |
|
701
|
|
|
$this->_set_cached_class($class_obj, $class_name, $addon, $from_db); |
|
702
|
|
|
} |
|
703
|
|
|
$this->_cache_on = true; |
|
704
|
|
|
return $class_obj; |
|
705
|
|
|
} |
|
706
|
|
|
|
|
707
|
|
|
|
|
708
|
|
|
|
|
709
|
|
|
/** |
|
710
|
|
|
* instantiates, caches, and injects dependencies for classes |
|
711
|
|
|
* |
|
712
|
|
|
* @param array $file_paths an array of paths to folders to look in |
|
713
|
|
|
* @param string $class_prefix EE or EEM or... ??? |
|
714
|
|
|
* @param bool|string $class_name $class name |
|
715
|
|
|
* @param string $type file type - core? class? helper? model? |
|
716
|
|
|
* @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
717
|
|
|
* @param bool $from_db some classes are instantiated from the db |
|
718
|
|
|
* and thus call a different method to instantiate |
|
719
|
|
|
* @param bool $cache whether to cache the instantiated object for reuse |
|
720
|
|
|
* @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
721
|
|
|
* @return bool|null|object null = failure to load or instantiate class object. |
|
722
|
|
|
* object = class loaded and instantiated successfully. |
|
723
|
|
|
* bool = fail or success when $load_only is true |
|
724
|
|
|
* @throws EE_Error |
|
725
|
|
|
* @throws ReflectionException |
|
726
|
|
|
*/ |
|
727
|
|
|
protected function _load( |
|
728
|
|
|
$file_paths = array(), |
|
729
|
|
|
$class_prefix = 'EE_', |
|
730
|
|
|
$class_name = false, |
|
731
|
|
|
$type = 'class', |
|
732
|
|
|
$arguments = array(), |
|
733
|
|
|
$from_db = false, |
|
734
|
|
|
$cache = true, |
|
735
|
|
|
$load_only = false |
|
736
|
|
|
) { |
|
737
|
|
|
$class_name = ltrim($class_name, '\\'); |
|
738
|
|
|
// strip php file extension |
|
739
|
|
|
$class_name = str_replace('.php', '', trim($class_name)); |
|
740
|
|
|
// does the class have a prefix ? |
|
741
|
|
|
if (! empty($class_prefix) && $class_prefix !== 'addon') { |
|
742
|
|
|
// make sure $class_prefix is uppercase |
|
743
|
|
|
$class_prefix = strtoupper(trim($class_prefix)); |
|
744
|
|
|
// add class prefix ONCE!!! |
|
745
|
|
|
$class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
746
|
|
|
} |
|
747
|
|
|
$class_name = $this->_dependency_map->get_alias($class_name); |
|
748
|
|
|
$class_exists = class_exists($class_name); |
|
749
|
|
|
// if we're only loading the class and it already exists, then let's just return true immediately |
|
750
|
|
|
if ($load_only && $class_exists) { |
|
751
|
|
|
return true; |
|
752
|
|
|
} |
|
753
|
|
|
// $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
754
|
|
|
// $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
755
|
|
|
// $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
756
|
|
View Code Duplication |
if ($this->_cache_on && $cache && ! $load_only) { |
|
757
|
|
|
// return object if it's already cached |
|
758
|
|
|
$cached_class = $this->_get_cached_class($class_name, $class_prefix); |
|
759
|
|
|
if ($cached_class !== null) { |
|
760
|
|
|
return $cached_class; |
|
761
|
|
|
} |
|
762
|
|
|
} |
|
763
|
|
|
// if the class doesn't already exist.. then we need to try and find the file and load it |
|
764
|
|
|
if (! $class_exists) { |
|
765
|
|
|
// get full path to file |
|
766
|
|
|
$path = $this->_resolve_path($class_name, $type, $file_paths); |
|
767
|
|
|
// load the file |
|
768
|
|
|
$loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
769
|
|
|
// if loading failed, or we are only loading a file but NOT instantiating an object |
|
770
|
|
|
if (! $loaded || $load_only) { |
|
771
|
|
|
// return boolean if only loading, or null if an object was expected |
|
772
|
|
|
return $load_only |
|
773
|
|
|
? $loaded |
|
774
|
|
|
: null; |
|
775
|
|
|
} |
|
776
|
|
|
} |
|
777
|
|
|
// instantiate the requested object |
|
778
|
|
|
$class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
779
|
|
|
if ($this->_cache_on && $cache) { |
|
780
|
|
|
// save it for later... kinda like gum { : $ |
|
781
|
|
|
$this->_set_cached_class($class_obj, $class_name, $class_prefix, $from_db); |
|
782
|
|
|
} |
|
783
|
|
|
$this->_cache_on = true; |
|
784
|
|
|
return $class_obj; |
|
785
|
|
|
} |
|
786
|
|
|
|
|
787
|
|
|
|
|
788
|
|
|
|
|
789
|
|
|
/** |
|
790
|
|
|
* attempts to find a cached version of the requested class |
|
791
|
|
|
* by looking in the following places: |
|
792
|
|
|
* $this->{$class_abbreviation} ie: $this->CART |
|
793
|
|
|
* $this->{$class_name} ie: $this->Some_Class |
|
794
|
|
|
* $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
795
|
|
|
* $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
796
|
|
|
* |
|
797
|
|
|
* @param string $class_name |
|
798
|
|
|
* @param string $class_prefix |
|
799
|
|
|
* @return mixed |
|
800
|
|
|
*/ |
|
801
|
|
|
protected function _get_cached_class($class_name, $class_prefix = '') |
|
802
|
|
|
{ |
|
803
|
|
|
if ($class_name === 'EE_Registry') { |
|
804
|
|
|
return $this; |
|
805
|
|
|
} |
|
806
|
|
|
// have to specify something, but not anything that will conflict |
|
807
|
|
|
$class_abbreviation = isset($this->_class_abbreviations[$class_name]) |
|
808
|
|
|
? $this->_class_abbreviations[$class_name] |
|
809
|
|
|
: 'FANCY_BATMAN_PANTS'; |
|
810
|
|
|
$class_name = str_replace('\\', '_', $class_name); |
|
811
|
|
|
// check if class has already been loaded, and return it if it has been |
|
812
|
|
|
if (isset($this->{$class_abbreviation})) { |
|
813
|
|
|
return $this->{$class_abbreviation}; |
|
814
|
|
|
} |
|
815
|
|
|
if (isset ($this->{$class_name})) { |
|
816
|
|
|
return $this->{$class_name}; |
|
817
|
|
|
} |
|
818
|
|
|
if (isset ($this->LIB->{$class_name})) { |
|
819
|
|
|
return $this->LIB->{$class_name}; |
|
820
|
|
|
} |
|
821
|
|
|
if ($class_prefix === 'addon' && isset ($this->addons->{$class_name})) { |
|
822
|
|
|
return $this->addons->{$class_name}; |
|
823
|
|
|
} |
|
824
|
|
|
return null; |
|
825
|
|
|
} |
|
826
|
|
|
|
|
827
|
|
|
|
|
828
|
|
|
|
|
829
|
|
|
/** |
|
830
|
|
|
* removes a cached version of the requested class |
|
831
|
|
|
* |
|
832
|
|
|
* @param string $class_name |
|
833
|
|
|
* @param boolean $addon |
|
834
|
|
|
* @return boolean |
|
835
|
|
|
*/ |
|
836
|
|
|
public function clear_cached_class($class_name, $addon = false) |
|
837
|
|
|
{ |
|
838
|
|
|
// have to specify something, but not anything that will conflict |
|
839
|
|
|
$class_abbreviation = isset($this->_class_abbreviations[$class_name]) |
|
840
|
|
|
? $this->_class_abbreviations[$class_name] |
|
841
|
|
|
: 'FANCY_BATMAN_PANTS'; |
|
842
|
|
|
$class_name = str_replace('\\', '_', $class_name); |
|
843
|
|
|
// check if class has already been loaded, and return it if it has been |
|
844
|
|
|
if (isset($this->{$class_abbreviation})) { |
|
845
|
|
|
$this->{$class_abbreviation} = null; |
|
846
|
|
|
return true; |
|
847
|
|
|
} |
|
848
|
|
|
if (isset($this->{$class_name})) { |
|
849
|
|
|
$this->{$class_name} = null; |
|
850
|
|
|
return true; |
|
851
|
|
|
} |
|
852
|
|
|
if (isset($this->LIB->{$class_name})) { |
|
853
|
|
|
unset($this->LIB->{$class_name}); |
|
854
|
|
|
return true; |
|
855
|
|
|
} |
|
856
|
|
|
if ($addon && isset($this->addons->{$class_name})) { |
|
857
|
|
|
unset($this->addons->{$class_name}); |
|
858
|
|
|
return true; |
|
859
|
|
|
} |
|
860
|
|
|
return false; |
|
861
|
|
|
} |
|
862
|
|
|
|
|
863
|
|
|
|
|
864
|
|
|
|
|
865
|
|
|
/** |
|
866
|
|
|
* attempts to find a full valid filepath for the requested class. |
|
867
|
|
|
* loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
868
|
|
|
* then returns that path if the target file has been found and is readable |
|
869
|
|
|
* |
|
870
|
|
|
* @param string $class_name |
|
871
|
|
|
* @param string $type |
|
872
|
|
|
* @param array $file_paths |
|
873
|
|
|
* @return string | bool |
|
874
|
|
|
*/ |
|
875
|
|
|
protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
876
|
|
|
{ |
|
877
|
|
|
// make sure $file_paths is an array |
|
878
|
|
|
$file_paths = is_array($file_paths) |
|
879
|
|
|
? $file_paths |
|
880
|
|
|
: array($file_paths); |
|
881
|
|
|
// cycle thru paths |
|
882
|
|
|
foreach ($file_paths as $key => $file_path) { |
|
883
|
|
|
// convert all separators to proper DS, if no filepath, then use EE_CLASSES |
|
884
|
|
|
$file_path = $file_path |
|
885
|
|
|
? str_replace(array('/', '\\'), DS, $file_path) |
|
886
|
|
|
: EE_CLASSES; |
|
887
|
|
|
// prep file type |
|
888
|
|
|
$type = ! empty($type) |
|
889
|
|
|
? trim($type, '.') . '.' |
|
890
|
|
|
: ''; |
|
891
|
|
|
// build full file path |
|
892
|
|
|
$file_paths[$key] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
893
|
|
|
//does the file exist and can be read ? |
|
894
|
|
|
if (is_readable($file_paths[$key])) { |
|
895
|
|
|
return $file_paths[$key]; |
|
896
|
|
|
} |
|
897
|
|
|
} |
|
898
|
|
|
return false; |
|
899
|
|
|
} |
|
900
|
|
|
|
|
901
|
|
|
|
|
902
|
|
|
|
|
903
|
|
|
/** |
|
904
|
|
|
* basically just performs a require_once() |
|
905
|
|
|
* but with some error handling |
|
906
|
|
|
* |
|
907
|
|
|
* @param string $path |
|
908
|
|
|
* @param string $class_name |
|
909
|
|
|
* @param string $type |
|
910
|
|
|
* @param array $file_paths |
|
911
|
|
|
* @return bool |
|
912
|
|
|
* @throws EE_Error |
|
913
|
|
|
* @throws ReflectionException |
|
914
|
|
|
*/ |
|
915
|
|
|
protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
916
|
|
|
{ |
|
917
|
|
|
// don't give up! you gotta... |
|
918
|
|
|
try { |
|
919
|
|
|
//does the file exist and can it be read ? |
|
920
|
|
View Code Duplication |
if (! $path) { |
|
921
|
|
|
// so sorry, can't find the file |
|
922
|
|
|
throw new EE_Error ( |
|
923
|
|
|
sprintf( |
|
924
|
|
|
esc_html__( |
|
925
|
|
|
'The %1$s file %2$s could not be located or is not readable due to file permissions. Please ensure that the following filepath(s) are correct: %3$s', |
|
926
|
|
|
'event_espresso' |
|
927
|
|
|
), |
|
928
|
|
|
trim($type, '.'), |
|
929
|
|
|
$class_name, |
|
930
|
|
|
'<br />' . implode(',<br />', $file_paths) |
|
931
|
|
|
) |
|
932
|
|
|
); |
|
933
|
|
|
} |
|
934
|
|
|
// get the file |
|
935
|
|
|
require_once($path); |
|
936
|
|
|
// if the class isn't already declared somewhere |
|
937
|
|
|
if (class_exists($class_name, false) === false) { |
|
938
|
|
|
// so sorry, not a class |
|
939
|
|
|
throw new EE_Error( |
|
940
|
|
|
sprintf( |
|
941
|
|
|
esc_html__('The %s file %s does not appear to contain the %s Class.', 'event_espresso'), |
|
942
|
|
|
$type, |
|
943
|
|
|
$path, |
|
944
|
|
|
$class_name |
|
945
|
|
|
) |
|
946
|
|
|
); |
|
947
|
|
|
} |
|
948
|
|
|
} catch (EE_Error $e) { |
|
949
|
|
|
$e->get_error(); |
|
950
|
|
|
return false; |
|
951
|
|
|
} |
|
952
|
|
|
return true; |
|
953
|
|
|
} |
|
954
|
|
|
|
|
955
|
|
|
|
|
956
|
|
|
|
|
957
|
|
|
/** |
|
958
|
|
|
* _create_object |
|
959
|
|
|
* Attempts to instantiate the requested class via any of the |
|
960
|
|
|
* commonly used instantiation methods employed throughout EE. |
|
961
|
|
|
* The priority for instantiation is as follows: |
|
962
|
|
|
* - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
963
|
|
|
* - model objects via their 'new_instance_from_db' method |
|
964
|
|
|
* - model objects via their 'new_instance' method |
|
965
|
|
|
* - "singleton" classes" via their 'instance' method |
|
966
|
|
|
* - standard instantiable classes via their __constructor |
|
967
|
|
|
* Prior to instantiation, if the classname exists in the dependency_map, |
|
968
|
|
|
* then the constructor for the requested class will be examined to determine |
|
969
|
|
|
* if any dependencies exist, and if they can be injected. |
|
970
|
|
|
* If so, then those classes will be added to the array of arguments passed to the constructor |
|
971
|
|
|
* |
|
972
|
|
|
* @param string $class_name |
|
973
|
|
|
* @param array $arguments |
|
974
|
|
|
* @param string $type |
|
975
|
|
|
* @param bool $from_db |
|
976
|
|
|
* @return null|object |
|
977
|
|
|
* @throws EE_Error |
|
978
|
|
|
* @throws ReflectionException |
|
979
|
|
|
*/ |
|
980
|
|
|
protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
981
|
|
|
{ |
|
982
|
|
|
$class_obj = null; |
|
983
|
|
|
$instantiation_mode = '0) none'; |
|
984
|
|
|
// don't give up! you gotta... |
|
985
|
|
|
try { |
|
986
|
|
|
// create reflection |
|
987
|
|
|
$reflector = $this->get_ReflectionClass($class_name); |
|
988
|
|
|
// make sure arguments are an array |
|
989
|
|
|
$arguments = is_array($arguments) |
|
990
|
|
|
? $arguments |
|
991
|
|
|
: array($arguments); |
|
992
|
|
|
// and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
993
|
|
|
// else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
994
|
|
|
$arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
995
|
|
|
? $arguments |
|
996
|
|
|
: array($arguments); |
|
997
|
|
|
// attempt to inject dependencies ? |
|
998
|
|
|
if ($this->_dependency_map->has($class_name)) { |
|
999
|
|
|
$arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
1000
|
|
|
} |
|
1001
|
|
|
// instantiate the class if possible |
|
1002
|
|
|
if ($reflector->isAbstract()) { |
|
1003
|
|
|
// nothing to instantiate, loading file was enough |
|
1004
|
|
|
// does not throw an exception so $instantiation_mode is unused |
|
1005
|
|
|
// $instantiation_mode = "1) no constructor abstract class"; |
|
1006
|
|
|
$class_obj = true; |
|
1007
|
|
|
} else if (empty($arguments) && $reflector->getConstructor() === null && $reflector->isInstantiable()) { |
|
1008
|
|
|
// no constructor = static methods only... nothing to instantiate, loading file was enough |
|
1009
|
|
|
$instantiation_mode = '2) no constructor but instantiable'; |
|
1010
|
|
|
$class_obj = $reflector->newInstance(); |
|
1011
|
|
|
} else if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
1012
|
|
|
$instantiation_mode = '3) new_instance_from_db()'; |
|
1013
|
|
|
$class_obj = call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
1014
|
|
|
} else if (method_exists($class_name, 'new_instance')) { |
|
1015
|
|
|
$instantiation_mode = '4) new_instance()'; |
|
1016
|
|
|
$class_obj = call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
1017
|
|
|
} else if (method_exists($class_name, 'instance')) { |
|
1018
|
|
|
$instantiation_mode = '5) instance()'; |
|
1019
|
|
|
$class_obj = call_user_func_array(array($class_name, 'instance'), $arguments); |
|
1020
|
|
|
} else if ($reflector->isInstantiable()) { |
|
1021
|
|
|
$instantiation_mode = '6) constructor'; |
|
1022
|
|
|
$class_obj = $reflector->newInstanceArgs($arguments); |
|
1023
|
|
|
} else { |
|
1024
|
|
|
// heh ? something's not right ! |
|
1025
|
|
|
throw new EE_Error( |
|
1026
|
|
|
sprintf( |
|
1027
|
|
|
esc_html__('The %s file %s could not be instantiated.', 'event_espresso'), |
|
1028
|
|
|
$type, |
|
1029
|
|
|
$class_name |
|
1030
|
|
|
) |
|
1031
|
|
|
); |
|
1032
|
|
|
} |
|
1033
|
|
|
} catch (Exception $e) { |
|
1034
|
|
|
if (! $e instanceof EE_Error) { |
|
1035
|
|
|
$e = new EE_Error( |
|
1036
|
|
|
sprintf( |
|
1037
|
|
|
esc_html__( |
|
1038
|
|
|
'The following error occurred while attempting to instantiate "%1$s": %2$s %3$s %2$s instantiation mode : %4$s', |
|
1039
|
|
|
'event_espresso' |
|
1040
|
|
|
), |
|
1041
|
|
|
$class_name, |
|
1042
|
|
|
'<br />', |
|
1043
|
|
|
$e->getMessage(), |
|
1044
|
|
|
$instantiation_mode |
|
1045
|
|
|
) |
|
1046
|
|
|
); |
|
1047
|
|
|
} |
|
1048
|
|
|
$e->get_error(); |
|
1049
|
|
|
} |
|
1050
|
|
|
return $class_obj; |
|
1051
|
|
|
} |
|
1052
|
|
|
|
|
1053
|
|
|
|
|
1054
|
|
|
|
|
1055
|
|
|
/** |
|
1056
|
|
|
* @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
1057
|
|
|
* @param array $array |
|
1058
|
|
|
* @return bool |
|
1059
|
|
|
*/ |
|
1060
|
|
|
protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
1061
|
|
|
{ |
|
1062
|
|
|
return ! empty($array) |
|
1063
|
|
|
? array_keys($array) === range(0, count($array) - 1) |
|
1064
|
|
|
: true; |
|
1065
|
|
|
} |
|
1066
|
|
|
|
|
1067
|
|
|
|
|
1068
|
|
|
|
|
1069
|
|
|
/** |
|
1070
|
|
|
* getReflectionClass |
|
1071
|
|
|
* checks if a ReflectionClass object has already been generated for a class |
|
1072
|
|
|
* and returns that instead of creating a new one |
|
1073
|
|
|
* |
|
1074
|
|
|
* @param string $class_name |
|
1075
|
|
|
* @return ReflectionClass |
|
1076
|
|
|
* @throws ReflectionException |
|
1077
|
|
|
*/ |
|
1078
|
|
View Code Duplication |
public function get_ReflectionClass($class_name) |
|
1079
|
|
|
{ |
|
1080
|
|
|
if ( |
|
1081
|
|
|
! isset($this->_reflectors[$class_name]) |
|
1082
|
|
|
|| ! $this->_reflectors[$class_name] instanceof ReflectionClass |
|
1083
|
|
|
) { |
|
1084
|
|
|
$this->_reflectors[$class_name] = new ReflectionClass($class_name); |
|
1085
|
|
|
} |
|
1086
|
|
|
return $this->_reflectors[$class_name]; |
|
1087
|
|
|
} |
|
1088
|
|
|
|
|
1089
|
|
|
|
|
1090
|
|
|
|
|
1091
|
|
|
/** |
|
1092
|
|
|
* _resolve_dependencies |
|
1093
|
|
|
* examines the constructor for the requested class to determine |
|
1094
|
|
|
* if any dependencies exist, and if they can be injected. |
|
1095
|
|
|
* If so, then those classes will be added to the array of arguments passed to the constructor |
|
1096
|
|
|
* PLZ NOTE: this is achieved by type hinting the constructor params |
|
1097
|
|
|
* For example: |
|
1098
|
|
|
* if attempting to load a class "Foo" with the following constructor: |
|
1099
|
|
|
* __construct( Bar $bar_class, Fighter $grohl_class ) |
|
1100
|
|
|
* then $bar_class and $grohl_class will be added to the $arguments array, |
|
1101
|
|
|
* but only IF they are NOT already present in the incoming arguments array, |
|
1102
|
|
|
* and the correct classes can be loaded |
|
1103
|
|
|
* |
|
1104
|
|
|
* @param ReflectionClass $reflector |
|
1105
|
|
|
* @param string $class_name |
|
1106
|
|
|
* @param array $arguments |
|
1107
|
|
|
* @return array |
|
1108
|
|
|
* @throws EE_Error |
|
1109
|
|
|
* @throws ReflectionException |
|
1110
|
|
|
*/ |
|
1111
|
|
|
protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, $arguments = array()) |
|
1112
|
|
|
{ |
|
1113
|
|
|
// let's examine the constructor |
|
1114
|
|
|
$constructor = $reflector->getConstructor(); |
|
1115
|
|
|
// whu? huh? nothing? |
|
1116
|
|
|
if (! $constructor) { |
|
1117
|
|
|
return $arguments; |
|
1118
|
|
|
} |
|
1119
|
|
|
// get constructor parameters |
|
1120
|
|
|
$params = $constructor->getParameters(); |
|
1121
|
|
|
// and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
1122
|
|
|
$argument_keys = array_keys($arguments); |
|
1123
|
|
|
// now loop thru all of the constructors expected parameters |
|
1124
|
|
|
foreach ($params as $index => $param) { |
|
1125
|
|
|
// is this a dependency for a specific class ? |
|
1126
|
|
|
$param_class = $param->getClass() |
|
1127
|
|
|
? $param->getClass()->name |
|
1128
|
|
|
: null; |
|
1129
|
|
|
// BUT WAIT !!! This class may be an alias for something else (or getting replaced at runtime) |
|
1130
|
|
|
$param_class = $this->_dependency_map->has_alias($param_class, $class_name) |
|
1131
|
|
|
? $this->_dependency_map->get_alias($param_class, $class_name) |
|
1132
|
|
|
: $param_class; |
|
1133
|
|
|
if ( |
|
1134
|
|
|
// param is not even a class |
|
1135
|
|
|
empty($param_class) |
|
1136
|
|
|
// and something already exists in the incoming arguments for this param |
|
1137
|
|
|
&& isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
1138
|
|
|
) { |
|
1139
|
|
|
// so let's skip this argument and move on to the next |
|
1140
|
|
|
continue; |
|
1141
|
|
|
} |
|
1142
|
|
|
if ( |
|
1143
|
|
|
// parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
1144
|
|
|
! empty($param_class) |
|
1145
|
|
|
&& isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
1146
|
|
|
&& $arguments[$argument_keys[$index]] instanceof $param_class |
|
1147
|
|
|
) { |
|
1148
|
|
|
// skip this argument and move on to the next |
|
1149
|
|
|
continue; |
|
1150
|
|
|
} |
|
1151
|
|
|
if ( |
|
1152
|
|
|
// parameter is type hinted as a class, and should be injected |
|
1153
|
|
|
! empty($param_class) |
|
1154
|
|
|
&& $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
1155
|
|
|
) { |
|
1156
|
|
|
$arguments = $this->_resolve_dependency($class_name, $param_class, $arguments, $index); |
|
1157
|
|
|
} else { |
|
1158
|
|
|
try { |
|
1159
|
|
|
$arguments[$index] = $param->getDefaultValue(); |
|
1160
|
|
|
} catch (ReflectionException $e) { |
|
1161
|
|
|
throw new ReflectionException( |
|
1162
|
|
|
sprintf( |
|
1163
|
|
|
esc_html__('%1$s for parameter "$%2$s"', 'event_espresso'), |
|
1164
|
|
|
$e->getMessage(), |
|
1165
|
|
|
$param->getName() |
|
1166
|
|
|
) |
|
1167
|
|
|
); |
|
1168
|
|
|
} |
|
1169
|
|
|
} |
|
1170
|
|
|
} |
|
1171
|
|
|
return $arguments; |
|
1172
|
|
|
} |
|
1173
|
|
|
|
|
1174
|
|
|
|
|
1175
|
|
|
|
|
1176
|
|
|
/** |
|
1177
|
|
|
* @param string $class_name |
|
1178
|
|
|
* @param string $param_class |
|
1179
|
|
|
* @param array $arguments |
|
1180
|
|
|
* @param mixed $index |
|
1181
|
|
|
* @return array |
|
1182
|
|
|
* @throws EE_Error |
|
1183
|
|
|
* @throws ReflectionException |
|
1184
|
|
|
*/ |
|
1185
|
|
|
protected function _resolve_dependency($class_name, $param_class, $arguments, $index) |
|
1186
|
|
|
{ |
|
1187
|
|
|
$dependency = null; |
|
1188
|
|
|
// should dependency be loaded from cache ? |
|
1189
|
|
|
$cache_on = $this->_dependency_map->loading_strategy_for_class_dependency($class_name, $param_class) |
|
1190
|
|
|
!== EE_Dependency_Map::load_new_object; |
|
1191
|
|
|
// we might have a dependency... |
|
1192
|
|
|
// let's MAYBE try and find it in our cache if that's what's been requested |
|
1193
|
|
|
$cached_class = $cache_on |
|
1194
|
|
|
? $this->_get_cached_class($param_class) |
|
1195
|
|
|
: null; |
|
1196
|
|
|
// and grab it if it exists |
|
1197
|
|
|
if ($cached_class instanceof $param_class) { |
|
1198
|
|
|
$dependency = $cached_class; |
|
1199
|
|
|
} else if ($param_class !== $class_name) { |
|
1200
|
|
|
// obtain the loader method from the dependency map |
|
1201
|
|
|
$loader = $this->_dependency_map->class_loader($param_class); |
|
1202
|
|
|
// is loader a custom closure ? |
|
1203
|
|
|
if ($loader instanceof Closure) { |
|
1204
|
|
|
$dependency = $loader(); |
|
1205
|
|
|
} else { |
|
1206
|
|
|
// set the cache on property for the recursive loading call |
|
1207
|
|
|
$this->_cache_on = $cache_on; |
|
1208
|
|
|
// if not, then let's try and load it via the registry |
|
1209
|
|
|
if ($loader && method_exists($this, $loader)) { |
|
1210
|
|
|
$dependency = $this->{$loader}($param_class); |
|
1211
|
|
|
} else { |
|
1212
|
|
|
$dependency = $this->create($param_class, array(), $cache_on); |
|
1213
|
|
|
} |
|
1214
|
|
|
} |
|
1215
|
|
|
} |
|
1216
|
|
|
// did we successfully find the correct dependency ? |
|
1217
|
|
|
if ($dependency instanceof $param_class) { |
|
1218
|
|
|
// then let's inject it into the incoming array of arguments at the correct location |
|
1219
|
|
|
if (isset($argument_keys[$index])) { |
|
|
|
|
|
|
1220
|
|
|
$arguments[$argument_keys[$index]] = $dependency; |
|
1221
|
|
|
} else { |
|
1222
|
|
|
$arguments[$index] = $dependency; |
|
1223
|
|
|
} |
|
1224
|
|
|
} |
|
1225
|
|
|
return $arguments; |
|
1226
|
|
|
} |
|
1227
|
|
|
|
|
1228
|
|
|
|
|
1229
|
|
|
|
|
1230
|
|
|
/** |
|
1231
|
|
|
* _set_cached_class |
|
1232
|
|
|
* attempts to cache the instantiated class locally |
|
1233
|
|
|
* in one of the following places, in the following order: |
|
1234
|
|
|
* $this->{class_abbreviation} ie: $this->CART |
|
1235
|
|
|
* $this->{$class_name} ie: $this->Some_Class |
|
1236
|
|
|
* $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
1237
|
|
|
* $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
1238
|
|
|
* |
|
1239
|
|
|
* @param object $class_obj |
|
1240
|
|
|
* @param string $class_name |
|
1241
|
|
|
* @param string $class_prefix |
|
1242
|
|
|
* @param bool $from_db |
|
1243
|
|
|
* @return void |
|
1244
|
|
|
*/ |
|
1245
|
|
|
protected function _set_cached_class($class_obj, $class_name, $class_prefix = '', $from_db = false) |
|
1246
|
|
|
{ |
|
1247
|
|
|
if ($class_name === 'EE_Registry' || empty($class_obj)) { |
|
1248
|
|
|
return; |
|
1249
|
|
|
} |
|
1250
|
|
|
// return newly instantiated class |
|
1251
|
|
|
if (isset($this->_class_abbreviations[$class_name])) { |
|
1252
|
|
|
$class_abbreviation = $this->_class_abbreviations[$class_name]; |
|
1253
|
|
|
$this->{$class_abbreviation} = $class_obj; |
|
1254
|
|
|
return; |
|
1255
|
|
|
} |
|
1256
|
|
|
$class_name = str_replace('\\', '_', $class_name); |
|
1257
|
|
|
if (property_exists($this, $class_name)) { |
|
1258
|
|
|
$this->{$class_name} = $class_obj; |
|
1259
|
|
|
return; |
|
1260
|
|
|
} |
|
1261
|
|
|
if ($class_prefix === 'addon') { |
|
1262
|
|
|
$this->addons->{$class_name} = $class_obj; |
|
1263
|
|
|
return; |
|
1264
|
|
|
} |
|
1265
|
|
|
if (! $from_db) { |
|
1266
|
|
|
$this->LIB->{$class_name} = $class_obj; |
|
1267
|
|
|
} |
|
1268
|
|
|
} |
|
1269
|
|
|
|
|
1270
|
|
|
|
|
1271
|
|
|
|
|
1272
|
|
|
/** |
|
1273
|
|
|
* call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
1274
|
|
|
* |
|
1275
|
|
|
* @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
1276
|
|
|
* in the EE_Dependency_Map::$_class_loaders array, |
|
1277
|
|
|
* including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
1278
|
|
|
* @param array $arguments |
|
1279
|
|
|
* @return object |
|
1280
|
|
|
*/ |
|
1281
|
|
|
public static function factory($classname, $arguments = array()) |
|
1282
|
|
|
{ |
|
1283
|
|
|
$loader = self::instance()->_dependency_map->class_loader($classname); |
|
1284
|
|
|
if ($loader instanceof Closure) { |
|
1285
|
|
|
return $loader($arguments); |
|
1286
|
|
|
} |
|
1287
|
|
|
if (method_exists(self::instance(), $loader)) { |
|
1288
|
|
|
return self::instance()->{$loader}($classname, $arguments); |
|
1289
|
|
|
} |
|
1290
|
|
|
return null; |
|
1291
|
|
|
} |
|
1292
|
|
|
|
|
1293
|
|
|
|
|
1294
|
|
|
|
|
1295
|
|
|
/** |
|
1296
|
|
|
* Gets the addon by its name/slug (not classname. For that, just |
|
1297
|
|
|
* use the classname as the property name on EE_Config::instance()->addons) |
|
1298
|
|
|
* |
|
1299
|
|
|
* @param string $name |
|
1300
|
|
|
* @return EE_Addon |
|
1301
|
|
|
*/ |
|
1302
|
|
|
public function get_addon_by_name($name) |
|
1303
|
|
|
{ |
|
1304
|
|
|
foreach ($this->addons as $addon) { |
|
1305
|
|
|
if ($addon->name() === $name) { |
|
1306
|
|
|
return $addon; |
|
1307
|
|
|
} |
|
1308
|
|
|
} |
|
1309
|
|
|
return null; |
|
1310
|
|
|
} |
|
1311
|
|
|
|
|
1312
|
|
|
|
|
1313
|
|
|
|
|
1314
|
|
|
/** |
|
1315
|
|
|
* Gets an array of all the registered addons, where the keys are their names. (ie, what each returns for their |
|
1316
|
|
|
* name() function) They're already available on EE_Config::instance()->addons as properties, where each property's |
|
1317
|
|
|
* name is the addon's classname. So if you just want to get the addon by classname, use |
|
1318
|
|
|
* EE_Config::instance()->addons->{classname} |
|
1319
|
|
|
* |
|
1320
|
|
|
* @return EE_Addon[] where the KEYS are the addon's name() |
|
1321
|
|
|
*/ |
|
1322
|
|
|
public function get_addons_by_name() |
|
1323
|
|
|
{ |
|
1324
|
|
|
$addons = array(); |
|
1325
|
|
|
foreach ($this->addons as $addon) { |
|
1326
|
|
|
$addons[$addon->name()] = $addon; |
|
1327
|
|
|
} |
|
1328
|
|
|
return $addons; |
|
1329
|
|
|
} |
|
1330
|
|
|
|
|
1331
|
|
|
|
|
1332
|
|
|
|
|
1333
|
|
|
/** |
|
1334
|
|
|
* Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
1335
|
|
|
* a stale copy of it around |
|
1336
|
|
|
* |
|
1337
|
|
|
* @param string $model_name |
|
1338
|
|
|
* @return \EEM_Base |
|
1339
|
|
|
* @throws \EE_Error |
|
1340
|
|
|
*/ |
|
1341
|
|
|
public function reset_model($model_name) |
|
1342
|
|
|
{ |
|
1343
|
|
|
$model_class_name = strpos($model_name, 'EEM_') !== 0 |
|
1344
|
|
|
? "EEM_{$model_name}" |
|
1345
|
|
|
: $model_name; |
|
1346
|
|
|
if (! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
1347
|
|
|
return null; |
|
1348
|
|
|
} |
|
1349
|
|
|
//get that model reset it and make sure we nuke the old reference to it |
|
1350
|
|
|
if ($this->LIB->{$model_class_name} instanceof $model_class_name |
|
1351
|
|
|
&& is_callable( |
|
1352
|
|
|
array($model_class_name, 'reset') |
|
1353
|
|
|
)) { |
|
1354
|
|
|
$this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
1355
|
|
|
} else { |
|
1356
|
|
|
throw new EE_Error(sprintf(esc_html__('Model %s does not have a method "reset"', 'event_espresso'), $model_name)); |
|
1357
|
|
|
} |
|
1358
|
|
|
return $this->LIB->{$model_class_name}; |
|
1359
|
|
|
} |
|
1360
|
|
|
|
|
1361
|
|
|
|
|
1362
|
|
|
|
|
1363
|
|
|
/** |
|
1364
|
|
|
* Resets the registry. |
|
1365
|
|
|
* The criteria for what gets reset is based on what can be shared between sites on the same request when |
|
1366
|
|
|
* switch_to_blog is used in a multisite install. Here is a list of things that are NOT reset. |
|
1367
|
|
|
* - $_dependency_map |
|
1368
|
|
|
* - $_class_abbreviations |
|
1369
|
|
|
* - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
1370
|
|
|
* - $REQ: Still on the same request so no need to change. |
|
1371
|
|
|
* - $CAP: There is no site specific state in the EE_Capability class. |
|
1372
|
|
|
* - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only |
|
1373
|
|
|
* one Session can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
1374
|
|
|
* - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
1375
|
|
|
* for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
1376
|
|
|
* switch or on the restore. |
|
1377
|
|
|
* - $modules |
|
1378
|
|
|
* - $shortcodes |
|
1379
|
|
|
* - $widgets |
|
1380
|
|
|
* |
|
1381
|
|
|
* @param boolean $hard whether to reset data in the database too, or just refresh |
|
1382
|
|
|
* the Registry to its state at the beginning of the request |
|
1383
|
|
|
* @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
1384
|
|
|
* or just reset without re-instantiating (handy to set to FALSE if you're not |
|
1385
|
|
|
* sure if you CAN currently reinstantiate the singletons at the moment) |
|
1386
|
|
|
* @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so |
|
1387
|
|
|
* client |
|
1388
|
|
|
* code instead can just change the model context to a different blog id if |
|
1389
|
|
|
* necessary |
|
1390
|
|
|
* @return EE_Registry |
|
1391
|
|
|
* @throws EE_Error |
|
1392
|
|
|
* @throws ReflectionException |
|
1393
|
|
|
*/ |
|
1394
|
|
|
public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
1395
|
|
|
{ |
|
1396
|
|
|
$instance = self::instance(); |
|
1397
|
|
|
$instance->_cache_on = true; |
|
1398
|
|
|
// reset some "special" classes |
|
1399
|
|
|
EEH_Activation::reset(); |
|
1400
|
|
|
$instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
1401
|
|
|
$instance->CART = null; |
|
1402
|
|
|
$instance->MRM = null; |
|
1403
|
|
|
$instance->AssetsRegistry = $instance->create('EventEspresso\core\services\assets\Registry'); |
|
1404
|
|
|
//messages reset |
|
1405
|
|
|
EED_Messages::reset(); |
|
1406
|
|
|
//handle of objects cached on LIB |
|
1407
|
|
|
foreach (array('LIB', 'modules') as $cache) { |
|
1408
|
|
|
foreach ($instance->{$cache} as $class_name => $class) { |
|
1409
|
|
|
if (self::_reset_and_unset_object($class, $reset_models)) { |
|
1410
|
|
|
unset($instance->{$cache}->{$class_name}); |
|
1411
|
|
|
} |
|
1412
|
|
|
} |
|
1413
|
|
|
} |
|
1414
|
|
|
return $instance; |
|
1415
|
|
|
} |
|
1416
|
|
|
|
|
1417
|
|
|
|
|
1418
|
|
|
|
|
1419
|
|
|
/** |
|
1420
|
|
|
* if passed object implements ResettableInterface, then call it's reset() method |
|
1421
|
|
|
* if passed object implements InterminableInterface, then return false, |
|
1422
|
|
|
* to indicate that it should NOT be cleared from the Registry cache |
|
1423
|
|
|
* |
|
1424
|
|
|
* @param $object |
|
1425
|
|
|
* @param bool $reset_models |
|
1426
|
|
|
* @return bool returns true if cached object should be unset |
|
1427
|
|
|
*/ |
|
1428
|
|
|
private static function _reset_and_unset_object($object, $reset_models) |
|
1429
|
|
|
{ |
|
1430
|
|
|
if (! is_object($object)) { |
|
1431
|
|
|
// don't unset anything that's not an object |
|
1432
|
|
|
return false; |
|
1433
|
|
|
} |
|
1434
|
|
|
if ($object instanceof EED_Module) { |
|
1435
|
|
|
$object::reset(); |
|
1436
|
|
|
// don't unset modules |
|
1437
|
|
|
return false; |
|
1438
|
|
|
} |
|
1439
|
|
|
if ($object instanceof ResettableInterface) { |
|
1440
|
|
|
if ($object instanceof EEM_Base) { |
|
1441
|
|
|
if ($reset_models) { |
|
1442
|
|
|
$object->reset(); |
|
1443
|
|
|
return true; |
|
1444
|
|
|
} |
|
1445
|
|
|
return false; |
|
1446
|
|
|
} |
|
1447
|
|
|
$object->reset(); |
|
1448
|
|
|
return true; |
|
1449
|
|
|
} |
|
1450
|
|
|
if (! $object instanceof InterminableInterface) { |
|
1451
|
|
|
return true; |
|
1452
|
|
|
} |
|
1453
|
|
|
return false; |
|
1454
|
|
|
} |
|
1455
|
|
|
|
|
1456
|
|
|
|
|
1457
|
|
|
|
|
1458
|
|
|
/** |
|
1459
|
|
|
* Gets all the custom post type models defined |
|
1460
|
|
|
* |
|
1461
|
|
|
* @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
1462
|
|
|
*/ |
|
1463
|
|
|
public function cpt_models() |
|
1464
|
|
|
{ |
|
1465
|
|
|
$cpt_models = array(); |
|
1466
|
|
|
foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
1467
|
|
|
if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
1468
|
|
|
$cpt_models[$short_name] = $classname; |
|
1469
|
|
|
} |
|
1470
|
|
|
} |
|
1471
|
|
|
return $cpt_models; |
|
1472
|
|
|
} |
|
1473
|
|
|
|
|
1474
|
|
|
|
|
1475
|
|
|
|
|
1476
|
|
|
/** |
|
1477
|
|
|
* @return \EE_Config |
|
1478
|
|
|
*/ |
|
1479
|
|
|
public static function CFG() |
|
1480
|
|
|
{ |
|
1481
|
|
|
return self::instance()->CFG; |
|
1482
|
|
|
} |
|
1483
|
|
|
|
|
1484
|
|
|
|
|
1485
|
|
|
} |
|
1486
|
|
|
// End of file EE_Registry.core.php |
|
1487
|
|
|
// Location: ./core/EE_Registry.core.php |
|
1488
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):