Completed
Branch Gutenberg/master (b3a823)
by
unknown
73:52 queued 60:28
created

EE_Dependency_Map::has()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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