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{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* list of loaded module |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private static $list = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* logger instance |
37
|
|
|
* @var Log |
38
|
|
|
*/ |
39
|
|
|
private static $logger; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The signleton of the logger |
43
|
|
|
* @return Object the Log instance |
44
|
|
|
*/ |
45
|
|
|
private static function getLogger(){ |
46
|
|
|
if(self::$logger == null){ |
47
|
|
|
self::$logger[0] =& class_loader('Log', 'classes'); |
48
|
|
|
self::$logger[0]->setLogger('Library::Module'); |
49
|
|
|
} |
50
|
|
|
return self::$logger[0]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Initialise the module list by scanning the directory MODULE_PATH |
55
|
|
|
*/ |
56
|
|
|
public function init(){ |
57
|
|
|
$logger = self::getLogger(); |
58
|
|
|
$logger->debug('Check if the application contains the modules ...'); |
59
|
|
|
$moduleDir = opendir(MODULE_PATH); |
60
|
|
|
if(is_resource($moduleDir)){ |
61
|
|
|
while(($module = readdir($moduleDir)) !== false){ |
62
|
|
|
if(preg_match('/^([a-z0-9-_]+)$/i', $module) && is_dir(MODULE_PATH . $module)){ |
63
|
|
|
self::$list[] = $module; |
64
|
|
|
} |
65
|
|
|
else{ |
66
|
|
|
$logger->info('Skipping [' .$module. '], may be this is not a directory or does not exists or is invalid name'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
closedir($moduleDir); |
70
|
|
|
} |
71
|
|
|
ksort(self::$list); |
72
|
|
|
|
73
|
|
|
if(self::hasModule()){ |
74
|
|
|
$logger->info('The application contains the module below [' . implode(', ', self::getModuleList()) . ']'); |
75
|
|
|
} |
76
|
|
|
else{ |
77
|
|
|
$logger->info('The application contains no module skipping'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the list of the custom autoload configuration from module if exists |
83
|
|
|
* @return array|boolean the autoload configurations list or false if no module contains the autoload configuration values |
84
|
|
|
*/ |
85
|
|
|
public static function getModulesAutoloadConfig(){ |
86
|
|
|
$logger = self::getLogger(); |
87
|
|
|
if(! self::hasModule()){ |
88
|
|
|
$logger->info('No module was loaded skipping.'); |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
$autoloads = array(); |
92
|
|
|
$autoloads['libraries'] = array(); |
93
|
|
|
$autoloads['config'] = array(); |
94
|
|
|
$autoloads['models'] = array(); |
95
|
|
|
$autoloads['functions'] = array(); |
96
|
|
|
$autoloads['languages'] = array(); |
97
|
|
|
|
98
|
|
|
foreach (self::$list as $module) { |
99
|
|
|
$file = MODULE_PATH . $module . DS . 'config' . DS . 'autoload.php'; |
100
|
|
|
if(file_exists($file)){ |
101
|
|
|
$autoload = array(); |
102
|
|
|
require_once $file; |
103
|
|
|
if(! empty($autoload) && is_array($autoload)){ |
104
|
|
|
$autoloads = array_merge_recursive($autoloads, $autoload); |
105
|
|
|
unset($autoload); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
return $autoloads; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the list of the custom routes configuration from module if exists |
114
|
|
|
* @return array|boolean the routes list or false if no module contains the routes configuration |
115
|
|
|
*/ |
116
|
|
|
public static function getModulesRoutes(){ |
117
|
|
|
$logger = self::getLogger(); |
118
|
|
|
if(! self::hasModule()){ |
119
|
|
|
$logger->info('No module was loaded skipping.'); |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
$routes = array(); |
123
|
|
|
foreach (self::$list as $module) { |
124
|
|
|
$file = MODULE_PATH . $module . DS . 'config' . DS . 'routes.php'; |
125
|
|
|
if(file_exists($file)){ |
126
|
|
|
require_once $file; |
127
|
|
|
if(! empty($route) && is_array($route)){ |
|
|
|
|
128
|
|
|
$routes = array_merge($routes, $route); |
129
|
|
|
unset($route); |
130
|
|
|
} |
131
|
|
|
else{ |
132
|
|
|
show_error('No routing configuration found in [' .$file. '] for module [' . $module . ']'); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
return $routes; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Check if in module list can have this controller |
142
|
|
|
* @param string $class the controller class |
143
|
|
|
* @param string $module the module name |
144
|
|
|
* @return boolean|string false or null if no module have this controller, path the full path of the controller |
145
|
|
|
*/ |
146
|
|
|
public static function findControllerFullPath($class, $module = null){ |
147
|
|
|
$logger = self::getLogger(); |
148
|
|
|
if(! self::hasModule()){ |
149
|
|
|
$logger->info('No module was loaded skiping.'); |
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
$class = str_ireplace('.php', '', $class); |
153
|
|
|
$class = ucfirst($class); |
154
|
|
|
$classFile = $class.'.php'; |
155
|
|
|
$logger->debug('Checking the controller [' . $class . '] in module [' .$module. '] ...'); |
156
|
|
|
$filePath = MODULE_PATH . $module . DS . 'controllers' . DS . $classFile; |
157
|
|
|
if(file_exists($filePath)){ |
158
|
|
|
$logger->info('Found controller [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
159
|
|
|
return $filePath; |
160
|
|
|
} |
161
|
|
|
else{ |
162
|
|
|
$logger->info('Controller [' . $class . '] does not exist in the module [' .$module. ']'); |
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Check if in module list can have this model |
169
|
|
|
* @param string $class the model class |
170
|
|
|
* @param string $module the module name |
171
|
|
|
* @return boolean|string false or null if no module have this model, return the full path of this model |
172
|
|
|
*/ |
173
|
|
|
public static function findModelFullPath($class, $module = null){ |
174
|
|
|
$logger = self::getLogger(); |
175
|
|
|
if(! self::hasModule()){ |
176
|
|
|
$logger->info('No module was loaded skiping.'); |
177
|
|
|
return false; |
178
|
|
|
} |
179
|
|
|
$class = str_ireplace('.php', '', $class); |
180
|
|
|
$class = ucfirst($class); |
181
|
|
|
$classFile = $class.'.php'; |
182
|
|
|
$logger->debug('Checking model [' . $class . '] in module [' .$module. '] ...'); |
183
|
|
|
$filePath = MODULE_PATH . $module . DS . 'models' . DS . $classFile; |
184
|
|
|
if(file_exists($filePath)){ |
185
|
|
|
$logger->info('Found model [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
186
|
|
|
return $filePath; |
187
|
|
|
} |
188
|
|
|
else{ |
189
|
|
|
$logger->info('Model [' . $class . '] does not exist in the module [' .$module. ']'); |
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Check if in module list can have this config |
196
|
|
|
* @param string $configuration the config name |
197
|
|
|
* @param string $module the module name |
198
|
|
|
* @return boolean|string false or null if no module have this configuration, return the full path of this configuration |
199
|
|
|
*/ |
200
|
|
|
public static function findConfigFullPath($configuration, $module = null){ |
201
|
|
|
$logger = self::getLogger(); |
202
|
|
|
if(! self::hasModule()){ |
203
|
|
|
$logger->info('No module was loaded skiping.'); |
204
|
|
|
return false; |
205
|
|
|
} |
206
|
|
|
$configuration = str_ireplace('.php', '', $configuration); |
207
|
|
|
$file = $configuration.'.php'; |
208
|
|
|
$logger->debug('Checking configuration [' . $configuration . '] in module [' .$module. '] ...'); |
209
|
|
|
$filePath = MODULE_PATH . $module . DS . 'config' . DS . $file; |
210
|
|
|
if(file_exists($filePath)){ |
211
|
|
|
$logger->info('Found configuration [' . $configuration . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
212
|
|
|
return $filePath; |
213
|
|
|
} |
214
|
|
|
else{ |
215
|
|
|
$logger->info('Configuration [' . $configuration . '] does not exist in the module [' .$module. ']'); |
216
|
|
|
return false; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Check if in module list can have this helper |
222
|
|
|
* @param string $helper the helper name |
223
|
|
|
* @param string $module the module name |
224
|
|
|
* @return boolean|string false or null if no module have this helper, return the full path of this helper |
225
|
|
|
*/ |
226
|
|
|
public static function findFunctionFullPath($helper, $module = null){ |
227
|
|
|
$logger = self::getLogger(); |
228
|
|
|
if(! self::hasModule()){ |
229
|
|
|
$logger->info('No module was loaded skiping.'); |
230
|
|
|
return false; |
231
|
|
|
} |
232
|
|
|
$helper = str_ireplace('.php', '', $helper); |
233
|
|
|
$helper = str_ireplace('function_', '', $helper); |
234
|
|
|
$file = 'function_'.$helper.'.php'; |
235
|
|
|
$logger->debug('Checking helper [' . $helper . '] in module [' .$module. '] ...'); |
236
|
|
|
$filePath = MODULE_PATH . $module . DS . 'functions' . DS . $file; |
237
|
|
|
if(file_exists($filePath)){ |
238
|
|
|
$logger->info('Found helper [' . $helper . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
239
|
|
|
return $filePath; |
240
|
|
|
} |
241
|
|
|
else{ |
242
|
|
|
$logger->info('Helper [' . $helper . '] does not exist in the module [' .$module. ']'); |
243
|
|
|
return false; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Check if in module list can have this library |
250
|
|
|
* @param string $class the library name |
251
|
|
|
* @param string $module the module name |
252
|
|
|
* @return boolean|string false or null if no module have this library, return the full path of this library |
253
|
|
|
*/ |
254
|
|
|
public static function findLibraryFullPath($class, $module = null){ |
255
|
|
|
$logger = self::getLogger(); |
256
|
|
|
if(! self::hasModule()){ |
257
|
|
|
$logger->info('No module was loaded skiping.'); |
258
|
|
|
return false; |
259
|
|
|
} |
260
|
|
|
$class = str_ireplace('.php', '', $class); |
261
|
|
|
$file = $class.'.php'; |
262
|
|
|
$logger->debug('Checking library [' . $class . '] in module [' .$module. '] ...'); |
263
|
|
|
$filePath = MODULE_PATH . $module . DS . 'libraries' . DS . $file; |
264
|
|
|
if(file_exists($filePath)){ |
265
|
|
|
$logger->info('Found library [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
266
|
|
|
return $filePath; |
267
|
|
|
} |
268
|
|
|
else{ |
269
|
|
|
$logger->info('Library [' . $class . '] does not exist in the module [' .$module. ']'); |
270
|
|
|
return false; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Check if in module list can have this view |
277
|
|
|
* @param string $view the view path |
278
|
|
|
* @param string $module the module name to check |
279
|
|
|
* @return boolean|string false or null if no module have this view, path the full path of the view |
280
|
|
|
*/ |
281
|
|
|
public static function findViewFullPath($view, $module = null){ |
282
|
|
|
$logger = self::getLogger(); |
283
|
|
|
if(! self::hasModule()){ |
284
|
|
|
$logger->info('No module was loaded skiping.'); |
285
|
|
|
return false; |
286
|
|
|
} |
287
|
|
|
$view = str_ireplace('.php', '', $view); |
288
|
|
|
$view = trim($view, '/\\'); |
289
|
|
|
$view = str_ireplace('/', DS, $view); |
290
|
|
|
$viewFile = $view . '.php'; |
291
|
|
|
$logger->debug('Checking view [' . $view . '] in module [' .$module. '] ...'); |
292
|
|
|
$filePath = MODULE_PATH . $module . DS . 'views' . DS . $viewFile; |
293
|
|
|
if(file_exists($filePath)){ |
294
|
|
|
$logger->info('Found view [' . $view . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
295
|
|
|
return $filePath; |
296
|
|
|
} |
297
|
|
|
else{ |
298
|
|
|
$logger->info('View [' . $view . '] does not exist in the module [' .$module. ']'); |
299
|
|
|
return false; |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Check if in module list can have this language |
305
|
|
|
* @param string $language the language name |
306
|
|
|
* @param string $module the module name |
307
|
|
|
* @param string $appLang the application language like 'en', 'fr' |
308
|
|
|
* @return boolean|string false or null if no module have this language, return the full path of this language |
309
|
|
|
*/ |
310
|
|
|
public static function findLanguageFullPath($language, $module = null, $appLang){ |
311
|
|
|
$logger = self::getLogger(); |
312
|
|
|
if(! self::hasModule()){ |
313
|
|
|
$logger->info('No module was loaded skiping.'); |
314
|
|
|
return false; |
315
|
|
|
} |
316
|
|
|
$language = str_ireplace('.php', '', $language); |
317
|
|
|
$language = str_ireplace('lang_', '', $language); |
318
|
|
|
$file = 'lang_'.$language.'.php'; |
319
|
|
|
$logger->debug('Checking language [' . $language . '] in module [' .$module. '] ...'); |
320
|
|
|
$filePath = MODULE_PATH . $module . DS . 'lang' . DS . $appLang . DS . $file; |
321
|
|
|
if(file_exists($filePath)){ |
322
|
|
|
$logger->info('Found language [' . $language . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
323
|
|
|
return $filePath; |
324
|
|
|
} |
325
|
|
|
else{ |
326
|
|
|
$logger->info('Language [' . $language . '] does not exist in the module [' .$module. ']'); |
327
|
|
|
return false; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Get the list of module loaded |
333
|
|
|
* @return array the module list |
334
|
|
|
*/ |
335
|
|
|
public static function getModuleList(){ |
336
|
|
|
return self::$list; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Check if the application has an module |
341
|
|
|
* @return boolean |
342
|
|
|
*/ |
343
|
|
|
public static function hasModule(){ |
344
|
|
|
return !empty(self::$list); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
} |
348
|
|
|
|