ModuleList::readNeedMeDependencies()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 0
dl 0
loc 22
rs 9.4888
c 0
b 0
f 0
1
<?php
2
3
namespace BFW\Core;
4
5
use \Exception;
6
7
/**
8
 * Class to manage all modules in the application
9
 */
10
class ModuleList
11
{
12
    /**
13
     * @const ERR_NOT_FOUND Exception code if a module is not found
14
     */
15
    const ERR_NOT_FOUND = 1204001;
16
    
17
    /**
18
     * @const ERR_NEEDED_NOT_FOUND Exception code if a needed dependency is
19
     * not found.
20
     */
21
    const ERR_NEEDED_NOT_FOUND = 1204002;
22
    
23
    /**
24
     * @var \BFW\Module[] All module instance
25
     */
26
    protected $modules = [];
27
28
    /**
29
     * @var array $loadTree The dependency tree for all modules
30
     */
31
    protected $loadTree = [];
32
33
    /**
34
     * Get the module's list
35
     * 
36
     * @return \BFW\Module[]
37
     */
38
    public function getModules(): array
39
    {
40
        return $this->modules;
41
    }
42
43
    /**
44
     * Get the dependency tree
45
     * 
46
     * @return array
47
     */
48
    public function getLoadTree(): array
49
    {
50
        return $this->loadTree;
51
    }
52
53
    /**
54
     * Add a module to the modules's list
55
     * And instantiate \BFW\Module for this module
56
     * 
57
     * @param string $moduleName The module's name
58
     * 
59
     * @return void
60
     */
61
    public function addModule(string $moduleName)
62
    {
63
        $this->modules[$moduleName] = new \BFW\Module($moduleName);
64
        $this->modules[$moduleName]->loadModule();
65
    }
66
67
    /**
68
     * Check if a module exist in the list
69
     *
70
     * @param string $moduleName The module's name
71
     *
72
     * @return boolean
73
     */
74
    public function hasModule(string $moduleName)
75
    {
76
        return array_key_exists($moduleName, $this->modules);
77
    }
78
79
    /**
80
     * Get the \BFW\Module instance for a module
81
     * 
82
     * @param string $moduleName The module's name
83
     * 
84
     * @return \BFW\Module
85
     * 
86
     * @throws \Exception If the module is not found
87
     */
88
    public function getModuleByName(string $moduleName): \BFW\Module
89
    {
90
        if ($this->hasModule($moduleName) === false) {
91
            throw new Exception(
92
                'The Module '.$moduleName.' has not been found.',
93
                $this::ERR_NOT_FOUND
94
            );
95
        }
96
97
        return $this->modules[$moduleName];
98
    }
99
    
100
    /**
101
     * Read the "needMe" property for each module and add the dependency
102
     * 
103
     * @throws \Exception If the dependency is not found
104
     * 
105
     * @return void
106
     */
107
    public function readNeedMeDependencies()
108
    {
109
        foreach ($this->modules as $readModuleName => $module) {
110
            $loadInfos = $module->getLoadInfos();
111
            
112
            if (!property_exists($loadInfos, 'needMe')) {
113
                continue;
114
            }
115
            
116
            $needMe = (array) $loadInfos->needMe;
117
            foreach ($needMe as $needModuleName) {
118
                if (!isset($this->modules[$needModuleName])) {
119
                    throw new Exception(
120
                        'Module error: '.$readModuleName
121
                        .' need '.$needModuleName
122
                        .' but the module has not been found.',
123
                        $this::ERR_NEEDED_NOT_FOUND
124
                    );
125
                }
126
                
127
                $this->modules[$needModuleName]->addDependency(
128
                    $readModuleName
129
                );
130
            }
131
        }
132
    }
133
134
    /**
135
     * Generate the dependency tree for all declared module
136
     * 
137
     * @return void
138
     */
139
    public function generateTree()
140
    {
141
        $tree = new \bultonFr\DependencyTree\DependencyTree;
142
143
        foreach ($this->modules as $moduleName => $module) {
144
            $priority = 0;
145
            $depends  = [];
146
147
            $loadInfos = $module->getLoadInfos();
148
            if (property_exists($loadInfos, 'priority')) {
149
                $priority = (int) $loadInfos->priority;
150
            }
151
            if (property_exists($loadInfos, 'require')) {
152
                $depends = (array) $loadInfos->require;
153
            }
154
155
            $tree->addDependency($moduleName, $priority, $depends);
156
        }
157
158
        $this->loadTree = $tree->generateTree();
159
    }
160
}
161