@@ -16,1418 +16,1418 @@ |
||
| 16 | 16 | class EE_Registry |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * EE_Registry Object |
|
| 21 | - * |
|
| 22 | - * @var EE_Registry $_instance |
|
| 23 | - * @access private |
|
| 24 | - */ |
|
| 25 | - private static $_instance = null; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var EE_Dependency_Map $_dependency_map |
|
| 29 | - * @access protected |
|
| 30 | - */ |
|
| 31 | - protected $_dependency_map = null; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var array $_class_abbreviations |
|
| 35 | - * @access protected |
|
| 36 | - */ |
|
| 37 | - protected $_class_abbreviations = array(); |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @access public |
|
| 41 | - * @var \EventEspresso\core\services\commands\CommandBusInterface $BUS |
|
| 42 | - */ |
|
| 43 | - public $BUS; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * EE_Cart Object |
|
| 47 | - * |
|
| 48 | - * @access public |
|
| 49 | - * @var EE_Cart $CART |
|
| 50 | - */ |
|
| 51 | - public $CART = null; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * EE_Config Object |
|
| 55 | - * |
|
| 56 | - * @access public |
|
| 57 | - * @var EE_Config $CFG |
|
| 58 | - */ |
|
| 59 | - public $CFG = null; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * EE_Network_Config Object |
|
| 63 | - * |
|
| 64 | - * @access public |
|
| 65 | - * @var EE_Network_Config $NET_CFG |
|
| 66 | - */ |
|
| 67 | - public $NET_CFG = null; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * StdClass object for storing library classes in |
|
| 71 | - * |
|
| 72 | - * @public LIB |
|
| 73 | - * @var StdClass $LIB |
|
| 74 | - */ |
|
| 75 | - public $LIB = null; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * EE_Request_Handler Object |
|
| 79 | - * |
|
| 80 | - * @access public |
|
| 81 | - * @var EE_Request_Handler $REQ |
|
| 82 | - */ |
|
| 83 | - public $REQ = null; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * EE_Session Object |
|
| 87 | - * |
|
| 88 | - * @access public |
|
| 89 | - * @var EE_Session $SSN |
|
| 90 | - */ |
|
| 91 | - public $SSN = null; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * holds the ee capabilities object. |
|
| 95 | - * |
|
| 96 | - * @since 4.5.0 |
|
| 97 | - * @var EE_Capabilities |
|
| 98 | - */ |
|
| 99 | - public $CAP = null; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * holds the EE_Message_Resource_Manager object. |
|
| 103 | - * |
|
| 104 | - * @since 4.9.0 |
|
| 105 | - * @var EE_Message_Resource_Manager |
|
| 106 | - */ |
|
| 107 | - public $MRM = null; |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Holds the Assets Registry instance |
|
| 112 | - * @var Registry |
|
| 113 | - */ |
|
| 114 | - public $AssetsRegistry = null; |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * $addons - StdClass object for holding addons which have registered themselves to work with EE core |
|
| 118 | - * |
|
| 119 | - * @access public |
|
| 120 | - * @var EE_Addon[] |
|
| 121 | - */ |
|
| 122 | - public $addons = null; |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * $models |
|
| 126 | - * @access public |
|
| 127 | - * @var EEM_Base[] $models keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
| 128 | - */ |
|
| 129 | - public $models = array(); |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * $modules |
|
| 133 | - * @access public |
|
| 134 | - * @var EED_Module[] $modules |
|
| 135 | - */ |
|
| 136 | - public $modules = null; |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * $shortcodes |
|
| 140 | - * @access public |
|
| 141 | - * @var EES_Shortcode[] $shortcodes |
|
| 142 | - */ |
|
| 143 | - public $shortcodes = null; |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * $widgets |
|
| 147 | - * @access public |
|
| 148 | - * @var WP_Widget[] $widgets |
|
| 149 | - */ |
|
| 150 | - public $widgets = null; |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * $non_abstract_db_models |
|
| 154 | - * @access public |
|
| 155 | - * @var array this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
| 156 | - * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
| 157 | - * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
| 158 | - * classnames (eg "EEM_Event") |
|
| 159 | - */ |
|
| 160 | - public $non_abstract_db_models = array(); |
|
| 161 | - |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * $i18n_js_strings - internationalization for JS strings |
|
| 165 | - * usage: EE_Registry::i18n_js_strings['string_key'] = __( 'string to translate.', 'event_espresso' ); |
|
| 166 | - * in js file: var translatedString = eei18n.string_key; |
|
| 167 | - * |
|
| 168 | - * @access public |
|
| 169 | - * @var array |
|
| 170 | - */ |
|
| 171 | - public static $i18n_js_strings = array(); |
|
| 172 | - |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * $main_file - path to espresso.php |
|
| 176 | - * |
|
| 177 | - * @access public |
|
| 178 | - * @var array |
|
| 179 | - */ |
|
| 180 | - public $main_file; |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * array of ReflectionClass objects where the key is the class name |
|
| 184 | - * |
|
| 185 | - * @access public |
|
| 186 | - * @var ReflectionClass[] |
|
| 187 | - */ |
|
| 188 | - public $_reflectors; |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
| 192 | - * |
|
| 193 | - * @access protected |
|
| 194 | - * @var boolean $_cache_on |
|
| 195 | - */ |
|
| 196 | - protected $_cache_on = true; |
|
| 197 | - |
|
| 198 | - |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * @singleton method used to instantiate class object |
|
| 202 | - * @access public |
|
| 203 | - * @param \EE_Dependency_Map $dependency_map |
|
| 204 | - * @return \EE_Registry instance |
|
| 205 | - */ |
|
| 206 | - public static function instance(\EE_Dependency_Map $dependency_map = null) |
|
| 207 | - { |
|
| 208 | - // check if class object is instantiated |
|
| 209 | - if ( ! self::$_instance instanceof EE_Registry) { |
|
| 210 | - self::$_instance = new EE_Registry($dependency_map); |
|
| 211 | - } |
|
| 212 | - return self::$_instance; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - *protected constructor to prevent direct creation |
|
| 219 | - * |
|
| 220 | - * @Constructor |
|
| 221 | - * @access protected |
|
| 222 | - * @param \EE_Dependency_Map $dependency_map |
|
| 223 | - */ |
|
| 224 | - protected function __construct(\EE_Dependency_Map $dependency_map) |
|
| 225 | - { |
|
| 226 | - $this->_dependency_map = $dependency_map; |
|
| 227 | - $this->LIB = new stdClass(); |
|
| 228 | - $this->addons = new stdClass(); |
|
| 229 | - $this->modules = new stdClass(); |
|
| 230 | - $this->shortcodes = new stdClass(); |
|
| 231 | - $this->widgets = new stdClass(); |
|
| 232 | - add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * initialize |
|
| 239 | - */ |
|
| 240 | - public function initialize() |
|
| 241 | - { |
|
| 242 | - $this->_class_abbreviations = apply_filters( |
|
| 243 | - 'FHEE__EE_Registry____construct___class_abbreviations', |
|
| 244 | - array( |
|
| 245 | - 'EE_Config' => 'CFG', |
|
| 246 | - 'EE_Session' => 'SSN', |
|
| 247 | - 'EE_Capabilities' => 'CAP', |
|
| 248 | - 'EE_Cart' => 'CART', |
|
| 249 | - 'EE_Network_Config' => 'NET_CFG', |
|
| 250 | - 'EE_Request_Handler' => 'REQ', |
|
| 251 | - 'EE_Message_Resource_Manager' => 'MRM', |
|
| 252 | - 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
| 253 | - 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
| 254 | - ) |
|
| 255 | - ); |
|
| 256 | - $this->load_core('Base', array(), true); |
|
| 257 | - // add our request and response objects to the cache |
|
| 258 | - $request_loader = $this->_dependency_map->class_loader('EE_Request'); |
|
| 259 | - $this->_set_cached_class( |
|
| 260 | - $request_loader(), |
|
| 261 | - 'EE_Request' |
|
| 262 | - ); |
|
| 263 | - $response_loader = $this->_dependency_map->class_loader('EE_Response'); |
|
| 264 | - $this->_set_cached_class( |
|
| 265 | - $response_loader(), |
|
| 266 | - 'EE_Response' |
|
| 267 | - ); |
|
| 268 | - add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * init |
|
| 275 | - * |
|
| 276 | - * @access public |
|
| 277 | - * @return void |
|
| 278 | - */ |
|
| 279 | - public function init() |
|
| 280 | - { |
|
| 281 | - // Get current page protocol |
|
| 282 | - $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
| 283 | - // Output admin-ajax.php URL with same protocol as current page |
|
| 284 | - self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
| 285 | - self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * localize_i18n_js_strings |
|
| 292 | - * |
|
| 293 | - * @return string |
|
| 294 | - */ |
|
| 295 | - public static function localize_i18n_js_strings() |
|
| 296 | - { |
|
| 297 | - $i18n_js_strings = (array)EE_Registry::$i18n_js_strings; |
|
| 298 | - foreach ($i18n_js_strings as $key => $value) { |
|
| 299 | - if (is_scalar($value)) { |
|
| 300 | - $i18n_js_strings[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8'); |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - return "/* <![CDATA[ */ var eei18n = " . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * @param mixed string | EED_Module $module |
|
| 310 | - */ |
|
| 311 | - public function add_module($module) |
|
| 312 | - { |
|
| 313 | - if ($module instanceof EED_Module) { |
|
| 314 | - $module_class = get_class($module); |
|
| 315 | - $this->modules->{$module_class} = $module; |
|
| 316 | - } else { |
|
| 317 | - if ( ! class_exists('EE_Module_Request_Router')) { |
|
| 318 | - $this->load_core('Module_Request_Router'); |
|
| 319 | - } |
|
| 320 | - $this->modules->{$module} = EE_Module_Request_Router::module_factory($module); |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * @param string $module_name |
|
| 328 | - * @return mixed EED_Module | NULL |
|
| 329 | - */ |
|
| 330 | - public function get_module($module_name = '') |
|
| 331 | - { |
|
| 332 | - return isset($this->modules->{$module_name}) ? $this->modules->{$module_name} : null; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * loads core classes - must be singletons |
|
| 339 | - * |
|
| 340 | - * @access public |
|
| 341 | - * @param string $class_name - simple class name ie: session |
|
| 342 | - * @param mixed $arguments |
|
| 343 | - * @param bool $load_only |
|
| 344 | - * @return mixed |
|
| 345 | - */ |
|
| 346 | - public function load_core($class_name, $arguments = array(), $load_only = false) |
|
| 347 | - { |
|
| 348 | - $core_paths = apply_filters( |
|
| 349 | - 'FHEE__EE_Registry__load_core__core_paths', |
|
| 350 | - array( |
|
| 351 | - EE_CORE, |
|
| 352 | - EE_ADMIN, |
|
| 353 | - EE_CPTS, |
|
| 354 | - EE_CORE . 'data_migration_scripts' . DS, |
|
| 355 | - EE_CORE . 'request_stack' . DS, |
|
| 356 | - EE_CORE . 'middleware' . DS, |
|
| 357 | - ) |
|
| 358 | - ); |
|
| 359 | - // retrieve instantiated class |
|
| 360 | - return $this->_load($core_paths, 'EE_', $class_name, 'core', $arguments, true, $load_only); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * loads service classes |
|
| 367 | - * |
|
| 368 | - * @access public |
|
| 369 | - * @param string $class_name - simple class name ie: session |
|
| 370 | - * @param mixed $arguments |
|
| 371 | - * @param bool $load_only |
|
| 372 | - * @return mixed |
|
| 373 | - */ |
|
| 374 | - public function load_service($class_name, $arguments = array(), $load_only = false) |
|
| 375 | - { |
|
| 376 | - $service_paths = apply_filters( |
|
| 377 | - 'FHEE__EE_Registry__load_service__service_paths', |
|
| 378 | - array( |
|
| 379 | - EE_CORE . 'services' . DS, |
|
| 380 | - ) |
|
| 381 | - ); |
|
| 382 | - // retrieve instantiated class |
|
| 383 | - return $this->_load($service_paths, 'EE_', $class_name, 'class', $arguments, true, $load_only); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * loads data_migration_scripts |
|
| 390 | - * |
|
| 391 | - * @access public |
|
| 392 | - * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
| 393 | - * @param mixed $arguments |
|
| 394 | - * @return EE_Data_Migration_Script_Base|mixed |
|
| 395 | - */ |
|
| 396 | - public function load_dms($class_name, $arguments = array()) |
|
| 397 | - { |
|
| 398 | - // retrieve instantiated class |
|
| 399 | - return $this->_load( |
|
| 400 | - EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), |
|
| 401 | - 'EE_DMS_', $class_name, 'dms', $arguments, false, false |
|
| 402 | - ); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * loads object creating classes - must be singletons |
|
| 409 | - * |
|
| 410 | - * @param string $class_name - simple class name ie: attendee |
|
| 411 | - * @param mixed $arguments - an array of arguments to pass to the class |
|
| 412 | - * @param bool $from_db - deprecated |
|
| 413 | - * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
| 414 | - * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate (default) |
|
| 415 | - * @return EE_Base_Class | bool |
|
| 416 | - */ |
|
| 417 | - public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
| 418 | - { |
|
| 419 | - $paths = apply_filters('FHEE__EE_Registry__load_class__paths', array( |
|
| 420 | - EE_CORE, |
|
| 421 | - EE_CLASSES, |
|
| 422 | - EE_BUSINESS, |
|
| 423 | - )); |
|
| 424 | - // retrieve instantiated class |
|
| 425 | - return $this->_load($paths, 'EE_', $class_name, 'class', $arguments, $cache, $load_only); |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * loads helper classes - must be singletons |
|
| 432 | - * |
|
| 433 | - * @param string $class_name - simple class name ie: price |
|
| 434 | - * @param mixed $arguments |
|
| 435 | - * @param bool $load_only |
|
| 436 | - * @return EEH_Base | bool |
|
| 437 | - */ |
|
| 438 | - public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
| 439 | - { |
|
| 440 | - // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
| 441 | - $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
| 442 | - // retrieve instantiated class |
|
| 443 | - return $this->_load($helper_paths, 'EEH_', $class_name, 'helper', $arguments, true, $load_only); |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * loads core classes - must be singletons |
|
| 450 | - * |
|
| 451 | - * @access public |
|
| 452 | - * @param string $class_name - simple class name ie: session |
|
| 453 | - * @param mixed $arguments |
|
| 454 | - * @param bool $load_only |
|
| 455 | - * @param bool $cache whether to cache the object or not. |
|
| 456 | - * @return mixed |
|
| 457 | - */ |
|
| 458 | - public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
| 459 | - { |
|
| 460 | - $paths = array( |
|
| 461 | - EE_LIBRARIES, |
|
| 462 | - EE_LIBRARIES . 'messages' . DS, |
|
| 463 | - EE_LIBRARIES . 'shortcodes' . DS, |
|
| 464 | - EE_LIBRARIES . 'qtips' . DS, |
|
| 465 | - EE_LIBRARIES . 'payment_methods' . DS, |
|
| 466 | - ); |
|
| 467 | - // retrieve instantiated class |
|
| 468 | - return $this->_load($paths, 'EE_', $class_name, 'lib', $arguments, $cache, $load_only); |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - |
|
| 472 | - |
|
| 473 | - /** |
|
| 474 | - * loads model classes - must be singletons |
|
| 475 | - * |
|
| 476 | - * @param string $class_name - simple class name ie: price |
|
| 477 | - * @param mixed $arguments |
|
| 478 | - * @param bool $load_only |
|
| 479 | - * @return EEM_Base | bool |
|
| 480 | - */ |
|
| 481 | - public function load_model($class_name, $arguments = array(), $load_only = false) |
|
| 482 | - { |
|
| 483 | - $paths = apply_filters('FHEE__EE_Registry__load_model__paths', array( |
|
| 484 | - EE_MODELS, |
|
| 485 | - EE_CORE, |
|
| 486 | - )); |
|
| 487 | - // retrieve instantiated class |
|
| 488 | - return $this->_load($paths, 'EEM_', $class_name, 'model', $arguments, true, $load_only); |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - |
|
| 492 | - |
|
| 493 | - /** |
|
| 494 | - * loads model classes - must be singletons |
|
| 495 | - * |
|
| 496 | - * @param string $class_name - simple class name ie: price |
|
| 497 | - * @param mixed $arguments |
|
| 498 | - * @param bool $load_only |
|
| 499 | - * @return mixed | bool |
|
| 500 | - */ |
|
| 501 | - public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
| 502 | - { |
|
| 503 | - $paths = array( |
|
| 504 | - EE_MODELS . 'fields' . DS, |
|
| 505 | - EE_MODELS . 'helpers' . DS, |
|
| 506 | - EE_MODELS . 'relations' . DS, |
|
| 507 | - EE_MODELS . 'strategies' . DS, |
|
| 508 | - ); |
|
| 509 | - // retrieve instantiated class |
|
| 510 | - return $this->_load($paths, 'EE_', $class_name, '', $arguments, true, $load_only); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - |
|
| 514 | - |
|
| 515 | - /** |
|
| 516 | - * Determines if $model_name is the name of an actual EE model. |
|
| 517 | - * |
|
| 518 | - * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
| 519 | - * @return boolean |
|
| 520 | - */ |
|
| 521 | - public function is_model_name($model_name) |
|
| 522 | - { |
|
| 523 | - return isset($this->models[$model_name]) ? true : false; |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - |
|
| 527 | - |
|
| 528 | - /** |
|
| 529 | - * generic class loader |
|
| 530 | - * |
|
| 531 | - * @param string $path_to_file - directory path to file location, not including filename |
|
| 532 | - * @param string $file_name - file name ie: my_file.php, including extension |
|
| 533 | - * @param string $type - file type - core? class? helper? model? |
|
| 534 | - * @param mixed $arguments |
|
| 535 | - * @param bool $load_only |
|
| 536 | - * @return mixed |
|
| 537 | - */ |
|
| 538 | - public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
| 539 | - { |
|
| 540 | - // retrieve instantiated class |
|
| 541 | - return $this->_load($path_to_file, '', $file_name, $type, $arguments, true, $load_only); |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * load_addon |
|
| 548 | - * |
|
| 549 | - * @param string $path_to_file - directory path to file location, not including filename |
|
| 550 | - * @param string $class_name - full class name ie: My_Class |
|
| 551 | - * @param string $type - file type - core? class? helper? model? |
|
| 552 | - * @param mixed $arguments |
|
| 553 | - * @param bool $load_only |
|
| 554 | - * @return EE_Addon |
|
| 555 | - */ |
|
| 556 | - public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
| 557 | - { |
|
| 558 | - // retrieve instantiated class |
|
| 559 | - return $this->_load($path_to_file, 'addon', $class_name, $type, $arguments, true, $load_only); |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - |
|
| 563 | - |
|
| 564 | - /** |
|
| 565 | - * instantiates, caches, and automatically resolves dependencies |
|
| 566 | - * for classes that use a Fully Qualified Class Name. |
|
| 567 | - * if the class is not capable of being loaded using PSR-4 autoloading, |
|
| 568 | - * then you need to use one of the existing load_*() methods |
|
| 569 | - * which can resolve the classname and filepath from the passed arguments |
|
| 570 | - * |
|
| 571 | - * @param bool|string $class_name Fully Qualified Class Name |
|
| 572 | - * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
| 573 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
| 574 | - * @param bool $from_db some classes are instantiated from the db |
|
| 575 | - * and thus call a different method to instantiate |
|
| 576 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 577 | - * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
| 578 | - * @return mixed null = failure to load or instantiate class object. |
|
| 579 | - * object = class loaded and instantiated successfully. |
|
| 580 | - * bool = fail or success when $load_only is true |
|
| 581 | - */ |
|
| 582 | - public function create( |
|
| 583 | - $class_name = false, |
|
| 584 | - $arguments = array(), |
|
| 585 | - $cache = true, |
|
| 586 | - $load_only = false, |
|
| 587 | - $addon = false |
|
| 588 | - ) { |
|
| 589 | - $class_name = ltrim($class_name, '\\'); |
|
| 590 | - $class_name = $this->_dependency_map->get_alias($class_name); |
|
| 591 | - if ( ! class_exists($class_name)) { |
|
| 592 | - // maybe the class is registered with a preceding \ |
|
| 593 | - $class_name = strpos($class_name, '\\') !== 0 ? '\\' . $class_name : $class_name; |
|
| 594 | - // still doesn't exist ? |
|
| 595 | - if ( ! class_exists($class_name)) { |
|
| 596 | - return null; |
|
| 597 | - } |
|
| 598 | - } |
|
| 599 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 600 | - if ($load_only) { |
|
| 601 | - return true; |
|
| 602 | - } |
|
| 603 | - $addon = $addon ? 'addon' : ''; |
|
| 604 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 605 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 606 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 607 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
| 608 | - // return object if it's already cached |
|
| 609 | - $cached_class = $this->_get_cached_class($class_name, $addon); |
|
| 610 | - if ($cached_class !== null) { |
|
| 611 | - return $cached_class; |
|
| 612 | - } |
|
| 613 | - } |
|
| 614 | - // instantiate the requested object |
|
| 615 | - $class_obj = $this->_create_object($class_name, $arguments, $addon); |
|
| 616 | - if ($this->_cache_on && $cache) { |
|
| 617 | - // save it for later... kinda like gum { : $ |
|
| 618 | - $this->_set_cached_class($class_obj, $class_name, $addon); |
|
| 619 | - } |
|
| 620 | - $this->_cache_on = true; |
|
| 621 | - return $class_obj; |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * instantiates, caches, and injects dependencies for classes |
|
| 628 | - * |
|
| 629 | - * @param array $file_paths an array of paths to folders to look in |
|
| 630 | - * @param string $class_prefix EE or EEM or... ??? |
|
| 631 | - * @param bool|string $class_name $class name |
|
| 632 | - * @param string $type file type - core? class? helper? model? |
|
| 633 | - * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
| 634 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
| 635 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 636 | - * @return null|object|bool null = failure to load or instantiate class object. |
|
| 637 | - * object = class loaded and instantiated successfully. |
|
| 638 | - * bool = fail or success when $load_only is true |
|
| 639 | - */ |
|
| 640 | - protected function _load( |
|
| 641 | - $file_paths = array(), |
|
| 642 | - $class_prefix = 'EE_', |
|
| 643 | - $class_name = false, |
|
| 644 | - $type = 'class', |
|
| 645 | - $arguments = array(), |
|
| 646 | - $cache = true, |
|
| 647 | - $load_only = false |
|
| 648 | - ) { |
|
| 649 | - $class_name = ltrim($class_name, '\\'); |
|
| 650 | - // strip php file extension |
|
| 651 | - $class_name = str_replace('.php', '', trim($class_name)); |
|
| 652 | - // does the class have a prefix ? |
|
| 653 | - if ( ! empty($class_prefix) && $class_prefix != 'addon') { |
|
| 654 | - // make sure $class_prefix is uppercase |
|
| 655 | - $class_prefix = strtoupper(trim($class_prefix)); |
|
| 656 | - // add class prefix ONCE!!! |
|
| 657 | - $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
| 658 | - } |
|
| 659 | - $class_name = $this->_dependency_map->get_alias($class_name); |
|
| 660 | - $class_exists = class_exists($class_name); |
|
| 661 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 662 | - if ($load_only && $class_exists) { |
|
| 663 | - return true; |
|
| 664 | - } |
|
| 665 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 666 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 667 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 668 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
| 669 | - // return object if it's already cached |
|
| 670 | - $cached_class = $this->_get_cached_class($class_name, $class_prefix); |
|
| 671 | - if ($cached_class !== null) { |
|
| 672 | - return $cached_class; |
|
| 673 | - } |
|
| 674 | - } |
|
| 675 | - // if the class doesn't already exist.. then we need to try and find the file and load it |
|
| 676 | - if ( ! $class_exists) { |
|
| 677 | - // get full path to file |
|
| 678 | - $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
| 679 | - // load the file |
|
| 680 | - $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
| 681 | - // if loading failed, or we are only loading a file but NOT instantiating an object |
|
| 682 | - if ( ! $loaded || $load_only) { |
|
| 683 | - // return boolean if only loading, or null if an object was expected |
|
| 684 | - return $load_only ? $loaded : null; |
|
| 685 | - } |
|
| 686 | - } |
|
| 687 | - // instantiate the requested object |
|
| 688 | - $class_obj = $this->_create_object($class_name, $arguments, $type); |
|
| 689 | - if ($this->_cache_on && $cache) { |
|
| 690 | - // save it for later... kinda like gum { : $ |
|
| 691 | - $this->_set_cached_class($class_obj, $class_name, $class_prefix); |
|
| 692 | - } |
|
| 693 | - $this->_cache_on = true; |
|
| 694 | - return $class_obj; |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - |
|
| 698 | - |
|
| 699 | - |
|
| 700 | - /** |
|
| 701 | - * _get_cached_class |
|
| 702 | - * attempts to find a cached version of the requested class |
|
| 703 | - * by looking in the following places: |
|
| 704 | - * $this->{$class_abbreviation} ie: $this->CART |
|
| 705 | - * $this->{$class_name} ie: $this->Some_Class |
|
| 706 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 707 | - * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
| 708 | - * |
|
| 709 | - * @access protected |
|
| 710 | - * @param string $class_name |
|
| 711 | - * @param string $class_prefix |
|
| 712 | - * @return mixed |
|
| 713 | - */ |
|
| 714 | - protected function _get_cached_class($class_name, $class_prefix = '') |
|
| 715 | - { |
|
| 716 | - // have to specify something, but not anything that will conflict |
|
| 717 | - $class_abbreviation = isset($this->_class_abbreviations[ $class_name ]) |
|
| 718 | - ? $this->_class_abbreviations[ $class_name ] |
|
| 719 | - : 'FANCY_BATMAN_PANTS'; |
|
| 720 | - $class_name = str_replace('\\', '_', $class_name); |
|
| 721 | - // check if class has already been loaded, and return it if it has been |
|
| 722 | - if (isset($this->{$class_abbreviation}) && ! is_null($this->{$class_abbreviation})) { |
|
| 723 | - return $this->{$class_abbreviation}; |
|
| 724 | - } |
|
| 725 | - if (isset ($this->{$class_name})) { |
|
| 726 | - return $this->{$class_name}; |
|
| 727 | - } |
|
| 728 | - if (isset ($this->LIB->{$class_name})) { |
|
| 729 | - return $this->LIB->{$class_name}; |
|
| 730 | - } |
|
| 731 | - if ($class_prefix === 'addon' && isset ($this->addons->{$class_name})) { |
|
| 732 | - return $this->addons->{$class_name}; |
|
| 733 | - } |
|
| 734 | - return null; |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - |
|
| 738 | - |
|
| 739 | - /** |
|
| 740 | - * removes a cached version of the requested class |
|
| 741 | - * |
|
| 742 | - * @param string $class_name |
|
| 743 | - * @param boolean $addon |
|
| 744 | - * @return boolean |
|
| 745 | - */ |
|
| 746 | - public function clear_cached_class($class_name, $addon = false) |
|
| 747 | - { |
|
| 748 | - // have to specify something, but not anything that will conflict |
|
| 749 | - $class_abbreviation = isset($this->_class_abbreviations[ $class_name ]) |
|
| 750 | - ? $this->_class_abbreviations[ $class_name ] |
|
| 751 | - : 'FANCY_BATMAN_PANTS'; |
|
| 752 | - $class_name = str_replace('\\', '_', $class_name); |
|
| 753 | - // check if class has already been loaded, and return it if it has been |
|
| 754 | - if (isset($this->{$class_abbreviation}) && ! is_null($this->{$class_abbreviation})) { |
|
| 755 | - $this->{$class_abbreviation} = null; |
|
| 756 | - return true; |
|
| 757 | - } |
|
| 758 | - if (isset($this->{$class_name})) { |
|
| 759 | - $this->{$class_name} = null; |
|
| 760 | - return true; |
|
| 761 | - } |
|
| 762 | - if (isset($this->LIB->{$class_name})) { |
|
| 763 | - unset($this->LIB->{$class_name}); |
|
| 764 | - return true; |
|
| 765 | - } |
|
| 766 | - if ($addon && isset($this->addons->{$class_name})) { |
|
| 767 | - unset($this->addons->{$class_name}); |
|
| 768 | - return true; |
|
| 769 | - } |
|
| 770 | - return false; |
|
| 771 | - } |
|
| 772 | - |
|
| 773 | - |
|
| 774 | - /** |
|
| 775 | - * _resolve_path |
|
| 776 | - * attempts to find a full valid filepath for the requested class. |
|
| 777 | - * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
| 778 | - * then returns that path if the target file has been found and is readable |
|
| 779 | - * |
|
| 780 | - * @access protected |
|
| 781 | - * @param string $class_name |
|
| 782 | - * @param string $type |
|
| 783 | - * @param array $file_paths |
|
| 784 | - * @return string | bool |
|
| 785 | - */ |
|
| 786 | - protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
| 787 | - { |
|
| 788 | - // make sure $file_paths is an array |
|
| 789 | - $file_paths = is_array($file_paths) ? $file_paths : array($file_paths); |
|
| 790 | - // cycle thru paths |
|
| 791 | - foreach ($file_paths as $key => $file_path) { |
|
| 792 | - // convert all separators to proper DS, if no filepath, then use EE_CLASSES |
|
| 793 | - $file_path = $file_path ? str_replace(array('/', '\\'), DS, $file_path) : EE_CLASSES; |
|
| 794 | - // prep file type |
|
| 795 | - $type = ! empty($type) ? trim($type, '.') . '.' : ''; |
|
| 796 | - // build full file path |
|
| 797 | - $file_paths[$key] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
| 798 | - //does the file exist and can be read ? |
|
| 799 | - if (is_readable($file_paths[$key])) { |
|
| 800 | - return $file_paths[$key]; |
|
| 801 | - } |
|
| 802 | - } |
|
| 803 | - return false; |
|
| 804 | - } |
|
| 805 | - |
|
| 806 | - |
|
| 807 | - |
|
| 808 | - /** |
|
| 809 | - * _require_file |
|
| 810 | - * basically just performs a require_once() |
|
| 811 | - * but with some error handling |
|
| 812 | - * |
|
| 813 | - * @access protected |
|
| 814 | - * @param string $path |
|
| 815 | - * @param string $class_name |
|
| 816 | - * @param string $type |
|
| 817 | - * @param array $file_paths |
|
| 818 | - * @return boolean |
|
| 819 | - * @throws \EE_Error |
|
| 820 | - */ |
|
| 821 | - protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
| 822 | - { |
|
| 823 | - // don't give up! you gotta... |
|
| 824 | - try { |
|
| 825 | - //does the file exist and can it be read ? |
|
| 826 | - if ( ! $path) { |
|
| 827 | - // so sorry, can't find the file |
|
| 828 | - throw new EE_Error ( |
|
| 829 | - sprintf( |
|
| 830 | - __('The %1$s file %2$s could not be located or is not readable due to file permissions. Please ensure that the following filepath(s) are correct: %3$s', 'event_espresso'), |
|
| 831 | - trim($type, '.'), |
|
| 832 | - $class_name, |
|
| 833 | - '<br />' . implode(',<br />', $file_paths) |
|
| 834 | - ) |
|
| 835 | - ); |
|
| 836 | - } |
|
| 837 | - // get the file |
|
| 838 | - require_once($path); |
|
| 839 | - // if the class isn't already declared somewhere |
|
| 840 | - if (class_exists($class_name, false) === false) { |
|
| 841 | - // so sorry, not a class |
|
| 842 | - throw new EE_Error( |
|
| 843 | - sprintf( |
|
| 844 | - __('The %s file %s does not appear to contain the %s Class.', 'event_espresso'), |
|
| 845 | - $type, |
|
| 846 | - $path, |
|
| 847 | - $class_name |
|
| 848 | - ) |
|
| 849 | - ); |
|
| 850 | - } |
|
| 851 | - } catch (EE_Error $e) { |
|
| 852 | - $e->get_error(); |
|
| 853 | - return false; |
|
| 854 | - } |
|
| 855 | - return true; |
|
| 856 | - } |
|
| 857 | - |
|
| 858 | - |
|
| 859 | - |
|
| 860 | - /** |
|
| 861 | - * _create_object |
|
| 862 | - * Attempts to instantiate the requested class via any of the |
|
| 863 | - * commonly used instantiation methods employed throughout EE. |
|
| 864 | - * The priority for instantiation is as follows: |
|
| 865 | - * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
| 866 | - * - model objects via their 'new_instance_from_db' method |
|
| 867 | - * - model objects via their 'new_instance' method |
|
| 868 | - * - "singleton" classes" via their 'instance' method |
|
| 869 | - * - standard instantiable classes via their __constructor |
|
| 870 | - * Prior to instantiation, if the classname exists in the dependency_map, |
|
| 871 | - * then the constructor for the requested class will be examined to determine |
|
| 872 | - * if any dependencies exist, and if they can be injected. |
|
| 873 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 874 | - * |
|
| 875 | - * @access protected |
|
| 876 | - * @param string $class_name |
|
| 877 | - * @param array $arguments |
|
| 878 | - * @param string $type |
|
| 879 | - * @return null | object |
|
| 880 | - * @throws \EE_Error |
|
| 881 | - */ |
|
| 882 | - protected function _create_object($class_name, $arguments = array(), $type = '') |
|
| 883 | - { |
|
| 884 | - $class_obj = null; |
|
| 885 | - $instantiation_mode = '0) none'; |
|
| 886 | - // don't give up! you gotta... |
|
| 887 | - try { |
|
| 888 | - // create reflection |
|
| 889 | - $reflector = $this->get_ReflectionClass($class_name); |
|
| 890 | - // make sure arguments are an array |
|
| 891 | - $arguments = is_array($arguments) ? $arguments : array($arguments); |
|
| 892 | - // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
| 893 | - // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
| 894 | - $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
| 895 | - ? $arguments |
|
| 896 | - : array($arguments); |
|
| 897 | - // attempt to inject dependencies ? |
|
| 898 | - if ($this->_dependency_map->has($class_name)) { |
|
| 899 | - $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
| 900 | - } |
|
| 901 | - // instantiate the class if possible |
|
| 902 | - if ($reflector->isAbstract()) { |
|
| 903 | - // nothing to instantiate, loading file was enough |
|
| 904 | - // does not throw an exception so $instantiation_mode is unused |
|
| 905 | - // $instantiation_mode = "1) no constructor abstract class"; |
|
| 906 | - $class_obj = true; |
|
| 907 | - } else if ($reflector->getConstructor() === null && $reflector->isInstantiable() && empty($arguments)) { |
|
| 908 | - // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
| 909 | - $instantiation_mode = "2) no constructor but instantiable"; |
|
| 910 | - $class_obj = $reflector->newInstance(); |
|
| 911 | - } else if (method_exists($class_name, 'new_instance')) { |
|
| 912 | - $instantiation_mode = "4) new_instance()"; |
|
| 913 | - $class_obj = call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
| 914 | - } else if (method_exists($class_name, 'instance')) { |
|
| 915 | - $instantiation_mode = "5) instance()"; |
|
| 916 | - $class_obj = call_user_func_array(array($class_name, 'instance'), $arguments); |
|
| 917 | - } else if ($reflector->isInstantiable()) { |
|
| 918 | - $instantiation_mode = "6) constructor"; |
|
| 919 | - $class_obj = $reflector->newInstanceArgs($arguments); |
|
| 920 | - } else { |
|
| 921 | - // heh ? something's not right ! |
|
| 922 | - throw new EE_Error( |
|
| 923 | - sprintf( |
|
| 924 | - __('The %s file %s could not be instantiated.', 'event_espresso'), |
|
| 925 | - $type, |
|
| 926 | - $class_name |
|
| 927 | - ) |
|
| 928 | - ); |
|
| 929 | - } |
|
| 930 | - } catch (Exception $e) { |
|
| 931 | - if ( ! $e instanceof EE_Error) { |
|
| 932 | - $e = new EE_Error( |
|
| 933 | - sprintf( |
|
| 934 | - __('The following error occurred while attempting to instantiate "%1$s": %2$s %3$s %2$s instantiation mode : %4$s', 'event_espresso'), |
|
| 935 | - $class_name, |
|
| 936 | - '<br />', |
|
| 937 | - $e->getMessage(), |
|
| 938 | - $instantiation_mode |
|
| 939 | - ) |
|
| 940 | - ); |
|
| 941 | - } |
|
| 942 | - $e->get_error(); |
|
| 943 | - } |
|
| 944 | - return $class_obj; |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - |
|
| 948 | - |
|
| 949 | - /** |
|
| 950 | - * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
| 951 | - * @param array $array |
|
| 952 | - * @return bool |
|
| 953 | - */ |
|
| 954 | - protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
| 955 | - { |
|
| 956 | - return ! empty($array) ? array_keys($array) === range(0, count($array) - 1) : true; |
|
| 957 | - } |
|
| 958 | - |
|
| 959 | - |
|
| 960 | - |
|
| 961 | - /** |
|
| 962 | - * getReflectionClass |
|
| 963 | - * checks if a ReflectionClass object has already been generated for a class |
|
| 964 | - * and returns that instead of creating a new one |
|
| 965 | - * |
|
| 966 | - * @access public |
|
| 967 | - * @param string $class_name |
|
| 968 | - * @return ReflectionClass |
|
| 969 | - */ |
|
| 970 | - public function get_ReflectionClass($class_name) |
|
| 971 | - { |
|
| 972 | - if ( |
|
| 973 | - ! isset($this->_reflectors[$class_name]) |
|
| 974 | - || ! $this->_reflectors[$class_name] instanceof ReflectionClass |
|
| 975 | - ) { |
|
| 976 | - $this->_reflectors[$class_name] = new ReflectionClass($class_name); |
|
| 977 | - } |
|
| 978 | - return $this->_reflectors[$class_name]; |
|
| 979 | - } |
|
| 980 | - |
|
| 981 | - |
|
| 982 | - |
|
| 983 | - /** |
|
| 984 | - * _resolve_dependencies |
|
| 985 | - * examines the constructor for the requested class to determine |
|
| 986 | - * if any dependencies exist, and if they can be injected. |
|
| 987 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 988 | - * PLZ NOTE: this is achieved by type hinting the constructor params |
|
| 989 | - * For example: |
|
| 990 | - * if attempting to load a class "Foo" with the following constructor: |
|
| 991 | - * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
| 992 | - * then $bar_class and $grohl_class will be added to the $arguments array, |
|
| 993 | - * but only IF they are NOT already present in the incoming arguments array, |
|
| 994 | - * and the correct classes can be loaded |
|
| 995 | - * |
|
| 996 | - * @access protected |
|
| 997 | - * @param ReflectionClass $reflector |
|
| 998 | - * @param string $class_name |
|
| 999 | - * @param array $arguments |
|
| 1000 | - * @return array |
|
| 1001 | - * @throws \ReflectionException |
|
| 1002 | - */ |
|
| 1003 | - protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, $arguments = array()) |
|
| 1004 | - { |
|
| 1005 | - // let's examine the constructor |
|
| 1006 | - $constructor = $reflector->getConstructor(); |
|
| 1007 | - // whu? huh? nothing? |
|
| 1008 | - if ( ! $constructor) { |
|
| 1009 | - return $arguments; |
|
| 1010 | - } |
|
| 1011 | - // get constructor parameters |
|
| 1012 | - $params = $constructor->getParameters(); |
|
| 1013 | - // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
| 1014 | - $argument_keys = array_keys($arguments); |
|
| 1015 | - // now loop thru all of the constructors expected parameters |
|
| 1016 | - foreach ($params as $index => $param) { |
|
| 1017 | - // is this a dependency for a specific class ? |
|
| 1018 | - $param_class = $param->getClass() ? $param->getClass()->name : null; |
|
| 1019 | - // BUT WAIT !!! This class may be an alias for something else (or getting replaced at runtime) |
|
| 1020 | - $param_class = $this->_dependency_map->has_alias($param_class, $class_name) |
|
| 1021 | - ? $this->_dependency_map->get_alias($param_class, $class_name) |
|
| 1022 | - : $param_class; |
|
| 1023 | - if ( |
|
| 1024 | - // param is not even a class |
|
| 1025 | - empty($param_class) |
|
| 1026 | - // and something already exists in the incoming arguments for this param |
|
| 1027 | - && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
| 1028 | - ) { |
|
| 1029 | - // so let's skip this argument and move on to the next |
|
| 1030 | - continue; |
|
| 1031 | - } |
|
| 1032 | - if ( |
|
| 1033 | - // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
| 1034 | - ! empty($param_class) |
|
| 1035 | - && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
| 1036 | - && $arguments[$argument_keys[$index]] instanceof $param_class |
|
| 1037 | - ) { |
|
| 1038 | - // skip this argument and move on to the next |
|
| 1039 | - continue; |
|
| 1040 | - } |
|
| 1041 | - if ( |
|
| 1042 | - // parameter is type hinted as a class, and should be injected |
|
| 1043 | - ! empty($param_class) |
|
| 1044 | - && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
| 1045 | - ) { |
|
| 1046 | - $arguments = $this->_resolve_dependency($class_name, $param_class, $arguments, $index); |
|
| 1047 | - } else { |
|
| 1048 | - try { |
|
| 1049 | - $arguments[$index] = $param->getDefaultValue(); |
|
| 1050 | - } catch (ReflectionException $e) { |
|
| 1051 | - throw new ReflectionException( |
|
| 1052 | - sprintf( |
|
| 1053 | - __('%1$s for parameter "$%2$s"', 'event_espresso'), |
|
| 1054 | - $e->getMessage(), |
|
| 1055 | - $param->getName() |
|
| 1056 | - ) |
|
| 1057 | - ); |
|
| 1058 | - } |
|
| 1059 | - } |
|
| 1060 | - } |
|
| 1061 | - return $arguments; |
|
| 1062 | - } |
|
| 1063 | - |
|
| 1064 | - |
|
| 1065 | - |
|
| 1066 | - /** |
|
| 1067 | - * @access protected |
|
| 1068 | - * @param string $class_name |
|
| 1069 | - * @param string $param_class |
|
| 1070 | - * @param array $arguments |
|
| 1071 | - * @param mixed $index |
|
| 1072 | - * @return array |
|
| 1073 | - */ |
|
| 1074 | - protected function _resolve_dependency($class_name, $param_class, $arguments, $index) |
|
| 1075 | - { |
|
| 1076 | - $dependency = null; |
|
| 1077 | - // should dependency be loaded from cache ? |
|
| 1078 | - $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency($class_name, $param_class) |
|
| 1079 | - !== EE_Dependency_Map::load_new_object |
|
| 1080 | - ? true |
|
| 1081 | - : false; |
|
| 1082 | - // we might have a dependency... |
|
| 1083 | - // let's MAYBE try and find it in our cache if that's what's been requested |
|
| 1084 | - $cached_class = $cache_on ? $this->_get_cached_class($param_class) : null; |
|
| 1085 | - // and grab it if it exists |
|
| 1086 | - if ($cached_class instanceof $param_class) { |
|
| 1087 | - $dependency = $cached_class; |
|
| 1088 | - } else if ($param_class !== $class_name) { |
|
| 1089 | - // obtain the loader method from the dependency map |
|
| 1090 | - $loader = $this->_dependency_map->class_loader($param_class); |
|
| 1091 | - // is loader a custom closure ? |
|
| 1092 | - if ($loader instanceof Closure) { |
|
| 1093 | - $dependency = $loader(); |
|
| 1094 | - } else { |
|
| 1095 | - // set the cache on property for the recursive loading call |
|
| 1096 | - $this->_cache_on = $cache_on; |
|
| 1097 | - // if not, then let's try and load it via the registry |
|
| 1098 | - if ($loader && method_exists($this, $loader)) { |
|
| 1099 | - $dependency = $this->{$loader}($param_class); |
|
| 1100 | - } else { |
|
| 1101 | - $dependency = $this->create($param_class, array(), $cache_on); |
|
| 1102 | - } |
|
| 1103 | - } |
|
| 1104 | - } |
|
| 1105 | - // did we successfully find the correct dependency ? |
|
| 1106 | - if ($dependency instanceof $param_class) { |
|
| 1107 | - // then let's inject it into the incoming array of arguments at the correct location |
|
| 1108 | - if (isset($argument_keys[$index])) { |
|
| 1109 | - $arguments[$argument_keys[$index]] = $dependency; |
|
| 1110 | - } else { |
|
| 1111 | - $arguments[$index] = $dependency; |
|
| 1112 | - } |
|
| 1113 | - } |
|
| 1114 | - return $arguments; |
|
| 1115 | - } |
|
| 1116 | - |
|
| 1117 | - |
|
| 1118 | - |
|
| 1119 | - /** |
|
| 1120 | - * _set_cached_class |
|
| 1121 | - * attempts to cache the instantiated class locally |
|
| 1122 | - * in one of the following places, in the following order: |
|
| 1123 | - * $this->{class_abbreviation} ie: $this->CART |
|
| 1124 | - * $this->{$class_name} ie: $this->Some_Class |
|
| 1125 | - * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
| 1126 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 1127 | - * |
|
| 1128 | - * @access protected |
|
| 1129 | - * @param object $class_obj |
|
| 1130 | - * @param string $class_name |
|
| 1131 | - * @param string $class_prefix |
|
| 1132 | - * @return void |
|
| 1133 | - */ |
|
| 1134 | - protected function _set_cached_class($class_obj, $class_name, $class_prefix = '') |
|
| 1135 | - { |
|
| 1136 | - if (empty($class_obj)) { |
|
| 1137 | - return; |
|
| 1138 | - } |
|
| 1139 | - // return newly instantiated class |
|
| 1140 | - if (isset($this->_class_abbreviations[$class_name])) { |
|
| 1141 | - $class_abbreviation = $this->_class_abbreviations[$class_name]; |
|
| 1142 | - $this->{$class_abbreviation} = $class_obj; |
|
| 1143 | - return; |
|
| 1144 | - } |
|
| 1145 | - $class_name = str_replace('\\', '_', $class_name); |
|
| 1146 | - if (property_exists($this, $class_name)) { |
|
| 1147 | - $this->{$class_name} = $class_obj; |
|
| 1148 | - return; |
|
| 1149 | - } |
|
| 1150 | - if ($class_prefix === 'addon') { |
|
| 1151 | - $this->addons->{$class_name} = $class_obj; |
|
| 1152 | - return; |
|
| 1153 | - } |
|
| 1154 | - $this->LIB->{$class_name} = $class_obj; |
|
| 1155 | - } |
|
| 1156 | - |
|
| 1157 | - |
|
| 1158 | - |
|
| 1159 | - /** |
|
| 1160 | - * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
| 1161 | - * |
|
| 1162 | - * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
| 1163 | - * in the EE_Dependency_Map::$_class_loaders array, |
|
| 1164 | - * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
| 1165 | - * @param array $arguments |
|
| 1166 | - * @return object |
|
| 1167 | - */ |
|
| 1168 | - public static function factory($classname, $arguments = array()) |
|
| 1169 | - { |
|
| 1170 | - $loader = self::instance()->_dependency_map->class_loader($classname); |
|
| 1171 | - if ($loader instanceof Closure) { |
|
| 1172 | - return $loader($arguments); |
|
| 1173 | - } |
|
| 1174 | - if (method_exists(EE_Registry::instance(), $loader)) { |
|
| 1175 | - return EE_Registry::instance()->{$loader}($classname, $arguments); |
|
| 1176 | - } |
|
| 1177 | - return null; |
|
| 1178 | - } |
|
| 1179 | - |
|
| 1180 | - |
|
| 1181 | - |
|
| 1182 | - /** |
|
| 1183 | - * Gets the addon by its name/slug (not classname. For that, just |
|
| 1184 | - * use the classname as the property name on EE_Config::instance()->addons) |
|
| 1185 | - * |
|
| 1186 | - * @param string $name |
|
| 1187 | - * @return EE_Addon |
|
| 1188 | - */ |
|
| 1189 | - public function get_addon_by_name($name) |
|
| 1190 | - { |
|
| 1191 | - foreach ($this->addons as $addon) { |
|
| 1192 | - if ($addon->name() == $name) { |
|
| 1193 | - return $addon; |
|
| 1194 | - } |
|
| 1195 | - } |
|
| 1196 | - return null; |
|
| 1197 | - } |
|
| 1198 | - |
|
| 1199 | - |
|
| 1200 | - |
|
| 1201 | - /** |
|
| 1202 | - * 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 |
|
| 1203 | - * the addon's classname. So if you just want to get the addon by classname, use EE_Config::instance()->addons->{classname} |
|
| 1204 | - * |
|
| 1205 | - * @return EE_Addon[] where the KEYS are the addon's name() |
|
| 1206 | - */ |
|
| 1207 | - public function get_addons_by_name() |
|
| 1208 | - { |
|
| 1209 | - $addons = array(); |
|
| 1210 | - foreach ($this->addons as $addon) { |
|
| 1211 | - $addons[$addon->name()] = $addon; |
|
| 1212 | - } |
|
| 1213 | - return $addons; |
|
| 1214 | - } |
|
| 1215 | - |
|
| 1216 | - |
|
| 1217 | - |
|
| 1218 | - /** |
|
| 1219 | - * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
| 1220 | - * a stale copy of it around |
|
| 1221 | - * |
|
| 1222 | - * @param string $model_name |
|
| 1223 | - * @return \EEM_Base |
|
| 1224 | - * @throws \EE_Error |
|
| 1225 | - */ |
|
| 1226 | - public function reset_model($model_name) |
|
| 1227 | - { |
|
| 1228 | - $model_class_name = strpos($model_name, 'EEM_') !== 0 ? "EEM_{$model_name}" : $model_name; |
|
| 1229 | - if ( ! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
| 1230 | - return null; |
|
| 1231 | - } |
|
| 1232 | - //get that model reset it and make sure we nuke the old reference to it |
|
| 1233 | - if ($this->LIB->{$model_class_name} instanceof $model_class_name && is_callable(array($model_class_name, 'reset'))) { |
|
| 1234 | - $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
| 1235 | - } else { |
|
| 1236 | - throw new EE_Error(sprintf(__('Model %s does not have a method "reset"', 'event_espresso'), $model_name)); |
|
| 1237 | - } |
|
| 1238 | - return $this->LIB->{$model_class_name}; |
|
| 1239 | - } |
|
| 1240 | - |
|
| 1241 | - |
|
| 1242 | - |
|
| 1243 | - /** |
|
| 1244 | - * Resets the registry. |
|
| 1245 | - * The criteria for what gets reset is based on what can be shared between sites on the same request when switch_to_blog |
|
| 1246 | - * is used in a multisite install. Here is a list of things that are NOT reset. |
|
| 1247 | - * - $_dependency_map |
|
| 1248 | - * - $_class_abbreviations |
|
| 1249 | - * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
| 1250 | - * - $REQ: Still on the same request so no need to change. |
|
| 1251 | - * - $CAP: There is no site specific state in the EE_Capability class. |
|
| 1252 | - * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only one Session |
|
| 1253 | - * can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
| 1254 | - * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
| 1255 | - * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
| 1256 | - * switch or on the restore. |
|
| 1257 | - * - $modules |
|
| 1258 | - * - $shortcodes |
|
| 1259 | - * - $widgets |
|
| 1260 | - * |
|
| 1261 | - * @param boolean $hard whether to reset data in the database too, or just refresh |
|
| 1262 | - * the Registry to its state at the beginning of the request |
|
| 1263 | - * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
| 1264 | - * or just reset without re-instantiating (handy to set to FALSE if you're not sure if you CAN |
|
| 1265 | - * currently reinstantiate the singletons at the moment) |
|
| 1266 | - * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so client |
|
| 1267 | - * code instead can just change the model context to a different blog id if necessary |
|
| 1268 | - * @return EE_Registry |
|
| 1269 | - */ |
|
| 1270 | - public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
| 1271 | - { |
|
| 1272 | - $instance = self::instance(); |
|
| 1273 | - EEH_Activation::reset(); |
|
| 1274 | - //properties that get reset |
|
| 1275 | - $instance->_cache_on = true; |
|
| 1276 | - $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
| 1277 | - $instance->CART = null; |
|
| 1278 | - $instance->MRM = null; |
|
| 1279 | - $instance->AssetsRegistry = null; |
|
| 1280 | - $instance->AssetsRegistry = $instance->create('EventEspresso\core\services\assets\Registry'); |
|
| 1281 | - //messages reset |
|
| 1282 | - EED_Messages::reset(); |
|
| 1283 | - if ($reset_models) { |
|
| 1284 | - foreach (array_keys($instance->non_abstract_db_models) as $model_name) { |
|
| 1285 | - $instance->reset_model($model_name); |
|
| 1286 | - } |
|
| 1287 | - } |
|
| 1288 | - $instance->LIB = new stdClass(); |
|
| 1289 | - return $instance; |
|
| 1290 | - } |
|
| 1291 | - |
|
| 1292 | - |
|
| 1293 | - |
|
| 1294 | - /** |
|
| 1295 | - * @override magic methods |
|
| 1296 | - * @return void |
|
| 1297 | - */ |
|
| 1298 | - public final function __destruct() |
|
| 1299 | - { |
|
| 1300 | - } |
|
| 1301 | - |
|
| 1302 | - |
|
| 1303 | - |
|
| 1304 | - /** |
|
| 1305 | - * @param $a |
|
| 1306 | - * @param $b |
|
| 1307 | - */ |
|
| 1308 | - public final function __call($a, $b) |
|
| 1309 | - { |
|
| 1310 | - } |
|
| 1311 | - |
|
| 1312 | - |
|
| 1313 | - |
|
| 1314 | - /** |
|
| 1315 | - * @param $a |
|
| 1316 | - */ |
|
| 1317 | - public final function __get($a) |
|
| 1318 | - { |
|
| 1319 | - } |
|
| 1320 | - |
|
| 1321 | - |
|
| 1322 | - |
|
| 1323 | - /** |
|
| 1324 | - * @param $a |
|
| 1325 | - * @param $b |
|
| 1326 | - */ |
|
| 1327 | - public final function __set($a, $b) |
|
| 1328 | - { |
|
| 1329 | - } |
|
| 1330 | - |
|
| 1331 | - |
|
| 1332 | - |
|
| 1333 | - /** |
|
| 1334 | - * @param $a |
|
| 1335 | - */ |
|
| 1336 | - public final function __isset($a) |
|
| 1337 | - { |
|
| 1338 | - } |
|
| 19 | + /** |
|
| 20 | + * EE_Registry Object |
|
| 21 | + * |
|
| 22 | + * @var EE_Registry $_instance |
|
| 23 | + * @access private |
|
| 24 | + */ |
|
| 25 | + private static $_instance = null; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var EE_Dependency_Map $_dependency_map |
|
| 29 | + * @access protected |
|
| 30 | + */ |
|
| 31 | + protected $_dependency_map = null; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var array $_class_abbreviations |
|
| 35 | + * @access protected |
|
| 36 | + */ |
|
| 37 | + protected $_class_abbreviations = array(); |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @access public |
|
| 41 | + * @var \EventEspresso\core\services\commands\CommandBusInterface $BUS |
|
| 42 | + */ |
|
| 43 | + public $BUS; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * EE_Cart Object |
|
| 47 | + * |
|
| 48 | + * @access public |
|
| 49 | + * @var EE_Cart $CART |
|
| 50 | + */ |
|
| 51 | + public $CART = null; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * EE_Config Object |
|
| 55 | + * |
|
| 56 | + * @access public |
|
| 57 | + * @var EE_Config $CFG |
|
| 58 | + */ |
|
| 59 | + public $CFG = null; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * EE_Network_Config Object |
|
| 63 | + * |
|
| 64 | + * @access public |
|
| 65 | + * @var EE_Network_Config $NET_CFG |
|
| 66 | + */ |
|
| 67 | + public $NET_CFG = null; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * StdClass object for storing library classes in |
|
| 71 | + * |
|
| 72 | + * @public LIB |
|
| 73 | + * @var StdClass $LIB |
|
| 74 | + */ |
|
| 75 | + public $LIB = null; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * EE_Request_Handler Object |
|
| 79 | + * |
|
| 80 | + * @access public |
|
| 81 | + * @var EE_Request_Handler $REQ |
|
| 82 | + */ |
|
| 83 | + public $REQ = null; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * EE_Session Object |
|
| 87 | + * |
|
| 88 | + * @access public |
|
| 89 | + * @var EE_Session $SSN |
|
| 90 | + */ |
|
| 91 | + public $SSN = null; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * holds the ee capabilities object. |
|
| 95 | + * |
|
| 96 | + * @since 4.5.0 |
|
| 97 | + * @var EE_Capabilities |
|
| 98 | + */ |
|
| 99 | + public $CAP = null; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * holds the EE_Message_Resource_Manager object. |
|
| 103 | + * |
|
| 104 | + * @since 4.9.0 |
|
| 105 | + * @var EE_Message_Resource_Manager |
|
| 106 | + */ |
|
| 107 | + public $MRM = null; |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Holds the Assets Registry instance |
|
| 112 | + * @var Registry |
|
| 113 | + */ |
|
| 114 | + public $AssetsRegistry = null; |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * $addons - StdClass object for holding addons which have registered themselves to work with EE core |
|
| 118 | + * |
|
| 119 | + * @access public |
|
| 120 | + * @var EE_Addon[] |
|
| 121 | + */ |
|
| 122 | + public $addons = null; |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * $models |
|
| 126 | + * @access public |
|
| 127 | + * @var EEM_Base[] $models keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
| 128 | + */ |
|
| 129 | + public $models = array(); |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * $modules |
|
| 133 | + * @access public |
|
| 134 | + * @var EED_Module[] $modules |
|
| 135 | + */ |
|
| 136 | + public $modules = null; |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * $shortcodes |
|
| 140 | + * @access public |
|
| 141 | + * @var EES_Shortcode[] $shortcodes |
|
| 142 | + */ |
|
| 143 | + public $shortcodes = null; |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * $widgets |
|
| 147 | + * @access public |
|
| 148 | + * @var WP_Widget[] $widgets |
|
| 149 | + */ |
|
| 150 | + public $widgets = null; |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * $non_abstract_db_models |
|
| 154 | + * @access public |
|
| 155 | + * @var array this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
| 156 | + * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
| 157 | + * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
| 158 | + * classnames (eg "EEM_Event") |
|
| 159 | + */ |
|
| 160 | + public $non_abstract_db_models = array(); |
|
| 161 | + |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * $i18n_js_strings - internationalization for JS strings |
|
| 165 | + * usage: EE_Registry::i18n_js_strings['string_key'] = __( 'string to translate.', 'event_espresso' ); |
|
| 166 | + * in js file: var translatedString = eei18n.string_key; |
|
| 167 | + * |
|
| 168 | + * @access public |
|
| 169 | + * @var array |
|
| 170 | + */ |
|
| 171 | + public static $i18n_js_strings = array(); |
|
| 172 | + |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * $main_file - path to espresso.php |
|
| 176 | + * |
|
| 177 | + * @access public |
|
| 178 | + * @var array |
|
| 179 | + */ |
|
| 180 | + public $main_file; |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * array of ReflectionClass objects where the key is the class name |
|
| 184 | + * |
|
| 185 | + * @access public |
|
| 186 | + * @var ReflectionClass[] |
|
| 187 | + */ |
|
| 188 | + public $_reflectors; |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
| 192 | + * |
|
| 193 | + * @access protected |
|
| 194 | + * @var boolean $_cache_on |
|
| 195 | + */ |
|
| 196 | + protected $_cache_on = true; |
|
| 197 | + |
|
| 198 | + |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * @singleton method used to instantiate class object |
|
| 202 | + * @access public |
|
| 203 | + * @param \EE_Dependency_Map $dependency_map |
|
| 204 | + * @return \EE_Registry instance |
|
| 205 | + */ |
|
| 206 | + public static function instance(\EE_Dependency_Map $dependency_map = null) |
|
| 207 | + { |
|
| 208 | + // check if class object is instantiated |
|
| 209 | + if ( ! self::$_instance instanceof EE_Registry) { |
|
| 210 | + self::$_instance = new EE_Registry($dependency_map); |
|
| 211 | + } |
|
| 212 | + return self::$_instance; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + *protected constructor to prevent direct creation |
|
| 219 | + * |
|
| 220 | + * @Constructor |
|
| 221 | + * @access protected |
|
| 222 | + * @param \EE_Dependency_Map $dependency_map |
|
| 223 | + */ |
|
| 224 | + protected function __construct(\EE_Dependency_Map $dependency_map) |
|
| 225 | + { |
|
| 226 | + $this->_dependency_map = $dependency_map; |
|
| 227 | + $this->LIB = new stdClass(); |
|
| 228 | + $this->addons = new stdClass(); |
|
| 229 | + $this->modules = new stdClass(); |
|
| 230 | + $this->shortcodes = new stdClass(); |
|
| 231 | + $this->widgets = new stdClass(); |
|
| 232 | + add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * initialize |
|
| 239 | + */ |
|
| 240 | + public function initialize() |
|
| 241 | + { |
|
| 242 | + $this->_class_abbreviations = apply_filters( |
|
| 243 | + 'FHEE__EE_Registry____construct___class_abbreviations', |
|
| 244 | + array( |
|
| 245 | + 'EE_Config' => 'CFG', |
|
| 246 | + 'EE_Session' => 'SSN', |
|
| 247 | + 'EE_Capabilities' => 'CAP', |
|
| 248 | + 'EE_Cart' => 'CART', |
|
| 249 | + 'EE_Network_Config' => 'NET_CFG', |
|
| 250 | + 'EE_Request_Handler' => 'REQ', |
|
| 251 | + 'EE_Message_Resource_Manager' => 'MRM', |
|
| 252 | + 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
| 253 | + 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
| 254 | + ) |
|
| 255 | + ); |
|
| 256 | + $this->load_core('Base', array(), true); |
|
| 257 | + // add our request and response objects to the cache |
|
| 258 | + $request_loader = $this->_dependency_map->class_loader('EE_Request'); |
|
| 259 | + $this->_set_cached_class( |
|
| 260 | + $request_loader(), |
|
| 261 | + 'EE_Request' |
|
| 262 | + ); |
|
| 263 | + $response_loader = $this->_dependency_map->class_loader('EE_Response'); |
|
| 264 | + $this->_set_cached_class( |
|
| 265 | + $response_loader(), |
|
| 266 | + 'EE_Response' |
|
| 267 | + ); |
|
| 268 | + add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * init |
|
| 275 | + * |
|
| 276 | + * @access public |
|
| 277 | + * @return void |
|
| 278 | + */ |
|
| 279 | + public function init() |
|
| 280 | + { |
|
| 281 | + // Get current page protocol |
|
| 282 | + $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
| 283 | + // Output admin-ajax.php URL with same protocol as current page |
|
| 284 | + self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
| 285 | + self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * localize_i18n_js_strings |
|
| 292 | + * |
|
| 293 | + * @return string |
|
| 294 | + */ |
|
| 295 | + public static function localize_i18n_js_strings() |
|
| 296 | + { |
|
| 297 | + $i18n_js_strings = (array)EE_Registry::$i18n_js_strings; |
|
| 298 | + foreach ($i18n_js_strings as $key => $value) { |
|
| 299 | + if (is_scalar($value)) { |
|
| 300 | + $i18n_js_strings[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8'); |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + return "/* <![CDATA[ */ var eei18n = " . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * @param mixed string | EED_Module $module |
|
| 310 | + */ |
|
| 311 | + public function add_module($module) |
|
| 312 | + { |
|
| 313 | + if ($module instanceof EED_Module) { |
|
| 314 | + $module_class = get_class($module); |
|
| 315 | + $this->modules->{$module_class} = $module; |
|
| 316 | + } else { |
|
| 317 | + if ( ! class_exists('EE_Module_Request_Router')) { |
|
| 318 | + $this->load_core('Module_Request_Router'); |
|
| 319 | + } |
|
| 320 | + $this->modules->{$module} = EE_Module_Request_Router::module_factory($module); |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * @param string $module_name |
|
| 328 | + * @return mixed EED_Module | NULL |
|
| 329 | + */ |
|
| 330 | + public function get_module($module_name = '') |
|
| 331 | + { |
|
| 332 | + return isset($this->modules->{$module_name}) ? $this->modules->{$module_name} : null; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * loads core classes - must be singletons |
|
| 339 | + * |
|
| 340 | + * @access public |
|
| 341 | + * @param string $class_name - simple class name ie: session |
|
| 342 | + * @param mixed $arguments |
|
| 343 | + * @param bool $load_only |
|
| 344 | + * @return mixed |
|
| 345 | + */ |
|
| 346 | + public function load_core($class_name, $arguments = array(), $load_only = false) |
|
| 347 | + { |
|
| 348 | + $core_paths = apply_filters( |
|
| 349 | + 'FHEE__EE_Registry__load_core__core_paths', |
|
| 350 | + array( |
|
| 351 | + EE_CORE, |
|
| 352 | + EE_ADMIN, |
|
| 353 | + EE_CPTS, |
|
| 354 | + EE_CORE . 'data_migration_scripts' . DS, |
|
| 355 | + EE_CORE . 'request_stack' . DS, |
|
| 356 | + EE_CORE . 'middleware' . DS, |
|
| 357 | + ) |
|
| 358 | + ); |
|
| 359 | + // retrieve instantiated class |
|
| 360 | + return $this->_load($core_paths, 'EE_', $class_name, 'core', $arguments, true, $load_only); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * loads service classes |
|
| 367 | + * |
|
| 368 | + * @access public |
|
| 369 | + * @param string $class_name - simple class name ie: session |
|
| 370 | + * @param mixed $arguments |
|
| 371 | + * @param bool $load_only |
|
| 372 | + * @return mixed |
|
| 373 | + */ |
|
| 374 | + public function load_service($class_name, $arguments = array(), $load_only = false) |
|
| 375 | + { |
|
| 376 | + $service_paths = apply_filters( |
|
| 377 | + 'FHEE__EE_Registry__load_service__service_paths', |
|
| 378 | + array( |
|
| 379 | + EE_CORE . 'services' . DS, |
|
| 380 | + ) |
|
| 381 | + ); |
|
| 382 | + // retrieve instantiated class |
|
| 383 | + return $this->_load($service_paths, 'EE_', $class_name, 'class', $arguments, true, $load_only); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * loads data_migration_scripts |
|
| 390 | + * |
|
| 391 | + * @access public |
|
| 392 | + * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
| 393 | + * @param mixed $arguments |
|
| 394 | + * @return EE_Data_Migration_Script_Base|mixed |
|
| 395 | + */ |
|
| 396 | + public function load_dms($class_name, $arguments = array()) |
|
| 397 | + { |
|
| 398 | + // retrieve instantiated class |
|
| 399 | + return $this->_load( |
|
| 400 | + EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), |
|
| 401 | + 'EE_DMS_', $class_name, 'dms', $arguments, false, false |
|
| 402 | + ); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * loads object creating classes - must be singletons |
|
| 409 | + * |
|
| 410 | + * @param string $class_name - simple class name ie: attendee |
|
| 411 | + * @param mixed $arguments - an array of arguments to pass to the class |
|
| 412 | + * @param bool $from_db - deprecated |
|
| 413 | + * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
| 414 | + * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate (default) |
|
| 415 | + * @return EE_Base_Class | bool |
|
| 416 | + */ |
|
| 417 | + public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
| 418 | + { |
|
| 419 | + $paths = apply_filters('FHEE__EE_Registry__load_class__paths', array( |
|
| 420 | + EE_CORE, |
|
| 421 | + EE_CLASSES, |
|
| 422 | + EE_BUSINESS, |
|
| 423 | + )); |
|
| 424 | + // retrieve instantiated class |
|
| 425 | + return $this->_load($paths, 'EE_', $class_name, 'class', $arguments, $cache, $load_only); |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * loads helper classes - must be singletons |
|
| 432 | + * |
|
| 433 | + * @param string $class_name - simple class name ie: price |
|
| 434 | + * @param mixed $arguments |
|
| 435 | + * @param bool $load_only |
|
| 436 | + * @return EEH_Base | bool |
|
| 437 | + */ |
|
| 438 | + public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
| 439 | + { |
|
| 440 | + // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
| 441 | + $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
| 442 | + // retrieve instantiated class |
|
| 443 | + return $this->_load($helper_paths, 'EEH_', $class_name, 'helper', $arguments, true, $load_only); |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * loads core classes - must be singletons |
|
| 450 | + * |
|
| 451 | + * @access public |
|
| 452 | + * @param string $class_name - simple class name ie: session |
|
| 453 | + * @param mixed $arguments |
|
| 454 | + * @param bool $load_only |
|
| 455 | + * @param bool $cache whether to cache the object or not. |
|
| 456 | + * @return mixed |
|
| 457 | + */ |
|
| 458 | + public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
| 459 | + { |
|
| 460 | + $paths = array( |
|
| 461 | + EE_LIBRARIES, |
|
| 462 | + EE_LIBRARIES . 'messages' . DS, |
|
| 463 | + EE_LIBRARIES . 'shortcodes' . DS, |
|
| 464 | + EE_LIBRARIES . 'qtips' . DS, |
|
| 465 | + EE_LIBRARIES . 'payment_methods' . DS, |
|
| 466 | + ); |
|
| 467 | + // retrieve instantiated class |
|
| 468 | + return $this->_load($paths, 'EE_', $class_name, 'lib', $arguments, $cache, $load_only); |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + |
|
| 472 | + |
|
| 473 | + /** |
|
| 474 | + * loads model classes - must be singletons |
|
| 475 | + * |
|
| 476 | + * @param string $class_name - simple class name ie: price |
|
| 477 | + * @param mixed $arguments |
|
| 478 | + * @param bool $load_only |
|
| 479 | + * @return EEM_Base | bool |
|
| 480 | + */ |
|
| 481 | + public function load_model($class_name, $arguments = array(), $load_only = false) |
|
| 482 | + { |
|
| 483 | + $paths = apply_filters('FHEE__EE_Registry__load_model__paths', array( |
|
| 484 | + EE_MODELS, |
|
| 485 | + EE_CORE, |
|
| 486 | + )); |
|
| 487 | + // retrieve instantiated class |
|
| 488 | + return $this->_load($paths, 'EEM_', $class_name, 'model', $arguments, true, $load_only); |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + |
|
| 492 | + |
|
| 493 | + /** |
|
| 494 | + * loads model classes - must be singletons |
|
| 495 | + * |
|
| 496 | + * @param string $class_name - simple class name ie: price |
|
| 497 | + * @param mixed $arguments |
|
| 498 | + * @param bool $load_only |
|
| 499 | + * @return mixed | bool |
|
| 500 | + */ |
|
| 501 | + public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
| 502 | + { |
|
| 503 | + $paths = array( |
|
| 504 | + EE_MODELS . 'fields' . DS, |
|
| 505 | + EE_MODELS . 'helpers' . DS, |
|
| 506 | + EE_MODELS . 'relations' . DS, |
|
| 507 | + EE_MODELS . 'strategies' . DS, |
|
| 508 | + ); |
|
| 509 | + // retrieve instantiated class |
|
| 510 | + return $this->_load($paths, 'EE_', $class_name, '', $arguments, true, $load_only); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + |
|
| 514 | + |
|
| 515 | + /** |
|
| 516 | + * Determines if $model_name is the name of an actual EE model. |
|
| 517 | + * |
|
| 518 | + * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
| 519 | + * @return boolean |
|
| 520 | + */ |
|
| 521 | + public function is_model_name($model_name) |
|
| 522 | + { |
|
| 523 | + return isset($this->models[$model_name]) ? true : false; |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + |
|
| 527 | + |
|
| 528 | + /** |
|
| 529 | + * generic class loader |
|
| 530 | + * |
|
| 531 | + * @param string $path_to_file - directory path to file location, not including filename |
|
| 532 | + * @param string $file_name - file name ie: my_file.php, including extension |
|
| 533 | + * @param string $type - file type - core? class? helper? model? |
|
| 534 | + * @param mixed $arguments |
|
| 535 | + * @param bool $load_only |
|
| 536 | + * @return mixed |
|
| 537 | + */ |
|
| 538 | + public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
| 539 | + { |
|
| 540 | + // retrieve instantiated class |
|
| 541 | + return $this->_load($path_to_file, '', $file_name, $type, $arguments, true, $load_only); |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * load_addon |
|
| 548 | + * |
|
| 549 | + * @param string $path_to_file - directory path to file location, not including filename |
|
| 550 | + * @param string $class_name - full class name ie: My_Class |
|
| 551 | + * @param string $type - file type - core? class? helper? model? |
|
| 552 | + * @param mixed $arguments |
|
| 553 | + * @param bool $load_only |
|
| 554 | + * @return EE_Addon |
|
| 555 | + */ |
|
| 556 | + public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
| 557 | + { |
|
| 558 | + // retrieve instantiated class |
|
| 559 | + return $this->_load($path_to_file, 'addon', $class_name, $type, $arguments, true, $load_only); |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + |
|
| 563 | + |
|
| 564 | + /** |
|
| 565 | + * instantiates, caches, and automatically resolves dependencies |
|
| 566 | + * for classes that use a Fully Qualified Class Name. |
|
| 567 | + * if the class is not capable of being loaded using PSR-4 autoloading, |
|
| 568 | + * then you need to use one of the existing load_*() methods |
|
| 569 | + * which can resolve the classname and filepath from the passed arguments |
|
| 570 | + * |
|
| 571 | + * @param bool|string $class_name Fully Qualified Class Name |
|
| 572 | + * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
| 573 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
| 574 | + * @param bool $from_db some classes are instantiated from the db |
|
| 575 | + * and thus call a different method to instantiate |
|
| 576 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 577 | + * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
| 578 | + * @return mixed null = failure to load or instantiate class object. |
|
| 579 | + * object = class loaded and instantiated successfully. |
|
| 580 | + * bool = fail or success when $load_only is true |
|
| 581 | + */ |
|
| 582 | + public function create( |
|
| 583 | + $class_name = false, |
|
| 584 | + $arguments = array(), |
|
| 585 | + $cache = true, |
|
| 586 | + $load_only = false, |
|
| 587 | + $addon = false |
|
| 588 | + ) { |
|
| 589 | + $class_name = ltrim($class_name, '\\'); |
|
| 590 | + $class_name = $this->_dependency_map->get_alias($class_name); |
|
| 591 | + if ( ! class_exists($class_name)) { |
|
| 592 | + // maybe the class is registered with a preceding \ |
|
| 593 | + $class_name = strpos($class_name, '\\') !== 0 ? '\\' . $class_name : $class_name; |
|
| 594 | + // still doesn't exist ? |
|
| 595 | + if ( ! class_exists($class_name)) { |
|
| 596 | + return null; |
|
| 597 | + } |
|
| 598 | + } |
|
| 599 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 600 | + if ($load_only) { |
|
| 601 | + return true; |
|
| 602 | + } |
|
| 603 | + $addon = $addon ? 'addon' : ''; |
|
| 604 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 605 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 606 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 607 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
| 608 | + // return object if it's already cached |
|
| 609 | + $cached_class = $this->_get_cached_class($class_name, $addon); |
|
| 610 | + if ($cached_class !== null) { |
|
| 611 | + return $cached_class; |
|
| 612 | + } |
|
| 613 | + } |
|
| 614 | + // instantiate the requested object |
|
| 615 | + $class_obj = $this->_create_object($class_name, $arguments, $addon); |
|
| 616 | + if ($this->_cache_on && $cache) { |
|
| 617 | + // save it for later... kinda like gum { : $ |
|
| 618 | + $this->_set_cached_class($class_obj, $class_name, $addon); |
|
| 619 | + } |
|
| 620 | + $this->_cache_on = true; |
|
| 621 | + return $class_obj; |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * instantiates, caches, and injects dependencies for classes |
|
| 628 | + * |
|
| 629 | + * @param array $file_paths an array of paths to folders to look in |
|
| 630 | + * @param string $class_prefix EE or EEM or... ??? |
|
| 631 | + * @param bool|string $class_name $class name |
|
| 632 | + * @param string $type file type - core? class? helper? model? |
|
| 633 | + * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
| 634 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
| 635 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 636 | + * @return null|object|bool null = failure to load or instantiate class object. |
|
| 637 | + * object = class loaded and instantiated successfully. |
|
| 638 | + * bool = fail or success when $load_only is true |
|
| 639 | + */ |
|
| 640 | + protected function _load( |
|
| 641 | + $file_paths = array(), |
|
| 642 | + $class_prefix = 'EE_', |
|
| 643 | + $class_name = false, |
|
| 644 | + $type = 'class', |
|
| 645 | + $arguments = array(), |
|
| 646 | + $cache = true, |
|
| 647 | + $load_only = false |
|
| 648 | + ) { |
|
| 649 | + $class_name = ltrim($class_name, '\\'); |
|
| 650 | + // strip php file extension |
|
| 651 | + $class_name = str_replace('.php', '', trim($class_name)); |
|
| 652 | + // does the class have a prefix ? |
|
| 653 | + if ( ! empty($class_prefix) && $class_prefix != 'addon') { |
|
| 654 | + // make sure $class_prefix is uppercase |
|
| 655 | + $class_prefix = strtoupper(trim($class_prefix)); |
|
| 656 | + // add class prefix ONCE!!! |
|
| 657 | + $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
| 658 | + } |
|
| 659 | + $class_name = $this->_dependency_map->get_alias($class_name); |
|
| 660 | + $class_exists = class_exists($class_name); |
|
| 661 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 662 | + if ($load_only && $class_exists) { |
|
| 663 | + return true; |
|
| 664 | + } |
|
| 665 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 666 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 667 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 668 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
| 669 | + // return object if it's already cached |
|
| 670 | + $cached_class = $this->_get_cached_class($class_name, $class_prefix); |
|
| 671 | + if ($cached_class !== null) { |
|
| 672 | + return $cached_class; |
|
| 673 | + } |
|
| 674 | + } |
|
| 675 | + // if the class doesn't already exist.. then we need to try and find the file and load it |
|
| 676 | + if ( ! $class_exists) { |
|
| 677 | + // get full path to file |
|
| 678 | + $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
| 679 | + // load the file |
|
| 680 | + $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
| 681 | + // if loading failed, or we are only loading a file but NOT instantiating an object |
|
| 682 | + if ( ! $loaded || $load_only) { |
|
| 683 | + // return boolean if only loading, or null if an object was expected |
|
| 684 | + return $load_only ? $loaded : null; |
|
| 685 | + } |
|
| 686 | + } |
|
| 687 | + // instantiate the requested object |
|
| 688 | + $class_obj = $this->_create_object($class_name, $arguments, $type); |
|
| 689 | + if ($this->_cache_on && $cache) { |
|
| 690 | + // save it for later... kinda like gum { : $ |
|
| 691 | + $this->_set_cached_class($class_obj, $class_name, $class_prefix); |
|
| 692 | + } |
|
| 693 | + $this->_cache_on = true; |
|
| 694 | + return $class_obj; |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + |
|
| 698 | + |
|
| 699 | + |
|
| 700 | + /** |
|
| 701 | + * _get_cached_class |
|
| 702 | + * attempts to find a cached version of the requested class |
|
| 703 | + * by looking in the following places: |
|
| 704 | + * $this->{$class_abbreviation} ie: $this->CART |
|
| 705 | + * $this->{$class_name} ie: $this->Some_Class |
|
| 706 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 707 | + * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
| 708 | + * |
|
| 709 | + * @access protected |
|
| 710 | + * @param string $class_name |
|
| 711 | + * @param string $class_prefix |
|
| 712 | + * @return mixed |
|
| 713 | + */ |
|
| 714 | + protected function _get_cached_class($class_name, $class_prefix = '') |
|
| 715 | + { |
|
| 716 | + // have to specify something, but not anything that will conflict |
|
| 717 | + $class_abbreviation = isset($this->_class_abbreviations[ $class_name ]) |
|
| 718 | + ? $this->_class_abbreviations[ $class_name ] |
|
| 719 | + : 'FANCY_BATMAN_PANTS'; |
|
| 720 | + $class_name = str_replace('\\', '_', $class_name); |
|
| 721 | + // check if class has already been loaded, and return it if it has been |
|
| 722 | + if (isset($this->{$class_abbreviation}) && ! is_null($this->{$class_abbreviation})) { |
|
| 723 | + return $this->{$class_abbreviation}; |
|
| 724 | + } |
|
| 725 | + if (isset ($this->{$class_name})) { |
|
| 726 | + return $this->{$class_name}; |
|
| 727 | + } |
|
| 728 | + if (isset ($this->LIB->{$class_name})) { |
|
| 729 | + return $this->LIB->{$class_name}; |
|
| 730 | + } |
|
| 731 | + if ($class_prefix === 'addon' && isset ($this->addons->{$class_name})) { |
|
| 732 | + return $this->addons->{$class_name}; |
|
| 733 | + } |
|
| 734 | + return null; |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + |
|
| 738 | + |
|
| 739 | + /** |
|
| 740 | + * removes a cached version of the requested class |
|
| 741 | + * |
|
| 742 | + * @param string $class_name |
|
| 743 | + * @param boolean $addon |
|
| 744 | + * @return boolean |
|
| 745 | + */ |
|
| 746 | + public function clear_cached_class($class_name, $addon = false) |
|
| 747 | + { |
|
| 748 | + // have to specify something, but not anything that will conflict |
|
| 749 | + $class_abbreviation = isset($this->_class_abbreviations[ $class_name ]) |
|
| 750 | + ? $this->_class_abbreviations[ $class_name ] |
|
| 751 | + : 'FANCY_BATMAN_PANTS'; |
|
| 752 | + $class_name = str_replace('\\', '_', $class_name); |
|
| 753 | + // check if class has already been loaded, and return it if it has been |
|
| 754 | + if (isset($this->{$class_abbreviation}) && ! is_null($this->{$class_abbreviation})) { |
|
| 755 | + $this->{$class_abbreviation} = null; |
|
| 756 | + return true; |
|
| 757 | + } |
|
| 758 | + if (isset($this->{$class_name})) { |
|
| 759 | + $this->{$class_name} = null; |
|
| 760 | + return true; |
|
| 761 | + } |
|
| 762 | + if (isset($this->LIB->{$class_name})) { |
|
| 763 | + unset($this->LIB->{$class_name}); |
|
| 764 | + return true; |
|
| 765 | + } |
|
| 766 | + if ($addon && isset($this->addons->{$class_name})) { |
|
| 767 | + unset($this->addons->{$class_name}); |
|
| 768 | + return true; |
|
| 769 | + } |
|
| 770 | + return false; |
|
| 771 | + } |
|
| 772 | + |
|
| 773 | + |
|
| 774 | + /** |
|
| 775 | + * _resolve_path |
|
| 776 | + * attempts to find a full valid filepath for the requested class. |
|
| 777 | + * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
| 778 | + * then returns that path if the target file has been found and is readable |
|
| 779 | + * |
|
| 780 | + * @access protected |
|
| 781 | + * @param string $class_name |
|
| 782 | + * @param string $type |
|
| 783 | + * @param array $file_paths |
|
| 784 | + * @return string | bool |
|
| 785 | + */ |
|
| 786 | + protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
| 787 | + { |
|
| 788 | + // make sure $file_paths is an array |
|
| 789 | + $file_paths = is_array($file_paths) ? $file_paths : array($file_paths); |
|
| 790 | + // cycle thru paths |
|
| 791 | + foreach ($file_paths as $key => $file_path) { |
|
| 792 | + // convert all separators to proper DS, if no filepath, then use EE_CLASSES |
|
| 793 | + $file_path = $file_path ? str_replace(array('/', '\\'), DS, $file_path) : EE_CLASSES; |
|
| 794 | + // prep file type |
|
| 795 | + $type = ! empty($type) ? trim($type, '.') . '.' : ''; |
|
| 796 | + // build full file path |
|
| 797 | + $file_paths[$key] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
| 798 | + //does the file exist and can be read ? |
|
| 799 | + if (is_readable($file_paths[$key])) { |
|
| 800 | + return $file_paths[$key]; |
|
| 801 | + } |
|
| 802 | + } |
|
| 803 | + return false; |
|
| 804 | + } |
|
| 805 | + |
|
| 806 | + |
|
| 807 | + |
|
| 808 | + /** |
|
| 809 | + * _require_file |
|
| 810 | + * basically just performs a require_once() |
|
| 811 | + * but with some error handling |
|
| 812 | + * |
|
| 813 | + * @access protected |
|
| 814 | + * @param string $path |
|
| 815 | + * @param string $class_name |
|
| 816 | + * @param string $type |
|
| 817 | + * @param array $file_paths |
|
| 818 | + * @return boolean |
|
| 819 | + * @throws \EE_Error |
|
| 820 | + */ |
|
| 821 | + protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
| 822 | + { |
|
| 823 | + // don't give up! you gotta... |
|
| 824 | + try { |
|
| 825 | + //does the file exist and can it be read ? |
|
| 826 | + if ( ! $path) { |
|
| 827 | + // so sorry, can't find the file |
|
| 828 | + throw new EE_Error ( |
|
| 829 | + sprintf( |
|
| 830 | + __('The %1$s file %2$s could not be located or is not readable due to file permissions. Please ensure that the following filepath(s) are correct: %3$s', 'event_espresso'), |
|
| 831 | + trim($type, '.'), |
|
| 832 | + $class_name, |
|
| 833 | + '<br />' . implode(',<br />', $file_paths) |
|
| 834 | + ) |
|
| 835 | + ); |
|
| 836 | + } |
|
| 837 | + // get the file |
|
| 838 | + require_once($path); |
|
| 839 | + // if the class isn't already declared somewhere |
|
| 840 | + if (class_exists($class_name, false) === false) { |
|
| 841 | + // so sorry, not a class |
|
| 842 | + throw new EE_Error( |
|
| 843 | + sprintf( |
|
| 844 | + __('The %s file %s does not appear to contain the %s Class.', 'event_espresso'), |
|
| 845 | + $type, |
|
| 846 | + $path, |
|
| 847 | + $class_name |
|
| 848 | + ) |
|
| 849 | + ); |
|
| 850 | + } |
|
| 851 | + } catch (EE_Error $e) { |
|
| 852 | + $e->get_error(); |
|
| 853 | + return false; |
|
| 854 | + } |
|
| 855 | + return true; |
|
| 856 | + } |
|
| 857 | + |
|
| 858 | + |
|
| 859 | + |
|
| 860 | + /** |
|
| 861 | + * _create_object |
|
| 862 | + * Attempts to instantiate the requested class via any of the |
|
| 863 | + * commonly used instantiation methods employed throughout EE. |
|
| 864 | + * The priority for instantiation is as follows: |
|
| 865 | + * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
| 866 | + * - model objects via their 'new_instance_from_db' method |
|
| 867 | + * - model objects via their 'new_instance' method |
|
| 868 | + * - "singleton" classes" via their 'instance' method |
|
| 869 | + * - standard instantiable classes via their __constructor |
|
| 870 | + * Prior to instantiation, if the classname exists in the dependency_map, |
|
| 871 | + * then the constructor for the requested class will be examined to determine |
|
| 872 | + * if any dependencies exist, and if they can be injected. |
|
| 873 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 874 | + * |
|
| 875 | + * @access protected |
|
| 876 | + * @param string $class_name |
|
| 877 | + * @param array $arguments |
|
| 878 | + * @param string $type |
|
| 879 | + * @return null | object |
|
| 880 | + * @throws \EE_Error |
|
| 881 | + */ |
|
| 882 | + protected function _create_object($class_name, $arguments = array(), $type = '') |
|
| 883 | + { |
|
| 884 | + $class_obj = null; |
|
| 885 | + $instantiation_mode = '0) none'; |
|
| 886 | + // don't give up! you gotta... |
|
| 887 | + try { |
|
| 888 | + // create reflection |
|
| 889 | + $reflector = $this->get_ReflectionClass($class_name); |
|
| 890 | + // make sure arguments are an array |
|
| 891 | + $arguments = is_array($arguments) ? $arguments : array($arguments); |
|
| 892 | + // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
| 893 | + // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
| 894 | + $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
| 895 | + ? $arguments |
|
| 896 | + : array($arguments); |
|
| 897 | + // attempt to inject dependencies ? |
|
| 898 | + if ($this->_dependency_map->has($class_name)) { |
|
| 899 | + $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
| 900 | + } |
|
| 901 | + // instantiate the class if possible |
|
| 902 | + if ($reflector->isAbstract()) { |
|
| 903 | + // nothing to instantiate, loading file was enough |
|
| 904 | + // does not throw an exception so $instantiation_mode is unused |
|
| 905 | + // $instantiation_mode = "1) no constructor abstract class"; |
|
| 906 | + $class_obj = true; |
|
| 907 | + } else if ($reflector->getConstructor() === null && $reflector->isInstantiable() && empty($arguments)) { |
|
| 908 | + // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
| 909 | + $instantiation_mode = "2) no constructor but instantiable"; |
|
| 910 | + $class_obj = $reflector->newInstance(); |
|
| 911 | + } else if (method_exists($class_name, 'new_instance')) { |
|
| 912 | + $instantiation_mode = "4) new_instance()"; |
|
| 913 | + $class_obj = call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
| 914 | + } else if (method_exists($class_name, 'instance')) { |
|
| 915 | + $instantiation_mode = "5) instance()"; |
|
| 916 | + $class_obj = call_user_func_array(array($class_name, 'instance'), $arguments); |
|
| 917 | + } else if ($reflector->isInstantiable()) { |
|
| 918 | + $instantiation_mode = "6) constructor"; |
|
| 919 | + $class_obj = $reflector->newInstanceArgs($arguments); |
|
| 920 | + } else { |
|
| 921 | + // heh ? something's not right ! |
|
| 922 | + throw new EE_Error( |
|
| 923 | + sprintf( |
|
| 924 | + __('The %s file %s could not be instantiated.', 'event_espresso'), |
|
| 925 | + $type, |
|
| 926 | + $class_name |
|
| 927 | + ) |
|
| 928 | + ); |
|
| 929 | + } |
|
| 930 | + } catch (Exception $e) { |
|
| 931 | + if ( ! $e instanceof EE_Error) { |
|
| 932 | + $e = new EE_Error( |
|
| 933 | + sprintf( |
|
| 934 | + __('The following error occurred while attempting to instantiate "%1$s": %2$s %3$s %2$s instantiation mode : %4$s', 'event_espresso'), |
|
| 935 | + $class_name, |
|
| 936 | + '<br />', |
|
| 937 | + $e->getMessage(), |
|
| 938 | + $instantiation_mode |
|
| 939 | + ) |
|
| 940 | + ); |
|
| 941 | + } |
|
| 942 | + $e->get_error(); |
|
| 943 | + } |
|
| 944 | + return $class_obj; |
|
| 945 | + } |
|
| 946 | + |
|
| 947 | + |
|
| 948 | + |
|
| 949 | + /** |
|
| 950 | + * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
| 951 | + * @param array $array |
|
| 952 | + * @return bool |
|
| 953 | + */ |
|
| 954 | + protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
| 955 | + { |
|
| 956 | + return ! empty($array) ? array_keys($array) === range(0, count($array) - 1) : true; |
|
| 957 | + } |
|
| 958 | + |
|
| 959 | + |
|
| 960 | + |
|
| 961 | + /** |
|
| 962 | + * getReflectionClass |
|
| 963 | + * checks if a ReflectionClass object has already been generated for a class |
|
| 964 | + * and returns that instead of creating a new one |
|
| 965 | + * |
|
| 966 | + * @access public |
|
| 967 | + * @param string $class_name |
|
| 968 | + * @return ReflectionClass |
|
| 969 | + */ |
|
| 970 | + public function get_ReflectionClass($class_name) |
|
| 971 | + { |
|
| 972 | + if ( |
|
| 973 | + ! isset($this->_reflectors[$class_name]) |
|
| 974 | + || ! $this->_reflectors[$class_name] instanceof ReflectionClass |
|
| 975 | + ) { |
|
| 976 | + $this->_reflectors[$class_name] = new ReflectionClass($class_name); |
|
| 977 | + } |
|
| 978 | + return $this->_reflectors[$class_name]; |
|
| 979 | + } |
|
| 980 | + |
|
| 981 | + |
|
| 982 | + |
|
| 983 | + /** |
|
| 984 | + * _resolve_dependencies |
|
| 985 | + * examines the constructor for the requested class to determine |
|
| 986 | + * if any dependencies exist, and if they can be injected. |
|
| 987 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 988 | + * PLZ NOTE: this is achieved by type hinting the constructor params |
|
| 989 | + * For example: |
|
| 990 | + * if attempting to load a class "Foo" with the following constructor: |
|
| 991 | + * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
| 992 | + * then $bar_class and $grohl_class will be added to the $arguments array, |
|
| 993 | + * but only IF they are NOT already present in the incoming arguments array, |
|
| 994 | + * and the correct classes can be loaded |
|
| 995 | + * |
|
| 996 | + * @access protected |
|
| 997 | + * @param ReflectionClass $reflector |
|
| 998 | + * @param string $class_name |
|
| 999 | + * @param array $arguments |
|
| 1000 | + * @return array |
|
| 1001 | + * @throws \ReflectionException |
|
| 1002 | + */ |
|
| 1003 | + protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, $arguments = array()) |
|
| 1004 | + { |
|
| 1005 | + // let's examine the constructor |
|
| 1006 | + $constructor = $reflector->getConstructor(); |
|
| 1007 | + // whu? huh? nothing? |
|
| 1008 | + if ( ! $constructor) { |
|
| 1009 | + return $arguments; |
|
| 1010 | + } |
|
| 1011 | + // get constructor parameters |
|
| 1012 | + $params = $constructor->getParameters(); |
|
| 1013 | + // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
| 1014 | + $argument_keys = array_keys($arguments); |
|
| 1015 | + // now loop thru all of the constructors expected parameters |
|
| 1016 | + foreach ($params as $index => $param) { |
|
| 1017 | + // is this a dependency for a specific class ? |
|
| 1018 | + $param_class = $param->getClass() ? $param->getClass()->name : null; |
|
| 1019 | + // BUT WAIT !!! This class may be an alias for something else (or getting replaced at runtime) |
|
| 1020 | + $param_class = $this->_dependency_map->has_alias($param_class, $class_name) |
|
| 1021 | + ? $this->_dependency_map->get_alias($param_class, $class_name) |
|
| 1022 | + : $param_class; |
|
| 1023 | + if ( |
|
| 1024 | + // param is not even a class |
|
| 1025 | + empty($param_class) |
|
| 1026 | + // and something already exists in the incoming arguments for this param |
|
| 1027 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
| 1028 | + ) { |
|
| 1029 | + // so let's skip this argument and move on to the next |
|
| 1030 | + continue; |
|
| 1031 | + } |
|
| 1032 | + if ( |
|
| 1033 | + // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
| 1034 | + ! empty($param_class) |
|
| 1035 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
| 1036 | + && $arguments[$argument_keys[$index]] instanceof $param_class |
|
| 1037 | + ) { |
|
| 1038 | + // skip this argument and move on to the next |
|
| 1039 | + continue; |
|
| 1040 | + } |
|
| 1041 | + if ( |
|
| 1042 | + // parameter is type hinted as a class, and should be injected |
|
| 1043 | + ! empty($param_class) |
|
| 1044 | + && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
| 1045 | + ) { |
|
| 1046 | + $arguments = $this->_resolve_dependency($class_name, $param_class, $arguments, $index); |
|
| 1047 | + } else { |
|
| 1048 | + try { |
|
| 1049 | + $arguments[$index] = $param->getDefaultValue(); |
|
| 1050 | + } catch (ReflectionException $e) { |
|
| 1051 | + throw new ReflectionException( |
|
| 1052 | + sprintf( |
|
| 1053 | + __('%1$s for parameter "$%2$s"', 'event_espresso'), |
|
| 1054 | + $e->getMessage(), |
|
| 1055 | + $param->getName() |
|
| 1056 | + ) |
|
| 1057 | + ); |
|
| 1058 | + } |
|
| 1059 | + } |
|
| 1060 | + } |
|
| 1061 | + return $arguments; |
|
| 1062 | + } |
|
| 1063 | + |
|
| 1064 | + |
|
| 1065 | + |
|
| 1066 | + /** |
|
| 1067 | + * @access protected |
|
| 1068 | + * @param string $class_name |
|
| 1069 | + * @param string $param_class |
|
| 1070 | + * @param array $arguments |
|
| 1071 | + * @param mixed $index |
|
| 1072 | + * @return array |
|
| 1073 | + */ |
|
| 1074 | + protected function _resolve_dependency($class_name, $param_class, $arguments, $index) |
|
| 1075 | + { |
|
| 1076 | + $dependency = null; |
|
| 1077 | + // should dependency be loaded from cache ? |
|
| 1078 | + $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency($class_name, $param_class) |
|
| 1079 | + !== EE_Dependency_Map::load_new_object |
|
| 1080 | + ? true |
|
| 1081 | + : false; |
|
| 1082 | + // we might have a dependency... |
|
| 1083 | + // let's MAYBE try and find it in our cache if that's what's been requested |
|
| 1084 | + $cached_class = $cache_on ? $this->_get_cached_class($param_class) : null; |
|
| 1085 | + // and grab it if it exists |
|
| 1086 | + if ($cached_class instanceof $param_class) { |
|
| 1087 | + $dependency = $cached_class; |
|
| 1088 | + } else if ($param_class !== $class_name) { |
|
| 1089 | + // obtain the loader method from the dependency map |
|
| 1090 | + $loader = $this->_dependency_map->class_loader($param_class); |
|
| 1091 | + // is loader a custom closure ? |
|
| 1092 | + if ($loader instanceof Closure) { |
|
| 1093 | + $dependency = $loader(); |
|
| 1094 | + } else { |
|
| 1095 | + // set the cache on property for the recursive loading call |
|
| 1096 | + $this->_cache_on = $cache_on; |
|
| 1097 | + // if not, then let's try and load it via the registry |
|
| 1098 | + if ($loader && method_exists($this, $loader)) { |
|
| 1099 | + $dependency = $this->{$loader}($param_class); |
|
| 1100 | + } else { |
|
| 1101 | + $dependency = $this->create($param_class, array(), $cache_on); |
|
| 1102 | + } |
|
| 1103 | + } |
|
| 1104 | + } |
|
| 1105 | + // did we successfully find the correct dependency ? |
|
| 1106 | + if ($dependency instanceof $param_class) { |
|
| 1107 | + // then let's inject it into the incoming array of arguments at the correct location |
|
| 1108 | + if (isset($argument_keys[$index])) { |
|
| 1109 | + $arguments[$argument_keys[$index]] = $dependency; |
|
| 1110 | + } else { |
|
| 1111 | + $arguments[$index] = $dependency; |
|
| 1112 | + } |
|
| 1113 | + } |
|
| 1114 | + return $arguments; |
|
| 1115 | + } |
|
| 1116 | + |
|
| 1117 | + |
|
| 1118 | + |
|
| 1119 | + /** |
|
| 1120 | + * _set_cached_class |
|
| 1121 | + * attempts to cache the instantiated class locally |
|
| 1122 | + * in one of the following places, in the following order: |
|
| 1123 | + * $this->{class_abbreviation} ie: $this->CART |
|
| 1124 | + * $this->{$class_name} ie: $this->Some_Class |
|
| 1125 | + * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
| 1126 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 1127 | + * |
|
| 1128 | + * @access protected |
|
| 1129 | + * @param object $class_obj |
|
| 1130 | + * @param string $class_name |
|
| 1131 | + * @param string $class_prefix |
|
| 1132 | + * @return void |
|
| 1133 | + */ |
|
| 1134 | + protected function _set_cached_class($class_obj, $class_name, $class_prefix = '') |
|
| 1135 | + { |
|
| 1136 | + if (empty($class_obj)) { |
|
| 1137 | + return; |
|
| 1138 | + } |
|
| 1139 | + // return newly instantiated class |
|
| 1140 | + if (isset($this->_class_abbreviations[$class_name])) { |
|
| 1141 | + $class_abbreviation = $this->_class_abbreviations[$class_name]; |
|
| 1142 | + $this->{$class_abbreviation} = $class_obj; |
|
| 1143 | + return; |
|
| 1144 | + } |
|
| 1145 | + $class_name = str_replace('\\', '_', $class_name); |
|
| 1146 | + if (property_exists($this, $class_name)) { |
|
| 1147 | + $this->{$class_name} = $class_obj; |
|
| 1148 | + return; |
|
| 1149 | + } |
|
| 1150 | + if ($class_prefix === 'addon') { |
|
| 1151 | + $this->addons->{$class_name} = $class_obj; |
|
| 1152 | + return; |
|
| 1153 | + } |
|
| 1154 | + $this->LIB->{$class_name} = $class_obj; |
|
| 1155 | + } |
|
| 1156 | + |
|
| 1157 | + |
|
| 1158 | + |
|
| 1159 | + /** |
|
| 1160 | + * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
| 1161 | + * |
|
| 1162 | + * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
| 1163 | + * in the EE_Dependency_Map::$_class_loaders array, |
|
| 1164 | + * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
| 1165 | + * @param array $arguments |
|
| 1166 | + * @return object |
|
| 1167 | + */ |
|
| 1168 | + public static function factory($classname, $arguments = array()) |
|
| 1169 | + { |
|
| 1170 | + $loader = self::instance()->_dependency_map->class_loader($classname); |
|
| 1171 | + if ($loader instanceof Closure) { |
|
| 1172 | + return $loader($arguments); |
|
| 1173 | + } |
|
| 1174 | + if (method_exists(EE_Registry::instance(), $loader)) { |
|
| 1175 | + return EE_Registry::instance()->{$loader}($classname, $arguments); |
|
| 1176 | + } |
|
| 1177 | + return null; |
|
| 1178 | + } |
|
| 1179 | + |
|
| 1180 | + |
|
| 1181 | + |
|
| 1182 | + /** |
|
| 1183 | + * Gets the addon by its name/slug (not classname. For that, just |
|
| 1184 | + * use the classname as the property name on EE_Config::instance()->addons) |
|
| 1185 | + * |
|
| 1186 | + * @param string $name |
|
| 1187 | + * @return EE_Addon |
|
| 1188 | + */ |
|
| 1189 | + public function get_addon_by_name($name) |
|
| 1190 | + { |
|
| 1191 | + foreach ($this->addons as $addon) { |
|
| 1192 | + if ($addon->name() == $name) { |
|
| 1193 | + return $addon; |
|
| 1194 | + } |
|
| 1195 | + } |
|
| 1196 | + return null; |
|
| 1197 | + } |
|
| 1198 | + |
|
| 1199 | + |
|
| 1200 | + |
|
| 1201 | + /** |
|
| 1202 | + * 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 |
|
| 1203 | + * the addon's classname. So if you just want to get the addon by classname, use EE_Config::instance()->addons->{classname} |
|
| 1204 | + * |
|
| 1205 | + * @return EE_Addon[] where the KEYS are the addon's name() |
|
| 1206 | + */ |
|
| 1207 | + public function get_addons_by_name() |
|
| 1208 | + { |
|
| 1209 | + $addons = array(); |
|
| 1210 | + foreach ($this->addons as $addon) { |
|
| 1211 | + $addons[$addon->name()] = $addon; |
|
| 1212 | + } |
|
| 1213 | + return $addons; |
|
| 1214 | + } |
|
| 1215 | + |
|
| 1216 | + |
|
| 1217 | + |
|
| 1218 | + /** |
|
| 1219 | + * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
| 1220 | + * a stale copy of it around |
|
| 1221 | + * |
|
| 1222 | + * @param string $model_name |
|
| 1223 | + * @return \EEM_Base |
|
| 1224 | + * @throws \EE_Error |
|
| 1225 | + */ |
|
| 1226 | + public function reset_model($model_name) |
|
| 1227 | + { |
|
| 1228 | + $model_class_name = strpos($model_name, 'EEM_') !== 0 ? "EEM_{$model_name}" : $model_name; |
|
| 1229 | + if ( ! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
| 1230 | + return null; |
|
| 1231 | + } |
|
| 1232 | + //get that model reset it and make sure we nuke the old reference to it |
|
| 1233 | + if ($this->LIB->{$model_class_name} instanceof $model_class_name && is_callable(array($model_class_name, 'reset'))) { |
|
| 1234 | + $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
| 1235 | + } else { |
|
| 1236 | + throw new EE_Error(sprintf(__('Model %s does not have a method "reset"', 'event_espresso'), $model_name)); |
|
| 1237 | + } |
|
| 1238 | + return $this->LIB->{$model_class_name}; |
|
| 1239 | + } |
|
| 1240 | + |
|
| 1241 | + |
|
| 1242 | + |
|
| 1243 | + /** |
|
| 1244 | + * Resets the registry. |
|
| 1245 | + * The criteria for what gets reset is based on what can be shared between sites on the same request when switch_to_blog |
|
| 1246 | + * is used in a multisite install. Here is a list of things that are NOT reset. |
|
| 1247 | + * - $_dependency_map |
|
| 1248 | + * - $_class_abbreviations |
|
| 1249 | + * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
| 1250 | + * - $REQ: Still on the same request so no need to change. |
|
| 1251 | + * - $CAP: There is no site specific state in the EE_Capability class. |
|
| 1252 | + * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only one Session |
|
| 1253 | + * can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
| 1254 | + * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
| 1255 | + * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
| 1256 | + * switch or on the restore. |
|
| 1257 | + * - $modules |
|
| 1258 | + * - $shortcodes |
|
| 1259 | + * - $widgets |
|
| 1260 | + * |
|
| 1261 | + * @param boolean $hard whether to reset data in the database too, or just refresh |
|
| 1262 | + * the Registry to its state at the beginning of the request |
|
| 1263 | + * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
| 1264 | + * or just reset without re-instantiating (handy to set to FALSE if you're not sure if you CAN |
|
| 1265 | + * currently reinstantiate the singletons at the moment) |
|
| 1266 | + * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so client |
|
| 1267 | + * code instead can just change the model context to a different blog id if necessary |
|
| 1268 | + * @return EE_Registry |
|
| 1269 | + */ |
|
| 1270 | + public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
| 1271 | + { |
|
| 1272 | + $instance = self::instance(); |
|
| 1273 | + EEH_Activation::reset(); |
|
| 1274 | + //properties that get reset |
|
| 1275 | + $instance->_cache_on = true; |
|
| 1276 | + $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
| 1277 | + $instance->CART = null; |
|
| 1278 | + $instance->MRM = null; |
|
| 1279 | + $instance->AssetsRegistry = null; |
|
| 1280 | + $instance->AssetsRegistry = $instance->create('EventEspresso\core\services\assets\Registry'); |
|
| 1281 | + //messages reset |
|
| 1282 | + EED_Messages::reset(); |
|
| 1283 | + if ($reset_models) { |
|
| 1284 | + foreach (array_keys($instance->non_abstract_db_models) as $model_name) { |
|
| 1285 | + $instance->reset_model($model_name); |
|
| 1286 | + } |
|
| 1287 | + } |
|
| 1288 | + $instance->LIB = new stdClass(); |
|
| 1289 | + return $instance; |
|
| 1290 | + } |
|
| 1291 | + |
|
| 1292 | + |
|
| 1293 | + |
|
| 1294 | + /** |
|
| 1295 | + * @override magic methods |
|
| 1296 | + * @return void |
|
| 1297 | + */ |
|
| 1298 | + public final function __destruct() |
|
| 1299 | + { |
|
| 1300 | + } |
|
| 1301 | + |
|
| 1302 | + |
|
| 1303 | + |
|
| 1304 | + /** |
|
| 1305 | + * @param $a |
|
| 1306 | + * @param $b |
|
| 1307 | + */ |
|
| 1308 | + public final function __call($a, $b) |
|
| 1309 | + { |
|
| 1310 | + } |
|
| 1311 | + |
|
| 1312 | + |
|
| 1313 | + |
|
| 1314 | + /** |
|
| 1315 | + * @param $a |
|
| 1316 | + */ |
|
| 1317 | + public final function __get($a) |
|
| 1318 | + { |
|
| 1319 | + } |
|
| 1320 | + |
|
| 1321 | + |
|
| 1322 | + |
|
| 1323 | + /** |
|
| 1324 | + * @param $a |
|
| 1325 | + * @param $b |
|
| 1326 | + */ |
|
| 1327 | + public final function __set($a, $b) |
|
| 1328 | + { |
|
| 1329 | + } |
|
| 1330 | + |
|
| 1331 | + |
|
| 1332 | + |
|
| 1333 | + /** |
|
| 1334 | + * @param $a |
|
| 1335 | + */ |
|
| 1336 | + public final function __isset($a) |
|
| 1337 | + { |
|
| 1338 | + } |
|
| 1339 | 1339 | |
| 1340 | 1340 | |
| 1341 | 1341 | |
| 1342 | - /** |
|
| 1343 | - * @param $a |
|
| 1344 | - */ |
|
| 1345 | - public final function __unset($a) |
|
| 1346 | - { |
|
| 1347 | - } |
|
| 1342 | + /** |
|
| 1343 | + * @param $a |
|
| 1344 | + */ |
|
| 1345 | + public final function __unset($a) |
|
| 1346 | + { |
|
| 1347 | + } |
|
| 1348 | 1348 | |
| 1349 | 1349 | |
| 1350 | 1350 | |
| 1351 | - /** |
|
| 1352 | - * @return array |
|
| 1353 | - */ |
|
| 1354 | - public final function __sleep() |
|
| 1355 | - { |
|
| 1356 | - return array(); |
|
| 1357 | - } |
|
| 1351 | + /** |
|
| 1352 | + * @return array |
|
| 1353 | + */ |
|
| 1354 | + public final function __sleep() |
|
| 1355 | + { |
|
| 1356 | + return array(); |
|
| 1357 | + } |
|
| 1358 | 1358 | |
| 1359 | 1359 | |
| 1360 | 1360 | |
| 1361 | - public final function __wakeup() |
|
| 1362 | - { |
|
| 1363 | - } |
|
| 1361 | + public final function __wakeup() |
|
| 1362 | + { |
|
| 1363 | + } |
|
| 1364 | 1364 | |
| 1365 | 1365 | |
| 1366 | 1366 | |
| 1367 | - /** |
|
| 1368 | - * @return string |
|
| 1369 | - */ |
|
| 1370 | - public final function __toString() |
|
| 1371 | - { |
|
| 1372 | - return ''; |
|
| 1373 | - } |
|
| 1367 | + /** |
|
| 1368 | + * @return string |
|
| 1369 | + */ |
|
| 1370 | + public final function __toString() |
|
| 1371 | + { |
|
| 1372 | + return ''; |
|
| 1373 | + } |
|
| 1374 | 1374 | |
| 1375 | 1375 | |
| 1376 | 1376 | |
| 1377 | - public final function __invoke() |
|
| 1378 | - { |
|
| 1379 | - } |
|
| 1377 | + public final function __invoke() |
|
| 1378 | + { |
|
| 1379 | + } |
|
| 1380 | 1380 | |
| 1381 | 1381 | |
| 1382 | 1382 | |
| 1383 | - public final static function __set_state($array = array()) |
|
| 1384 | - { |
|
| 1385 | - return EE_Registry::instance(); |
|
| 1386 | - } |
|
| 1383 | + public final static function __set_state($array = array()) |
|
| 1384 | + { |
|
| 1385 | + return EE_Registry::instance(); |
|
| 1386 | + } |
|
| 1387 | 1387 | |
| 1388 | 1388 | |
| 1389 | 1389 | |
| 1390 | - public final function __clone() |
|
| 1391 | - { |
|
| 1392 | - } |
|
| 1390 | + public final function __clone() |
|
| 1391 | + { |
|
| 1392 | + } |
|
| 1393 | 1393 | |
| 1394 | 1394 | |
| 1395 | 1395 | |
| 1396 | - /** |
|
| 1397 | - * @param $a |
|
| 1398 | - * @param $b |
|
| 1399 | - */ |
|
| 1400 | - public final static function __callStatic($a, $b) |
|
| 1401 | - { |
|
| 1402 | - } |
|
| 1396 | + /** |
|
| 1397 | + * @param $a |
|
| 1398 | + * @param $b |
|
| 1399 | + */ |
|
| 1400 | + public final static function __callStatic($a, $b) |
|
| 1401 | + { |
|
| 1402 | + } |
|
| 1403 | 1403 | |
| 1404 | 1404 | |
| 1405 | 1405 | |
| 1406 | - /** |
|
| 1407 | - * Gets all the custom post type models defined |
|
| 1408 | - * |
|
| 1409 | - * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
| 1410 | - */ |
|
| 1411 | - public function cpt_models() |
|
| 1412 | - { |
|
| 1413 | - $cpt_models = array(); |
|
| 1414 | - foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
| 1415 | - if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
| 1416 | - $cpt_models[$short_name] = $classname; |
|
| 1417 | - } |
|
| 1418 | - } |
|
| 1419 | - return $cpt_models; |
|
| 1420 | - } |
|
| 1406 | + /** |
|
| 1407 | + * Gets all the custom post type models defined |
|
| 1408 | + * |
|
| 1409 | + * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
| 1410 | + */ |
|
| 1411 | + public function cpt_models() |
|
| 1412 | + { |
|
| 1413 | + $cpt_models = array(); |
|
| 1414 | + foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
| 1415 | + if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
| 1416 | + $cpt_models[$short_name] = $classname; |
|
| 1417 | + } |
|
| 1418 | + } |
|
| 1419 | + return $cpt_models; |
|
| 1420 | + } |
|
| 1421 | 1421 | |
| 1422 | 1422 | |
| 1423 | 1423 | |
| 1424 | - /** |
|
| 1425 | - * @return \EE_Config |
|
| 1426 | - */ |
|
| 1427 | - public static function CFG() |
|
| 1428 | - { |
|
| 1429 | - return self::instance()->CFG; |
|
| 1430 | - } |
|
| 1424 | + /** |
|
| 1425 | + * @return \EE_Config |
|
| 1426 | + */ |
|
| 1427 | + public static function CFG() |
|
| 1428 | + { |
|
| 1429 | + return self::instance()->CFG; |
|
| 1430 | + } |
|
| 1431 | 1431 | |
| 1432 | 1432 | |
| 1433 | 1433 | } |