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