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 |
||
27 | class EE_Registry implements ResettableInterface |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * @var EE_Registry $_instance |
||
32 | */ |
||
33 | private static $_instance; |
||
34 | |||
35 | /** |
||
36 | * @var EE_Dependency_Map $_dependency_map |
||
37 | */ |
||
38 | protected $_dependency_map; |
||
39 | |||
40 | /** |
||
41 | * @var Mirror |
||
42 | */ |
||
43 | private $mirror; |
||
44 | |||
45 | /** |
||
46 | * @var ClassInterfaceCache $class_cache |
||
47 | */ |
||
48 | private $class_cache; |
||
49 | |||
50 | /** |
||
51 | * @var array $_class_abbreviations |
||
52 | */ |
||
53 | protected $_class_abbreviations = array(); |
||
54 | |||
55 | /** |
||
56 | * @var CommandBusInterface $BUS |
||
57 | */ |
||
58 | public $BUS; |
||
59 | |||
60 | /** |
||
61 | * @var EE_Cart $CART |
||
62 | */ |
||
63 | public $CART; |
||
64 | |||
65 | /** |
||
66 | * @var EE_Config $CFG |
||
67 | */ |
||
68 | public $CFG; |
||
69 | |||
70 | /** |
||
71 | * @var EE_Network_Config $NET_CFG |
||
72 | */ |
||
73 | public $NET_CFG; |
||
74 | |||
75 | /** |
||
76 | * StdClass object for storing library classes in |
||
77 | * |
||
78 | * @var StdClass $LIB |
||
79 | */ |
||
80 | public $LIB; |
||
81 | |||
82 | /** |
||
83 | * @var EE_Request_Handler $REQ |
||
84 | */ |
||
85 | public $REQ; |
||
86 | |||
87 | /** |
||
88 | * @var EE_Session $SSN |
||
89 | */ |
||
90 | public $SSN; |
||
91 | |||
92 | /** |
||
93 | * @since 4.5.0 |
||
94 | * @var EE_Capabilities $CAP |
||
95 | */ |
||
96 | public $CAP; |
||
97 | |||
98 | /** |
||
99 | * @since 4.9.0 |
||
100 | * @var EE_Message_Resource_Manager $MRM |
||
101 | */ |
||
102 | public $MRM; |
||
103 | |||
104 | |||
105 | /** |
||
106 | * @var Registry $AssetsRegistry |
||
107 | */ |
||
108 | public $AssetsRegistry; |
||
109 | |||
110 | /** |
||
111 | * StdClass object for holding addons which have registered themselves to work with EE core |
||
112 | * |
||
113 | * @var EE_Addon[] $addons |
||
114 | */ |
||
115 | public $addons; |
||
116 | |||
117 | /** |
||
118 | * keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
||
119 | * |
||
120 | * @var EEM_Base[] $models |
||
121 | */ |
||
122 | public $models = array(); |
||
123 | |||
124 | /** |
||
125 | * @var EED_Module[] $modules |
||
126 | */ |
||
127 | public $modules; |
||
128 | |||
129 | /** |
||
130 | * @var EES_Shortcode[] $shortcodes |
||
131 | */ |
||
132 | public $shortcodes; |
||
133 | |||
134 | /** |
||
135 | * @var WP_Widget[] $widgets |
||
136 | */ |
||
137 | public $widgets; |
||
138 | |||
139 | /** |
||
140 | * this is an array of all implemented model names (i.e. not the parent abstract models, or models |
||
141 | * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
||
142 | * Keys are model "short names" (eg "Event") as used in model relations, and values are |
||
143 | * classnames (eg "EEM_Event") |
||
144 | * |
||
145 | * @var array $non_abstract_db_models |
||
146 | */ |
||
147 | public $non_abstract_db_models = array(); |
||
148 | |||
149 | |||
150 | /** |
||
151 | * internationalization for JS strings |
||
152 | * usage: EE_Registry::i18n_js_strings['string_key'] = esc_html__( 'string to translate.', 'event_espresso' ); |
||
153 | * in js file: var translatedString = eei18n.string_key; |
||
154 | * |
||
155 | * @var array $i18n_js_strings |
||
156 | */ |
||
157 | public static $i18n_js_strings = array(); |
||
158 | |||
159 | |||
160 | /** |
||
161 | * $main_file - path to espresso.php |
||
162 | * |
||
163 | * @var array $main_file |
||
164 | */ |
||
165 | public $main_file; |
||
166 | |||
167 | /** |
||
168 | * array of ReflectionClass objects where the key is the class name |
||
169 | * |
||
170 | * @deprecated $VID:$ |
||
171 | * @var ReflectionClass[] $_reflectors |
||
172 | */ |
||
173 | public $_reflectors; |
||
174 | |||
175 | /** |
||
176 | * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
||
177 | * |
||
178 | * @var boolean $_cache_on |
||
179 | */ |
||
180 | protected $_cache_on = true; |
||
181 | |||
182 | |||
183 | /** |
||
184 | * @singleton method used to instantiate class object |
||
185 | * @param EE_Dependency_Map|null $dependency_map |
||
186 | * @param Mirror|null $mirror |
||
187 | * @param ClassInterfaceCache|null $class_cache |
||
188 | * @return EE_Registry instance |
||
189 | */ |
||
190 | public static function instance( |
||
191 | EE_Dependency_Map $dependency_map = null, |
||
192 | Mirror $mirror = null, |
||
193 | ClassInterfaceCache $class_cache = null |
||
194 | ) { |
||
195 | // check if class object is instantiated |
||
196 | if ( |
||
197 | ! EE_Registry::$_instance instanceof EE_Registry |
||
198 | && $dependency_map instanceof EE_Dependency_Map |
||
199 | && $mirror instanceof Mirror |
||
200 | && $class_cache instanceof ClassInterfaceCache |
||
201 | ) { |
||
202 | EE_Registry::$_instance = new self($dependency_map, $mirror, $class_cache); |
||
203 | } |
||
204 | return EE_Registry::$_instance; |
||
205 | } |
||
206 | |||
207 | |||
208 | /** |
||
209 | * protected constructor to prevent direct creation |
||
210 | * |
||
211 | * @Constructor |
||
212 | * @param EE_Dependency_Map $dependency_map |
||
213 | * @param Mirror $mirror |
||
214 | * @param ClassInterfaceCache $class_cache |
||
215 | */ |
||
216 | protected function __construct(EE_Dependency_Map $dependency_map, Mirror $mirror, ClassInterfaceCache $class_cache) |
||
217 | { |
||
218 | $this->_dependency_map = $dependency_map; |
||
219 | $this->mirror = $mirror; |
||
220 | $this->class_cache = $class_cache; |
||
221 | // $registry_container = new RegistryContainer(); |
||
222 | $this->LIB = new RegistryContainer(); |
||
223 | $this->addons = new RegistryContainer(); |
||
224 | $this->modules = new RegistryContainer(); |
||
225 | $this->shortcodes = new RegistryContainer(); |
||
226 | $this->widgets = new RegistryContainer(); |
||
227 | add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
||
228 | } |
||
229 | |||
230 | |||
231 | |||
232 | /** |
||
233 | * initialize |
||
234 | * |
||
235 | * @throws EE_Error |
||
236 | * @throws ReflectionException |
||
237 | */ |
||
238 | public function initialize() |
||
239 | { |
||
240 | $this->_class_abbreviations = apply_filters( |
||
241 | 'FHEE__EE_Registry____construct___class_abbreviations', |
||
242 | array( |
||
243 | 'EE_Config' => 'CFG', |
||
244 | 'EE_Session' => 'SSN', |
||
245 | 'EE_Capabilities' => 'CAP', |
||
246 | 'EE_Cart' => 'CART', |
||
247 | 'EE_Network_Config' => 'NET_CFG', |
||
248 | 'EE_Request_Handler' => 'REQ', |
||
249 | 'EE_Message_Resource_Manager' => 'MRM', |
||
250 | 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
||
251 | 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
||
252 | ) |
||
253 | ); |
||
254 | $this->load_core('Base', array(), true); |
||
255 | // add our request and response objects to the cache |
||
256 | $request_loader = $this->_dependency_map->class_loader( |
||
257 | 'EventEspresso\core\services\request\Request' |
||
258 | ); |
||
259 | $this->_set_cached_class( |
||
260 | $request_loader(), |
||
261 | 'EventEspresso\core\services\request\Request' |
||
262 | ); |
||
263 | $response_loader = $this->_dependency_map->class_loader( |
||
264 | 'EventEspresso\core\services\request\Response' |
||
265 | ); |
||
266 | $this->_set_cached_class( |
||
267 | $response_loader(), |
||
268 | 'EventEspresso\core\services\request\Response' |
||
269 | ); |
||
270 | add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
||
271 | } |
||
272 | |||
273 | |||
274 | |||
275 | /** |
||
276 | * @return void |
||
277 | */ |
||
278 | public function init() |
||
279 | { |
||
280 | // Get current page protocol |
||
281 | $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
||
282 | // Output admin-ajax.php URL with same protocol as current page |
||
283 | self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
||
284 | self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
||
285 | } |
||
286 | |||
287 | |||
288 | |||
289 | /** |
||
290 | * localize_i18n_js_strings |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | public static function localize_i18n_js_strings() |
||
295 | { |
||
296 | $i18n_js_strings = (array)self::$i18n_js_strings; |
||
297 | foreach ($i18n_js_strings as $key => $value) { |
||
298 | if (is_scalar($value)) { |
||
299 | $i18n_js_strings[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8'); |
||
300 | } |
||
301 | } |
||
302 | return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
||
303 | } |
||
304 | |||
305 | |||
306 | |||
307 | /** |
||
308 | * @param mixed string | EED_Module $module |
||
309 | * @throws EE_Error |
||
310 | * @throws ReflectionException |
||
311 | */ |
||
312 | public function add_module($module) |
||
324 | |||
325 | |||
326 | |||
327 | /** |
||
328 | * @param string $module_name |
||
329 | * @return mixed EED_Module | NULL |
||
330 | */ |
||
331 | public function get_module($module_name = '') |
||
337 | |||
338 | |||
339 | |||
340 | /** |
||
341 | * loads core classes - must be singletons |
||
342 | * |
||
343 | * @param string $class_name - simple class name ie: session |
||
344 | * @param mixed $arguments |
||
345 | * @param bool $load_only |
||
346 | * @return mixed |
||
347 | * @throws EE_Error |
||
348 | * @throws ReflectionException |
||
349 | */ |
||
350 | public function load_core($class_name, $arguments = array(), $load_only = false) |
||
376 | |||
377 | |||
378 | |||
379 | /** |
||
380 | * loads service classes |
||
381 | * |
||
382 | * @param string $class_name - simple class name ie: session |
||
383 | * @param mixed $arguments |
||
384 | * @param bool $load_only |
||
385 | * @return mixed |
||
386 | * @throws EE_Error |
||
387 | * @throws ReflectionException |
||
388 | */ |
||
389 | public function load_service($class_name, $arguments = array(), $load_only = false) |
||
409 | |||
410 | |||
411 | |||
412 | /** |
||
413 | * loads data_migration_scripts |
||
414 | * |
||
415 | * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
||
416 | * @param mixed $arguments |
||
417 | * @return EE_Data_Migration_Script_Base|mixed |
||
418 | * @throws EE_Error |
||
419 | * @throws ReflectionException |
||
420 | */ |
||
421 | public function load_dms($class_name, $arguments = array()) |
||
434 | |||
435 | |||
436 | |||
437 | /** |
||
438 | * loads object creating classes - must be singletons |
||
439 | * |
||
440 | * @param string $class_name - simple class name ie: attendee |
||
441 | * @param mixed $arguments - an array of arguments to pass to the class |
||
442 | * @param bool $from_db - some classes are instantiated from the db and thus call a different method to |
||
443 | * instantiate |
||
444 | * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then |
||
445 | * set this to FALSE (ie. when instantiating model objects from client in a loop) |
||
446 | * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate |
||
447 | * (default) |
||
448 | * @return EE_Base_Class | bool |
||
449 | * @throws EE_Error |
||
450 | * @throws ReflectionException |
||
451 | */ |
||
452 | public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
||
473 | |||
474 | |||
475 | |||
476 | /** |
||
477 | * loads helper classes - must be singletons |
||
478 | * |
||
479 | * @param string $class_name - simple class name ie: price |
||
480 | * @param mixed $arguments |
||
481 | * @param bool $load_only |
||
482 | * @return EEH_Base | bool |
||
483 | * @throws EE_Error |
||
484 | * @throws ReflectionException |
||
485 | */ |
||
486 | public function load_helper($class_name, $arguments = array(), $load_only = true) |
||
502 | |||
503 | |||
504 | |||
505 | /** |
||
506 | * loads core classes - must be singletons |
||
507 | * |
||
508 | * @param string $class_name - simple class name ie: session |
||
509 | * @param mixed $arguments |
||
510 | * @param bool $load_only |
||
511 | * @param bool $cache whether to cache the object or not. |
||
512 | * @return mixed |
||
513 | * @throws EE_Error |
||
514 | * @throws ReflectionException |
||
515 | */ |
||
516 | public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
||
537 | |||
538 | |||
539 | |||
540 | /** |
||
541 | * loads model classes - must be singletons |
||
542 | * |
||
543 | * @param string $class_name - simple class name ie: price |
||
544 | * @param mixed $arguments |
||
545 | * @param bool $load_only |
||
546 | * @return EEM_Base | bool |
||
547 | * @throws EE_Error |
||
548 | * @throws ReflectionException |
||
549 | */ |
||
550 | public function load_model($class_name, $arguments = array(), $load_only = false) |
||
570 | |||
571 | |||
572 | |||
573 | /** |
||
574 | * loads model classes - must be singletons |
||
575 | * |
||
576 | * @param string $class_name - simple class name ie: price |
||
577 | * @param mixed $arguments |
||
578 | * @param bool $load_only |
||
579 | * @return mixed | bool |
||
580 | * @throws EE_Error |
||
581 | * @throws ReflectionException |
||
582 | */ |
||
583 | public function load_model_class($class_name, $arguments = array(), $load_only = true) |
||
603 | |||
604 | |||
605 | |||
606 | /** |
||
607 | * Determines if $model_name is the name of an actual EE model. |
||
608 | * |
||
609 | * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
||
610 | * @return boolean |
||
611 | */ |
||
612 | public function is_model_name($model_name) |
||
616 | |||
617 | |||
618 | |||
619 | /** |
||
620 | * generic class loader |
||
621 | * |
||
622 | * @param string $path_to_file - directory path to file location, not including filename |
||
623 | * @param string $file_name - file name ie: my_file.php, including extension |
||
624 | * @param string $type - file type - core? class? helper? model? |
||
625 | * @param mixed $arguments |
||
626 | * @param bool $load_only |
||
627 | * @return mixed |
||
628 | * @throws EE_Error |
||
629 | * @throws ReflectionException |
||
630 | */ |
||
631 | public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
||
645 | |||
646 | |||
647 | |||
648 | /** |
||
649 | * @param string $path_to_file - directory path to file location, not including filename |
||
650 | * @param string $class_name - full class name ie: My_Class |
||
651 | * @param string $type - file type - core? class? helper? model? |
||
652 | * @param mixed $arguments |
||
653 | * @param bool $load_only |
||
654 | * @return bool|EE_Addon|object |
||
655 | * @throws EE_Error |
||
656 | * @throws ReflectionException |
||
657 | */ |
||
658 | public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
||
672 | |||
673 | |||
674 | /** |
||
675 | * instantiates, caches, and automatically resolves dependencies |
||
676 | * for classes that use a Fully Qualified Class Name. |
||
677 | * if the class is not capable of being loaded using PSR-4 autoloading, |
||
678 | * then you need to use one of the existing load_*() methods |
||
679 | * which can resolve the classname and filepath from the passed arguments |
||
680 | * |
||
681 | * @param bool|string $class_name Fully Qualified Class Name |
||
682 | * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
||
683 | * @param bool $cache whether to cache the instantiated object for reuse |
||
684 | * @param bool $from_db some classes are instantiated from the db |
||
685 | * and thus call a different method to instantiate |
||
686 | * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
||
687 | * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
||
688 | * @return bool|null|mixed null = failure to load or instantiate class object. |
||
689 | * object = class loaded and instantiated successfully. |
||
690 | * bool = fail or success when $load_only is true |
||
691 | * @throws InvalidInterfaceException |
||
692 | * @throws InvalidDataTypeException |
||
693 | * @throws InvalidClassException |
||
694 | * @throws EE_Error |
||
695 | * @throws ReflectionException |
||
696 | */ |
||
697 | public function create( |
||
749 | |||
750 | |||
751 | |||
752 | /** |
||
753 | * Recursively checks that a class exists and potentially attempts to load classes with non-FQCNs |
||
754 | * |
||
755 | * @param string $class_name |
||
756 | * @param array $arguments |
||
757 | * @param int $attempt |
||
758 | * @return mixed |
||
759 | */ |
||
760 | private function loadOrVerifyClassExists($class_name, array $arguments, $attempt = 1) { |
||
785 | |||
786 | |||
787 | |||
788 | /** |
||
789 | * instantiates, caches, and injects dependencies for classes |
||
790 | * |
||
791 | * @param array $file_paths an array of paths to folders to look in |
||
792 | * @param string $class_prefix EE or EEM or... ??? |
||
793 | * @param bool|string $class_name $class name |
||
794 | * @param string $type file type - core? class? helper? model? |
||
795 | * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
||
796 | * @param bool $from_db some classes are instantiated from the db |
||
797 | * and thus call a different method to instantiate |
||
798 | * @param bool $cache whether to cache the instantiated object for reuse |
||
799 | * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
||
800 | * @return bool|null|object null = failure to load or instantiate class object. |
||
801 | * object = class loaded and instantiated successfully. |
||
802 | * bool = fail or success when $load_only is true |
||
803 | * @throws EE_Error |
||
804 | * @throws ReflectionException |
||
805 | * @throws InvalidInterfaceException |
||
806 | * @throws InvalidDataTypeException |
||
807 | * @throws InvalidClassException |
||
808 | */ |
||
809 | protected function _load( |
||
868 | |||
869 | |||
870 | |||
871 | /** |
||
872 | * @param string $class_name |
||
873 | * @param string $default have to specify something, but not anything that will conflict |
||
874 | * @return mixed|string |
||
875 | */ |
||
876 | protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
||
882 | |||
883 | /** |
||
884 | * attempts to find a cached version of the requested class |
||
885 | * by looking in the following places: |
||
886 | * $this->{$class_abbreviation} ie: $this->CART |
||
887 | * $this->{$class_name} ie: $this->Some_Class |
||
888 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
889 | * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
||
890 | * |
||
891 | * @param string $class_name |
||
892 | * @param string $class_prefix |
||
893 | * @return mixed |
||
894 | */ |
||
895 | protected function _get_cached_class($class_name, $class_prefix = '') |
||
917 | |||
918 | |||
919 | |||
920 | /** |
||
921 | * removes a cached version of the requested class |
||
922 | * |
||
923 | * @param string $class_name |
||
924 | * @param boolean $addon |
||
925 | * @return boolean |
||
926 | */ |
||
927 | public function clear_cached_class($class_name, $addon = false) |
||
950 | |||
951 | |||
952 | |||
953 | /** |
||
954 | * attempts to find a full valid filepath for the requested class. |
||
955 | * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
||
956 | * then returns that path if the target file has been found and is readable |
||
957 | * |
||
958 | * @param string $class_name |
||
959 | * @param string $type |
||
960 | * @param array $file_paths |
||
961 | * @return string | bool |
||
962 | */ |
||
963 | protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
||
988 | |||
989 | |||
990 | |||
991 | /** |
||
992 | * basically just performs a require_once() |
||
993 | * but with some error handling |
||
994 | * |
||
995 | * @param string $path |
||
996 | * @param string $class_name |
||
997 | * @param string $type |
||
998 | * @param array $file_paths |
||
999 | * @return bool |
||
1000 | * @throws EE_Error |
||
1001 | * @throws ReflectionException |
||
1002 | */ |
||
1003 | protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
||
1051 | |||
1052 | |||
1053 | |||
1054 | /** |
||
1055 | * Some of our legacy classes that extended a parent class would simply use a require() statement |
||
1056 | * before their class declaration in order to ensure that the parent class was loaded. |
||
1057 | * This is not ideal, but it's nearly impossible to determine the parent class of a non-namespaced class, |
||
1058 | * without triggering a fatal error because the parent class has yet to be loaded and therefore doesn't exist. |
||
1059 | * |
||
1060 | * @param string $class_name |
||
1061 | */ |
||
1062 | protected function resolve_legacy_class_parent($class_name = '') |
||
1074 | |||
1075 | |||
1076 | /** |
||
1077 | * _create_object |
||
1078 | * Attempts to instantiate the requested class via any of the |
||
1079 | * commonly used instantiation methods employed throughout EE. |
||
1080 | * The priority for instantiation is as follows: |
||
1081 | * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
||
1082 | * - model objects via their 'new_instance_from_db' method |
||
1083 | * - model objects via their 'new_instance' method |
||
1084 | * - "singleton" classes" via their 'instance' method |
||
1085 | * - standard instantiable classes via their __constructor |
||
1086 | * Prior to instantiation, if the classname exists in the dependency_map, |
||
1087 | * then the constructor for the requested class will be examined to determine |
||
1088 | * if any dependencies exist, and if they can be injected. |
||
1089 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
1090 | * |
||
1091 | * @param string $class_name |
||
1092 | * @param array $arguments |
||
1093 | * @param string $type |
||
1094 | * @param bool $from_db |
||
1095 | * @return null|object |
||
1096 | * @throws EE_Error |
||
1097 | * @throws ReflectionException |
||
1098 | * @throws InvalidDataTypeException |
||
1099 | */ |
||
1100 | protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
||
1158 | |||
1159 | |||
1160 | |||
1161 | /** |
||
1162 | * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
||
1163 | * @param array $array |
||
1164 | * @return bool |
||
1165 | */ |
||
1166 | protected function _array_is_numerically_and_sequentially_indexed(array $array) |
||
1172 | |||
1173 | |||
1174 | /** |
||
1175 | * _resolve_dependencies |
||
1176 | * examines the constructor for the requested class to determine |
||
1177 | * if any dependencies exist, and if they can be injected. |
||
1178 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
1179 | * PLZ NOTE: this is achieved by type hinting the constructor params |
||
1180 | * For example: |
||
1181 | * if attempting to load a class "Foo" with the following constructor: |
||
1182 | * __construct( Bar $bar_class, Fighter $grohl_class ) |
||
1183 | * then $bar_class and $grohl_class will be added to the $arguments array, |
||
1184 | * but only IF they are NOT already present in the incoming arguments array, |
||
1185 | * and the correct classes can be loaded |
||
1186 | * |
||
1187 | * @param ReflectionClass $reflector |
||
1188 | * @param string $class_name |
||
1189 | * @param array $arguments |
||
1190 | * @return array |
||
1191 | * @throws EE_Error |
||
1192 | * @throws InvalidArgumentException |
||
1193 | * @throws InvalidDataTypeException |
||
1194 | * @throws InvalidInterfaceException |
||
1195 | * @throws ReflectionException |
||
1196 | * @throws InvalidClassException |
||
1197 | */ |
||
1198 | protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, array $arguments = array()) |
||
1270 | |||
1271 | |||
1272 | |||
1273 | /** |
||
1274 | * @param string $class_name |
||
1275 | * @param string $param_class |
||
1276 | * @param array $arguments |
||
1277 | * @param mixed $index |
||
1278 | * @param array $argument_keys |
||
1279 | * @return array |
||
1280 | * @throws EE_Error |
||
1281 | * @throws ReflectionException |
||
1282 | * @throws InvalidArgumentException |
||
1283 | * @throws InvalidInterfaceException |
||
1284 | * @throws InvalidDataTypeException |
||
1285 | */ |
||
1286 | protected function _resolve_dependency($class_name, $param_class, $arguments, $index, array $argument_keys) |
||
1331 | |||
1332 | |||
1333 | |||
1334 | /** |
||
1335 | * _set_cached_class |
||
1336 | * attempts to cache the instantiated class locally |
||
1337 | * in one of the following places, in the following order: |
||
1338 | * $this->{class_abbreviation} ie: $this->CART |
||
1339 | * $this->{$class_name} ie: $this->Some_Class |
||
1340 | * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
||
1341 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
1342 | * |
||
1343 | * @param object $class_obj |
||
1344 | * @param string $class_name |
||
1345 | * @param string $class_prefix |
||
1346 | * @param bool $from_db |
||
1347 | * @return void |
||
1348 | */ |
||
1349 | protected function _set_cached_class($class_obj, $class_name, $class_prefix = '', $from_db = false) |
||
1373 | |||
1374 | |||
1375 | |||
1376 | /** |
||
1377 | * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
||
1378 | * |
||
1379 | * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
||
1380 | * in the EE_Dependency_Map::$_class_loaders array, |
||
1381 | * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
||
1382 | * @param array $arguments |
||
1383 | * @return object |
||
1384 | */ |
||
1385 | public static function factory($classname, $arguments = array()) |
||
1396 | |||
1397 | |||
1398 | |||
1399 | /** |
||
1400 | * Gets the addon by its class name |
||
1401 | * |
||
1402 | * @param string $class_name |
||
1403 | * @return EE_Addon |
||
1404 | */ |
||
1405 | public function getAddon($class_name) |
||
1410 | |||
1411 | |||
1412 | /** |
||
1413 | * removes the addon from the internal cache |
||
1414 | * |
||
1415 | * @param string $class_name |
||
1416 | * @return void |
||
1417 | */ |
||
1418 | public function removeAddon($class_name) |
||
1423 | |||
1424 | |||
1425 | |||
1426 | /** |
||
1427 | * Gets the addon by its name/slug (not classname. For that, just |
||
1428 | * use the get_addon() method above |
||
1429 | * |
||
1430 | * @param string $name |
||
1431 | * @return EE_Addon |
||
1432 | */ |
||
1433 | public function get_addon_by_name($name) |
||
1442 | |||
1443 | |||
1444 | |||
1445 | /** |
||
1446 | * Gets an array of all the registered addons, where the keys are their names. |
||
1447 | * (ie, what each returns for their name() function) |
||
1448 | * They're already available on EE_Registry::instance()->addons as properties, |
||
1449 | * where each property's name is the addon's classname, |
||
1450 | * So if you just want to get the addon by classname, |
||
1451 | * OR use the get_addon() method above. |
||
1452 | * PLEASE NOTE: |
||
1453 | * addons with Fully Qualified Class Names |
||
1454 | * have had the namespace separators converted to underscores, |
||
1455 | * so a classname like Fully\Qualified\ClassName |
||
1456 | * would have been converted to Fully_Qualified_ClassName |
||
1457 | * |
||
1458 | * @return EE_Addon[] where the KEYS are the addon's name() |
||
1459 | */ |
||
1460 | public function get_addons_by_name() |
||
1468 | |||
1469 | |||
1470 | /** |
||
1471 | * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
||
1472 | * a stale copy of it around |
||
1473 | * |
||
1474 | * @param string $model_name |
||
1475 | * @return \EEM_Base |
||
1476 | * @throws \EE_Error |
||
1477 | */ |
||
1478 | public function reset_model($model_name) |
||
1497 | |||
1498 | |||
1499 | |||
1500 | /** |
||
1501 | * Resets the registry. |
||
1502 | * The criteria for what gets reset is based on what can be shared between sites on the same request when |
||
1503 | * switch_to_blog is used in a multisite install. Here is a list of things that are NOT reset. |
||
1504 | * - $_dependency_map |
||
1505 | * - $_class_abbreviations |
||
1506 | * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
||
1507 | * - $REQ: Still on the same request so no need to change. |
||
1508 | * - $CAP: There is no site specific state in the EE_Capability class. |
||
1509 | * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only |
||
1510 | * one Session can be active in a single request. Resetting could resolve in "headers already sent" errors. |
||
1511 | * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
||
1512 | * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
||
1513 | * switch or on the restore. |
||
1514 | * - $modules |
||
1515 | * - $shortcodes |
||
1516 | * - $widgets |
||
1517 | * |
||
1518 | * @param boolean $hard [deprecated] |
||
1519 | * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
||
1520 | * or just reset without re-instantiating (handy to set to FALSE if you're not |
||
1521 | * sure if you CAN currently reinstantiate the singletons at the moment) |
||
1522 | * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so |
||
1523 | * client |
||
1524 | * code instead can just change the model context to a different blog id if |
||
1525 | * necessary |
||
1526 | * @return EE_Registry |
||
1527 | * @throws EE_Error |
||
1528 | * @throws ReflectionException |
||
1529 | */ |
||
1530 | public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
||
1553 | |||
1554 | |||
1555 | |||
1556 | /** |
||
1557 | * if passed object implements ResettableInterface, then call it's reset() method |
||
1558 | * if passed object implements InterminableInterface, then return false, |
||
1559 | * to indicate that it should NOT be cleared from the Registry cache |
||
1560 | * |
||
1561 | * @param $object |
||
1562 | * @param bool $reset_models |
||
1563 | * @return bool returns true if cached object should be unset |
||
1564 | */ |
||
1565 | private static function _reset_and_unset_object($object, $reset_models) |
||
1592 | |||
1593 | |||
1594 | |||
1595 | /** |
||
1596 | * Gets all the custom post type models defined |
||
1597 | * |
||
1598 | * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
||
1599 | */ |
||
1600 | public function cpt_models() |
||
1610 | |||
1611 | |||
1612 | |||
1613 | /** |
||
1614 | * @return \EE_Config |
||
1615 | */ |
||
1616 | public static function CFG() |
||
1620 | |||
1621 | |||
1622 | /** |
||
1623 | * @deprecated $VID:$ |
||
1624 | * @param string $class_name |
||
1625 | * @return ReflectionClass |
||
1626 | * @throws ReflectionException |
||
1627 | * @throws InvalidDataTypeException |
||
1628 | */ |
||
1629 | public function get_ReflectionClass($class_name) |
||
1633 | } |
||
1634 | // End of file EE_Registry.core.php |
||
1636 |