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 |
||
19 | class EE_Registry implements ResettableInterface |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * EE_Registry Object |
||
24 | * |
||
25 | * @var EE_Registry $_instance |
||
26 | * @access private |
||
27 | */ |
||
28 | private static $_instance = null; |
||
29 | |||
30 | /** |
||
31 | * @var EE_Dependency_Map $_dependency_map |
||
32 | * @access protected |
||
33 | */ |
||
34 | protected $_dependency_map = null; |
||
35 | |||
36 | /** |
||
37 | * @var array $_class_abbreviations |
||
38 | * @access protected |
||
39 | */ |
||
40 | protected $_class_abbreviations = array(); |
||
41 | |||
42 | /** |
||
43 | * @access public |
||
44 | * @var \EventEspresso\core\services\commands\CommandBusInterface $BUS |
||
45 | */ |
||
46 | public $BUS; |
||
47 | |||
48 | /** |
||
49 | * EE_Cart Object |
||
50 | * |
||
51 | * @access public |
||
52 | * @var EE_Cart $CART |
||
53 | */ |
||
54 | public $CART = null; |
||
55 | |||
56 | /** |
||
57 | * EE_Config Object |
||
58 | * |
||
59 | * @access public |
||
60 | * @var EE_Config $CFG |
||
61 | */ |
||
62 | public $CFG = null; |
||
63 | |||
64 | /** |
||
65 | * EE_Network_Config Object |
||
66 | * |
||
67 | * @access public |
||
68 | * @var EE_Network_Config $NET_CFG |
||
69 | */ |
||
70 | public $NET_CFG = null; |
||
71 | |||
72 | /** |
||
73 | * StdClass object for storing library classes in |
||
74 | * |
||
75 | * @public LIB |
||
76 | * @var StdClass $LIB |
||
77 | */ |
||
78 | public $LIB = null; |
||
79 | |||
80 | /** |
||
81 | * EE_Request_Handler Object |
||
82 | * |
||
83 | * @access public |
||
84 | * @var EE_Request_Handler $REQ |
||
85 | */ |
||
86 | public $REQ = null; |
||
87 | |||
88 | /** |
||
89 | * EE_Session Object |
||
90 | * |
||
91 | * @access public |
||
92 | * @var EE_Session $SSN |
||
93 | */ |
||
94 | public $SSN = null; |
||
95 | |||
96 | /** |
||
97 | * holds the ee capabilities object. |
||
98 | * |
||
99 | * @since 4.5.0 |
||
100 | * @var EE_Capabilities |
||
101 | */ |
||
102 | public $CAP = null; |
||
103 | |||
104 | /** |
||
105 | * holds the EE_Message_Resource_Manager object. |
||
106 | * |
||
107 | * @since 4.9.0 |
||
108 | * @var EE_Message_Resource_Manager |
||
109 | */ |
||
110 | public $MRM = null; |
||
111 | |||
112 | |||
113 | /** |
||
114 | * Holds the Assets Registry instance |
||
115 | * @var Registry |
||
116 | */ |
||
117 | public $AssetsRegistry = null; |
||
118 | |||
119 | /** |
||
120 | * $addons - StdClass object for holding addons which have registered themselves to work with EE core |
||
121 | * |
||
122 | * @access public |
||
123 | * @var EE_Addon[] |
||
124 | */ |
||
125 | public $addons = null; |
||
126 | |||
127 | /** |
||
128 | * $models |
||
129 | * @access public |
||
130 | * @var EEM_Base[] $models keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
||
131 | */ |
||
132 | public $models = array(); |
||
133 | |||
134 | /** |
||
135 | * $modules |
||
136 | * @access public |
||
137 | * @var EED_Module[] $modules |
||
138 | */ |
||
139 | public $modules = null; |
||
140 | |||
141 | /** |
||
142 | * $shortcodes |
||
143 | * @access public |
||
144 | * @var EES_Shortcode[] $shortcodes |
||
145 | */ |
||
146 | public $shortcodes = null; |
||
147 | |||
148 | /** |
||
149 | * $widgets |
||
150 | * @access public |
||
151 | * @var WP_Widget[] $widgets |
||
152 | */ |
||
153 | public $widgets = null; |
||
154 | |||
155 | /** |
||
156 | * $non_abstract_db_models |
||
157 | * @access public |
||
158 | * @var array this is an array of all implemented model names (i.e. not the parent abstract models, or models |
||
159 | * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
||
160 | * Keys are model "short names" (eg "Event") as used in model relations, and values are |
||
161 | * classnames (eg "EEM_Event") |
||
162 | */ |
||
163 | public $non_abstract_db_models = array(); |
||
164 | |||
165 | |||
166 | /** |
||
167 | * $i18n_js_strings - internationalization for JS strings |
||
168 | * usage: EE_Registry::i18n_js_strings['string_key'] = __( 'string to translate.', 'event_espresso' ); |
||
169 | * in js file: var translatedString = eei18n.string_key; |
||
170 | * |
||
171 | * @access public |
||
172 | * @var array |
||
173 | */ |
||
174 | public static $i18n_js_strings = array(); |
||
175 | |||
176 | |||
177 | /** |
||
178 | * $main_file - path to espresso.php |
||
179 | * |
||
180 | * @access public |
||
181 | * @var array |
||
182 | */ |
||
183 | public $main_file; |
||
184 | |||
185 | /** |
||
186 | * array of ReflectionClass objects where the key is the class name |
||
187 | * |
||
188 | * @access public |
||
189 | * @var ReflectionClass[] |
||
190 | */ |
||
191 | public $_reflectors; |
||
192 | |||
193 | /** |
||
194 | * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
||
195 | * |
||
196 | * @access protected |
||
197 | * @var boolean $_cache_on |
||
198 | */ |
||
199 | protected $_cache_on = true; |
||
200 | |||
201 | |||
202 | |||
203 | /** |
||
204 | * @singleton method used to instantiate class object |
||
205 | * @access public |
||
206 | * @param \EE_Dependency_Map $dependency_map |
||
207 | * @return \EE_Registry instance |
||
208 | */ |
||
209 | public static function instance(\EE_Dependency_Map $dependency_map = null) |
||
210 | { |
||
211 | // check if class object is instantiated |
||
212 | if ( ! self::$_instance instanceof EE_Registry) { |
||
213 | self::$_instance = new EE_Registry($dependency_map); |
||
|
|||
214 | } |
||
215 | return self::$_instance; |
||
216 | } |
||
217 | |||
218 | |||
219 | |||
220 | /** |
||
221 | *protected constructor to prevent direct creation |
||
222 | * |
||
223 | * @Constructor |
||
224 | * @access protected |
||
225 | * @param \EE_Dependency_Map $dependency_map |
||
226 | */ |
||
227 | protected function __construct(\EE_Dependency_Map $dependency_map) |
||
228 | { |
||
229 | $this->_dependency_map = $dependency_map; |
||
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 | add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
||
236 | } |
||
237 | |||
238 | |||
239 | |||
240 | /** |
||
241 | * initialize |
||
242 | */ |
||
243 | public function initialize() |
||
244 | { |
||
245 | $this->_class_abbreviations = apply_filters( |
||
246 | 'FHEE__EE_Registry____construct___class_abbreviations', |
||
247 | array( |
||
248 | 'EE_Config' => 'CFG', |
||
249 | 'EE_Session' => 'SSN', |
||
250 | 'EE_Capabilities' => 'CAP', |
||
251 | 'EE_Cart' => 'CART', |
||
252 | 'EE_Network_Config' => 'NET_CFG', |
||
253 | 'EE_Request_Handler' => 'REQ', |
||
254 | 'EE_Message_Resource_Manager' => 'MRM', |
||
255 | 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
||
256 | 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
||
257 | ) |
||
258 | ); |
||
259 | $this->load_core('Base', array(), true); |
||
260 | // add our request and response objects to the cache |
||
261 | $request_loader = $this->_dependency_map->class_loader('EE_Request'); |
||
262 | $this->_set_cached_class( |
||
263 | $request_loader(), |
||
264 | 'EE_Request' |
||
265 | ); |
||
266 | $response_loader = $this->_dependency_map->class_loader('EE_Response'); |
||
267 | $this->_set_cached_class( |
||
268 | $response_loader(), |
||
269 | 'EE_Response' |
||
270 | ); |
||
271 | add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
||
272 | } |
||
273 | |||
274 | |||
275 | |||
276 | /** |
||
277 | * init |
||
278 | * |
||
279 | * @access public |
||
280 | * @return void |
||
281 | */ |
||
282 | public function init() |
||
283 | { |
||
284 | // Get current page protocol |
||
285 | $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
||
286 | // Output admin-ajax.php URL with same protocol as current page |
||
287 | self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
||
288 | self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
||
289 | } |
||
290 | |||
291 | |||
292 | |||
293 | /** |
||
294 | * localize_i18n_js_strings |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | public static function localize_i18n_js_strings() |
||
299 | { |
||
300 | $i18n_js_strings = (array)EE_Registry::$i18n_js_strings; |
||
301 | foreach ($i18n_js_strings as $key => $value) { |
||
302 | if (is_scalar($value)) { |
||
303 | $i18n_js_strings[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8'); |
||
304 | } |
||
305 | } |
||
306 | return "/* <![CDATA[ */ var eei18n = " . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
||
307 | } |
||
308 | |||
309 | |||
310 | |||
311 | /** |
||
312 | * @param mixed string | EED_Module $module |
||
313 | */ |
||
314 | public function add_module($module) |
||
315 | { |
||
316 | if ($module instanceof EED_Module) { |
||
317 | $module_class = get_class($module); |
||
318 | $this->modules->{$module_class} = $module; |
||
319 | } else { |
||
320 | if ( ! class_exists('EE_Module_Request_Router')) { |
||
321 | $this->load_core('Module_Request_Router'); |
||
322 | } |
||
323 | $this->modules->{$module} = EE_Module_Request_Router::module_factory($module); |
||
324 | } |
||
325 | } |
||
326 | |||
327 | |||
328 | |||
329 | /** |
||
330 | * @param string $module_name |
||
331 | * @return mixed EED_Module | NULL |
||
332 | */ |
||
333 | public function get_module($module_name = '') |
||
334 | { |
||
335 | return isset($this->modules->{$module_name}) ? $this->modules->{$module_name} : null; |
||
336 | } |
||
337 | |||
338 | |||
339 | |||
340 | /** |
||
341 | * loads core classes - must be singletons |
||
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_core($class_name, $arguments = array(), $load_only = false) |
||
350 | { |
||
351 | $core_paths = apply_filters( |
||
352 | 'FHEE__EE_Registry__load_core__core_paths', |
||
353 | array( |
||
354 | EE_CORE, |
||
355 | EE_ADMIN, |
||
356 | EE_CPTS, |
||
357 | EE_CORE . 'data_migration_scripts' . DS, |
||
358 | EE_CORE . 'request_stack' . DS, |
||
359 | EE_CORE . 'middleware' . DS, |
||
360 | ) |
||
361 | ); |
||
362 | // retrieve instantiated class |
||
363 | return $this->_load($core_paths, 'EE_', $class_name, 'core', $arguments, false, true, $load_only); |
||
364 | } |
||
365 | |||
366 | |||
367 | |||
368 | /** |
||
369 | * loads service classes |
||
370 | * |
||
371 | * @access public |
||
372 | * @param string $class_name - simple class name ie: session |
||
373 | * @param mixed $arguments |
||
374 | * @param bool $load_only |
||
375 | * @return mixed |
||
376 | */ |
||
377 | public function load_service($class_name, $arguments = array(), $load_only = false) |
||
378 | { |
||
379 | $service_paths = apply_filters( |
||
380 | 'FHEE__EE_Registry__load_service__service_paths', |
||
381 | array( |
||
382 | EE_CORE . 'services' . DS, |
||
383 | ) |
||
384 | ); |
||
385 | // retrieve instantiated class |
||
386 | return $this->_load($service_paths, 'EE_', $class_name, 'class', $arguments, false, true, $load_only); |
||
387 | } |
||
388 | |||
389 | |||
390 | |||
391 | /** |
||
392 | * loads data_migration_scripts |
||
393 | * |
||
394 | * @access public |
||
395 | * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
||
396 | * @param mixed $arguments |
||
397 | * @return EE_Data_Migration_Script_Base|mixed |
||
398 | */ |
||
399 | public function load_dms($class_name, $arguments = array()) |
||
400 | { |
||
401 | // retrieve instantiated class |
||
402 | return $this->_load(EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), 'EE_DMS_', $class_name, 'dms', $arguments, false, false, false); |
||
403 | } |
||
404 | |||
405 | |||
406 | |||
407 | /** |
||
408 | * loads object creating classes - must be singletons |
||
409 | * |
||
410 | * @param string $class_name - simple class name ie: attendee |
||
411 | * @param mixed $arguments - an array of arguments to pass to the class |
||
412 | * @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
||
413 | * @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) |
||
414 | * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate (default) |
||
415 | * @return EE_Base_Class | bool |
||
416 | */ |
||
417 | public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
||
418 | { |
||
419 | $paths = apply_filters('FHEE__EE_Registry__load_class__paths', array( |
||
420 | EE_CORE, |
||
421 | EE_CLASSES, |
||
422 | EE_BUSINESS, |
||
423 | )); |
||
424 | // retrieve instantiated class |
||
425 | return $this->_load($paths, 'EE_', $class_name, 'class', $arguments, $from_db, $cache, $load_only); |
||
426 | } |
||
427 | |||
428 | |||
429 | |||
430 | /** |
||
431 | * loads helper classes - must be singletons |
||
432 | * |
||
433 | * @param string $class_name - simple class name ie: price |
||
434 | * @param mixed $arguments |
||
435 | * @param bool $load_only |
||
436 | * @return EEH_Base | bool |
||
437 | */ |
||
438 | public function load_helper($class_name, $arguments = array(), $load_only = true) |
||
439 | { |
||
440 | // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
||
441 | $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
||
442 | // retrieve instantiated class |
||
443 | return $this->_load($helper_paths, 'EEH_', $class_name, 'helper', $arguments, false, true, $load_only); |
||
444 | } |
||
445 | |||
446 | |||
447 | |||
448 | /** |
||
449 | * loads core classes - must be singletons |
||
450 | * |
||
451 | * @access public |
||
452 | * @param string $class_name - simple class name ie: session |
||
453 | * @param mixed $arguments |
||
454 | * @param bool $load_only |
||
455 | * @param bool $cache whether to cache the object or not. |
||
456 | * @return mixed |
||
457 | */ |
||
458 | public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
||
459 | { |
||
460 | $paths = array( |
||
461 | EE_LIBRARIES, |
||
462 | EE_LIBRARIES . 'messages' . DS, |
||
463 | EE_LIBRARIES . 'shortcodes' . DS, |
||
464 | EE_LIBRARIES . 'qtips' . DS, |
||
465 | EE_LIBRARIES . 'payment_methods' . DS, |
||
466 | ); |
||
467 | // retrieve instantiated class |
||
468 | return $this->_load($paths, 'EE_', $class_name, 'lib', $arguments, false, $cache, $load_only); |
||
469 | } |
||
470 | |||
471 | |||
472 | |||
473 | /** |
||
474 | * loads model classes - must be singletons |
||
475 | * |
||
476 | * @param string $class_name - simple class name ie: price |
||
477 | * @param mixed $arguments |
||
478 | * @param bool $load_only |
||
479 | * @return EEM_Base | bool |
||
480 | */ |
||
481 | public function load_model($class_name, $arguments = array(), $load_only = false) |
||
490 | |||
491 | |||
492 | |||
493 | /** |
||
494 | * loads model classes - must be singletons |
||
495 | * |
||
496 | * @param string $class_name - simple class name ie: price |
||
497 | * @param mixed $arguments |
||
498 | * @param bool $load_only |
||
499 | * @return mixed | bool |
||
500 | */ |
||
501 | public function load_model_class($class_name, $arguments = array(), $load_only = true) |
||
512 | |||
513 | |||
514 | |||
515 | /** |
||
516 | * Determines if $model_name is the name of an actual EE model. |
||
517 | * |
||
518 | * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
||
519 | * @return boolean |
||
520 | */ |
||
521 | public function is_model_name($model_name) |
||
525 | |||
526 | |||
527 | |||
528 | /** |
||
529 | * generic class loader |
||
530 | * |
||
531 | * @param string $path_to_file - directory path to file location, not including filename |
||
532 | * @param string $file_name - file name ie: my_file.php, including extension |
||
533 | * @param string $type - file type - core? class? helper? model? |
||
534 | * @param mixed $arguments |
||
535 | * @param bool $load_only |
||
536 | * @return mixed |
||
537 | */ |
||
538 | public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
||
543 | |||
544 | |||
545 | |||
546 | /** |
||
547 | * load_addon |
||
548 | * |
||
549 | * @param string $path_to_file - directory path to file location, not including filename |
||
550 | * @param string $class_name - full class name ie: My_Class |
||
551 | * @param string $type - file type - core? class? helper? model? |
||
552 | * @param mixed $arguments |
||
553 | * @param bool $load_only |
||
554 | * @return EE_Addon |
||
555 | */ |
||
556 | public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
||
561 | |||
562 | |||
563 | |||
564 | /** |
||
565 | * instantiates, caches, and automatically resolves dependencies |
||
566 | * for classes that use a Fully Qualified Class Name. |
||
567 | * if the class is not capable of being loaded using PSR-4 autoloading, |
||
568 | * then you need to use one of the existing load_*() methods |
||
569 | * which can resolve the classname and filepath from the passed arguments |
||
570 | * |
||
571 | * @param bool|string $class_name Fully Qualified Class Name |
||
572 | * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
||
573 | * @param bool $cache whether to cache the instantiated object for reuse |
||
574 | * @param bool $from_db some classes are instantiated from the db |
||
575 | * and thus call a different method to instantiate |
||
576 | * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
||
577 | * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
||
578 | * @return mixed null = failure to load or instantiate class object. |
||
579 | * object = class loaded and instantiated successfully. |
||
580 | * bool = fail or success when $load_only is true |
||
581 | */ |
||
582 | public function create( |
||
624 | |||
625 | |||
626 | |||
627 | /** |
||
628 | * instantiates, caches, and injects dependencies for classes |
||
629 | * |
||
630 | * @param array $file_paths an array of paths to folders to look in |
||
631 | * @param string $class_prefix EE or EEM or... ??? |
||
632 | * @param bool|string $class_name $class name |
||
633 | * @param string $type file type - core? class? helper? model? |
||
634 | * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
||
635 | * @param bool $from_db some classes are instantiated from the db |
||
636 | * and thus call a different method to instantiate |
||
637 | * @param bool $cache whether to cache the instantiated object for reuse |
||
638 | * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
||
639 | * @return null|object|bool null = failure to load or instantiate class object. |
||
640 | * object = class loaded and instantiated successfully. |
||
641 | * bool = fail or success when $load_only is true |
||
642 | */ |
||
643 | protected function _load( |
||
700 | |||
701 | |||
702 | |||
703 | |||
704 | /** |
||
705 | * _get_cached_class |
||
706 | * attempts to find a cached version of the requested class |
||
707 | * by looking in the following places: |
||
708 | * $this->{$class_abbreviation} ie: $this->CART |
||
709 | * $this->{$class_name} ie: $this->Some_Class |
||
710 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
711 | * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
||
712 | * |
||
713 | * @access protected |
||
714 | * @param string $class_name |
||
715 | * @param string $class_prefix |
||
716 | * @return mixed |
||
717 | */ |
||
718 | protected function _get_cached_class($class_name, $class_prefix = '') |
||
743 | |||
744 | |||
745 | |||
746 | /** |
||
747 | * removes a cached version of the requested class |
||
748 | * |
||
749 | * @param string $class_name |
||
750 | * @param boolean $addon |
||
751 | * @return boolean |
||
752 | */ |
||
753 | public function clear_cached_class($class_name, $addon = false) |
||
779 | |||
780 | |||
781 | /** |
||
782 | * _resolve_path |
||
783 | * attempts to find a full valid filepath for the requested class. |
||
784 | * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
||
785 | * then returns that path if the target file has been found and is readable |
||
786 | * |
||
787 | * @access protected |
||
788 | * @param string $class_name |
||
789 | * @param string $type |
||
790 | * @param array $file_paths |
||
791 | * @return string | bool |
||
792 | */ |
||
793 | protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
||
812 | |||
813 | |||
814 | |||
815 | /** |
||
816 | * _require_file |
||
817 | * basically just performs a require_once() |
||
818 | * but with some error handling |
||
819 | * |
||
820 | * @access protected |
||
821 | * @param string $path |
||
822 | * @param string $class_name |
||
823 | * @param string $type |
||
824 | * @param array $file_paths |
||
825 | * @return boolean |
||
826 | * @throws \EE_Error |
||
827 | */ |
||
828 | protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
||
864 | |||
865 | |||
866 | |||
867 | /** |
||
868 | * _create_object |
||
869 | * Attempts to instantiate the requested class via any of the |
||
870 | * commonly used instantiation methods employed throughout EE. |
||
871 | * The priority for instantiation is as follows: |
||
872 | * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
||
873 | * - model objects via their 'new_instance_from_db' method |
||
874 | * - model objects via their 'new_instance' method |
||
875 | * - "singleton" classes" via their 'instance' method |
||
876 | * - standard instantiable classes via their __constructor |
||
877 | * Prior to instantiation, if the classname exists in the dependency_map, |
||
878 | * then the constructor for the requested class will be examined to determine |
||
879 | * if any dependencies exist, and if they can be injected. |
||
880 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
881 | * |
||
882 | * @access protected |
||
883 | * @param string $class_name |
||
884 | * @param array $arguments |
||
885 | * @param string $type |
||
886 | * @param bool $from_db |
||
887 | * @return null | object |
||
888 | * @throws \EE_Error |
||
889 | */ |
||
890 | protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
||
957 | |||
958 | |||
959 | |||
960 | /** |
||
961 | * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
||
962 | * @param array $array |
||
963 | * @return bool |
||
964 | */ |
||
965 | protected function _array_is_numerically_and_sequentially_indexed(array $array) |
||
969 | |||
970 | |||
971 | |||
972 | /** |
||
973 | * getReflectionClass |
||
974 | * checks if a ReflectionClass object has already been generated for a class |
||
975 | * and returns that instead of creating a new one |
||
976 | * |
||
977 | * @access public |
||
978 | * @param string $class_name |
||
979 | * @return ReflectionClass |
||
980 | */ |
||
981 | View Code Duplication | public function get_ReflectionClass($class_name) |
|
991 | |||
992 | |||
993 | |||
994 | /** |
||
995 | * _resolve_dependencies |
||
996 | * examines the constructor for the requested class to determine |
||
997 | * if any dependencies exist, and if they can be injected. |
||
998 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
999 | * PLZ NOTE: this is achieved by type hinting the constructor params |
||
1000 | * For example: |
||
1001 | * if attempting to load a class "Foo" with the following constructor: |
||
1002 | * __construct( Bar $bar_class, Fighter $grohl_class ) |
||
1003 | * then $bar_class and $grohl_class will be added to the $arguments array, |
||
1004 | * but only IF they are NOT already present in the incoming arguments array, |
||
1005 | * and the correct classes can be loaded |
||
1006 | * |
||
1007 | * @access protected |
||
1008 | * @param ReflectionClass $reflector |
||
1009 | * @param string $class_name |
||
1010 | * @param array $arguments |
||
1011 | * @return array |
||
1012 | * @throws \ReflectionException |
||
1013 | */ |
||
1014 | protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, $arguments = array()) |
||
1074 | |||
1075 | |||
1076 | |||
1077 | /** |
||
1078 | * @access protected |
||
1079 | * @param string $class_name |
||
1080 | * @param string $param_class |
||
1081 | * @param array $arguments |
||
1082 | * @param mixed $index |
||
1083 | * @return array |
||
1084 | */ |
||
1085 | protected function _resolve_dependency($class_name, $param_class, $arguments, $index) |
||
1127 | |||
1128 | |||
1129 | |||
1130 | /** |
||
1131 | * _set_cached_class |
||
1132 | * attempts to cache the instantiated class locally |
||
1133 | * in one of the following places, in the following order: |
||
1134 | * $this->{class_abbreviation} ie: $this->CART |
||
1135 | * $this->{$class_name} ie: $this->Some_Class |
||
1136 | * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
||
1137 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
1138 | * |
||
1139 | * @access protected |
||
1140 | * @param object $class_obj |
||
1141 | * @param string $class_name |
||
1142 | * @param string $class_prefix |
||
1143 | * @param bool $from_db |
||
1144 | * @return void |
||
1145 | */ |
||
1146 | protected function _set_cached_class($class_obj, $class_name, $class_prefix = '', $from_db = false) |
||
1170 | |||
1171 | |||
1172 | |||
1173 | /** |
||
1174 | * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
||
1175 | * |
||
1176 | * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
||
1177 | * in the EE_Dependency_Map::$_class_loaders array, |
||
1178 | * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
||
1179 | * @param array $arguments |
||
1180 | * @return object |
||
1181 | */ |
||
1182 | public static function factory($classname, $arguments = array()) |
||
1193 | |||
1194 | |||
1195 | |||
1196 | /** |
||
1197 | * Gets the addon by its name/slug (not classname. For that, just |
||
1198 | * use the classname as the property name on EE_Config::instance()->addons) |
||
1199 | * |
||
1200 | * @param string $name |
||
1201 | * @return EE_Addon |
||
1202 | */ |
||
1203 | public function get_addon_by_name($name) |
||
1212 | |||
1213 | |||
1214 | |||
1215 | /** |
||
1216 | * 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 |
||
1217 | * the addon's classname. So if you just want to get the addon by classname, use EE_Config::instance()->addons->{classname} |
||
1218 | * |
||
1219 | * @return EE_Addon[] where the KEYS are the addon's name() |
||
1220 | */ |
||
1221 | public function get_addons_by_name() |
||
1229 | |||
1230 | |||
1231 | |||
1232 | /** |
||
1233 | * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
||
1234 | * a stale copy of it around |
||
1235 | * |
||
1236 | * @param string $model_name |
||
1237 | * @return \EEM_Base |
||
1238 | * @throws \EE_Error |
||
1239 | */ |
||
1240 | public function reset_model($model_name) |
||
1254 | |||
1255 | |||
1256 | |||
1257 | /** |
||
1258 | * Resets the registry. |
||
1259 | * The criteria for what gets reset is based on what can be shared between sites on the same request when switch_to_blog |
||
1260 | * is used in a multisite install. Here is a list of things that are NOT reset. |
||
1261 | * - $_dependency_map |
||
1262 | * - $_class_abbreviations |
||
1263 | * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
||
1264 | * - $REQ: Still on the same request so no need to change. |
||
1265 | * - $CAP: There is no site specific state in the EE_Capability class. |
||
1266 | * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only one Session |
||
1267 | * can be active in a single request. Resetting could resolve in "headers already sent" errors. |
||
1268 | * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
||
1269 | * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
||
1270 | * switch or on the restore. |
||
1271 | * - $modules |
||
1272 | * - $shortcodes |
||
1273 | * - $widgets |
||
1274 | * |
||
1275 | * @param boolean $hard whether to reset data in the database too, or just refresh |
||
1276 | * the Registry to its state at the beginning of the request |
||
1277 | * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
||
1278 | * or just reset without re-instantiating (handy to set to FALSE if you're not sure if you CAN |
||
1279 | * currently reinstantiate the singletons at the moment) |
||
1280 | * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so client |
||
1281 | * code instead can just change the model context to a different blog id if necessary |
||
1282 | * @return EE_Registry |
||
1283 | */ |
||
1284 | public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
||
1307 | |||
1308 | |||
1309 | |||
1310 | /** |
||
1311 | * if passed object implements ResettableInterface, then call it's reset() method |
||
1312 | * if passed object implements InterminableInterface, then return false, |
||
1313 | * to indicate that it should NOT be cleared from the Registry cache |
||
1314 | * |
||
1315 | * @param $object |
||
1316 | * @param bool $reset_models |
||
1317 | * @return bool returns true if cached object should be unset |
||
1318 | */ |
||
1319 | private static function _reset_and_unset_object($object, $reset_models) |
||
1339 | |||
1340 | |||
1341 | |||
1342 | /** |
||
1343 | * @override magic methods |
||
1344 | * @return void |
||
1345 | */ |
||
1346 | public final function __destruct() |
||
1349 | |||
1350 | |||
1351 | |||
1352 | /** |
||
1353 | * @param $a |
||
1354 | * @param $b |
||
1355 | */ |
||
1356 | public final function __call($a, $b) |
||
1359 | |||
1360 | |||
1361 | |||
1362 | /** |
||
1363 | * @param $a |
||
1364 | */ |
||
1365 | public final function __get($a) |
||
1368 | |||
1369 | |||
1370 | |||
1371 | /** |
||
1372 | * @param $a |
||
1373 | * @param $b |
||
1374 | */ |
||
1375 | public final function __set($a, $b) |
||
1378 | |||
1379 | |||
1380 | |||
1381 | /** |
||
1382 | * @param $a |
||
1383 | */ |
||
1384 | public final function __isset($a) |
||
1387 | |||
1388 | |||
1389 | |||
1390 | /** |
||
1391 | * @param $a |
||
1392 | */ |
||
1393 | public final function __unset($a) |
||
1396 | |||
1397 | |||
1398 | |||
1399 | /** |
||
1400 | * @return array |
||
1401 | */ |
||
1402 | public final function __sleep() |
||
1406 | |||
1407 | |||
1408 | |||
1409 | public final function __wakeup() |
||
1412 | |||
1413 | |||
1414 | |||
1415 | /** |
||
1416 | * @return string |
||
1417 | */ |
||
1418 | public final function __toString() |
||
1422 | |||
1423 | |||
1424 | |||
1425 | public final function __invoke() |
||
1428 | |||
1429 | |||
1430 | |||
1431 | public final static function __set_state($array = array()) |
||
1435 | |||
1436 | |||
1437 | |||
1438 | public final function __clone() |
||
1441 | |||
1442 | |||
1443 | |||
1444 | /** |
||
1445 | * @param $a |
||
1446 | * @param $b |
||
1447 | */ |
||
1448 | public final static function __callStatic($a, $b) |
||
1451 | |||
1452 | |||
1453 | |||
1454 | /** |
||
1455 | * Gets all the custom post type models defined |
||
1456 | * |
||
1457 | * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
||
1458 | */ |
||
1459 | public function cpt_models() |
||
1469 | |||
1470 | |||
1471 | |||
1472 | /** |
||
1473 | * @return \EE_Config |
||
1474 | */ |
||
1475 | public static function CFG() |
||
1479 | |||
1480 | |||
1481 | } |
||
1482 | // End of file EE_Registry.core.php |
||
1484 |
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):