Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EE_Registry often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EE_Registry, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { exit('No direct script access allowed'); } |
||
11 | class EE_Registry { |
||
12 | |||
13 | /** |
||
14 | * EE_Registry Object |
||
15 | * |
||
16 | * @var EE_Registry $_instance |
||
17 | * @access private |
||
18 | */ |
||
19 | private static $_instance = null; |
||
20 | |||
21 | /** |
||
22 | * @var EE_Dependency_Map $_dependency_map |
||
23 | * @access protected |
||
24 | */ |
||
25 | protected $_dependency_map = null; |
||
26 | |||
27 | /** |
||
28 | * @var array $_class_abbreviations |
||
29 | * @access protected |
||
30 | */ |
||
31 | protected $_class_abbreviations = array(); |
||
32 | |||
33 | /** |
||
34 | * EE_Cart Object |
||
35 | * @access public |
||
36 | * @var EE_Cart $CART |
||
37 | */ |
||
38 | public $CART = null; |
||
39 | |||
40 | /** |
||
41 | * EE_Config Object |
||
42 | * @access public |
||
43 | * @var EE_Config $CFG |
||
44 | */ |
||
45 | public $CFG = null; |
||
46 | |||
47 | /** |
||
48 | * EE_Network_Config Object |
||
49 | * @access public |
||
50 | * @var EE_Network_Config $NET_CFG |
||
51 | */ |
||
52 | public $NET_CFG = null; |
||
53 | |||
54 | /** |
||
55 | * StdClass object for storing library classes in |
||
56 | * |
||
57 | * @public LIB |
||
58 | * @var StdClass $LIB |
||
59 | */ |
||
60 | public $LIB = null; |
||
61 | |||
62 | /** |
||
63 | * EE_Request_Handler Object |
||
64 | * @access public |
||
65 | * @var EE_Request_Handler $REQ |
||
66 | */ |
||
67 | public $REQ = null; |
||
68 | |||
69 | /** |
||
70 | * EE_Session Object |
||
71 | * @access public |
||
72 | * @var EE_Session $SSN |
||
73 | */ |
||
74 | public $SSN = null; |
||
75 | |||
76 | /** |
||
77 | * holds the ee capabilities object. |
||
78 | * |
||
79 | * @since 4.5.0 |
||
80 | * |
||
81 | * @var EE_Capabilities |
||
82 | */ |
||
83 | public $CAP = null; |
||
84 | |||
85 | /** |
||
86 | * holds the EE_Message_Resource_Manager object. |
||
87 | * |
||
88 | * @since 4.9.0 |
||
89 | * |
||
90 | * @var EE_Message_Resource_Manager |
||
91 | */ |
||
92 | public $MRM = null; |
||
93 | |||
94 | /** |
||
95 | * $addons - StdClass object for holding addons which have registered themselves to work with EE core |
||
96 | * @access public |
||
97 | * @var EE_Addon[] |
||
98 | */ |
||
99 | public $addons = null; |
||
100 | |||
101 | /** |
||
102 | * $models |
||
103 | * @access public |
||
104 | * @var EEM_Base[] $models keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
||
105 | */ |
||
106 | public $models = array(); |
||
107 | |||
108 | /** |
||
109 | * $modules |
||
110 | * @access public |
||
111 | * @var EED_Module[] $modules |
||
112 | */ |
||
113 | public $modules = null; |
||
114 | |||
115 | /** |
||
116 | * $shortcodes |
||
117 | * @access public |
||
118 | * @var EES_Shortcode[] $shortcodes |
||
119 | */ |
||
120 | public $shortcodes = null; |
||
121 | |||
122 | /** |
||
123 | * $widgets |
||
124 | * @access public |
||
125 | * @var WP_Widget[] $widgets |
||
126 | */ |
||
127 | public $widgets = null; |
||
128 | |||
129 | /** |
||
130 | * $non_abstract_db_models |
||
131 | * @access public |
||
132 | * @var array this is an array of all implemented model names (i.e. not the parent abstract models, or models |
||
133 | * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
||
134 | * Keys are model "short names" (eg "Event") as used in model relations, and values are |
||
135 | * classnames (eg "EEM_Event") |
||
136 | */ |
||
137 | public $non_abstract_db_models = array(); |
||
138 | |||
139 | /** |
||
140 | * $i18n_js_strings - internationalization for JS strings |
||
141 | * usage: EE_Registry::i18n_js_strings['string_key'] = __( 'string to translate.', 'event_espresso' ); |
||
142 | * in js file: var translatedString = eei18n.string_key; |
||
143 | * |
||
144 | * @access public |
||
145 | * @var array |
||
146 | */ |
||
147 | public static $i18n_js_strings = array(); |
||
148 | |||
149 | /** |
||
150 | * $main_file - path to espresso.php |
||
151 | * |
||
152 | * @access public |
||
153 | * @var array |
||
154 | */ |
||
155 | public $main_file; |
||
156 | |||
157 | /** |
||
158 | * array of ReflectionClass objects where the key is the class name |
||
159 | * |
||
160 | * @access public |
||
161 | * @var ReflectionClass[] |
||
162 | */ |
||
163 | public $_reflectors; |
||
164 | |||
165 | /** |
||
166 | * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
||
167 | * |
||
168 | * @access protected |
||
169 | * @var boolean $_cache_on |
||
170 | */ |
||
171 | protected $_cache_on = true; |
||
172 | |||
173 | |||
174 | |||
175 | /** |
||
176 | * @singleton method used to instantiate class object |
||
177 | * @access public |
||
178 | * @param \EE_Dependency_Map $dependency_map |
||
179 | * @return \EE_Registry instance |
||
180 | */ |
||
181 | public static function instance( \EE_Dependency_Map $dependency_map = null ) { |
||
182 | // check if class object is instantiated |
||
183 | if ( ! self::$_instance instanceof EE_Registry ) { |
||
184 | self::$_instance = new EE_Registry( $dependency_map ); |
||
|
|||
185 | } |
||
186 | return self::$_instance; |
||
187 | } |
||
188 | |||
189 | |||
190 | |||
191 | /** |
||
192 | *protected constructor to prevent direct creation |
||
193 | * |
||
194 | * @Constructor |
||
195 | * @access protected |
||
196 | * @param \EE_Dependency_Map $dependency_map |
||
197 | * @return \EE_Registry |
||
198 | */ |
||
199 | protected function __construct( \EE_Dependency_Map $dependency_map ) { |
||
200 | $this->_dependency_map = $dependency_map; |
||
201 | add_action( 'EE_Load_Espresso_Core__handle_request__initialize_core_loading', array( $this, 'initialize' ) ); |
||
202 | } |
||
203 | |||
204 | |||
205 | |||
206 | /** |
||
207 | * initialize |
||
208 | */ |
||
209 | public function initialize() { |
||
210 | $this->_class_abbreviations = apply_filters( |
||
211 | 'FHEE__EE_Registry____construct___class_abbreviations', |
||
212 | array( |
||
213 | 'EE_Config' => 'CFG', |
||
214 | 'EE_Session' => 'SSN', |
||
215 | 'EE_Capabilities' => 'CAP', |
||
216 | 'EE_Cart' => 'CART', |
||
217 | 'EE_Network_Config' => 'NET_CFG', |
||
218 | 'EE_Request_Handler' => 'REQ', |
||
219 | 'EE_Message_Resource_Manager' => 'MRM', |
||
220 | ) |
||
221 | ); |
||
222 | // class library |
||
223 | $this->LIB = new StdClass(); |
||
224 | $this->addons = new StdClass(); |
||
225 | $this->modules = new StdClass(); |
||
226 | $this->shortcodes = new StdClass(); |
||
227 | $this->widgets = new StdClass(); |
||
228 | $this->load_core( 'Base', array(), true ); |
||
229 | // add our request and response objects to the cache |
||
230 | $request_loader = $this->_dependency_map->class_loader( 'EE_Request' ); |
||
231 | $this->_set_cached_class( |
||
232 | $request_loader(), |
||
233 | 'EE_Request' |
||
234 | ); |
||
235 | $response_loader = $this->_dependency_map->class_loader( 'EE_Response' ); |
||
236 | $this->_set_cached_class( |
||
237 | $response_loader(), |
||
238 | 'EE_Response' |
||
239 | ); |
||
240 | add_action( 'AHEE__EE_System__set_hooks_for_core', array( $this, 'init' ) ); |
||
241 | } |
||
242 | |||
243 | |||
244 | |||
245 | /** |
||
246 | * init |
||
247 | * |
||
248 | * @access public |
||
249 | * @return void |
||
250 | */ |
||
251 | public function init() { |
||
258 | |||
259 | |||
260 | |||
261 | /** |
||
262 | * localize_i18n_js_strings |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | public static function localize_i18n_js_strings() { |
||
276 | |||
277 | |||
278 | |||
279 | /** |
||
280 | * @param mixed string | EED_Module $module |
||
281 | */ |
||
282 | public function add_module( $module ) { |
||
293 | |||
294 | |||
295 | |||
296 | /** |
||
297 | * @param string $module_name |
||
298 | * @return mixed EED_Module | NULL |
||
299 | */ |
||
300 | public function get_module( $module_name = '' ) { |
||
303 | |||
304 | |||
305 | |||
306 | /** |
||
307 | * loads core classes - must be singletons |
||
308 | * |
||
309 | * @access public |
||
310 | * @param string $class_name - simple class name ie: session |
||
311 | * @param mixed $arguments |
||
312 | * @param bool $load_only |
||
313 | * @return mixed |
||
314 | */ |
||
315 | public function load_core( $class_name, $arguments = array(), $load_only = false ) { |
||
330 | |||
331 | |||
332 | |||
333 | /** |
||
334 | * loads service classes |
||
335 | * |
||
336 | * @access public |
||
337 | * @param string $class_name - simple class name ie: session |
||
338 | * @param mixed $arguments |
||
339 | * @param bool $load_only |
||
340 | * @return mixed |
||
341 | */ |
||
342 | public function load_service( $class_name, $arguments = array(), $load_only = false ) { |
||
352 | |||
353 | |||
354 | |||
355 | /** |
||
356 | * loads data_migration_scripts |
||
357 | * |
||
358 | * @access public |
||
359 | * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
||
360 | * @param mixed $arguments |
||
361 | * @return EE_Data_Migration_Script_Base |
||
362 | */ |
||
363 | public function load_dms( $class_name, $arguments = array() ) { |
||
367 | |||
368 | |||
369 | |||
370 | /** |
||
371 | * loads object creating classes - must be singletons |
||
372 | * |
||
373 | * @param string $class_name - simple class name ie: attendee |
||
374 | * @param mixed $arguments - an array of arguments to pass to the class |
||
375 | * @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
||
376 | * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then set this to FALSE (ie. when instantiating model objects from client in a loop) |
||
377 | * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate (default) |
||
378 | * @return EE_Base_Class | bool |
||
379 | */ |
||
380 | public function load_class( $class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false ) { |
||
389 | |||
390 | |||
391 | |||
392 | /** |
||
393 | * loads helper classes - must be singletons |
||
394 | * |
||
395 | * @param string $class_name - simple class name ie: price |
||
396 | * @param mixed $arguments |
||
397 | * @param bool $load_only |
||
398 | * @return EEH_Base | bool |
||
399 | */ |
||
400 | public function load_helper( $class_name, $arguments = array(), $load_only = true ) { |
||
401 | // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
||
402 | $helper_paths = apply_filters( 'FHEE__EE_Registry__load_helper__helper_paths', array( EE_HELPERS ) ); |
||
403 | // retrieve instantiated class |
||
404 | return $this->_load( $helper_paths, 'EEH_', $class_name, 'helper', $arguments, false, true, $load_only ); |
||
405 | } |
||
406 | |||
407 | |||
408 | |||
409 | /** |
||
410 | * loads core classes - must be singletons |
||
411 | * |
||
412 | * @access public |
||
413 | * @param string $class_name - simple class name ie: session |
||
414 | * @param mixed $arguments |
||
415 | * @param bool $load_only |
||
416 | * @param bool $cache whether to cache the object or not. |
||
417 | * @return mixed |
||
418 | */ |
||
419 | public function load_lib( $class_name, $arguments = array(), $load_only = false, $cache = true ) { |
||
420 | $paths = array( |
||
421 | EE_LIBRARIES, |
||
422 | EE_LIBRARIES . 'messages' . DS, |
||
423 | EE_LIBRARIES . 'shortcodes' . DS, |
||
424 | EE_LIBRARIES . 'qtips' . DS, |
||
425 | EE_LIBRARIES . 'payment_methods' . DS, |
||
426 | ); |
||
427 | // retrieve instantiated class |
||
428 | return $this->_load( $paths, 'EE_', $class_name, 'lib', $arguments, false, $cache, $load_only ); |
||
429 | } |
||
430 | |||
431 | |||
432 | |||
433 | /** |
||
434 | * loads model classes - must be singletons |
||
435 | * |
||
436 | * @param string $class_name - simple class name ie: price |
||
437 | * @param mixed $arguments |
||
438 | * @param bool $load_only |
||
439 | * @return EEM_Base | bool |
||
440 | */ |
||
441 | public function load_model( $class_name, $arguments = array(), $load_only = false ) { |
||
442 | $paths = apply_filters( 'FHEE__EE_Registry__load_model__paths', array( |
||
443 | EE_MODELS, |
||
444 | EE_CORE |
||
445 | ) ); |
||
446 | // retrieve instantiated class |
||
447 | return $this->_load( $paths, 'EEM_', $class_name, 'model', $arguments, false, true, $load_only ); |
||
448 | } |
||
449 | |||
450 | |||
451 | |||
452 | /** |
||
453 | * loads model classes - must be singletons |
||
454 | * |
||
455 | * @param string $class_name - simple class name ie: price |
||
456 | * @param mixed $arguments |
||
457 | * @param bool $load_only |
||
458 | * @return mixed | bool |
||
459 | */ |
||
460 | public function load_model_class( $class_name, $arguments = array(), $load_only = true ) { |
||
461 | $paths = array( |
||
462 | EE_MODELS . 'fields' . DS, |
||
463 | EE_MODELS . 'helpers' . DS, |
||
464 | EE_MODELS . 'relations' . DS, |
||
465 | EE_MODELS . 'strategies' . DS |
||
466 | ); |
||
467 | // retrieve instantiated class |
||
468 | return $this->_load( $paths, 'EE_', $class_name, '', $arguments, false, true, $load_only ); |
||
469 | } |
||
470 | |||
471 | |||
472 | |||
473 | /** |
||
474 | * Determines if $model_name is the name of an actual EE model. |
||
475 | * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
||
476 | * @return boolean |
||
477 | */ |
||
478 | public function is_model_name( $model_name ) { |
||
479 | return isset( $this->models[ $model_name ] ) ? true : false; |
||
480 | } |
||
481 | |||
482 | |||
483 | |||
484 | /** |
||
485 | * generic class loader |
||
486 | * |
||
487 | * @param string $path_to_file - directory path to file location, not including filename |
||
488 | * @param string $file_name - file name ie: my_file.php, including extension |
||
489 | * @param string $type - file type - core? class? helper? model? |
||
490 | * @param mixed $arguments |
||
491 | * @param bool $load_only |
||
492 | * @return mixed |
||
493 | */ |
||
494 | public function load_file( $path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true ) { |
||
495 | // retrieve instantiated class |
||
496 | return $this->_load( $path_to_file, '', $file_name, $type, $arguments, false, true, $load_only ); |
||
497 | } |
||
498 | |||
499 | |||
500 | |||
501 | /** |
||
502 | * load_addon |
||
503 | * |
||
504 | * @param string $path_to_file - directory path to file location, not including filename |
||
505 | * @param string $class_name - full class name ie: My_Class |
||
506 | * @param string $type - file type - core? class? helper? model? |
||
507 | * @param mixed $arguments |
||
508 | * @param bool $load_only |
||
509 | * @return EE_Addon |
||
510 | */ |
||
511 | public function load_addon( $path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false ) { |
||
512 | // retrieve instantiated class |
||
513 | return $this->_load( $path_to_file, 'addon', $class_name, $type, $arguments, false, true, $load_only ); |
||
514 | } |
||
515 | |||
516 | |||
517 | |||
518 | /** |
||
519 | * loads and tracks classes |
||
520 | * |
||
521 | * @param array $file_paths |
||
522 | * @param string $class_prefix - EE or EEM or... ??? |
||
523 | * @param bool|string $class_name - $class name |
||
524 | * @param string $type - file type - core? class? helper? model? |
||
525 | * @param mixed $arguments - an argument or array of arguments to pass to the class upon instantiation |
||
526 | * @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
||
527 | * @param bool $cache |
||
528 | * @param bool $load_only |
||
529 | * @return null|object|bool null = failure to load or instantiate class object. |
||
530 | * object = class loaded and instantiated successfully. |
||
531 | * bool = fail or success when $load_only is true |
||
532 | */ |
||
533 | protected function _load( |
||
534 | $file_paths = array(), |
||
535 | $class_prefix = 'EE_', |
||
536 | $class_name = false, |
||
537 | $type = 'class', |
||
538 | $arguments = array(), |
||
539 | $from_db = false, |
||
540 | $cache = true, |
||
541 | $load_only = false |
||
542 | ) { |
||
543 | // strip php file extension |
||
544 | $class_name = str_replace( '.php', '', trim( $class_name ) ); |
||
545 | // does the class have a prefix ? |
||
546 | if ( ! empty( $class_prefix ) && $class_prefix != 'addon' ) { |
||
547 | // make sure $class_prefix is uppercase |
||
548 | $class_prefix = strtoupper( trim( $class_prefix ) ); |
||
549 | // add class prefix ONCE!!! |
||
550 | $class_name = $class_prefix . str_replace( $class_prefix, '', $class_name ); |
||
551 | } |
||
552 | $class_exists = class_exists( $class_name ); |
||
553 | // if we're only loading the class and it already exists, then let's just return true immediately |
||
554 | if ( $load_only && $class_exists ) { |
||
555 | return true; |
||
556 | } |
||
557 | // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
||
558 | // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
||
559 | // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
||
560 | if ( $this->_cache_on && $cache && ! $load_only ) { |
||
561 | // return object if it's already cached |
||
562 | $cached_class = $this->_get_cached_class( $class_name, $class_prefix ); |
||
563 | if ( $cached_class !== null ) { |
||
564 | return $cached_class; |
||
565 | } |
||
566 | } |
||
567 | // if the class doesn't already exist.. then we need to try and find the file and load it |
||
568 | if ( ! $class_exists ) { |
||
569 | // get full path to file |
||
570 | $path = $this->_resolve_path( $class_name, $type, $file_paths ); |
||
571 | // load the file |
||
572 | $loaded = $this->_require_file( $path, $class_name, $type, $file_paths ); |
||
573 | // if loading failed, or we are only loading a file but NOT instantiating an object |
||
574 | if ( ! $loaded || $load_only ) { |
||
575 | // return boolean if only loading, or null if an object was expected |
||
576 | return $load_only ? $loaded : null; |
||
577 | } |
||
578 | } |
||
579 | // instantiate the requested object |
||
580 | $class_obj = $this->_create_object( $class_name, $arguments, $type, $from_db ); |
||
581 | if ( $this->_cache_on && $cache ) { |
||
582 | // save it for later... kinda like gum { : $ |
||
583 | $this->_set_cached_class( $class_obj, $class_name, $class_prefix, $from_db ); |
||
584 | } |
||
585 | $this->_cache_on = true; |
||
586 | return $class_obj; |
||
587 | } |
||
588 | |||
589 | |||
590 | |||
591 | /** |
||
592 | * _get_cached_class |
||
593 | * |
||
594 | * attempts to find a cached version of the requested class |
||
595 | * by looking in the following places: |
||
596 | * $this->{$class_abbreviation} ie: $this->CART |
||
597 | * $this->{$class_name} ie: $this->Some_Class |
||
598 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
599 | * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
||
600 | * |
||
601 | * @access protected |
||
602 | * @param string $class_name |
||
603 | * @param string $class_prefix |
||
604 | * @return null|object |
||
605 | */ |
||
606 | protected function _get_cached_class( $class_name, $class_prefix = '' ) { |
||
607 | if ( isset( $this->_class_abbreviations[ $class_name ] ) ) { |
||
608 | $class_abbreviation = $this->_class_abbreviations[ $class_name ]; |
||
609 | } else { |
||
610 | // have to specify something, but not anything that will conflict |
||
611 | $class_abbreviation = 'FANCY_BATMAN_PANTS'; |
||
612 | } |
||
613 | // check if class has already been loaded, and return it if it has been |
||
614 | if ( isset( $this->{$class_abbreviation} ) && ! is_null( $this->{$class_abbreviation} ) ) { |
||
615 | return $this->{$class_abbreviation}; |
||
616 | } else if ( isset ( $this->{$class_name} ) ) { |
||
617 | return $this->{$class_name}; |
||
618 | } else if ( isset ( $this->LIB->{$class_name} ) ) { |
||
619 | return $this->LIB->{$class_name}; |
||
620 | } else if ( $class_prefix == 'addon' && isset ( $this->addons->{$class_name} ) ) { |
||
621 | return $this->addons->{$class_name}; |
||
622 | } |
||
623 | return null; |
||
624 | } |
||
625 | |||
626 | |||
627 | |||
628 | /** |
||
629 | * _resolve_path |
||
630 | * |
||
631 | * attempts to find a full valid filepath for the requested class. |
||
632 | * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
||
633 | * then returns that path if the target file has been found and is readable |
||
634 | * |
||
635 | * @access protected |
||
636 | * @param string $class_name |
||
637 | * @param string $type |
||
638 | * @param array $file_paths |
||
639 | * @return string | bool |
||
640 | */ |
||
641 | protected function _resolve_path( $class_name, $type = '', $file_paths = array() ) { |
||
642 | // make sure $file_paths is an array |
||
643 | $file_paths = is_array( $file_paths ) ? $file_paths : array( $file_paths ); |
||
644 | // cycle thru paths |
||
645 | foreach ( $file_paths as $key => $file_path ) { |
||
646 | // convert all separators to proper DS, if no filepath, then use EE_CLASSES |
||
647 | $file_path = $file_path ? str_replace( array( '/', '\\' ), DS, $file_path ) : EE_CLASSES; |
||
648 | // prep file type |
||
649 | $type = ! empty( $type ) ? trim( $type, '.' ) . '.' : ''; |
||
650 | // build full file path |
||
651 | $file_paths[ $key ] = rtrim( $file_path, DS ) . DS . $class_name . '.' . $type . 'php'; |
||
652 | //does the file exist and can be read ? |
||
653 | if ( is_readable( $file_paths[ $key ] ) ) { |
||
654 | return $file_paths[ $key ]; |
||
655 | } |
||
656 | } |
||
657 | return false; |
||
658 | } |
||
659 | |||
660 | |||
661 | |||
662 | /** |
||
663 | * _require_file |
||
664 | * |
||
665 | * basically just performs a require_once() |
||
666 | * but with some error handling |
||
667 | * |
||
668 | * @access protected |
||
669 | * @param string $path |
||
670 | * @param string $class_name |
||
671 | * @param string $type |
||
672 | * @param array $file_paths |
||
673 | * @return boolean |
||
674 | * @throws \EE_Error |
||
675 | */ |
||
676 | protected function _require_file( $path, $class_name, $type = '', $file_paths = array() ) { |
||
677 | // don't give up! you gotta... |
||
678 | try { |
||
679 | //does the file exist and can it be read ? |
||
680 | View Code Duplication | if ( ! $path ) { |
|
681 | // so sorry, can't find the file |
||
682 | throw new EE_Error ( |
||
683 | sprintf( |
||
684 | __( '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', 'event_espresso' ), |
||
685 | trim( $type, '.' ), |
||
686 | $class_name, |
||
687 | '<br />' . implode( ',<br />', $file_paths ) |
||
688 | ) |
||
689 | ); |
||
690 | } |
||
691 | // get the file |
||
692 | require_once( $path ); |
||
693 | // if the class isn't already declared somewhere |
||
694 | View Code Duplication | if ( class_exists( $class_name, false ) === false ) { |
|
695 | // so sorry, not a class |
||
696 | throw new EE_Error( |
||
697 | sprintf( |
||
698 | __( 'The %s file %s does not appear to contain the %s Class.', 'event_espresso' ), |
||
699 | $type, |
||
700 | $path, |
||
701 | $class_name |
||
702 | ) |
||
703 | ); |
||
704 | } |
||
705 | |||
706 | } catch ( EE_Error $e ) { |
||
707 | $e->get_error(); |
||
708 | return false; |
||
709 | } |
||
710 | return true; |
||
711 | } |
||
712 | |||
713 | |||
714 | |||
715 | /** |
||
716 | * _create_object |
||
717 | * Attempts to instantiate the requested class via any of the |
||
718 | * commonly used instantiation methods employed throughout EE. |
||
719 | * The priority for instantiation is as follows: |
||
720 | * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
||
721 | * - model objects via their 'new_instance_from_db' method |
||
722 | * - model objects via their 'new_instance' method |
||
723 | * - "singleton" classes" via their 'instance' method |
||
724 | * - standard instantiable classes via their __constructor |
||
725 | * Prior to instantiation, if the classname exists in the dependency_map, |
||
726 | * then the constructor for the requested class will be examined to determine |
||
727 | * if any dependencies exist, and if they can be injected. |
||
728 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
729 | * |
||
730 | * @access protected |
||
731 | * @param string $class_name |
||
732 | * @param array $arguments |
||
733 | * @param string $type |
||
734 | * @param bool $from_db |
||
735 | * @return null | object |
||
736 | * @throws \EE_Error |
||
737 | */ |
||
738 | protected function _create_object( $class_name, $arguments = array(), $type = '', $from_db = false ) { |
||
739 | $class_obj = null; |
||
740 | // don't give up! you gotta... |
||
741 | try { |
||
742 | // create reflection |
||
743 | $reflector = $this->get_ReflectionClass( $class_name ); |
||
744 | // make sure arguments are an array |
||
745 | $arguments = is_array( $arguments ) ? $arguments : array( $arguments ); |
||
746 | // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
||
747 | // else wrap it in an additional array so that it doesn't get split into multiple parameters |
||
748 | $arguments = $this->_array_is_numerically_and_sequentially_indexed( $arguments ) |
||
749 | ? $arguments |
||
750 | : array( $arguments ); |
||
751 | // attempt to inject dependencies ? |
||
752 | if ( $this->_dependency_map->has( $class_name ) ) { |
||
753 | $arguments = $this->_resolve_dependencies( $reflector, $class_name, $arguments ); |
||
754 | } |
||
755 | // instantiate the class and add to the LIB array for tracking |
||
756 | // EE_Base_Classes are instantiated via new_instance by default (models call them via new_instance_from_db) |
||
757 | if ( $reflector->getConstructor() === null || $reflector->isAbstract() ) { |
||
758 | // no constructor = static methods only... nothing to instantiate, loading file was enough |
||
759 | //$instantiation_mode = "no constructor"; |
||
760 | $class_obj = true; |
||
761 | } else if ( $from_db && method_exists( $class_name, 'new_instance_from_db' ) ) { |
||
762 | //$instantiation_mode = "new_instance_from_db"; |
||
763 | $class_obj = call_user_func_array( array( $class_name, 'new_instance_from_db' ), $arguments ); |
||
764 | } else if ( method_exists( $class_name, 'new_instance' ) ) { |
||
765 | //$instantiation_mode = "new_instance"; |
||
766 | $class_obj = call_user_func_array( array( $class_name, 'new_instance' ), $arguments ); |
||
767 | } else if ( method_exists( $class_name, 'instance' ) ) { |
||
768 | //$instantiation_mode = "instance"; |
||
769 | $class_obj = call_user_func_array( array( $class_name, 'instance' ), $arguments ); |
||
770 | } else if ( $reflector->isInstantiable() ) { |
||
771 | //$instantiation_mode = "isInstantiable"; |
||
772 | $class_obj = $reflector->newInstanceArgs( $arguments ); |
||
773 | } else { |
||
774 | // heh ? something's not right ! |
||
775 | //$instantiation_mode = 'none'; |
||
776 | throw new EE_Error( |
||
777 | sprintf( |
||
778 | __( 'The %s file %s could not be instantiated.', 'event_espresso' ), |
||
779 | $type, |
||
780 | $class_name |
||
781 | ) |
||
782 | ); |
||
783 | } |
||
784 | } catch ( Exception $e ) { |
||
785 | if ( ! $e instanceof EE_Error ) { |
||
786 | $e = new EE_Error( $e->getMessage() ); |
||
787 | } |
||
788 | $e->get_error(); |
||
789 | } |
||
790 | return $class_obj; |
||
791 | } |
||
792 | |||
793 | |||
794 | |||
795 | /** |
||
796 | * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
||
797 | * @param array $array |
||
798 | * @return bool |
||
799 | */ |
||
800 | protected function _array_is_numerically_and_sequentially_indexed( array $array ) { |
||
801 | return ! empty( $array ) ? array_keys( $array ) === range( 0, count( $array ) - 1 ) : true; |
||
802 | } |
||
803 | |||
804 | |||
805 | |||
806 | /** |
||
807 | * getReflectionClass |
||
808 | * |
||
809 | * checks if a ReflectionClass object has already been generated for a class |
||
810 | * and returns that instead of creating a new one |
||
811 | * |
||
812 | * @access public |
||
813 | * @param string $class_name |
||
814 | * @return ReflectionClass |
||
815 | */ |
||
816 | public function get_ReflectionClass( $class_name ) { |
||
817 | if ( |
||
818 | ! isset( $this->_reflectors[ $class_name ] ) |
||
819 | || ! $this->_reflectors[ $class_name ] instanceof ReflectionClass |
||
820 | ) { |
||
821 | $this->_reflectors[ $class_name ] = new ReflectionClass( $class_name ); |
||
822 | } |
||
823 | return $this->_reflectors[ $class_name ]; |
||
824 | } |
||
825 | |||
826 | |||
827 | |||
828 | /** |
||
829 | * _resolve_dependencies |
||
830 | * |
||
831 | * examines the constructor for the requested class to determine |
||
832 | * if any dependencies exist, and if they can be injected. |
||
833 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
834 | * PLZ NOTE: this is achieved by type hinting the constructor params |
||
835 | * For example: |
||
836 | * if attempting to load a class "Foo" with the following constructor: |
||
837 | * __construct( Bar $bar_class, Fighter $grohl_class ) |
||
838 | * then $bar_class and $grohl_class will be added to the $arguments array, |
||
839 | * but only IF they are NOT already present in the incoming arguments array, |
||
840 | * and the correct classes can be loaded |
||
841 | * |
||
842 | * @access protected |
||
843 | * @param ReflectionClass $reflector |
||
844 | * @param string $class_name |
||
845 | * @param array $arguments |
||
846 | * @return array |
||
847 | */ |
||
848 | protected function _resolve_dependencies( ReflectionClass $reflector, $class_name, $arguments = array() ) { |
||
849 | // let's examine the constructor |
||
850 | $constructor = $reflector->getConstructor(); |
||
851 | // whu? huh? nothing? |
||
852 | if ( ! $constructor ) { |
||
853 | return $arguments; |
||
854 | } |
||
855 | // get constructor parameters |
||
856 | $params = $constructor->getParameters(); |
||
857 | // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
||
858 | $argument_keys = array_keys( $arguments ); |
||
859 | // now loop thru all of the constructors expected parameters |
||
860 | foreach ( $params as $index => $param ) { |
||
861 | // is this a dependency for a specific class ? |
||
862 | $param_class = $param->getClass() ? $param->getClass()->name : null; |
||
863 | if ( |
||
864 | // param is not even a class |
||
865 | empty( $param_class ) |
||
866 | // and something already exists in the incoming arguments for this param |
||
867 | && isset( $argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ] ) |
||
868 | ) { |
||
869 | // so let's skip this argument and move on to the next |
||
870 | continue; |
||
871 | } else if ( |
||
872 | // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
||
873 | ! empty( $param_class ) |
||
874 | && isset( $argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ] ) |
||
875 | && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
||
876 | ) { |
||
877 | // skip this argument and move on to the next |
||
878 | continue; |
||
879 | } else if ( |
||
880 | // parameter is type hinted as a class, and should be injected |
||
881 | ! empty( $param_class ) |
||
882 | && $this->_dependency_map->has_dependency_for_class( $class_name, $param_class ) |
||
883 | ) { |
||
884 | $arguments = $this->_resolve_dependency( $class_name, $param_class, $arguments, $index ); |
||
885 | } else { |
||
886 | $arguments[ $index ] = $param->getDefaultValue(); |
||
887 | } |
||
888 | |||
889 | } |
||
890 | return $arguments; |
||
891 | } |
||
892 | |||
893 | |||
894 | |||
895 | /** |
||
896 | * @access protected |
||
897 | * @param string $class_name |
||
898 | * @param string $param_class |
||
899 | * @param array $arguments |
||
900 | * @param mixed $index |
||
901 | * @return array |
||
902 | */ |
||
903 | protected function _resolve_dependency( $class_name, $param_class , $arguments, $index ) { |
||
904 | $dependency = null; |
||
905 | // should dependency be loaded from cache ? |
||
906 | $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency( |
||
907 | $class_name, $param_class |
||
908 | ) !== EE_Dependency_Map::load_new_object |
||
909 | ? true |
||
910 | : false; |
||
911 | // we might have a dependency... |
||
912 | // let's MAYBE try and find it in our cache if that's what's been requested |
||
913 | $cached_class = $cache_on ? $this->_get_cached_class( $param_class ) : null; |
||
914 | // and grab it if it exists |
||
915 | if ( $cached_class instanceof $param_class ) { |
||
916 | $dependency = $cached_class; |
||
917 | } else if ( $param_class != $class_name ) { |
||
918 | // obtain the loader method from the dependency map |
||
919 | $loader = $this->_dependency_map->class_loader( $param_class ); |
||
920 | // is loader a custom closure ? |
||
921 | if ( $loader instanceof Closure ) { |
||
922 | $dependency = $loader(); |
||
923 | } else { |
||
924 | // set the cache on property for the recursive loading call |
||
925 | $this->_cache_on = $cache_on; |
||
926 | // if not, then let's try and load it via the registry |
||
927 | $dependency = $this->{$loader}( $param_class ); |
||
928 | } |
||
929 | } |
||
930 | // did we successfully find the correct dependency ? |
||
931 | if ( $dependency instanceof $param_class ) { |
||
932 | // then let's inject it into the incoming array of arguments at the correct location |
||
933 | if ( isset( $argument_keys[ $index ] ) ) { |
||
934 | $arguments[ $argument_keys[ $index ] ] = $dependency; |
||
935 | } else { |
||
936 | $arguments[ $index ] = $dependency; |
||
937 | } |
||
938 | } |
||
939 | return $arguments; |
||
940 | } |
||
941 | |||
942 | |||
943 | |||
944 | /** |
||
945 | * _set_cached_class |
||
946 | * |
||
947 | * attempts to cache the instantiated class locally |
||
948 | * in one of the following places, in the following order: |
||
949 | * $this->{class_abbreviation} ie: $this->CART |
||
950 | * $this->{$class_name} ie: $this->Some_Class |
||
951 | * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
||
952 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
953 | * |
||
954 | * @access protected |
||
955 | * @param object $class_obj |
||
956 | * @param string $class_name |
||
957 | * @param string $class_prefix |
||
958 | * @param bool $from_db |
||
959 | * @return void |
||
960 | */ |
||
961 | protected function _set_cached_class( $class_obj, $class_name, $class_prefix = '', $from_db = false ) { |
||
962 | // return newly instantiated class |
||
963 | if ( isset( $this->_class_abbreviations[ $class_name ] ) ) { |
||
964 | $class_abbreviation = $this->_class_abbreviations[ $class_name ]; |
||
965 | $this->{$class_abbreviation} = $class_obj; |
||
966 | } else if ( property_exists( $this, $class_name ) ) { |
||
967 | $this->{$class_name} = $class_obj; |
||
968 | } else if ( $class_prefix == 'addon' ) { |
||
969 | $this->addons->{$class_name} = $class_obj; |
||
970 | } else if ( ! $from_db ) { |
||
971 | $this->LIB->{$class_name} = $class_obj; |
||
972 | } |
||
973 | } |
||
974 | |||
975 | |||
976 | |||
977 | /** |
||
978 | * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
||
979 | * |
||
980 | * |
||
981 | * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
||
982 | * in the EE_Dependency_Map::$_class_loaders array, |
||
983 | * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
||
984 | * @param array $arguments |
||
985 | * @return object |
||
986 | */ |
||
987 | public static function factory( $classname, $arguments = array() ) { |
||
988 | $loader = self::instance()->_dependency_map->class_loader( $classname ); |
||
989 | if ( $loader instanceof Closure ) { |
||
990 | return $loader( $arguments ); |
||
991 | } else if ( method_exists( EE_Registry::instance(), $loader ) ) { |
||
992 | return EE_Registry::instance()->{$loader}( $classname, $arguments ); |
||
993 | } |
||
994 | return null; |
||
995 | } |
||
996 | |||
997 | |||
998 | |||
999 | /** |
||
1000 | * Gets the addon by its name/slug (not classname. For that, just |
||
1001 | * use the classname as the property name on EE_Config::instance()->addons) |
||
1002 | * @param string $name |
||
1003 | * @return EE_Addon |
||
1004 | */ |
||
1005 | public function get_addon_by_name( $name ) { |
||
1006 | foreach ( $this->addons as $addon ) { |
||
1007 | if ( $addon->name() == $name ) { |
||
1008 | return $addon; |
||
1009 | } |
||
1010 | } |
||
1011 | return null; |
||
1012 | } |
||
1013 | |||
1014 | |||
1015 | |||
1016 | /** |
||
1017 | * Gets an array of all the registered addons, where the keys are their names. (ie, what each returns for their name() function) They're already available on EE_Config::instance()->addons as properties, where each property's name is the addon's classname. So if you just want to get the addon by classname, use EE_Config::instance()->addons->{classname} |
||
1018 | * |
||
1019 | * @return EE_Addon[] where the KEYS are the addon's name() |
||
1020 | */ |
||
1021 | public function get_addons_by_name() { |
||
1022 | $addons = array(); |
||
1023 | foreach ( $this->addons as $addon ) { |
||
1024 | $addons[ $addon->name() ] = $addon; |
||
1025 | } |
||
1026 | return $addons; |
||
1027 | } |
||
1028 | |||
1029 | |||
1030 | |||
1031 | /** |
||
1032 | * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
||
1033 | * a stale copy of it around |
||
1034 | * |
||
1035 | * @param string $model_name |
||
1036 | * @return \EEM_Base |
||
1037 | * @throws \EE_Error |
||
1038 | */ |
||
1039 | public function reset_model( $model_name ) { |
||
1040 | $model = $this->load_model( $model_name ); |
||
1041 | $model_class_name = get_class( $model ); |
||
1042 | //get that model reset it and make sure we nuke the old reference to it |
||
1043 | if ( $model instanceof $model_class_name && is_callable( array( $model_class_name, 'reset' ))) { |
||
1044 | $this->LIB->{$model_class_name} = $model::reset(); |
||
1045 | } else { |
||
1046 | throw new EE_Error( sprintf( __( 'Model %s does not have a method "reset"', 'event_espresso' ), $model_name ) ); |
||
1047 | } |
||
1048 | return $this->LIB->{$model_class_name}; |
||
1049 | } |
||
1050 | |||
1051 | |||
1052 | |||
1053 | /** |
||
1054 | * Resets the registry and everything in it (eventually, getting it to properly |
||
1055 | * reset absolutely everything will probably be tricky. right now it just resets |
||
1056 | * the config, data migration manager, and the models) |
||
1057 | * @param boolean $hard whether to reset data in the database too, or just refresh |
||
1058 | * the Registry to its state at the beginning of the request |
||
1059 | * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
||
1060 | * or just reset without re-instantiating (handy to set to FALSE if you're not sure if you CAN |
||
1061 | * currently reinstantiate the singletons at the moment) |
||
1062 | * @return EE_Registry |
||
1063 | */ |
||
1064 | public static function reset( $hard = false, $reinstantiate = true ) { |
||
1065 | $instance = self::instance(); |
||
1066 | EEH_Activation::reset(); |
||
1067 | $instance->_cache_on = true; |
||
1068 | $instance->CFG = EE_Config::reset( $hard, $reinstantiate ); |
||
1069 | $instance->LIB->EE_Data_Migration_Manager = EE_Data_Migration_Manager::reset(); |
||
1070 | $instance->LIB = new stdClass(); |
||
1071 | foreach ( array_keys( $instance->non_abstract_db_models ) as $model_name ) { |
||
1072 | $instance->reset_model( $model_name ); |
||
1073 | } |
||
1074 | return $instance; |
||
1075 | } |
||
1076 | |||
1077 | |||
1078 | |||
1079 | /** |
||
1080 | * @override magic methods |
||
1081 | * @return void |
||
1082 | */ |
||
1083 | final function __destruct() { |
||
1085 | |||
1086 | |||
1087 | |||
1088 | /** |
||
1089 | * @param $a |
||
1090 | * @param $b |
||
1091 | */ |
||
1092 | final function __call( $a, $b ) { |
||
1094 | |||
1095 | |||
1096 | |||
1097 | /** |
||
1098 | * @param $a |
||
1099 | */ |
||
1100 | final function __get( $a ) { |
||
1102 | |||
1103 | |||
1104 | |||
1105 | /** |
||
1106 | * @param $a |
||
1107 | * @param $b |
||
1108 | */ |
||
1109 | final function __set( $a, $b ) { |
||
1111 | |||
1112 | |||
1113 | |||
1114 | /** |
||
1115 | * @param $a |
||
1116 | */ |
||
1117 | final function __isset( $a ) { |
||
1119 | |||
1120 | |||
1121 | |||
1122 | /** |
||
1123 | * @param $a |
||
1124 | */ |
||
1125 | final function __unset( $a ) { |
||
1127 | |||
1128 | |||
1129 | |||
1130 | /** |
||
1131 | * @return array |
||
1132 | */ |
||
1133 | final function __sleep() { |
||
1136 | |||
1137 | |||
1138 | |||
1139 | final function __wakeup() { |
||
1141 | |||
1142 | |||
1143 | |||
1144 | /** |
||
1145 | * @return string |
||
1146 | */ |
||
1147 | final function __toString() { |
||
1150 | |||
1151 | |||
1152 | |||
1153 | final function __invoke() { |
||
1155 | |||
1156 | |||
1157 | |||
1158 | final function __set_state() { |
||
1160 | |||
1161 | |||
1162 | |||
1163 | final function __clone() { |
||
1165 | |||
1166 | |||
1167 | |||
1168 | /** |
||
1169 | * @param $a |
||
1170 | * @param $b |
||
1171 | */ |
||
1172 | final static function __callStatic( $a, $b ) { |
||
1174 | |||
1175 | /** |
||
1176 | * Gets all the custom post type models defined |
||
1177 | * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
||
1178 | */ |
||
1179 | public function cpt_models() { |
||
1188 | |||
1189 | |||
1190 | |||
1191 | public static function CFG() { |
||
1192 | return self::instance()->CFG; |
||
1193 | } |
||
1194 | |||
1195 | |||
1196 | } |
||
1197 | // End of file EE_Registry.core.php |
||
1198 | // Location: ./core/EE_Registry.core.php |
||
1199 |
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):