1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BFW; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
use \stdClass; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class to manage a module |
10
|
|
|
*/ |
11
|
|
|
class Module |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string $pathName Module's name |
15
|
|
|
*/ |
16
|
|
|
protected $pathName = ''; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \BFW\Config $config Config object for this module |
20
|
|
|
*/ |
21
|
|
|
protected $config; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \stdClass $loadInfos All informations about the module running |
25
|
|
|
*/ |
26
|
|
|
protected $loadInfos; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
* @var \stdClass $status Load and run status |
31
|
|
|
*/ |
32
|
|
|
protected $status; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor |
36
|
|
|
* Load all informations if $loadModule is true |
37
|
|
|
* |
38
|
|
|
* @param string $pathName Module name |
39
|
|
|
* @param boolean $loadModule (default true) If run load information |
40
|
|
|
*/ |
41
|
|
|
public function __construct($pathName, $loadModule = true) |
42
|
|
|
{ |
43
|
|
|
$this->pathName = $pathName; |
44
|
|
|
|
45
|
|
|
if ($loadModule === true) { |
46
|
|
|
$this->loadModule(); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Load informations about the module |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function loadModule() |
56
|
|
|
{ |
57
|
|
|
$this->status = new stdClass(); |
58
|
|
|
$this->status->load = false; |
59
|
|
|
$this->status->run = false; |
60
|
|
|
|
61
|
|
|
$this->loadConfig(); |
62
|
|
|
$this->loadModuleInfos(); |
63
|
|
|
|
64
|
|
|
$this->status->load = true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get installation informations |
69
|
|
|
* |
70
|
|
|
* @param string $sourceFiles Path to module source (in vendor) |
71
|
|
|
* |
72
|
|
|
* @return \stdClass |
73
|
|
|
*/ |
74
|
|
|
public static function installInfos($sourceFiles) |
75
|
|
|
{ |
76
|
|
|
$currentClass = get_called_class(); //Allow extends |
77
|
|
|
return $currentClass::loadJsonFile($sourceFiles.'/bfwModulesInfos.json'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the module's name |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getPathName() |
86
|
|
|
{ |
87
|
|
|
return $this->pathName; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the Config object which have config for this module |
92
|
|
|
* |
93
|
|
|
* @return \BFW\Config |
94
|
|
|
*/ |
95
|
|
|
public function getConfig() |
96
|
|
|
{ |
97
|
|
|
return $this->config; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the load informations |
102
|
|
|
* |
103
|
|
|
* @return \stdClass |
104
|
|
|
*/ |
105
|
|
|
public function getLoadInfos() |
106
|
|
|
{ |
107
|
|
|
return $this->loadInfos; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the status object for this module |
112
|
|
|
* |
113
|
|
|
* @return \stdClass |
114
|
|
|
*/ |
115
|
|
|
public function getStatus() |
116
|
|
|
{ |
117
|
|
|
return $this->status; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Return the load status |
122
|
|
|
* |
123
|
|
|
* @return boolean |
124
|
|
|
*/ |
125
|
|
|
public function isLoaded() |
126
|
|
|
{ |
127
|
|
|
return $this->status->load; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Return the run status |
132
|
|
|
* |
133
|
|
|
* @return boolean |
134
|
|
|
*/ |
135
|
|
|
public function isRun() |
136
|
|
|
{ |
137
|
|
|
return $this->status->run; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Instantiate the Config object to obtains module's configuration |
142
|
|
|
* |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
public function loadConfig() |
146
|
|
|
{ |
147
|
|
|
if (!file_exists(CONFIG_DIR.$this->pathName)) { |
148
|
|
|
return; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$this->config = new \BFW\Config($this->pathName); |
152
|
|
|
$this->config->loadFiles(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get load information from json file |
157
|
|
|
* |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
public function loadModuleInfos() |
161
|
|
|
{ |
162
|
|
|
$currentClass = get_called_class(); //Allow extends |
163
|
|
|
|
164
|
|
|
$this->loadInfos = $currentClass::loadJsonFile( |
165
|
|
|
MODULES_DIR.$this->pathName |
166
|
|
|
.'/module.json' |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Read a json file and return datas in json |
172
|
|
|
* |
173
|
|
|
* @param string $jsonFilePath : The path to the file to read |
174
|
|
|
* |
175
|
|
|
* @return mixed Json parsed datas |
176
|
|
|
* |
177
|
|
|
* @throws Exception If the file is not found or for a json parser error |
178
|
|
|
*/ |
179
|
|
|
protected static function loadJsonFile($jsonFilePath) |
180
|
|
|
{ |
181
|
|
|
if (!file_exists($jsonFilePath)) { |
182
|
|
|
throw new Exception('File '.$jsonFilePath.' not found.'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$infos = json_decode(file_get_contents($jsonFilePath)); |
186
|
|
|
if ($infos === null) { |
187
|
|
|
throw new Exception(json_last_error_msg()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $infos; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Add a dependency to module |
195
|
|
|
* Used by needMe property in module infos |
196
|
|
|
* |
197
|
|
|
* @param string $dependencyName The dependency name to add |
198
|
|
|
* |
199
|
|
|
* @return $this |
200
|
|
|
*/ |
201
|
|
|
public function addDependency($dependencyName) |
202
|
|
|
{ |
203
|
|
|
if (!property_exists($this->loadInfos, 'require')) { |
204
|
|
|
$this->loadInfos->require = []; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if (!is_array($this->loadInfos->require)) { |
208
|
|
|
$this->loadInfos->require = [$this->loadInfos->require]; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$this->loadInfos->require[] = $dependencyName; |
212
|
|
|
|
213
|
|
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get path to the runner file |
218
|
|
|
* |
219
|
|
|
* @return string |
220
|
|
|
* |
221
|
|
|
* @throws Exception If the file not exists |
222
|
|
|
*/ |
223
|
|
|
protected function getRunnerFile() |
224
|
|
|
{ |
225
|
|
|
$moduleInfos = $this->loadInfos; |
226
|
|
|
$runnerFile = ''; |
227
|
|
|
|
228
|
|
|
if (property_exists($moduleInfos, 'runner')) { |
229
|
|
|
$runnerFile = (string) $moduleInfos->runner; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if ($runnerFile === '') { |
233
|
|
|
return; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$runnerFile = MODULES_DIR.$this->pathName |
237
|
|
|
.'/'.$runnerFile |
238
|
|
|
; |
239
|
|
|
|
240
|
|
|
if (!file_exists($runnerFile)) { |
241
|
|
|
throw new Exception( |
242
|
|
|
'Runner file for module '.$this->pathName.' not found.' |
243
|
|
|
); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $runnerFile; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Run the module in a closure |
251
|
|
|
* |
252
|
|
|
* @return void |
253
|
|
|
*/ |
254
|
|
|
public function runModule() |
255
|
|
|
{ |
256
|
|
|
$runnerFile = $this->getRunnerFile(); |
257
|
|
|
$module = $this; |
258
|
|
|
|
259
|
|
|
$initFunction = function() use ($runnerFile, $module) { |
260
|
|
|
if ($runnerFile === null) { |
261
|
|
|
return; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
require(realpath($runnerFile)); |
265
|
|
|
}; |
266
|
|
|
|
267
|
|
|
$this->status->run = true; |
268
|
|
|
$initFunction(); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|