Completed
Branch EDTR/master (6bd139)
by
unknown
35:03 queued 26:41
created

EE_Dependency_Map::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\domain\DomainFactory;
4
use EventEspresso\core\exceptions\InvalidAliasException;
5
use EventEspresso\core\services\loaders\ClassInterfaceCache;
6
use EventEspresso\core\services\loaders\LoaderFactory;
7
use EventEspresso\core\services\loaders\LoaderInterface;
8
use EventEspresso\core\services\request\LegacyRequestInterface;
9
use EventEspresso\core\services\request\RequestInterface;
10
use EventEspresso\core\services\request\ResponseInterface;
11
12
/**
13
 * Class EE_Dependency_Map
14
 * info about how to load classes required by other classes
15
 *
16
 * @package       Event Espresso
17
 * @subpackage    core
18
 * @author        Brent Christensen
19
 * @since         4.9.0
20
 */
21
class EE_Dependency_Map
22
{
23
24
    /**
25
     * This means that the requested class dependency is not present in the dependency map
26
     */
27
    const not_registered = 0;
28
29
    /**
30
     * This instructs class loaders to ALWAYS return a newly instantiated object for the requested class.
31
     */
32
    const load_new_object = 1;
33
34
    /**
35
     * This instructs class loaders to return a previously instantiated and cached object for the requested class.
36
     * IF a previously instantiated object does not exist, a new one will be created and added to the cache.
37
     */
38
    const load_from_cache = 2;
39
40
    /**
41
     * When registering a dependency,
42
     * this indicates to keep any existing dependencies that already exist,
43
     * and simply discard any new dependencies declared in the incoming data
44
     */
45
    const KEEP_EXISTING_DEPENDENCIES = 0;
46
47
    /**
48
     * When registering a dependency,
49
     * this indicates to overwrite any existing dependencies that already exist using the incoming data
50
     */
51
    const OVERWRITE_DEPENDENCIES = 1;
52
53
    /**
54
     * @type EE_Dependency_Map $_instance
55
     */
56
    protected static $_instance;
57
58
    /**
59
     * @var ClassInterfaceCache $class_cache
60
     */
61
    private $class_cache;
62
63
    /**
64
     * @type RequestInterface $request
65
     */
66
    protected $request;
67
68
    /**
69
     * @type LegacyRequestInterface $legacy_request
70
     */
71
    protected $legacy_request;
72
73
    /**
74
     * @type ResponseInterface $response
75
     */
76
    protected $response;
77
78
    /**
79
     * @type LoaderInterface $loader
80
     */
81
    protected $loader;
82
83
    /**
84
     * @type array $_dependency_map
85
     */
86
    protected $_dependency_map = [];
87
88
    /**
89
     * @type array $_class_loaders
90
     */
91
    protected $_class_loaders = [];
92
93
94
    /**
95
     * EE_Dependency_Map constructor.
96
     *
97
     * @param ClassInterfaceCache $class_cache
98
     */
99
    protected function __construct(ClassInterfaceCache $class_cache)
100
    {
101
        $this->class_cache = $class_cache;
102
        do_action('EE_Dependency_Map____construct', $this);
103
    }
104
105
106
    /**
107
     * @return void
108
     * @throws InvalidAliasException
109
     */
110
    public function initialize()
111
    {
112
        $this->_register_core_dependencies();
113
        $this->_register_core_class_loaders();
114
        $this->_register_core_aliases();
115
    }
116
117
118
    /**
119
     * @singleton method used to instantiate class object
120
     * @param ClassInterfaceCache|null $class_cache
121
     * @return EE_Dependency_Map
122
     */
123
    public static function instance(ClassInterfaceCache $class_cache = null)
124
    {
125
        // check if class object is instantiated, and instantiated properly
126
        if (! EE_Dependency_Map::$_instance instanceof EE_Dependency_Map
127
            && $class_cache instanceof ClassInterfaceCache
128
        ) {
129
            EE_Dependency_Map::$_instance = new EE_Dependency_Map($class_cache);
130
        }
131
        return EE_Dependency_Map::$_instance;
132
    }
133
134
135
    /**
136
     * @param RequestInterface $request
137
     */
138
    public function setRequest(RequestInterface $request)
139
    {
140
        $this->request = $request;
141
    }
142
143
144
    /**
145
     * @param LegacyRequestInterface $legacy_request
146
     */
147
    public function setLegacyRequest(LegacyRequestInterface $legacy_request)
148
    {
149
        $this->legacy_request = $legacy_request;
150
    }
151
152
153
    /**
154
     * @param ResponseInterface $response
155
     */
156
    public function setResponse(ResponseInterface $response)
157
    {
158
        $this->response = $response;
159
    }
160
161
162
    /**
163
     * @param LoaderInterface $loader
164
     */
165
    public function setLoader(LoaderInterface $loader)
166
    {
167
        $this->loader = $loader;
168
    }
169
170
171
    /**
172
     * @param string $class
173
     * @param array  $dependencies
174
     * @param int    $overwrite
175
     * @return bool
176
     */
177
    public static function register_dependencies(
178
        $class,
179
        array $dependencies,
180
        $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES
181
    ) {
182
        return EE_Dependency_Map::$_instance->registerDependencies($class, $dependencies, $overwrite);
183
    }
184
185
186
    /**
187
     * Assigns an array of class names and corresponding load sources (new or cached)
188
     * to the class specified by the first parameter.
189
     * IMPORTANT !!!
190
     * The order of elements in the incoming $dependencies array MUST match
191
     * the order of the constructor parameters for the class in question.
192
     * This is especially important when overriding any existing dependencies that are registered.
193
     * the third parameter controls whether any duplicate dependencies are overwritten or not.
194
     *
195
     * @param string $class
196
     * @param array  $dependencies
197
     * @param int    $overwrite
198
     * @return bool
199
     */
200
    public function registerDependencies(
201
        $class,
202
        array $dependencies,
203
        $overwrite = EE_Dependency_Map::KEEP_EXISTING_DEPENDENCIES
204
    ) {
205
        $class = trim($class, '\\');
206
        $registered = false;
207
        if (empty(EE_Dependency_Map::$_instance->_dependency_map[ $class ])) {
208
            EE_Dependency_Map::$_instance->_dependency_map[ $class ] = [];
209
        }
210
        // we need to make sure that any aliases used when registering a dependency
211
        // get resolved to the correct class name
212
        foreach ($dependencies as $dependency => $load_source) {
213
            $alias = EE_Dependency_Map::$_instance->getFqnForAlias($dependency);
214
            if ($overwrite === EE_Dependency_Map::OVERWRITE_DEPENDENCIES
215
                || ! isset(EE_Dependency_Map::$_instance->_dependency_map[ $class ][ $alias ])
216
            ) {
217
                unset($dependencies[ $dependency ]);
218
                $dependencies[ $alias ] = $load_source;
219
                $registered = true;
220
            }
221
        }
222
        // now add our two lists of dependencies together.
223
        // using Union (+=) favours the arrays in precedence from left to right,
224
        // so $dependencies is NOT overwritten because it is listed first
225
        // ie: with A = B + C, entries in B take precedence over duplicate entries in C
226
        // Union is way faster than array_merge() but should be used with caution...
227
        // especially with numerically indexed arrays
228
        $dependencies += EE_Dependency_Map::$_instance->_dependency_map[ $class ];
229
        // now we need to ensure that the resulting dependencies
230
        // array only has the entries that are required for the class
231
        // so first count how many dependencies were originally registered for the class
232
        $dependency_count = count(EE_Dependency_Map::$_instance->_dependency_map[ $class ]);
233
        // if that count is non-zero (meaning dependencies were already registered)
234
        EE_Dependency_Map::$_instance->_dependency_map[ $class ] = $dependency_count
235
            // then truncate the  final array to match that count
236
            ? array_slice($dependencies, 0, $dependency_count)
237
            // otherwise just take the incoming array because nothing previously existed
238
            : $dependencies;
239
        return $registered;
240
    }
241
242
243
    /**
244
     * @param string $class_name
245
     * @param string $loader
246
     * @return bool
247
     * @throws DomainException
248
     */
249
    public static function register_class_loader($class_name, $loader = 'load_core')
250
    {
251
        return EE_Dependency_Map::$_instance->registerClassLoader($class_name, $loader);
252
    }
253
254
255
    /**
256
     * @param string $class_name
257
     * @param string $loader
258
     * @return bool
259
     * @throws DomainException
260
     */
261
    public function registerClassLoader($class_name, $loader = 'load_core')
262
    {
263
        if (! $loader instanceof Closure && strpos($class_name, '\\') !== false) {
264
            throw new DomainException(
265
                esc_html__('Don\'t use class loaders for FQCNs.', 'event_espresso')
266
            );
267
        }
268
        // check that loader is callable or method starts with "load_" and exists in EE_Registry
269
        if (! is_callable($loader)
270
            && (
271
                strpos($loader, 'load_') !== 0
272
                || ! method_exists('EE_Registry', $loader)
273
            )
274
        ) {
275
            throw new DomainException(
276
                sprintf(
277
                    esc_html__(
278
                        '"%1$s" is not a valid loader method on EE_Registry.',
279
                        'event_espresso'
280
                    ),
281
                    $loader
282
                )
283
            );
284
        }
285
        $class_name = EE_Dependency_Map::$_instance->getFqnForAlias($class_name);
286
        if (! isset(EE_Dependency_Map::$_instance->_class_loaders[ $class_name ])) {
287
            EE_Dependency_Map::$_instance->_class_loaders[ $class_name ] = $loader;
288
            return true;
289
        }
290
        return false;
291
    }
292
293
294
    /**
295
     * @return array
296
     */
297
    public function dependency_map()
298
    {
299
        return $this->_dependency_map;
300
    }
301
302
303
    /**
304
     * returns TRUE if dependency map contains a listing for the provided class name
305
     *
306
     * @param string $class_name
307
     * @return boolean
308
     */
309
    public function has($class_name = '')
310
    {
311
        // all legacy models have the same dependencies
312
        if (strpos($class_name, 'EEM_') === 0) {
313
            $class_name = 'LEGACY_MODELS';
314
        }
315
        return isset($this->_dependency_map[ $class_name ]);
316
    }
317
318
319
    /**
320
     * returns TRUE if dependency map contains a listing for the provided class name AND dependency
321
     *
322
     * @param string $class_name
323
     * @param string $dependency
324
     * @return bool
325
     */
326 View Code Duplication
    public function has_dependency_for_class($class_name = '', $dependency = '')
327
    {
328
        // all legacy models have the same dependencies
329
        if (strpos($class_name, 'EEM_') === 0) {
330
            $class_name = 'LEGACY_MODELS';
331
        }
332
        $dependency = $this->getFqnForAlias($dependency, $class_name);
333
        return isset($this->_dependency_map[ $class_name ][ $dependency ]);
334
    }
335
336
337
    /**
338
     * returns loading strategy for whether a previously cached dependency should be loaded or a new instance returned
339
     *
340
     * @param string $class_name
341
     * @param string $dependency
342
     * @return int
343
     */
344 View Code Duplication
    public function loading_strategy_for_class_dependency($class_name = '', $dependency = '')
345
    {
346
        // all legacy models have the same dependencies
347
        if (strpos($class_name, 'EEM_') === 0) {
348
            $class_name = 'LEGACY_MODELS';
349
        }
350
        $dependency = $this->getFqnForAlias($dependency);
351
        return $this->has_dependency_for_class($class_name, $dependency)
352
            ? $this->_dependency_map[ $class_name ][ $dependency ]
353
            : EE_Dependency_Map::not_registered;
354
    }
355
356
357
    /**
358
     * @param string $class_name
359
     * @return string | Closure
360
     */
361
    public function class_loader($class_name)
362
    {
363
        // all legacy models use load_model()
364
        if (strpos($class_name, 'EEM_') === 0) {
365
            return 'load_model';
366
        }
367
        // EE_CPT_*_Strategy classes like EE_CPT_Event_Strategy, EE_CPT_Venue_Strategy, etc
368
        // perform strpos() first to avoid loading regex every time we load a class
369
        if (strpos($class_name, 'EE_CPT_') === 0
370
            && preg_match('/^EE_CPT_([a-zA-Z]+)_Strategy$/', $class_name)
371
        ) {
372
            return 'load_core';
373
        }
374
        $class_name = $this->getFqnForAlias($class_name);
375
        return isset($this->_class_loaders[ $class_name ]) ? $this->_class_loaders[ $class_name ] : '';
376
    }
377
378
379
    /**
380
     * @return array
381
     */
382
    public function class_loaders()
383
    {
384
        return $this->_class_loaders;
385
    }
386
387
388
    /**
389
     * adds an alias for a classname
390
     *
391
     * @param string $fqcn      the class name that should be used (concrete class to replace interface)
392
     * @param string $alias     the class name that would be type hinted for (abstract parent or interface)
393
     * @param string $for_class the class that has the dependency (is type hinting for the interface)
394
     * @throws InvalidAliasException
395
     */
396
    public function add_alias($fqcn, $alias, $for_class = '')
397
    {
398
        $this->class_cache->addAlias($fqcn, $alias, $for_class);
399
    }
400
401
402
    /**
403
     * Returns TRUE if the provided fully qualified name IS an alias
404
     * WHY?
405
     * Because if a class is type hinting for a concretion,
406
     * then why would we need to find another class to supply it?
407
     * ie: if a class asks for `Fully/Qualified/Namespace/SpecificClassName`,
408
     * then give it an instance of `Fully/Qualified/Namespace/SpecificClassName`.
409
     * Don't go looking for some substitute.
410
     * Whereas if a class is type hinting for an interface...
411
     * then we need to find an actual class to use.
412
     * So the interface IS the alias for some other FQN,
413
     * and we need to find out if `Fully/Qualified/Namespace/SomeInterface`
414
     * represents some other class.
415
     *
416
     * @param string $fqn
417
     * @param string $for_class
418
     * @return bool
419
     */
420
    public function isAlias($fqn = '', $for_class = '')
421
    {
422
        return $this->class_cache->isAlias($fqn, $for_class);
423
    }
424
425
426
    /**
427
     * Returns a FQN for provided alias if one exists, otherwise returns the original $alias
428
     * functions recursively, so that multiple aliases can be used to drill down to a FQN
429
     *  for example:
430
     *      if the following two entries were added to the _aliases array:
431
     *          array(
432
     *              'interface_alias'           => 'some\namespace\interface'
433
     *              'some\namespace\interface'  => 'some\namespace\classname'
434
     *          )
435
     *      then one could use EE_Registry::instance()->create( 'interface_alias' )
436
     *      to load an instance of 'some\namespace\classname'
437
     *
438
     * @param string $alias
439
     * @param string $for_class
440
     * @return string
441
     */
442
    public function getFqnForAlias($alias = '', $for_class = '')
443
    {
444
        return (string) $this->class_cache->getFqnForAlias($alias, $for_class);
445
    }
446
447
448
    /**
449
     * Registers the core dependencies and whether a previously instantiated object should be loaded from the cache,
450
     * if one exists, or whether a new object should be generated every time the requested class is loaded.
451
     * This is done by using the following class constants:
452
     *        EE_Dependency_Map::load_from_cache - loads previously instantiated object
453
     *        EE_Dependency_Map::load_new_object - generates a new object every time
454
     */
455
    protected function _register_core_dependencies()
456
    {
457
        $this->_dependency_map = [
458
            'EE_Request_Handler'                                                                                          => [
459
                'EE_Request' => EE_Dependency_Map::load_from_cache,
460
            ],
461
            'EE_System'                                                                                                   => [
462
                'EE_Registry'                                 => EE_Dependency_Map::load_from_cache,
463
                'EventEspresso\core\services\loaders\Loader'  => EE_Dependency_Map::load_from_cache,
464
                'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache,
465
                'EE_Maintenance_Mode'                         => EE_Dependency_Map::load_from_cache,
466
            ],
467
            'EE_Cart'                                                                                                     => [
468
                'EE_Session' => EE_Dependency_Map::load_from_cache,
469
            ],
470
            'EE_Messenger_Collection_Loader'                                                                              => [
471
                'EE_Messenger_Collection' => EE_Dependency_Map::load_new_object,
472
            ],
473
            'EE_Message_Type_Collection_Loader'                                                                           => [
474
                'EE_Message_Type_Collection' => EE_Dependency_Map::load_new_object,
475
            ],
476
            'EE_Message_Resource_Manager'                                                                                 => [
477
                'EE_Messenger_Collection_Loader'    => EE_Dependency_Map::load_new_object,
478
                'EE_Message_Type_Collection_Loader' => EE_Dependency_Map::load_new_object,
479
                'EEM_Message_Template_Group'        => EE_Dependency_Map::load_from_cache,
480
            ],
481
            'EE_Message_Factory'                                                                                          => [
482
                'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache,
483
            ],
484
            'EE_messages'                                                                                                 => [
485
                'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache,
486
            ],
487
            'EE_Messages_Generator'                                                                                       => [
488
                'EE_Messages_Queue'                    => EE_Dependency_Map::load_new_object,
489
                'EE_Messages_Data_Handler_Collection'  => EE_Dependency_Map::load_new_object,
490
                'EE_Message_Template_Group_Collection' => EE_Dependency_Map::load_new_object,
491
                'EEH_Parse_Shortcodes'                 => EE_Dependency_Map::load_from_cache,
492
            ],
493
            'EE_Messages_Processor'                                                                                       => [
494
                'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache,
495
            ],
496
            'EE_Messages_Queue'                                                                                           => [
497
                'EE_Message_Repository' => EE_Dependency_Map::load_new_object,
498
            ],
499
            'EE_Messages_Template_Defaults'                                                                               => [
500
                'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache,
501
                'EEM_Message_Template'       => EE_Dependency_Map::load_from_cache,
502
            ],
503
            'EE_Message_To_Generate_From_Request'                                                                         => [
504
                'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache,
505
                'EE_Request_Handler'          => EE_Dependency_Map::load_from_cache,
506
            ],
507
            'EventEspresso\core\services\commands\CommandBus'                                                             => [
508
                'EventEspresso\core\services\commands\CommandHandlerManager' => EE_Dependency_Map::load_from_cache,
509
            ],
510
            'EventEspresso\services\commands\CommandHandler'                                                              => [
511
                'EE_Registry'         => EE_Dependency_Map::load_from_cache,
512
                'CommandBusInterface' => EE_Dependency_Map::load_from_cache,
513
            ],
514
            'EventEspresso\core\services\commands\CommandHandlerManager'                                                  => [
515
                'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache,
516
            ],
517
            'EventEspresso\core\services\commands\CompositeCommandHandler'                                                => [
518
                'EventEspresso\core\services\commands\CommandBus'     => EE_Dependency_Map::load_from_cache,
519
                'EventEspresso\core\services\commands\CommandFactory' => EE_Dependency_Map::load_from_cache,
520
            ],
521
            'EventEspresso\core\services\commands\CommandFactory'                                                         => [
522
                'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache,
523
            ],
524
            'EventEspresso\core\services\commands\middleware\CapChecker'                                                  => [
525
                'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache,
526
            ],
527
            'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker'                                         => [
528
                'EE_Capabilities' => EE_Dependency_Map::load_from_cache,
529
            ],
530
            'EventEspresso\core\domain\services\capabilities\RegistrationsCapChecker'                                     => [
531
                'EE_Capabilities' => EE_Dependency_Map::load_from_cache,
532
            ],
533
            'EventEspresso\core\services\commands\registration\CreateRegistrationCommandHandler'                          => [
534
                'EventEspresso\core\domain\services\registration\CreateRegistrationService' => EE_Dependency_Map::load_from_cache,
535
            ],
536
            'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommandHandler'                     => [
537
                'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache,
538
            ],
539
            'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommandHandler'                    => [
540
                'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache,
541
            ],
542
            'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler'         => [
543
                'EventEspresso\core\domain\services\registration\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache,
544
            ],
545
            'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler' => [
546
                'EventEspresso\core\domain\services\registration\UpdateRegistrationService' => EE_Dependency_Map::load_from_cache,
547
            ],
548
            'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommandHandler'                              => [
549
                'EventEspresso\core\domain\services\ticket\CreateTicketLineItemService' => EE_Dependency_Map::load_from_cache,
550
            ],
551
            'EventEspresso\core\services\commands\ticket\CancelTicketLineItemCommandHandler'                              => [
552
                'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache,
553
            ],
554
            'EventEspresso\core\domain\services\registration\CancelRegistrationService'                                   => [
555
                'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache,
556
            ],
557
            'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler'                                  => [
558
                'EEM_Attendee' => EE_Dependency_Map::load_from_cache,
559
            ],
560
            'EventEspresso\core\services\database\TableManager'                                                           => [
561
                'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache,
562
            ],
563
            'EE_Data_Migration_Class_Base'                                                                                => [
564
                'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache,
565
                'EventEspresso\core\services\database\TableManager'  => EE_Dependency_Map::load_from_cache,
566
            ],
567
            'EE_DMS_Core_4_1_0'                                                                                           => [
568
                'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache,
569
                'EventEspresso\core\services\database\TableManager'  => EE_Dependency_Map::load_from_cache,
570
            ],
571
            'EE_DMS_Core_4_2_0'                                                                                           => [
572
                'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache,
573
                'EventEspresso\core\services\database\TableManager'  => EE_Dependency_Map::load_from_cache,
574
            ],
575
            'EE_DMS_Core_4_3_0'                                                                                           => [
576
                'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache,
577
                'EventEspresso\core\services\database\TableManager'  => EE_Dependency_Map::load_from_cache,
578
            ],
579
            'EE_DMS_Core_4_4_0'                                                                                           => [
580
                'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache,
581
                'EventEspresso\core\services\database\TableManager'  => EE_Dependency_Map::load_from_cache,
582
            ],
583
            'EE_DMS_Core_4_5_0'                                                                                           => [
584
                'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache,
585
                'EventEspresso\core\services\database\TableManager'  => EE_Dependency_Map::load_from_cache,
586
            ],
587
            'EE_DMS_Core_4_6_0'                                                                                           => [
588
                'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache,
589
                'EventEspresso\core\services\database\TableManager'  => EE_Dependency_Map::load_from_cache,
590
            ],
591
            'EE_DMS_Core_4_7_0'                                                                                           => [
592
                'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache,
593
                'EventEspresso\core\services\database\TableManager'  => EE_Dependency_Map::load_from_cache,
594
            ],
595
            'EE_DMS_Core_4_8_0'                                                                                           => [
596
                'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache,
597
                'EventEspresso\core\services\database\TableManager'  => EE_Dependency_Map::load_from_cache,
598
            ],
599
            'EE_DMS_Core_4_9_0'                                                                                           => [
600
                'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache,
601
                'EventEspresso\core\services\database\TableManager'  => EE_Dependency_Map::load_from_cache,
602
            ],
603
            'EE_DMS_Core_4_10_0'                                                                                          => [
604
                'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache,
605
                'EventEspresso\core\services\database\TableManager'  => EE_Dependency_Map::load_from_cache,
606
                'EE_DMS_Core_4_9_0'                                  => EE_Dependency_Map::load_from_cache,
607
            ],
608
            'EventEspresso\core\services\assets\I18nRegistry'                                                             => [
609
                'EventEspresso\core\domain\Domain'                 => EE_Dependency_Map::load_from_cache,
610
                'EventEspresso\core\services\assets\JedLocaleData' => EE_Dependency_Map::load_from_cache,
611
                [],
612
            ],
613
            'EventEspresso\core\services\assets\Registry'                                                                 => [
614
                'EventEspresso\core\services\assets\AssetCollection' => EE_Dependency_Map::load_from_cache,
615
                'EventEspresso\core\services\assets\I18nRegistry'    => EE_Dependency_Map::load_from_cache,
616
            ],
617
            'EventEspresso\core\services\cache\BasicCacheManager'                                                         => [
618
                'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache,
619
            ],
620
            'EventEspresso\core\services\cache\PostRelatedCacheManager'                                                   => [
621
                'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache,
622
            ],
623
            'EventEspresso\core\domain\services\validation\email\EmailValidationService'                                  => [
624
                'EE_Registration_Config'                     => EE_Dependency_Map::load_from_cache,
625
                'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache,
626
            ],
627
            'EventEspresso\core\domain\values\EmailAddress'                                                               => [
628
                null,
629
                'EventEspresso\core\domain\services\validation\email\EmailValidationService' => EE_Dependency_Map::load_from_cache,
630
            ],
631
            'EventEspresso\core\services\orm\ModelFieldFactory'                                                           => [
632
                'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache,
633
            ],
634
            'LEGACY_MODELS'                                                                                               => [
635
                null,
636
                'EventEspresso\core\services\database\ModelFieldFactory' => EE_Dependency_Map::load_from_cache,
637
            ],
638
            'EE_Module_Request_Router'                                                                                    => [
639
                'EE_Request' => EE_Dependency_Map::load_from_cache,
640
            ],
641
            'EE_Registration_Processor'                                                                                   => [
642
                'EE_Request' => EE_Dependency_Map::load_from_cache,
643
            ],
644
            'EventEspresso\core\services\notifications\PersistentAdminNoticeManager'                                      => [
645
                null,
646
                'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache,
647
                'EventEspresso\core\services\request\Request'                         => EE_Dependency_Map::load_from_cache,
648
            ],
649
            'EventEspresso\caffeinated\modules\recaptcha_invisible\InvisibleRecaptcha'                                    => [
650
                'EE_Registration_Config' => EE_Dependency_Map::load_from_cache,
651
                'EE_Session'             => EE_Dependency_Map::load_from_cache,
652
            ],
653
            'EventEspresso\modules\ticket_selector\ProcessTicketSelector'                                                 => [
654
                'EE_Core_Config'                                                          => EE_Dependency_Map::load_from_cache,
655
                'EventEspresso\core\services\request\Request'                             => EE_Dependency_Map::load_from_cache,
656
                'EE_Session'                                                              => EE_Dependency_Map::load_from_cache,
657
                'EEM_Ticket'                                                              => EE_Dependency_Map::load_from_cache,
658
                'EventEspresso\modules\ticket_selector\TicketDatetimeAvailabilityTracker' => EE_Dependency_Map::load_from_cache,
659
            ],
660
            'EventEspresso\modules\ticket_selector\TicketDatetimeAvailabilityTracker'                                     => [
661
                'EEM_Datetime' => EE_Dependency_Map::load_from_cache,
662
            ],
663
            'EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions'                              => [
664
                'EE_Core_Config'                             => EE_Dependency_Map::load_from_cache,
665
                'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache,
666
            ],
667
            'EventEspresso\core\domain\services\custom_post_types\RegisterCustomPostTypes'                                => [
668
                'EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions' => EE_Dependency_Map::load_from_cache,
669
            ],
670
            'EventEspresso\core\domain\services\custom_post_types\RegisterCustomTaxonomies'                               => [
671
                'EventEspresso\core\domain\entities\custom_post_types\CustomTaxonomyDefinitions' => EE_Dependency_Map::load_from_cache,
672
            ],
673
            'EE_CPT_Strategy'                                                                                             => [
674
                'EventEspresso\core\domain\entities\custom_post_types\CustomPostTypeDefinitions' => EE_Dependency_Map::load_from_cache,
675
                'EventEspresso\core\domain\entities\custom_post_types\CustomTaxonomyDefinitions' => EE_Dependency_Map::load_from_cache,
676
            ],
677
            'EventEspresso\core\services\loaders\ObjectIdentifier'                                                        => [
678
                'EventEspresso\core\services\loaders\ClassInterfaceCache' => EE_Dependency_Map::load_from_cache,
679
            ],
680
            'EventEspresso\core\CPTs\CptQueryModifier'                                                                    => [
681
                null,
682
                null,
683
                null,
684
                'EE_Request_Handler'                          => EE_Dependency_Map::load_from_cache,
685
                'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache,
686
                'EventEspresso\core\services\loaders\Loader'  => EE_Dependency_Map::load_from_cache,
687
            ],
688
            'EventEspresso\core\services\dependencies\DependencyResolver'                                                 => [
689
                'EventEspresso\core\services\container\Mirror'            => EE_Dependency_Map::load_from_cache,
690
                'EventEspresso\core\services\loaders\ClassInterfaceCache' => EE_Dependency_Map::load_from_cache,
691
                'EE_Dependency_Map'                                       => EE_Dependency_Map::load_from_cache,
692
            ],
693
            'EventEspresso\core\services\routing\RouteMatchSpecificationDependencyResolver'                               => [
694
                'EventEspresso\core\services\container\Mirror'            => EE_Dependency_Map::load_from_cache,
695
                'EventEspresso\core\services\loaders\ClassInterfaceCache' => EE_Dependency_Map::load_from_cache,
696
                'EE_Dependency_Map'                                       => EE_Dependency_Map::load_from_cache,
697
            ],
698
            'EventEspresso\core\services\routing\RouteMatchSpecificationFactory'                                          => [
699
                'EventEspresso\core\services\routing\RouteMatchSpecificationDependencyResolver' => EE_Dependency_Map::load_from_cache,
700
                'EventEspresso\core\services\loaders\Loader'                                    => EE_Dependency_Map::load_from_cache,
701
            ],
702
            'EventEspresso\core\services\routing\RouteMatchSpecificationManager'                                          => [
703
                'EventEspresso\core\services\routing\RouteMatchSpecificationCollection' => EE_Dependency_Map::load_from_cache,
704
                'EventEspresso\core\services\routing\RouteMatchSpecificationFactory'    => EE_Dependency_Map::load_from_cache,
705
            ],
706
            'EE_URL_Validation_Strategy'                                                                                  => [
707
                null,
708
                null,
709
                'EventEspresso\core\services\validators\URLValidator' => EE_Dependency_Map::load_from_cache
710
            ],
711
            'EventEspresso\core\services\request\files\FilesDataHandler'                                                  => [
712
                'EventEspresso\core\services\request\Request' => EE_Dependency_Map::load_from_cache,
713
            ],
714
            'EventEspressoBatchRequest\BatchRequestProcessor'                                                             => [
715
                'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache,
716
            ],
717
            'EventEspresso\core\domain\services\converters\RestApiSpoofer'                                                => [
718
                'WP_REST_Server'                                               => EE_Dependency_Map::load_from_cache,
719
                'EED_Core_Rest_Api'                                            => EE_Dependency_Map::load_from_cache,
720
                'EventEspresso\core\libraries\rest_api\controllers\model\Read' => EE_Dependency_Map::load_from_cache,
721
                null
722
            ],
723
            'EventEspresso\core\services\routing\RouteHandler'                                                            => [
724
                'EventEspresso\core\services\loaders\Loader'          => EE_Dependency_Map::load_from_cache,
725
                'EventEspresso\core\services\routing\RouteCollection' => EE_Dependency_Map::load_from_cache,
726
            ],
727
            'EventEspresso\core\domain\entities\routing\handlers\shared\RoutingRequests'                                          => [
728
                'EE_Dependency_Map'                                                                          => EE_Dependency_Map::load_from_cache,
729
                'EventEspresso\core\services\loaders\Loader'                                                 => EE_Dependency_Map::load_from_cache,
730
                'EventEspresso\core\services\request\Request'                                                => EE_Dependency_Map::load_from_cache,
731
                'EventEspresso\core\domain\entities\routing\specifications\RouteMatchSpecificationInterface' => EE_Dependency_Map::load_from_cache,
732
            ],
733
        ];
734
    }
735
736
737
    /**
738
     * Registers how core classes are loaded.
739
     * This can either be done by simply providing the name of one of the EE_Registry loader methods such as:
740
     *        'EE_Request_Handler' => 'load_core'
741
     *        'EE_Messages_Queue'  => 'load_lib'
742
     *        'EEH_Debug_Tools'    => 'load_helper'
743
     * or, if greater control is required, by providing a custom closure. For example:
744
     *        'Some_Class' => function () {
745
     *            return new Some_Class();
746
     *        },
747
     * This is required for instantiating dependencies
748
     * where an interface has been type hinted in a class constructor. For example:
749
     *        'Required_Interface' => function () {
750
     *            return new A_Class_That_Implements_Required_Interface();
751
     *        },
752
     */
753
    protected function _register_core_class_loaders()
754
    {
755
        $this->_class_loaders = [
756
            // load_core
757
            'EE_Dependency_Map'                            => function () {
758
                return $this;
759
            },
760
            'EE_Capabilities'                              => 'load_core',
761
            'EE_Encryption'                                => 'load_core',
762
            'EE_Front_Controller'                          => 'load_core',
763
            'EE_Module_Request_Router'                     => 'load_core',
764
            'EE_Registry'                                  => 'load_core',
765
            'EE_Request'                                   => function () {
766
                return $this->legacy_request;
767
            },
768
            'EventEspresso\core\services\request\Request'  => function () {
769
                return $this->request;
770
            },
771
            'EventEspresso\core\services\request\Response' => function () {
772
                return $this->response;
773
            },
774
            'EE_Base'                                      => 'load_core',
775
            'EE_Request_Handler'                           => 'load_core',
776
            'EE_Session'                                   => 'load_core',
777
            'EE_Cron_Tasks'                                => 'load_core',
778
            'EE_System'                                    => 'load_core',
779
            'EE_Maintenance_Mode'                          => 'load_core',
780
            'EE_Register_CPTs'                             => 'load_core',
781
            'EE_Admin'                                     => 'load_core',
782
            'EE_CPT_Strategy'                              => 'load_core',
783
            // load_class
784
            'EE_Registration_Processor'                    => 'load_class',
785
            // load_lib
786
            'EE_Message_Resource_Manager'                  => 'load_lib',
787
            'EE_Message_Type_Collection'                   => 'load_lib',
788
            'EE_Message_Type_Collection_Loader'            => 'load_lib',
789
            'EE_Messenger_Collection'                      => 'load_lib',
790
            'EE_Messenger_Collection_Loader'               => 'load_lib',
791
            'EE_Messages_Processor'                        => 'load_lib',
792
            'EE_Message_Repository'                        => 'load_lib',
793
            'EE_Messages_Queue'                            => 'load_lib',
794
            'EE_Messages_Data_Handler_Collection'          => 'load_lib',
795
            'EE_Message_Template_Group_Collection'         => 'load_lib',
796
            'EE_Payment_Method_Manager'                    => 'load_lib',
797
            'EE_DMS_Core_4_1_0'                            => 'load_dms',
798
            'EE_DMS_Core_4_2_0'                            => 'load_dms',
799
            'EE_DMS_Core_4_3_0'                            => 'load_dms',
800
            'EE_DMS_Core_4_5_0'                            => 'load_dms',
801
            'EE_DMS_Core_4_6_0'                            => 'load_dms',
802
            'EE_DMS_Core_4_7_0'                            => 'load_dms',
803
            'EE_DMS_Core_4_8_0'                            => 'load_dms',
804
            'EE_DMS_Core_4_9_0'                            => 'load_dms',
805
            'EE_DMS_Core_4_10_0'                           => 'load_dms',
806
            'EE_Messages_Generator'                        => static function () {
807
                return EE_Registry::instance()->load_lib(
808
                    'Messages_Generator',
809
                    [],
810
                    false,
811
                    false
812
                );
813
            },
814
            'EE_Messages_Template_Defaults'                => static function ($arguments = []) {
815
                return EE_Registry::instance()->load_lib(
816
                    'Messages_Template_Defaults',
817
                    $arguments,
818
                    false,
819
                    false
820
                );
821
            },
822
            // load_helper
823
            'EEH_Parse_Shortcodes'                         => static function () {
824
                if (EE_Registry::instance()->load_helper('Parse_Shortcodes')) {
825
                    return new EEH_Parse_Shortcodes();
826
                }
827
                return null;
828
            },
829
            'EE_Template_Config'                           => static function () {
830
                return EE_Config::instance()->template_settings;
831
            },
832
            'EE_Currency_Config'                           => static function () {
833
                return EE_Config::instance()->currency;
834
            },
835
            'EE_Registration_Config'                       => static function () {
836
                return EE_Config::instance()->registration;
837
            },
838
            'EE_Core_Config'                               => static function () {
839
                return EE_Config::instance()->core;
840
            },
841
            'EventEspresso\core\services\loaders\Loader'   => static function () {
842
                return LoaderFactory::getLoader();
843
            },
844
            'EE_Network_Config'                            => static function () {
845
                return EE_Network_Config::instance();
846
            },
847
            'EE_Config'                                    => static function () {
848
                return EE_Config::instance();
849
            },
850
            'EventEspresso\core\domain\Domain'             => static function () {
851
                return DomainFactory::getEventEspressoCoreDomain();
852
            },
853
            'EE_Admin_Config'                              => static function () {
854
                return EE_Config::instance()->admin;
855
            },
856
            'EE_Organization_Config'                       => static function () {
857
                return EE_Config::instance()->organization;
858
            },
859
            'EE_Network_Core_Config'                       => static function () {
860
                return EE_Network_Config::instance()->core;
861
            },
862
            'EE_Environment_Config'                        => static function () {
863
                return EE_Config::instance()->environment;
864
            },
865
            'EED_Core_Rest_Api'                            => static function () {
866
                return EED_Core_Rest_Api::instance();
867
            },
868
            'WP_REST_Server'                               => static function () {
869
                return rest_get_server();
870
            },
871
        ];
872
    }
873
874
875
    /**
876
     * can be used for supplying alternate names for classes,
877
     * or for connecting interface names to instantiable classes
878
     *
879
     * @throws InvalidAliasException
880
     */
881
    protected function _register_core_aliases()
882
    {
883
        $aliases = [
884
            'CommandBusInterface'                                                          => 'EventEspresso\core\services\commands\CommandBusInterface',
885
            'EventEspresso\core\services\commands\CommandBusInterface'                     => 'EventEspresso\core\services\commands\CommandBus',
886
            'CommandHandlerManagerInterface'                                               => 'EventEspresso\core\services\commands\CommandHandlerManagerInterface',
887
            'EventEspresso\core\services\commands\CommandHandlerManagerInterface'          => 'EventEspresso\core\services\commands\CommandHandlerManager',
888
            'CapChecker'                                                                   => 'EventEspresso\core\services\commands\middleware\CapChecker',
889
            'AddActionHook'                                                                => 'EventEspresso\core\services\commands\middleware\AddActionHook',
890
            'CapabilitiesChecker'                                                          => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker',
891
            'CapabilitiesCheckerInterface'                                                 => 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface',
892
            'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker',
893
            'CreateRegistrationService'                                                    => 'EventEspresso\core\domain\services\registration\CreateRegistrationService',
894
            'CreateRegistrationCommandHandler'                                             => 'EventEspresso\core\services\commands\registration\CreateRegistrationCommand',
895
            'CopyRegistrationDetailsCommandHandler'                                        => 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommand',
896
            'CopyRegistrationPaymentsCommandHandler'                                       => 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommand',
897
            'CancelRegistrationAndTicketLineItemCommandHandler'                            => 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler',
898
            'UpdateRegistrationAndTransactionAfterChangeCommandHandler'                    => 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler',
899
            'CreateTicketLineItemCommandHandler'                                           => 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommand',
900
            'CreateTransactionCommandHandler'                                              => 'EventEspresso\core\services\commands\transaction\CreateTransactionCommandHandler',
901
            'CreateAttendeeCommandHandler'                                                 => 'EventEspresso\core\services\commands\attendee\CreateAttendeeCommandHandler',
902
            'TableManager'                                                                 => 'EventEspresso\core\services\database\TableManager',
903
            'TableAnalysis'                                                                => 'EventEspresso\core\services\database\TableAnalysis',
904
            'EspressoShortcode'                                                            => 'EventEspresso\core\services\shortcodes\EspressoShortcode',
905
            'ShortcodeInterface'                                                           => 'EventEspresso\core\services\shortcodes\ShortcodeInterface',
906
            'EventEspresso\core\services\shortcodes\ShortcodeInterface'                    => 'EventEspresso\core\services\shortcodes\EspressoShortcode',
907
            'EventEspresso\core\services\cache\CacheStorageInterface'                      => 'EventEspresso\core\services\cache\TransientCacheStorage',
908
            'LoaderInterface'                                                              => 'EventEspresso\core\services\loaders\LoaderInterface',
909
            'EventEspresso\core\services\loaders\LoaderInterface'                          => 'EventEspresso\core\services\loaders\Loader',
910
            'CommandFactoryInterface'                                                      => 'EventEspresso\core\services\commands\CommandFactoryInterface',
911
            'EventEspresso\core\services\commands\CommandFactoryInterface'                 => 'EventEspresso\core\services\commands\CommandFactory',
912
            'EmailValidatorInterface'                                                      => 'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface',
913
            'EventEspresso\core\domain\services\validation\email\EmailValidatorInterface'  => 'EventEspresso\core\domain\services\validation\email\EmailValidationService',
914
            'NoticeConverterInterface'                                                     => 'EventEspresso\core\services\notices\NoticeConverterInterface',
915
            'EventEspresso\core\services\notices\NoticeConverterInterface'                 => 'EventEspresso\core\services\notices\ConvertNoticesToEeErrors',
916
            'NoticesContainerInterface'                                                    => 'EventEspresso\core\services\notices\NoticesContainerInterface',
917
            'EventEspresso\core\services\notices\NoticesContainerInterface'                => 'EventEspresso\core\services\notices\NoticesContainer',
918
            'EventEspresso\core\services\request\RequestInterface'                         => 'EventEspresso\core\services\request\Request',
919
            'EventEspresso\core\services\request\ResponseInterface'                        => 'EventEspresso\core\services\request\Response',
920
            'EventEspresso\core\domain\DomainInterface'                                    => 'EventEspresso\core\domain\Domain',
921
            'Registration_Processor'                                                       => 'EE_Registration_Processor',
922
        ];
923
        foreach ($aliases as $alias => $fqn) {
924
            if (is_array($fqn)) {
925
                foreach ($fqn as $class => $for_class) {
926
                    $this->class_cache->addAlias($class, $alias, $for_class);
927
                }
928
                continue;
929
            }
930
            $this->class_cache->addAlias($fqn, $alias);
931
        }
932
        if (! (defined('DOING_AJAX') && DOING_AJAX) && is_admin()) {
933
            $this->class_cache->addAlias(
934
                'EventEspresso\core\services\notices\ConvertNoticesToAdminNotices',
935
                'EventEspresso\core\services\notices\NoticeConverterInterface'
936
            );
937
        }
938
    }
939
940
941
    /**
942
     * This is used to reset the internal map and class_loaders to their original default state at the beginning of the
943
     * request Primarily used by unit tests.
944
     */
945
    public function reset()
946
    {
947
        $this->_register_core_class_loaders();
948
        $this->_register_core_dependencies();
949
    }
950
951
952
    /**
953
     * PLZ NOTE: a better name for this method would be is_alias()
954
     * because it returns TRUE if the provided fully qualified name IS an alias
955
     * WHY?
956
     * Because if a class is type hinting for a concretion,
957
     * then why would we need to find another class to supply it?
958
     * ie: if a class asks for `Fully/Qualified/Namespace/SpecificClassName`,
959
     * then give it an instance of `Fully/Qualified/Namespace/SpecificClassName`.
960
     * Don't go looking for some substitute.
961
     * Whereas if a class is type hinting for an interface...
962
     * then we need to find an actual class to use.
963
     * So the interface IS the alias for some other FQN,
964
     * and we need to find out if `Fully/Qualified/Namespace/SomeInterface`
965
     * represents some other class.
966
     *
967
     * @param string $fqn
968
     * @param string $for_class
969
     * @return bool
970
     * @deprecated 4.9.62.p
971
     */
972
    public function has_alias($fqn = '', $for_class = '')
973
    {
974
        return $this->isAlias($fqn, $for_class);
975
    }
976
977
978
    /**
979
     * PLZ NOTE: a better name for this method would be get_fqn_for_alias()
980
     * because it returns a FQN for provided alias if one exists, otherwise returns the original $alias
981
     * functions recursively, so that multiple aliases can be used to drill down to a FQN
982
     *  for example:
983
     *      if the following two entries were added to the _aliases array:
984
     *          array(
985
     *              'interface_alias'           => 'some\namespace\interface'
986
     *              'some\namespace\interface'  => 'some\namespace\classname'
987
     *          )
988
     *      then one could use EE_Registry::instance()->create( 'interface_alias' )
989
     *      to load an instance of 'some\namespace\classname'
990
     *
991
     * @param string $alias
992
     * @param string $for_class
993
     * @return string
994
     * @deprecated 4.9.62.p
995
     */
996
    public function get_alias($alias = '', $for_class = '')
997
    {
998
        return $this->getFqnForAlias($alias, $for_class);
999
    }
1000
}
1001