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 |
||
| 16 | class EE_Registry implements ResettableInterface { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * EE_Registry Object |
||
| 20 | * |
||
| 21 | * @var EE_Registry $_instance |
||
| 22 | * @access private |
||
| 23 | */ |
||
| 24 | private static $_instance = null; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var EE_Dependency_Map $_dependency_map |
||
| 28 | * @access protected |
||
| 29 | */ |
||
| 30 | protected $_dependency_map = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array $_class_abbreviations |
||
| 34 | * @access protected |
||
| 35 | */ |
||
| 36 | protected $_class_abbreviations = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @access public |
||
| 40 | * @var \EventEspresso\core\services\commands\CommandBusInterface $BUS |
||
| 41 | */ |
||
| 42 | public $BUS; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * EE_Cart Object |
||
| 46 | * @access public |
||
| 47 | * @var EE_Cart $CART |
||
| 48 | */ |
||
| 49 | public $CART = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * EE_Config Object |
||
| 53 | * @access public |
||
| 54 | * @var EE_Config $CFG |
||
| 55 | */ |
||
| 56 | public $CFG = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * EE_Network_Config Object |
||
| 60 | * @access public |
||
| 61 | * @var EE_Network_Config $NET_CFG |
||
| 62 | */ |
||
| 63 | public $NET_CFG = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * StdClass object for storing library classes in |
||
| 67 | * |
||
| 68 | * @public LIB |
||
| 69 | * @var StdClass $LIB |
||
| 70 | */ |
||
| 71 | public $LIB = null; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * EE_Request_Handler Object |
||
| 75 | * @access public |
||
| 76 | * @var EE_Request_Handler $REQ |
||
| 77 | */ |
||
| 78 | public $REQ = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * EE_Session Object |
||
| 82 | * @access public |
||
| 83 | * @var EE_Session $SSN |
||
| 84 | */ |
||
| 85 | public $SSN = null; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * holds the ee capabilities object. |
||
| 89 | * |
||
| 90 | * @since 4.5.0 |
||
| 91 | * |
||
| 92 | * @var EE_Capabilities |
||
| 93 | */ |
||
| 94 | public $CAP = null; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * holds the EE_Message_Resource_Manager object. |
||
| 98 | * |
||
| 99 | * @since 4.9.0 |
||
| 100 | * |
||
| 101 | * @var EE_Message_Resource_Manager |
||
| 102 | */ |
||
| 103 | public $MRM = null; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * $addons - StdClass object for holding addons which have registered themselves to work with EE core |
||
| 107 | * @access public |
||
| 108 | * @var EE_Addon[] |
||
| 109 | */ |
||
| 110 | public $addons = null; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * $models |
||
| 114 | * @access public |
||
| 115 | * @var EEM_Base[] $models keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
||
| 116 | */ |
||
| 117 | public $models = array(); |
||
| 118 | |||
| 119 | /** |
||
| 120 | * $modules |
||
| 121 | * @access public |
||
| 122 | * @var EED_Module[] $modules |
||
| 123 | */ |
||
| 124 | public $modules = null; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * $shortcodes |
||
| 128 | * @access public |
||
| 129 | * @var EES_Shortcode[] $shortcodes |
||
| 130 | */ |
||
| 131 | public $shortcodes = null; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * $widgets |
||
| 135 | * @access public |
||
| 136 | * @var WP_Widget[] $widgets |
||
| 137 | */ |
||
| 138 | public $widgets = null; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * $non_abstract_db_models |
||
| 142 | * @access public |
||
| 143 | * @var array this is an array of all implemented model names (i.e. not the parent abstract models, or models |
||
| 144 | * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
||
| 145 | * Keys are model "short names" (eg "Event") as used in model relations, and values are |
||
| 146 | * classnames (eg "EEM_Event") |
||
| 147 | */ |
||
| 148 | public $non_abstract_db_models = array(); |
||
| 149 | |||
| 150 | /** |
||
| 151 | * $i18n_js_strings - internationalization for JS strings |
||
| 152 | * usage: EE_Registry::i18n_js_strings['string_key'] = __( 'string to translate.', 'event_espresso' ); |
||
| 153 | * in js file: var translatedString = eei18n.string_key; |
||
| 154 | * |
||
| 155 | * @access public |
||
| 156 | * @var array |
||
| 157 | */ |
||
| 158 | public static $i18n_js_strings = array(); |
||
| 159 | |||
| 160 | /** |
||
| 161 | * $main_file - path to espresso.php |
||
| 162 | * |
||
| 163 | * @access public |
||
| 164 | * @var array |
||
| 165 | */ |
||
| 166 | public $main_file; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * array of ReflectionClass objects where the key is the class name |
||
| 170 | * |
||
| 171 | * @access public |
||
| 172 | * @var ReflectionClass[] |
||
| 173 | */ |
||
| 174 | public $_reflectors; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
||
| 178 | * |
||
| 179 | * @access protected |
||
| 180 | * @var boolean $_cache_on |
||
| 181 | */ |
||
| 182 | protected $_cache_on = true; |
||
| 183 | |||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * @singleton method used to instantiate class object |
||
| 188 | * @access public |
||
| 189 | * @param \EE_Dependency_Map $dependency_map |
||
| 190 | * @return \EE_Registry instance |
||
| 191 | */ |
||
| 192 | public static function instance( \EE_Dependency_Map $dependency_map = null ) { |
||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | *protected constructor to prevent direct creation |
||
| 204 | * |
||
| 205 | * @Constructor |
||
| 206 | * @access protected |
||
| 207 | * @param \EE_Dependency_Map $dependency_map |
||
| 208 | * @return \EE_Registry |
||
| 209 | */ |
||
| 210 | protected function __construct( \EE_Dependency_Map $dependency_map ) { |
||
| 214 | |||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * initialize |
||
| 219 | */ |
||
| 220 | public function initialize() { |
||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * init |
||
| 259 | * |
||
| 260 | * @access public |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | public function init() { |
||
| 270 | |||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * localize_i18n_js_strings |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public static function localize_i18n_js_strings() { |
||
| 288 | |||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * @param mixed string | EED_Module $module |
||
| 293 | */ |
||
| 294 | public function add_module( $module ) { |
||
| 305 | |||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * @param string $module_name |
||
| 310 | * @return mixed EED_Module | NULL |
||
| 311 | */ |
||
| 312 | public function get_module( $module_name = '' ) { |
||
| 315 | |||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * loads core classes - must be singletons |
||
| 320 | * |
||
| 321 | * @access public |
||
| 322 | * @param string $class_name - simple class name ie: session |
||
| 323 | * @param mixed $arguments |
||
| 324 | * @param bool $load_only |
||
| 325 | * @return mixed |
||
| 326 | */ |
||
| 327 | public function load_core( $class_name, $arguments = array(), $load_only = false ) { |
||
| 342 | |||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * loads service classes |
||
| 347 | * |
||
| 348 | * @access public |
||
| 349 | * @param string $class_name - simple class name ie: session |
||
| 350 | * @param mixed $arguments |
||
| 351 | * @param bool $load_only |
||
| 352 | * @return mixed |
||
| 353 | */ |
||
| 354 | public function load_service( $class_name, $arguments = array(), $load_only = false ) { |
||
| 364 | |||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * loads data_migration_scripts |
||
| 369 | * |
||
| 370 | * @access public |
||
| 371 | * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
||
| 372 | * @param mixed $arguments |
||
| 373 | * @return EE_Data_Migration_Script_Base|mixed |
||
| 374 | */ |
||
| 375 | public function load_dms( $class_name, $arguments = array() ) { |
||
| 379 | |||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * loads object creating classes - must be singletons |
||
| 384 | * |
||
| 385 | * @param string $class_name - simple class name ie: attendee |
||
| 386 | * @param mixed $arguments - an array of arguments to pass to the class |
||
| 387 | * @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
||
| 388 | * @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) |
||
| 389 | * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate (default) |
||
| 390 | * @return EE_Base_Class | bool |
||
| 391 | */ |
||
| 392 | public function load_class( $class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false ) { |
||
| 401 | |||
| 402 | |||
| 403 | |||
| 404 | /** |
||
| 405 | * loads helper classes - must be singletons |
||
| 406 | * |
||
| 407 | * @param string $class_name - simple class name ie: price |
||
| 408 | * @param mixed $arguments |
||
| 409 | * @param bool $load_only |
||
| 410 | * @return EEH_Base | bool |
||
| 411 | */ |
||
| 412 | public function load_helper( $class_name, $arguments = array(), $load_only = true ) { |
||
| 418 | |||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * loads core classes - must be singletons |
||
| 423 | * |
||
| 424 | * @access public |
||
| 425 | * @param string $class_name - simple class name ie: session |
||
| 426 | * @param mixed $arguments |
||
| 427 | * @param bool $load_only |
||
| 428 | * @param bool $cache whether to cache the object or not. |
||
| 429 | * @return mixed |
||
| 430 | */ |
||
| 431 | public function load_lib( $class_name, $arguments = array(), $load_only = false, $cache = true ) { |
||
| 442 | |||
| 443 | |||
| 444 | |||
| 445 | /** |
||
| 446 | * loads model classes - must be singletons |
||
| 447 | * |
||
| 448 | * @param string $class_name - simple class name ie: price |
||
| 449 | * @param mixed $arguments |
||
| 450 | * @param bool $load_only |
||
| 451 | * @return EEM_Base | bool |
||
| 452 | */ |
||
| 453 | public function load_model( $class_name, $arguments = array(), $load_only = false ) { |
||
| 461 | |||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * loads model classes - must be singletons |
||
| 466 | * |
||
| 467 | * @param string $class_name - simple class name ie: price |
||
| 468 | * @param mixed $arguments |
||
| 469 | * @param bool $load_only |
||
| 470 | * @return mixed | bool |
||
| 471 | */ |
||
| 472 | public function load_model_class( $class_name, $arguments = array(), $load_only = true ) { |
||
| 482 | |||
| 483 | |||
| 484 | |||
| 485 | /** |
||
| 486 | * Determines if $model_name is the name of an actual EE model. |
||
| 487 | * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
||
| 488 | * @return boolean |
||
| 489 | */ |
||
| 490 | public function is_model_name( $model_name ) { |
||
| 493 | |||
| 494 | |||
| 495 | |||
| 496 | /** |
||
| 497 | * generic class loader |
||
| 498 | * |
||
| 499 | * @param string $path_to_file - directory path to file location, not including filename |
||
| 500 | * @param string $file_name - file name ie: my_file.php, including extension |
||
| 501 | * @param string $type - file type - core? class? helper? model? |
||
| 502 | * @param mixed $arguments |
||
| 503 | * @param bool $load_only |
||
| 504 | * @return mixed |
||
| 505 | */ |
||
| 506 | public function load_file( $path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true ) { |
||
| 510 | |||
| 511 | |||
| 512 | |||
| 513 | /** |
||
| 514 | * load_addon |
||
| 515 | * |
||
| 516 | * @param string $path_to_file - directory path to file location, not including filename |
||
| 517 | * @param string $class_name - full class name ie: My_Class |
||
| 518 | * @param string $type - file type - core? class? helper? model? |
||
| 519 | * @param mixed $arguments |
||
| 520 | * @param bool $load_only |
||
| 521 | * @return EE_Addon |
||
| 522 | */ |
||
| 523 | public function load_addon( $path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false ) { |
||
| 527 | |||
| 528 | |||
| 529 | |||
| 530 | /** |
||
| 531 | * instantiates, caches, and automatically resolves dependencies |
||
| 532 | * for classes that use a Fully Qualified Class Name. |
||
| 533 | * if the class is not capable of being loaded using PSR-4 autoloading, |
||
| 534 | * then you need to use one of the existing load_*() methods |
||
| 535 | * which can resolve the classname and filepath from the passed arguments |
||
| 536 | * |
||
| 537 | * @param bool|string $class_name Fully Qualified Class Name |
||
| 538 | * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
||
| 539 | * @param bool $cache whether to cache the instantiated object for reuse |
||
| 540 | * @param bool $from_db some classes are instantiated from the db |
||
| 541 | * and thus call a different method to instantiate |
||
| 542 | * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
||
| 543 | * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
||
| 544 | * @return mixed null = failure to load or instantiate class object. |
||
| 545 | * object = class loaded and instantiated successfully. |
||
| 546 | * bool = fail or success when $load_only is true |
||
| 547 | */ |
||
| 548 | public function create( |
||
| 589 | |||
| 590 | |||
| 591 | |||
| 592 | /** |
||
| 593 | * instantiates, caches, and injects dependencies for classes |
||
| 594 | * |
||
| 595 | * @param array $file_paths an array of paths to folders to look in |
||
| 596 | * @param string $class_prefix EE or EEM or... ??? |
||
| 597 | * @param bool|string $class_name $class name |
||
| 598 | * @param string $type file type - core? class? helper? model? |
||
| 599 | * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
||
| 600 | * @param bool $from_db some classes are instantiated from the db |
||
| 601 | * and thus call a different method to instantiate |
||
| 602 | * @param bool $cache whether to cache the instantiated object for reuse |
||
| 603 | * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
||
| 604 | * @return null|object|bool null = failure to load or instantiate class object. |
||
| 605 | * object = class loaded and instantiated successfully. |
||
| 606 | * bool = fail or success when $load_only is true |
||
| 607 | */ |
||
| 608 | protected function _load( |
||
| 664 | |||
| 665 | |||
| 666 | |||
| 667 | /** |
||
| 668 | * _get_cached_class |
||
| 669 | * |
||
| 670 | * attempts to find a cached version of the requested class |
||
| 671 | * by looking in the following places: |
||
| 672 | * $this->{$class_abbreviation} ie: $this->CART |
||
| 673 | * $this->{$class_name} ie: $this->Some_Class |
||
| 674 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
| 675 | * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
||
| 676 | * |
||
| 677 | * @access protected |
||
| 678 | * @param string $class_name |
||
| 679 | * @param string $class_prefix |
||
| 680 | * @return mixed |
||
| 681 | */ |
||
| 682 | protected function _get_cached_class( $class_name, $class_prefix = '' ) { |
||
| 704 | |||
| 705 | |||
| 706 | |||
| 707 | /** |
||
| 708 | * _resolve_path |
||
| 709 | * |
||
| 710 | * attempts to find a full valid filepath for the requested class. |
||
| 711 | * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
||
| 712 | * then returns that path if the target file has been found and is readable |
||
| 713 | * |
||
| 714 | * @access protected |
||
| 715 | * @param string $class_name |
||
| 716 | * @param string $type |
||
| 717 | * @param array $file_paths |
||
| 718 | * @return string | bool |
||
| 719 | */ |
||
| 720 | protected function _resolve_path( $class_name, $type = '', $file_paths = array() ) { |
||
| 738 | |||
| 739 | |||
| 740 | |||
| 741 | /** |
||
| 742 | * _require_file |
||
| 743 | * |
||
| 744 | * basically just performs a require_once() |
||
| 745 | * but with some error handling |
||
| 746 | * |
||
| 747 | * @access protected |
||
| 748 | * @param string $path |
||
| 749 | * @param string $class_name |
||
| 750 | * @param string $type |
||
| 751 | * @param array $file_paths |
||
| 752 | * @return boolean |
||
| 753 | * @throws \EE_Error |
||
| 754 | */ |
||
| 755 | protected function _require_file( $path, $class_name, $type = '', $file_paths = array() ) { |
||
| 791 | |||
| 792 | |||
| 793 | |||
| 794 | /** |
||
| 795 | * _create_object |
||
| 796 | * Attempts to instantiate the requested class via any of the |
||
| 797 | * commonly used instantiation methods employed throughout EE. |
||
| 798 | * The priority for instantiation is as follows: |
||
| 799 | * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
||
| 800 | * - model objects via their 'new_instance_from_db' method |
||
| 801 | * - model objects via their 'new_instance' method |
||
| 802 | * - "singleton" classes" via their 'instance' method |
||
| 803 | * - standard instantiable classes via their __constructor |
||
| 804 | * Prior to instantiation, if the classname exists in the dependency_map, |
||
| 805 | * then the constructor for the requested class will be examined to determine |
||
| 806 | * if any dependencies exist, and if they can be injected. |
||
| 807 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
| 808 | * |
||
| 809 | * @access protected |
||
| 810 | * @param string $class_name |
||
| 811 | * @param array $arguments |
||
| 812 | * @param string $type |
||
| 813 | * @param bool $from_db |
||
| 814 | * @return null | object |
||
| 815 | * @throws \EE_Error |
||
| 816 | */ |
||
| 817 | protected function _create_object( $class_name, $arguments = array(), $type = '', $from_db = false ) { |
||
| 883 | |||
| 884 | |||
| 885 | |||
| 886 | /** |
||
| 887 | * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
||
| 888 | * @param array $array |
||
| 889 | * @return bool |
||
| 890 | */ |
||
| 891 | protected function _array_is_numerically_and_sequentially_indexed( array $array ) { |
||
| 894 | |||
| 895 | |||
| 896 | |||
| 897 | /** |
||
| 898 | * getReflectionClass |
||
| 899 | * |
||
| 900 | * checks if a ReflectionClass object has already been generated for a class |
||
| 901 | * and returns that instead of creating a new one |
||
| 902 | * |
||
| 903 | * @access public |
||
| 904 | * @param string $class_name |
||
| 905 | * @return ReflectionClass |
||
| 906 | */ |
||
| 907 | View Code Duplication | public function get_ReflectionClass( $class_name ) { |
|
| 916 | |||
| 917 | |||
| 918 | |||
| 919 | /** |
||
| 920 | * _resolve_dependencies |
||
| 921 | * examines the constructor for the requested class to determine |
||
| 922 | * if any dependencies exist, and if they can be injected. |
||
| 923 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
| 924 | * PLZ NOTE: this is achieved by type hinting the constructor params |
||
| 925 | * For example: |
||
| 926 | * if attempting to load a class "Foo" with the following constructor: |
||
| 927 | * __construct( Bar $bar_class, Fighter $grohl_class ) |
||
| 928 | * then $bar_class and $grohl_class will be added to the $arguments array, |
||
| 929 | * but only IF they are NOT already present in the incoming arguments array, |
||
| 930 | * and the correct classes can be loaded |
||
| 931 | * |
||
| 932 | * @access protected |
||
| 933 | * @param ReflectionClass $reflector |
||
| 934 | * @param string $class_name |
||
| 935 | * @param array $arguments |
||
| 936 | * @return array |
||
| 937 | * @throws \ReflectionException |
||
| 938 | */ |
||
| 939 | protected function _resolve_dependencies( ReflectionClass $reflector, $class_name, $arguments = array() ) { |
||
| 993 | |||
| 994 | |||
| 995 | |||
| 996 | /** |
||
| 997 | * @access protected |
||
| 998 | * @param string $class_name |
||
| 999 | * @param string $param_class |
||
| 1000 | * @param array $arguments |
||
| 1001 | * @param mixed $index |
||
| 1002 | * @return array |
||
| 1003 | */ |
||
| 1004 | protected function _resolve_dependency( $class_name, $param_class , $arguments, $index ) { |
||
| 1045 | |||
| 1046 | |||
| 1047 | |||
| 1048 | /** |
||
| 1049 | * _set_cached_class |
||
| 1050 | * |
||
| 1051 | * attempts to cache the instantiated class locally |
||
| 1052 | * in one of the following places, in the following order: |
||
| 1053 | * $this->{class_abbreviation} ie: $this->CART |
||
| 1054 | * $this->{$class_name} ie: $this->Some_Class |
||
| 1055 | * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
||
| 1056 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
| 1057 | * |
||
| 1058 | * @access protected |
||
| 1059 | * @param object $class_obj |
||
| 1060 | * @param string $class_name |
||
| 1061 | * @param string $class_prefix |
||
| 1062 | * @param bool $from_db |
||
| 1063 | * @return void |
||
| 1064 | */ |
||
| 1065 | protected function _set_cached_class( $class_obj, $class_name, $class_prefix = '', $from_db = false ) { |
||
| 1081 | |||
| 1082 | |||
| 1083 | |||
| 1084 | /** |
||
| 1085 | * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
||
| 1086 | * |
||
| 1087 | * |
||
| 1088 | * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
||
| 1089 | * in the EE_Dependency_Map::$_class_loaders array, |
||
| 1090 | * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
||
| 1091 | * @param array $arguments |
||
| 1092 | * @return object |
||
| 1093 | */ |
||
| 1094 | public static function factory( $classname, $arguments = array() ) { |
||
| 1103 | |||
| 1104 | |||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Gets the addon by its name/slug (not classname. For that, just |
||
| 1108 | * use the classname as the property name on EE_Config::instance()->addons) |
||
| 1109 | * @param string $name |
||
| 1110 | * @return EE_Addon |
||
| 1111 | */ |
||
| 1112 | public function get_addon_by_name( $name ) { |
||
| 1120 | |||
| 1121 | |||
| 1122 | |||
| 1123 | /** |
||
| 1124 | * 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} |
||
| 1125 | * |
||
| 1126 | * @return EE_Addon[] where the KEYS are the addon's name() |
||
| 1127 | */ |
||
| 1128 | public function get_addons_by_name() { |
||
| 1135 | |||
| 1136 | |||
| 1137 | |||
| 1138 | /** |
||
| 1139 | * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
||
| 1140 | * a stale copy of it around |
||
| 1141 | * |
||
| 1142 | * @param string $model_name |
||
| 1143 | * @return \EEM_Base |
||
| 1144 | * @throws \EE_Error |
||
| 1145 | */ |
||
| 1146 | public function reset_model( $model_name ) { |
||
| 1147 | $model_class_name = strpos( $model_name, 'EEM_' ) !== 0 ? "EEM_{$model_name}" : $model_name; |
||
| 1148 | if( ! isset( $this->LIB->{$model_class_name} ) || ! $this->LIB->{$model_class_name} instanceof EEM_Base ) { |
||
| 1149 | return null; |
||
| 1150 | } |
||
| 1151 | //get that model reset it and make sure we nuke the old reference to it |
||
| 1152 | if ( $this->LIB->{$model_class_name} instanceof $model_class_name && is_callable( array( $model_class_name, 'reset' ))) { |
||
| 1153 | $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
||
| 1154 | } else { |
||
| 1155 | throw new EE_Error( sprintf( __( 'Model %s does not have a method "reset"', 'event_espresso' ), $model_name ) ); |
||
| 1156 | } |
||
| 1157 | return $this->LIB->{$model_class_name}; |
||
| 1158 | } |
||
| 1159 | |||
| 1160 | |||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Resets the registry. |
||
| 1164 | * |
||
| 1165 | * The criteria for what gets reset is based on what can be shared between sites on the same request when switch_to_blog |
||
| 1166 | * is used in a multisite install. Here is a list of things that are NOT reset. |
||
| 1167 | * |
||
| 1168 | * - $_dependency_map |
||
| 1169 | * - $_class_abbreviations |
||
| 1170 | * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
||
| 1171 | * - $REQ: Still on the same request so no need to change. |
||
| 1172 | * - $CAP: There is no site specific state in the EE_Capability class. |
||
| 1173 | * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only one Session |
||
| 1174 | * can be active in a single request. Resetting could resolve in "headers already sent" errors. |
||
| 1175 | * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
||
| 1176 | * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
||
| 1177 | * switch or on the restore. |
||
| 1178 | * - $modules |
||
| 1179 | * - $shortcodes |
||
| 1180 | * - $widgets |
||
| 1181 | * |
||
| 1182 | * @param boolean $hard whether to reset data in the database too, or just refresh |
||
| 1183 | * the Registry to its state at the beginning of the request |
||
| 1184 | * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
||
| 1185 | * or just reset without re-instantiating (handy to set to FALSE if you're not sure if you CAN |
||
| 1186 | * currently reinstantiate the singletons at the moment) |
||
| 1187 | * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so client |
||
| 1188 | * code instead can just change the model context to a different blog id if necessary |
||
| 1189 | * |
||
| 1190 | * @return EE_Registry |
||
| 1191 | */ |
||
| 1192 | public static function reset( $hard = false, $reinstantiate = true, $reset_models = true ) { |
||
| 1193 | $instance = self::instance(); |
||
| 1194 | $instance->_cache_on = true; |
||
| 1195 | // reset some "special" classes |
||
| 1196 | EEH_Activation::reset(); |
||
| 1197 | $instance->CFG = $instance->CFG->reset( $hard, $reinstantiate ); |
||
| 1198 | $instance->CART = null; |
||
| 1199 | $instance->MRM = null; |
||
| 1200 | //handle of objects cached on LIB |
||
| 1201 | foreach ( array( 'LIB', 'modules', 'shortcodes' ) as $cache ) { |
||
| 1202 | foreach ( $instance->{$cache} as $class_name => $class ) { |
||
| 1203 | if ( EE_Registry::_reset_and_unset_object( $class, $reset_models )) { |
||
| 1204 | unset( $instance->{$cache}->{$class_name} ); |
||
| 1205 | } |
||
| 1206 | } |
||
| 1207 | } |
||
| 1208 | return $instance; |
||
| 1209 | } |
||
| 1210 | |||
| 1211 | |||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * if passed object implements ResettableInterface, then call it's reset() method |
||
| 1215 | * if passed object implements InterminableInterface, then return false, |
||
| 1216 | * to indicate that it should NOT be cleared from the Registry cache |
||
| 1217 | * |
||
| 1218 | * @param $object |
||
| 1219 | * @param bool $reset_models |
||
| 1220 | * @return bool returns true if cached object should be unset |
||
| 1221 | */ |
||
| 1222 | private static function _reset_and_unset_object( $object, $reset_models ) { |
||
| 1223 | static $count = 0; |
||
| 1224 | $count++; |
||
| 1225 | if ( $object instanceof ResettableInterface ) { |
||
| 1226 | if ( $object instanceof EEM_Base ) { |
||
| 1227 | if ( $reset_models ) { |
||
| 1228 | $object->reset(); |
||
| 1229 | return true; |
||
| 1230 | } |
||
| 1231 | return false; |
||
| 1232 | } |
||
| 1233 | $object->reset(); |
||
| 1234 | return true; |
||
| 1235 | } |
||
| 1236 | if ( ! $object instanceof InterminableInterface ) { |
||
| 1237 | return true; |
||
| 1238 | } |
||
| 1239 | return false; |
||
| 1240 | } |
||
| 1241 | |||
| 1242 | |||
| 1243 | |||
| 1244 | /** |
||
| 1245 | * @override magic methods |
||
| 1246 | * @return void |
||
| 1247 | */ |
||
| 1248 | final function __destruct() { |
||
| 1250 | |||
| 1251 | |||
| 1252 | |||
| 1253 | /** |
||
| 1254 | * @param $a |
||
| 1255 | * @param $b |
||
| 1256 | */ |
||
| 1257 | final function __call( $a, $b ) { |
||
| 1259 | |||
| 1260 | |||
| 1261 | |||
| 1262 | /** |
||
| 1263 | * @param $a |
||
| 1264 | */ |
||
| 1265 | final function __get( $a ) { |
||
| 1267 | |||
| 1268 | |||
| 1269 | |||
| 1270 | /** |
||
| 1271 | * @param $a |
||
| 1272 | * @param $b |
||
| 1273 | */ |
||
| 1274 | final function __set( $a, $b ) { |
||
| 1276 | |||
| 1277 | |||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * @param $a |
||
| 1281 | */ |
||
| 1282 | final function __isset( $a ) { |
||
| 1284 | |||
| 1285 | |||
| 1286 | |||
| 1287 | /** |
||
| 1288 | * @param $a |
||
| 1289 | */ |
||
| 1290 | final function __unset( $a ) { |
||
| 1292 | |||
| 1293 | |||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * @return array |
||
| 1297 | */ |
||
| 1298 | final function __sleep() { |
||
| 1301 | |||
| 1302 | |||
| 1303 | |||
| 1304 | final function __wakeup() { |
||
| 1306 | |||
| 1307 | |||
| 1308 | |||
| 1309 | /** |
||
| 1310 | * @return string |
||
| 1311 | */ |
||
| 1312 | final function __toString() { |
||
| 1315 | |||
| 1316 | |||
| 1317 | |||
| 1318 | final function __invoke() { |
||
| 1320 | |||
| 1321 | |||
| 1322 | |||
| 1323 | final function __set_state() { |
||
| 1325 | |||
| 1326 | |||
| 1327 | |||
| 1328 | final function __clone() { |
||
| 1330 | |||
| 1331 | |||
| 1332 | |||
| 1333 | /** |
||
| 1334 | * @param $a |
||
| 1335 | * @param $b |
||
| 1336 | */ |
||
| 1337 | final static function __callStatic( $a, $b ) { |
||
| 1339 | |||
| 1340 | /** |
||
| 1341 | * Gets all the custom post type models defined |
||
| 1342 | * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
||
| 1343 | */ |
||
| 1344 | public function cpt_models() { |
||
| 1353 | |||
| 1354 | |||
| 1355 | |||
| 1356 | /** |
||
| 1357 | * @return \EE_Config |
||
| 1358 | */ |
||
| 1359 | public static function CFG() { |
||
| 1362 | |||
| 1363 | |||
| 1364 | } |
||
| 1365 | // End of file EE_Registry.core.php |
||
| 1367 |
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):