|
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 MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (c) 2017 TNH Framework |
|
11
|
|
|
* |
|
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
14
|
|
|
* in the Software without restriction, including without limitation the rights |
|
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
17
|
|
|
* furnished to do so, subject to the following conditions: |
|
18
|
|
|
* |
|
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
20
|
|
|
* copies or substantial portions of the Software. |
|
21
|
|
|
* |
|
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
28
|
|
|
* SOFTWARE. |
|
29
|
|
|
*/ |
|
30
|
|
|
class Loader extends BaseClass { |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* List of loaded resources |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $loaded = array(); |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
public function __construct() { |
|
40
|
|
|
parent::__construct(); |
|
41
|
|
|
//add the resources already loaded during application bootstrap |
|
42
|
|
|
//in the list to prevent duplicate or loading the resources again. |
|
43
|
|
|
$this->loaded = class_loaded(); |
|
44
|
|
|
|
|
45
|
|
|
//Load resources from autoload configuration |
|
46
|
|
|
$this->loadResourcesFromAutoloadConfig(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Load the model class |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $class the class name to be loaded |
|
54
|
|
|
* @param string $instance the name of the instance to use in super object |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
|
public function model($class, $instance = null) { |
|
59
|
|
|
$class = str_ireplace('.php', '', $class); |
|
60
|
|
|
$class = trim($class, '/\\'); |
|
61
|
|
|
$file = ucfirst($class) . '.php'; |
|
62
|
|
|
$this->logger->debug('Loading model [' . $class . '] ...'); |
|
63
|
|
|
//************ |
|
64
|
|
|
if (!$instance) { |
|
65
|
|
|
$instance = $this->getModelLibraryInstanceName($class); |
|
66
|
|
|
} |
|
67
|
|
|
//**************** |
|
68
|
|
|
if (isset($this->loaded[$instance])) { |
|
69
|
|
|
$this->logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
$classFilePath = APPS_MODEL_PATH . $file; |
|
73
|
|
|
//first check if this model is in the module |
|
74
|
|
|
$this->logger->debug('Checking model [' . $class . '] from module list ...'); |
|
75
|
|
|
//check if the request class contains module name |
|
76
|
|
|
$moduleInfo = $this->getModuleInfoForModelLibrary($class); |
|
77
|
|
|
$module = $moduleInfo['module']; |
|
78
|
|
|
$class = $moduleInfo['class']; |
|
79
|
|
|
|
|
80
|
|
|
$moduleModelFilePath = get_instance()->module->findModelFullPath($class, $module); |
|
81
|
|
|
if ($moduleModelFilePath) { |
|
82
|
|
|
$this->logger->info('Found model [' . $class . '] from module [' . $module . '], ' |
|
83
|
|
|
. 'the file path is [' . $moduleModelFilePath . '] we will used it'); |
|
84
|
|
|
$classFilePath = $moduleModelFilePath; |
|
85
|
|
|
} else { |
|
86
|
|
|
$this->logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
|
87
|
|
|
} |
|
88
|
|
|
$this->logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
|
89
|
|
|
if (file_exists($classFilePath)) { |
|
90
|
|
|
require_once $classFilePath; |
|
91
|
|
|
if (class_exists($class)) { |
|
92
|
|
|
$cls = new $class(); |
|
93
|
|
|
$obj = & get_instance(); |
|
94
|
|
|
$obj->{$instance} = $cls; |
|
95
|
|
|
$this->loaded[$instance] = $class; |
|
96
|
|
|
$this->logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
|
97
|
|
|
} else { |
|
98
|
|
|
show_error('The file ' . $classFilePath . ' exists but does not contain the class [' . $class . ']'); |
|
99
|
|
|
} |
|
100
|
|
|
} else { |
|
101
|
|
|
show_error('Unable to find the model [' . $class . ']'); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Load the library class |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $class the library class name to be loaded |
|
110
|
|
|
* @param string $instance the instance name to use in super object |
|
111
|
|
|
* @param mixed $params the arguments to pass to the constructor |
|
112
|
|
|
* |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
|
|
public function library($class, $instance = null, array $params = array()) { |
|
116
|
|
|
$class = str_ireplace('.php', '', $class); |
|
117
|
|
|
$class = trim($class, '/\\'); |
|
118
|
|
|
$file = ucfirst($class) . '.php'; |
|
119
|
|
|
$this->logger->debug('Loading library [' . $class . '] ...'); |
|
120
|
|
|
if (!$instance) { |
|
121
|
|
|
$instance = $this->getModelLibraryInstanceName($class); |
|
122
|
|
|
} |
|
123
|
|
|
if (isset($this->loaded[$instance])) { |
|
124
|
|
|
$this->logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
125
|
|
|
return; |
|
126
|
|
|
} |
|
127
|
|
|
//Check and load Database library |
|
128
|
|
|
if (strtolower($class) == 'database') { |
|
129
|
|
|
$this->logger->info('This is the Database library ...'); |
|
130
|
|
|
$this->loadDatabase(); |
|
131
|
|
|
return; |
|
132
|
|
|
} |
|
133
|
|
|
$libraryFilePath = null; |
|
134
|
|
|
$this->logger->debug('Check if this is a system library ...'); |
|
135
|
|
|
if (file_exists(CORE_LIBRARY_PATH . $file)) { |
|
136
|
|
|
$libraryFilePath = CORE_LIBRARY_PATH . $file; |
|
137
|
|
|
$class = ucfirst($class); |
|
138
|
|
|
$this->logger->info('This library is a system library'); |
|
139
|
|
|
} else { |
|
140
|
|
|
$this->logger->info('This library is not a system library'); |
|
141
|
|
|
//first check if this library is in the module |
|
142
|
|
|
$info = $this->getLibraryPathUsingModuleInfo($class); |
|
143
|
|
|
$class = $info['class']; |
|
144
|
|
|
$libraryFilePath = $info['path']; |
|
145
|
|
|
} |
|
146
|
|
|
if (!$libraryFilePath && file_exists(LIBRARY_PATH . $file)) { |
|
147
|
|
|
$libraryFilePath = LIBRARY_PATH . $file; |
|
148
|
|
|
} |
|
149
|
|
|
$this->logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
|
150
|
|
|
$this->loadLibrary($libraryFilePath, $class, $instance, $params); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Load the helper |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $function the helper name to be loaded |
|
157
|
|
|
* |
|
158
|
|
|
* @return void |
|
159
|
|
|
*/ |
|
160
|
|
|
public function functions($function) { |
|
161
|
|
|
$function = str_ireplace('.php', '', $function); |
|
162
|
|
|
$function = trim($function, '/\\'); |
|
163
|
|
|
$function = str_ireplace('function_', '', $function); |
|
164
|
|
|
$file = 'function_' . $function . '.php'; |
|
165
|
|
|
$this->logger->debug('Loading helper [' . $function . '] ...'); |
|
166
|
|
|
if (isset($this->loaded['function_' . $function])) { |
|
167
|
|
|
$this->logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
|
168
|
|
|
return; |
|
169
|
|
|
} |
|
170
|
|
|
$functionFilePath = null; |
|
171
|
|
|
//first check if this helper is in the module |
|
172
|
|
|
$this->logger->debug('Checking helper [' . $function . '] from module list ...'); |
|
173
|
|
|
$moduleInfo = $this->getModuleInfoForFunctionLanguage($function, 'function'); |
|
174
|
|
|
$module = $moduleInfo['module']; |
|
175
|
|
|
$function = $moduleInfo['name']; |
|
176
|
|
|
if (!empty($moduleInfo['file'])) { |
|
177
|
|
|
$file = $moduleInfo['file']; |
|
178
|
|
|
} |
|
179
|
|
|
$moduleFunctionPath = get_instance()->module->findFunctionFullPath($function, $module); |
|
180
|
|
|
if ($moduleFunctionPath) { |
|
181
|
|
|
$this->logger->info('Found helper [' . $function . '] from module [' . $module . '], ' |
|
182
|
|
|
. 'the file path is [' . $moduleFunctionPath . '] we will used it'); |
|
183
|
|
|
$functionFilePath = $moduleFunctionPath; |
|
184
|
|
|
} else { |
|
185
|
|
|
$this->logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
|
186
|
|
|
} |
|
187
|
|
|
if (!$functionFilePath) { |
|
188
|
|
|
$functionFilePath = $this->getDefaultFilePathForFunctionLanguage($file, 'function'); |
|
189
|
|
|
} |
|
190
|
|
|
$this->logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
|
191
|
|
|
if ($functionFilePath) { |
|
192
|
|
|
require_once $functionFilePath; |
|
193
|
|
|
$this->loaded['function_' . $function] = $functionFilePath; |
|
194
|
|
|
$this->logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
|
195
|
|
|
} else { |
|
196
|
|
|
show_error('Unable to find helper file [' . $file . ']'); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Load the configuration file |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $filename the configuration filename located at CONFIG_PATH or MODULE_PATH/config |
|
204
|
|
|
* |
|
205
|
|
|
* @return void |
|
206
|
|
|
*/ |
|
207
|
|
|
public function config($filename) { |
|
208
|
|
|
$filename = str_ireplace('.php', '', $filename); |
|
209
|
|
|
$filename = trim($filename, '/\\'); |
|
210
|
|
|
$filename = str_ireplace('config_', '', $filename); |
|
211
|
|
|
$file = 'config_' . $filename . '.php'; |
|
212
|
|
|
$this->logger->debug('Loading configuration [' . $filename . '] ...'); |
|
213
|
|
|
$configFilePath = CONFIG_PATH . $file; |
|
214
|
|
|
//first check if this config is in the module |
|
215
|
|
|
$this->logger->debug('Checking config [' . $filename . '] from module list ...'); |
|
216
|
|
|
$moduleInfo = $this->getModuleInfoForConfig($filename); |
|
217
|
|
|
$module = $moduleInfo['module']; |
|
218
|
|
|
$filename = $moduleInfo['filename']; |
|
219
|
|
|
$moduleConfigPath = get_instance()->module->findConfigFullPath($filename, $module); |
|
220
|
|
|
if ($moduleConfigPath) { |
|
221
|
|
|
$this->logger->info('Found config [' . $filename . '] from module [' . $module . '], ' |
|
222
|
|
|
. 'the file path is [' . $moduleConfigPath . '] we will used it'); |
|
223
|
|
|
$configFilePath = $moduleConfigPath; |
|
224
|
|
|
} else { |
|
225
|
|
|
$this->logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
|
226
|
|
|
} |
|
227
|
|
|
$this->logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
|
228
|
|
|
$config = array(); |
|
229
|
|
|
if (file_exists($configFilePath)) { |
|
230
|
|
|
//note need use require instead of require_once |
|
231
|
|
|
require $configFilePath; |
|
232
|
|
|
if (!empty($config) && is_array($config)) { |
|
233
|
|
|
get_instance()->config->setAll($config); |
|
234
|
|
|
$this->logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
|
235
|
|
|
$this->logger->info('The custom application configuration loaded are listed below: ' . stringify_vars($config)); |
|
236
|
|
|
unset($config); |
|
237
|
|
|
} |
|
238
|
|
|
} else { |
|
239
|
|
|
show_error('Unable to find config file [' . $configFilePath . ']'); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Load the language |
|
246
|
|
|
* |
|
247
|
|
|
* @param string $language the language name to be loaded |
|
248
|
|
|
* @param string $langCode the language code to use, if null will use app current language |
|
249
|
|
|
* |
|
250
|
|
|
* @return void |
|
251
|
|
|
*/ |
|
252
|
|
|
public function lang($language, $langCode = null) { |
|
253
|
|
|
$language = str_ireplace('.php', '', $language); |
|
254
|
|
|
$language = trim($language, '/\\'); |
|
255
|
|
|
$language = str_ireplace('lang_', '', $language); |
|
256
|
|
|
$file = 'lang_' . $language . '.php'; |
|
257
|
|
|
if(!$langCode){ |
|
258
|
|
|
//get the current language |
|
259
|
|
|
$langCode = $this->getAppLang(); |
|
260
|
|
|
} |
|
261
|
|
|
$this->logger->debug('Loading language [' . $language . '] ...'); |
|
262
|
|
|
if (isset($this->loaded['lang_' . $langCode . '_' . $language])) { |
|
263
|
|
|
$this->logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
|
264
|
|
|
return; |
|
265
|
|
|
} |
|
266
|
|
|
$languageFilePath = null; |
|
267
|
|
|
//first check if this language is in the module |
|
268
|
|
|
$this->logger->debug('Checking language [' . $language . '] from module list ...'); |
|
269
|
|
|
$moduleInfo = $this->getModuleInfoForFunctionLanguage($language, 'lang'); |
|
270
|
|
|
$module = $moduleInfo['module']; |
|
271
|
|
|
$language = $moduleInfo['name']; |
|
272
|
|
|
if (!empty($moduleInfo['file'])) { |
|
273
|
|
|
$file = $moduleInfo['file']; |
|
274
|
|
|
} |
|
275
|
|
|
$moduleLanguagePath = get_instance()->module->findLanguageFullPath($language, $langCode, $module); |
|
276
|
|
|
if ($moduleLanguagePath) { |
|
277
|
|
|
$this->logger->info('Found language [' . $language . '] from module [' . $module . '], ' |
|
278
|
|
|
. 'the file path is [' . $moduleLanguagePath . '] we will used it'); |
|
279
|
|
|
$languageFilePath = $moduleLanguagePath; |
|
280
|
|
|
} else { |
|
281
|
|
|
$this->logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
|
282
|
|
|
} |
|
283
|
|
|
if (!$languageFilePath) { |
|
284
|
|
|
$languageFilePath = $this->getDefaultFilePathForFunctionLanguage($file, 'language', $langCode); |
|
285
|
|
|
} |
|
286
|
|
|
$this->logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
|
287
|
|
|
$this->loadLanguage($languageFilePath, $language, $langCode); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Load the library database with dependencies |
|
292
|
|
|
*/ |
|
293
|
|
|
protected function loadDatabase() { |
|
294
|
|
|
$connection = &class_loader('DatabaseConnection', 'classes/database'); |
|
295
|
|
|
$connection->connect(); |
|
296
|
|
|
|
|
297
|
|
|
$obj = &get_instance(); |
|
298
|
|
|
$db = & class_loader('Database', 'classes/database', $connection); |
|
299
|
|
|
$queryResult = &class_loader('DatabaseQueryResult', 'classes/database'); |
|
300
|
|
|
$queryRunner = &class_loader('DatabaseQueryRunner', 'classes/database', $connection); |
|
301
|
|
|
$queryRunner->setQueryResult($queryResult); |
|
302
|
|
|
$queryRunner->setBenchmark($obj->benchmark); |
|
303
|
|
|
$db->setQueryRunner($queryRunner); |
|
304
|
|
|
|
|
305
|
|
|
$queryBuilder = &class_loader('DatabaseQueryBuilder', 'classes/database', $connection); |
|
306
|
|
|
$db->setQueryBuilder($queryBuilder); |
|
307
|
|
|
|
|
308
|
|
|
$dbCache = &class_loader('DatabaseCache', 'classes/database'); |
|
309
|
|
|
$db->setCache($dbCache); |
|
310
|
|
|
$obj->database = $db; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Return the current app language by default will use the value from cookie |
|
315
|
|
|
* if can not found will use the default value from configuration |
|
316
|
|
|
* @return string the app language like "en", "fr" |
|
317
|
|
|
*/ |
|
318
|
|
|
protected function getAppLang() { |
|
319
|
|
|
//determine the current language |
|
320
|
|
|
$appLang = get_instance()->config->get('default_language'); |
|
321
|
|
|
//if the language exists in the cookie use it |
|
322
|
|
|
$cfgKey = get_instance()->config->get('language_cookie_name'); |
|
323
|
|
|
$objCookie = & class_loader('Cookie'); |
|
324
|
|
|
$cookieLang = $objCookie->get($cfgKey); |
|
325
|
|
|
if ($cookieLang) { |
|
326
|
|
|
$appLang = $cookieLang; |
|
327
|
|
|
} |
|
328
|
|
|
return $appLang; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Return the default full file path for function, language |
|
333
|
|
|
* @param string $file the filename |
|
334
|
|
|
* @param string $type the type can be "function", "language" |
|
335
|
|
|
* @param string $appLang the application language, only if type = "language" |
|
336
|
|
|
* @return string|null the full file path |
|
337
|
|
|
*/ |
|
338
|
|
|
protected function getDefaultFilePathForFunctionLanguage($file, $type, $appLang = null){ |
|
339
|
|
|
//Default to "function" |
|
340
|
|
|
$searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
|
341
|
|
|
if ($type == 'language') { |
|
342
|
|
|
$searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
|
343
|
|
|
$file = $appLang . DS . $file; |
|
344
|
|
|
} |
|
345
|
|
|
$fullFilePath = null; |
|
346
|
|
|
foreach ($searchDir as $dir) { |
|
347
|
|
|
$filePath = $dir . $file; |
|
348
|
|
|
if (file_exists($filePath)) { |
|
349
|
|
|
$fullFilePath = $filePath; |
|
350
|
|
|
//is already found not to continue |
|
351
|
|
|
break; |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
|
|
return $fullFilePath; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Get the module using the attribute of super controller "moduleName" |
|
359
|
|
|
* @param string|null $module the module if is not null will return it |
|
360
|
|
|
* @return string|null |
|
361
|
|
|
*/ |
|
362
|
|
|
protected function getModuleFromSuperController($module){ |
|
363
|
|
|
$obj = & get_instance(); |
|
364
|
|
|
if (!$module && !empty($obj->moduleName)) { |
|
365
|
|
|
$module = $obj->moduleName; |
|
366
|
|
|
} |
|
367
|
|
|
return $module; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* Get the module information for the model and library to load |
|
372
|
|
|
* @param string $class the full class name like moduleName/className, className, |
|
373
|
|
|
* @return array the module information |
|
374
|
|
|
* array( |
|
375
|
|
|
* 'module'=> 'module_name' |
|
376
|
|
|
* 'class' => 'class_name' |
|
377
|
|
|
* ) |
|
378
|
|
|
*/ |
|
379
|
|
|
protected function getModuleInfoForModelLibrary($class) { |
|
380
|
|
|
$module = null; |
|
381
|
|
|
$path = explode('/', $class); |
|
382
|
|
|
if (count($path) >= 2 && in_array($path[0], get_instance()->module->getModuleList())) { |
|
383
|
|
|
$module = $path[0]; |
|
384
|
|
|
$class = ucfirst($path[1]); |
|
385
|
|
|
} else { |
|
386
|
|
|
$class = ucfirst($class); |
|
387
|
|
|
} |
|
388
|
|
|
$module = $this->getModuleFromSuperController($module); |
|
389
|
|
|
return array( |
|
390
|
|
|
'class' => $class, |
|
391
|
|
|
'module' => $module |
|
392
|
|
|
); |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* Get the module information for the function or language to load |
|
397
|
|
|
* |
|
398
|
|
|
* @param string $name the function or language name like moduleName/functionName, functionName, |
|
399
|
|
|
* moduleName/langName, langName, etc. |
|
400
|
|
|
* @param string $type the type of resource can be "function", "lang" |
|
401
|
|
|
* |
|
402
|
|
|
* @return array the module information |
|
403
|
|
|
* array( |
|
404
|
|
|
* 'module'=> 'module_name' |
|
405
|
|
|
* 'name' => 'function or language' |
|
406
|
|
|
* 'file' => 'file' |
|
407
|
|
|
* ) |
|
408
|
|
|
*/ |
|
409
|
|
|
protected function getModuleInfoForFunctionLanguage($name, $type = 'function') { |
|
410
|
|
|
$module = null; |
|
411
|
|
|
$file = null; |
|
412
|
|
|
//check if the request class contains module name |
|
413
|
|
|
$path = explode('/', $name); |
|
414
|
|
|
if (count($path) >= 2 && in_array($path[0], get_instance()->module->getModuleList())) { |
|
415
|
|
|
$module = $path[0]; |
|
416
|
|
|
$name = $type . '_' . $path[1]; |
|
417
|
|
|
$file = $path[0] . DS . $name . '.php'; |
|
418
|
|
|
} |
|
419
|
|
|
$module = $this->getModuleFromSuperController($module); |
|
420
|
|
|
return array( |
|
421
|
|
|
'name' => $name, |
|
422
|
|
|
'module' => $module, |
|
423
|
|
|
'file' => $file |
|
424
|
|
|
); |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
/** |
|
428
|
|
|
* Get the module information for the config to load |
|
429
|
|
|
* @param string $filename the filename of the configuration file, |
|
430
|
|
|
* @return array the module information |
|
431
|
|
|
* array( |
|
432
|
|
|
* 'module'=> 'module_name' |
|
433
|
|
|
* 'filename' => 'filename' |
|
434
|
|
|
* ) |
|
435
|
|
|
*/ |
|
436
|
|
|
protected function getModuleInfoForConfig($filename) { |
|
437
|
|
|
$module = null; |
|
438
|
|
|
//check if the request class contains module name |
|
439
|
|
|
$path = explode('/', $filename); |
|
440
|
|
|
if (count($path) >= 2 && in_array($path[0], get_instance()->module->getModuleList())) { |
|
441
|
|
|
$module = $path[0]; |
|
442
|
|
|
$filename = $path[1] . '.php'; |
|
443
|
|
|
} |
|
444
|
|
|
$module = $this->getModuleFromSuperController($module); |
|
445
|
|
|
return array( |
|
446
|
|
|
'filename' => $filename, |
|
447
|
|
|
'module' => $module |
|
448
|
|
|
); |
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
/** |
|
452
|
|
|
* Get the name of model or library instance if is null |
|
453
|
|
|
* @param string $class the class name to determine the instance |
|
454
|
|
|
* @return string the instance name |
|
455
|
|
|
*/ |
|
456
|
|
|
protected function getModelLibraryInstanceName($class) { |
|
457
|
|
|
//for module |
|
458
|
|
|
$instance = null; |
|
459
|
|
|
$path = explode('/', $class); |
|
460
|
|
|
if (count($path) >= 2) { |
|
461
|
|
|
$instance = strtolower($path[1]); |
|
462
|
|
|
} else { |
|
463
|
|
|
$instance = strtolower($class); |
|
464
|
|
|
} |
|
465
|
|
|
return $instance; |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
/** |
|
469
|
|
|
* Get the library file path and class name using the module information |
|
470
|
|
|
* @param string $class the class name |
|
471
|
|
|
* @return array the library file path and class name |
|
472
|
|
|
*/ |
|
473
|
|
|
protected function getLibraryPathUsingModuleInfo($class) { |
|
474
|
|
|
$libraryFilePath = null; |
|
475
|
|
|
$this->logger->debug('Checking library [' . $class . '] from module list ...'); |
|
476
|
|
|
$moduleInfo = $this->getModuleInfoForModelLibrary($class); |
|
477
|
|
|
$module = $moduleInfo['module']; |
|
478
|
|
|
$class = $moduleInfo['class']; |
|
479
|
|
|
$moduleLibraryPath = get_instance()->module->findLibraryFullPath($class, $module); |
|
480
|
|
|
if ($moduleLibraryPath) { |
|
481
|
|
|
$this->logger->info('Found library [' . $class . '] from module [' . $module . '], the ' |
|
482
|
|
|
. 'file path is [' . $moduleLibraryPath . '] we will used it'); |
|
483
|
|
|
$libraryFilePath = $moduleLibraryPath; |
|
484
|
|
|
} else { |
|
485
|
|
|
$this->logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
|
486
|
|
|
} |
|
487
|
|
|
return array( |
|
488
|
|
|
'path' => $libraryFilePath, |
|
489
|
|
|
'class' => $class |
|
490
|
|
|
); |
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
|
|
/** |
|
494
|
|
|
* Load the library |
|
495
|
|
|
* @param string $libraryFilePath the file path of the library to load |
|
496
|
|
|
* @param string $class the class name |
|
497
|
|
|
* @param string $instance the instance |
|
498
|
|
|
* @param array $params the parameter to use |
|
499
|
|
|
* @return void |
|
500
|
|
|
*/ |
|
501
|
|
|
protected function loadLibrary($libraryFilePath, $class, $instance, $params = array()) { |
|
502
|
|
|
if ($libraryFilePath) { |
|
503
|
|
|
require_once $libraryFilePath; |
|
504
|
|
|
if (class_exists($class)) { |
|
505
|
|
|
$cls = $params ? new $class($params) : new $class(); |
|
506
|
|
|
$obj = & get_instance(); |
|
507
|
|
|
$obj->{$instance} = $cls; |
|
508
|
|
|
$this->loaded[$instance] = $class; |
|
509
|
|
|
$this->logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
|
510
|
|
|
} else { |
|
511
|
|
|
show_error('The file ' . $libraryFilePath . ' exists but does not contain the class ' . $class); |
|
512
|
|
|
} |
|
513
|
|
|
} else { |
|
514
|
|
|
show_error('Unable to find library class [' . $class . ']'); |
|
515
|
|
|
} |
|
516
|
|
|
} |
|
517
|
|
|
|
|
518
|
|
|
/** |
|
519
|
|
|
* Load the language |
|
520
|
|
|
* @param string $languageFilePath the file path of the language to load |
|
521
|
|
|
* @param string $language the language name |
|
522
|
|
|
* @param string $langCode the language code to use |
|
523
|
|
|
* @return void |
|
524
|
|
|
*/ |
|
525
|
|
|
protected function loadLanguage($languageFilePath, $language, $langCode) { |
|
526
|
|
|
if ($languageFilePath) { |
|
527
|
|
|
$lang = array(); |
|
528
|
|
|
require_once $languageFilePath; |
|
529
|
|
|
if (!empty($lang) && is_array($lang)) { |
|
530
|
|
|
$this->logger->info('Language file [' . $languageFilePath . '] contains the ' |
|
531
|
|
|
. 'valid languages keys add them to language list'); |
|
532
|
|
|
//Note: may be here the class 'Lang' not yet loaded |
|
533
|
|
|
$langObj = & class_loader('Lang', 'classes'); |
|
534
|
|
|
$langObj->addLangMessages($lang); |
|
535
|
|
|
//free the memory |
|
536
|
|
|
unset($lang); |
|
537
|
|
|
} |
|
538
|
|
|
$this->loaded['lang_' . $langCode . '_' . $language] = $languageFilePath; |
|
539
|
|
|
$this->logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
|
540
|
|
|
} else { |
|
541
|
|
|
show_error('Unable to find language [' . $language . ']'); |
|
542
|
|
|
} |
|
543
|
|
|
} |
|
544
|
|
|
|
|
545
|
|
|
/** |
|
546
|
|
|
* Load the resources autoload array |
|
547
|
|
|
* @param string $method this object method name to call |
|
548
|
|
|
* @param array $resources the resource to load |
|
549
|
|
|
* @return void |
|
550
|
|
|
*/ |
|
551
|
|
|
protected function loadAutoloadResourcesArray($method, array $resources) { |
|
552
|
|
|
foreach ($resources as $name) { |
|
553
|
|
|
$this->{$method}($name); |
|
554
|
|
|
} |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
/** |
|
558
|
|
|
* Get all the autoload using the configuration file |
|
559
|
|
|
* @return array |
|
560
|
|
|
*/ |
|
561
|
|
|
protected function getResourcesFromAutoloadConfig() { |
|
562
|
|
|
$autoloads = array(); |
|
563
|
|
|
$autoloads['config'] = array(); |
|
564
|
|
|
$autoloads['languages'] = array(); |
|
565
|
|
|
$autoloads['libraries'] = array(); |
|
566
|
|
|
$autoloads['models'] = array(); |
|
567
|
|
|
$autoloads['functions'] = array(); |
|
568
|
|
|
//loading of the resources from autoload configuration file |
|
569
|
|
|
if (file_exists(CONFIG_PATH . 'autoload.php')) { |
|
570
|
|
|
$autoload = array(); |
|
571
|
|
|
require CONFIG_PATH . 'autoload.php'; |
|
572
|
|
|
if (!empty($autoload) && is_array($autoload)) { |
|
573
|
|
|
$autoloads = array_merge($autoloads, $autoload); |
|
574
|
|
|
unset($autoload); |
|
575
|
|
|
} |
|
576
|
|
|
} |
|
577
|
|
|
//loading autoload configuration for modules |
|
578
|
|
|
$modulesAutoloads = get_instance()->module->getModulesAutoloadConfig(); |
|
579
|
|
|
if (!empty($modulesAutoloads) && is_array($modulesAutoloads)) { |
|
580
|
|
|
$autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
|
581
|
|
|
} |
|
582
|
|
|
return $autoloads; |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
/** |
|
586
|
|
|
* Load the autoload configuration |
|
587
|
|
|
* @return void |
|
588
|
|
|
*/ |
|
589
|
|
|
protected function loadResourcesFromAutoloadConfig() { |
|
590
|
|
|
$autoloads = array(); |
|
591
|
|
|
$autoloads['config'] = array(); |
|
592
|
|
|
$autoloads['languages'] = array(); |
|
593
|
|
|
$autoloads['libraries'] = array(); |
|
594
|
|
|
$autoloads['models'] = array(); |
|
595
|
|
|
$autoloads['functions'] = array(); |
|
596
|
|
|
|
|
597
|
|
|
$list = $this->getResourcesFromAutoloadConfig(); |
|
598
|
|
|
$autoloads = array_merge($autoloads, $list); |
|
599
|
|
|
|
|
600
|
|
|
//config autoload |
|
601
|
|
|
$this->loadAutoloadResourcesArray('config', $autoloads['config']); |
|
602
|
|
|
|
|
603
|
|
|
//languages autoload |
|
604
|
|
|
$this->loadAutoloadResourcesArray('lang', $autoloads['languages']); |
|
605
|
|
|
|
|
606
|
|
|
//libraries autoload |
|
607
|
|
|
$this->loadAutoloadResourcesArray('library', $autoloads['libraries']); |
|
608
|
|
|
|
|
609
|
|
|
//models autoload |
|
610
|
|
|
$this->loadAutoloadResourcesArray('model', $autoloads['models']); |
|
611
|
|
|
|
|
612
|
|
|
//functions autoload |
|
613
|
|
|
$this->loadAutoloadResourcesArray('functions', $autoloads['functions']); |
|
614
|
|
|
} |
|
615
|
|
|
} |
|
616
|
|
|
|