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 | * @access public |
||
35 | * @var \EventEspresso\core\services\commands\CommandBusInterface $BUS |
||
36 | */ |
||
37 | public $BUS; |
||
38 | |||
39 | /** |
||
40 | * EE_Cart Object |
||
41 | * @access public |
||
42 | * @var EE_Cart $CART |
||
43 | */ |
||
44 | public $CART = null; |
||
45 | |||
46 | /** |
||
47 | * EE_Config Object |
||
48 | * @access public |
||
49 | * @var EE_Config $CFG |
||
50 | */ |
||
51 | public $CFG = null; |
||
52 | |||
53 | /** |
||
54 | * EE_Network_Config Object |
||
55 | * @access public |
||
56 | * @var EE_Network_Config $NET_CFG |
||
57 | */ |
||
58 | public $NET_CFG = null; |
||
59 | |||
60 | /** |
||
61 | * StdClass object for storing library classes in |
||
62 | * |
||
63 | * @public LIB |
||
64 | * @var StdClass $LIB |
||
65 | */ |
||
66 | public $LIB = null; |
||
67 | |||
68 | /** |
||
69 | * EE_Request_Handler Object |
||
70 | * @access public |
||
71 | * @var EE_Request_Handler $REQ |
||
72 | */ |
||
73 | public $REQ = null; |
||
74 | |||
75 | /** |
||
76 | * EE_Session Object |
||
77 | * @access public |
||
78 | * @var EE_Session $SSN |
||
79 | */ |
||
80 | public $SSN = null; |
||
81 | |||
82 | /** |
||
83 | * holds the ee capabilities object. |
||
84 | * |
||
85 | * @since 4.5.0 |
||
86 | * |
||
87 | * @var EE_Capabilities |
||
88 | */ |
||
89 | public $CAP = null; |
||
90 | |||
91 | /** |
||
92 | * holds the EE_Message_Resource_Manager object. |
||
93 | * |
||
94 | * @since 4.9.0 |
||
95 | * |
||
96 | * @var EE_Message_Resource_Manager |
||
97 | */ |
||
98 | public $MRM = null; |
||
99 | |||
100 | /** |
||
101 | * $addons - StdClass object for holding addons which have registered themselves to work with EE core |
||
102 | * @access public |
||
103 | * @var EE_Addon[] |
||
104 | */ |
||
105 | public $addons = null; |
||
106 | |||
107 | /** |
||
108 | * $models |
||
109 | * @access public |
||
110 | * @var EEM_Base[] $models keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
||
111 | */ |
||
112 | public $models = array(); |
||
113 | |||
114 | /** |
||
115 | * $modules |
||
116 | * @access public |
||
117 | * @var EED_Module[] $modules |
||
118 | */ |
||
119 | public $modules = null; |
||
120 | |||
121 | /** |
||
122 | * $shortcodes |
||
123 | * @access public |
||
124 | * @var EES_Shortcode[] $shortcodes |
||
125 | */ |
||
126 | public $shortcodes = null; |
||
127 | |||
128 | /** |
||
129 | * $widgets |
||
130 | * @access public |
||
131 | * @var WP_Widget[] $widgets |
||
132 | */ |
||
133 | public $widgets = null; |
||
134 | |||
135 | /** |
||
136 | * $non_abstract_db_models |
||
137 | * @access public |
||
138 | * @var array this is an array of all implemented model names (i.e. not the parent abstract models, or models |
||
139 | * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
||
140 | * Keys are model "short names" (eg "Event") as used in model relations, and values are |
||
141 | * classnames (eg "EEM_Event") |
||
142 | */ |
||
143 | public $non_abstract_db_models = array(); |
||
144 | |||
145 | /** |
||
146 | * $i18n_js_strings - internationalization for JS strings |
||
147 | * usage: EE_Registry::i18n_js_strings['string_key'] = __( 'string to translate.', 'event_espresso' ); |
||
148 | * in js file: var translatedString = eei18n.string_key; |
||
149 | * |
||
150 | * @access public |
||
151 | * @var array |
||
152 | */ |
||
153 | public static $i18n_js_strings = array(); |
||
154 | |||
155 | /** |
||
156 | * $main_file - path to espresso.php |
||
157 | * |
||
158 | * @access public |
||
159 | * @var array |
||
160 | */ |
||
161 | public $main_file; |
||
162 | |||
163 | /** |
||
164 | * array of ReflectionClass objects where the key is the class name |
||
165 | * |
||
166 | * @access public |
||
167 | * @var ReflectionClass[] |
||
168 | */ |
||
169 | public $_reflectors; |
||
170 | |||
171 | /** |
||
172 | * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
||
173 | * |
||
174 | * @access protected |
||
175 | * @var boolean $_cache_on |
||
176 | */ |
||
177 | protected $_cache_on = true; |
||
178 | |||
179 | |||
180 | |||
181 | /** |
||
182 | * @singleton method used to instantiate class object |
||
183 | * @access public |
||
184 | * @param \EE_Dependency_Map $dependency_map |
||
185 | * @return \EE_Registry instance |
||
186 | */ |
||
187 | public static function instance( \EE_Dependency_Map $dependency_map = null ) { |
||
194 | |||
195 | |||
196 | |||
197 | /** |
||
198 | *protected constructor to prevent direct creation |
||
199 | * |
||
200 | * @Constructor |
||
201 | * @access protected |
||
202 | * @param \EE_Dependency_Map $dependency_map |
||
203 | * @return \EE_Registry |
||
204 | */ |
||
205 | protected function __construct( \EE_Dependency_Map $dependency_map ) { |
||
209 | |||
210 | |||
211 | |||
212 | /** |
||
213 | * initialize |
||
214 | */ |
||
215 | public function initialize() { |
||
216 | $this->_class_abbreviations = apply_filters( |
||
217 | 'FHEE__EE_Registry____construct___class_abbreviations', |
||
218 | array( |
||
219 | 'EE_Config' => 'CFG', |
||
220 | 'EE_Session' => 'SSN', |
||
221 | 'EE_Capabilities' => 'CAP', |
||
222 | 'EE_Cart' => 'CART', |
||
223 | 'EE_Network_Config' => 'NET_CFG', |
||
224 | 'EE_Request_Handler' => 'REQ', |
||
225 | 'EE_Message_Resource_Manager' => 'MRM', |
||
226 | 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
||
227 | ) |
||
228 | ); |
||
229 | // class library |
||
230 | $this->LIB = new stdClass(); |
||
231 | $this->addons = new stdClass(); |
||
232 | $this->modules = new stdClass(); |
||
233 | $this->shortcodes = new stdClass(); |
||
234 | $this->widgets = new stdClass(); |
||
235 | $this->load_core( 'Base', array(), true ); |
||
236 | // add our request and response objects to the cache |
||
237 | $request_loader = $this->_dependency_map->class_loader( 'EE_Request' ); |
||
238 | $this->_set_cached_class( |
||
239 | $request_loader(), |
||
240 | 'EE_Request' |
||
241 | ); |
||
242 | $response_loader = $this->_dependency_map->class_loader( 'EE_Response' ); |
||
243 | $this->_set_cached_class( |
||
244 | $response_loader(), |
||
245 | 'EE_Response' |
||
246 | ); |
||
247 | add_action( 'AHEE__EE_System__set_hooks_for_core', array( $this, 'init' ) ); |
||
248 | } |
||
249 | |||
250 | |||
251 | |||
252 | /** |
||
253 | * init |
||
254 | * |
||
255 | * @access public |
||
256 | * @return void |
||
257 | */ |
||
258 | public function init() { |
||
265 | |||
266 | |||
267 | |||
268 | /** |
||
269 | * localize_i18n_js_strings |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | public static function localize_i18n_js_strings() { |
||
283 | |||
284 | |||
285 | |||
286 | /** |
||
287 | * @param mixed string | EED_Module $module |
||
288 | */ |
||
289 | public function add_module( $module ) { |
||
300 | |||
301 | |||
302 | |||
303 | /** |
||
304 | * @param string $module_name |
||
305 | * @return mixed EED_Module | NULL |
||
306 | */ |
||
307 | public function get_module( $module_name = '' ) { |
||
310 | |||
311 | |||
312 | |||
313 | /** |
||
314 | * loads core classes - must be singletons |
||
315 | * |
||
316 | * @access public |
||
317 | * @param string $class_name - simple class name ie: session |
||
318 | * @param mixed $arguments |
||
319 | * @param bool $load_only |
||
320 | * @return mixed |
||
321 | */ |
||
322 | public function load_core( $class_name, $arguments = array(), $load_only = false ) { |
||
337 | |||
338 | |||
339 | |||
340 | /** |
||
341 | * loads service classes |
||
342 | * |
||
343 | * @access public |
||
344 | * @param string $class_name - simple class name ie: session |
||
345 | * @param mixed $arguments |
||
346 | * @param bool $load_only |
||
347 | * @return mixed |
||
348 | */ |
||
349 | public function load_service( $class_name, $arguments = array(), $load_only = false ) { |
||
359 | |||
360 | |||
361 | |||
362 | /** |
||
363 | * loads data_migration_scripts |
||
364 | * |
||
365 | * @access public |
||
366 | * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
||
367 | * @param mixed $arguments |
||
368 | * @return EE_Data_Migration_Script_Base |
||
369 | */ |
||
370 | public function load_dms( $class_name, $arguments = array() ) { |
||
374 | |||
375 | |||
376 | |||
377 | /** |
||
378 | * loads object creating classes - must be singletons |
||
379 | * |
||
380 | * @param string $class_name - simple class name ie: attendee |
||
381 | * @param mixed $arguments - an array of arguments to pass to the class |
||
382 | * @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
||
383 | * @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) |
||
384 | * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate (default) |
||
385 | * @return EE_Base_Class | bool |
||
386 | */ |
||
387 | public function load_class( $class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false ) { |
||
396 | |||
397 | |||
398 | |||
399 | /** |
||
400 | * loads helper classes - must be singletons |
||
401 | * |
||
402 | * @param string $class_name - simple class name ie: price |
||
403 | * @param mixed $arguments |
||
404 | * @param bool $load_only |
||
405 | * @return EEH_Base | bool |
||
406 | */ |
||
407 | public function load_helper( $class_name, $arguments = array(), $load_only = true ) { |
||
413 | |||
414 | |||
415 | |||
416 | /** |
||
417 | * loads core classes - must be singletons |
||
418 | * |
||
419 | * @access public |
||
420 | * @param string $class_name - simple class name ie: session |
||
421 | * @param mixed $arguments |
||
422 | * @param bool $load_only |
||
423 | * @param bool $cache whether to cache the object or not. |
||
424 | * @return mixed |
||
425 | */ |
||
426 | public function load_lib( $class_name, $arguments = array(), $load_only = false, $cache = true ) { |
||
437 | |||
438 | |||
439 | |||
440 | /** |
||
441 | * loads model classes - must be singletons |
||
442 | * |
||
443 | * @param string $class_name - simple class name ie: price |
||
444 | * @param mixed $arguments |
||
445 | * @param bool $load_only |
||
446 | * @return EEM_Base | bool |
||
447 | */ |
||
448 | public function load_model( $class_name, $arguments = array(), $load_only = false ) { |
||
456 | |||
457 | |||
458 | |||
459 | /** |
||
460 | * loads model classes - must be singletons |
||
461 | * |
||
462 | * @param string $class_name - simple class name ie: price |
||
463 | * @param mixed $arguments |
||
464 | * @param bool $load_only |
||
465 | * @return mixed | bool |
||
466 | */ |
||
467 | public function load_model_class( $class_name, $arguments = array(), $load_only = true ) { |
||
477 | |||
478 | |||
479 | |||
480 | /** |
||
481 | * Determines if $model_name is the name of an actual EE model. |
||
482 | * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
||
483 | * @return boolean |
||
484 | */ |
||
485 | public function is_model_name( $model_name ) { |
||
488 | |||
489 | |||
490 | |||
491 | /** |
||
492 | * generic class loader |
||
493 | * |
||
494 | * @param string $path_to_file - directory path to file location, not including filename |
||
495 | * @param string $file_name - file name ie: my_file.php, including extension |
||
496 | * @param string $type - file type - core? class? helper? model? |
||
497 | * @param mixed $arguments |
||
498 | * @param bool $load_only |
||
499 | * @return mixed |
||
500 | */ |
||
501 | public function load_file( $path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true ) { |
||
505 | |||
506 | |||
507 | |||
508 | /** |
||
509 | * load_addon |
||
510 | * |
||
511 | * @param string $path_to_file - directory path to file location, not including filename |
||
512 | * @param string $class_name - full class name ie: My_Class |
||
513 | * @param string $type - file type - core? class? helper? model? |
||
514 | * @param mixed $arguments |
||
515 | * @param bool $load_only |
||
516 | * @return EE_Addon |
||
517 | */ |
||
518 | public function load_addon( $path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false ) { |
||
522 | |||
523 | |||
524 | |||
525 | /** |
||
526 | * instantiates, caches, and automatically resolves dependencies |
||
527 | * for classes that use a Fully Qualified Class Name. |
||
528 | * if the class is not capable of being loaded using PSR-4 autoloading, |
||
529 | * then you need to use one of the existing load_*() methods |
||
530 | * which can resolve the classname and filepath from the passed arguments |
||
531 | * |
||
532 | * @param bool|string $class_name Fully Qualified Class Name |
||
533 | * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
||
534 | * @param bool $cache whether to cache the instantiated object for reuse |
||
535 | * @param bool $from_db some classes are instantiated from the db |
||
536 | * and thus call a different method to instantiate |
||
537 | * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
||
538 | * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
||
539 | * @return mixed null = failure to load or instantiate class object. |
||
540 | * object = class loaded and instantiated successfully. |
||
541 | * bool = fail or success when $load_only is true |
||
542 | */ |
||
543 | public function create( |
||
544 | $class_name = false, |
||
545 | $arguments = array(), |
||
546 | $cache = false, |
||
547 | $from_db = false, |
||
548 | $load_only = false, |
||
549 | $addon = false |
||
550 | ) { |
||
551 | $class_name = $this->_dependency_map->get_alias( $class_name ); |
||
552 | if ( ! class_exists( $class_name ) ) { |
||
553 | // maybe the class is registered with a preceding \ |
||
554 | $class_name = strpos( $class_name, '\\' ) !== 0 ? '\\' . $class_name : $class_name; |
||
555 | // still doesn't exist ? |
||
556 | if ( ! class_exists( $class_name ) ) { |
||
557 | return null; |
||
558 | } |
||
559 | } |
||
560 | // if we're only loading the class and it already exists, then let's just return true immediately |
||
561 | if ( $load_only ) { |
||
562 | return true; |
||
563 | } |
||
564 | $addon = $addon ? 'addon' : ''; |
||
565 | // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
||
566 | // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
||
567 | // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
||
568 | View Code Duplication | if ( $this->_cache_on && $cache && ! $load_only ) { |
|
569 | // return object if it's already cached |
||
570 | $cached_class = $this->_get_cached_class( $class_name, $addon ); |
||
571 | if ( $cached_class !== null ) { |
||
572 | return $cached_class; |
||
573 | } |
||
574 | } |
||
575 | // instantiate the requested object |
||
576 | $class_obj = $this->_create_object( $class_name, $arguments, $addon, $from_db ); |
||
577 | if ( $this->_cache_on && $cache ) { |
||
578 | // save it for later... kinda like gum { : $ |
||
579 | $this->_set_cached_class( $class_obj, $class_name, $addon, $from_db ); |
||
580 | } |
||
581 | $this->_cache_on = true; |
||
582 | return $class_obj; |
||
583 | } |
||
584 | |||
585 | |||
586 | |||
587 | /** |
||
588 | * instantiates, caches, and injects dependencies for classes |
||
589 | * |
||
590 | * @param array $file_paths an array of paths to folders to look in |
||
591 | * @param string $class_prefix EE or EEM or... ??? |
||
592 | * @param bool|string $class_name $class name |
||
593 | * @param string $type file type - core? class? helper? model? |
||
594 | * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
||
595 | * @param bool $from_db some classes are instantiated from the db |
||
596 | * and thus call a different method to instantiate |
||
597 | * @param bool $cache whether to cache the instantiated object for reuse |
||
598 | * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
||
599 | * @return null|object|bool null = failure to load or instantiate class object. |
||
600 | * object = class loaded and instantiated successfully. |
||
601 | * bool = fail or success when $load_only is true |
||
602 | */ |
||
603 | protected function _load( |
||
659 | |||
660 | |||
661 | |||
662 | /** |
||
663 | * _get_cached_class |
||
664 | * |
||
665 | * attempts to find a cached version of the requested class |
||
666 | * by looking in the following places: |
||
667 | * $this->{$class_abbreviation} ie: $this->CART |
||
668 | * $this->{$class_name} ie: $this->Some_Class |
||
669 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
670 | * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
||
671 | * |
||
672 | * @access protected |
||
673 | * @param string $class_name |
||
674 | * @param string $class_prefix |
||
675 | * @return null|object |
||
676 | */ |
||
677 | protected function _get_cached_class( $class_name, $class_prefix = '' ) { |
||
696 | |||
697 | |||
698 | |||
699 | /** |
||
700 | * _resolve_path |
||
701 | * |
||
702 | * attempts to find a full valid filepath for the requested class. |
||
703 | * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
||
704 | * then returns that path if the target file has been found and is readable |
||
705 | * |
||
706 | * @access protected |
||
707 | * @param string $class_name |
||
708 | * @param string $type |
||
709 | * @param array $file_paths |
||
710 | * @return string | bool |
||
711 | */ |
||
712 | protected function _resolve_path( $class_name, $type = '', $file_paths = array() ) { |
||
730 | |||
731 | |||
732 | |||
733 | /** |
||
734 | * _require_file |
||
735 | * |
||
736 | * basically just performs a require_once() |
||
737 | * but with some error handling |
||
738 | * |
||
739 | * @access protected |
||
740 | * @param string $path |
||
741 | * @param string $class_name |
||
742 | * @param string $type |
||
743 | * @param array $file_paths |
||
744 | * @return boolean |
||
745 | * @throws \EE_Error |
||
746 | */ |
||
747 | protected function _require_file( $path, $class_name, $type = '', $file_paths = array() ) { |
||
783 | |||
784 | |||
785 | |||
786 | /** |
||
787 | * _create_object |
||
788 | * Attempts to instantiate the requested class via any of the |
||
789 | * commonly used instantiation methods employed throughout EE. |
||
790 | * The priority for instantiation is as follows: |
||
791 | * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
||
792 | * - model objects via their 'new_instance_from_db' method |
||
793 | * - model objects via their 'new_instance' method |
||
794 | * - "singleton" classes" via their 'instance' method |
||
795 | * - standard instantiable classes via their __constructor |
||
796 | * Prior to instantiation, if the classname exists in the dependency_map, |
||
797 | * then the constructor for the requested class will be examined to determine |
||
798 | * if any dependencies exist, and if they can be injected. |
||
799 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
800 | * |
||
801 | * @access protected |
||
802 | * @param string $class_name |
||
803 | * @param array $arguments |
||
804 | * @param string $type |
||
805 | * @param bool $from_db |
||
806 | * @return null | object |
||
807 | * @throws \EE_Error |
||
808 | */ |
||
809 | protected function _create_object( $class_name, $arguments = array(), $type = '', $from_db = false ) { |
||
810 | $class_obj = null; |
||
811 | $instantiation_mode = '0) none'; |
||
812 | // don't give up! you gotta... |
||
813 | try { |
||
814 | // create reflection |
||
815 | $reflector = $this->get_ReflectionClass( $class_name ); |
||
816 | // make sure arguments are an array |
||
817 | $arguments = is_array( $arguments ) ? $arguments : array( $arguments ); |
||
818 | // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
||
819 | // else wrap it in an additional array so that it doesn't get split into multiple parameters |
||
820 | $arguments = $this->_array_is_numerically_and_sequentially_indexed( $arguments ) |
||
821 | ? $arguments |
||
822 | : array( $arguments ); |
||
823 | // attempt to inject dependencies ? |
||
824 | if ( $this->_dependency_map->has( $class_name ) ) { |
||
825 | $arguments = $this->_resolve_dependencies( $reflector, $class_name, $arguments ); |
||
826 | } |
||
827 | // instantiate the class if possible |
||
828 | if ( $reflector->isAbstract() ) { |
||
829 | // nothing to instantiate, loading file was enough |
||
830 | // does not throw an exception so $instantiation_mode is unused |
||
831 | // $instantiation_mode = "1) no constructor abstract class"; |
||
832 | $class_obj = true; |
||
833 | } else if ( $reflector->getConstructor() === null && $reflector->isInstantiable() && empty( $arguments ) ) { |
||
834 | // no constructor = static methods only... nothing to instantiate, loading file was enough |
||
835 | $instantiation_mode = "2) no constructor but instantiable"; |
||
836 | $class_obj = $reflector->newInstance(); |
||
837 | } else if ( $from_db && method_exists( $class_name, 'new_instance_from_db' ) ) { |
||
838 | $instantiation_mode = "3) new_instance_from_db()"; |
||
839 | $class_obj = call_user_func_array( array( $class_name, 'new_instance_from_db' ), $arguments ); |
||
840 | } else if ( method_exists( $class_name, 'new_instance' ) ) { |
||
841 | $instantiation_mode = "4) new_instance()"; |
||
842 | $class_obj = call_user_func_array( array( $class_name, 'new_instance' ), $arguments ); |
||
843 | } else if ( method_exists( $class_name, 'instance' ) ) { |
||
844 | $instantiation_mode = "5) instance()"; |
||
845 | $class_obj = call_user_func_array( array( $class_name, 'instance' ), $arguments ); |
||
846 | } else if ( $reflector->isInstantiable() ) { |
||
847 | $instantiation_mode = "6) constructor"; |
||
848 | $class_obj = $reflector->newInstanceArgs( $arguments ); |
||
849 | } else { |
||
850 | // heh ? something's not right ! |
||
851 | throw new EE_Error( |
||
852 | sprintf( |
||
853 | __( 'The %s file %s could not be instantiated.', 'event_espresso' ), |
||
854 | $type, |
||
855 | $class_name |
||
856 | ) |
||
857 | ); |
||
858 | } |
||
859 | } catch ( Exception $e ) { |
||
860 | if ( ! $e instanceof EE_Error ) { |
||
861 | $e = new EE_Error( |
||
862 | sprintf( |
||
863 | __( 'The following error occurred while attempting to instantiate "%1$s": %2$s %3$s %2$s instantiation mode : %4$s', 'event_espresso' ), |
||
864 | $class_name, |
||
865 | '<br />', |
||
866 | $e->getMessage(), |
||
867 | $instantiation_mode |
||
868 | ) |
||
869 | ); |
||
870 | } |
||
871 | $e->get_error(); |
||
872 | } |
||
873 | return $class_obj; |
||
874 | } |
||
875 | |||
876 | |||
877 | |||
878 | /** |
||
879 | * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
||
880 | * @param array $array |
||
881 | * @return bool |
||
882 | */ |
||
883 | protected function _array_is_numerically_and_sequentially_indexed( array $array ) { |
||
886 | |||
887 | |||
888 | |||
889 | /** |
||
890 | * getReflectionClass |
||
891 | * |
||
892 | * checks if a ReflectionClass object has already been generated for a class |
||
893 | * and returns that instead of creating a new one |
||
894 | * |
||
895 | * @access public |
||
896 | * @param string $class_name |
||
897 | * @return ReflectionClass |
||
898 | */ |
||
899 | View Code Duplication | public function get_ReflectionClass( $class_name ) { |
|
900 | if ( |
||
901 | ! isset( $this->_reflectors[ $class_name ] ) |
||
902 | || ! $this->_reflectors[ $class_name ] instanceof ReflectionClass |
||
903 | ) { |
||
904 | $this->_reflectors[ $class_name ] = new ReflectionClass( $class_name ); |
||
905 | } |
||
906 | return $this->_reflectors[ $class_name ]; |
||
907 | } |
||
908 | |||
909 | |||
910 | |||
911 | /** |
||
912 | * _resolve_dependencies |
||
913 | * examines the constructor for the requested class to determine |
||
914 | * if any dependencies exist, and if they can be injected. |
||
915 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
916 | * PLZ NOTE: this is achieved by type hinting the constructor params |
||
917 | * For example: |
||
918 | * if attempting to load a class "Foo" with the following constructor: |
||
919 | * __construct( Bar $bar_class, Fighter $grohl_class ) |
||
920 | * then $bar_class and $grohl_class will be added to the $arguments array, |
||
921 | * but only IF they are NOT already present in the incoming arguments array, |
||
922 | * and the correct classes can be loaded |
||
923 | * |
||
924 | * @access protected |
||
925 | * @param ReflectionClass $reflector |
||
926 | * @param string $class_name |
||
927 | * @param array $arguments |
||
928 | * @return array |
||
929 | * @throws \ReflectionException |
||
930 | */ |
||
931 | protected function _resolve_dependencies( ReflectionClass $reflector, $class_name, $arguments = array() ) { |
||
932 | // let's examine the constructor |
||
933 | $constructor = $reflector->getConstructor(); |
||
934 | // whu? huh? nothing? |
||
935 | if ( ! $constructor ) { |
||
936 | return $arguments; |
||
937 | } |
||
938 | // get constructor parameters |
||
939 | $params = $constructor->getParameters(); |
||
940 | // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
||
941 | $argument_keys = array_keys( $arguments ); |
||
942 | // now loop thru all of the constructors expected parameters |
||
943 | foreach ( $params as $index => $param ) { |
||
944 | // is this a dependency for a specific class ? |
||
945 | $param_class = $param->getClass() ? $param->getClass()->name : null; |
||
946 | if ( |
||
947 | // param is not even a class |
||
948 | empty( $param_class ) |
||
949 | // and something already exists in the incoming arguments for this param |
||
950 | && isset( $argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ] ) |
||
951 | ) { |
||
952 | // so let's skip this argument and move on to the next |
||
953 | continue; |
||
954 | } else if ( |
||
955 | // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
||
956 | ! empty( $param_class ) |
||
957 | && isset( $argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ] ) |
||
958 | && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
||
959 | ) { |
||
960 | // skip this argument and move on to the next |
||
961 | continue; |
||
962 | } else if ( |
||
963 | // parameter is type hinted as a class, and should be injected |
||
964 | ! empty( $param_class ) |
||
965 | && $this->_dependency_map->has_dependency_for_class( $class_name, $param_class ) |
||
966 | ) { |
||
967 | $arguments = $this->_resolve_dependency( $class_name, $param_class, $arguments, $index ); |
||
968 | } else { |
||
969 | try { |
||
970 | $arguments[ $index ] = $param->getDefaultValue(); |
||
971 | } catch ( ReflectionException $e ) { |
||
972 | throw new ReflectionException( |
||
973 | sprintf( |
||
974 | __( '%1$s for parameter "$%2$s"', 'event_espresso' ), |
||
975 | $e->getMessage(), |
||
976 | $param->getName() |
||
977 | ) |
||
978 | ); |
||
979 | } |
||
980 | } |
||
981 | |||
982 | } |
||
983 | return $arguments; |
||
984 | } |
||
985 | |||
986 | |||
987 | |||
988 | /** |
||
989 | * @access protected |
||
990 | * @param string $class_name |
||
991 | * @param string $param_class |
||
992 | * @param array $arguments |
||
993 | * @param mixed $index |
||
994 | * @return array |
||
995 | */ |
||
996 | protected function _resolve_dependency( $class_name, $param_class , $arguments, $index ) { |
||
997 | $dependency = null; |
||
998 | // should dependency be loaded from cache ? |
||
999 | $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency( $class_name, $param_class ) |
||
1000 | !== EE_Dependency_Map::load_new_object |
||
1001 | ? true |
||
1002 | : false; |
||
1003 | // we might have a dependency... |
||
1004 | // let's MAYBE try and find it in our cache if that's what's been requested |
||
1005 | $cached_class = $cache_on ? $this->_get_cached_class( $param_class ) : null; |
||
1006 | // and grab it if it exists |
||
1007 | if ( $cached_class instanceof $param_class ) { |
||
1008 | $dependency = $cached_class; |
||
1009 | } else if ( $param_class != $class_name ) { |
||
1010 | // obtain the loader method from the dependency map |
||
1011 | $loader = $this->_dependency_map->class_loader( $param_class ); |
||
1012 | // is loader a custom closure ? |
||
1013 | if ( $loader instanceof Closure ) { |
||
1014 | $dependency = $loader(); |
||
1015 | } else { |
||
1016 | // set the cache on property for the recursive loading call |
||
1017 | $this->_cache_on = $cache_on; |
||
1018 | // if not, then let's try and load it via the registry |
||
1019 | if ( method_exists( $this, $loader ) ) { |
||
1020 | $dependency = $this->{$loader}($param_class); |
||
1021 | } else { |
||
1022 | $dependency = $this->create($param_class, array(), $cache_on); |
||
1023 | } |
||
1024 | } |
||
1025 | } |
||
1026 | // did we successfully find the correct dependency ? |
||
1027 | if ( $dependency instanceof $param_class ) { |
||
1028 | // then let's inject it into the incoming array of arguments at the correct location |
||
1029 | if ( isset( $argument_keys[ $index ] ) ) { |
||
1030 | $arguments[ $argument_keys[ $index ] ] = $dependency; |
||
1031 | } else { |
||
1032 | $arguments[ $index ] = $dependency; |
||
1033 | } |
||
1034 | } |
||
1035 | return $arguments; |
||
1036 | } |
||
1037 | |||
1038 | |||
1039 | |||
1040 | /** |
||
1041 | * _set_cached_class |
||
1042 | * |
||
1043 | * attempts to cache the instantiated class locally |
||
1044 | * in one of the following places, in the following order: |
||
1045 | * $this->{class_abbreviation} ie: $this->CART |
||
1046 | * $this->{$class_name} ie: $this->Some_Class |
||
1047 | * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
||
1048 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
1049 | * |
||
1050 | * @access protected |
||
1051 | * @param object $class_obj |
||
1052 | * @param string $class_name |
||
1053 | * @param string $class_prefix |
||
1054 | * @param bool $from_db |
||
1055 | * @return void |
||
1056 | */ |
||
1057 | protected function _set_cached_class( $class_obj, $class_name, $class_prefix = '', $from_db = false ) { |
||
1058 | if ( empty( $class_obj ) ) { |
||
1059 | return; |
||
1060 | } |
||
1061 | // return newly instantiated class |
||
1062 | if ( isset( $this->_class_abbreviations[ $class_name ] ) ) { |
||
1063 | $class_abbreviation = $this->_class_abbreviations[ $class_name ]; |
||
1064 | $this->{$class_abbreviation} = $class_obj; |
||
1065 | } else if ( property_exists( $this, $class_name ) ) { |
||
1066 | $this->{$class_name} = $class_obj; |
||
1067 | } else if ( $class_prefix == 'addon' ) { |
||
1068 | $this->addons->{$class_name} = $class_obj; |
||
1069 | } else if ( ! $from_db ) { |
||
1070 | $this->LIB->{$class_name} = $class_obj; |
||
1071 | } |
||
1072 | } |
||
1073 | |||
1074 | |||
1075 | |||
1076 | /** |
||
1077 | * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
||
1078 | * |
||
1079 | * |
||
1080 | * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
||
1081 | * in the EE_Dependency_Map::$_class_loaders array, |
||
1082 | * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
||
1083 | * @param array $arguments |
||
1084 | * @return object |
||
1085 | */ |
||
1086 | public static function factory( $classname, $arguments = array() ) { |
||
1095 | |||
1096 | |||
1097 | |||
1098 | /** |
||
1099 | * Gets the addon by its name/slug (not classname. For that, just |
||
1100 | * use the classname as the property name on EE_Config::instance()->addons) |
||
1101 | * @param string $name |
||
1102 | * @return EE_Addon |
||
1103 | */ |
||
1104 | public function get_addon_by_name( $name ) { |
||
1112 | |||
1113 | |||
1114 | |||
1115 | /** |
||
1116 | * 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} |
||
1117 | * |
||
1118 | * @return EE_Addon[] where the KEYS are the addon's name() |
||
1119 | */ |
||
1120 | public function get_addons_by_name() { |
||
1127 | |||
1128 | |||
1129 | |||
1130 | /** |
||
1131 | * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
||
1132 | * a stale copy of it around |
||
1133 | * |
||
1134 | * @param string $model_name |
||
1135 | * @return \EEM_Base |
||
1136 | * @throws \EE_Error |
||
1137 | */ |
||
1138 | public function reset_model( $model_name ) { |
||
1149 | |||
1150 | |||
1151 | |||
1152 | /** |
||
1153 | * Resets the registry. |
||
1154 | * |
||
1155 | * The criteria for what gets reset is based on what can be shared between sites on the same request when switch_to_blog |
||
1156 | * is used in a multisite install. Here is a list of things that are NOT reset. |
||
1157 | * |
||
1158 | * - $_dependency_map |
||
1159 | * - $_class_abbreviations |
||
1160 | * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
||
1161 | * - $REQ: Still on the same request so no need to change. |
||
1162 | * - $CAP: There is no site specific state in the EE_Capability class. |
||
1163 | * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only one Session |
||
1164 | * can be active in a single request. Resetting could resolve in "headers already sent" errors. |
||
1165 | * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
||
1166 | * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
||
1167 | * switch or on the restore. |
||
1168 | * - $modules |
||
1169 | * - $shortcodes |
||
1170 | * - $widgets |
||
1171 | * |
||
1172 | * @param boolean $hard whether to reset data in the database too, or just refresh |
||
1173 | * the Registry to its state at the beginning of the request |
||
1174 | * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
||
1175 | * or just reset without re-instantiating (handy to set to FALSE if you're not sure if you CAN |
||
1176 | * currently reinstantiate the singletons at the moment) |
||
1177 | * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so client |
||
1178 | * code instead can just change the model context to a different blog id if necessary |
||
1179 | * |
||
1180 | * @return EE_Registry |
||
1181 | */ |
||
1182 | public static function reset( $hard = false, $reinstantiate = true, $reset_models = true ) { |
||
1204 | |||
1205 | |||
1206 | |||
1207 | /** |
||
1208 | * @override magic methods |
||
1209 | * @return void |
||
1210 | */ |
||
1211 | final function __destruct() { |
||
1213 | |||
1214 | |||
1215 | |||
1216 | /** |
||
1217 | * @param $a |
||
1218 | * @param $b |
||
1219 | */ |
||
1220 | final function __call( $a, $b ) { |
||
1222 | |||
1223 | |||
1224 | |||
1225 | /** |
||
1226 | * @param $a |
||
1227 | */ |
||
1228 | final function __get( $a ) { |
||
1230 | |||
1231 | |||
1232 | |||
1233 | /** |
||
1234 | * @param $a |
||
1235 | * @param $b |
||
1236 | */ |
||
1237 | final function __set( $a, $b ) { |
||
1239 | |||
1240 | |||
1241 | |||
1242 | /** |
||
1243 | * @param $a |
||
1244 | */ |
||
1245 | final function __isset( $a ) { |
||
1247 | |||
1248 | |||
1249 | |||
1250 | /** |
||
1251 | * @param $a |
||
1252 | */ |
||
1253 | final function __unset( $a ) { |
||
1255 | |||
1256 | |||
1257 | |||
1258 | /** |
||
1259 | * @return array |
||
1260 | */ |
||
1261 | final function __sleep() { |
||
1264 | |||
1265 | |||
1266 | |||
1267 | final function __wakeup() { |
||
1269 | |||
1270 | |||
1271 | |||
1272 | /** |
||
1273 | * @return string |
||
1274 | */ |
||
1275 | final function __toString() { |
||
1278 | |||
1279 | |||
1280 | |||
1281 | final function __invoke() { |
||
1283 | |||
1284 | |||
1285 | |||
1286 | final function __set_state() { |
||
1288 | |||
1289 | |||
1290 | |||
1291 | final function __clone() { |
||
1293 | |||
1294 | |||
1295 | |||
1296 | /** |
||
1297 | * @param $a |
||
1298 | * @param $b |
||
1299 | */ |
||
1300 | final static function __callStatic( $a, $b ) { |
||
1302 | |||
1303 | /** |
||
1304 | * Gets all the custom post type models defined |
||
1305 | * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
||
1306 | */ |
||
1307 | public function cpt_models() { |
||
1316 | |||
1317 | |||
1318 | |||
1319 | /** |
||
1320 | * @return \EE_Config |
||
1321 | */ |
||
1322 | public static function CFG() { |
||
1325 | |||
1326 | |||
1327 | } |
||
1328 | // End of file EE_Registry.core.php |
||
1329 | // Location: ./core/EE_Registry.core.php |
||
1330 |
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):