Passed
Push — 3.0 ( 1eb04a...9ca0ec )
by Vermeulen
02:07
created

ModuleList::getModuleByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 10
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
     * Get the \BFW\Module instance for a module
69
     * 
70
     * @param string $moduleName The module's name
71
     * 
72
     * @return \BFW\Module
73
     * 
74
     * @throws \Exception If the module is not found
75
     */
76
    public function getModuleByName(string $moduleName): \BFW\Module
77
    {
78
        if (!isset($this->modules[$moduleName])) {
79
            throw new Exception(
80
                'The Module '.$moduleName.' has not been found.',
81
                $this::ERR_NOT_FOUND
82
            );
83
        }
84
85
        return $this->modules[$moduleName];
86
    }
87
    
88
    /**
89
     * Read the "needMe" property for each module and add the dependency
90
     * 
91
     * @throws \Exception If the dependency is not found
92
     * 
93
     * @return void
94
     */
95
    public function readNeedMeDependencies()
96
    {
97
        foreach ($this->modules as $readModuleName => $module) {
98
            $loadInfos = $module->getLoadInfos();
99
            
100
            if (!property_exists($loadInfos, 'needMe')) {
101
                continue;
102
            }
103
            
104
            $needMe = (array) $loadInfos->needMe;
105
            foreach ($needMe as $needModuleName) {
106
                if (!isset($this->modules[$needModuleName])) {
107
                    throw new Exception(
108
                        'Module error: '.$readModuleName
109
                        .' need '.$needModuleName
110
                        .' but the module has not been found.',
111
                        $this::ERR_NEEDED_NOT_FOUND
112
                    );
113
                }
114
                
115
                $this->modules[$needModuleName]->addDependency(
116
                    $readModuleName
117
                );
118
            }
119
        }
120
    }
121
122
    /**
123
     * Generate the dependency tree for all declared module
124
     * 
125
     * @return void
126
     */
127
    public function generateTree()
128
    {
129
        $tree = new \bultonFr\DependencyTree\DependencyTree;
130
131
        foreach ($this->modules as $moduleName => $module) {
132
            $priority = 0;
133
            $depends  = [];
134
135
            $loadInfos = $module->getLoadInfos();
136
            if (property_exists($loadInfos, 'priority')) {
137
                $priority = (int) $loadInfos->priority;
138
            }
139
            if (property_exists($loadInfos, 'require')) {
140
                $depends = (array) $loadInfos->require;
141
            }
142
143
            $tree->addDependency($moduleName, $priority, $depends);
144
        }
145
146
        $this->loadTree = $tree->generateTree();
147
    }
148
}
149