Completed
Branch new-addon-api (b7bde0)
by
unknown
18:29 queued 08:41
created

AddonManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A initialize() 0 6 1
A loadAddons() 0 24 4
A registerAddon() 0 11 2
1
<?php
2
3
namespace EventEspresso\core\services\addon;
4
5
use EE_Error;
6
use EEH_Autoloader;
7
use EventEspresso\core\exceptions\ExceptionStackTraceDisplay;
8
use EventEspresso\core\Psr4Autoloader;
9
use EventEspresso\core\services\addon\api\AddonApiVersion;
10
use EventEspresso\core\services\addon\api\IncompatibleAddonHandler;
11
use EventEspresso\core\services\addon\api\ThirdPartyPluginHandler;
12
use EventEspresso\core\services\addon\api\v1\AddonApi as AddonApiV1;
13
use EventEspresso\core\services\addon\api\v1\RegisterAddon as RegisterV1Addon;
14
use Exception;
15
use ReflectionException;
16
17
/**
18
 * Class AddonManager
19
 *
20
 * @author  Brent Christensen
21
 * @package EventEspresso\core\services\addon
22
 * @since   $VID:$
23
 */
24
class AddonManager
25
{
26
    /**
27
     * @var AddonCollection
28
     */
29
    private $addons;
30
31
    /**
32
     * @var IncompatibleAddonHandler
33
     */
34
    private $incompatible_addon_handler;
35
36
    /**
37
     * @var Psr4Autoloader
38
     */
39
    private $psr4_loader;
40
41
    /**
42
     * @var RegisterV1Addon
43
     */
44
    private $register_v1_addon;
45
46
    /**
47
     * @var ThirdPartyPluginHandler
48
     */
49
    private $third_party_plugin_handler;
50
51
52
    /**
53
     * AddonManager constructor.
54
     *
55
     * @param AddonCollection          $addons
56
     * @param Psr4Autoloader           $psr4_loader
57
     * @param RegisterV1Addon          $register_v1_addon
58
     * @param IncompatibleAddonHandler $incompatible_addon_handler
59
     * @param ThirdPartyPluginHandler  $third_party_plugin_handler
60
     */
61
    public function __construct(
62
        AddonCollection $addons,
63
        Psr4Autoloader $psr4_loader,
64
        RegisterV1Addon $register_v1_addon,
65
        IncompatibleAddonHandler $incompatible_addon_handler,
66
        ThirdPartyPluginHandler $third_party_plugin_handler
67
    ) {
68
        $this->addons                     = $addons;
69
        $this->psr4_loader                = $psr4_loader;
70
        $this->register_v1_addon          = $register_v1_addon;
71
        $this->incompatible_addon_handler = $incompatible_addon_handler;
72
        $this->third_party_plugin_handler = $third_party_plugin_handler;
73
    }
74
75
76
    /**
77
     * @throws Exception
78
     */
79
    public function initialize()
80
    {
81
        // set autoloaders for all of the classes implementing the legacy EEI_Plugin_API
82
        // which provide helpers for EE plugin authors to more easily register certain components with EE.
83
        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'plugin_api');
84
    }
85
86
87
    /**
88
     * @throws Exception
89
     */
90
    public function loadAddons()
91
    {
92
        try {
93
            $this->incompatible_addon_handler->deactivateIncompatibleAddons();
94
            // legacy add-on API
95
            do_action('AHEE__EE_System__load_espresso_addons');
96
            // new add-on API that uses versioning
97
            do_action(
98
                'AHEE__EventEspresso_core_services_addon_AddonManager__initialize__addons',
99
                $this->addons,
100
                espresso_version()
101
            );
102
            // addons are responsible for loading their AddonApiVersion into the AddonCollection
103
            foreach ($this->addons as $addon) {
104
                if ($addon instanceof AddonApiVersion) {
105
                    $this->registerAddon($addon);
106
                }
107
            }
108
            $this->third_party_plugin_handler->loadPlugins();
109
            do_action('AHEE__EE_System__load_espresso_addons__complete');
110
        } catch (Exception $exception) {
111
            new ExceptionStackTraceDisplay($exception);
112
        }
113
    }
114
115
116
    /**
117
     * @param AddonApiVersion $addon
118
     * @throws EE_Error
119
     */
120
    private function registerAddon(AddonApiVersion $addon)
121
    {
122
        // first register addon namespace so that FQCNs resolve correctly
123
        $this->psr4_loader->addNamespace($addon->getNamespace(), dirname($addon->mainFile()) . '/src/');
124
        // then allow add-on to perform any other setup that relied on PSR4 autoloading
125
        $addon->initialize();
126
        // now register each addon based on it's API version
127
        if ($addon instanceof AddonApiV1) {
128
            $this->register_v1_addon->register($addon);
129
        }
130
    }
131
}
132