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