| Total Complexity | 87 |
| Total Lines | 486 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Loader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Loader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Loader{ |
||
| 27 | |||
| 28 | /** |
||
| 29 | * List of loaded resources |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | public static $loaded = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The logger instance |
||
| 36 | * @var Log |
||
| 37 | */ |
||
| 38 | private static $logger; |
||
| 39 | |||
| 40 | |||
| 41 | public function __construct(){ |
||
| 42 | //add the resources already loaded during application bootstrap |
||
| 43 | //in the list to prevent duplicate or loading the resources again. |
||
| 44 | static::$loaded = class_loaded(); |
||
| 45 | |||
| 46 | //Load resources from autoload configuration |
||
| 47 | $this->loadResourcesFromAutoloadConfig(); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the logger singleton instance |
||
| 52 | * @return Log the logger instance |
||
| 53 | */ |
||
| 54 | private static function getLogger(){ |
||
| 55 | if(self::$logger == null){ |
||
| 56 | self::$logger[0] =& class_loader('Log', 'classes'); |
||
| 57 | self::$logger[0]->setLogger('Library::Loader'); |
||
| 58 | } |
||
| 59 | return self::$logger[0]; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Load the model class |
||
| 64 | * |
||
| 65 | * @param string $class the class name to be loaded |
||
| 66 | * @param string $instance the name of the instance to use in super object |
||
| 67 | * |
||
| 68 | * @return void |
||
| 69 | */ |
||
| 70 | public static function model($class, $instance = null){ |
||
| 71 | $logger = static::getLogger(); |
||
| 72 | $class = str_ireplace('.php', '', $class); |
||
| 73 | $class = trim($class, '/\\'); |
||
| 74 | $file = ucfirst($class).'.php'; |
||
| 75 | $logger->debug('Loading model [' . $class . '] ...'); |
||
| 76 | if(! $instance){ |
||
| 77 | //for module |
||
| 78 | if(strpos($class, '/') !== false){ |
||
| 79 | $path = explode('/', $class); |
||
| 80 | if(isset($path[1])){ |
||
| 81 | $instance = strtolower($path[1]); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | else{ |
||
| 85 | $instance = strtolower($class); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | if(isset(static::$loaded[$instance])){ |
||
| 89 | $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
||
| 90 | return; |
||
| 91 | } |
||
| 92 | $classFilePath = APPS_MODEL_PATH . $file; |
||
| 93 | //first check if this model is in the module |
||
| 94 | $logger->debug('Checking model [' . $class . '] from module list ...'); |
||
| 95 | $searchModuleName = null; |
||
| 96 | $obj = & get_instance(); |
||
| 97 | //check if the request class contains module name |
||
| 98 | if(strpos($class, '/') !== false){ |
||
| 99 | $path = explode('/', $class); |
||
| 100 | if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
||
| 101 | $searchModuleName = $path[0]; |
||
| 102 | $class = ucfirst($path[1]); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | else{ |
||
| 106 | $class = ucfirst($class); |
||
| 107 | } |
||
| 108 | |||
| 109 | if(! $searchModuleName && !empty($obj->moduleName)){ |
||
| 110 | $searchModuleName = $obj->moduleName; |
||
| 111 | } |
||
| 112 | $moduleModelFilePath = Module::findModelFullPath($class, $searchModuleName); |
||
| 113 | if($moduleModelFilePath){ |
||
| 114 | $logger->info('Found model [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
||
| 115 | $classFilePath = $moduleModelFilePath; |
||
| 116 | } |
||
| 117 | else{ |
||
| 118 | $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
||
| 119 | } |
||
| 120 | $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
||
| 121 | if(file_exists($classFilePath)){ |
||
| 122 | require_once $classFilePath; |
||
| 123 | if(class_exists($class)){ |
||
| 124 | $c = new $class(); |
||
| 125 | $obj = & get_instance(); |
||
| 126 | $obj->{$instance} = $c; |
||
| 127 | static::$loaded[$instance] = $class; |
||
| 128 | $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
||
| 129 | } |
||
| 130 | else{ |
||
| 131 | show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | else{ |
||
| 135 | show_error('Unable to find the model [' . $class . ']'); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * Load the library class |
||
| 142 | * |
||
| 143 | * @param string $class the library class name to be loaded |
||
| 144 | * @param string $instance the instance name to use in super object |
||
| 145 | * @param mixed $params the arguments to pass to the constructor |
||
| 146 | * |
||
| 147 | * @return void |
||
| 148 | */ |
||
| 149 | public static function library($class, $instance = null, array $params = array()){ |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Load the helper |
||
| 248 | * |
||
| 249 | * @param string $function the helper name to be loaded |
||
| 250 | * |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | public static function functions($function){ |
||
| 254 | $logger = static::getLogger(); |
||
| 255 | $function = str_ireplace('.php', '', $function); |
||
| 256 | $function = trim($function, '/\\'); |
||
| 257 | $function = str_ireplace('function_', '', $function); |
||
| 258 | $file = 'function_'.$function.'.php'; |
||
| 259 | $logger->debug('Loading helper [' . $function . '] ...'); |
||
| 260 | if(isset(static::$loaded['function_' . $function])){ |
||
| 261 | $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
||
| 262 | return; |
||
| 263 | } |
||
| 264 | $functionFilePath = null; |
||
| 265 | //first check if this helper is in the module |
||
| 266 | $logger->debug('Checking helper [' . $function . '] from module list ...'); |
||
| 267 | $searchModuleName = null; |
||
| 268 | $obj = & get_instance(); |
||
| 269 | //check if the request class contains module name |
||
| 270 | if(strpos($function, '/') !== false){ |
||
| 271 | $path = explode('/', $function); |
||
| 272 | if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
||
| 273 | $searchModuleName = $path[0]; |
||
| 274 | $function = 'function_' . $path[1] . '.php'; |
||
| 275 | $file = $path[0] . DS . 'function_'.$function.'.php'; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | if(! $searchModuleName && !empty($obj->moduleName)){ |
||
| 279 | $searchModuleName = $obj->moduleName; |
||
| 280 | } |
||
| 281 | $moduleFunctionPath = Module::findFunctionFullPath($function, $searchModuleName); |
||
| 282 | if($moduleFunctionPath){ |
||
| 283 | $logger->info('Found helper [' . $function . '] from module [' .$searchModuleName. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
||
| 284 | $functionFilePath = $moduleFunctionPath; |
||
| 285 | } |
||
| 286 | else{ |
||
| 287 | $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
||
| 288 | } |
||
| 289 | if(! $functionFilePath){ |
||
| 290 | $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
||
| 291 | foreach($searchDir as $dir){ |
||
| 292 | $filePath = $dir . $file; |
||
| 293 | if(file_exists($filePath)){ |
||
| 294 | $functionFilePath = $filePath; |
||
| 295 | //is already found not to continue |
||
| 296 | break; |
||
| 297 | } |
||
| 298 | } |
||
| 299 | } |
||
| 300 | $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
||
| 301 | if($functionFilePath){ |
||
| 302 | require_once $functionFilePath; |
||
| 303 | static::$loaded['function_' . $function] = $functionFilePath; |
||
| 304 | $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
||
| 305 | } |
||
| 306 | else{ |
||
| 307 | show_error('Unable to find helper file [' . $file . ']'); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Load the configuration file |
||
| 313 | * |
||
| 314 | * @param string $filename the configuration filename located at CONFIG_PATH or MODULE_PATH/config |
||
| 315 | * |
||
| 316 | * @return void |
||
| 317 | */ |
||
| 318 | public static function config($filename){ |
||
| 319 | $logger = static::getLogger(); |
||
| 320 | $filename = str_ireplace('.php', '', $filename); |
||
| 321 | $filename = trim($filename, '/\\'); |
||
| 322 | $filename = str_ireplace('config_', '', $filename); |
||
| 323 | $file = 'config_'.$filename.'.php'; |
||
| 324 | $logger->debug('Loading configuration [' . $filename . '] ...'); |
||
| 325 | if(isset(static::$loaded['config_' . $filename])){ |
||
| 326 | $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
||
| 327 | return; |
||
| 328 | } |
||
| 329 | $configFilePath = CONFIG_PATH . $file; |
||
| 330 | //first check if this config is in the module |
||
| 331 | $logger->debug('Checking config [' . $filename . '] from module list ...'); |
||
| 332 | $searchModuleName = null; |
||
| 333 | $obj = & get_instance(); |
||
| 334 | //check if the request class contains module name |
||
| 335 | if(strpos($filename, '/') !== false){ |
||
| 336 | $path = explode('/', $filename); |
||
| 337 | if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
||
| 338 | $searchModuleName = $path[0]; |
||
| 339 | $filename = $path[1] . '.php'; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | if(! $searchModuleName && !empty($obj->moduleName)){ |
||
| 343 | $searchModuleName = $obj->moduleName; |
||
| 344 | } |
||
| 345 | $moduleConfigPath = Module::findConfigFullPath($filename, $searchModuleName); |
||
| 346 | if($moduleConfigPath){ |
||
| 347 | $logger->info('Found config [' . $filename . '] from module [' .$searchModuleName. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
||
| 348 | $configFilePath = $moduleConfigPath; |
||
| 349 | } |
||
| 350 | else{ |
||
| 351 | $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
||
| 352 | } |
||
| 353 | $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
||
| 354 | if(file_exists($configFilePath)){ |
||
| 355 | require_once $configFilePath; |
||
| 356 | if(! empty($config) && is_array($config)){ |
||
|
|
|||
| 357 | Config::setAll($config); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | else{ |
||
| 361 | show_error('Unable to find config file ['. $configFilePath . ']'); |
||
| 362 | } |
||
| 363 | static::$loaded['config_' . $filename] = $configFilePath; |
||
| 364 | $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
||
| 365 | $logger->info('The custom application configuration loaded are listed below: ' . stringfy_vars($config)); |
||
| 366 | unset($config); |
||
| 367 | } |
||
| 368 | |||
| 369 | |||
| 370 | /** |
||
| 371 | * Load the language |
||
| 372 | * |
||
| 373 | * @param string $language the language name to be loaded |
||
| 374 | * |
||
| 375 | * @return void |
||
| 376 | */ |
||
| 377 | public static function lang($language){ |
||
| 378 | $logger = static::getLogger(); |
||
| 379 | $language = str_ireplace('.php', '', $language); |
||
| 380 | $language = trim($language, '/\\'); |
||
| 381 | $language = str_ireplace('lang_', '', $language); |
||
| 382 | $file = 'lang_'.$language.'.php'; |
||
| 383 | $logger->debug('Loading language [' . $language . '] ...'); |
||
| 384 | if(isset(static::$loaded['lang_' . $language])){ |
||
| 385 | $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
||
| 386 | return; |
||
| 387 | } |
||
| 388 | //determine the current language |
||
| 389 | $appLang = get_config('default_language'); |
||
| 390 | //if the language exists in the cookie use it |
||
| 391 | $cfgKey = get_config('language_cookie_name'); |
||
| 392 | $objCookie = & class_loader('Cookie'); |
||
| 393 | $cookieLang = $objCookie->get($cfgKey); |
||
| 394 | if($cookieLang){ |
||
| 395 | $appLang = $cookieLang; |
||
| 396 | } |
||
| 397 | $languageFilePath = null; |
||
| 398 | //first check if this language is in the module |
||
| 399 | $logger->debug('Checking language [' . $language . '] from module list ...'); |
||
| 400 | $searchModuleName = null; |
||
| 401 | $obj = & get_instance(); |
||
| 402 | //check if the request class contains module name |
||
| 403 | if(strpos($language, '/') !== false){ |
||
| 404 | $path = explode('/', $language); |
||
| 405 | if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
||
| 406 | $searchModuleName = $path[0]; |
||
| 407 | $language = 'lang_' . $path[1] . '.php'; |
||
| 408 | $file = $path[0] . DS .$language; |
||
| 409 | } |
||
| 410 | } |
||
| 411 | if(! $searchModuleName && !empty($obj->moduleName)){ |
||
| 412 | $searchModuleName = $obj->moduleName; |
||
| 413 | } |
||
| 414 | $moduleLanguagePath = Module::findLanguageFullPath($language, $searchModuleName, $appLang); |
||
| 415 | if($moduleLanguagePath){ |
||
| 416 | $logger->info('Found language [' . $language . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
||
| 417 | $languageFilePath = $moduleLanguagePath; |
||
| 418 | } |
||
| 419 | else{ |
||
| 420 | $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
||
| 421 | } |
||
| 422 | if(! $languageFilePath){ |
||
| 423 | $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
||
| 424 | foreach($searchDir as $dir){ |
||
| 425 | $filePath = $dir . $appLang . DS . $file; |
||
| 426 | if(file_exists($filePath)){ |
||
| 427 | $languageFilePath = $filePath; |
||
| 428 | //is already found not to continue |
||
| 429 | break; |
||
| 430 | } |
||
| 431 | } |
||
| 432 | } |
||
| 433 | $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
||
| 434 | if($languageFilePath){ |
||
| 435 | require_once $languageFilePath; |
||
| 436 | if(! empty($lang) && is_array($lang)){ |
||
| 437 | $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
||
| 438 | //Note: may be here the class 'Lang' not yet loaded |
||
| 439 | $langObj =& class_loader('Lang', 'classes'); |
||
| 440 | $langObj->addLangMessages($lang); |
||
| 441 | //free the memory |
||
| 442 | unset($lang); |
||
| 443 | } |
||
| 444 | static::$loaded['lang_' . $language] = $languageFilePath; |
||
| 445 | $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
||
| 446 | } |
||
| 447 | else{ |
||
| 448 | show_error('Unable to find language file [' . $file . ']'); |
||
| 449 | } |
||
| 450 | } |
||
| 451 | |||
| 452 | |||
| 453 | private function getResourcesFromAutoloadConfig(){ |
||
| 454 | $autoloads = array(); |
||
| 455 | $autoloads['config'] = array(); |
||
| 456 | $autoloads['languages'] = array(); |
||
| 457 | $autoloads['libraries'] = array(); |
||
| 458 | $autoloads['models'] = array(); |
||
| 459 | $autoloads['functions'] = array(); |
||
| 460 | //loading of the resources in autoload.php configuration file |
||
| 461 | if(file_exists(CONFIG_PATH . 'autoload.php')){ |
||
| 462 | require_once CONFIG_PATH . 'autoload.php'; |
||
| 463 | if(! empty($autoload) && is_array($autoload)){ |
||
| 464 | $autoloads = array_merge($autoloads, $autoload); |
||
| 465 | unset($autoload); |
||
| 466 | } |
||
| 467 | } |
||
| 468 | //loading autoload configuration for modules |
||
| 469 | $modulesAutoloads = Module::getModulesAutoloadConfig(); |
||
| 470 | if(! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
||
| 471 | $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
||
| 472 | } |
||
| 473 | return $autoloads; |
||
| 474 | } |
||
| 475 | |||
| 476 | private function loadResourcesFromAutoloadConfig(){ |
||
| 512 | } |
||
| 513 | } |
||
| 514 | } |
||
| 515 |