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 | ! self::$_instance instanceof EE_Registry |
||
| 198 | && $dependency_map instanceof EE_Dependency_Map |
||
| 199 | && $mirror instanceof Mirror |
||
| 200 | && $class_cache instanceof ClassInterfaceCache |
||
| 201 | ) { |
||
| 202 | self::$_instance = new self($dependency_map, $mirror, $class_cache); |
||
| 203 | } |
||
| 204 | return self::$_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 | * initialize |
||
| 233 | * |
||
| 234 | * @throws OutOfBoundsException |
||
| 235 | * @throws InvalidArgumentException |
||
| 236 | * @throws InvalidInterfaceException |
||
| 237 | * @throws InvalidDataTypeException |
||
| 238 | * @throws InvalidClassException |
||
| 239 | * @throws EE_Error |
||
| 240 | * @throws ReflectionException |
||
| 241 | */ |
||
| 242 | public function initialize() |
||
| 243 | { |
||
| 244 | $this->_class_abbreviations = apply_filters( |
||
| 245 | 'FHEE__EE_Registry____construct___class_abbreviations', |
||
| 246 | array( |
||
| 247 | 'EE_Config' => 'CFG', |
||
| 248 | 'EE_Session' => 'SSN', |
||
| 249 | 'EE_Capabilities' => 'CAP', |
||
| 250 | 'EE_Cart' => 'CART', |
||
| 251 | 'EE_Network_Config' => 'NET_CFG', |
||
| 252 | 'EE_Request_Handler' => 'REQ', |
||
| 253 | 'EE_Message_Resource_Manager' => 'MRM', |
||
| 254 | 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
||
| 255 | 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
||
| 256 | ) |
||
| 257 | ); |
||
| 258 | $this->load_core('Base', array(), true); |
||
| 259 | // add our request and response objects to the cache |
||
| 260 | $request_loader = $this->_dependency_map->class_loader( |
||
| 261 | 'EventEspresso\core\services\request\Request' |
||
| 262 | ); |
||
| 263 | $this->_set_cached_class( |
||
| 264 | $request_loader(), |
||
| 265 | 'EventEspresso\core\services\request\Request' |
||
| 266 | ); |
||
| 267 | $response_loader = $this->_dependency_map->class_loader( |
||
| 268 | 'EventEspresso\core\services\request\Response' |
||
| 269 | ); |
||
| 270 | $this->_set_cached_class( |
||
| 271 | $response_loader(), |
||
| 272 | 'EventEspresso\core\services\request\Response' |
||
| 273 | ); |
||
| 274 | add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
||
| 275 | } |
||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * @return void |
||
| 281 | */ |
||
| 282 | public function init() |
||
| 290 | |||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * localize_i18n_js_strings |
||
| 295 | * |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | public static function localize_i18n_js_strings() |
||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * @param mixed string | EED_Module $module |
||
| 312 | * @throws OutOfBoundsException |
||
| 313 | * @throws InvalidArgumentException |
||
| 314 | * @throws InvalidInterfaceException |
||
| 315 | * @throws InvalidDataTypeException |
||
| 316 | * @throws InvalidClassException |
||
| 317 | * @throws EE_Error |
||
| 318 | * @throws ReflectionException |
||
| 319 | */ |
||
| 320 | public function add_module($module) |
||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * @param string $module_name |
||
| 337 | * @return mixed EED_Module | NULL |
||
| 338 | */ |
||
| 339 | public function get_module($module_name = '') |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * loads core classes - must be singletons |
||
| 349 | * |
||
| 350 | * @param string $class_name - simple class name ie: session |
||
| 351 | * @param mixed $arguments |
||
| 352 | * @param bool $load_only |
||
| 353 | * @return mixed |
||
| 354 | * @throws InvalidInterfaceException |
||
| 355 | * @throws InvalidDataTypeException |
||
| 356 | * @throws InvalidClassException |
||
| 357 | * @throws EE_Error |
||
| 358 | * @throws ReflectionException |
||
| 359 | * @throws OutOfBoundsException |
||
| 360 | * @throws InvalidArgumentException |
||
| 361 | */ |
||
| 362 | public function load_core($class_name, $arguments = array(), $load_only = false) |
||
| 388 | |||
| 389 | |||
| 390 | /** |
||
| 391 | * loads service classes |
||
| 392 | * |
||
| 393 | * @param string $class_name - simple class name ie: session |
||
| 394 | * @param mixed $arguments |
||
| 395 | * @param bool $load_only |
||
| 396 | * @return mixed |
||
| 397 | * @throws InvalidInterfaceException |
||
| 398 | * @throws InvalidDataTypeException |
||
| 399 | * @throws InvalidClassException |
||
| 400 | * @throws EE_Error |
||
| 401 | * @throws ReflectionException |
||
| 402 | * @throws OutOfBoundsException |
||
| 403 | * @throws InvalidArgumentException |
||
| 404 | */ |
||
| 405 | public function load_service($class_name, $arguments = array(), $load_only = false) |
||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * loads data_migration_scripts |
||
| 429 | * |
||
| 430 | * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
||
| 431 | * @param mixed $arguments |
||
| 432 | * @return EE_Data_Migration_Script_Base|mixed |
||
| 433 | * @throws InvalidInterfaceException |
||
| 434 | * @throws InvalidDataTypeException |
||
| 435 | * @throws InvalidClassException |
||
| 436 | * @throws EE_Error |
||
| 437 | * @throws ReflectionException |
||
| 438 | * @throws OutOfBoundsException |
||
| 439 | * @throws InvalidArgumentException |
||
| 440 | */ |
||
| 441 | public function load_dms($class_name, $arguments = array()) |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * loads object creating classes - must be singletons |
||
| 458 | * |
||
| 459 | * @param string $class_name - simple class name ie: attendee |
||
| 460 | * @param mixed $arguments - an array of arguments to pass to the class |
||
| 461 | * @param bool $from_db - some classes are instantiated from the db and thus call a different method to |
||
| 462 | * instantiate |
||
| 463 | * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then |
||
| 464 | * set this to FALSE (ie. when instantiating model objects from client in a loop) |
||
| 465 | * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate |
||
| 466 | * (default) |
||
| 467 | * @return EE_Base_Class | bool |
||
| 468 | * @throws InvalidInterfaceException |
||
| 469 | * @throws InvalidDataTypeException |
||
| 470 | * @throws InvalidClassException |
||
| 471 | * @throws EE_Error |
||
| 472 | * @throws ReflectionException |
||
| 473 | * @throws OutOfBoundsException |
||
| 474 | * @throws InvalidArgumentException |
||
| 475 | */ |
||
| 476 | public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
||
| 497 | |||
| 498 | |||
| 499 | /** |
||
| 500 | * loads helper classes - must be singletons |
||
| 501 | * |
||
| 502 | * @param string $class_name - simple class name ie: price |
||
| 503 | * @param mixed $arguments |
||
| 504 | * @param bool $load_only |
||
| 505 | * @return EEH_Base | bool |
||
| 506 | * @throws InvalidInterfaceException |
||
| 507 | * @throws InvalidDataTypeException |
||
| 508 | * @throws InvalidClassException |
||
| 509 | * @throws EE_Error |
||
| 510 | * @throws ReflectionException |
||
| 511 | * @throws OutOfBoundsException |
||
| 512 | * @throws InvalidArgumentException |
||
| 513 | */ |
||
| 514 | public function load_helper($class_name, $arguments = array(), $load_only = true) |
||
| 530 | |||
| 531 | |||
| 532 | /** |
||
| 533 | * loads core classes - must be singletons |
||
| 534 | * |
||
| 535 | * @param string $class_name - simple class name ie: session |
||
| 536 | * @param mixed $arguments |
||
| 537 | * @param bool $load_only |
||
| 538 | * @param bool $cache whether to cache the object or not. |
||
| 539 | * @return mixed |
||
| 540 | * @throws InvalidInterfaceException |
||
| 541 | * @throws InvalidDataTypeException |
||
| 542 | * @throws InvalidClassException |
||
| 543 | * @throws EE_Error |
||
| 544 | * @throws ReflectionException |
||
| 545 | * @throws OutOfBoundsException |
||
| 546 | * @throws InvalidArgumentException |
||
| 547 | */ |
||
| 548 | public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
||
| 569 | |||
| 570 | |||
| 571 | /** |
||
| 572 | * loads model classes - must be singletons |
||
| 573 | * |
||
| 574 | * @param string $class_name - simple class name ie: price |
||
| 575 | * @param mixed $arguments |
||
| 576 | * @param bool $load_only |
||
| 577 | * @return EEM_Base | bool |
||
| 578 | * @throws InvalidInterfaceException |
||
| 579 | * @throws InvalidDataTypeException |
||
| 580 | * @throws InvalidClassException |
||
| 581 | * @throws EE_Error |
||
| 582 | * @throws ReflectionException |
||
| 583 | * @throws OutOfBoundsException |
||
| 584 | * @throws InvalidArgumentException |
||
| 585 | */ |
||
| 586 | public function load_model($class_name, $arguments = array(), $load_only = false) |
||
| 606 | |||
| 607 | |||
| 608 | /** |
||
| 609 | * loads model classes - must be singletons |
||
| 610 | * |
||
| 611 | * @param string $class_name - simple class name ie: price |
||
| 612 | * @param mixed $arguments |
||
| 613 | * @param bool $load_only |
||
| 614 | * @return mixed | bool |
||
| 615 | * @throws InvalidInterfaceException |
||
| 616 | * @throws InvalidDataTypeException |
||
| 617 | * @throws InvalidClassException |
||
| 618 | * @throws EE_Error |
||
| 619 | * @throws ReflectionException |
||
| 620 | * @throws OutOfBoundsException |
||
| 621 | * @throws InvalidArgumentException |
||
| 622 | */ |
||
| 623 | public function load_model_class($class_name, $arguments = array(), $load_only = true) |
||
| 643 | |||
| 644 | |||
| 645 | |||
| 646 | /** |
||
| 647 | * Determines if $model_name is the name of an actual EE model. |
||
| 648 | * |
||
| 649 | * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
||
| 650 | * @return boolean |
||
| 651 | */ |
||
| 652 | public function is_model_name($model_name) |
||
| 656 | |||
| 657 | |||
| 658 | /** |
||
| 659 | * generic class loader |
||
| 660 | * |
||
| 661 | * @param string $path_to_file - directory path to file location, not including filename |
||
| 662 | * @param string $file_name - file name ie: my_file.php, including extension |
||
| 663 | * @param string $type - file type - core? class? helper? model? |
||
| 664 | * @param mixed $arguments |
||
| 665 | * @param bool $load_only |
||
| 666 | * @return mixed |
||
| 667 | * @throws InvalidInterfaceException |
||
| 668 | * @throws InvalidDataTypeException |
||
| 669 | * @throws InvalidClassException |
||
| 670 | * @throws EE_Error |
||
| 671 | * @throws ReflectionException |
||
| 672 | * @throws OutOfBoundsException |
||
| 673 | * @throws InvalidArgumentException |
||
| 674 | */ |
||
| 675 | public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
||
| 689 | |||
| 690 | |||
| 691 | /** |
||
| 692 | * @param string $path_to_file - directory path to file location, not including filename |
||
| 693 | * @param string $class_name - full class name ie: My_Class |
||
| 694 | * @param string $type - file type - core? class? helper? model? |
||
| 695 | * @param mixed $arguments |
||
| 696 | * @param bool $load_only |
||
| 697 | * @return bool|EE_Addon|object |
||
| 698 | * @throws InvalidInterfaceException |
||
| 699 | * @throws InvalidDataTypeException |
||
| 700 | * @throws InvalidClassException |
||
| 701 | * @throws EE_Error |
||
| 702 | * @throws ReflectionException |
||
| 703 | * @throws OutOfBoundsException |
||
| 704 | * @throws InvalidArgumentException |
||
| 705 | */ |
||
| 706 | public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
||
| 720 | |||
| 721 | |||
| 722 | /** |
||
| 723 | * instantiates, caches, and automatically resolves dependencies |
||
| 724 | * for classes that use a Fully Qualified Class Name. |
||
| 725 | * if the class is not capable of being loaded using PSR-4 autoloading, |
||
| 726 | * then you need to use one of the existing load_*() methods |
||
| 727 | * which can resolve the classname and filepath from the passed arguments |
||
| 728 | * |
||
| 729 | * @param bool|string $class_name Fully Qualified Class Name |
||
| 730 | * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
||
| 731 | * @param bool $cache whether to cache the instantiated object for reuse |
||
| 732 | * @param bool $from_db some classes are instantiated from the db |
||
| 733 | * and thus call a different method to instantiate |
||
| 734 | * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
||
| 735 | * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
||
| 736 | * @return bool|null|mixed null = failure to load or instantiate class object. |
||
| 737 | * object = class loaded and instantiated successfully. |
||
| 738 | * bool = fail or success when $load_only is true |
||
| 739 | * @throws OutOfBoundsException |
||
| 740 | * @throws InvalidInterfaceException |
||
| 741 | * @throws InvalidDataTypeException |
||
| 742 | * @throws EE_Error |
||
| 743 | * @throws ReflectionException |
||
| 744 | * @throws InvalidArgumentException |
||
| 745 | */ |
||
| 746 | public function create( |
||
| 797 | |||
| 798 | |||
| 799 | |||
| 800 | /** |
||
| 801 | * Recursively checks that a class exists and potentially attempts to load classes with non-FQCNs |
||
| 802 | * |
||
| 803 | * @param string|object $class_name |
||
| 804 | * @param array $arguments |
||
| 805 | * @param int $attempt |
||
| 806 | * @return mixed |
||
| 807 | */ |
||
| 808 | private function loadOrVerifyClassExists($class_name, array $arguments, $attempt = 1) { |
||
| 833 | |||
| 834 | |||
| 835 | /** |
||
| 836 | * instantiates, caches, and injects dependencies for classes |
||
| 837 | * |
||
| 838 | * @param array $file_paths an array of paths to folders to look in |
||
| 839 | * @param string $class_prefix EE or EEM or... ??? |
||
| 840 | * @param bool|string $class_name $class name |
||
| 841 | * @param string $type file type - core? class? helper? model? |
||
| 842 | * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
||
| 843 | * @param bool $from_db some classes are instantiated from the db |
||
| 844 | * and thus call a different method to instantiate |
||
| 845 | * @param bool $cache whether to cache the instantiated object for reuse |
||
| 846 | * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
||
| 847 | * @return bool|null|object null = failure to load or instantiate class object. |
||
| 848 | * object = class loaded and instantiated successfully. |
||
| 849 | * bool = fail or success when $load_only is true |
||
| 850 | * @throws OutOfBoundsException |
||
| 851 | * @throws EE_Error |
||
| 852 | * @throws ReflectionException |
||
| 853 | * @throws InvalidInterfaceException |
||
| 854 | * @throws InvalidDataTypeException |
||
| 855 | * @throws InvalidArgumentException |
||
| 856 | */ |
||
| 857 | protected function _load( |
||
| 916 | |||
| 917 | |||
| 918 | |||
| 919 | /** |
||
| 920 | * @param string $class_name |
||
| 921 | * @param string $default have to specify something, but not anything that will conflict |
||
| 922 | * @return mixed|string |
||
| 923 | */ |
||
| 924 | protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
||
| 930 | |||
| 931 | |||
| 932 | /** |
||
| 933 | * attempts to find a cached version of the requested class |
||
| 934 | * by looking in the following places: |
||
| 935 | * $this->{$class_abbreviation} ie: $this->CART |
||
| 936 | * $this->{$class_name} ie: $this->Some_Class |
||
| 937 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
| 938 | * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
||
| 939 | * |
||
| 940 | * @param string $class_name |
||
| 941 | * @param string $class_prefix |
||
| 942 | * @param array $arguments |
||
| 943 | * @return mixed |
||
| 944 | */ |
||
| 945 | protected function _get_cached_class( |
||
| 982 | |||
| 983 | |||
| 984 | /** |
||
| 985 | * removes a cached version of the requested class |
||
| 986 | * |
||
| 987 | * @param string $class_name |
||
| 988 | * @param boolean $addon |
||
| 989 | * @param array $arguments |
||
| 990 | * @return boolean |
||
| 991 | */ |
||
| 992 | public function clear_cached_class( |
||
| 1019 | |||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * _set_cached_class |
||
| 1023 | * attempts to cache the instantiated class locally |
||
| 1024 | * in one of the following places, in the following order: |
||
| 1025 | * $this->{class_abbreviation} ie: $this->CART |
||
| 1026 | * $this->{$class_name} ie: $this->Some_Class |
||
| 1027 | * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
||
| 1028 | * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
||
| 1029 | * |
||
| 1030 | * @param object $class_obj |
||
| 1031 | * @param string $class_name |
||
| 1032 | * @param string $class_prefix |
||
| 1033 | * @param bool $from_db |
||
| 1034 | * @param array $arguments |
||
| 1035 | * @return void |
||
| 1036 | */ |
||
| 1037 | protected function _set_cached_class( |
||
| 1067 | |||
| 1068 | |||
| 1069 | |||
| 1070 | /** |
||
| 1071 | * build a string representation of a class' name and arguments |
||
| 1072 | * |
||
| 1073 | * @param string $class_name |
||
| 1074 | * @param array $arguments |
||
| 1075 | * @return string |
||
| 1076 | */ |
||
| 1077 | private function getClassIdentifier($class_name, $arguments = array()) |
||
| 1085 | |||
| 1086 | |||
| 1087 | |||
| 1088 | /** |
||
| 1089 | * build a string representation of a class' arguments |
||
| 1090 | * (mostly because Closures can't be serialized) |
||
| 1091 | * |
||
| 1092 | * @param array $arguments |
||
| 1093 | * @return string |
||
| 1094 | */ |
||
| 1095 | private function getIdentifierForArguments($arguments = array()) |
||
| 1118 | |||
| 1119 | |||
| 1120 | |||
| 1121 | /** |
||
| 1122 | * attempts to find a full valid filepath for the requested class. |
||
| 1123 | * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
||
| 1124 | * then returns that path if the target file has been found and is readable |
||
| 1125 | * |
||
| 1126 | * @param string $class_name |
||
| 1127 | * @param string $type |
||
| 1128 | * @param array $file_paths |
||
| 1129 | * @return string | bool |
||
| 1130 | */ |
||
| 1131 | protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
||
| 1156 | |||
| 1157 | |||
| 1158 | |||
| 1159 | /** |
||
| 1160 | * basically just performs a require_once() |
||
| 1161 | * but with some error handling |
||
| 1162 | * |
||
| 1163 | * @param string $path |
||
| 1164 | * @param string $class_name |
||
| 1165 | * @param string $type |
||
| 1166 | * @param array $file_paths |
||
| 1167 | * @return bool |
||
| 1168 | * @throws EE_Error |
||
| 1169 | * @throws ReflectionException |
||
| 1170 | */ |
||
| 1171 | protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
||
| 1219 | |||
| 1220 | |||
| 1221 | |||
| 1222 | /** |
||
| 1223 | * Some of our legacy classes that extended a parent class would simply use a require() statement |
||
| 1224 | * before their class declaration in order to ensure that the parent class was loaded. |
||
| 1225 | * This is not ideal, but it's nearly impossible to determine the parent class of a non-namespaced class, |
||
| 1226 | * without triggering a fatal error because the parent class has yet to be loaded and therefore doesn't exist. |
||
| 1227 | * |
||
| 1228 | * @param string $class_name |
||
| 1229 | */ |
||
| 1230 | protected function resolve_legacy_class_parent($class_name = '') |
||
| 1242 | |||
| 1243 | |||
| 1244 | /** |
||
| 1245 | * _create_object |
||
| 1246 | * Attempts to instantiate the requested class via any of the |
||
| 1247 | * commonly used instantiation methods employed throughout EE. |
||
| 1248 | * The priority for instantiation is as follows: |
||
| 1249 | * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
||
| 1250 | * - model objects via their 'new_instance_from_db' method |
||
| 1251 | * - model objects via their 'new_instance' method |
||
| 1252 | * - "singleton" classes" via their 'instance' method |
||
| 1253 | * - standard instantiable classes via their __constructor |
||
| 1254 | * Prior to instantiation, if the classname exists in the dependency_map, |
||
| 1255 | * then the constructor for the requested class will be examined to determine |
||
| 1256 | * if any dependencies exist, and if they can be injected. |
||
| 1257 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
| 1258 | * |
||
| 1259 | * @param string $class_name |
||
| 1260 | * @param array $arguments |
||
| 1261 | * @param string $type |
||
| 1262 | * @param bool $from_db |
||
| 1263 | * @return null|object|bool |
||
| 1264 | * @throws InvalidArgumentException |
||
| 1265 | * @throws InvalidInterfaceException |
||
| 1266 | * @throws EE_Error |
||
| 1267 | * @throws ReflectionException |
||
| 1268 | * @throws InvalidDataTypeException |
||
| 1269 | * @throws OutOfBoundsException |
||
| 1270 | */ |
||
| 1271 | protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
||
| 1329 | |||
| 1330 | |||
| 1331 | |||
| 1332 | /** |
||
| 1333 | * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
||
| 1334 | * @param array $array |
||
| 1335 | * @return bool |
||
| 1336 | */ |
||
| 1337 | protected function _array_is_numerically_and_sequentially_indexed(array $array) |
||
| 1343 | |||
| 1344 | |||
| 1345 | /** |
||
| 1346 | * _resolve_dependencies |
||
| 1347 | * examines the constructor for the requested class to determine |
||
| 1348 | * if any dependencies exist, and if they can be injected. |
||
| 1349 | * If so, then those classes will be added to the array of arguments passed to the constructor |
||
| 1350 | * PLZ NOTE: this is achieved by type hinting the constructor params |
||
| 1351 | * For example: |
||
| 1352 | * if attempting to load a class "Foo" with the following constructor: |
||
| 1353 | * __construct( Bar $bar_class, Fighter $grohl_class ) |
||
| 1354 | * then $bar_class and $grohl_class will be added to the $arguments array, |
||
| 1355 | * but only IF they are NOT already present in the incoming arguments array, |
||
| 1356 | * and the correct classes can be loaded |
||
| 1357 | * |
||
| 1358 | * @param ReflectionClass $reflector |
||
| 1359 | * @param string $class_name |
||
| 1360 | * @param array $arguments |
||
| 1361 | * @return array |
||
| 1362 | * @throws InvalidArgumentException |
||
| 1363 | * @throws InvalidDataTypeException |
||
| 1364 | * @throws InvalidInterfaceException |
||
| 1365 | * @throws ReflectionException |
||
| 1366 | */ |
||
| 1367 | protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, array $arguments = array()) |
||
| 1439 | |||
| 1440 | |||
| 1441 | /** |
||
| 1442 | * @param string $class_name |
||
| 1443 | * @param string $param_class |
||
| 1444 | * @param array $arguments |
||
| 1445 | * @param mixed $index |
||
| 1446 | * @param array $argument_keys |
||
| 1447 | * @return array |
||
| 1448 | * @throws InvalidArgumentException |
||
| 1449 | * @throws InvalidInterfaceException |
||
| 1450 | * @throws InvalidDataTypeException |
||
| 1451 | */ |
||
| 1452 | protected function _resolve_dependency($class_name, $param_class, $arguments, $index, array $argument_keys) |
||
| 1497 | |||
| 1498 | |||
| 1499 | |||
| 1500 | /** |
||
| 1501 | * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
||
| 1502 | * |
||
| 1503 | * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
||
| 1504 | * in the EE_Dependency_Map::$_class_loaders array, |
||
| 1505 | * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
||
| 1506 | * @param array $arguments |
||
| 1507 | * @return object |
||
| 1508 | */ |
||
| 1509 | public static function factory($classname, $arguments = array()) |
||
| 1520 | |||
| 1521 | |||
| 1522 | |||
| 1523 | /** |
||
| 1524 | * Gets the addon by its class name |
||
| 1525 | * |
||
| 1526 | * @param string $class_name |
||
| 1527 | * @return EE_Addon |
||
| 1528 | */ |
||
| 1529 | public function getAddon($class_name) |
||
| 1534 | |||
| 1535 | |||
| 1536 | /** |
||
| 1537 | * removes the addon from the internal cache |
||
| 1538 | * |
||
| 1539 | * @param string $class_name |
||
| 1540 | * @return void |
||
| 1541 | */ |
||
| 1542 | public function removeAddon($class_name) |
||
| 1547 | |||
| 1548 | |||
| 1549 | |||
| 1550 | /** |
||
| 1551 | * Gets the addon by its name/slug (not classname. For that, just |
||
| 1552 | * use the get_addon() method above |
||
| 1553 | * |
||
| 1554 | * @param string $name |
||
| 1555 | * @return EE_Addon |
||
| 1556 | */ |
||
| 1557 | public function get_addon_by_name($name) |
||
| 1566 | |||
| 1567 | |||
| 1568 | |||
| 1569 | /** |
||
| 1570 | * Gets an array of all the registered addons, where the keys are their names. |
||
| 1571 | * (ie, what each returns for their name() function) |
||
| 1572 | * They're already available on EE_Registry::instance()->addons as properties, |
||
| 1573 | * where each property's name is the addon's classname, |
||
| 1574 | * So if you just want to get the addon by classname, |
||
| 1575 | * OR use the get_addon() method above. |
||
| 1576 | * PLEASE NOTE: |
||
| 1577 | * addons with Fully Qualified Class Names |
||
| 1578 | * have had the namespace separators converted to underscores, |
||
| 1579 | * so a classname like Fully\Qualified\ClassName |
||
| 1580 | * would have been converted to Fully_Qualified_ClassName |
||
| 1581 | * |
||
| 1582 | * @return EE_Addon[] where the KEYS are the addon's name() |
||
| 1583 | */ |
||
| 1584 | public function get_addons_by_name() |
||
| 1592 | |||
| 1593 | |||
| 1594 | /** |
||
| 1595 | * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
||
| 1596 | * a stale copy of it around |
||
| 1597 | * |
||
| 1598 | * @param string $model_name |
||
| 1599 | * @return \EEM_Base |
||
| 1600 | * @throws \EE_Error |
||
| 1601 | */ |
||
| 1602 | public function reset_model($model_name) |
||
| 1621 | |||
| 1622 | |||
| 1623 | /** |
||
| 1624 | * Resets the registry. |
||
| 1625 | * The criteria for what gets reset is based on what can be shared between sites on the same request when |
||
| 1626 | * switch_to_blog is used in a multisite install. Here is a list of things that are NOT reset. |
||
| 1627 | * - $_dependency_map |
||
| 1628 | * - $_class_abbreviations |
||
| 1629 | * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
||
| 1630 | * - $REQ: Still on the same request so no need to change. |
||
| 1631 | * - $CAP: There is no site specific state in the EE_Capability class. |
||
| 1632 | * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only |
||
| 1633 | * one Session can be active in a single request. Resetting could resolve in "headers already sent" errors. |
||
| 1634 | * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
||
| 1635 | * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
||
| 1636 | * switch or on the restore. |
||
| 1637 | * - $modules |
||
| 1638 | * - $shortcodes |
||
| 1639 | * - $widgets |
||
| 1640 | * |
||
| 1641 | * @param boolean $hard [deprecated] |
||
| 1642 | * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
||
| 1643 | * or just reset without re-instantiating (handy to set to FALSE if you're not |
||
| 1644 | * sure if you CAN currently reinstantiate the singletons at the moment) |
||
| 1645 | * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so |
||
| 1646 | * client |
||
| 1647 | * code instead can just change the model context to a different blog id if |
||
| 1648 | * necessary |
||
| 1649 | * @return EE_Registry |
||
| 1650 | * @throws InvalidInterfaceException |
||
| 1651 | * @throws InvalidDataTypeException |
||
| 1652 | * @throws InvalidClassException |
||
| 1653 | * @throws EE_Error |
||
| 1654 | * @throws ReflectionException |
||
| 1655 | * @throws OutOfBoundsException |
||
| 1656 | * @throws InvalidArgumentException |
||
| 1657 | */ |
||
| 1658 | public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
||
| 1681 | |||
| 1682 | |||
| 1683 | |||
| 1684 | /** |
||
| 1685 | * if passed object implements ResettableInterface, then call it's reset() method |
||
| 1686 | * if passed object implements InterminableInterface, then return false, |
||
| 1687 | * to indicate that it should NOT be cleared from the Registry cache |
||
| 1688 | * |
||
| 1689 | * @param $object |
||
| 1690 | * @param bool $reset_models |
||
| 1691 | * @return bool returns true if cached object should be unset |
||
| 1692 | */ |
||
| 1693 | private static function _reset_and_unset_object($object, $reset_models) |
||
| 1720 | |||
| 1721 | |||
| 1722 | |||
| 1723 | /** |
||
| 1724 | * Gets all the custom post type models defined |
||
| 1725 | * |
||
| 1726 | * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
||
| 1727 | */ |
||
| 1728 | public function cpt_models() |
||
| 1738 | |||
| 1739 | |||
| 1740 | |||
| 1741 | /** |
||
| 1742 | * @return \EE_Config |
||
| 1743 | */ |
||
| 1744 | public static function CFG() |
||
| 1748 | |||
| 1749 | |||
| 1750 | /** |
||
| 1751 | * @deprecated $VID:$ |
||
| 1752 | * @param string $class_name |
||
| 1753 | * @return ReflectionClass |
||
| 1754 | * @throws ReflectionException |
||
| 1755 | * @throws InvalidDataTypeException |
||
| 1756 | */ |
||
| 1757 | public function get_ReflectionClass($class_name) |
||
| 1761 | } |
||
| 1762 | // End of file EE_Registry.core.php |
||
| 1764 |