@@ -1,58 +1,58 @@ |
||
| 1 | 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 | - */ |
|
| 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 | 26 | |
| 27 | - class BaseStaticClass{ |
|
| 28 | - /** |
|
| 29 | - * The logger instance |
|
| 30 | - * @var object |
|
| 31 | - */ |
|
| 32 | - protected static $logger; |
|
| 27 | + class BaseStaticClass{ |
|
| 28 | + /** |
|
| 29 | + * The logger instance |
|
| 30 | + * @var object |
|
| 31 | + */ |
|
| 32 | + protected static $logger; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The signleton of the logger |
|
| 36 | - * @return Object the Log instance |
|
| 37 | - */ |
|
| 38 | - public static function getLogger(){ |
|
| 39 | - if(self::$logger == null){ |
|
| 40 | - $logger = array(); |
|
| 41 | - $logger[0] =& class_loader('Log', 'classes'); |
|
| 42 | - $logger[0]->setLogger('Class::' . get_called_class()); |
|
| 43 | - self::$logger = $logger[0]; |
|
| 44 | - } |
|
| 45 | - return self::$logger; |
|
| 46 | - } |
|
| 34 | + /** |
|
| 35 | + * The signleton of the logger |
|
| 36 | + * @return Object the Log instance |
|
| 37 | + */ |
|
| 38 | + public static function getLogger(){ |
|
| 39 | + if(self::$logger == null){ |
|
| 40 | + $logger = array(); |
|
| 41 | + $logger[0] =& class_loader('Log', 'classes'); |
|
| 42 | + $logger[0]->setLogger('Class::' . get_called_class()); |
|
| 43 | + self::$logger = $logger[0]; |
|
| 44 | + } |
|
| 45 | + return self::$logger; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Set the log instance for future use |
|
| 50 | - * @param object $logger the log object |
|
| 51 | - * @return object the log instance |
|
| 52 | - */ |
|
| 53 | - public static function setLogger($logger){ |
|
| 54 | - self::$logger = $logger; |
|
| 55 | - return self::$logger; |
|
| 56 | - } |
|
| 48 | + /** |
|
| 49 | + * Set the log instance for future use |
|
| 50 | + * @param object $logger the log object |
|
| 51 | + * @return object the log instance |
|
| 52 | + */ |
|
| 53 | + public static function setLogger($logger){ |
|
| 54 | + self::$logger = $logger; |
|
| 55 | + return self::$logger; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - } |
|
| 58 | + } |
|
@@ -1,627 +1,627 @@ |
||
| 1 | 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 | - class Loader extends BaseStaticClass{ |
|
| 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 | + class Loader extends BaseStaticClass{ |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * List of loaded resources |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - public static $loaded = array(); |
|
| 28 | + /** |
|
| 29 | + * List of loaded resources |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + public static $loaded = array(); |
|
| 33 | 33 | |
| 34 | 34 | |
| 35 | - public function __construct(){ |
|
| 36 | - //add the resources already loaded during application bootstrap |
|
| 37 | - //in the list to prevent duplicate or loading the resources again. |
|
| 38 | - static::$loaded = class_loaded(); |
|
| 35 | + public function __construct(){ |
|
| 36 | + //add the resources already loaded during application bootstrap |
|
| 37 | + //in the list to prevent duplicate or loading the resources again. |
|
| 38 | + static::$loaded = class_loaded(); |
|
| 39 | 39 | |
| 40 | - //Load resources from autoload configuration |
|
| 41 | - $this->loadResourcesFromAutoloadConfig(); |
|
| 42 | - } |
|
| 40 | + //Load resources from autoload configuration |
|
| 41 | + $this->loadResourcesFromAutoloadConfig(); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Load the model class |
|
| 47 | - * |
|
| 48 | - * @param string $class the class name to be loaded |
|
| 49 | - * @param string $instance the name of the instance to use in super object |
|
| 50 | - * |
|
| 51 | - * @return void |
|
| 52 | - */ |
|
| 53 | - public static function model($class, $instance = null){ |
|
| 54 | - $logger = static::getLogger(); |
|
| 55 | - $class = str_ireplace('.php', '', $class); |
|
| 56 | - $class = trim($class, '/\\'); |
|
| 57 | - $file = ucfirst($class).'.php'; |
|
| 58 | - $logger->debug('Loading model [' . $class . '] ...'); |
|
| 59 | - //************ |
|
| 60 | - if (! $instance){ |
|
| 61 | - $instance = self::getModelLibraryInstanceName($class); |
|
| 62 | - } |
|
| 63 | - //**************** |
|
| 64 | - if (isset(static::$loaded[$instance])){ |
|
| 65 | - $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 66 | - return; |
|
| 67 | - } |
|
| 68 | - $classFilePath = APPS_MODEL_PATH . $file; |
|
| 69 | - //first check if this model is in the module |
|
| 70 | - $logger->debug('Checking model [' . $class . '] from module list ...'); |
|
| 71 | - //check if the request class contains module name |
|
| 72 | - $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 73 | - $module = $moduleInfo['module']; |
|
| 74 | - $class = $moduleInfo['class']; |
|
| 45 | + /** |
|
| 46 | + * Load the model class |
|
| 47 | + * |
|
| 48 | + * @param string $class the class name to be loaded |
|
| 49 | + * @param string $instance the name of the instance to use in super object |
|
| 50 | + * |
|
| 51 | + * @return void |
|
| 52 | + */ |
|
| 53 | + public static function model($class, $instance = null){ |
|
| 54 | + $logger = static::getLogger(); |
|
| 55 | + $class = str_ireplace('.php', '', $class); |
|
| 56 | + $class = trim($class, '/\\'); |
|
| 57 | + $file = ucfirst($class).'.php'; |
|
| 58 | + $logger->debug('Loading model [' . $class . '] ...'); |
|
| 59 | + //************ |
|
| 60 | + if (! $instance){ |
|
| 61 | + $instance = self::getModelLibraryInstanceName($class); |
|
| 62 | + } |
|
| 63 | + //**************** |
|
| 64 | + if (isset(static::$loaded[$instance])){ |
|
| 65 | + $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 66 | + return; |
|
| 67 | + } |
|
| 68 | + $classFilePath = APPS_MODEL_PATH . $file; |
|
| 69 | + //first check if this model is in the module |
|
| 70 | + $logger->debug('Checking model [' . $class . '] from module list ...'); |
|
| 71 | + //check if the request class contains module name |
|
| 72 | + $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 73 | + $module = $moduleInfo['module']; |
|
| 74 | + $class = $moduleInfo['class']; |
|
| 75 | 75 | |
| 76 | - $moduleModelFilePath = Module::findModelFullPath($class, $module); |
|
| 77 | - if ($moduleModelFilePath){ |
|
| 78 | - $logger->info('Found model [' . $class . '] from module [' .$module. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
|
| 79 | - $classFilePath = $moduleModelFilePath; |
|
| 80 | - } |
|
| 81 | - else{ |
|
| 82 | - $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
|
| 83 | - } |
|
| 84 | - $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
|
| 85 | - if (file_exists($classFilePath)){ |
|
| 86 | - require_once $classFilePath; |
|
| 87 | - if (class_exists($class)){ |
|
| 88 | - $c = new $class(); |
|
| 89 | - $obj = & get_instance(); |
|
| 90 | - $obj->{$instance} = $c; |
|
| 91 | - static::$loaded[$instance] = $class; |
|
| 92 | - $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
|
| 93 | - } |
|
| 94 | - else{ |
|
| 95 | - show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - else{ |
|
| 99 | - show_error('Unable to find the model [' . $class . ']'); |
|
| 100 | - } |
|
| 101 | - } |
|
| 76 | + $moduleModelFilePath = Module::findModelFullPath($class, $module); |
|
| 77 | + if ($moduleModelFilePath){ |
|
| 78 | + $logger->info('Found model [' . $class . '] from module [' .$module. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
|
| 79 | + $classFilePath = $moduleModelFilePath; |
|
| 80 | + } |
|
| 81 | + else{ |
|
| 82 | + $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
|
| 83 | + } |
|
| 84 | + $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
|
| 85 | + if (file_exists($classFilePath)){ |
|
| 86 | + require_once $classFilePath; |
|
| 87 | + if (class_exists($class)){ |
|
| 88 | + $c = new $class(); |
|
| 89 | + $obj = & get_instance(); |
|
| 90 | + $obj->{$instance} = $c; |
|
| 91 | + static::$loaded[$instance] = $class; |
|
| 92 | + $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
|
| 93 | + } |
|
| 94 | + else{ |
|
| 95 | + show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + else{ |
|
| 99 | + show_error('Unable to find the model [' . $class . ']'); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Load the library class |
|
| 106 | - * |
|
| 107 | - * @param string $class the library class name to be loaded |
|
| 108 | - * @param string $instance the instance name to use in super object |
|
| 109 | - * @param mixed $params the arguments to pass to the constructor |
|
| 110 | - * |
|
| 111 | - * @return void |
|
| 112 | - */ |
|
| 113 | - public static function library($class, $instance = null, array $params = array()){ |
|
| 114 | - $logger = static::getLogger(); |
|
| 115 | - $class = str_ireplace('.php', '', $class); |
|
| 116 | - $class = trim($class, '/\\'); |
|
| 117 | - $file = ucfirst($class) .'.php'; |
|
| 118 | - $logger->debug('Loading library [' . $class . '] ...'); |
|
| 119 | - if (! $instance){ |
|
| 120 | - $instance = self::getModelLibraryInstanceName($class); |
|
| 121 | - } |
|
| 122 | - if (isset(static::$loaded[$instance])){ |
|
| 123 | - $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 124 | - return; |
|
| 125 | - } |
|
| 126 | - $obj = & get_instance(); |
|
| 127 | - //Check and load Database library |
|
| 128 | - if (strtolower($class) == 'database'){ |
|
| 129 | - $logger->info('This is the Database library ...'); |
|
| 130 | - $obj->{$instance} = & class_loader('Database', 'classes/database', $params); |
|
| 131 | - static::$loaded[$instance] = $class; |
|
| 132 | - $logger->info('Library Database loaded successfully.'); |
|
| 133 | - return; |
|
| 134 | - } |
|
| 135 | - $libraryFilePath = null; |
|
| 136 | - $logger->debug('Check if this is a system library ...'); |
|
| 137 | - if (file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 138 | - $libraryFilePath = CORE_LIBRARY_PATH . $file; |
|
| 139 | - $class = ucfirst($class); |
|
| 140 | - $logger->info('This library is a system library'); |
|
| 141 | - } |
|
| 142 | - else{ |
|
| 143 | - $logger->info('This library is not a system library'); |
|
| 144 | - //first check if this library is in the module |
|
| 145 | - $libraryFilePath = self::getLibraryPathUsingModuleInfo($class); |
|
| 146 | - //*************** |
|
| 147 | - } |
|
| 148 | - if (! $libraryFilePath && file_exists(LIBRARY_PATH . $file)){ |
|
| 149 | - $libraryFilePath = LIBRARY_PATH . $file; |
|
| 150 | - } |
|
| 151 | - $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
|
| 152 | - //************************* |
|
| 153 | - self::loadLibrary($libraryFilePath, $class, $instance, $params); |
|
| 154 | - } |
|
| 104 | + /** |
|
| 105 | + * Load the library class |
|
| 106 | + * |
|
| 107 | + * @param string $class the library class name to be loaded |
|
| 108 | + * @param string $instance the instance name to use in super object |
|
| 109 | + * @param mixed $params the arguments to pass to the constructor |
|
| 110 | + * |
|
| 111 | + * @return void |
|
| 112 | + */ |
|
| 113 | + public static function library($class, $instance = null, array $params = array()){ |
|
| 114 | + $logger = static::getLogger(); |
|
| 115 | + $class = str_ireplace('.php', '', $class); |
|
| 116 | + $class = trim($class, '/\\'); |
|
| 117 | + $file = ucfirst($class) .'.php'; |
|
| 118 | + $logger->debug('Loading library [' . $class . '] ...'); |
|
| 119 | + if (! $instance){ |
|
| 120 | + $instance = self::getModelLibraryInstanceName($class); |
|
| 121 | + } |
|
| 122 | + if (isset(static::$loaded[$instance])){ |
|
| 123 | + $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 124 | + return; |
|
| 125 | + } |
|
| 126 | + $obj = & get_instance(); |
|
| 127 | + //Check and load Database library |
|
| 128 | + if (strtolower($class) == 'database'){ |
|
| 129 | + $logger->info('This is the Database library ...'); |
|
| 130 | + $obj->{$instance} = & class_loader('Database', 'classes/database', $params); |
|
| 131 | + static::$loaded[$instance] = $class; |
|
| 132 | + $logger->info('Library Database loaded successfully.'); |
|
| 133 | + return; |
|
| 134 | + } |
|
| 135 | + $libraryFilePath = null; |
|
| 136 | + $logger->debug('Check if this is a system library ...'); |
|
| 137 | + if (file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 138 | + $libraryFilePath = CORE_LIBRARY_PATH . $file; |
|
| 139 | + $class = ucfirst($class); |
|
| 140 | + $logger->info('This library is a system library'); |
|
| 141 | + } |
|
| 142 | + else{ |
|
| 143 | + $logger->info('This library is not a system library'); |
|
| 144 | + //first check if this library is in the module |
|
| 145 | + $libraryFilePath = self::getLibraryPathUsingModuleInfo($class); |
|
| 146 | + //*************** |
|
| 147 | + } |
|
| 148 | + if (! $libraryFilePath && file_exists(LIBRARY_PATH . $file)){ |
|
| 149 | + $libraryFilePath = LIBRARY_PATH . $file; |
|
| 150 | + } |
|
| 151 | + $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
|
| 152 | + //************************* |
|
| 153 | + self::loadLibrary($libraryFilePath, $class, $instance, $params); |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * Load the helper |
|
| 158 | - * |
|
| 159 | - * @param string $function the helper name to be loaded |
|
| 160 | - * |
|
| 161 | - * @return void |
|
| 162 | - */ |
|
| 163 | - public static function functions($function){ |
|
| 164 | - $logger = static::getLogger(); |
|
| 165 | - $function = str_ireplace('.php', '', $function); |
|
| 166 | - $function = trim($function, '/\\'); |
|
| 167 | - $function = str_ireplace('function_', '', $function); |
|
| 168 | - $file = 'function_'.$function.'.php'; |
|
| 169 | - $logger->debug('Loading helper [' . $function . '] ...'); |
|
| 170 | - if (isset(static::$loaded['function_' . $function])){ |
|
| 171 | - $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
|
| 172 | - return; |
|
| 173 | - } |
|
| 174 | - $functionFilePath = null; |
|
| 175 | - //first check if this helper is in the module |
|
| 176 | - $logger->debug('Checking helper [' . $function . '] from module list ...'); |
|
| 177 | - $moduleInfo = self::getModuleInfoForFunction($function); |
|
| 178 | - $module = $moduleInfo['module']; |
|
| 179 | - $function = $moduleInfo['function']; |
|
| 180 | - if(! empty($moduleInfo['file'])){ |
|
| 181 | - $file = $moduleInfo['file']; |
|
| 182 | - } |
|
| 183 | - $moduleFunctionPath = Module::findFunctionFullPath($function, $module); |
|
| 184 | - if ($moduleFunctionPath){ |
|
| 185 | - $logger->info('Found helper [' . $function . '] from module [' .$module. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
|
| 186 | - $functionFilePath = $moduleFunctionPath; |
|
| 187 | - } |
|
| 188 | - else{ |
|
| 189 | - $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
|
| 190 | - } |
|
| 191 | - if (! $functionFilePath){ |
|
| 192 | - $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
|
| 193 | - foreach($searchDir as $dir){ |
|
| 194 | - $filePath = $dir . $file; |
|
| 195 | - if (file_exists($filePath)){ |
|
| 196 | - $functionFilePath = $filePath; |
|
| 197 | - //is already found not to continue |
|
| 198 | - break; |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
|
| 203 | - if ($functionFilePath){ |
|
| 204 | - require_once $functionFilePath; |
|
| 205 | - static::$loaded['function_' . $function] = $functionFilePath; |
|
| 206 | - $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
|
| 207 | - } |
|
| 208 | - else{ |
|
| 209 | - show_error('Unable to find helper file [' . $file . ']'); |
|
| 210 | - } |
|
| 211 | - } |
|
| 156 | + /** |
|
| 157 | + * Load the helper |
|
| 158 | + * |
|
| 159 | + * @param string $function the helper name to be loaded |
|
| 160 | + * |
|
| 161 | + * @return void |
|
| 162 | + */ |
|
| 163 | + public static function functions($function){ |
|
| 164 | + $logger = static::getLogger(); |
|
| 165 | + $function = str_ireplace('.php', '', $function); |
|
| 166 | + $function = trim($function, '/\\'); |
|
| 167 | + $function = str_ireplace('function_', '', $function); |
|
| 168 | + $file = 'function_'.$function.'.php'; |
|
| 169 | + $logger->debug('Loading helper [' . $function . '] ...'); |
|
| 170 | + if (isset(static::$loaded['function_' . $function])){ |
|
| 171 | + $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
|
| 172 | + return; |
|
| 173 | + } |
|
| 174 | + $functionFilePath = null; |
|
| 175 | + //first check if this helper is in the module |
|
| 176 | + $logger->debug('Checking helper [' . $function . '] from module list ...'); |
|
| 177 | + $moduleInfo = self::getModuleInfoForFunction($function); |
|
| 178 | + $module = $moduleInfo['module']; |
|
| 179 | + $function = $moduleInfo['function']; |
|
| 180 | + if(! empty($moduleInfo['file'])){ |
|
| 181 | + $file = $moduleInfo['file']; |
|
| 182 | + } |
|
| 183 | + $moduleFunctionPath = Module::findFunctionFullPath($function, $module); |
|
| 184 | + if ($moduleFunctionPath){ |
|
| 185 | + $logger->info('Found helper [' . $function . '] from module [' .$module. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
|
| 186 | + $functionFilePath = $moduleFunctionPath; |
|
| 187 | + } |
|
| 188 | + else{ |
|
| 189 | + $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
|
| 190 | + } |
|
| 191 | + if (! $functionFilePath){ |
|
| 192 | + $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
|
| 193 | + foreach($searchDir as $dir){ |
|
| 194 | + $filePath = $dir . $file; |
|
| 195 | + if (file_exists($filePath)){ |
|
| 196 | + $functionFilePath = $filePath; |
|
| 197 | + //is already found not to continue |
|
| 198 | + break; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
|
| 203 | + if ($functionFilePath){ |
|
| 204 | + require_once $functionFilePath; |
|
| 205 | + static::$loaded['function_' . $function] = $functionFilePath; |
|
| 206 | + $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
|
| 207 | + } |
|
| 208 | + else{ |
|
| 209 | + show_error('Unable to find helper file [' . $file . ']'); |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - /** |
|
| 214 | - * Load the configuration file |
|
| 215 | - * |
|
| 216 | - * @param string $filename the configuration filename located at CONFIG_PATH or MODULE_PATH/config |
|
| 217 | - * |
|
| 218 | - * @return void |
|
| 219 | - */ |
|
| 220 | - public static function config($filename){ |
|
| 221 | - $logger = static::getLogger(); |
|
| 222 | - $filename = str_ireplace('.php', '', $filename); |
|
| 223 | - $filename = trim($filename, '/\\'); |
|
| 224 | - $filename = str_ireplace('config_', '', $filename); |
|
| 225 | - $file = 'config_'.$filename.'.php'; |
|
| 226 | - $logger->debug('Loading configuration [' . $filename . '] ...'); |
|
| 227 | - if (isset(static::$loaded['config_' . $filename])){ |
|
| 228 | - $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
|
| 229 | - return; |
|
| 230 | - } |
|
| 231 | - $configFilePath = CONFIG_PATH . $file; |
|
| 232 | - //first check if this config is in the module |
|
| 233 | - $logger->debug('Checking config [' . $filename . '] from module list ...'); |
|
| 234 | - $moduleInfo = self::getModuleInfoForConfig($filename); |
|
| 235 | - $module = $moduleInfo['module']; |
|
| 236 | - $filename = $moduleInfo['filename']; |
|
| 237 | - $moduleConfigPath = Module::findConfigFullPath($filename, $module); |
|
| 238 | - if ($moduleConfigPath){ |
|
| 239 | - $logger->info('Found config [' . $filename . '] from module [' .$module. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
|
| 240 | - $configFilePath = $moduleConfigPath; |
|
| 241 | - } |
|
| 242 | - else{ |
|
| 243 | - $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
|
| 244 | - } |
|
| 245 | - $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
|
| 246 | - $config = array(); |
|
| 247 | - if (file_exists($configFilePath)){ |
|
| 248 | - require_once $configFilePath; |
|
| 249 | - if (! empty($config) && is_array($config)){ |
|
| 250 | - Config::setAll($config); |
|
| 251 | - static::$loaded['config_' . $filename] = $configFilePath; |
|
| 252 | - $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
|
| 253 | - $logger->info('The custom application configuration loaded are listed below: ' . stringfy_vars($config)); |
|
| 254 | - unset($config); |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - else{ |
|
| 258 | - show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 259 | - } |
|
| 260 | - } |
|
| 213 | + /** |
|
| 214 | + * Load the configuration file |
|
| 215 | + * |
|
| 216 | + * @param string $filename the configuration filename located at CONFIG_PATH or MODULE_PATH/config |
|
| 217 | + * |
|
| 218 | + * @return void |
|
| 219 | + */ |
|
| 220 | + public static function config($filename){ |
|
| 221 | + $logger = static::getLogger(); |
|
| 222 | + $filename = str_ireplace('.php', '', $filename); |
|
| 223 | + $filename = trim($filename, '/\\'); |
|
| 224 | + $filename = str_ireplace('config_', '', $filename); |
|
| 225 | + $file = 'config_'.$filename.'.php'; |
|
| 226 | + $logger->debug('Loading configuration [' . $filename . '] ...'); |
|
| 227 | + if (isset(static::$loaded['config_' . $filename])){ |
|
| 228 | + $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
|
| 229 | + return; |
|
| 230 | + } |
|
| 231 | + $configFilePath = CONFIG_PATH . $file; |
|
| 232 | + //first check if this config is in the module |
|
| 233 | + $logger->debug('Checking config [' . $filename . '] from module list ...'); |
|
| 234 | + $moduleInfo = self::getModuleInfoForConfig($filename); |
|
| 235 | + $module = $moduleInfo['module']; |
|
| 236 | + $filename = $moduleInfo['filename']; |
|
| 237 | + $moduleConfigPath = Module::findConfigFullPath($filename, $module); |
|
| 238 | + if ($moduleConfigPath){ |
|
| 239 | + $logger->info('Found config [' . $filename . '] from module [' .$module. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
|
| 240 | + $configFilePath = $moduleConfigPath; |
|
| 241 | + } |
|
| 242 | + else{ |
|
| 243 | + $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
|
| 244 | + } |
|
| 245 | + $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
|
| 246 | + $config = array(); |
|
| 247 | + if (file_exists($configFilePath)){ |
|
| 248 | + require_once $configFilePath; |
|
| 249 | + if (! empty($config) && is_array($config)){ |
|
| 250 | + Config::setAll($config); |
|
| 251 | + static::$loaded['config_' . $filename] = $configFilePath; |
|
| 252 | + $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
|
| 253 | + $logger->info('The custom application configuration loaded are listed below: ' . stringfy_vars($config)); |
|
| 254 | + unset($config); |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + else{ |
|
| 258 | + show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | 261 | |
| 262 | 262 | |
| 263 | - /** |
|
| 264 | - * Load the language |
|
| 265 | - * |
|
| 266 | - * @param string $language the language name to be loaded |
|
| 267 | - * |
|
| 268 | - * @return void |
|
| 269 | - */ |
|
| 270 | - public static function lang($language){ |
|
| 271 | - $logger = static::getLogger(); |
|
| 272 | - $language = str_ireplace('.php', '', $language); |
|
| 273 | - $language = trim($language, '/\\'); |
|
| 274 | - $language = str_ireplace('lang_', '', $language); |
|
| 275 | - $file = 'lang_'.$language.'.php'; |
|
| 276 | - $logger->debug('Loading language [' . $language . '] ...'); |
|
| 277 | - if (isset(static::$loaded['lang_' . $language])){ |
|
| 278 | - $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
|
| 279 | - return; |
|
| 280 | - } |
|
| 281 | - //get the current language |
|
| 282 | - $appLang = self::getAppLang(); |
|
| 283 | - $languageFilePath = null; |
|
| 284 | - //first check if this language is in the module |
|
| 285 | - $logger->debug('Checking language [' . $language . '] from module list ...'); |
|
| 286 | - $moduleInfo = self::getModuleInfoForLanguage($language); |
|
| 287 | - $module = $moduleInfo['module']; |
|
| 288 | - $language = $moduleInfo['language']; |
|
| 289 | - if(! empty($moduleInfo['file'])){ |
|
| 290 | - $file = $moduleInfo['file']; |
|
| 291 | - } |
|
| 292 | - $moduleLanguagePath = Module::findLanguageFullPath($language, $appLang, $module); |
|
| 293 | - if ($moduleLanguagePath){ |
|
| 294 | - $logger->info('Found language [' . $language . '] from module [' .$module. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 295 | - $languageFilePath = $moduleLanguagePath; |
|
| 296 | - } |
|
| 297 | - else{ |
|
| 298 | - $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
|
| 299 | - } |
|
| 300 | - if (! $languageFilePath){ |
|
| 301 | - $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
|
| 302 | - foreach($searchDir as $dir){ |
|
| 303 | - $filePath = $dir . $appLang . DS . $file; |
|
| 304 | - if (file_exists($filePath)){ |
|
| 305 | - $languageFilePath = $filePath; |
|
| 306 | - //already found no need continue |
|
| 307 | - break; |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
|
| 312 | - self::loadLanguage($languageFilePath, $language); |
|
| 313 | - } |
|
| 263 | + /** |
|
| 264 | + * Load the language |
|
| 265 | + * |
|
| 266 | + * @param string $language the language name to be loaded |
|
| 267 | + * |
|
| 268 | + * @return void |
|
| 269 | + */ |
|
| 270 | + public static function lang($language){ |
|
| 271 | + $logger = static::getLogger(); |
|
| 272 | + $language = str_ireplace('.php', '', $language); |
|
| 273 | + $language = trim($language, '/\\'); |
|
| 274 | + $language = str_ireplace('lang_', '', $language); |
|
| 275 | + $file = 'lang_'.$language.'.php'; |
|
| 276 | + $logger->debug('Loading language [' . $language . '] ...'); |
|
| 277 | + if (isset(static::$loaded['lang_' . $language])){ |
|
| 278 | + $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
|
| 279 | + return; |
|
| 280 | + } |
|
| 281 | + //get the current language |
|
| 282 | + $appLang = self::getAppLang(); |
|
| 283 | + $languageFilePath = null; |
|
| 284 | + //first check if this language is in the module |
|
| 285 | + $logger->debug('Checking language [' . $language . '] from module list ...'); |
|
| 286 | + $moduleInfo = self::getModuleInfoForLanguage($language); |
|
| 287 | + $module = $moduleInfo['module']; |
|
| 288 | + $language = $moduleInfo['language']; |
|
| 289 | + if(! empty($moduleInfo['file'])){ |
|
| 290 | + $file = $moduleInfo['file']; |
|
| 291 | + } |
|
| 292 | + $moduleLanguagePath = Module::findLanguageFullPath($language, $appLang, $module); |
|
| 293 | + if ($moduleLanguagePath){ |
|
| 294 | + $logger->info('Found language [' . $language . '] from module [' .$module. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 295 | + $languageFilePath = $moduleLanguagePath; |
|
| 296 | + } |
|
| 297 | + else{ |
|
| 298 | + $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
|
| 299 | + } |
|
| 300 | + if (! $languageFilePath){ |
|
| 301 | + $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
|
| 302 | + foreach($searchDir as $dir){ |
|
| 303 | + $filePath = $dir . $appLang . DS . $file; |
|
| 304 | + if (file_exists($filePath)){ |
|
| 305 | + $languageFilePath = $filePath; |
|
| 306 | + //already found no need continue |
|
| 307 | + break; |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
|
| 312 | + self::loadLanguage($languageFilePath, $language); |
|
| 313 | + } |
|
| 314 | 314 | |
| 315 | - /** |
|
| 316 | - * Return the current app language by default will use the value from cookie |
|
| 317 | - * if can not found will use the default value from configuration |
|
| 318 | - * @return string the app language like "en", "fr" |
|
| 319 | - */ |
|
| 320 | - protected static function getAppLang(){ |
|
| 321 | - //determine the current language |
|
| 322 | - $appLang = get_config('default_language'); |
|
| 323 | - //if the language exists in the cookie use it |
|
| 324 | - $cfgKey = get_config('language_cookie_name'); |
|
| 325 | - $objCookie = & class_loader('Cookie'); |
|
| 326 | - $cookieLang = $objCookie->get($cfgKey); |
|
| 327 | - if ($cookieLang){ |
|
| 328 | - $appLang = $cookieLang; |
|
| 329 | - } |
|
| 330 | - return $appLang; |
|
| 331 | - } |
|
| 332 | - /** |
|
| 333 | - * Get the module information for the model and library to load |
|
| 334 | - * @param string $class the full class name like moduleName/className, className, |
|
| 335 | - * @return array the module information |
|
| 336 | - * array( |
|
| 337 | - * 'module'=> 'module_name' |
|
| 338 | - * 'class' => 'class_name' |
|
| 339 | - * ) |
|
| 340 | - */ |
|
| 341 | - protected static function getModuleInfoForModelLibrary($class){ |
|
| 342 | - $module = null; |
|
| 343 | - $obj = & get_instance(); |
|
| 344 | - if (strpos($class, '/') !== false){ |
|
| 345 | - $path = explode('/', $class); |
|
| 346 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 347 | - $module = $path[0]; |
|
| 348 | - $class = ucfirst($path[1]); |
|
| 349 | - } |
|
| 350 | - } |
|
| 351 | - else{ |
|
| 352 | - $class = ucfirst($class); |
|
| 353 | - } |
|
| 354 | - if (! $module && !empty($obj->moduleName)){ |
|
| 355 | - $module = $obj->moduleName; |
|
| 356 | - } |
|
| 357 | - return array( |
|
| 358 | - 'class' => $class, |
|
| 359 | - 'module' => $module |
|
| 360 | - ); |
|
| 361 | - } |
|
| 315 | + /** |
|
| 316 | + * Return the current app language by default will use the value from cookie |
|
| 317 | + * if can not found will use the default value from configuration |
|
| 318 | + * @return string the app language like "en", "fr" |
|
| 319 | + */ |
|
| 320 | + protected static function getAppLang(){ |
|
| 321 | + //determine the current language |
|
| 322 | + $appLang = get_config('default_language'); |
|
| 323 | + //if the language exists in the cookie use it |
|
| 324 | + $cfgKey = get_config('language_cookie_name'); |
|
| 325 | + $objCookie = & class_loader('Cookie'); |
|
| 326 | + $cookieLang = $objCookie->get($cfgKey); |
|
| 327 | + if ($cookieLang){ |
|
| 328 | + $appLang = $cookieLang; |
|
| 329 | + } |
|
| 330 | + return $appLang; |
|
| 331 | + } |
|
| 332 | + /** |
|
| 333 | + * Get the module information for the model and library to load |
|
| 334 | + * @param string $class the full class name like moduleName/className, className, |
|
| 335 | + * @return array the module information |
|
| 336 | + * array( |
|
| 337 | + * 'module'=> 'module_name' |
|
| 338 | + * 'class' => 'class_name' |
|
| 339 | + * ) |
|
| 340 | + */ |
|
| 341 | + protected static function getModuleInfoForModelLibrary($class){ |
|
| 342 | + $module = null; |
|
| 343 | + $obj = & get_instance(); |
|
| 344 | + if (strpos($class, '/') !== false){ |
|
| 345 | + $path = explode('/', $class); |
|
| 346 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 347 | + $module = $path[0]; |
|
| 348 | + $class = ucfirst($path[1]); |
|
| 349 | + } |
|
| 350 | + } |
|
| 351 | + else{ |
|
| 352 | + $class = ucfirst($class); |
|
| 353 | + } |
|
| 354 | + if (! $module && !empty($obj->moduleName)){ |
|
| 355 | + $module = $obj->moduleName; |
|
| 356 | + } |
|
| 357 | + return array( |
|
| 358 | + 'class' => $class, |
|
| 359 | + 'module' => $module |
|
| 360 | + ); |
|
| 361 | + } |
|
| 362 | 362 | |
| 363 | - /** |
|
| 364 | - * Get the module information for the function to load |
|
| 365 | - * @param string $function the function name like moduleName/functionName, functionName, |
|
| 366 | - * @return array the module information |
|
| 367 | - * array( |
|
| 368 | - * 'module'=> 'module_name' |
|
| 369 | - * 'function' => 'function' |
|
| 370 | - * 'file' => 'file' |
|
| 371 | - * ) |
|
| 372 | - */ |
|
| 373 | - protected static function getModuleInfoForFunction($function){ |
|
| 374 | - $module = null; |
|
| 375 | - $file = null; |
|
| 376 | - $obj = & get_instance(); |
|
| 377 | - //check if the request class contains module name |
|
| 378 | - if (strpos($function, '/') !== false){ |
|
| 379 | - $path = explode('/', $function); |
|
| 380 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 381 | - $module = $path[0]; |
|
| 382 | - $function = 'function_' . $path[1]; |
|
| 383 | - $file = $path[0] . DS . $function.'.php'; |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - if (! $module && !empty($obj->moduleName)){ |
|
| 387 | - $module = $obj->moduleName; |
|
| 388 | - } |
|
| 389 | - return array( |
|
| 390 | - 'function' => $function, |
|
| 391 | - 'module' => $module, |
|
| 392 | - 'file' => $file |
|
| 393 | - ); |
|
| 394 | - } |
|
| 363 | + /** |
|
| 364 | + * Get the module information for the function to load |
|
| 365 | + * @param string $function the function name like moduleName/functionName, functionName, |
|
| 366 | + * @return array the module information |
|
| 367 | + * array( |
|
| 368 | + * 'module'=> 'module_name' |
|
| 369 | + * 'function' => 'function' |
|
| 370 | + * 'file' => 'file' |
|
| 371 | + * ) |
|
| 372 | + */ |
|
| 373 | + protected static function getModuleInfoForFunction($function){ |
|
| 374 | + $module = null; |
|
| 375 | + $file = null; |
|
| 376 | + $obj = & get_instance(); |
|
| 377 | + //check if the request class contains module name |
|
| 378 | + if (strpos($function, '/') !== false){ |
|
| 379 | + $path = explode('/', $function); |
|
| 380 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 381 | + $module = $path[0]; |
|
| 382 | + $function = 'function_' . $path[1]; |
|
| 383 | + $file = $path[0] . DS . $function.'.php'; |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + if (! $module && !empty($obj->moduleName)){ |
|
| 387 | + $module = $obj->moduleName; |
|
| 388 | + } |
|
| 389 | + return array( |
|
| 390 | + 'function' => $function, |
|
| 391 | + 'module' => $module, |
|
| 392 | + 'file' => $file |
|
| 393 | + ); |
|
| 394 | + } |
|
| 395 | 395 | |
| 396 | - /** |
|
| 397 | - * Get the module information for the language to load |
|
| 398 | - * @param string $language the language name like moduleName/languageName, languageName, |
|
| 399 | - * @return array the module information |
|
| 400 | - * array( |
|
| 401 | - * 'module'=> 'module_name' |
|
| 402 | - * 'language' => 'language' |
|
| 403 | - * 'file' => 'file' |
|
| 404 | - * ) |
|
| 405 | - */ |
|
| 406 | - protected static function getModuleInfoForLanguage($language){ |
|
| 407 | - $module = null; |
|
| 408 | - $file = null; |
|
| 409 | - $obj = & get_instance(); |
|
| 410 | - //check if the request class contains module name |
|
| 411 | - if (strpos($language, '/') !== false){ |
|
| 412 | - $path = explode('/', $language); |
|
| 413 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 414 | - $module = $path[0]; |
|
| 415 | - $language = 'lang_' . $path[1] . '.php'; |
|
| 416 | - $file = $path[0] . DS .$language; |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - if (! $module && !empty($obj->moduleName)){ |
|
| 420 | - $module = $obj->moduleName; |
|
| 421 | - } |
|
| 422 | - return array( |
|
| 423 | - 'language' => $language, |
|
| 424 | - 'module' => $module, |
|
| 425 | - 'file' => $file |
|
| 426 | - ); |
|
| 427 | - } |
|
| 396 | + /** |
|
| 397 | + * Get the module information for the language to load |
|
| 398 | + * @param string $language the language name like moduleName/languageName, languageName, |
|
| 399 | + * @return array the module information |
|
| 400 | + * array( |
|
| 401 | + * 'module'=> 'module_name' |
|
| 402 | + * 'language' => 'language' |
|
| 403 | + * 'file' => 'file' |
|
| 404 | + * ) |
|
| 405 | + */ |
|
| 406 | + protected static function getModuleInfoForLanguage($language){ |
|
| 407 | + $module = null; |
|
| 408 | + $file = null; |
|
| 409 | + $obj = & get_instance(); |
|
| 410 | + //check if the request class contains module name |
|
| 411 | + if (strpos($language, '/') !== false){ |
|
| 412 | + $path = explode('/', $language); |
|
| 413 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 414 | + $module = $path[0]; |
|
| 415 | + $language = 'lang_' . $path[1] . '.php'; |
|
| 416 | + $file = $path[0] . DS .$language; |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + if (! $module && !empty($obj->moduleName)){ |
|
| 420 | + $module = $obj->moduleName; |
|
| 421 | + } |
|
| 422 | + return array( |
|
| 423 | + 'language' => $language, |
|
| 424 | + 'module' => $module, |
|
| 425 | + 'file' => $file |
|
| 426 | + ); |
|
| 427 | + } |
|
| 428 | 428 | |
| 429 | 429 | |
| 430 | - /** |
|
| 431 | - * Get the module information for the config to load |
|
| 432 | - * @param string $filename the filename of the configuration file, |
|
| 433 | - * @return array the module information |
|
| 434 | - * array( |
|
| 435 | - * 'module'=> 'module_name' |
|
| 436 | - * 'filename' => 'filename' |
|
| 437 | - * ) |
|
| 438 | - */ |
|
| 439 | - protected static function getModuleInfoForConfig($filename){ |
|
| 440 | - $module = null; |
|
| 441 | - $obj = & get_instance(); |
|
| 442 | - //check if the request class contains module name |
|
| 443 | - if (strpos($filename, '/') !== false){ |
|
| 444 | - $path = explode('/', $filename); |
|
| 445 | - if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 446 | - $module = $path[0]; |
|
| 447 | - $filename = $path[1] . '.php'; |
|
| 448 | - } |
|
| 449 | - } |
|
| 450 | - if (! $module && !empty($obj->moduleName)){ |
|
| 451 | - $module = $obj->moduleName; |
|
| 452 | - } |
|
| 453 | - return array( |
|
| 454 | - 'filename' => $filename, |
|
| 455 | - 'module' => $module |
|
| 456 | - ); |
|
| 457 | - } |
|
| 430 | + /** |
|
| 431 | + * Get the module information for the config to load |
|
| 432 | + * @param string $filename the filename of the configuration file, |
|
| 433 | + * @return array the module information |
|
| 434 | + * array( |
|
| 435 | + * 'module'=> 'module_name' |
|
| 436 | + * 'filename' => 'filename' |
|
| 437 | + * ) |
|
| 438 | + */ |
|
| 439 | + protected static function getModuleInfoForConfig($filename){ |
|
| 440 | + $module = null; |
|
| 441 | + $obj = & get_instance(); |
|
| 442 | + //check if the request class contains module name |
|
| 443 | + if (strpos($filename, '/') !== false){ |
|
| 444 | + $path = explode('/', $filename); |
|
| 445 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 446 | + $module = $path[0]; |
|
| 447 | + $filename = $path[1] . '.php'; |
|
| 448 | + } |
|
| 449 | + } |
|
| 450 | + if (! $module && !empty($obj->moduleName)){ |
|
| 451 | + $module = $obj->moduleName; |
|
| 452 | + } |
|
| 453 | + return array( |
|
| 454 | + 'filename' => $filename, |
|
| 455 | + 'module' => $module |
|
| 456 | + ); |
|
| 457 | + } |
|
| 458 | 458 | |
| 459 | - /** |
|
| 460 | - * Get the name of model or library instance if is null |
|
| 461 | - * @param string $class the class name to determine the instance |
|
| 462 | - * @return string the instance name |
|
| 463 | - */ |
|
| 464 | - protected static function getModelLibraryInstanceName($class){ |
|
| 465 | - //for module |
|
| 466 | - $instance = null; |
|
| 467 | - if (strpos($class, '/') !== false){ |
|
| 468 | - $path = explode('/', $class); |
|
| 469 | - if (isset($path[1])){ |
|
| 470 | - $instance = strtolower($path[1]); |
|
| 471 | - } |
|
| 472 | - } |
|
| 473 | - else{ |
|
| 474 | - $instance = strtolower($class); |
|
| 475 | - } |
|
| 476 | - return $instance; |
|
| 477 | - } |
|
| 459 | + /** |
|
| 460 | + * Get the name of model or library instance if is null |
|
| 461 | + * @param string $class the class name to determine the instance |
|
| 462 | + * @return string the instance name |
|
| 463 | + */ |
|
| 464 | + protected static function getModelLibraryInstanceName($class){ |
|
| 465 | + //for module |
|
| 466 | + $instance = null; |
|
| 467 | + if (strpos($class, '/') !== false){ |
|
| 468 | + $path = explode('/', $class); |
|
| 469 | + if (isset($path[1])){ |
|
| 470 | + $instance = strtolower($path[1]); |
|
| 471 | + } |
|
| 472 | + } |
|
| 473 | + else{ |
|
| 474 | + $instance = strtolower($class); |
|
| 475 | + } |
|
| 476 | + return $instance; |
|
| 477 | + } |
|
| 478 | 478 | |
| 479 | - /** |
|
| 480 | - * Get the library file path using the module information |
|
| 481 | - * @param string $class the class name |
|
| 482 | - * @return string|null the library file path otherwise null will be returned |
|
| 483 | - */ |
|
| 484 | - protected static function getLibraryPathUsingModuleInfo($class){ |
|
| 485 | - $logger = static::getLogger(); |
|
| 486 | - $libraryFilePath = null; |
|
| 487 | - $logger->debug('Checking library [' . $class . '] from module list ...'); |
|
| 488 | - $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 489 | - $module = $moduleInfo['module']; |
|
| 490 | - $class = $moduleInfo['class']; |
|
| 491 | - $moduleLibraryPath = Module::findLibraryFullPath($class, $module); |
|
| 492 | - if ($moduleLibraryPath){ |
|
| 493 | - $logger->info('Found library [' . $class . '] from module [' .$module. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 494 | - $libraryFilePath = $moduleLibraryPath; |
|
| 495 | - } |
|
| 496 | - else{ |
|
| 497 | - $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
|
| 498 | - } |
|
| 499 | - return $libraryFilePath; |
|
| 500 | - } |
|
| 479 | + /** |
|
| 480 | + * Get the library file path using the module information |
|
| 481 | + * @param string $class the class name |
|
| 482 | + * @return string|null the library file path otherwise null will be returned |
|
| 483 | + */ |
|
| 484 | + protected static function getLibraryPathUsingModuleInfo($class){ |
|
| 485 | + $logger = static::getLogger(); |
|
| 486 | + $libraryFilePath = null; |
|
| 487 | + $logger->debug('Checking library [' . $class . '] from module list ...'); |
|
| 488 | + $moduleInfo = self::getModuleInfoForModelLibrary($class); |
|
| 489 | + $module = $moduleInfo['module']; |
|
| 490 | + $class = $moduleInfo['class']; |
|
| 491 | + $moduleLibraryPath = Module::findLibraryFullPath($class, $module); |
|
| 492 | + if ($moduleLibraryPath){ |
|
| 493 | + $logger->info('Found library [' . $class . '] from module [' .$module. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 494 | + $libraryFilePath = $moduleLibraryPath; |
|
| 495 | + } |
|
| 496 | + else{ |
|
| 497 | + $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
|
| 498 | + } |
|
| 499 | + return $libraryFilePath; |
|
| 500 | + } |
|
| 501 | 501 | |
| 502 | - /** |
|
| 503 | - * Load the library |
|
| 504 | - * @param string $libraryFilePath the file path of the library to load |
|
| 505 | - * @param string $class the class name |
|
| 506 | - * @param string $instance the instance |
|
| 507 | - * @param array $params the parameter to use |
|
| 508 | - * @return void |
|
| 509 | - */ |
|
| 510 | - protected static function loadLibrary($libraryFilePath, $class, $instance, $params = array()){ |
|
| 511 | - if ($libraryFilePath){ |
|
| 512 | - $logger = static::getLogger(); |
|
| 513 | - require_once $libraryFilePath; |
|
| 514 | - if (class_exists($class)){ |
|
| 515 | - $c = $params ? new $class($params) : new $class(); |
|
| 516 | - $obj = & get_instance(); |
|
| 517 | - $obj->{$instance} = $c; |
|
| 518 | - static::$loaded[$instance] = $class; |
|
| 519 | - $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
|
| 520 | - } |
|
| 521 | - else{ |
|
| 522 | - show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 523 | - } |
|
| 524 | - } |
|
| 525 | - else{ |
|
| 526 | - show_error('Unable to find library class [' . $class . ']'); |
|
| 527 | - } |
|
| 528 | - } |
|
| 502 | + /** |
|
| 503 | + * Load the library |
|
| 504 | + * @param string $libraryFilePath the file path of the library to load |
|
| 505 | + * @param string $class the class name |
|
| 506 | + * @param string $instance the instance |
|
| 507 | + * @param array $params the parameter to use |
|
| 508 | + * @return void |
|
| 509 | + */ |
|
| 510 | + protected static function loadLibrary($libraryFilePath, $class, $instance, $params = array()){ |
|
| 511 | + if ($libraryFilePath){ |
|
| 512 | + $logger = static::getLogger(); |
|
| 513 | + require_once $libraryFilePath; |
|
| 514 | + if (class_exists($class)){ |
|
| 515 | + $c = $params ? new $class($params) : new $class(); |
|
| 516 | + $obj = & get_instance(); |
|
| 517 | + $obj->{$instance} = $c; |
|
| 518 | + static::$loaded[$instance] = $class; |
|
| 519 | + $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
|
| 520 | + } |
|
| 521 | + else{ |
|
| 522 | + show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 523 | + } |
|
| 524 | + } |
|
| 525 | + else{ |
|
| 526 | + show_error('Unable to find library class [' . $class . ']'); |
|
| 527 | + } |
|
| 528 | + } |
|
| 529 | 529 | |
| 530 | - /** |
|
| 531 | - * Load the language |
|
| 532 | - * @param string $languageFilePath the file path of the language to load |
|
| 533 | - * @param string $language the language name |
|
| 534 | - * @return void |
|
| 535 | - */ |
|
| 536 | - protected static function loadLanguage($languageFilePath, $language){ |
|
| 537 | - if ($languageFilePath){ |
|
| 538 | - $logger = static::getLogger(); |
|
| 539 | - $lang = array(); |
|
| 540 | - require_once $languageFilePath; |
|
| 541 | - if (! empty($lang) && is_array($lang)){ |
|
| 542 | - $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
|
| 543 | - //Note: may be here the class 'Lang' not yet loaded |
|
| 544 | - $langObj =& class_loader('Lang', 'classes'); |
|
| 545 | - $langObj->addLangMessages($lang); |
|
| 546 | - //free the memory |
|
| 547 | - unset($lang); |
|
| 548 | - } |
|
| 549 | - static::$loaded['lang_' . $language] = $languageFilePath; |
|
| 550 | - $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
|
| 551 | - } |
|
| 552 | - else{ |
|
| 553 | - show_error('Unable to find language [' . $language . ']'); |
|
| 554 | - } |
|
| 555 | - } |
|
| 530 | + /** |
|
| 531 | + * Load the language |
|
| 532 | + * @param string $languageFilePath the file path of the language to load |
|
| 533 | + * @param string $language the language name |
|
| 534 | + * @return void |
|
| 535 | + */ |
|
| 536 | + protected static function loadLanguage($languageFilePath, $language){ |
|
| 537 | + if ($languageFilePath){ |
|
| 538 | + $logger = static::getLogger(); |
|
| 539 | + $lang = array(); |
|
| 540 | + require_once $languageFilePath; |
|
| 541 | + if (! empty($lang) && is_array($lang)){ |
|
| 542 | + $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
|
| 543 | + //Note: may be here the class 'Lang' not yet loaded |
|
| 544 | + $langObj =& class_loader('Lang', 'classes'); |
|
| 545 | + $langObj->addLangMessages($lang); |
|
| 546 | + //free the memory |
|
| 547 | + unset($lang); |
|
| 548 | + } |
|
| 549 | + static::$loaded['lang_' . $language] = $languageFilePath; |
|
| 550 | + $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
|
| 551 | + } |
|
| 552 | + else{ |
|
| 553 | + show_error('Unable to find language [' . $language . ']'); |
|
| 554 | + } |
|
| 555 | + } |
|
| 556 | 556 | |
| 557 | - /** |
|
| 558 | - * Get all the autoload using the configuration file |
|
| 559 | - * @return array |
|
| 560 | - */ |
|
| 561 | - private 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_once 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 = Module::getModulesAutoloadConfig(); |
|
| 579 | - if (! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
|
| 580 | - $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
|
| 581 | - } |
|
| 582 | - return $autoloads; |
|
| 583 | - } |
|
| 557 | + /** |
|
| 558 | + * Get all the autoload using the configuration file |
|
| 559 | + * @return array |
|
| 560 | + */ |
|
| 561 | + private 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_once 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 = Module::getModulesAutoloadConfig(); |
|
| 579 | + if (! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
|
| 580 | + $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
|
| 581 | + } |
|
| 582 | + return $autoloads; |
|
| 583 | + } |
|
| 584 | 584 | |
| 585 | - /** |
|
| 586 | - * Load the autoload configuration |
|
| 587 | - * @return void |
|
| 588 | - */ |
|
| 589 | - private 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(); |
|
| 585 | + /** |
|
| 586 | + * Load the autoload configuration |
|
| 587 | + * @return void |
|
| 588 | + */ |
|
| 589 | + private 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 | 596 | |
| 597 | - $list = $this->getResourcesFromAutoloadConfig(); |
|
| 598 | - $autoloads = array_merge($autoloads, $list); |
|
| 597 | + $list = $this->getResourcesFromAutoloadConfig(); |
|
| 598 | + $autoloads = array_merge($autoloads, $list); |
|
| 599 | 599 | |
| 600 | - //config autoload |
|
| 601 | - $this->loadAutoloadResourcesArray('config', $autoloads['config']); |
|
| 600 | + //config autoload |
|
| 601 | + $this->loadAutoloadResourcesArray('config', $autoloads['config']); |
|
| 602 | 602 | |
| 603 | - //languages autoload |
|
| 604 | - $this->loadAutoloadResourcesArray('lang', $autoloads['languages']); |
|
| 603 | + //languages autoload |
|
| 604 | + $this->loadAutoloadResourcesArray('lang', $autoloads['languages']); |
|
| 605 | 605 | |
| 606 | - //libraries autoload |
|
| 607 | - $this->loadAutoloadResourcesArray('library', $autoloads['libraries']); |
|
| 606 | + //libraries autoload |
|
| 607 | + $this->loadAutoloadResourcesArray('library', $autoloads['libraries']); |
|
| 608 | 608 | |
| 609 | - //models autoload |
|
| 610 | - $this->loadAutoloadResourcesArray('model', $autoloads['models']); |
|
| 609 | + //models autoload |
|
| 610 | + $this->loadAutoloadResourcesArray('model', $autoloads['models']); |
|
| 611 | 611 | |
| 612 | - //functions autoload |
|
| 613 | - $this->loadAutoloadResourcesArray('functions', $autoloads['functions']); |
|
| 614 | - } |
|
| 612 | + //functions autoload |
|
| 613 | + $this->loadAutoloadResourcesArray('functions', $autoloads['functions']); |
|
| 614 | + } |
|
| 615 | 615 | |
| 616 | - /** |
|
| 617 | - * Load the resources autoload array |
|
| 618 | - * @param string $method this object method name to call |
|
| 619 | - * @param array $resources the resource to load |
|
| 620 | - * @return void |
|
| 621 | - */ |
|
| 622 | - private function loadAutoloadResourcesArray($method, array $resources){ |
|
| 623 | - foreach ($resources as $name) { |
|
| 624 | - $this->{$method}($name); |
|
| 625 | - } |
|
| 626 | - } |
|
| 627 | - } |
|
| 616 | + /** |
|
| 617 | + * Load the resources autoload array |
|
| 618 | + * @param string $method this object method name to call |
|
| 619 | + * @param array $resources the resource to load |
|
| 620 | + * @return void |
|
| 621 | + */ |
|
| 622 | + private function loadAutoloadResourcesArray($method, array $resources){ |
|
| 623 | + foreach ($resources as $name) { |
|
| 624 | + $this->{$method}($name); |
|
| 625 | + } |
|
| 626 | + } |
|
| 627 | + } |
|
@@ -1,293 +1,293 @@ |
||
| 1 | 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 | - */ |
|
| 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 | 26 | |
| 27 | - class Log{ |
|
| 27 | + class Log{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The defined constante for Log level |
|
| 31 | - */ |
|
| 32 | - const NONE = 99999999; |
|
| 33 | - const FATAL = 500; |
|
| 34 | - const ERROR = 400; |
|
| 35 | - const WARNING = 300; |
|
| 36 | - const INFO = 200; |
|
| 37 | - const DEBUG = 100; |
|
| 38 | - const ALL = -99999999; |
|
| 29 | + /** |
|
| 30 | + * The defined constante for Log level |
|
| 31 | + */ |
|
| 32 | + const NONE = 99999999; |
|
| 33 | + const FATAL = 500; |
|
| 34 | + const ERROR = 400; |
|
| 35 | + const WARNING = 300; |
|
| 36 | + const INFO = 200; |
|
| 37 | + const DEBUG = 100; |
|
| 38 | + const ALL = -99999999; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * The logger name |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - private $logger = 'ROOT_LOGGER'; |
|
| 40 | + /** |
|
| 41 | + * The logger name |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + private $logger = 'ROOT_LOGGER'; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * List of valid log level to be checked for the configuration |
|
| 48 | - * @var array |
|
| 49 | - */ |
|
| 50 | - private static $validConfigLevel = array('off', 'none', 'fatal', 'error', 'warning', 'warn', 'info', 'debug', 'all'); |
|
| 46 | + /** |
|
| 47 | + * List of valid log level to be checked for the configuration |
|
| 48 | + * @var array |
|
| 49 | + */ |
|
| 50 | + private static $validConfigLevel = array('off', 'none', 'fatal', 'error', 'warning', 'warn', 'info', 'debug', 'all'); |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Create new Log instance |
|
| 54 | - */ |
|
| 55 | - public function __construct(){ |
|
| 56 | - } |
|
| 52 | + /** |
|
| 53 | + * Create new Log instance |
|
| 54 | + */ |
|
| 55 | + public function __construct(){ |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Set the logger to identify each message in the log |
|
| 60 | - * @param string $logger the logger name |
|
| 61 | - */ |
|
| 62 | - public function setLogger($logger){ |
|
| 63 | - $this->logger = $logger; |
|
| 64 | - } |
|
| 58 | + /** |
|
| 59 | + * Set the logger to identify each message in the log |
|
| 60 | + * @param string $logger the logger name |
|
| 61 | + */ |
|
| 62 | + public function setLogger($logger){ |
|
| 63 | + $this->logger = $logger; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Save the fatal message in the log |
|
| 68 | - * @see Log::writeLog for more detail |
|
| 69 | - * @param string $message the log message to save |
|
| 70 | - */ |
|
| 71 | - public function fatal($message){ |
|
| 72 | - $this->writeLog($message, self::FATAL); |
|
| 73 | - } |
|
| 66 | + /** |
|
| 67 | + * Save the fatal message in the log |
|
| 68 | + * @see Log::writeLog for more detail |
|
| 69 | + * @param string $message the log message to save |
|
| 70 | + */ |
|
| 71 | + public function fatal($message){ |
|
| 72 | + $this->writeLog($message, self::FATAL); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Save the error message in the log |
|
| 77 | - * @see Log::writeLog for more detail |
|
| 78 | - * @param string $message the log message to save |
|
| 79 | - */ |
|
| 80 | - public function error($message){ |
|
| 81 | - $this->writeLog($message, self::ERROR); |
|
| 82 | - } |
|
| 75 | + /** |
|
| 76 | + * Save the error message in the log |
|
| 77 | + * @see Log::writeLog for more detail |
|
| 78 | + * @param string $message the log message to save |
|
| 79 | + */ |
|
| 80 | + public function error($message){ |
|
| 81 | + $this->writeLog($message, self::ERROR); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Save the warning message in the log |
|
| 86 | - * @see Log::writeLog for more detail |
|
| 87 | - * @param string $message the log message to save |
|
| 88 | - */ |
|
| 89 | - public function warning($message){ |
|
| 90 | - $this->writeLog($message, self::WARNING); |
|
| 91 | - } |
|
| 84 | + /** |
|
| 85 | + * Save the warning message in the log |
|
| 86 | + * @see Log::writeLog for more detail |
|
| 87 | + * @param string $message the log message to save |
|
| 88 | + */ |
|
| 89 | + public function warning($message){ |
|
| 90 | + $this->writeLog($message, self::WARNING); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Save the info message in the log |
|
| 95 | - * @see Log::writeLog for more detail |
|
| 96 | - * @param string $message the log message to save |
|
| 97 | - */ |
|
| 98 | - public function info($message){ |
|
| 99 | - $this->writeLog($message, self::INFO); |
|
| 100 | - } |
|
| 93 | + /** |
|
| 94 | + * Save the info message in the log |
|
| 95 | + * @see Log::writeLog for more detail |
|
| 96 | + * @param string $message the log message to save |
|
| 97 | + */ |
|
| 98 | + public function info($message){ |
|
| 99 | + $this->writeLog($message, self::INFO); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Save the debug message in the log |
|
| 104 | - * @see Log::writeLog for more detail |
|
| 105 | - * @param string $message the log message to save |
|
| 106 | - */ |
|
| 107 | - public function debug($message){ |
|
| 108 | - $this->writeLog($message, self::DEBUG); |
|
| 109 | - } |
|
| 102 | + /** |
|
| 103 | + * Save the debug message in the log |
|
| 104 | + * @see Log::writeLog for more detail |
|
| 105 | + * @param string $message the log message to save |
|
| 106 | + */ |
|
| 107 | + public function debug($message){ |
|
| 108 | + $this->writeLog($message, self::DEBUG); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Save the log message |
|
| 114 | - * @param string $message the log message to be saved |
|
| 115 | - * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
|
| 116 | - * to allow check the log level threshold. |
|
| 117 | - */ |
|
| 118 | - public function writeLog($message, $level = self::INFO){ |
|
| 119 | - $configLogLevel = get_config('log_level'); |
|
| 120 | - if(! $configLogLevel){ |
|
| 121 | - //so means no need log just stop here |
|
| 122 | - return; |
|
| 123 | - } |
|
| 124 | - //check config log level |
|
| 125 | - if(! self::isValidConfigLevel($configLogLevel)){ |
|
| 126 | - //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
| 127 | - show_error('Invalid config log level [' . $configLogLevel . '], the value must be one of the following: ' . implode(', ', array_map('strtoupper', self::$validConfigLevel)), $title = 'Log Config Error', $logging = false); |
|
| 128 | - } |
|
| 112 | + /** |
|
| 113 | + * Save the log message |
|
| 114 | + * @param string $message the log message to be saved |
|
| 115 | + * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
|
| 116 | + * to allow check the log level threshold. |
|
| 117 | + */ |
|
| 118 | + public function writeLog($message, $level = self::INFO){ |
|
| 119 | + $configLogLevel = get_config('log_level'); |
|
| 120 | + if(! $configLogLevel){ |
|
| 121 | + //so means no need log just stop here |
|
| 122 | + return; |
|
| 123 | + } |
|
| 124 | + //check config log level |
|
| 125 | + if(! self::isValidConfigLevel($configLogLevel)){ |
|
| 126 | + //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
| 127 | + show_error('Invalid config log level [' . $configLogLevel . '], the value must be one of the following: ' . implode(', ', array_map('strtoupper', self::$validConfigLevel)), $title = 'Log Config Error', $logging = false); |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - //check if config log_logger_name and current log can save log data |
|
| 131 | - if(! $this->canSaveLogDataForLogger()){ |
|
| 132 | - return; |
|
| 133 | - } |
|
| 130 | + //check if config log_logger_name and current log can save log data |
|
| 131 | + if(! $this->canSaveLogDataForLogger()){ |
|
| 132 | + return; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - //if $level is not an integer |
|
| 136 | - if(! is_numeric($level)){ |
|
| 137 | - $level = self::getLevelValue($level); |
|
| 138 | - } |
|
| 135 | + //if $level is not an integer |
|
| 136 | + if(! is_numeric($level)){ |
|
| 137 | + $level = self::getLevelValue($level); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - //check if can logging regarding the log level config |
|
| 141 | - $configLevel = self::getLevelValue($configLogLevel); |
|
| 142 | - if($configLevel > $level){ |
|
| 143 | - //can't log |
|
| 144 | - return; |
|
| 145 | - } |
|
| 146 | - //check log file and directory |
|
| 147 | - $path = $this->checkAndSetLogFileDirectory(); |
|
| 148 | - //save the log data |
|
| 149 | - $this->saveLogData($path, $level, $message); |
|
| 150 | - } |
|
| 140 | + //check if can logging regarding the log level config |
|
| 141 | + $configLevel = self::getLevelValue($configLogLevel); |
|
| 142 | + if($configLevel > $level){ |
|
| 143 | + //can't log |
|
| 144 | + return; |
|
| 145 | + } |
|
| 146 | + //check log file and directory |
|
| 147 | + $path = $this->checkAndSetLogFileDirectory(); |
|
| 148 | + //save the log data |
|
| 149 | + $this->saveLogData($path, $level, $message); |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * Save the log data into file |
|
| 154 | - * @param string $path the path of the log file |
|
| 155 | - * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
|
| 156 | - * @param string $message the log message to save |
|
| 157 | - * @return void |
|
| 158 | - */ |
|
| 159 | - protected function saveLogData($path, $level, $message){ |
|
| 160 | - //may be at this time helper user_agent not yet included |
|
| 161 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 152 | + /** |
|
| 153 | + * Save the log data into file |
|
| 154 | + * @param string $path the path of the log file |
|
| 155 | + * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
|
| 156 | + * @param string $message the log message to save |
|
| 157 | + * @return void |
|
| 158 | + */ |
|
| 159 | + protected function saveLogData($path, $level, $message){ |
|
| 160 | + //may be at this time helper user_agent not yet included |
|
| 161 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 162 | 162 | |
| 163 | - ///////////////////// date ////////////// |
|
| 164 | - $timestampWithMicro = microtime(true); |
|
| 165 | - $microtime = sprintf('%06d', ($timestampWithMicro - floor($timestampWithMicro)) * 1000000); |
|
| 166 | - $dateTime = new DateTime(date('Y-m-d H:i:s.' . $microtime, $timestampWithMicro)); |
|
| 167 | - $logDate = $dateTime->format('Y-m-d H:i:s.u'); |
|
| 168 | - //ip |
|
| 169 | - $ip = get_ip(); |
|
| 163 | + ///////////////////// date ////////////// |
|
| 164 | + $timestampWithMicro = microtime(true); |
|
| 165 | + $microtime = sprintf('%06d', ($timestampWithMicro - floor($timestampWithMicro)) * 1000000); |
|
| 166 | + $dateTime = new DateTime(date('Y-m-d H:i:s.' . $microtime, $timestampWithMicro)); |
|
| 167 | + $logDate = $dateTime->format('Y-m-d H:i:s.u'); |
|
| 168 | + //ip |
|
| 169 | + $ip = get_ip(); |
|
| 170 | 170 | |
| 171 | - //if $level is not an integer |
|
| 172 | - if(! is_numeric($level)){ |
|
| 173 | - $level = self::getLevelValue($level); |
|
| 174 | - } |
|
| 171 | + //if $level is not an integer |
|
| 172 | + if(! is_numeric($level)){ |
|
| 173 | + $level = self::getLevelValue($level); |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - //level name |
|
| 177 | - $levelName = self::getLevelName($level); |
|
| 176 | + //level name |
|
| 177 | + $levelName = self::getLevelName($level); |
|
| 178 | 178 | |
| 179 | - //debug info |
|
| 180 | - $dtrace = debug_backtrace(); |
|
| 181 | - $fileInfo = $dtrace[0]; |
|
| 182 | - if ($dtrace[0]['file'] == __FILE__ || $dtrace[1]['file'] == __FILE__){ |
|
| 183 | - $fileInfo = $dtrace[2]; |
|
| 184 | - } |
|
| 179 | + //debug info |
|
| 180 | + $dtrace = debug_backtrace(); |
|
| 181 | + $fileInfo = $dtrace[0]; |
|
| 182 | + if ($dtrace[0]['file'] == __FILE__ || $dtrace[1]['file'] == __FILE__){ |
|
| 183 | + $fileInfo = $dtrace[2]; |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - $str = $logDate . ' [' . str_pad($levelName, 7 /*warning len*/) . '] ' . ' [' . str_pad($ip, 15) . '] ' . $this->logger . ' : ' . $message . ' ' . '[' . $fileInfo['file'] . '::' . $fileInfo['line'] . ']' . "\n"; |
|
| 187 | - $fp = fopen($path, 'a+'); |
|
| 188 | - if(is_resource($fp)){ |
|
| 189 | - flock($fp, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 190 | - fwrite($fp, $str); |
|
| 191 | - fclose($fp); |
|
| 192 | - } |
|
| 193 | - } |
|
| 186 | + $str = $logDate . ' [' . str_pad($levelName, 7 /*warning len*/) . '] ' . ' [' . str_pad($ip, 15) . '] ' . $this->logger . ' : ' . $message . ' ' . '[' . $fileInfo['file'] . '::' . $fileInfo['line'] . ']' . "\n"; |
|
| 187 | + $fp = fopen($path, 'a+'); |
|
| 188 | + if(is_resource($fp)){ |
|
| 189 | + flock($fp, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 190 | + fwrite($fp, $str); |
|
| 191 | + fclose($fp); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Check if the current logger can save log data regarding the configuration |
|
| 197 | - * of logger filter |
|
| 198 | - * @return boolean |
|
| 199 | - */ |
|
| 200 | - protected function canSaveLogDataForLogger(){ |
|
| 201 | - if(! empty($this->logger)){ |
|
| 202 | - $configLoggersName = get_config('log_logger_name', array()); |
|
| 203 | - if (!empty($configLoggersName)) { |
|
| 204 | - //for best comparaison put all string to lowercase |
|
| 205 | - $configLoggersName = array_map('strtolower', $configLoggersName); |
|
| 206 | - if(! in_array(strtolower($this->logger), $configLoggersName)){ |
|
| 207 | - return false; |
|
| 208 | - } |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - return true; |
|
| 212 | - } |
|
| 195 | + /** |
|
| 196 | + * Check if the current logger can save log data regarding the configuration |
|
| 197 | + * of logger filter |
|
| 198 | + * @return boolean |
|
| 199 | + */ |
|
| 200 | + protected function canSaveLogDataForLogger(){ |
|
| 201 | + if(! empty($this->logger)){ |
|
| 202 | + $configLoggersName = get_config('log_logger_name', array()); |
|
| 203 | + if (!empty($configLoggersName)) { |
|
| 204 | + //for best comparaison put all string to lowercase |
|
| 205 | + $configLoggersName = array_map('strtolower', $configLoggersName); |
|
| 206 | + if(! in_array(strtolower($this->logger), $configLoggersName)){ |
|
| 207 | + return false; |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + return true; |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * Check the file and directory |
|
| 216 | - * @return string the log file path |
|
| 217 | - */ |
|
| 218 | - protected function checkAndSetLogFileDirectory(){ |
|
| 219 | - $logSavePath = get_config('log_save_path'); |
|
| 220 | - if(! $logSavePath){ |
|
| 221 | - $logSavePath = LOGS_PATH; |
|
| 222 | - } |
|
| 214 | + /** |
|
| 215 | + * Check the file and directory |
|
| 216 | + * @return string the log file path |
|
| 217 | + */ |
|
| 218 | + protected function checkAndSetLogFileDirectory(){ |
|
| 219 | + $logSavePath = get_config('log_save_path'); |
|
| 220 | + if(! $logSavePath){ |
|
| 221 | + $logSavePath = LOGS_PATH; |
|
| 222 | + } |
|
| 223 | 223 | |
| 224 | - if(! is_dir($logSavePath) || !is_writable($logSavePath)){ |
|
| 225 | - //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
| 226 | - show_error('Error : the log dir does not exists or is not writable', $title = 'Log directory error', $logging = false); |
|
| 227 | - } |
|
| 224 | + if(! is_dir($logSavePath) || !is_writable($logSavePath)){ |
|
| 225 | + //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
| 226 | + show_error('Error : the log dir does not exists or is not writable', $title = 'Log directory error', $logging = false); |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - $path = $logSavePath . 'logs-' . date('Y-m-d') . '.log'; |
|
| 230 | - if(! file_exists($path)){ |
|
| 231 | - touch($path); |
|
| 232 | - } |
|
| 233 | - return $path; |
|
| 234 | - } |
|
| 229 | + $path = $logSavePath . 'logs-' . date('Y-m-d') . '.log'; |
|
| 230 | + if(! file_exists($path)){ |
|
| 231 | + touch($path); |
|
| 232 | + } |
|
| 233 | + return $path; |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - /** |
|
| 237 | - * Check if the given log level is valid |
|
| 238 | - * |
|
| 239 | - * @param string $level the log level |
|
| 240 | - * |
|
| 241 | - * @return boolean true if the given log level is valid, false if not |
|
| 242 | - */ |
|
| 243 | - protected static function isValidConfigLevel($level){ |
|
| 244 | - $level = strtolower($level); |
|
| 245 | - return in_array($level, self::$validConfigLevel); |
|
| 246 | - } |
|
| 236 | + /** |
|
| 237 | + * Check if the given log level is valid |
|
| 238 | + * |
|
| 239 | + * @param string $level the log level |
|
| 240 | + * |
|
| 241 | + * @return boolean true if the given log level is valid, false if not |
|
| 242 | + */ |
|
| 243 | + protected static function isValidConfigLevel($level){ |
|
| 244 | + $level = strtolower($level); |
|
| 245 | + return in_array($level, self::$validConfigLevel); |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - /** |
|
| 249 | - * Get the log level number for the given level string |
|
| 250 | - * @param string $level the log level in string format |
|
| 251 | - * |
|
| 252 | - * @return int the log level in integer format using the predefined constants |
|
| 253 | - */ |
|
| 254 | - protected static function getLevelValue($level){ |
|
| 255 | - $level = strtolower($level); |
|
| 256 | - $levelMaps = array( |
|
| 257 | - 'fatal' => self::FATAL, |
|
| 258 | - 'error' => self::ERROR, |
|
| 259 | - 'warning' => self::WARNING, |
|
| 260 | - 'warn' => self::WARNING, |
|
| 261 | - 'info' => self::INFO, |
|
| 262 | - 'debug' => self::DEBUG, |
|
| 263 | - 'all' => self::ALL |
|
| 264 | - ); |
|
| 265 | - //the default value is NONE, so means no need test for NONE |
|
| 266 | - $value = self::NONE; |
|
| 267 | - if(isset($levelMaps[$level])){ |
|
| 268 | - $value = $levelMaps[$level]; |
|
| 269 | - } |
|
| 270 | - return $value; |
|
| 271 | - } |
|
| 248 | + /** |
|
| 249 | + * Get the log level number for the given level string |
|
| 250 | + * @param string $level the log level in string format |
|
| 251 | + * |
|
| 252 | + * @return int the log level in integer format using the predefined constants |
|
| 253 | + */ |
|
| 254 | + protected static function getLevelValue($level){ |
|
| 255 | + $level = strtolower($level); |
|
| 256 | + $levelMaps = array( |
|
| 257 | + 'fatal' => self::FATAL, |
|
| 258 | + 'error' => self::ERROR, |
|
| 259 | + 'warning' => self::WARNING, |
|
| 260 | + 'warn' => self::WARNING, |
|
| 261 | + 'info' => self::INFO, |
|
| 262 | + 'debug' => self::DEBUG, |
|
| 263 | + 'all' => self::ALL |
|
| 264 | + ); |
|
| 265 | + //the default value is NONE, so means no need test for NONE |
|
| 266 | + $value = self::NONE; |
|
| 267 | + if(isset($levelMaps[$level])){ |
|
| 268 | + $value = $levelMaps[$level]; |
|
| 269 | + } |
|
| 270 | + return $value; |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | - /** |
|
| 274 | - * Get the log level string for the given log level integer |
|
| 275 | - * @param integer $level the log level in integer format |
|
| 276 | - * @return string the log level in string format |
|
| 277 | - */ |
|
| 278 | - protected static function getLevelName($level){ |
|
| 279 | - $levelMaps = array( |
|
| 280 | - self::FATAL => 'FATAL', |
|
| 281 | - self::ERROR => 'ERROR', |
|
| 282 | - self::WARNING => 'WARNING', |
|
| 283 | - self::INFO => 'INFO', |
|
| 284 | - self::DEBUG => 'DEBUG' |
|
| 285 | - ); |
|
| 286 | - $value = ''; |
|
| 287 | - if(isset($levelMaps[$level])){ |
|
| 288 | - $value = $levelMaps[$level]; |
|
| 289 | - } |
|
| 290 | - return $value; |
|
| 291 | - } |
|
| 273 | + /** |
|
| 274 | + * Get the log level string for the given log level integer |
|
| 275 | + * @param integer $level the log level in integer format |
|
| 276 | + * @return string the log level in string format |
|
| 277 | + */ |
|
| 278 | + protected static function getLevelName($level){ |
|
| 279 | + $levelMaps = array( |
|
| 280 | + self::FATAL => 'FATAL', |
|
| 281 | + self::ERROR => 'ERROR', |
|
| 282 | + self::WARNING => 'WARNING', |
|
| 283 | + self::INFO => 'INFO', |
|
| 284 | + self::DEBUG => 'DEBUG' |
|
| 285 | + ); |
|
| 286 | + $value = ''; |
|
| 287 | + if(isset($levelMaps[$level])){ |
|
| 288 | + $value = $levelMaps[$level]; |
|
| 289 | + } |
|
| 290 | + return $value; |
|
| 291 | + } |
|
| 292 | 292 | |
| 293 | - } |
|
| 293 | + } |
|