|
1
|
|
|
<?php |
|
2
|
|
|
defined('ROOT_PATH') || exit('Access denied'); |
|
3
|
|
|
/** |
|
4
|
|
|
* TNH Framework |
|
5
|
|
|
* |
|
6
|
|
|
* A simple PHP framework using HMVC architecture |
|
7
|
|
|
* |
|
8
|
|
|
* This content is released under the GNU GPL License (GPL) |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (C) 2017 Tony NGUEREZA |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software; you can redistribute it and/or |
|
13
|
|
|
* modify it under the terms of the GNU General Public License |
|
14
|
|
|
* as published by the Free Software Foundation; either version 3 |
|
15
|
|
|
* of the License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU General Public License |
|
23
|
|
|
* along with this program; if not, write to the Free Software |
|
24
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
class Module extends BaseStaticClass { |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* list of loaded module |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private static $list = array(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Initialise the module list by scanning the directory MODULE_PATH |
|
37
|
|
|
*/ |
|
38
|
|
|
public function init() { |
|
39
|
|
|
$logger = self::getLogger(); |
|
40
|
|
|
$logger->debug('Check if the application contains the modules ...'); |
|
41
|
|
|
$moduleDir = opendir(MODULE_PATH); |
|
42
|
|
|
if (is_resource($moduleDir)) { |
|
43
|
|
|
while (($module = readdir($moduleDir)) !== false) { |
|
44
|
|
|
if (preg_match('/^([a-z0-9-_]+)$/i', $module) && is_dir(MODULE_PATH . $module)) { |
|
45
|
|
|
self::$list[] = $module; |
|
46
|
|
|
} else { |
|
47
|
|
|
$logger->info('Skipping [' . $module . '], may be this is not a directory or does not exists or is invalid name'); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
closedir($moduleDir); |
|
51
|
|
|
} |
|
52
|
|
|
ksort(self::$list); |
|
53
|
|
|
|
|
54
|
|
|
if (!empty(self::$list)) { |
|
55
|
|
|
$logger->info('The application contains the module below [' . implode(', ', self::getModuleList()) . ']'); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Add new module in the list |
|
62
|
|
|
* @param string $name the name of the module |
|
63
|
|
|
* |
|
64
|
|
|
* @return object the current instance |
|
65
|
|
|
*/ |
|
66
|
|
|
public function add($name) { |
|
67
|
|
|
$logger = self::getLogger(); |
|
68
|
|
|
if (in_array($name, self::$list)) { |
|
69
|
|
|
$logger->info('The module [' .$name. '] already added skipping.'); |
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
self::$list[] = $name; |
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Remove the module from list |
|
78
|
|
|
* @param string $name the module name |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function remove($name) { |
|
81
|
|
|
$logger = self::getLogger(); |
|
82
|
|
|
$logger->debug('Removing of the module [' . $name . '] ...'); |
|
83
|
|
|
if (false !== $index = array_search($name, self::$list, true)) { |
|
84
|
|
|
$logger->info('Found the module at index [' . $index . '] remove it'); |
|
85
|
|
|
unset(self::$list[$index]); |
|
86
|
|
|
} else { |
|
87
|
|
|
$logger->info('Cannot found this module in the list'); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Remove all the module. |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function removeAll() { |
|
95
|
|
|
$logger = self::getLogger(); |
|
96
|
|
|
$logger->debug('Removing of all module ...'); |
|
97
|
|
|
self::$list = array(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get the list of module loaded |
|
102
|
|
|
* @return array the module list |
|
103
|
|
|
*/ |
|
104
|
|
|
public static function getModuleList() { |
|
105
|
|
|
return self::$list; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Check if the application has an module |
|
110
|
|
|
* @return boolean |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function hasModule() { |
|
113
|
|
|
return !empty(self::$list); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get the list of the custom autoload configuration from module if exists |
|
118
|
|
|
* @return array|boolean the autoload configurations list or false if no module contains the autoload configuration values |
|
119
|
|
|
*/ |
|
120
|
|
|
public static function getModulesAutoloadConfig() { |
|
121
|
|
|
$logger = self::getLogger(); |
|
122
|
|
|
if (empty(self::$list)) { |
|
123
|
|
|
$logger->info('No module was loaded skipping.'); |
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
$autoloads = array(); |
|
127
|
|
|
$autoloads['libraries'] = array(); |
|
128
|
|
|
$autoloads['config'] = array(); |
|
129
|
|
|
$autoloads['models'] = array(); |
|
130
|
|
|
$autoloads['functions'] = array(); |
|
131
|
|
|
$autoloads['languages'] = array(); |
|
132
|
|
|
|
|
133
|
|
|
foreach (self::$list as $module) { |
|
134
|
|
|
$file = MODULE_PATH . $module . DS . 'config' . DS . 'autoload.php'; |
|
135
|
|
|
if (file_exists($file)) { |
|
136
|
|
|
$autoload = array(); |
|
137
|
|
|
require_once $file; |
|
138
|
|
|
if (!empty($autoload) && is_array($autoload)) { |
|
139
|
|
|
$autoloads = array_merge_recursive($autoloads, $autoload); |
|
140
|
|
|
unset($autoload); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
return $autoloads; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get the list of the custom routes configuration from module if exists |
|
149
|
|
|
* @return array|boolean the routes list or false if no module contains the routes configuration |
|
150
|
|
|
*/ |
|
151
|
|
|
public static function getModulesRoutesConfig() { |
|
152
|
|
|
$logger = self::getLogger(); |
|
153
|
|
|
if (empty(self::$list)) { |
|
154
|
|
|
$logger->info('No module was loaded skipping.'); |
|
155
|
|
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
$routes = array(); |
|
158
|
|
|
foreach (self::$list as $module) { |
|
159
|
|
|
$file = MODULE_PATH . $module . DS . 'config' . DS . 'routes.php'; |
|
160
|
|
|
if (file_exists($file)) { |
|
161
|
|
|
$route = array(); |
|
162
|
|
|
require_once $file; |
|
163
|
|
|
if (!empty($route) && is_array($route)) { |
|
164
|
|
|
$routes = array_merge($routes, $route); |
|
165
|
|
|
unset($route); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
return $routes; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Check if in module list can have this controller |
|
175
|
|
|
* @see Module::findClassInModuleFullFilePath |
|
176
|
|
|
* @return boolean|string false or null if no module have this controller, path the full path of the controller |
|
177
|
|
|
*/ |
|
178
|
|
|
public static function findControllerFullPath($class, $module = null) { |
|
179
|
|
|
return self::findClassInModuleFullFilePath($class, $module, 'controllers'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Check if in module list can have this model |
|
184
|
|
|
* @see Module::findClassInModuleFullFilePath |
|
185
|
|
|
* @return boolean|string false or null if no module have this model, return the full path of this model |
|
186
|
|
|
*/ |
|
187
|
|
|
public static function findModelFullPath($class, $module = null) { |
|
188
|
|
|
return self::findClassInModuleFullFilePath($class, $module, 'models'); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Check if in module list can have this library |
|
193
|
|
|
* @see Module::findClassInModuleFullFilePath |
|
194
|
|
|
* @return boolean|string false or null if no module have this library, return the full path of this library |
|
195
|
|
|
*/ |
|
196
|
|
|
public static function findLibraryFullPath($class, $module = null) { |
|
197
|
|
|
return self::findClassInModuleFullFilePath($class, $module, 'libraries'); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Check if in module list can have this config |
|
203
|
|
|
* @see Module::findNonClassInModuleFullFilePath |
|
204
|
|
|
* @return boolean|string false or null if no module have this configuration, return the full path of this configuration |
|
205
|
|
|
*/ |
|
206
|
|
|
public static function findConfigFullPath($configuration, $module = null) { |
|
207
|
|
|
return self::findNonClassInModuleFullFilePath($configuration, $module, 'config'); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Check if in module list can have this helper |
|
212
|
|
|
* @see Module::findNonClassInModuleFullFilePath |
|
213
|
|
|
* @return boolean|string false or null if no module have this helper, return the full path of this helper |
|
214
|
|
|
*/ |
|
215
|
|
|
public static function findFunctionFullPath($helper, $module = null) { |
|
216
|
|
|
return self::findNonClassInModuleFullFilePath($helper, $module, 'functions'); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Check if in module list can have this view |
|
221
|
|
|
* @see Module::findNonClassInModuleFullFilePath |
|
222
|
|
|
* @return boolean|string false or null if no module have this view, path the full path of the view |
|
223
|
|
|
*/ |
|
224
|
|
|
public static function findViewFullPath($view, $module = null) { |
|
225
|
|
|
return self::findNonClassInModuleFullFilePath($view, $module, 'views'); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Check if in module list can have this language |
|
230
|
|
|
* @see Module::findNonClassInModuleFullFilePath |
|
231
|
|
|
* @return boolean|string false or null if no module have this language, return the full path of this language |
|
232
|
|
|
*/ |
|
233
|
|
|
public static function findLanguageFullPath($language, $appLang, $module = null) { |
|
234
|
|
|
return self::findNonClassInModuleFullFilePath($language, $module, 'lang', $appLang); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Check if in module list can have the model, controller, library |
|
239
|
|
|
* @param string $class the class name of library, model, controller |
|
240
|
|
|
* @param string $module the module name |
|
241
|
|
|
* @param string $type the name of the type "controllers", "libraries", "models" |
|
242
|
|
|
* @return boolean|string false or null if no module |
|
243
|
|
|
* have this class, return the full path of the class |
|
244
|
|
|
*/ |
|
245
|
|
|
protected static function findClassInModuleFullFilePath($class, $module, $type) { |
|
246
|
|
|
$logger = self::getLogger(); |
|
247
|
|
|
$class = str_ireplace('.php', '', $class); |
|
248
|
|
|
$class = ucfirst($class); |
|
249
|
|
|
$classFile = $class . '.php'; |
|
250
|
|
|
$logger->debug('Checking the class [' . $class . '] in module [' . $module . '] for [' . $type . '] ...'); |
|
251
|
|
|
$filePath = MODULE_PATH . $module . DS . $type . DS . $classFile; |
|
252
|
|
|
if (file_exists($filePath)) { |
|
253
|
|
|
$logger->info('Found class [' . $class . '] in module [' . $module . '] for [' . $type . '] the file path is [' . $filePath . ']'); |
|
254
|
|
|
return $filePath; |
|
255
|
|
|
} |
|
256
|
|
|
$logger->info('Class [' . $class . '] does not exist in the module [' . $module . '] for [' . $type . ']'); |
|
257
|
|
|
return false; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Check if in module list can have the config, view, helper, language |
|
262
|
|
|
* @param string $name the name of config, view, helper, language |
|
263
|
|
|
* @param string $module the module name |
|
264
|
|
|
* @param string $type the name of the type "config", "functions", "views", "lang" |
|
265
|
|
|
* @param string|null $appLang the application language. This is use only when $type = "lang" |
|
266
|
|
|
* @return boolean|string false or null if no module |
|
267
|
|
|
* have this resource, return the full path of the resource |
|
268
|
|
|
*/ |
|
269
|
|
|
protected static function findNonClassInModuleFullFilePath($name, $module, $type, $appLang = null) { |
|
270
|
|
|
$logger = self::getLogger(); |
|
271
|
|
|
$name = str_ireplace('.php', '', $name); |
|
272
|
|
|
$file = $name . '.php'; |
|
273
|
|
|
$filePath = MODULE_PATH . $module . DS . $type . DS . $file; |
|
274
|
|
|
switch ($type) { |
|
275
|
|
|
case 'functions': |
|
276
|
|
|
$name = str_ireplace('function_', '', $name); |
|
277
|
|
|
$file = 'function_' . $name . '.php'; |
|
278
|
|
|
$filePath = MODULE_PATH . $module . DS . $type . DS . $file; |
|
279
|
|
|
break; |
|
280
|
|
|
case 'views': |
|
281
|
|
|
$name = trim($name, '/\\'); |
|
282
|
|
|
$name = str_ireplace('/', DS, $name); |
|
283
|
|
|
$file = $name . '.php'; |
|
284
|
|
|
$filePath = MODULE_PATH . $module . DS . $type . DS . $file; |
|
285
|
|
|
break; |
|
286
|
|
|
case 'lang': |
|
287
|
|
|
$name = str_ireplace('lang_', '', $name); |
|
288
|
|
|
$file = 'lang_' . $name . '.php'; |
|
289
|
|
|
$filePath = MODULE_PATH . $module . DS . $type . DS . $appLang . DS . $file; |
|
290
|
|
|
break; |
|
291
|
|
|
} |
|
292
|
|
|
$logger->debug('Checking resource [' . $name . '] in module [' . $module . '] for [' . $type . '] ...'); |
|
293
|
|
|
if (file_exists($filePath)) { |
|
294
|
|
|
$logger->info('Found resource [' . $name . '] in module [' . $module . '] for [' . $type . '] the file path is [' . $filePath . ']'); |
|
295
|
|
|
return $filePath; |
|
296
|
|
|
} |
|
297
|
|
|
$logger->info('Resource [' . $name . '] does not exist in the module [' . $module . '] for [' . $type . ']'); |
|
298
|
|
|
return false; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
} |
|
302
|
|
|
|