1 | <?php |
||
2 | |||
3 | namespace BFW\Install\ModuleManager; |
||
4 | |||
5 | use Exception; |
||
6 | use BFW\Install\ModuleManager as Manager; |
||
7 | use BFW\Install\ReadDirLoadModule; |
||
8 | use bultonFr\Utils\Cli\BasicMsg; |
||
9 | |||
10 | class Actions |
||
11 | { |
||
12 | /** |
||
13 | * @const EXCEP_MOD_NOT_FOUND Exception code if the asked module has not |
||
14 | * been found. |
||
15 | */ |
||
16 | const EXCEP_MOD_NOT_FOUND = 1701001; |
||
17 | |||
18 | /** |
||
19 | * The manager which have instancied this class |
||
20 | * |
||
21 | * @var \BFW\Install\ModuleManager |
||
22 | */ |
||
23 | protected $manager; |
||
24 | |||
25 | /** |
||
26 | * List of all path for all modules found. |
||
27 | * The key is the module name, the value the absolute path. |
||
28 | * |
||
29 | * @var string[string] $modulePathList |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
30 | */ |
||
31 | protected $modulePathList = []; |
||
32 | |||
33 | /** |
||
34 | * List of all module found. |
||
35 | * The key is the module name, the value the instance of |
||
36 | * \BFW\Install\ModuleManager\Module for the module. |
||
37 | * |
||
38 | * @var \BFW\Install\ModuleManager\Module[string] $moduleList |
||
0 ignored issues
–
show
|
|||
39 | */ |
||
40 | protected $moduleList = []; |
||
41 | |||
42 | /** |
||
43 | * Constructor |
||
44 | * |
||
45 | * @param Manager $manager |
||
46 | */ |
||
47 | public function __construct(Manager $manager) |
||
48 | { |
||
49 | $this->manager = $manager; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Get the value of $manager |
||
54 | * |
||
55 | * @return \BFW\Install\ModuleManager |
||
56 | */ |
||
57 | public function getManager(): Manager |
||
58 | { |
||
59 | return $this->manager; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Get the value of $modulePathList |
||
64 | * |
||
65 | * @return string[string] |
||
0 ignored issues
–
show
|
|||
66 | */ |
||
67 | public function getModulePathList(): array |
||
68 | { |
||
69 | return $this->modulePathList; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Get the value of $moduleList |
||
74 | * |
||
75 | * @return \BFW\Install\ModuleManager\Module[string] |
||
0 ignored issues
–
show
|
|||
76 | */ |
||
77 | public function getModuleList(): array |
||
78 | { |
||
79 | return $this->moduleList; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Call the method dedicated to the action. |
||
84 | * The action is obtain from Manager class. |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | public function doAction() |
||
89 | { |
||
90 | $actionName = $this->manager->getAction(); |
||
91 | |||
92 | if ($actionName === 'add') { |
||
93 | return $this->doAdd(); |
||
0 ignored issues
–
show
Are you sure the usage of
$this->doAdd() targeting BFW\Install\ModuleManager\Actions::doAdd() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
94 | } elseif ($actionName === 'enable') { |
||
95 | return $this->doEnable(); |
||
0 ignored issues
–
show
Are you sure the usage of
$this->doEnable() targeting BFW\Install\ModuleManager\Actions::doEnable() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
96 | } elseif ($actionName === 'disable') { |
||
97 | return $this->doDisable(); |
||
0 ignored issues
–
show
Are you sure the usage of
$this->doDisable() targeting BFW\Install\ModuleManager\Actions::doDisable() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
98 | } elseif ($actionName === 'delete') { |
||
99 | return $this->doDelete(); |
||
0 ignored issues
–
show
Are you sure the usage of
$this->doDelete() targeting BFW\Install\ModuleManager\Actions::doDelete() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
100 | } |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Run actions to do to prepare and execute add of modules |
||
105 | * Execute the deleting action if the reinstall option has been declared. |
||
106 | * |
||
107 | * @return void |
||
108 | */ |
||
109 | protected function doAdd() |
||
110 | { |
||
111 | if ($this->manager->getReinstall() === true) { |
||
112 | $this->doDelete(); |
||
113 | } |
||
114 | |||
115 | $app = \BFW\Install\Application::getInstance(); |
||
116 | $vendorPath = $app->getOptions()->getValue('vendorDir'); |
||
117 | |||
118 | $this->obtainModulePathList($vendorPath); |
||
119 | $this->executeForModules('doAdd', 'Add'); |
||
120 | |||
121 | foreach ($this->moduleList as $module) { |
||
122 | $this->runInstallScript($module); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Run actions to do to prepare and execute enabling of modules |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | protected function doEnable() |
||
132 | { |
||
133 | $this->obtainModulePathList(MODULES_AVAILABLE_DIR); |
||
0 ignored issues
–
show
|
|||
134 | $this->executeForModules('doEnable', 'Enable'); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Run actions to do to prepare and execute disabling of modules |
||
139 | * |
||
140 | * @return void |
||
141 | */ |
||
142 | protected function doDisable() |
||
143 | { |
||
144 | $this->obtainModulePathList(MODULES_AVAILABLE_DIR); |
||
0 ignored issues
–
show
|
|||
145 | $this->executeForModules('doDisable', 'Disable'); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Run actions to do to prepare and execute deleting of modules |
||
150 | * |
||
151 | * @return void |
||
152 | */ |
||
153 | protected function doDelete() |
||
154 | { |
||
155 | $this->obtainModulePathList(MODULES_AVAILABLE_DIR); |
||
0 ignored issues
–
show
|
|||
156 | $this->executeForModules('doDelete', 'Delete'); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * obtain all modules present in a directory and add each module to |
||
161 | * the property $modulePathList. |
||
162 | * |
||
163 | * @param string $dirPath |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | protected function obtainModulePathList(string $dirPath) |
||
168 | { |
||
169 | $listModules = $this->searchAllModulesInDir($dirPath); |
||
0 ignored issues
–
show
Are you sure the assignment to
$listModules is correct as $this->searchAllModulesInDir($dirPath) targeting BFW\Install\ModuleManage...searchAllModulesInDir() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
170 | |||
171 | foreach ($listModules as $modulePath) { |
||
0 ignored issues
–
show
|
|||
172 | $pathExploded = explode('/', $modulePath); |
||
173 | $moduleName = $pathExploded[(count($pathExploded) - 1)]; |
||
174 | |||
175 | $this->modulePathList[$moduleName] = $modulePath; |
||
176 | } |
||
177 | |||
178 | ksort($this->modulePathList); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Search all modules present in a directory. |
||
183 | * |
||
184 | * @param string $dirPath |
||
185 | * |
||
186 | * @return void |
||
187 | */ |
||
188 | protected function searchAllModulesInDir(string $dirPath) |
||
189 | { |
||
190 | $listModules = []; |
||
191 | $readDir = new ReadDirLoadModule($listModules); |
||
192 | $readDir->run($dirPath); |
||
193 | |||
194 | return $listModules; |
||
0 ignored issues
–
show
|
|||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Call the method actionOnModule for each module find, or only for the |
||
199 | * module specified on the Manager. |
||
200 | * |
||
201 | * @param string $actionMethodName The method to call in Module class |
||
202 | * @param string $actionName The name of the action (used by log) |
||
203 | * |
||
204 | * @return void |
||
205 | */ |
||
206 | protected function executeForModules( |
||
207 | string $actionMethodName, |
||
208 | string $actionName |
||
209 | ) { |
||
210 | $specificModule = $this->manager->getSpecificModule(); |
||
211 | if (empty($specificModule) === false) { |
||
212 | $this->actionOnModule( |
||
213 | $specificModule, |
||
214 | '', |
||
215 | $actionMethodName, |
||
216 | $actionName |
||
217 | ); |
||
218 | |||
219 | return; |
||
220 | } |
||
221 | |||
222 | foreach ($this->modulePathList as $moduleName => $modulePath) { |
||
223 | $this->actionOnModule( |
||
224 | $moduleName, |
||
225 | $modulePath, |
||
226 | $actionMethodName, |
||
227 | $actionName |
||
228 | ); |
||
229 | } |
||
230 | |||
231 | ksort($this->moduleList); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Instanciate Module class dedicated for $moduleName, and |
||
236 | * call $actionMethodName into the Module class to run the action for this |
||
237 | * module. |
||
238 | * |
||
239 | * Some output for shell are displayed from here. |
||
240 | * |
||
241 | * @param string $moduleName The module name |
||
242 | * @param string $modulePath The module path |
||
243 | * @param string $actionMethodName The method to call in Module class |
||
244 | * @param string $actionName The name of the action (used by log) |
||
245 | * |
||
246 | * @return void |
||
247 | */ |
||
248 | protected function actionOnModule( |
||
249 | string $moduleName, |
||
250 | string $modulePath, |
||
251 | string $actionMethodName, |
||
252 | string $actionName |
||
253 | ) { |
||
254 | BasicMsg::displayMsg('> '.$actionName.' module '.$moduleName.' ... ', 'yellow'); |
||
255 | |||
256 | if (empty($modulePath)) { |
||
257 | if (!isset($this->modulePathList[$moduleName])) { |
||
258 | throw new Exception( |
||
259 | 'The module '.$moduleName.' has not been found in the directory', |
||
260 | static::EXCEP_MOD_NOT_FOUND |
||
261 | ); |
||
262 | } |
||
263 | |||
264 | $modulePath = $this->modulePathList[$moduleName]; |
||
265 | } |
||
266 | |||
267 | try { |
||
268 | $module = $this->obtainModule($moduleName); |
||
269 | $module->setVendorPath($modulePath); |
||
270 | $module->{$actionMethodName}(); |
||
271 | } catch (Exception $e) { |
||
272 | BasicMsg::displayMsgNL( |
||
273 | 'ERROR #'.$e->getCode().' : '.$e->getMessage(), |
||
274 | 'red', |
||
275 | 'bold' |
||
276 | ); |
||
277 | |||
278 | return; |
||
279 | } |
||
280 | |||
281 | $this->moduleList[$moduleName] = $module; |
||
282 | |||
283 | BasicMsg::displayMsgNL('Done', 'green'); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Instanciate the Module class for the module $moduleName |
||
288 | * |
||
289 | * @param string $moduleName The module's name |
||
290 | * |
||
291 | * @return \BFW\Install\ModuleManager\Module |
||
292 | */ |
||
293 | protected function obtainModule(string $moduleName): Module |
||
294 | { |
||
295 | return new Module($moduleName); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Check if there are an install script for $module, and call the method |
||
300 | * to run it if there is one. |
||
301 | * |
||
302 | * Some output for shell are displayed from here. |
||
303 | * |
||
304 | * @param \BFW\Install\ModuleManager\Module $module |
||
305 | * |
||
306 | * @return void |
||
307 | */ |
||
308 | protected function runInstallScript(Module $module) |
||
309 | { |
||
310 | BasicMsg::displayMsg( |
||
311 | '> Execute install script for '.$module->getName().' ... ', |
||
312 | 'yellow' |
||
313 | ); |
||
314 | |||
315 | if ($module->hasInstallScript() === false) { |
||
316 | BasicMsg::displayMsgNL('No script, pass.', 'yellow'); |
||
317 | return; |
||
318 | } |
||
319 | |||
320 | try { |
||
321 | $module->runInstallScript(); |
||
322 | } catch (Exception $e) { |
||
323 | BasicMsg::displayMsgNL( |
||
324 | 'ERROR #'.$e->getCode().' : '.$e->getMessage(), |
||
325 | 'red', |
||
326 | 'bold' |
||
327 | ); |
||
328 | |||
329 | return; |
||
330 | } |
||
331 | |||
332 | BasicMsg::displayMsgNL('Done', 'green'); |
||
333 | } |
||
334 | } |
||
335 |