@@ -1,393 +1,393 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or 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') or 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 Router { |
|
| 27 | + class Router { |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @var array $pattern: The list of URIs to validate against |
|
| 31 | - */ |
|
| 32 | - private $pattern = array(); |
|
| 29 | + /** |
|
| 30 | + * @var array $pattern: The list of URIs to validate against |
|
| 31 | + */ |
|
| 32 | + private $pattern = array(); |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @var array $callback: The list of callback to call |
|
| 36 | - */ |
|
| 37 | - private $callback = array(); |
|
| 34 | + /** |
|
| 35 | + * @var array $callback: The list of callback to call |
|
| 36 | + */ |
|
| 37 | + private $callback = array(); |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @var string $uriTrim: The char to remove from the URIs |
|
| 41 | - */ |
|
| 42 | - protected $uriTrim = '/\^$'; |
|
| 39 | + /** |
|
| 40 | + * @var string $uriTrim: The char to remove from the URIs |
|
| 41 | + */ |
|
| 42 | + protected $uriTrim = '/\^$'; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * The module name of the current request |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 48 | - protected $module = null; |
|
| 44 | + /** |
|
| 45 | + * The module name of the current request |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | + protected $module = null; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * The controller name of the current request |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - protected $controller = null; |
|
| 50 | + /** |
|
| 51 | + * The controller name of the current request |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + protected $controller = null; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * The controller path |
|
| 58 | - * @var string |
|
| 59 | - */ |
|
| 60 | - protected $controllerPath = null; |
|
| 56 | + /** |
|
| 57 | + * The controller path |
|
| 58 | + * @var string |
|
| 59 | + */ |
|
| 60 | + protected $controllerPath = null; |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * The method name. The default value is "index" |
|
| 64 | - * @var string |
|
| 65 | - */ |
|
| 66 | - protected $method = 'index'; |
|
| 62 | + /** |
|
| 63 | + * The method name. The default value is "index" |
|
| 64 | + * @var string |
|
| 65 | + */ |
|
| 66 | + protected $method = 'index'; |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * List of argument to pass to the method |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - protected $args = array(); |
|
| 68 | + /** |
|
| 69 | + * List of argument to pass to the method |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + protected $args = array(); |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * List of routes configurations |
|
| 76 | - * @var array |
|
| 77 | - */ |
|
| 78 | - protected $routes = array(); |
|
| 74 | + /** |
|
| 75 | + * List of routes configurations |
|
| 76 | + * @var array |
|
| 77 | + */ |
|
| 78 | + protected $routes = array(); |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * The segments array for the current request |
|
| 82 | - * @var array |
|
| 83 | - */ |
|
| 84 | - protected $segments; |
|
| 80 | + /** |
|
| 81 | + * The segments array for the current request |
|
| 82 | + * @var array |
|
| 83 | + */ |
|
| 84 | + protected $segments; |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * The logger instance |
|
| 88 | - * @var Log |
|
| 89 | - */ |
|
| 90 | - private $logger; |
|
| 86 | + /** |
|
| 87 | + * The logger instance |
|
| 88 | + * @var Log |
|
| 89 | + */ |
|
| 90 | + private $logger; |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Construct the new Router instance |
|
| 94 | - */ |
|
| 95 | - public function __construct(){ |
|
| 96 | - $this->setLoggerFromParamOrCreateNewInstance(null); |
|
| 92 | + /** |
|
| 93 | + * Construct the new Router instance |
|
| 94 | + */ |
|
| 95 | + public function __construct(){ |
|
| 96 | + $this->setLoggerFromParamOrCreateNewInstance(null); |
|
| 97 | 97 | |
| 98 | - //loading routes for module |
|
| 99 | - $moduleRouteList = array(); |
|
| 100 | - $modulesRoutes = Module::getModulesRoutes(); |
|
| 101 | - if($modulesRoutes && is_array($modulesRoutes)){ |
|
| 102 | - $moduleRouteList = $modulesRoutes; |
|
| 103 | - unset($modulesRoutes); |
|
| 104 | - } |
|
| 105 | - $this->setRouteConfiguration($moduleRouteList); |
|
| 106 | - $this->logger->info('The routes configuration are listed below: ' . stringfy_vars($this->routes)); |
|
| 98 | + //loading routes for module |
|
| 99 | + $moduleRouteList = array(); |
|
| 100 | + $modulesRoutes = Module::getModulesRoutes(); |
|
| 101 | + if($modulesRoutes && is_array($modulesRoutes)){ |
|
| 102 | + $moduleRouteList = $modulesRoutes; |
|
| 103 | + unset($modulesRoutes); |
|
| 104 | + } |
|
| 105 | + $this->setRouteConfiguration($moduleRouteList); |
|
| 106 | + $this->logger->info('The routes configuration are listed below: ' . stringfy_vars($this->routes)); |
|
| 107 | 107 | |
| 108 | - //Set route parameters |
|
| 109 | - $this->setRouteParams(); |
|
| 110 | - } |
|
| 108 | + //Set route parameters |
|
| 109 | + $this->setRouteParams(); |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Add the URI and callback to the list of URIs to validate |
|
| 114 | - * |
|
| 115 | - * @param string $uri the request URI |
|
| 116 | - * @param object $callback the callback function |
|
| 117 | - */ |
|
| 118 | - public function add($uri, $callback) { |
|
| 119 | - $uri = trim($uri, $this->uriTrim); |
|
| 120 | - if(in_array($uri, $this->pattern)){ |
|
| 121 | - $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
|
| 122 | - } |
|
| 123 | - $this->pattern[] = $uri; |
|
| 124 | - $this->callback[] = $callback; |
|
| 125 | - } |
|
| 112 | + /** |
|
| 113 | + * Add the URI and callback to the list of URIs to validate |
|
| 114 | + * |
|
| 115 | + * @param string $uri the request URI |
|
| 116 | + * @param object $callback the callback function |
|
| 117 | + */ |
|
| 118 | + public function add($uri, $callback) { |
|
| 119 | + $uri = trim($uri, $this->uriTrim); |
|
| 120 | + if(in_array($uri, $this->pattern)){ |
|
| 121 | + $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
|
| 122 | + } |
|
| 123 | + $this->pattern[] = $uri; |
|
| 124 | + $this->callback[] = $callback; |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * Get the module name |
|
| 129 | - * @return string |
|
| 130 | - */ |
|
| 131 | - public function getModule(){ |
|
| 132 | - return $this->module; |
|
| 133 | - } |
|
| 127 | + /** |
|
| 128 | + * Get the module name |
|
| 129 | + * @return string |
|
| 130 | + */ |
|
| 131 | + public function getModule(){ |
|
| 132 | + return $this->module; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Get the controller name |
|
| 137 | - * @return string |
|
| 138 | - */ |
|
| 139 | - public function getController(){ |
|
| 140 | - return $this->controller; |
|
| 141 | - } |
|
| 135 | + /** |
|
| 136 | + * Get the controller name |
|
| 137 | + * @return string |
|
| 138 | + */ |
|
| 139 | + public function getController(){ |
|
| 140 | + return $this->controller; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * Get the controller file path |
|
| 145 | - * @return string |
|
| 146 | - */ |
|
| 147 | - public function getControllerPath(){ |
|
| 148 | - return $this->controllerPath; |
|
| 149 | - } |
|
| 143 | + /** |
|
| 144 | + * Get the controller file path |
|
| 145 | + * @return string |
|
| 146 | + */ |
|
| 147 | + public function getControllerPath(){ |
|
| 148 | + return $this->controllerPath; |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * Get the controller method |
|
| 153 | - * @return string |
|
| 154 | - */ |
|
| 155 | - public function getMethod(){ |
|
| 156 | - return $this->method; |
|
| 157 | - } |
|
| 151 | + /** |
|
| 152 | + * Get the controller method |
|
| 153 | + * @return string |
|
| 154 | + */ |
|
| 155 | + public function getMethod(){ |
|
| 156 | + return $this->method; |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * Get the request arguments |
|
| 161 | - * @return array |
|
| 162 | - */ |
|
| 163 | - public function getArgs(){ |
|
| 164 | - return $this->args; |
|
| 165 | - } |
|
| 159 | + /** |
|
| 160 | + * Get the request arguments |
|
| 161 | + * @return array |
|
| 162 | + */ |
|
| 163 | + public function getArgs(){ |
|
| 164 | + return $this->args; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Get the URL segments array |
|
| 169 | - * @return array |
|
| 170 | - */ |
|
| 171 | - public function getSegments(){ |
|
| 172 | - return $this->segments; |
|
| 173 | - } |
|
| 167 | + /** |
|
| 168 | + * Get the URL segments array |
|
| 169 | + * @return array |
|
| 170 | + */ |
|
| 171 | + public function getSegments(){ |
|
| 172 | + return $this->segments; |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | - /** |
|
| 176 | - * Routing the request to the correspondant module/controller/method if exists |
|
| 177 | - * otherwise send 404 error. |
|
| 178 | - */ |
|
| 179 | - public function run() { |
|
| 180 | - $benchmark =& class_loader('Benchmark'); |
|
| 181 | - $benchmark->mark('ROUTING_PROCESS_START'); |
|
| 182 | - $this->logger->debug('Routing process start ...'); |
|
| 183 | - $segment = $this->segments; |
|
| 184 | - $baseUrl = get_config('base_url'); |
|
| 185 | - //check if the app is not in DOCUMENT_ROOT |
|
| 186 | - if(isset($segment[0]) && stripos($baseUrl, $segment[0]) != false){ |
|
| 187 | - array_shift($segment); |
|
| 188 | - $this->segments = $segment; |
|
| 189 | - } |
|
| 190 | - $this->logger->debug('Check if the request URI contains the front controller'); |
|
| 191 | - if(isset($segment[0]) && $segment[0] == SELF){ |
|
| 192 | - $this->logger->info('The request URI contains the front controller'); |
|
| 193 | - array_shift($segment); |
|
| 194 | - $this->segments = $segment; |
|
| 195 | - } |
|
| 196 | - else{ |
|
| 197 | - $this->logger->info('The request URI does not contain the front controller'); |
|
| 198 | - } |
|
| 199 | - $uri = implode('/', $segment); |
|
| 200 | - $this->logger->info('The final Request URI is [' . $uri . ']' ); |
|
| 201 | - //generic routes |
|
| 202 | - $pattern = array(':num', ':alpha', ':alnum', ':any'); |
|
| 203 | - $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
|
| 204 | - $this->logger->debug('Begin to loop in the predefined routes configuration to check if the current request match'); |
|
| 205 | - // Cycle through the URIs stored in the array |
|
| 206 | - foreach ($this->pattern as $index => $uriList) { |
|
| 207 | - $uriList = str_ireplace($pattern, $replace, $uriList); |
|
| 208 | - // Check for an existant matching URI |
|
| 209 | - if (preg_match("#^$uriList$#", $uri, $args)) { |
|
| 210 | - $this->logger->info('Route found for request URI [' . $uri . '] using the predefined configuration [' . $this->pattern[$index] . '] --> [' . $this->callback[$index] . ']'); |
|
| 211 | - array_shift($args); |
|
| 212 | - //check if this contains an module |
|
| 213 | - $moduleControllerMethod = explode('#', $this->callback[$index]); |
|
| 214 | - if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
| 215 | - $this->logger->info('The current request use the module [' .$moduleControllerMethod[0]. ']'); |
|
| 216 | - $this->module = $moduleControllerMethod[0]; |
|
| 217 | - $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
|
| 218 | - } |
|
| 219 | - else{ |
|
| 220 | - $this->logger->info('The current request does not use the module'); |
|
| 221 | - $moduleControllerMethod = explode('@', $this->callback[$index]); |
|
| 222 | - } |
|
| 223 | - if(is_array($moduleControllerMethod)){ |
|
| 224 | - if(isset($moduleControllerMethod[0])){ |
|
| 225 | - $this->controller = $moduleControllerMethod[0]; |
|
| 226 | - } |
|
| 227 | - if(isset($moduleControllerMethod[1])){ |
|
| 228 | - $this->method = $moduleControllerMethod[1]; |
|
| 229 | - } |
|
| 230 | - $this->args = $args; |
|
| 231 | - } |
|
| 232 | - // stop here |
|
| 233 | - break; |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - //first if the controller is not set and the module is set use the module name as the controller |
|
| 237 | - if(! $this->getController() && $this->getModule()){ |
|
| 238 | - $this->logger->info('After loop in predefined routes configuration, the module name is set but the controller is not set, so we will use module as the controller'); |
|
| 239 | - $this->controller = $this->getModule(); |
|
| 240 | - } |
|
| 241 | - //if can not determine the module/controller/method via the defined routes configuration we will use |
|
| 242 | - //the URL like http://domain.com/module/controller/method/arg1/arg2 |
|
| 243 | - if(! $this->getController()){ |
|
| 244 | - $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
|
| 245 | - $nbSegment = count($segment); |
|
| 246 | - //if segment is null so means no need to perform |
|
| 247 | - if($nbSegment > 0){ |
|
| 248 | - //get the module list |
|
| 249 | - $modules = Module::getModuleList(); |
|
| 250 | - //first check if no module |
|
| 251 | - if(! $modules){ |
|
| 252 | - $this->logger->info('No module was loaded will skip the module checking'); |
|
| 253 | - //the application don't use module |
|
| 254 | - //controller |
|
| 255 | - if(isset($segment[0])){ |
|
| 256 | - $this->controller = $segment[0]; |
|
| 257 | - array_shift($segment); |
|
| 258 | - } |
|
| 259 | - //method |
|
| 260 | - if(isset($segment[0])){ |
|
| 261 | - $this->method = $segment[0]; |
|
| 262 | - array_shift($segment); |
|
| 263 | - } |
|
| 264 | - //args |
|
| 265 | - $this->args = $segment; |
|
| 266 | - } |
|
| 267 | - else{ |
|
| 268 | - $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
|
| 269 | - if(in_array($segment[0], $modules)){ |
|
| 270 | - $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
|
| 271 | - $this->module = $segment[0]; |
|
| 272 | - array_shift($segment); |
|
| 273 | - //check if the second arg is the controller from module |
|
| 274 | - if(isset($segment[0])){ |
|
| 275 | - $this->controller = $segment[0]; |
|
| 276 | - //check if the request use the same module name and controller |
|
| 277 | - $path = Module::findControllerFullPath(ucfirst($this->getController()), $this->getModule()); |
|
| 278 | - if(! $path){ |
|
| 279 | - $this->logger->info('The controller [' . $this->getController() . '] not found in the module, may be will use the module [' . $this->getModule() . '] as controller'); |
|
| 280 | - $this->controller = $this->getModule(); |
|
| 281 | - } |
|
| 282 | - else{ |
|
| 283 | - $this->controllerPath = $path; |
|
| 284 | - array_shift($segment); |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - //check for method |
|
| 288 | - if(isset($segment[0])){ |
|
| 289 | - $this->method = $segment[0]; |
|
| 290 | - array_shift($segment); |
|
| 291 | - } |
|
| 292 | - //the remaining is for args |
|
| 293 | - $this->args = $segment; |
|
| 294 | - } |
|
| 295 | - else{ |
|
| 296 | - $this->logger->info('The current request information is not found in the module list'); |
|
| 297 | - //controller |
|
| 298 | - if(isset($segment[0])){ |
|
| 299 | - $this->controller = $segment[0]; |
|
| 300 | - array_shift($segment); |
|
| 301 | - } |
|
| 302 | - //method |
|
| 303 | - if(isset($segment[0])){ |
|
| 304 | - $this->method = $segment[0]; |
|
| 305 | - array_shift($segment); |
|
| 306 | - } |
|
| 307 | - //args |
|
| 308 | - $this->args = $segment; |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - if(! $this->getController() && $this->getModule()){ |
|
| 314 | - $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
|
| 315 | - $this->controller = $this->getModule(); |
|
| 316 | - } |
|
| 317 | - //did we set the controller, so set the controller path |
|
| 318 | - if($this->getController() && ! $this->getControllerPath()){ |
|
| 319 | - $this->logger->debug('Setting the file path for the controller [' . $this->getController() . ']'); |
|
| 320 | - //if it is the module controller |
|
| 321 | - if($this->getModule()){ |
|
| 322 | - $this->controllerPath = Module::findControllerFullPath(ucfirst($this->getController()), $this->getModule()); |
|
| 323 | - } |
|
| 324 | - else{ |
|
| 325 | - $this->controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->getController()) . '.php'; |
|
| 326 | - } |
|
| 327 | - } |
|
| 328 | - $controller = ucfirst($this->getController()); |
|
| 329 | - $this->logger->info('The routing information are: module [' . $this->getModule() . '], controller [' . $controller . '], method [' . $this->getMethod() . '], args [' . stringfy_vars($this->args) . ']'); |
|
| 330 | - $classFilePath = $this->getControllerPath(); |
|
| 331 | - $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
|
| 332 | - $benchmark->mark('ROUTING_PROCESS_END'); |
|
| 333 | - $e404 = false; |
|
| 334 | - if(file_exists($classFilePath)){ |
|
| 335 | - require_once $classFilePath; |
|
| 336 | - if(! class_exists($controller, false)){ |
|
| 337 | - $e404 = true; |
|
| 338 | - $this->logger->info('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
| 339 | - } |
|
| 340 | - else{ |
|
| 341 | - $controllerInstance = new $controller(); |
|
| 342 | - $controllerMethod = $this->getMethod(); |
|
| 343 | - if(! method_exists($controllerInstance, $controllerMethod)){ |
|
| 344 | - $e404 = true; |
|
| 345 | - $this->logger->info('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
|
| 346 | - } |
|
| 347 | - else{ |
|
| 348 | - $this->logger->info('Routing data is set correctly now GO!'); |
|
| 349 | - call_user_func_array(array($controllerInstance, $controllerMethod), $this->getArgs()); |
|
| 350 | - $obj = & get_instance(); |
|
| 351 | - //render the final page to user |
|
| 352 | - $this->logger->info('Render the final output to the browser'); |
|
| 353 | - $obj->response->renderFinalPage(); |
|
| 354 | - } |
|
| 355 | - } |
|
| 356 | - } |
|
| 357 | - else{ |
|
| 358 | - $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
|
| 359 | - $e404 = true; |
|
| 360 | - } |
|
| 361 | - if($e404){ |
|
| 362 | - $response =& class_loader('Response', 'classes'); |
|
| 363 | - $response->send404(); |
|
| 364 | - } |
|
| 365 | - } |
|
| 175 | + /** |
|
| 176 | + * Routing the request to the correspondant module/controller/method if exists |
|
| 177 | + * otherwise send 404 error. |
|
| 178 | + */ |
|
| 179 | + public function run() { |
|
| 180 | + $benchmark =& class_loader('Benchmark'); |
|
| 181 | + $benchmark->mark('ROUTING_PROCESS_START'); |
|
| 182 | + $this->logger->debug('Routing process start ...'); |
|
| 183 | + $segment = $this->segments; |
|
| 184 | + $baseUrl = get_config('base_url'); |
|
| 185 | + //check if the app is not in DOCUMENT_ROOT |
|
| 186 | + if(isset($segment[0]) && stripos($baseUrl, $segment[0]) != false){ |
|
| 187 | + array_shift($segment); |
|
| 188 | + $this->segments = $segment; |
|
| 189 | + } |
|
| 190 | + $this->logger->debug('Check if the request URI contains the front controller'); |
|
| 191 | + if(isset($segment[0]) && $segment[0] == SELF){ |
|
| 192 | + $this->logger->info('The request URI contains the front controller'); |
|
| 193 | + array_shift($segment); |
|
| 194 | + $this->segments = $segment; |
|
| 195 | + } |
|
| 196 | + else{ |
|
| 197 | + $this->logger->info('The request URI does not contain the front controller'); |
|
| 198 | + } |
|
| 199 | + $uri = implode('/', $segment); |
|
| 200 | + $this->logger->info('The final Request URI is [' . $uri . ']' ); |
|
| 201 | + //generic routes |
|
| 202 | + $pattern = array(':num', ':alpha', ':alnum', ':any'); |
|
| 203 | + $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
|
| 204 | + $this->logger->debug('Begin to loop in the predefined routes configuration to check if the current request match'); |
|
| 205 | + // Cycle through the URIs stored in the array |
|
| 206 | + foreach ($this->pattern as $index => $uriList) { |
|
| 207 | + $uriList = str_ireplace($pattern, $replace, $uriList); |
|
| 208 | + // Check for an existant matching URI |
|
| 209 | + if (preg_match("#^$uriList$#", $uri, $args)) { |
|
| 210 | + $this->logger->info('Route found for request URI [' . $uri . '] using the predefined configuration [' . $this->pattern[$index] . '] --> [' . $this->callback[$index] . ']'); |
|
| 211 | + array_shift($args); |
|
| 212 | + //check if this contains an module |
|
| 213 | + $moduleControllerMethod = explode('#', $this->callback[$index]); |
|
| 214 | + if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
| 215 | + $this->logger->info('The current request use the module [' .$moduleControllerMethod[0]. ']'); |
|
| 216 | + $this->module = $moduleControllerMethod[0]; |
|
| 217 | + $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
|
| 218 | + } |
|
| 219 | + else{ |
|
| 220 | + $this->logger->info('The current request does not use the module'); |
|
| 221 | + $moduleControllerMethod = explode('@', $this->callback[$index]); |
|
| 222 | + } |
|
| 223 | + if(is_array($moduleControllerMethod)){ |
|
| 224 | + if(isset($moduleControllerMethod[0])){ |
|
| 225 | + $this->controller = $moduleControllerMethod[0]; |
|
| 226 | + } |
|
| 227 | + if(isset($moduleControllerMethod[1])){ |
|
| 228 | + $this->method = $moduleControllerMethod[1]; |
|
| 229 | + } |
|
| 230 | + $this->args = $args; |
|
| 231 | + } |
|
| 232 | + // stop here |
|
| 233 | + break; |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + //first if the controller is not set and the module is set use the module name as the controller |
|
| 237 | + if(! $this->getController() && $this->getModule()){ |
|
| 238 | + $this->logger->info('After loop in predefined routes configuration, the module name is set but the controller is not set, so we will use module as the controller'); |
|
| 239 | + $this->controller = $this->getModule(); |
|
| 240 | + } |
|
| 241 | + //if can not determine the module/controller/method via the defined routes configuration we will use |
|
| 242 | + //the URL like http://domain.com/module/controller/method/arg1/arg2 |
|
| 243 | + if(! $this->getController()){ |
|
| 244 | + $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
|
| 245 | + $nbSegment = count($segment); |
|
| 246 | + //if segment is null so means no need to perform |
|
| 247 | + if($nbSegment > 0){ |
|
| 248 | + //get the module list |
|
| 249 | + $modules = Module::getModuleList(); |
|
| 250 | + //first check if no module |
|
| 251 | + if(! $modules){ |
|
| 252 | + $this->logger->info('No module was loaded will skip the module checking'); |
|
| 253 | + //the application don't use module |
|
| 254 | + //controller |
|
| 255 | + if(isset($segment[0])){ |
|
| 256 | + $this->controller = $segment[0]; |
|
| 257 | + array_shift($segment); |
|
| 258 | + } |
|
| 259 | + //method |
|
| 260 | + if(isset($segment[0])){ |
|
| 261 | + $this->method = $segment[0]; |
|
| 262 | + array_shift($segment); |
|
| 263 | + } |
|
| 264 | + //args |
|
| 265 | + $this->args = $segment; |
|
| 266 | + } |
|
| 267 | + else{ |
|
| 268 | + $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
|
| 269 | + if(in_array($segment[0], $modules)){ |
|
| 270 | + $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
|
| 271 | + $this->module = $segment[0]; |
|
| 272 | + array_shift($segment); |
|
| 273 | + //check if the second arg is the controller from module |
|
| 274 | + if(isset($segment[0])){ |
|
| 275 | + $this->controller = $segment[0]; |
|
| 276 | + //check if the request use the same module name and controller |
|
| 277 | + $path = Module::findControllerFullPath(ucfirst($this->getController()), $this->getModule()); |
|
| 278 | + if(! $path){ |
|
| 279 | + $this->logger->info('The controller [' . $this->getController() . '] not found in the module, may be will use the module [' . $this->getModule() . '] as controller'); |
|
| 280 | + $this->controller = $this->getModule(); |
|
| 281 | + } |
|
| 282 | + else{ |
|
| 283 | + $this->controllerPath = $path; |
|
| 284 | + array_shift($segment); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + //check for method |
|
| 288 | + if(isset($segment[0])){ |
|
| 289 | + $this->method = $segment[0]; |
|
| 290 | + array_shift($segment); |
|
| 291 | + } |
|
| 292 | + //the remaining is for args |
|
| 293 | + $this->args = $segment; |
|
| 294 | + } |
|
| 295 | + else{ |
|
| 296 | + $this->logger->info('The current request information is not found in the module list'); |
|
| 297 | + //controller |
|
| 298 | + if(isset($segment[0])){ |
|
| 299 | + $this->controller = $segment[0]; |
|
| 300 | + array_shift($segment); |
|
| 301 | + } |
|
| 302 | + //method |
|
| 303 | + if(isset($segment[0])){ |
|
| 304 | + $this->method = $segment[0]; |
|
| 305 | + array_shift($segment); |
|
| 306 | + } |
|
| 307 | + //args |
|
| 308 | + $this->args = $segment; |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + if(! $this->getController() && $this->getModule()){ |
|
| 314 | + $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
|
| 315 | + $this->controller = $this->getModule(); |
|
| 316 | + } |
|
| 317 | + //did we set the controller, so set the controller path |
|
| 318 | + if($this->getController() && ! $this->getControllerPath()){ |
|
| 319 | + $this->logger->debug('Setting the file path for the controller [' . $this->getController() . ']'); |
|
| 320 | + //if it is the module controller |
|
| 321 | + if($this->getModule()){ |
|
| 322 | + $this->controllerPath = Module::findControllerFullPath(ucfirst($this->getController()), $this->getModule()); |
|
| 323 | + } |
|
| 324 | + else{ |
|
| 325 | + $this->controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->getController()) . '.php'; |
|
| 326 | + } |
|
| 327 | + } |
|
| 328 | + $controller = ucfirst($this->getController()); |
|
| 329 | + $this->logger->info('The routing information are: module [' . $this->getModule() . '], controller [' . $controller . '], method [' . $this->getMethod() . '], args [' . stringfy_vars($this->args) . ']'); |
|
| 330 | + $classFilePath = $this->getControllerPath(); |
|
| 331 | + $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
|
| 332 | + $benchmark->mark('ROUTING_PROCESS_END'); |
|
| 333 | + $e404 = false; |
|
| 334 | + if(file_exists($classFilePath)){ |
|
| 335 | + require_once $classFilePath; |
|
| 336 | + if(! class_exists($controller, false)){ |
|
| 337 | + $e404 = true; |
|
| 338 | + $this->logger->info('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
| 339 | + } |
|
| 340 | + else{ |
|
| 341 | + $controllerInstance = new $controller(); |
|
| 342 | + $controllerMethod = $this->getMethod(); |
|
| 343 | + if(! method_exists($controllerInstance, $controllerMethod)){ |
|
| 344 | + $e404 = true; |
|
| 345 | + $this->logger->info('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
|
| 346 | + } |
|
| 347 | + else{ |
|
| 348 | + $this->logger->info('Routing data is set correctly now GO!'); |
|
| 349 | + call_user_func_array(array($controllerInstance, $controllerMethod), $this->getArgs()); |
|
| 350 | + $obj = & get_instance(); |
|
| 351 | + //render the final page to user |
|
| 352 | + $this->logger->info('Render the final output to the browser'); |
|
| 353 | + $obj->response->renderFinalPage(); |
|
| 354 | + } |
|
| 355 | + } |
|
| 356 | + } |
|
| 357 | + else{ |
|
| 358 | + $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
|
| 359 | + $e404 = true; |
|
| 360 | + } |
|
| 361 | + if($e404){ |
|
| 362 | + $response =& class_loader('Response', 'classes'); |
|
| 363 | + $response->send404(); |
|
| 364 | + } |
|
| 365 | + } |
|
| 366 | 366 | |
| 367 | - /** |
|
| 367 | + /** |
|
| 368 | 368 | * Return the Log instance |
| 369 | 369 | * @return Log |
| 370 | 370 | */ |
| 371 | 371 | public function getLogger(){ |
| 372 | - return $this->logger; |
|
| 372 | + return $this->logger; |
|
| 373 | 373 | } |
| 374 | 374 | |
| 375 | 375 | /** |
| 376 | 376 | * Set the log instance |
| 377 | 377 | * @param Log $logger the log object |
| 378 | - * @return object |
|
| 378 | + * @return object |
|
| 379 | 379 | */ |
| 380 | 380 | public function setLogger($logger){ |
| 381 | - $this->logger = $logger; |
|
| 382 | - return $this; |
|
| 381 | + $this->logger = $logger; |
|
| 382 | + return $this; |
|
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | /** |
| 386 | - * Setting the route configuration using the configuration file and additional configuration from param |
|
| 387 | - * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 388 | - * @param boolean $useConfigFile whether to use route configuration file |
|
| 389 | - * @return object |
|
| 390 | - */ |
|
| 386 | + * Setting the route configuration using the configuration file and additional configuration from param |
|
| 387 | + * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 388 | + * @param boolean $useConfigFile whether to use route configuration file |
|
| 389 | + * @return object |
|
| 390 | + */ |
|
| 391 | 391 | public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
| 392 | 392 | $route = array(); |
| 393 | 393 | if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')){ |
@@ -395,45 +395,45 @@ discard block |
||
| 395 | 395 | } |
| 396 | 396 | $route = array_merge($route, $overwriteConfig); |
| 397 | 397 | $this->routes = $route; |
| 398 | - return $this; |
|
| 398 | + return $this; |
|
| 399 | 399 | } |
| 400 | 400 | |
| 401 | 401 | /** |
| 402 | 402 | * Set the route paramaters using the configuration |
| 403 | 403 | */ |
| 404 | 404 | protected function setRouteParams(){ |
| 405 | - //adding route |
|
| 406 | - foreach($this->routes as $pattern => $callback){ |
|
| 407 | - $this->add($pattern, $callback); |
|
| 408 | - } |
|
| 405 | + //adding route |
|
| 406 | + foreach($this->routes as $pattern => $callback){ |
|
| 407 | + $this->add($pattern, $callback); |
|
| 408 | + } |
|
| 409 | 409 | |
| 410 | - //here use directly the variable $_SERVER |
|
| 411 | - $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; |
|
| 412 | - $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
|
| 413 | - //remove url suffix from the request URI |
|
| 414 | - $suffix = get_config('url_suffix'); |
|
| 415 | - if ($suffix) { |
|
| 416 | - $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
| 417 | - $uri = str_ireplace($suffix, '', $uri); |
|
| 418 | - } |
|
| 419 | - if (strpos($uri, '?') !== false){ |
|
| 420 | - $uri = substr($uri, 0, strpos($uri, '?')); |
|
| 421 | - } |
|
| 422 | - $uri = trim($uri, $this->uriTrim); |
|
| 423 | - $this->segments = explode('/', $uri); |
|
| 424 | - } |
|
| 410 | + //here use directly the variable $_SERVER |
|
| 411 | + $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; |
|
| 412 | + $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
|
| 413 | + //remove url suffix from the request URI |
|
| 414 | + $suffix = get_config('url_suffix'); |
|
| 415 | + if ($suffix) { |
|
| 416 | + $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
| 417 | + $uri = str_ireplace($suffix, '', $uri); |
|
| 418 | + } |
|
| 419 | + if (strpos($uri, '?') !== false){ |
|
| 420 | + $uri = substr($uri, 0, strpos($uri, '?')); |
|
| 421 | + } |
|
| 422 | + $uri = trim($uri, $this->uriTrim); |
|
| 423 | + $this->segments = explode('/', $uri); |
|
| 424 | + } |
|
| 425 | 425 | |
| 426 | - /** |
|
| 426 | + /** |
|
| 427 | 427 | * Set the Log instance using argument or create new instance |
| 428 | 428 | * @param object $logger the Log instance if not null |
| 429 | 429 | */ |
| 430 | 430 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
| 431 | - if ($logger !== null){ |
|
| 431 | + if ($logger !== null){ |
|
| 432 | 432 | $this->logger = $logger; |
| 433 | - } |
|
| 434 | - else{ |
|
| 435 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 436 | - $this->logger->setLogger('Library::Router'); |
|
| 437 | - } |
|
| 433 | + } |
|
| 434 | + else{ |
|
| 435 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 436 | + $this->logger->setLogger('Library::Router'); |
|
| 437 | + } |
|
| 438 | 438 | } |
| 439 | 439 | } |
@@ -1,108 +1,108 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 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 DatabaseQueryBuilder{ |
|
| 27 | - /** |
|
| 28 | - * The SQL SELECT statment |
|
| 29 | - * @var string |
|
| 30 | - */ |
|
| 31 | - private $select = '*'; |
|
| 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 DatabaseQueryBuilder{ |
|
| 27 | + /** |
|
| 28 | + * The SQL SELECT statment |
|
| 29 | + * @var string |
|
| 30 | + */ |
|
| 31 | + private $select = '*'; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * The SQL FROM statment |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - private $from = null; |
|
| 33 | + /** |
|
| 34 | + * The SQL FROM statment |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + private $from = null; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * The SQL WHERE statment |
|
| 41 | - * @var string |
|
| 42 | - */ |
|
| 43 | - private $where = null; |
|
| 39 | + /** |
|
| 40 | + * The SQL WHERE statment |
|
| 41 | + * @var string |
|
| 42 | + */ |
|
| 43 | + private $where = null; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * The SQL LIMIT statment |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - private $limit = null; |
|
| 45 | + /** |
|
| 46 | + * The SQL LIMIT statment |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + private $limit = null; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * The SQL JOIN statment |
|
| 53 | - * @var string |
|
| 54 | - */ |
|
| 55 | - private $join = null; |
|
| 51 | + /** |
|
| 52 | + * The SQL JOIN statment |
|
| 53 | + * @var string |
|
| 54 | + */ |
|
| 55 | + private $join = null; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * The SQL ORDER BY statment |
|
| 59 | - * @var string |
|
| 60 | - */ |
|
| 61 | - private $orderBy = null; |
|
| 57 | + /** |
|
| 58 | + * The SQL ORDER BY statment |
|
| 59 | + * @var string |
|
| 60 | + */ |
|
| 61 | + private $orderBy = null; |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * The SQL GROUP BY statment |
|
| 65 | - * @var string |
|
| 66 | - */ |
|
| 67 | - private $groupBy = null; |
|
| 63 | + /** |
|
| 64 | + * The SQL GROUP BY statment |
|
| 65 | + * @var string |
|
| 66 | + */ |
|
| 67 | + private $groupBy = null; |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * The SQL HAVING statment |
|
| 71 | - * @var string |
|
| 72 | - */ |
|
| 73 | - private $having = null; |
|
| 69 | + /** |
|
| 70 | + * The SQL HAVING statment |
|
| 71 | + * @var string |
|
| 72 | + */ |
|
| 73 | + private $having = null; |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * The full SQL query statment after build for each command |
|
| 77 | - * @var string |
|
| 78 | - */ |
|
| 79 | - private $query = null; |
|
| 75 | + /** |
|
| 76 | + * The full SQL query statment after build for each command |
|
| 77 | + * @var string |
|
| 78 | + */ |
|
| 79 | + private $query = null; |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * The list of SQL valid operators |
|
| 83 | - * @var array |
|
| 84 | - */ |
|
| 81 | + /** |
|
| 82 | + * The list of SQL valid operators |
|
| 83 | + * @var array |
|
| 84 | + */ |
|
| 85 | 85 | private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
| 86 | 86 | |
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * The prefix used in each database table |
|
| 90 | - * @var string |
|
| 91 | - */ |
|
| 88 | + /** |
|
| 89 | + * The prefix used in each database table |
|
| 90 | + * @var string |
|
| 91 | + */ |
|
| 92 | 92 | private $prefix = null; |
| 93 | 93 | |
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * The PDO instance |
|
| 97 | - * @var object |
|
| 98 | - */ |
|
| 99 | - private $pdo = null; |
|
| 95 | + /** |
|
| 96 | + * The PDO instance |
|
| 97 | + * @var object |
|
| 98 | + */ |
|
| 99 | + private $pdo = null; |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * The database driver name used |
|
| 103 | - * @var string |
|
| 104 | - */ |
|
| 105 | - private $driver = null; |
|
| 101 | + /** |
|
| 102 | + * The database driver name used |
|
| 103 | + * @var string |
|
| 104 | + */ |
|
| 105 | + private $driver = null; |
|
| 106 | 106 | |
| 107 | 107 | |
| 108 | 108 | /** |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | */ |
| 112 | 112 | public function __construct(PDO $pdo = null){ |
| 113 | 113 | if (is_object($pdo)){ |
| 114 | - $this->setPdo($pdo); |
|
| 114 | + $this->setPdo($pdo); |
|
| 115 | 115 | } |
| 116 | 116 | } |
| 117 | 117 | |
@@ -121,16 +121,16 @@ discard block |
||
| 121 | 121 | * @return object the current DatabaseQueryBuilder instance |
| 122 | 122 | */ |
| 123 | 123 | public function from($table){ |
| 124 | - if (is_array($table)){ |
|
| 124 | + if (is_array($table)){ |
|
| 125 | 125 | $froms = ''; |
| 126 | 126 | foreach($table as $key){ |
| 127 | - $froms .= $this->getPrefix() . $key . ', '; |
|
| 127 | + $froms .= $this->getPrefix() . $key . ', '; |
|
| 128 | 128 | } |
| 129 | 129 | $this->from = rtrim($froms, ', '); |
| 130 | - } else { |
|
| 130 | + } else { |
|
| 131 | 131 | $this->from = $this->getPrefix() . $table; |
| 132 | - } |
|
| 133 | - return $this; |
|
| 132 | + } |
|
| 133 | + return $this; |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -139,9 +139,9 @@ discard block |
||
| 139 | 139 | * @return object the current DatabaseQueryBuilder instance |
| 140 | 140 | */ |
| 141 | 141 | public function select($fields){ |
| 142 | - $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 143 | - $this->select = (($this->select == '*' || empty($this->select)) ? $select : $this->select . ', ' . $select); |
|
| 144 | - return $this; |
|
| 142 | + $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
| 143 | + $this->select = (($this->select == '*' || empty($this->select)) ? $select : $this->select . ', ' . $select); |
|
| 144 | + return $this; |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
@@ -150,19 +150,19 @@ discard block |
||
| 150 | 150 | * @return object the current DatabaseQueryBuilder instance |
| 151 | 151 | */ |
| 152 | 152 | public function distinct($field){ |
| 153 | - $distinct = ' DISTINCT ' . $field; |
|
| 154 | - $this->select = (($this->select == '*' || empty($this->select)) ? $distinct : $this->select . ', ' . $distinct); |
|
| 155 | - return $this; |
|
| 153 | + $distinct = ' DISTINCT ' . $field; |
|
| 154 | + $this->select = (($this->select == '*' || empty($this->select)) ? $distinct : $this->select . ', ' . $distinct); |
|
| 155 | + return $this; |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - /** |
|
| 159 | - * Set the SQL function COUNT in SELECT statment |
|
| 160 | - * @param string $field the field name |
|
| 161 | - * @param string $name if is not null represent the alias used for this field in the result |
|
| 162 | - * @return object the current DatabaseQueryBuilder instance |
|
| 163 | - */ |
|
| 158 | + /** |
|
| 159 | + * Set the SQL function COUNT in SELECT statment |
|
| 160 | + * @param string $field the field name |
|
| 161 | + * @param string $name if is not null represent the alias used for this field in the result |
|
| 162 | + * @return object the current DatabaseQueryBuilder instance |
|
| 163 | + */ |
|
| 164 | 164 | public function count($field = '*', $name = null){ |
| 165 | - return $this->select_min_max_sum_count_avg('COUNT', $field, $name); |
|
| 165 | + return $this->select_min_max_sum_count_avg('COUNT', $field, $name); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | * @return object the current DatabaseQueryBuilder instance |
| 173 | 173 | */ |
| 174 | 174 | public function min($field, $name = null){ |
| 175 | - return $this->select_min_max_sum_count_avg('MIN', $field, $name); |
|
| 175 | + return $this->select_min_max_sum_count_avg('MIN', $field, $name); |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | /** |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | * @return object the current DatabaseQueryBuilder instance |
| 183 | 183 | */ |
| 184 | 184 | public function max($field, $name = null){ |
| 185 | - return $this->select_min_max_sum_count_avg('MAX', $field, $name); |
|
| 185 | + return $this->select_min_max_sum_count_avg('MAX', $field, $name); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | /** |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | * @return object the current DatabaseQueryBuilder instance |
| 193 | 193 | */ |
| 194 | 194 | public function sum($field, $name = null){ |
| 195 | - return $this->select_min_max_sum_count_avg('SUM', $field, $name); |
|
| 195 | + return $this->select_min_max_sum_count_avg('SUM', $field, $name); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | * @return object the current DatabaseQueryBuilder instance |
| 203 | 203 | */ |
| 204 | 204 | public function avg($field, $name = null){ |
| 205 | - return $this->select_min_max_sum_count_avg('AVG', $field, $name); |
|
| 205 | + return $this->select_min_max_sum_count_avg('AVG', $field, $name); |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | |
@@ -216,20 +216,20 @@ discard block |
||
| 216 | 216 | * @return object the current DatabaseQueryBuilder instance |
| 217 | 217 | */ |
| 218 | 218 | public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
| 219 | - $on = $field1; |
|
| 220 | - $table = $this->getPrefix() . $table; |
|
| 221 | - if (! is_null($op)){ |
|
| 219 | + $on = $field1; |
|
| 220 | + $table = $this->getPrefix() . $table; |
|
| 221 | + if (! is_null($op)){ |
|
| 222 | 222 | $on = (! in_array($op, $this->operatorList) |
| 223 | - ? ($this->getPrefix() . $field1 . ' = ' . $this->getPrefix() . $op) |
|
| 224 | - : ($this->getPrefix() . $field1 . ' ' . $op . ' ' . $this->getPrefix() . $field2)); |
|
| 225 | - } |
|
| 226 | - if (empty($this->join)){ |
|
| 223 | + ? ($this->getPrefix() . $field1 . ' = ' . $this->getPrefix() . $op) |
|
| 224 | + : ($this->getPrefix() . $field1 . ' ' . $op . ' ' . $this->getPrefix() . $field2)); |
|
| 225 | + } |
|
| 226 | + if (empty($this->join)){ |
|
| 227 | 227 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 228 | - } |
|
| 229 | - else{ |
|
| 228 | + } |
|
| 229 | + else{ |
|
| 230 | 230 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
| 231 | - } |
|
| 232 | - return $this; |
|
| 231 | + } |
|
| 232 | + return $this; |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | /** |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | * @return object the current DatabaseQueryBuilder instance |
| 239 | 239 | */ |
| 240 | 240 | public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
| 241 | - return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 241 | + return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -247,16 +247,16 @@ discard block |
||
| 247 | 247 | * @return object the current DatabaseQueryBuilder instance |
| 248 | 248 | */ |
| 249 | 249 | public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
| 250 | - return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 251 | - } |
|
| 250 | + return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - /** |
|
| 253 | + /** |
|
| 254 | 254 | * Set the SQL RIGHT JOIN statment |
| 255 | 255 | * @see DatabaseQueryBuilder::join() |
| 256 | 256 | * @return object the current DatabaseQueryBuilder instance |
| 257 | 257 | */ |
| 258 | 258 | public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
| 259 | - return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 259 | + return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | /** |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | * @return object the current DatabaseQueryBuilder instance |
| 266 | 266 | */ |
| 267 | 267 | public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
| 268 | - return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 268 | + return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | /** |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | * @return object the current DatabaseQueryBuilder instance |
| 275 | 275 | */ |
| 276 | 276 | public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
| 277 | - return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 277 | + return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | /** |
@@ -283,7 +283,7 @@ discard block |
||
| 283 | 283 | * @return object the current DatabaseQueryBuilder instance |
| 284 | 284 | */ |
| 285 | 285 | public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
| 286 | - return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 286 | + return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | /** |
@@ -293,14 +293,14 @@ discard block |
||
| 293 | 293 | * @return object the current DatabaseQueryBuilder instance |
| 294 | 294 | */ |
| 295 | 295 | public function whereIsNull($field, $andOr = 'AND'){ |
| 296 | - if (is_array($field)){ |
|
| 296 | + if (is_array($field)){ |
|
| 297 | 297 | foreach($field as $f){ |
| 298 | - $this->whereIsNull($f, $andOr); |
|
| 298 | + $this->whereIsNull($f, $andOr); |
|
| 299 | + } |
|
| 300 | + } else { |
|
| 301 | + $this->setWhereStr($field.' IS NULL ', $andOr); |
|
| 299 | 302 | } |
| 300 | - } else { |
|
| 301 | - $this->setWhereStr($field.' IS NULL ', $andOr); |
|
| 302 | - } |
|
| 303 | - return $this; |
|
| 303 | + return $this; |
|
| 304 | 304 | } |
| 305 | 305 | |
| 306 | 306 | /** |
@@ -310,14 +310,14 @@ discard block |
||
| 310 | 310 | * @return object the current DatabaseQueryBuilder instance |
| 311 | 311 | */ |
| 312 | 312 | public function whereIsNotNull($field, $andOr = 'AND'){ |
| 313 | - if (is_array($field)){ |
|
| 313 | + if (is_array($field)){ |
|
| 314 | 314 | foreach($field as $f){ |
| 315 | - $this->whereIsNotNull($f, $andOr); |
|
| 315 | + $this->whereIsNotNull($f, $andOr); |
|
| 316 | 316 | } |
| 317 | - } else { |
|
| 318 | - $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
| 319 | - } |
|
| 320 | - return $this; |
|
| 317 | + } else { |
|
| 318 | + $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
| 319 | + } |
|
| 320 | + return $this; |
|
| 321 | 321 | } |
| 322 | 322 | |
| 323 | 323 | /** |
@@ -331,19 +331,19 @@ discard block |
||
| 331 | 331 | * @return object the current DatabaseQueryBuilder instance |
| 332 | 332 | */ |
| 333 | 333 | public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
| 334 | - $whereStr = ''; |
|
| 335 | - if (is_array($where)){ |
|
| 334 | + $whereStr = ''; |
|
| 335 | + if (is_array($where)){ |
|
| 336 | 336 | $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
| 337 | - } |
|
| 338 | - else{ |
|
| 337 | + } |
|
| 338 | + else{ |
|
| 339 | 339 | if (is_array($op)){ |
| 340 | - $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 340 | + $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
| 341 | 341 | } else { |
| 342 | - $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
| 342 | + $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
| 343 | 343 | } |
| 344 | - } |
|
| 345 | - $this->setWhereStr($whereStr, $andOr); |
|
| 346 | - return $this; |
|
| 344 | + } |
|
| 345 | + $this->setWhereStr($whereStr, $andOr); |
|
| 346 | + return $this; |
|
| 347 | 347 | } |
| 348 | 348 | |
| 349 | 349 | /** |
@@ -352,7 +352,7 @@ discard block |
||
| 352 | 352 | * @return object the current DatabaseQueryBuilder instance |
| 353 | 353 | */ |
| 354 | 354 | public function orWhere($where, $op = null, $val = null, $escape = true){ |
| 355 | - return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 355 | + return $this->where($where, $op, $val, '', 'OR', $escape); |
|
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | |
@@ -362,7 +362,7 @@ discard block |
||
| 362 | 362 | * @return object the current DatabaseQueryBuilder instance |
| 363 | 363 | */ |
| 364 | 364 | public function notWhere($where, $op = null, $val = null, $escape = true){ |
| 365 | - return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 365 | + return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
| 366 | 366 | } |
| 367 | 367 | |
| 368 | 368 | /** |
@@ -371,7 +371,7 @@ discard block |
||
| 371 | 371 | * @return object the current DatabaseQueryBuilder instance |
| 372 | 372 | */ |
| 373 | 373 | public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
| 374 | - return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 374 | + return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
| 375 | 375 | } |
| 376 | 376 | |
| 377 | 377 | /** |
@@ -381,16 +381,16 @@ discard block |
||
| 381 | 381 | * @return object the current DatabaseQueryBuilder instance |
| 382 | 382 | */ |
| 383 | 383 | public function groupStart($type = '', $andOr = ' AND'){ |
| 384 | - if (empty($this->where)){ |
|
| 384 | + if (empty($this->where)){ |
|
| 385 | 385 | $this->where = $type . ' ('; |
| 386 | - } else { |
|
| 387 | - if (substr(trim($this->where), -1) == '('){ |
|
| 386 | + } else { |
|
| 387 | + if (substr(trim($this->where), -1) == '('){ |
|
| 388 | 388 | $this->where .= $type . ' ('; |
| 389 | - } else { |
|
| 390 | - $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - return $this; |
|
| 389 | + } else { |
|
| 390 | + $this->where .= $andOr . ' ' . $type . ' ('; |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + return $this; |
|
| 394 | 394 | } |
| 395 | 395 | |
| 396 | 396 | /** |
@@ -399,7 +399,7 @@ discard block |
||
| 399 | 399 | * @return object the current DatabaseQueryBuilder instance |
| 400 | 400 | */ |
| 401 | 401 | public function notGroupStart(){ |
| 402 | - return $this->groupStart('NOT'); |
|
| 402 | + return $this->groupStart('NOT'); |
|
| 403 | 403 | } |
| 404 | 404 | |
| 405 | 405 | /** |
@@ -408,16 +408,16 @@ discard block |
||
| 408 | 408 | * @return object the current DatabaseQueryBuilder instance |
| 409 | 409 | */ |
| 410 | 410 | public function orGroupStart(){ |
| 411 | - return $this->groupStart('', ' OR'); |
|
| 411 | + return $this->groupStart('', ' OR'); |
|
| 412 | 412 | } |
| 413 | 413 | |
| 414 | - /** |
|
| 415 | - * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 416 | - * @see DatabaseQueryBuilder::groupStart() |
|
| 417 | - * @return object the current DatabaseQueryBuilder instance |
|
| 418 | - */ |
|
| 414 | + /** |
|
| 415 | + * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
| 416 | + * @see DatabaseQueryBuilder::groupStart() |
|
| 417 | + * @return object the current DatabaseQueryBuilder instance |
|
| 418 | + */ |
|
| 419 | 419 | public function orNotGroupStart(){ |
| 420 | - return $this->groupStart('NOT', ' OR'); |
|
| 420 | + return $this->groupStart('NOT', ' OR'); |
|
| 421 | 421 | } |
| 422 | 422 | |
| 423 | 423 | /** |
@@ -425,8 +425,8 @@ discard block |
||
| 425 | 425 | * @return object the current DatabaseQueryBuilder instance |
| 426 | 426 | */ |
| 427 | 427 | public function groupEnd(){ |
| 428 | - $this->where .= ')'; |
|
| 429 | - return $this; |
|
| 428 | + $this->where .= ')'; |
|
| 429 | + return $this; |
|
| 430 | 430 | } |
| 431 | 431 | |
| 432 | 432 | /** |
@@ -439,17 +439,17 @@ discard block |
||
| 439 | 439 | * @return object the current DatabaseQueryBuilder instance |
| 440 | 440 | */ |
| 441 | 441 | public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
| 442 | - $_keys = array(); |
|
| 443 | - foreach ($keys as $k => $v){ |
|
| 442 | + $_keys = array(); |
|
| 443 | + foreach ($keys as $k => $v){ |
|
| 444 | 444 | if (is_null($v)){ |
| 445 | - $v = ''; |
|
| 445 | + $v = ''; |
|
| 446 | 446 | } |
| 447 | 447 | $_keys[] = (is_numeric($v) ? $v : $this->escape($v, $escape)); |
| 448 | - } |
|
| 449 | - $keys = implode(', ', $_keys); |
|
| 450 | - $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 451 | - $this->setWhereStr($whereStr, $andOr); |
|
| 452 | - return $this; |
|
| 448 | + } |
|
| 449 | + $keys = implode(', ', $_keys); |
|
| 450 | + $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
| 451 | + $this->setWhereStr($whereStr, $andOr); |
|
| 452 | + return $this; |
|
| 453 | 453 | } |
| 454 | 454 | |
| 455 | 455 | /** |
@@ -458,7 +458,7 @@ discard block |
||
| 458 | 458 | * @return object the current DatabaseQueryBuilder instance |
| 459 | 459 | */ |
| 460 | 460 | public function notIn($field, array $keys, $escape = true){ |
| 461 | - return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 461 | + return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
| 462 | 462 | } |
| 463 | 463 | |
| 464 | 464 | /** |
@@ -467,7 +467,7 @@ discard block |
||
| 467 | 467 | * @return object the current DatabaseQueryBuilder instance |
| 468 | 468 | */ |
| 469 | 469 | public function orIn($field, array $keys, $escape = true){ |
| 470 | - return $this->in($field, $keys, '', 'OR', $escape); |
|
| 470 | + return $this->in($field, $keys, '', 'OR', $escape); |
|
| 471 | 471 | } |
| 472 | 472 | |
| 473 | 473 | /** |
@@ -476,7 +476,7 @@ discard block |
||
| 476 | 476 | * @return object the current DatabaseQueryBuilder instance |
| 477 | 477 | */ |
| 478 | 478 | public function orNotIn($field, array $keys, $escape = true){ |
| 479 | - return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 479 | + return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
| 480 | 480 | } |
| 481 | 481 | |
| 482 | 482 | /** |
@@ -490,15 +490,15 @@ discard block |
||
| 490 | 490 | * @return object the current DatabaseQueryBuilder instance |
| 491 | 491 | */ |
| 492 | 492 | public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
| 493 | - if (is_null($value1)){ |
|
| 493 | + if (is_null($value1)){ |
|
| 494 | 494 | $value1 = ''; |
| 495 | - } |
|
| 496 | - if (is_null($value2)){ |
|
| 495 | + } |
|
| 496 | + if (is_null($value2)){ |
|
| 497 | 497 | $value2 = ''; |
| 498 | - } |
|
| 499 | - $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
|
| 500 | - $this->setWhereStr($whereStr, $andOr); |
|
| 501 | - return $this; |
|
| 498 | + } |
|
| 499 | + $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
|
| 500 | + $this->setWhereStr($whereStr, $andOr); |
|
| 501 | + return $this; |
|
| 502 | 502 | } |
| 503 | 503 | |
| 504 | 504 | /** |
@@ -507,7 +507,7 @@ discard block |
||
| 507 | 507 | * @return object the current DatabaseQueryBuilder instance |
| 508 | 508 | */ |
| 509 | 509 | public function notBetween($field, $value1, $value2, $escape = true){ |
| 510 | - return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 510 | + return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | /** |
@@ -516,7 +516,7 @@ discard block |
||
| 516 | 516 | * @return object the current DatabaseQueryBuilder instance |
| 517 | 517 | */ |
| 518 | 518 | public function orBetween($field, $value1, $value2, $escape = true){ |
| 519 | - return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 519 | + return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
| 520 | 520 | } |
| 521 | 521 | |
| 522 | 522 | /** |
@@ -525,7 +525,7 @@ discard block |
||
| 525 | 525 | * @return object the current DatabaseQueryBuilder instance |
| 526 | 526 | */ |
| 527 | 527 | public function orNotBetween($field, $value1, $value2, $escape = true){ |
| 528 | - return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 528 | + return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
| 529 | 529 | } |
| 530 | 530 | |
| 531 | 531 | /** |
@@ -538,11 +538,11 @@ discard block |
||
| 538 | 538 | * @return object the current DatabaseQueryBuilder instance |
| 539 | 539 | */ |
| 540 | 540 | public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
| 541 | - if (empty($data)){ |
|
| 541 | + if (empty($data)){ |
|
| 542 | 542 | $data = ''; |
| 543 | - } |
|
| 544 | - $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
|
| 545 | - return $this; |
|
| 543 | + } |
|
| 544 | + $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
|
| 545 | + return $this; |
|
| 546 | 546 | } |
| 547 | 547 | |
| 548 | 548 | /** |
@@ -551,7 +551,7 @@ discard block |
||
| 551 | 551 | * @return object the current DatabaseQueryBuilder instance |
| 552 | 552 | */ |
| 553 | 553 | public function orLike($field, $data, $escape = true){ |
| 554 | - return $this->like($field, $data, '', 'OR', $escape); |
|
| 554 | + return $this->like($field, $data, '', 'OR', $escape); |
|
| 555 | 555 | } |
| 556 | 556 | |
| 557 | 557 | /** |
@@ -560,7 +560,7 @@ discard block |
||
| 560 | 560 | * @return object the current DatabaseQueryBuilder instance |
| 561 | 561 | */ |
| 562 | 562 | public function notLike($field, $data, $escape = true){ |
| 563 | - return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 563 | + return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
| 564 | 564 | } |
| 565 | 565 | |
| 566 | 566 | /** |
@@ -569,7 +569,7 @@ discard block |
||
| 569 | 569 | * @return object the current DatabaseQueryBuilder instance |
| 570 | 570 | */ |
| 571 | 571 | public function orNotLike($field, $data, $escape = true){ |
| 572 | - return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 572 | + return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
| 573 | 573 | } |
| 574 | 574 | |
| 575 | 575 | /** |
@@ -580,16 +580,16 @@ discard block |
||
| 580 | 580 | * @return object the current DatabaseQueryBuilder instance |
| 581 | 581 | */ |
| 582 | 582 | public function limit($limit, $limitEnd = null){ |
| 583 | - if (empty($limit)){ |
|
| 583 | + if (empty($limit)){ |
|
| 584 | 584 | $limit = 0; |
| 585 | - } |
|
| 586 | - if (! is_null($limitEnd)){ |
|
| 585 | + } |
|
| 586 | + if (! is_null($limitEnd)){ |
|
| 587 | 587 | $this->limit = $limit . ', ' . $limitEnd; |
| 588 | - } |
|
| 589 | - else{ |
|
| 588 | + } |
|
| 589 | + else{ |
|
| 590 | 590 | $this->limit = $limit; |
| 591 | - } |
|
| 592 | - return $this; |
|
| 591 | + } |
|
| 592 | + return $this; |
|
| 593 | 593 | } |
| 594 | 594 | |
| 595 | 595 | /** |
@@ -599,15 +599,15 @@ discard block |
||
| 599 | 599 | * @return object the current DatabaseQueryBuilder instance |
| 600 | 600 | */ |
| 601 | 601 | public function orderBy($orderBy, $orderDir = ' ASC'){ |
| 602 | - if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 602 | + if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
| 603 | 603 | $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
| 604 | - } |
|
| 605 | - else{ |
|
| 604 | + } |
|
| 605 | + else{ |
|
| 606 | 606 | $this->orderBy = empty($this->orderBy) |
| 607 | - ? ($orderBy . ' ' . strtoupper($orderDir)) |
|
| 608 | - : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 609 | - } |
|
| 610 | - return $this; |
|
| 607 | + ? ($orderBy . ' ' . strtoupper($orderDir)) |
|
| 608 | + : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
| 609 | + } |
|
| 610 | + return $this; |
|
| 611 | 611 | } |
| 612 | 612 | |
| 613 | 613 | /** |
@@ -616,13 +616,13 @@ discard block |
||
| 616 | 616 | * @return object the current DatabaseQueryBuilder instance |
| 617 | 617 | */ |
| 618 | 618 | public function groupBy($field){ |
| 619 | - if (is_array($field)){ |
|
| 619 | + if (is_array($field)){ |
|
| 620 | 620 | $this->groupBy = implode(', ', $field); |
| 621 | - } |
|
| 622 | - else{ |
|
| 621 | + } |
|
| 622 | + else{ |
|
| 623 | 623 | $this->groupBy = $field; |
| 624 | - } |
|
| 625 | - return $this; |
|
| 624 | + } |
|
| 625 | + return $this; |
|
| 626 | 626 | } |
| 627 | 627 | |
| 628 | 628 | /** |
@@ -634,22 +634,22 @@ discard block |
||
| 634 | 634 | * @return object the current DatabaseQueryBuilder instance |
| 635 | 635 | */ |
| 636 | 636 | public function having($field, $op = null, $val = null, $escape = true){ |
| 637 | - if (is_array($op)){ |
|
| 637 | + if (is_array($op)){ |
|
| 638 | 638 | $this->having = $this->getHavingStrIfOperatorIsArray($field, $op, $escape); |
| 639 | - } |
|
| 640 | - else if (! in_array($op, $this->operatorList)){ |
|
| 639 | + } |
|
| 640 | + else if (! in_array($op, $this->operatorList)){ |
|
| 641 | 641 | if (is_null($op)){ |
| 642 | - $op = ''; |
|
| 642 | + $op = ''; |
|
| 643 | 643 | } |
| 644 | 644 | $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
| 645 | - } |
|
| 646 | - else{ |
|
| 645 | + } |
|
| 646 | + else{ |
|
| 647 | 647 | if (is_null($val)){ |
| 648 | - $val = ''; |
|
| 648 | + $val = ''; |
|
| 649 | 649 | } |
| 650 | 650 | $this->having = $field . ' ' . $op . ' ' . ($this->escape($val, $escape)); |
| 651 | - } |
|
| 652 | - return $this; |
|
| 651 | + } |
|
| 652 | + return $this; |
|
| 653 | 653 | } |
| 654 | 654 | |
| 655 | 655 | /** |
@@ -659,12 +659,12 @@ discard block |
||
| 659 | 659 | * @return object the current DatabaseQueryBuilder instance |
| 660 | 660 | */ |
| 661 | 661 | public function insert($data = array(), $escape = true){ |
| 662 | - $columns = array_keys($data); |
|
| 663 | - $column = implode(',', $columns); |
|
| 664 | - $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 662 | + $columns = array_keys($data); |
|
| 663 | + $column = implode(',', $columns); |
|
| 664 | + $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
| 665 | 665 | |
| 666 | - $this->query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 667 | - return $this; |
|
| 666 | + $this->query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
| 667 | + return $this; |
|
| 668 | 668 | } |
| 669 | 669 | |
| 670 | 670 | /** |
@@ -674,25 +674,25 @@ discard block |
||
| 674 | 674 | * @return object the current DatabaseQueryBuilder instance |
| 675 | 675 | */ |
| 676 | 676 | public function update($data = array(), $escape = true){ |
| 677 | - $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 678 | - $values = array(); |
|
| 679 | - foreach ($data as $column => $val){ |
|
| 677 | + $query = 'UPDATE ' . $this->from . ' SET '; |
|
| 678 | + $values = array(); |
|
| 679 | + foreach ($data as $column => $val){ |
|
| 680 | 680 | $values[] = $column . ' = ' . ($this->escape($val, $escape)); |
| 681 | - } |
|
| 682 | - $query .= implode(', ', $values); |
|
| 683 | - if (! empty($this->where)){ |
|
| 681 | + } |
|
| 682 | + $query .= implode(', ', $values); |
|
| 683 | + if (! empty($this->where)){ |
|
| 684 | 684 | $query .= ' WHERE ' . $this->where; |
| 685 | - } |
|
| 685 | + } |
|
| 686 | 686 | |
| 687 | - if (! empty($this->orderBy)){ |
|
| 687 | + if (! empty($this->orderBy)){ |
|
| 688 | 688 | $query .= ' ORDER BY ' . $this->orderBy; |
| 689 | - } |
|
| 689 | + } |
|
| 690 | 690 | |
| 691 | - if (! empty($this->limit)){ |
|
| 691 | + if (! empty($this->limit)){ |
|
| 692 | 692 | $query .= ' LIMIT ' . $this->limit; |
| 693 | - } |
|
| 694 | - $this->query = $query; |
|
| 695 | - return $this; |
|
| 693 | + } |
|
| 694 | + $this->query = $query; |
|
| 695 | + return $this; |
|
| 696 | 696 | } |
| 697 | 697 | |
| 698 | 698 | /** |
@@ -700,25 +700,25 @@ discard block |
||
| 700 | 700 | * @return object the current DatabaseQueryBuilder instance |
| 701 | 701 | */ |
| 702 | 702 | public function delete(){ |
| 703 | - $query = 'DELETE FROM ' . $this->from; |
|
| 704 | - $isTruncate = $query; |
|
| 705 | - if (! empty($this->where)){ |
|
| 706 | - $query .= ' WHERE ' . $this->where; |
|
| 707 | - } |
|
| 703 | + $query = 'DELETE FROM ' . $this->from; |
|
| 704 | + $isTruncate = $query; |
|
| 705 | + if (! empty($this->where)){ |
|
| 706 | + $query .= ' WHERE ' . $this->where; |
|
| 707 | + } |
|
| 708 | 708 | |
| 709 | - if (! empty($this->orderBy)){ |
|
| 710 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 711 | - } |
|
| 709 | + if (! empty($this->orderBy)){ |
|
| 710 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 711 | + } |
|
| 712 | 712 | |
| 713 | - if (! empty($this->limit)){ |
|
| 714 | - $query .= ' LIMIT ' . $this->limit; |
|
| 715 | - } |
|
| 713 | + if (! empty($this->limit)){ |
|
| 714 | + $query .= ' LIMIT ' . $this->limit; |
|
| 715 | + } |
|
| 716 | 716 | |
| 717 | - if ($isTruncate == $query && $this->driver != 'sqlite'){ |
|
| 718 | - $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 719 | - } |
|
| 720 | - $this->query = $query; |
|
| 721 | - return $this; |
|
| 717 | + if ($isTruncate == $query && $this->driver != 'sqlite'){ |
|
| 718 | + $query = 'TRUNCATE TABLE ' . $this->from; |
|
| 719 | + } |
|
| 720 | + $this->query = $query; |
|
| 721 | + return $this; |
|
| 722 | 722 | } |
| 723 | 723 | |
| 724 | 724 | /** |
@@ -728,7 +728,7 @@ discard block |
||
| 728 | 728 | * @return mixed the data after escaped or the same data if not |
| 729 | 729 | */ |
| 730 | 730 | public function escape($data, $escaped = true){ |
| 731 | - return $escaped |
|
| 731 | + return $escaped |
|
| 732 | 732 | ? $this->getPdo()->quote(trim($data)) |
| 733 | 733 | : $data; |
| 734 | 734 | } |
@@ -739,126 +739,126 @@ discard block |
||
| 739 | 739 | * @return string |
| 740 | 740 | */ |
| 741 | 741 | public function getQuery(){ |
| 742 | - //INSERT, UPDATE, DELETE already set it, if is the SELECT we need set it now |
|
| 743 | - if(empty($this->query)){ |
|
| 744 | - $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 745 | - if (! empty($this->join)){ |
|
| 746 | - $query .= $this->join; |
|
| 747 | - } |
|
| 742 | + //INSERT, UPDATE, DELETE already set it, if is the SELECT we need set it now |
|
| 743 | + if(empty($this->query)){ |
|
| 744 | + $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
| 745 | + if (! empty($this->join)){ |
|
| 746 | + $query .= $this->join; |
|
| 747 | + } |
|
| 748 | 748 | |
| 749 | - if (! empty($this->where)){ |
|
| 750 | - $query .= ' WHERE ' . $this->where; |
|
| 751 | - } |
|
| 749 | + if (! empty($this->where)){ |
|
| 750 | + $query .= ' WHERE ' . $this->where; |
|
| 751 | + } |
|
| 752 | 752 | |
| 753 | - if (! empty($this->groupBy)){ |
|
| 754 | - $query .= ' GROUP BY ' . $this->groupBy; |
|
| 755 | - } |
|
| 753 | + if (! empty($this->groupBy)){ |
|
| 754 | + $query .= ' GROUP BY ' . $this->groupBy; |
|
| 755 | + } |
|
| 756 | 756 | |
| 757 | - if (! empty($this->having)){ |
|
| 758 | - $query .= ' HAVING ' . $this->having; |
|
| 759 | - } |
|
| 757 | + if (! empty($this->having)){ |
|
| 758 | + $query .= ' HAVING ' . $this->having; |
|
| 759 | + } |
|
| 760 | 760 | |
| 761 | - if (! empty($this->orderBy)){ |
|
| 762 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
| 763 | - } |
|
| 761 | + if (! empty($this->orderBy)){ |
|
| 762 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
| 763 | + } |
|
| 764 | 764 | |
| 765 | - if (! empty($this->limit)){ |
|
| 766 | - $query .= ' LIMIT ' . $this->limit; |
|
| 767 | - } |
|
| 768 | - $this->query = $query; |
|
| 769 | - } |
|
| 770 | - return $this->query; |
|
| 765 | + if (! empty($this->limit)){ |
|
| 766 | + $query .= ' LIMIT ' . $this->limit; |
|
| 767 | + } |
|
| 768 | + $this->query = $query; |
|
| 769 | + } |
|
| 770 | + return $this->query; |
|
| 771 | 771 | } |
| 772 | 772 | |
| 773 | 773 | |
| 774 | - /** |
|
| 775 | - * Return the PDO instance |
|
| 776 | - * @return PDO |
|
| 777 | - */ |
|
| 774 | + /** |
|
| 775 | + * Return the PDO instance |
|
| 776 | + * @return PDO |
|
| 777 | + */ |
|
| 778 | 778 | public function getPdo(){ |
| 779 | - return $this->pdo; |
|
| 779 | + return $this->pdo; |
|
| 780 | 780 | } |
| 781 | 781 | |
| 782 | 782 | /** |
| 783 | 783 | * Set the PDO instance |
| 784 | 784 | * @param PDO $pdo the pdo object |
| 785 | - * @return object DatabaseQueryBuilder |
|
| 785 | + * @return object DatabaseQueryBuilder |
|
| 786 | 786 | */ |
| 787 | 787 | public function setPdo(PDO $pdo = null){ |
| 788 | - $this->pdo = $pdo; |
|
| 789 | - return $this; |
|
| 788 | + $this->pdo = $pdo; |
|
| 789 | + return $this; |
|
| 790 | 790 | } |
| 791 | 791 | |
| 792 | - /** |
|
| 793 | - * Return the database table prefix |
|
| 794 | - * @return string |
|
| 795 | - */ |
|
| 792 | + /** |
|
| 793 | + * Return the database table prefix |
|
| 794 | + * @return string |
|
| 795 | + */ |
|
| 796 | 796 | public function getPrefix(){ |
| 797 | - return $this->prefix; |
|
| 797 | + return $this->prefix; |
|
| 798 | 798 | } |
| 799 | 799 | |
| 800 | 800 | /** |
| 801 | 801 | * Set the database table prefix |
| 802 | 802 | * @param string $prefix the new prefix |
| 803 | - * @return object DatabaseQueryBuilder |
|
| 803 | + * @return object DatabaseQueryBuilder |
|
| 804 | 804 | */ |
| 805 | 805 | public function setPrefix($prefix){ |
| 806 | - $this->prefix = $prefix; |
|
| 807 | - return $this; |
|
| 806 | + $this->prefix = $prefix; |
|
| 807 | + return $this; |
|
| 808 | 808 | } |
| 809 | 809 | |
| 810 | - /** |
|
| 811 | - * Return the database driver |
|
| 812 | - * @return string |
|
| 813 | - */ |
|
| 810 | + /** |
|
| 811 | + * Return the database driver |
|
| 812 | + * @return string |
|
| 813 | + */ |
|
| 814 | 814 | public function getDriver(){ |
| 815 | - return $this->driver; |
|
| 815 | + return $this->driver; |
|
| 816 | 816 | } |
| 817 | 817 | |
| 818 | 818 | /** |
| 819 | 819 | * Set the database driver |
| 820 | 820 | * @param string $driver the new driver |
| 821 | - * @return object DatabaseQueryBuilder |
|
| 821 | + * @return object DatabaseQueryBuilder |
|
| 822 | 822 | */ |
| 823 | 823 | public function setDriver($driver){ |
| 824 | - $this->driver = $driver; |
|
| 825 | - return $this; |
|
| 824 | + $this->driver = $driver; |
|
| 825 | + return $this; |
|
| 826 | 826 | } |
| 827 | 827 | |
| 828 | - /** |
|
| 829 | - * Reset the DatabaseQueryBuilder class attributs to the initial values before each query. |
|
| 830 | - * @return object the current DatabaseQueryBuilder instance |
|
| 831 | - */ |
|
| 828 | + /** |
|
| 829 | + * Reset the DatabaseQueryBuilder class attributs to the initial values before each query. |
|
| 830 | + * @return object the current DatabaseQueryBuilder instance |
|
| 831 | + */ |
|
| 832 | 832 | public function reset(){ |
| 833 | - $this->select = '*'; |
|
| 834 | - $this->from = null; |
|
| 835 | - $this->where = null; |
|
| 836 | - $this->limit = null; |
|
| 837 | - $this->orderBy = null; |
|
| 838 | - $this->groupBy = null; |
|
| 839 | - $this->having = null; |
|
| 840 | - $this->join = null; |
|
| 841 | - $this->query = null; |
|
| 842 | - return $this; |
|
| 843 | - } |
|
| 844 | - |
|
| 845 | - /** |
|
| 846 | - * Get the SQL HAVING clause when operator argument is an array |
|
| 847 | - * @see DatabaseQueryBuilder::having |
|
| 848 | - * |
|
| 849 | - * @return string |
|
| 850 | - */ |
|
| 833 | + $this->select = '*'; |
|
| 834 | + $this->from = null; |
|
| 835 | + $this->where = null; |
|
| 836 | + $this->limit = null; |
|
| 837 | + $this->orderBy = null; |
|
| 838 | + $this->groupBy = null; |
|
| 839 | + $this->having = null; |
|
| 840 | + $this->join = null; |
|
| 841 | + $this->query = null; |
|
| 842 | + return $this; |
|
| 843 | + } |
|
| 844 | + |
|
| 845 | + /** |
|
| 846 | + * Get the SQL HAVING clause when operator argument is an array |
|
| 847 | + * @see DatabaseQueryBuilder::having |
|
| 848 | + * |
|
| 849 | + * @return string |
|
| 850 | + */ |
|
| 851 | 851 | protected function getHavingStrIfOperatorIsArray($field, $op = null, $escape = true){ |
| 852 | 852 | $x = explode('?', $field); |
| 853 | 853 | $w = ''; |
| 854 | 854 | foreach($x as $k => $v){ |
| 855 | - if (!empty($v)){ |
|
| 855 | + if (!empty($v)){ |
|
| 856 | 856 | if (! isset($op[$k])){ |
| 857 | - $op[$k] = ''; |
|
| 857 | + $op[$k] = ''; |
|
| 858 | + } |
|
| 859 | + $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
|
| 860 | + } |
|
| 858 | 861 | } |
| 859 | - $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
|
| 860 | - } |
|
| 861 | - } |
|
| 862 | 862 | return $w; |
| 863 | 863 | } |
| 864 | 864 | |
@@ -870,35 +870,35 @@ discard block |
||
| 870 | 870 | * @return string |
| 871 | 871 | */ |
| 872 | 872 | protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
| 873 | - $_where = array(); |
|
| 874 | - foreach ($where as $column => $data){ |
|
| 873 | + $_where = array(); |
|
| 874 | + foreach ($where as $column => $data){ |
|
| 875 | 875 | if (is_null($data)){ |
| 876 | - $data = ''; |
|
| 876 | + $data = ''; |
|
| 877 | 877 | } |
| 878 | 878 | $_where[] = $type . $column . ' = ' . ($this->escape($data, $escape)); |
| 879 | - } |
|
| 880 | - $where = implode(' '.$andOr.' ', $_where); |
|
| 881 | - return $where; |
|
| 879 | + } |
|
| 880 | + $where = implode(' '.$andOr.' ', $_where); |
|
| 881 | + return $where; |
|
| 882 | 882 | } |
| 883 | 883 | |
| 884 | - /** |
|
| 885 | - * Get the SQL WHERE clause when operator argument is an array |
|
| 886 | - * @see DatabaseQueryBuilder::where |
|
| 887 | - * |
|
| 888 | - * @return string |
|
| 889 | - */ |
|
| 884 | + /** |
|
| 885 | + * Get the SQL WHERE clause when operator argument is an array |
|
| 886 | + * @see DatabaseQueryBuilder::where |
|
| 887 | + * |
|
| 888 | + * @return string |
|
| 889 | + */ |
|
| 890 | 890 | protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
| 891 | - $x = explode('?', $where); |
|
| 892 | - $w = ''; |
|
| 893 | - foreach($x as $k => $v){ |
|
| 891 | + $x = explode('?', $where); |
|
| 892 | + $w = ''; |
|
| 893 | + foreach($x as $k => $v){ |
|
| 894 | 894 | if (! empty($v)){ |
| 895 | 895 | if (isset($op[$k]) && is_null($op[$k])){ |
| 896 | - $op[$k] = ''; |
|
| 896 | + $op[$k] = ''; |
|
| 897 | 897 | } |
| 898 | 898 | $w .= $type . $v . (isset($op[$k]) ? ($this->escape($op[$k], $escape)) : ''); |
| 899 | 899 | } |
| 900 | - } |
|
| 901 | - return $w; |
|
| 900 | + } |
|
| 901 | + return $w; |
|
| 902 | 902 | } |
| 903 | 903 | |
| 904 | 904 | /** |
@@ -908,53 +908,53 @@ discard block |
||
| 908 | 908 | * @return string |
| 909 | 909 | */ |
| 910 | 910 | protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
| 911 | - $w = ''; |
|
| 912 | - if (! in_array((string)$op, $this->operatorList)){ |
|
| 913 | - if (is_null($op)){ |
|
| 911 | + $w = ''; |
|
| 912 | + if (! in_array((string)$op, $this->operatorList)){ |
|
| 913 | + if (is_null($op)){ |
|
| 914 | 914 | $op = ''; |
| 915 | - } |
|
| 916 | - $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
|
| 915 | + } |
|
| 916 | + $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
|
| 917 | 917 | } else { |
| 918 | - if (is_null($val)){ |
|
| 918 | + if (is_null($val)){ |
|
| 919 | 919 | $val = ''; |
| 920 | - } |
|
| 921 | - $w = $type . $where . $op . ($this->escape($val, $escape)); |
|
| 920 | + } |
|
| 921 | + $w = $type . $where . $op . ($this->escape($val, $escape)); |
|
| 922 | 922 | } |
| 923 | 923 | return $w; |
| 924 | - } |
|
| 925 | - |
|
| 926 | - /** |
|
| 927 | - * Set the $this->where property |
|
| 928 | - * @param string $whereStr the WHERE clause string |
|
| 929 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 930 | - */ |
|
| 931 | - protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 924 | + } |
|
| 925 | + |
|
| 926 | + /** |
|
| 927 | + * Set the $this->where property |
|
| 928 | + * @param string $whereStr the WHERE clause string |
|
| 929 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
| 930 | + */ |
|
| 931 | + protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
| 932 | 932 | if (empty($this->where)){ |
| 933 | - $this->where = $whereStr; |
|
| 933 | + $this->where = $whereStr; |
|
| 934 | 934 | } else { |
| 935 | - if (substr(trim($this->where), -1) == '('){ |
|
| 935 | + if (substr(trim($this->where), -1) == '('){ |
|
| 936 | 936 | $this->where = $this->where . ' ' . $whereStr; |
| 937 | - } else { |
|
| 937 | + } else { |
|
| 938 | 938 | $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
| 939 | - } |
|
| 939 | + } |
|
| 940 | + } |
|
| 940 | 941 | } |
| 941 | - } |
|
| 942 | 942 | |
| 943 | 943 | |
| 944 | - /** |
|
| 945 | - * Set the SQL SELECT for function MIN, MAX, SUM, AVG, COUNT, AVG |
|
| 946 | - * @param string $clause the clause type like MIN, MAX, etc. |
|
| 947 | - * @see DatabaseQueryBuilder::min |
|
| 948 | - * @see DatabaseQueryBuilder::max |
|
| 949 | - * @see DatabaseQueryBuilder::sum |
|
| 950 | - * @see DatabaseQueryBuilder::count |
|
| 951 | - * @see DatabaseQueryBuilder::avg |
|
| 952 | - * @return object |
|
| 953 | - */ |
|
| 944 | + /** |
|
| 945 | + * Set the SQL SELECT for function MIN, MAX, SUM, AVG, COUNT, AVG |
|
| 946 | + * @param string $clause the clause type like MIN, MAX, etc. |
|
| 947 | + * @see DatabaseQueryBuilder::min |
|
| 948 | + * @see DatabaseQueryBuilder::max |
|
| 949 | + * @see DatabaseQueryBuilder::sum |
|
| 950 | + * @see DatabaseQueryBuilder::count |
|
| 951 | + * @see DatabaseQueryBuilder::avg |
|
| 952 | + * @return object |
|
| 953 | + */ |
|
| 954 | 954 | protected function select_min_max_sum_count_avg($clause, $field, $name = null){ |
| 955 | - $clause = strtoupper($clause); |
|
| 956 | - $func = $clause . '(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 957 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 958 | - return $this; |
|
| 955 | + $clause = strtoupper($clause); |
|
| 956 | + $func = $clause . '(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
| 957 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
| 958 | + return $this; |
|
| 959 | 959 | } |
| 960 | 960 | } |
@@ -1,182 +1,182 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or 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') or 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 | - /** |
|
| 28 | - * This class represent the event dispatcher management, permit to record the listener and |
|
| 29 | - * also to dispatch the event |
|
| 30 | - */ |
|
| 27 | + /** |
|
| 28 | + * This class represent the event dispatcher management, permit to record the listener and |
|
| 29 | + * also to dispatch the event |
|
| 30 | + */ |
|
| 31 | 31 | |
| 32 | - class EventDispatcher{ |
|
| 32 | + class EventDispatcher{ |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The list of the registered listeners |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - private $listeners = array(); |
|
| 34 | + /** |
|
| 35 | + * The list of the registered listeners |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + private $listeners = array(); |
|
| 39 | 39 | |
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The logger instance |
|
| 43 | - * @var Log |
|
| 44 | - */ |
|
| 45 | - private $logger; |
|
| 41 | + /** |
|
| 42 | + * The logger instance |
|
| 43 | + * @var Log |
|
| 44 | + */ |
|
| 45 | + private $logger; |
|
| 46 | 46 | |
| 47 | - public function __construct(){ |
|
| 48 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | - $this->logger->setLogger('Library::EventDispatcher'); |
|
| 50 | - } |
|
| 47 | + public function __construct(){ |
|
| 48 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 49 | + $this->logger->setLogger('Library::EventDispatcher'); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Register new listener |
|
| 54 | - * @param string $eventName the name of the event to register for |
|
| 55 | - * @param callable $listener the function or class method to receive the event information after dispatch |
|
| 56 | - */ |
|
| 57 | - public function addListener($eventName, callable $listener){ |
|
| 58 | - $this->logger->debug('Adding new event listener for the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 59 | - if(! isset($this->listeners[$eventName])){ |
|
| 60 | - $this->logger->info('This event does not have the registered event listener before, adding new one'); |
|
| 61 | - $this->listeners[$eventName] = array(); |
|
| 62 | - } |
|
| 63 | - else{ |
|
| 64 | - $this->logger->info('This event already have the registered listener, add this listener to the list'); |
|
| 65 | - } |
|
| 66 | - $this->listeners[$eventName][] = $listener; |
|
| 67 | - } |
|
| 52 | + /** |
|
| 53 | + * Register new listener |
|
| 54 | + * @param string $eventName the name of the event to register for |
|
| 55 | + * @param callable $listener the function or class method to receive the event information after dispatch |
|
| 56 | + */ |
|
| 57 | + public function addListener($eventName, callable $listener){ |
|
| 58 | + $this->logger->debug('Adding new event listener for the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 59 | + if(! isset($this->listeners[$eventName])){ |
|
| 60 | + $this->logger->info('This event does not have the registered event listener before, adding new one'); |
|
| 61 | + $this->listeners[$eventName] = array(); |
|
| 62 | + } |
|
| 63 | + else{ |
|
| 64 | + $this->logger->info('This event already have the registered listener, add this listener to the list'); |
|
| 65 | + } |
|
| 66 | + $this->listeners[$eventName][] = $listener; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Remove the event listener from list |
|
| 71 | - * @param string $eventName the event name |
|
| 72 | - * @param callable $listener the listener callback |
|
| 73 | - */ |
|
| 74 | - public function removeListener($eventName, callable $listener){ |
|
| 75 | - $this->logger->debug('Removing of the event listener, the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 76 | - if(isset($this->listeners[$eventName])){ |
|
| 77 | - $this->logger->info('This event have the listeners, check if this listener exists'); |
|
| 78 | - if(false !== $index = array_search($listener, $this->listeners[$eventName], true)){ |
|
| 79 | - $this->logger->info('Found the listener at index [' .$index. '] remove it'); |
|
| 80 | - unset($this->listeners[$eventName][$index]); |
|
| 81 | - } |
|
| 82 | - else{ |
|
| 83 | - $this->logger->info('Cannot found this listener in the event listener list'); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - else{ |
|
| 87 | - $this->logger->info('This event does not have this listener ignore remove'); |
|
| 88 | - } |
|
| 89 | - } |
|
| 69 | + /** |
|
| 70 | + * Remove the event listener from list |
|
| 71 | + * @param string $eventName the event name |
|
| 72 | + * @param callable $listener the listener callback |
|
| 73 | + */ |
|
| 74 | + public function removeListener($eventName, callable $listener){ |
|
| 75 | + $this->logger->debug('Removing of the event listener, the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
| 76 | + if(isset($this->listeners[$eventName])){ |
|
| 77 | + $this->logger->info('This event have the listeners, check if this listener exists'); |
|
| 78 | + if(false !== $index = array_search($listener, $this->listeners[$eventName], true)){ |
|
| 79 | + $this->logger->info('Found the listener at index [' .$index. '] remove it'); |
|
| 80 | + unset($this->listeners[$eventName][$index]); |
|
| 81 | + } |
|
| 82 | + else{ |
|
| 83 | + $this->logger->info('Cannot found this listener in the event listener list'); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + else{ |
|
| 87 | + $this->logger->info('This event does not have this listener ignore remove'); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Remove all the event listener. If event name is null will remove all listeners, else will just |
|
| 93 | - * remove all listeners for this event |
|
| 94 | - * @param string $eventName the event name |
|
| 95 | - */ |
|
| 96 | - public function removeAllListener($eventName = null){ |
|
| 97 | - $this->logger->debug('Removing of all event listener, the event name [' .$eventName. ']'); |
|
| 98 | - if($eventName !== null && isset($this->listeners[$eventName])){ |
|
| 99 | - $this->logger->info('The event name is set of exist in the listener just remove all event listener for this event'); |
|
| 100 | - unset($this->listeners[$eventName]); |
|
| 101 | - } |
|
| 102 | - else{ |
|
| 103 | - $this->logger->info('The event name is not set or does not exist in the listener, so remove all event listener'); |
|
| 104 | - $this->listeners = array(); |
|
| 105 | - } |
|
| 106 | - } |
|
| 91 | + /** |
|
| 92 | + * Remove all the event listener. If event name is null will remove all listeners, else will just |
|
| 93 | + * remove all listeners for this event |
|
| 94 | + * @param string $eventName the event name |
|
| 95 | + */ |
|
| 96 | + public function removeAllListener($eventName = null){ |
|
| 97 | + $this->logger->debug('Removing of all event listener, the event name [' .$eventName. ']'); |
|
| 98 | + if($eventName !== null && isset($this->listeners[$eventName])){ |
|
| 99 | + $this->logger->info('The event name is set of exist in the listener just remove all event listener for this event'); |
|
| 100 | + unset($this->listeners[$eventName]); |
|
| 101 | + } |
|
| 102 | + else{ |
|
| 103 | + $this->logger->info('The event name is not set or does not exist in the listener, so remove all event listener'); |
|
| 104 | + $this->listeners = array(); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Get the list of listener for this event |
|
| 110 | - * @param string $eventName the event name |
|
| 111 | - * @return array the listeners for this event or empty array if this event does not contain any listener |
|
| 112 | - */ |
|
| 113 | - public function getListeners($eventName){ |
|
| 114 | - return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : array(); |
|
| 115 | - } |
|
| 108 | + /** |
|
| 109 | + * Get the list of listener for this event |
|
| 110 | + * @param string $eventName the event name |
|
| 111 | + * @return array the listeners for this event or empty array if this event does not contain any listener |
|
| 112 | + */ |
|
| 113 | + public function getListeners($eventName){ |
|
| 114 | + return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : array(); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Dispatch the event to the registered listeners. |
|
| 119 | - * @param mixed|object $event the event information |
|
| 120 | - * @return void|object if event need return, will return the final EventInfo object. |
|
| 121 | - */ |
|
| 122 | - public function dispatch($event){ |
|
| 123 | - if(! $event || !$event instanceof EventInfo){ |
|
| 124 | - $this->logger->info('The event is not set or is not an instance of "EventInfo" create the default "EventInfo" object to use instead of.'); |
|
| 125 | - $event = new EventInfo((string) $event); |
|
| 126 | - } |
|
| 127 | - $this->logger->debug('Dispatch to the event listener, the event [' .stringfy_vars($event). ']'); |
|
| 128 | - if(isset($event->stop) && $event->stop){ |
|
| 129 | - $this->logger->info('This event need stopped, no need call any listener'); |
|
| 130 | - return; |
|
| 131 | - } |
|
| 132 | - if($event->returnBack){ |
|
| 133 | - $this->logger->info('This event need return back, return the result for future use'); |
|
| 134 | - return $this->dispatchToListerners($event); |
|
| 135 | - } |
|
| 136 | - else{ |
|
| 137 | - $this->logger->info('This event no need return back the result, just dispatch it'); |
|
| 138 | - $this->dispatchToListerners($event); |
|
| 139 | - } |
|
| 140 | - } |
|
| 117 | + /** |
|
| 118 | + * Dispatch the event to the registered listeners. |
|
| 119 | + * @param mixed|object $event the event information |
|
| 120 | + * @return void|object if event need return, will return the final EventInfo object. |
|
| 121 | + */ |
|
| 122 | + public function dispatch($event){ |
|
| 123 | + if(! $event || !$event instanceof EventInfo){ |
|
| 124 | + $this->logger->info('The event is not set or is not an instance of "EventInfo" create the default "EventInfo" object to use instead of.'); |
|
| 125 | + $event = new EventInfo((string) $event); |
|
| 126 | + } |
|
| 127 | + $this->logger->debug('Dispatch to the event listener, the event [' .stringfy_vars($event). ']'); |
|
| 128 | + if(isset($event->stop) && $event->stop){ |
|
| 129 | + $this->logger->info('This event need stopped, no need call any listener'); |
|
| 130 | + return; |
|
| 131 | + } |
|
| 132 | + if($event->returnBack){ |
|
| 133 | + $this->logger->info('This event need return back, return the result for future use'); |
|
| 134 | + return $this->dispatchToListerners($event); |
|
| 135 | + } |
|
| 136 | + else{ |
|
| 137 | + $this->logger->info('This event no need return back the result, just dispatch it'); |
|
| 138 | + $this->dispatchToListerners($event); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Dispatch the event to the registered listeners. |
|
| 144 | - * @param object EventInfo $event the event information |
|
| 145 | - * @return void|object if event need return, will return the final EventInfo instance. |
|
| 146 | - */ |
|
| 147 | - private function dispatchToListerners(EventInfo $event){ |
|
| 148 | - $eBackup = $event; |
|
| 149 | - $list = $this->getListeners($event->name); |
|
| 150 | - if(empty($list)){ |
|
| 151 | - $this->logger->info('No event listener is registered for the event [' .$event->name. '] skipping.'); |
|
| 152 | - if($event->returnBack){ |
|
| 153 | - return $event; |
|
| 154 | - } |
|
| 155 | - return; |
|
| 156 | - } |
|
| 157 | - else{ |
|
| 158 | - $this->logger->info('Found the registered event listener for the event [' .$event->name. '] the list are: ' . stringfy_vars($list)); |
|
| 159 | - } |
|
| 160 | - foreach($list as $listener){ |
|
| 161 | - if($eBackup->returnBack){ |
|
| 162 | - $returnedEvent = call_user_func_array($listener, array($event)); |
|
| 163 | - if($returnedEvent instanceof EventInfo){ |
|
| 164 | - $event = $returnedEvent; |
|
| 165 | - } |
|
| 166 | - else{ |
|
| 167 | - show_error('This event [' .$event->name. '] need you return the event object after processing'); |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - else{ |
|
| 171 | - call_user_func_array($listener, array($event)); |
|
| 172 | - } |
|
| 173 | - if($event->stop){ |
|
| 174 | - break; |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - //only test for original event may be during the flow some listeners change this parameter |
|
| 178 | - if($eBackup->returnBack){ |
|
| 179 | - return $event; |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - } |
|
| 142 | + /** |
|
| 143 | + * Dispatch the event to the registered listeners. |
|
| 144 | + * @param object EventInfo $event the event information |
|
| 145 | + * @return void|object if event need return, will return the final EventInfo instance. |
|
| 146 | + */ |
|
| 147 | + private function dispatchToListerners(EventInfo $event){ |
|
| 148 | + $eBackup = $event; |
|
| 149 | + $list = $this->getListeners($event->name); |
|
| 150 | + if(empty($list)){ |
|
| 151 | + $this->logger->info('No event listener is registered for the event [' .$event->name. '] skipping.'); |
|
| 152 | + if($event->returnBack){ |
|
| 153 | + return $event; |
|
| 154 | + } |
|
| 155 | + return; |
|
| 156 | + } |
|
| 157 | + else{ |
|
| 158 | + $this->logger->info('Found the registered event listener for the event [' .$event->name. '] the list are: ' . stringfy_vars($list)); |
|
| 159 | + } |
|
| 160 | + foreach($list as $listener){ |
|
| 161 | + if($eBackup->returnBack){ |
|
| 162 | + $returnedEvent = call_user_func_array($listener, array($event)); |
|
| 163 | + if($returnedEvent instanceof EventInfo){ |
|
| 164 | + $event = $returnedEvent; |
|
| 165 | + } |
|
| 166 | + else{ |
|
| 167 | + show_error('This event [' .$event->name. '] need you return the event object after processing'); |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + else{ |
|
| 171 | + call_user_func_array($listener, array($event)); |
|
| 172 | + } |
|
| 173 | + if($event->stop){ |
|
| 174 | + break; |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + //only test for original event may be during the flow some listeners change this parameter |
|
| 178 | + if($eBackup->returnBack){ |
|
| 179 | + return $event; |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + } |
|
@@ -1,265 +1,265 @@ |
||
| 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 is set |
|
| 131 | - if($this->logger){ |
|
| 132 | - $configLoggersName = get_config('log_logger_name', array()); |
|
| 133 | - if (!empty($configLoggersName)) { |
|
| 134 | - //for best comparaison put all string to lowercase |
|
| 135 | - $configLoggersName = array_map('strtolower', $configLoggersName); |
|
| 136 | - if(! in_array(strtolower($this->logger), $configLoggersName)){ |
|
| 137 | - return; |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - } |
|
| 130 | + //check if config log_logger_name is set |
|
| 131 | + if($this->logger){ |
|
| 132 | + $configLoggersName = get_config('log_logger_name', array()); |
|
| 133 | + if (!empty($configLoggersName)) { |
|
| 134 | + //for best comparaison put all string to lowercase |
|
| 135 | + $configLoggersName = array_map('strtolower', $configLoggersName); |
|
| 136 | + if(! in_array(strtolower($this->logger), $configLoggersName)){ |
|
| 137 | + return; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - //if $level is not an integer |
|
| 143 | - if(! is_numeric($level)){ |
|
| 144 | - $level = self::getLevelValue($level); |
|
| 145 | - } |
|
| 142 | + //if $level is not an integer |
|
| 143 | + if(! is_numeric($level)){ |
|
| 144 | + $level = self::getLevelValue($level); |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - //check if can logging regarding the log level config |
|
| 148 | - $configLevel = self::getLevelValue($configLogLevel); |
|
| 149 | - if($configLevel > $level){ |
|
| 150 | - //can't log |
|
| 151 | - return; |
|
| 152 | - } |
|
| 147 | + //check if can logging regarding the log level config |
|
| 148 | + $configLevel = self::getLevelValue($configLogLevel); |
|
| 149 | + if($configLevel > $level){ |
|
| 150 | + //can't log |
|
| 151 | + return; |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - $logSavePath = get_config('log_save_path'); |
|
| 155 | - if(! $logSavePath){ |
|
| 156 | - $logSavePath = LOGS_PATH; |
|
| 157 | - } |
|
| 154 | + $logSavePath = get_config('log_save_path'); |
|
| 155 | + if(! $logSavePath){ |
|
| 156 | + $logSavePath = LOGS_PATH; |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - if(! is_dir($logSavePath) || !is_writable($logSavePath)){ |
|
| 160 | - //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
| 161 | - show_error('Error : the log dir does not exists or is not writable', $title = 'Log directory error', $logging = false); |
|
| 162 | - } |
|
| 159 | + if(! is_dir($logSavePath) || !is_writable($logSavePath)){ |
|
| 160 | + //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
| 161 | + show_error('Error : the log dir does not exists or is not writable', $title = 'Log directory error', $logging = false); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - $path = $logSavePath . 'logs-' . date('Y-m-d') . '.log'; |
|
| 165 | - if(! file_exists($path)){ |
|
| 166 | - touch($path); |
|
| 167 | - } |
|
| 168 | - //may be at this time helper user_agent not yet included |
|
| 169 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 164 | + $path = $logSavePath . 'logs-' . date('Y-m-d') . '.log'; |
|
| 165 | + if(! file_exists($path)){ |
|
| 166 | + touch($path); |
|
| 167 | + } |
|
| 168 | + //may be at this time helper user_agent not yet included |
|
| 169 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
| 170 | 170 | |
| 171 | - ///////////////////// date ////////////// |
|
| 172 | - $timestampWithMicro = microtime(true); |
|
| 173 | - $microtime = sprintf('%06d', ($timestampWithMicro - floor($timestampWithMicro)) * 1000000); |
|
| 174 | - $dateTime = new DateTime(date('Y-m-d H:i:s.' . $microtime, $timestampWithMicro)); |
|
| 175 | - $logDate = $dateTime->format('Y-m-d H:i:s.u'); |
|
| 176 | - //ip |
|
| 177 | - $ip = get_ip(); |
|
| 178 | - //level name |
|
| 179 | - $levelName = self::getLevelName($level); |
|
| 171 | + ///////////////////// date ////////////// |
|
| 172 | + $timestampWithMicro = microtime(true); |
|
| 173 | + $microtime = sprintf('%06d', ($timestampWithMicro - floor($timestampWithMicro)) * 1000000); |
|
| 174 | + $dateTime = new DateTime(date('Y-m-d H:i:s.' . $microtime, $timestampWithMicro)); |
|
| 175 | + $logDate = $dateTime->format('Y-m-d H:i:s.u'); |
|
| 176 | + //ip |
|
| 177 | + $ip = get_ip(); |
|
| 178 | + //level name |
|
| 179 | + $levelName = self::getLevelName($level); |
|
| 180 | 180 | |
| 181 | - //debug info |
|
| 182 | - $dtrace = debug_backtrace(); |
|
| 183 | - $fileInfo = $dtrace[0]['file'] == __FILE__ ? $dtrace[1] : $dtrace[0]; |
|
| 181 | + //debug info |
|
| 182 | + $dtrace = debug_backtrace(); |
|
| 183 | + $fileInfo = $dtrace[0]['file'] == __FILE__ ? $dtrace[1] : $dtrace[0]; |
|
| 184 | 184 | |
| 185 | - $str = $logDate . ' [' . str_pad($levelName, 7 /*warning len*/) . '] ' . ' [' . str_pad($ip, 15) . '] ' . $this->logger . ' : ' . $message . ' ' . '[' . $fileInfo['file'] . '::' . $fileInfo['line'] . ']' . "\n"; |
|
| 186 | - $fp = fopen($path, 'a+'); |
|
| 187 | - if(is_resource($fp)){ |
|
| 188 | - flock($fp, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 189 | - fwrite($fp, $str); |
|
| 190 | - fclose($fp); |
|
| 191 | - } |
|
| 192 | - } |
|
| 185 | + $str = $logDate . ' [' . str_pad($levelName, 7 /*warning len*/) . '] ' . ' [' . str_pad($ip, 15) . '] ' . $this->logger . ' : ' . $message . ' ' . '[' . $fileInfo['file'] . '::' . $fileInfo['line'] . ']' . "\n"; |
|
| 186 | + $fp = fopen($path, 'a+'); |
|
| 187 | + if(is_resource($fp)){ |
|
| 188 | + flock($fp, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
| 189 | + fwrite($fp, $str); |
|
| 190 | + fclose($fp); |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * Check if the given log level is valid |
|
| 196 | - * |
|
| 197 | - * @param string $level the log level |
|
| 198 | - * |
|
| 199 | - * @return boolean true if the given log level is valid, false if not |
|
| 200 | - */ |
|
| 201 | - private static function isValidConfigLevel($level){ |
|
| 202 | - $level = strtolower($level); |
|
| 203 | - return in_array($level, self::$validConfigLevel); |
|
| 204 | - } |
|
| 194 | + /** |
|
| 195 | + * Check if the given log level is valid |
|
| 196 | + * |
|
| 197 | + * @param string $level the log level |
|
| 198 | + * |
|
| 199 | + * @return boolean true if the given log level is valid, false if not |
|
| 200 | + */ |
|
| 201 | + private static function isValidConfigLevel($level){ |
|
| 202 | + $level = strtolower($level); |
|
| 203 | + return in_array($level, self::$validConfigLevel); |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - /** |
|
| 207 | - * Get the log level number for the given level string |
|
| 208 | - * @param string $level the log level in string format |
|
| 209 | - * @return int the log level in integer format using the predefinied constants |
|
| 210 | - */ |
|
| 211 | - private static function getLevelValue($level){ |
|
| 212 | - $level = strtolower($level); |
|
| 213 | - $value = self::NONE; |
|
| 206 | + /** |
|
| 207 | + * Get the log level number for the given level string |
|
| 208 | + * @param string $level the log level in string format |
|
| 209 | + * @return int the log level in integer format using the predefinied constants |
|
| 210 | + */ |
|
| 211 | + private static function getLevelValue($level){ |
|
| 212 | + $level = strtolower($level); |
|
| 213 | + $value = self::NONE; |
|
| 214 | 214 | |
| 215 | - //the default value is NONE, so means no need test for NONE |
|
| 216 | - if($level == 'fatal'){ |
|
| 217 | - $value = self::FATAL; |
|
| 218 | - } |
|
| 219 | - else if($level == 'error'){ |
|
| 220 | - $value = self::ERROR; |
|
| 221 | - } |
|
| 222 | - else if($level == 'warning' || $level == 'warn'){ |
|
| 223 | - $value = self::WARNING; |
|
| 224 | - } |
|
| 225 | - else if($level == 'info'){ |
|
| 226 | - $value = self::INFO; |
|
| 227 | - } |
|
| 228 | - else if($level == 'debug'){ |
|
| 229 | - $value = self::DEBUG; |
|
| 230 | - } |
|
| 231 | - else if($level == 'all'){ |
|
| 232 | - $value = self::ALL; |
|
| 233 | - } |
|
| 234 | - return $value; |
|
| 235 | - } |
|
| 215 | + //the default value is NONE, so means no need test for NONE |
|
| 216 | + if($level == 'fatal'){ |
|
| 217 | + $value = self::FATAL; |
|
| 218 | + } |
|
| 219 | + else if($level == 'error'){ |
|
| 220 | + $value = self::ERROR; |
|
| 221 | + } |
|
| 222 | + else if($level == 'warning' || $level == 'warn'){ |
|
| 223 | + $value = self::WARNING; |
|
| 224 | + } |
|
| 225 | + else if($level == 'info'){ |
|
| 226 | + $value = self::INFO; |
|
| 227 | + } |
|
| 228 | + else if($level == 'debug'){ |
|
| 229 | + $value = self::DEBUG; |
|
| 230 | + } |
|
| 231 | + else if($level == 'all'){ |
|
| 232 | + $value = self::ALL; |
|
| 233 | + } |
|
| 234 | + return $value; |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | - /** |
|
| 238 | - * Get the log level string for the given log level integer |
|
| 239 | - * @param integer $level the log level in integer format |
|
| 240 | - * @return string the log level in string format |
|
| 241 | - */ |
|
| 242 | - private static function getLevelName($level){ |
|
| 243 | - $value = ''; |
|
| 237 | + /** |
|
| 238 | + * Get the log level string for the given log level integer |
|
| 239 | + * @param integer $level the log level in integer format |
|
| 240 | + * @return string the log level in string format |
|
| 241 | + */ |
|
| 242 | + private static function getLevelName($level){ |
|
| 243 | + $value = ''; |
|
| 244 | 244 | |
| 245 | - //the default value is NONE, so means no need test for NONE |
|
| 246 | - if($level == self::FATAL){ |
|
| 247 | - $value = 'FATAL'; |
|
| 248 | - } |
|
| 249 | - else if($level == self::ERROR){ |
|
| 250 | - $value = 'ERROR'; |
|
| 251 | - } |
|
| 252 | - else if($level == self::WARNING){ |
|
| 253 | - $value = 'WARNING'; |
|
| 254 | - } |
|
| 255 | - else if($level == self::INFO){ |
|
| 256 | - $value = 'INFO'; |
|
| 257 | - } |
|
| 258 | - else if($level == self::DEBUG){ |
|
| 259 | - $value = 'DEBUG'; |
|
| 260 | - } |
|
| 261 | - //no need for ALL |
|
| 262 | - return $value; |
|
| 263 | - } |
|
| 245 | + //the default value is NONE, so means no need test for NONE |
|
| 246 | + if($level == self::FATAL){ |
|
| 247 | + $value = 'FATAL'; |
|
| 248 | + } |
|
| 249 | + else if($level == self::ERROR){ |
|
| 250 | + $value = 'ERROR'; |
|
| 251 | + } |
|
| 252 | + else if($level == self::WARNING){ |
|
| 253 | + $value = 'WARNING'; |
|
| 254 | + } |
|
| 255 | + else if($level == self::INFO){ |
|
| 256 | + $value = 'INFO'; |
|
| 257 | + } |
|
| 258 | + else if($level == self::DEBUG){ |
|
| 259 | + $value = 'DEBUG'; |
|
| 260 | + } |
|
| 261 | + //no need for ALL |
|
| 262 | + return $value; |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - } |
|
| 265 | + } |
|
@@ -1,521 +1,521 @@ |
||
| 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{ |
|
| 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{ |
|
| 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 | - /** |
|
| 35 | - * The logger instance |
|
| 36 | - * @var Log |
|
| 37 | - */ |
|
| 38 | - private static $logger; |
|
| 34 | + /** |
|
| 35 | + * The logger instance |
|
| 36 | + * @var Log |
|
| 37 | + */ |
|
| 38 | + private static $logger; |
|
| 39 | 39 | |
| 40 | 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(); |
|
| 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 | 45 | |
| 46 | - //Load resources from autoload configuration |
|
| 47 | - $this->loadResourcesFromAutoloadConfig(); |
|
| 48 | - } |
|
| 46 | + //Load resources from autoload configuration |
|
| 47 | + $this->loadResourcesFromAutoloadConfig(); |
|
| 48 | + } |
|
| 49 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 138 | |
| 139 | 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()){ |
|
| 150 | - $logger = static::getLogger(); |
|
| 151 | - $class = str_ireplace('.php', '', $class); |
|
| 152 | - $class = trim($class, '/\\'); |
|
| 153 | - $file = ucfirst($class) .'.php'; |
|
| 154 | - $logger->debug('Loading library [' . $class . '] ...'); |
|
| 155 | - if(! $instance){ |
|
| 156 | - //for module |
|
| 157 | - if(strpos($class, '/') !== false){ |
|
| 158 | - $path = explode('/', $class); |
|
| 159 | - if(isset($path[1])){ |
|
| 160 | - $instance = strtolower($path[1]); |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - else{ |
|
| 164 | - $instance = strtolower($class); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - if(isset(static::$loaded[$instance])){ |
|
| 168 | - $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 169 | - return; |
|
| 170 | - } |
|
| 171 | - $obj = & get_instance(); |
|
| 172 | - //TODO for Database library |
|
| 173 | - if(strtolower($class) == 'database'){ |
|
| 174 | - $logger->info('This is the Database library ...'); |
|
| 175 | - $dbInstance = & class_loader('Database', 'classes/database', $params); |
|
| 176 | - $obj->{$instance} = $dbInstance; |
|
| 177 | - static::$loaded[$instance] = $class; |
|
| 178 | - $logger->info('Library Database loaded successfully.'); |
|
| 179 | - return; |
|
| 180 | - } |
|
| 181 | - $libraryFilePath = null; |
|
| 182 | - $logger->debug('Check if this is a system library ...'); |
|
| 183 | - if(file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 184 | - $libraryFilePath = CORE_LIBRARY_PATH . $file; |
|
| 185 | - $class = ucfirst($class); |
|
| 186 | - $logger->info('This library is a system library'); |
|
| 187 | - } |
|
| 188 | - else{ |
|
| 189 | - $logger->info('This library is not a system library'); |
|
| 190 | - //first check if this library is in the module |
|
| 191 | - $logger->debug('Checking library [' . $class . '] from module list ...'); |
|
| 192 | - $searchModuleName = null; |
|
| 193 | - //check if the request class contains module name |
|
| 194 | - if(strpos($class, '/') !== false){ |
|
| 195 | - $path = explode('/', $class); |
|
| 196 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 197 | - $searchModuleName = $path[0]; |
|
| 198 | - $class = ucfirst($path[1]); |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - else{ |
|
| 202 | - $class = ucfirst($class); |
|
| 203 | - } |
|
| 204 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 205 | - $searchModuleName = $obj->moduleName; |
|
| 206 | - } |
|
| 207 | - $moduleLibraryPath = Module::findLibraryFullPath($class, $searchModuleName); |
|
| 208 | - if($moduleLibraryPath){ |
|
| 209 | - $logger->info('Found library [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 210 | - $libraryFilePath = $moduleLibraryPath; |
|
| 211 | - } |
|
| 212 | - else{ |
|
| 213 | - $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - if(! $libraryFilePath){ |
|
| 217 | - $searchDir = array(LIBRARY_PATH); |
|
| 218 | - foreach($searchDir as $dir){ |
|
| 219 | - $filePath = $dir . $file; |
|
| 220 | - if(file_exists($filePath)){ |
|
| 221 | - $libraryFilePath = $filePath; |
|
| 222 | - //is already found not to continue |
|
| 223 | - break; |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
|
| 228 | - if($libraryFilePath){ |
|
| 229 | - require_once $libraryFilePath; |
|
| 230 | - if(class_exists($class)){ |
|
| 231 | - $c = $params ? new $class($params) : new $class(); |
|
| 232 | - $obj = & get_instance(); |
|
| 233 | - $obj->{$instance} = $c; |
|
| 234 | - static::$loaded[$instance] = $class; |
|
| 235 | - $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
|
| 236 | - } |
|
| 237 | - else{ |
|
| 238 | - show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 239 | - } |
|
| 240 | - } |
|
| 241 | - else{ |
|
| 242 | - show_error('Unable to find library class [' . $class . ']'); |
|
| 243 | - } |
|
| 244 | - } |
|
| 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()){ |
|
| 150 | + $logger = static::getLogger(); |
|
| 151 | + $class = str_ireplace('.php', '', $class); |
|
| 152 | + $class = trim($class, '/\\'); |
|
| 153 | + $file = ucfirst($class) .'.php'; |
|
| 154 | + $logger->debug('Loading library [' . $class . '] ...'); |
|
| 155 | + if(! $instance){ |
|
| 156 | + //for module |
|
| 157 | + if(strpos($class, '/') !== false){ |
|
| 158 | + $path = explode('/', $class); |
|
| 159 | + if(isset($path[1])){ |
|
| 160 | + $instance = strtolower($path[1]); |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + else{ |
|
| 164 | + $instance = strtolower($class); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + if(isset(static::$loaded[$instance])){ |
|
| 168 | + $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
|
| 169 | + return; |
|
| 170 | + } |
|
| 171 | + $obj = & get_instance(); |
|
| 172 | + //TODO for Database library |
|
| 173 | + if(strtolower($class) == 'database'){ |
|
| 174 | + $logger->info('This is the Database library ...'); |
|
| 175 | + $dbInstance = & class_loader('Database', 'classes/database', $params); |
|
| 176 | + $obj->{$instance} = $dbInstance; |
|
| 177 | + static::$loaded[$instance] = $class; |
|
| 178 | + $logger->info('Library Database loaded successfully.'); |
|
| 179 | + return; |
|
| 180 | + } |
|
| 181 | + $libraryFilePath = null; |
|
| 182 | + $logger->debug('Check if this is a system library ...'); |
|
| 183 | + if(file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 184 | + $libraryFilePath = CORE_LIBRARY_PATH . $file; |
|
| 185 | + $class = ucfirst($class); |
|
| 186 | + $logger->info('This library is a system library'); |
|
| 187 | + } |
|
| 188 | + else{ |
|
| 189 | + $logger->info('This library is not a system library'); |
|
| 190 | + //first check if this library is in the module |
|
| 191 | + $logger->debug('Checking library [' . $class . '] from module list ...'); |
|
| 192 | + $searchModuleName = null; |
|
| 193 | + //check if the request class contains module name |
|
| 194 | + if(strpos($class, '/') !== false){ |
|
| 195 | + $path = explode('/', $class); |
|
| 196 | + if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 197 | + $searchModuleName = $path[0]; |
|
| 198 | + $class = ucfirst($path[1]); |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + else{ |
|
| 202 | + $class = ucfirst($class); |
|
| 203 | + } |
|
| 204 | + if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 205 | + $searchModuleName = $obj->moduleName; |
|
| 206 | + } |
|
| 207 | + $moduleLibraryPath = Module::findLibraryFullPath($class, $searchModuleName); |
|
| 208 | + if($moduleLibraryPath){ |
|
| 209 | + $logger->info('Found library [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 210 | + $libraryFilePath = $moduleLibraryPath; |
|
| 211 | + } |
|
| 212 | + else{ |
|
| 213 | + $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + if(! $libraryFilePath){ |
|
| 217 | + $searchDir = array(LIBRARY_PATH); |
|
| 218 | + foreach($searchDir as $dir){ |
|
| 219 | + $filePath = $dir . $file; |
|
| 220 | + if(file_exists($filePath)){ |
|
| 221 | + $libraryFilePath = $filePath; |
|
| 222 | + //is already found not to continue |
|
| 223 | + break; |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
|
| 228 | + if($libraryFilePath){ |
|
| 229 | + require_once $libraryFilePath; |
|
| 230 | + if(class_exists($class)){ |
|
| 231 | + $c = $params ? new $class($params) : new $class(); |
|
| 232 | + $obj = & get_instance(); |
|
| 233 | + $obj->{$instance} = $c; |
|
| 234 | + static::$loaded[$instance] = $class; |
|
| 235 | + $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
|
| 236 | + } |
|
| 237 | + else{ |
|
| 238 | + show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + else{ |
|
| 242 | + show_error('Unable to find library class [' . $class . ']'); |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | 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 | - } |
|
| 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 | 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 | - $config = array(); |
|
| 355 | - if(file_exists($configFilePath)){ |
|
| 356 | - require_once $configFilePath; |
|
| 357 | - if(! empty($config) && is_array($config)){ |
|
| 358 | - Config::setAll($config); |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - else{ |
|
| 362 | - show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 363 | - } |
|
| 364 | - static::$loaded['config_' . $filename] = $configFilePath; |
|
| 365 | - $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
|
| 366 | - $logger->info('The custom application configuration loaded are listed below: ' . stringfy_vars($config)); |
|
| 367 | - unset($config); |
|
| 368 | - } |
|
| 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 | + $config = array(); |
|
| 355 | + if(file_exists($configFilePath)){ |
|
| 356 | + require_once $configFilePath; |
|
| 357 | + if(! empty($config) && is_array($config)){ |
|
| 358 | + Config::setAll($config); |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + else{ |
|
| 362 | + show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 363 | + } |
|
| 364 | + static::$loaded['config_' . $filename] = $configFilePath; |
|
| 365 | + $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
|
| 366 | + $logger->info('The custom application configuration loaded are listed below: ' . stringfy_vars($config)); |
|
| 367 | + unset($config); |
|
| 368 | + } |
|
| 369 | 369 | |
| 370 | 370 | |
| 371 | - /** |
|
| 372 | - * Load the language |
|
| 373 | - * |
|
| 374 | - * @param string $language the language name to be loaded |
|
| 375 | - * |
|
| 376 | - * @return void |
|
| 377 | - */ |
|
| 378 | - public static function lang($language){ |
|
| 379 | - $logger = static::getLogger(); |
|
| 380 | - $language = str_ireplace('.php', '', $language); |
|
| 381 | - $language = trim($language, '/\\'); |
|
| 382 | - $language = str_ireplace('lang_', '', $language); |
|
| 383 | - $file = 'lang_'.$language.'.php'; |
|
| 384 | - $logger->debug('Loading language [' . $language . '] ...'); |
|
| 385 | - if(isset(static::$loaded['lang_' . $language])){ |
|
| 386 | - $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
|
| 387 | - return; |
|
| 388 | - } |
|
| 389 | - //determine the current language |
|
| 390 | - $appLang = get_config('default_language'); |
|
| 391 | - //if the language exists in the cookie use it |
|
| 392 | - $cfgKey = get_config('language_cookie_name'); |
|
| 393 | - $objCookie = & class_loader('Cookie'); |
|
| 394 | - $cookieLang = $objCookie->get($cfgKey); |
|
| 395 | - if($cookieLang){ |
|
| 396 | - $appLang = $cookieLang; |
|
| 397 | - } |
|
| 398 | - $languageFilePath = null; |
|
| 399 | - //first check if this language is in the module |
|
| 400 | - $logger->debug('Checking language [' . $language . '] from module list ...'); |
|
| 401 | - $searchModuleName = null; |
|
| 402 | - $obj = & get_instance(); |
|
| 403 | - //check if the request class contains module name |
|
| 404 | - if(strpos($language, '/') !== false){ |
|
| 405 | - $path = explode('/', $language); |
|
| 406 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 407 | - $searchModuleName = $path[0]; |
|
| 408 | - $language = 'lang_' . $path[1] . '.php'; |
|
| 409 | - $file = $path[0] . DS .$language; |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 413 | - $searchModuleName = $obj->moduleName; |
|
| 414 | - } |
|
| 415 | - $moduleLanguagePath = Module::findLanguageFullPath($language, $searchModuleName, $appLang); |
|
| 416 | - if($moduleLanguagePath){ |
|
| 417 | - $logger->info('Found language [' . $language . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 418 | - $languageFilePath = $moduleLanguagePath; |
|
| 419 | - } |
|
| 420 | - else{ |
|
| 421 | - $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
|
| 422 | - } |
|
| 423 | - if(! $languageFilePath){ |
|
| 424 | - $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
|
| 425 | - foreach($searchDir as $dir){ |
|
| 426 | - $filePath = $dir . $appLang . DS . $file; |
|
| 427 | - if(file_exists($filePath)){ |
|
| 428 | - $languageFilePath = $filePath; |
|
| 429 | - //is already found not to continue |
|
| 430 | - break; |
|
| 431 | - } |
|
| 432 | - } |
|
| 433 | - } |
|
| 434 | - $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
|
| 435 | - if($languageFilePath){ |
|
| 436 | - $lang = array(); |
|
| 437 | - require_once $languageFilePath; |
|
| 438 | - if(! empty($lang) && is_array($lang)){ |
|
| 439 | - $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
|
| 440 | - //Note: may be here the class 'Lang' not yet loaded |
|
| 441 | - $langObj =& class_loader('Lang', 'classes'); |
|
| 442 | - $langObj->addLangMessages($lang); |
|
| 443 | - //free the memory |
|
| 444 | - unset($lang); |
|
| 445 | - } |
|
| 446 | - static::$loaded['lang_' . $language] = $languageFilePath; |
|
| 447 | - $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
|
| 448 | - } |
|
| 449 | - else{ |
|
| 450 | - show_error('Unable to find language file [' . $file . ']'); |
|
| 451 | - } |
|
| 452 | - } |
|
| 371 | + /** |
|
| 372 | + * Load the language |
|
| 373 | + * |
|
| 374 | + * @param string $language the language name to be loaded |
|
| 375 | + * |
|
| 376 | + * @return void |
|
| 377 | + */ |
|
| 378 | + public static function lang($language){ |
|
| 379 | + $logger = static::getLogger(); |
|
| 380 | + $language = str_ireplace('.php', '', $language); |
|
| 381 | + $language = trim($language, '/\\'); |
|
| 382 | + $language = str_ireplace('lang_', '', $language); |
|
| 383 | + $file = 'lang_'.$language.'.php'; |
|
| 384 | + $logger->debug('Loading language [' . $language . '] ...'); |
|
| 385 | + if(isset(static::$loaded['lang_' . $language])){ |
|
| 386 | + $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
|
| 387 | + return; |
|
| 388 | + } |
|
| 389 | + //determine the current language |
|
| 390 | + $appLang = get_config('default_language'); |
|
| 391 | + //if the language exists in the cookie use it |
|
| 392 | + $cfgKey = get_config('language_cookie_name'); |
|
| 393 | + $objCookie = & class_loader('Cookie'); |
|
| 394 | + $cookieLang = $objCookie->get($cfgKey); |
|
| 395 | + if($cookieLang){ |
|
| 396 | + $appLang = $cookieLang; |
|
| 397 | + } |
|
| 398 | + $languageFilePath = null; |
|
| 399 | + //first check if this language is in the module |
|
| 400 | + $logger->debug('Checking language [' . $language . '] from module list ...'); |
|
| 401 | + $searchModuleName = null; |
|
| 402 | + $obj = & get_instance(); |
|
| 403 | + //check if the request class contains module name |
|
| 404 | + if(strpos($language, '/') !== false){ |
|
| 405 | + $path = explode('/', $language); |
|
| 406 | + if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 407 | + $searchModuleName = $path[0]; |
|
| 408 | + $language = 'lang_' . $path[1] . '.php'; |
|
| 409 | + $file = $path[0] . DS .$language; |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 413 | + $searchModuleName = $obj->moduleName; |
|
| 414 | + } |
|
| 415 | + $moduleLanguagePath = Module::findLanguageFullPath($language, $searchModuleName, $appLang); |
|
| 416 | + if($moduleLanguagePath){ |
|
| 417 | + $logger->info('Found language [' . $language . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 418 | + $languageFilePath = $moduleLanguagePath; |
|
| 419 | + } |
|
| 420 | + else{ |
|
| 421 | + $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
|
| 422 | + } |
|
| 423 | + if(! $languageFilePath){ |
|
| 424 | + $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
|
| 425 | + foreach($searchDir as $dir){ |
|
| 426 | + $filePath = $dir . $appLang . DS . $file; |
|
| 427 | + if(file_exists($filePath)){ |
|
| 428 | + $languageFilePath = $filePath; |
|
| 429 | + //is already found not to continue |
|
| 430 | + break; |
|
| 431 | + } |
|
| 432 | + } |
|
| 433 | + } |
|
| 434 | + $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
|
| 435 | + if($languageFilePath){ |
|
| 436 | + $lang = array(); |
|
| 437 | + require_once $languageFilePath; |
|
| 438 | + if(! empty($lang) && is_array($lang)){ |
|
| 439 | + $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
|
| 440 | + //Note: may be here the class 'Lang' not yet loaded |
|
| 441 | + $langObj =& class_loader('Lang', 'classes'); |
|
| 442 | + $langObj->addLangMessages($lang); |
|
| 443 | + //free the memory |
|
| 444 | + unset($lang); |
|
| 445 | + } |
|
| 446 | + static::$loaded['lang_' . $language] = $languageFilePath; |
|
| 447 | + $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
|
| 448 | + } |
|
| 449 | + else{ |
|
| 450 | + show_error('Unable to find language file [' . $file . ']'); |
|
| 451 | + } |
|
| 452 | + } |
|
| 453 | 453 | |
| 454 | 454 | |
| 455 | - private function getResourcesFromAutoloadConfig(){ |
|
| 456 | - $autoloads = array(); |
|
| 457 | - $autoloads['config'] = array(); |
|
| 458 | - $autoloads['languages'] = array(); |
|
| 459 | - $autoloads['libraries'] = array(); |
|
| 460 | - $autoloads['models'] = array(); |
|
| 461 | - $autoloads['functions'] = array(); |
|
| 462 | - //loading of the resources in autoload.php configuration file |
|
| 463 | - if(file_exists(CONFIG_PATH . 'autoload.php')){ |
|
| 464 | - $autoload = array(); |
|
| 465 | - require_once CONFIG_PATH . 'autoload.php'; |
|
| 466 | - if(! empty($autoload) && is_array($autoload)){ |
|
| 467 | - $autoloads = array_merge($autoloads, $autoload); |
|
| 468 | - unset($autoload); |
|
| 469 | - } |
|
| 470 | - } |
|
| 471 | - //loading autoload configuration for modules |
|
| 472 | - $modulesAutoloads = Module::getModulesAutoloadConfig(); |
|
| 473 | - if(! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
|
| 474 | - $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
|
| 475 | - } |
|
| 476 | - return $autoloads; |
|
| 477 | - } |
|
| 455 | + private function getResourcesFromAutoloadConfig(){ |
|
| 456 | + $autoloads = array(); |
|
| 457 | + $autoloads['config'] = array(); |
|
| 458 | + $autoloads['languages'] = array(); |
|
| 459 | + $autoloads['libraries'] = array(); |
|
| 460 | + $autoloads['models'] = array(); |
|
| 461 | + $autoloads['functions'] = array(); |
|
| 462 | + //loading of the resources in autoload.php configuration file |
|
| 463 | + if(file_exists(CONFIG_PATH . 'autoload.php')){ |
|
| 464 | + $autoload = array(); |
|
| 465 | + require_once CONFIG_PATH . 'autoload.php'; |
|
| 466 | + if(! empty($autoload) && is_array($autoload)){ |
|
| 467 | + $autoloads = array_merge($autoloads, $autoload); |
|
| 468 | + unset($autoload); |
|
| 469 | + } |
|
| 470 | + } |
|
| 471 | + //loading autoload configuration for modules |
|
| 472 | + $modulesAutoloads = Module::getModulesAutoloadConfig(); |
|
| 473 | + if(! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
|
| 474 | + $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
|
| 475 | + } |
|
| 476 | + return $autoloads; |
|
| 477 | + } |
|
| 478 | 478 | |
| 479 | - /** |
|
| 480 | - * Load the autoload configuration |
|
| 481 | - * @return void |
|
| 482 | - */ |
|
| 483 | - private function loadResourcesFromAutoloadConfig(){ |
|
| 484 | - $autoloads = array(); |
|
| 485 | - $autoloads['config'] = array(); |
|
| 486 | - $autoloads['languages'] = array(); |
|
| 487 | - $autoloads['libraries'] = array(); |
|
| 488 | - $autoloads['models'] = array(); |
|
| 489 | - $autoloads['functions'] = array(); |
|
| 479 | + /** |
|
| 480 | + * Load the autoload configuration |
|
| 481 | + * @return void |
|
| 482 | + */ |
|
| 483 | + private function loadResourcesFromAutoloadConfig(){ |
|
| 484 | + $autoloads = array(); |
|
| 485 | + $autoloads['config'] = array(); |
|
| 486 | + $autoloads['languages'] = array(); |
|
| 487 | + $autoloads['libraries'] = array(); |
|
| 488 | + $autoloads['models'] = array(); |
|
| 489 | + $autoloads['functions'] = array(); |
|
| 490 | 490 | |
| 491 | - $list = $this->getResourcesFromAutoloadConfig(); |
|
| 492 | - $autoloads = array_merge($autoloads, $list); |
|
| 491 | + $list = $this->getResourcesFromAutoloadConfig(); |
|
| 492 | + $autoloads = array_merge($autoloads, $list); |
|
| 493 | 493 | |
| 494 | - //config autoload |
|
| 495 | - $this->loadAutoloadResourcesArray('config', $autoloads['config']); |
|
| 494 | + //config autoload |
|
| 495 | + $this->loadAutoloadResourcesArray('config', $autoloads['config']); |
|
| 496 | 496 | |
| 497 | - //languages autoload |
|
| 498 | - $this->loadAutoloadResourcesArray('lang', $autoloads['languages']); |
|
| 497 | + //languages autoload |
|
| 498 | + $this->loadAutoloadResourcesArray('lang', $autoloads['languages']); |
|
| 499 | 499 | |
| 500 | - //libraries autoload |
|
| 501 | - $this->loadAutoloadResourcesArray('library', $autoloads['libraries']); |
|
| 500 | + //libraries autoload |
|
| 501 | + $this->loadAutoloadResourcesArray('library', $autoloads['libraries']); |
|
| 502 | 502 | |
| 503 | - //models autoload |
|
| 504 | - $this->loadAutoloadResourcesArray('model', $autoloads['models']); |
|
| 503 | + //models autoload |
|
| 504 | + $this->loadAutoloadResourcesArray('model', $autoloads['models']); |
|
| 505 | 505 | |
| 506 | - //functions autoload |
|
| 507 | - $this->loadAutoloadResourcesArray('functions', $autoloads['functions']); |
|
| 508 | - } |
|
| 506 | + //functions autoload |
|
| 507 | + $this->loadAutoloadResourcesArray('functions', $autoloads['functions']); |
|
| 508 | + } |
|
| 509 | 509 | |
| 510 | - /** |
|
| 511 | - * Load the resources autoload array |
|
| 512 | - * @param string $method this object method name to call |
|
| 513 | - * @param array $resources the resource to load |
|
| 514 | - * @return void |
|
| 515 | - */ |
|
| 516 | - private function loadAutoloadResourcesArray($method, array $resources){ |
|
| 517 | - foreach ($resources as $name) { |
|
| 518 | - $this->{$method}($name); |
|
| 519 | - } |
|
| 520 | - } |
|
| 521 | - } |
|
| 510 | + /** |
|
| 511 | + * Load the resources autoload array |
|
| 512 | + * @param string $method this object method name to call |
|
| 513 | + * @param array $resources the resource to load |
|
| 514 | + * @return void |
|
| 515 | + */ |
|
| 516 | + private function loadAutoloadResourcesArray($method, array $resources){ |
|
| 517 | + foreach ($resources as $name) { |
|
| 518 | + $this->{$method}($name); |
|
| 519 | + } |
|
| 520 | + } |
|
| 521 | + } |
|
@@ -1,145 +1,145 @@ |
||
| 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 Controller{ |
|
| 27 | + class Controller{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The name of the module if this controller belong to an module |
|
| 31 | - * @var string |
|
| 32 | - */ |
|
| 33 | - public $moduleName = null; |
|
| 29 | + /** |
|
| 30 | + * The name of the module if this controller belong to an module |
|
| 31 | + * @var string |
|
| 32 | + */ |
|
| 33 | + public $moduleName = null; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The singleton of the super object |
|
| 37 | - * @var Controller |
|
| 38 | - */ |
|
| 39 | - private static $instance; |
|
| 35 | + /** |
|
| 36 | + * The singleton of the super object |
|
| 37 | + * @var Controller |
|
| 38 | + */ |
|
| 39 | + private static $instance; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The logger instance |
|
| 43 | - * @var Log |
|
| 44 | - */ |
|
| 45 | - protected $logger; |
|
| 41 | + /** |
|
| 42 | + * The logger instance |
|
| 43 | + * @var Log |
|
| 44 | + */ |
|
| 45 | + protected $logger; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Class constructor |
|
| 49 | - * @param object $logger the Log instance to use if is null will create one |
|
| 50 | - */ |
|
| 51 | - public function __construct(Log $logger = null){ |
|
| 52 | - //setting the Log instance |
|
| 53 | - $this->setLoggerFromParamOrCreateNewInstance($logger); |
|
| 47 | + /** |
|
| 48 | + * Class constructor |
|
| 49 | + * @param object $logger the Log instance to use if is null will create one |
|
| 50 | + */ |
|
| 51 | + public function __construct(Log $logger = null){ |
|
| 52 | + //setting the Log instance |
|
| 53 | + $this->setLoggerFromParamOrCreateNewInstance($logger); |
|
| 54 | 54 | |
| 55 | - //instance of the super object |
|
| 56 | - self::$instance = & $this; |
|
| 55 | + //instance of the super object |
|
| 56 | + self::$instance = & $this; |
|
| 57 | 57 | |
| 58 | - //load the required resources |
|
| 59 | - $this->loadRequiredResources(); |
|
| 58 | + //load the required resources |
|
| 59 | + $this->loadRequiredResources(); |
|
| 60 | 60 | |
| 61 | - //set the cache using the configuration |
|
| 62 | - $this->setCacheFromParamOrConfig(null); |
|
| 61 | + //set the cache using the configuration |
|
| 62 | + $this->setCacheFromParamOrConfig(null); |
|
| 63 | 63 | |
| 64 | - //set application session configuration |
|
| 65 | - $this->logger->debug('Setting PHP application session handler'); |
|
| 66 | - set_session_config(); |
|
| 64 | + //set application session configuration |
|
| 65 | + $this->logger->debug('Setting PHP application session handler'); |
|
| 66 | + set_session_config(); |
|
| 67 | 67 | |
| 68 | - //set module using the router |
|
| 69 | - $this->setModuleNameFromRouter(); |
|
| 68 | + //set module using the router |
|
| 69 | + $this->setModuleNameFromRouter(); |
|
| 70 | 70 | |
| 71 | - //dispatch the loaded instance of super controller event |
|
| 72 | - $this->eventdispatcher->dispatch('SUPER_CONTROLLER_CREATED'); |
|
| 73 | - } |
|
| 71 | + //dispatch the loaded instance of super controller event |
|
| 72 | + $this->eventdispatcher->dispatch('SUPER_CONTROLLER_CREATED'); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * This is a very useful method it's used to get the super object instance |
|
| 78 | - * @return Controller the super object instance |
|
| 79 | - */ |
|
| 80 | - public static function &get_instance(){ |
|
| 81 | - return self::$instance; |
|
| 82 | - } |
|
| 76 | + /** |
|
| 77 | + * This is a very useful method it's used to get the super object instance |
|
| 78 | + * @return Controller the super object instance |
|
| 79 | + */ |
|
| 80 | + public static function &get_instance(){ |
|
| 81 | + return self::$instance; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * This method is used to set the module name |
|
| 86 | - */ |
|
| 87 | - protected function setModuleNameFromRouter(){ |
|
| 88 | - //determine the current module |
|
| 89 | - if(isset($this->router) && $this->router->getModule()){ |
|
| 90 | - $this->moduleName = $this->router->getModule(); |
|
| 91 | - } |
|
| 92 | - } |
|
| 84 | + /** |
|
| 85 | + * This method is used to set the module name |
|
| 86 | + */ |
|
| 87 | + protected function setModuleNameFromRouter(){ |
|
| 88 | + //determine the current module |
|
| 89 | + if(isset($this->router) && $this->router->getModule()){ |
|
| 90 | + $this->moduleName = $this->router->getModule(); |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Set the cache using the argument otherwise will use the configuration |
|
| 96 | - * @param CacheInterface $cache the implementation of CacheInterface if null will use the configured |
|
| 97 | - */ |
|
| 98 | - protected function setCacheFromParamOrConfig(CacheInterface $cache = null){ |
|
| 99 | - $this->logger->debug('Setting the cache handler instance'); |
|
| 100 | - //set cache handler instance |
|
| 101 | - if(get_config('cache_enable', false)){ |
|
| 102 | - if ($cache !== null){ |
|
| 103 | - $this->cache = $cache; |
|
| 104 | - } else if (isset($this->{strtolower(get_config('cache_handler'))})){ |
|
| 105 | - $this->cache = $this->{strtolower(get_config('cache_handler'))}; |
|
| 106 | - unset($this->{strtolower(get_config('cache_handler'))}); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - } |
|
| 94 | + /** |
|
| 95 | + * Set the cache using the argument otherwise will use the configuration |
|
| 96 | + * @param CacheInterface $cache the implementation of CacheInterface if null will use the configured |
|
| 97 | + */ |
|
| 98 | + protected function setCacheFromParamOrConfig(CacheInterface $cache = null){ |
|
| 99 | + $this->logger->debug('Setting the cache handler instance'); |
|
| 100 | + //set cache handler instance |
|
| 101 | + if(get_config('cache_enable', false)){ |
|
| 102 | + if ($cache !== null){ |
|
| 103 | + $this->cache = $cache; |
|
| 104 | + } else if (isset($this->{strtolower(get_config('cache_handler'))})){ |
|
| 105 | + $this->cache = $this->{strtolower(get_config('cache_handler'))}; |
|
| 106 | + unset($this->{strtolower(get_config('cache_handler'))}); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Set the Log instance using argument or create new instance |
|
| 113 | - * @param object $logger the Log instance if not null |
|
| 114 | - */ |
|
| 115 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 116 | - if($logger !== null){ |
|
| 117 | - $this->logger = $logger; |
|
| 118 | - } |
|
| 119 | - else{ |
|
| 120 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 121 | - $this->logger->setLogger('MainController'); |
|
| 122 | - } |
|
| 123 | - } |
|
| 111 | + /** |
|
| 112 | + * Set the Log instance using argument or create new instance |
|
| 113 | + * @param object $logger the Log instance if not null |
|
| 114 | + */ |
|
| 115 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
| 116 | + if($logger !== null){ |
|
| 117 | + $this->logger = $logger; |
|
| 118 | + } |
|
| 119 | + else{ |
|
| 120 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 121 | + $this->logger->setLogger('MainController'); |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * This method is used to load the required resources for framework to work |
|
| 127 | - * @return void |
|
| 128 | - */ |
|
| 129 | - private function loadRequiredResources(){ |
|
| 130 | - $this->logger->debug('Adding the loaded classes to the super instance'); |
|
| 131 | - foreach (class_loaded() as $var => $class){ |
|
| 132 | - $this->$var =& class_loader($class); |
|
| 133 | - } |
|
| 125 | + /** |
|
| 126 | + * This method is used to load the required resources for framework to work |
|
| 127 | + * @return void |
|
| 128 | + */ |
|
| 129 | + private function loadRequiredResources(){ |
|
| 130 | + $this->logger->debug('Adding the loaded classes to the super instance'); |
|
| 131 | + foreach (class_loaded() as $var => $class){ |
|
| 132 | + $this->$var =& class_loader($class); |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - $this->logger->debug('Loading the required classes into super instance'); |
|
| 136 | - $this->eventdispatcher =& class_loader('EventDispatcher', 'classes'); |
|
| 137 | - $this->loader =& class_loader('Loader', 'classes'); |
|
| 138 | - $this->lang =& class_loader('Lang', 'classes'); |
|
| 139 | - $this->request =& class_loader('Request', 'classes'); |
|
| 140 | - //dispatch the request instance created event |
|
| 141 | - $this->eventdispatcher->dispatch('REQUEST_CREATED'); |
|
| 142 | - $this->response =& class_loader('Response', 'classes', 'classes'); |
|
| 143 | - } |
|
| 135 | + $this->logger->debug('Loading the required classes into super instance'); |
|
| 136 | + $this->eventdispatcher =& class_loader('EventDispatcher', 'classes'); |
|
| 137 | + $this->loader =& class_loader('Loader', 'classes'); |
|
| 138 | + $this->lang =& class_loader('Lang', 'classes'); |
|
| 139 | + $this->request =& class_loader('Request', 'classes'); |
|
| 140 | + //dispatch the request instance created event |
|
| 141 | + $this->eventdispatcher->dispatch('REQUEST_CREATED'); |
|
| 142 | + $this->response =& class_loader('Response', 'classes', 'classes'); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - } |
|
| 145 | + } |
|
@@ -1,39 +1,39 @@ discard block |
||
| 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 | - /** |
|
| 28 | - * DB session handler class |
|
| 29 | - */ |
|
| 30 | - abstract class DBSessionHandlerModel extends Model { |
|
| 27 | + /** |
|
| 28 | + * DB session handler class |
|
| 29 | + */ |
|
| 30 | + abstract class DBSessionHandlerModel extends Model { |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * The session table columns to use |
|
| 34 | - * @var array |
|
| 35 | - * @example |
|
| 36 | - * array( |
|
| 32 | + /** |
|
| 33 | + * The session table columns to use |
|
| 34 | + * @var array |
|
| 35 | + * @example |
|
| 36 | + * array( |
|
| 37 | 37 | 'sid' => '', //VARCHAR(255) Note: this a primary key |
| 38 | 38 | 'sdata' => '', //TEXT |
| 39 | 39 | 'stime' => '', //unix timestamp (INT|BIGINT) |
@@ -42,41 +42,41 @@ discard block |
||
| 42 | 42 | 'sbrowser' => '', //VARCHAR(255) |
| 43 | 43 | 'skey' => '' //VARCHAR(255) |
| 44 | 44 | ); |
| 45 | - */ |
|
| 46 | - protected $sessionTableColumns = array(); |
|
| 45 | + */ |
|
| 46 | + protected $sessionTableColumns = array(); |
|
| 47 | 47 | |
| 48 | - public function __construct(Database $db = null){ |
|
| 49 | - parent::__construct($db); |
|
| 50 | - } |
|
| 48 | + public function __construct(Database $db = null){ |
|
| 49 | + parent::__construct($db); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Return the session database table columns |
|
| 54 | - * @return array |
|
| 55 | - */ |
|
| 56 | - public function getSessionTableColumns(){ |
|
| 57 | - return $this->sessionTableColumns; |
|
| 58 | - } |
|
| 52 | + /** |
|
| 53 | + * Return the session database table columns |
|
| 54 | + * @return array |
|
| 55 | + */ |
|
| 56 | + public function getSessionTableColumns(){ |
|
| 57 | + return $this->sessionTableColumns; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Set the session database table columns |
|
| 62 | - * @param array $columns the columns definition |
|
| 63 | - */ |
|
| 64 | - public function setSessionTableColumns(array $columns){ |
|
| 65 | - $this->sessionTableColumns = $columns; |
|
| 66 | - return $this; |
|
| 67 | - } |
|
| 60 | + /** |
|
| 61 | + * Set the session database table columns |
|
| 62 | + * @param array $columns the columns definition |
|
| 63 | + */ |
|
| 64 | + public function setSessionTableColumns(array $columns){ |
|
| 65 | + $this->sessionTableColumns = $columns; |
|
| 66 | + return $this; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Delete the expire session |
|
| 71 | - * @param int $time the unix timestamp |
|
| 72 | - * @return int affected rows |
|
| 73 | - */ |
|
| 74 | - abstract public function deleteByTime($time); |
|
| 69 | + /** |
|
| 70 | + * Delete the expire session |
|
| 71 | + * @param int $time the unix timestamp |
|
| 72 | + * @return int affected rows |
|
| 73 | + */ |
|
| 74 | + abstract public function deleteByTime($time); |
|
| 75 | 75 | |
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * How to get the value of the table column key. Generally is the session key |
|
| 79 | - * @return mixed the key value like used to identify the data |
|
| 80 | - */ |
|
| 81 | - abstract public function getKeyValue(); |
|
| 82 | - } |
|
| 77 | + /** |
|
| 78 | + * How to get the value of the table column key. Generally is the session key |
|
| 79 | + * @return mixed the key value like used to identify the data |
|
| 80 | + */ |
|
| 81 | + abstract public function getKeyValue(); |
|
| 82 | + } |
|
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | - */ |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | 27 | |
| 28 | 28 | /** |
@@ -120,10 +120,10 @@ discard block |
||
| 120 | 120 | protected $_temporary_return_type = NULL; |
| 121 | 121 | |
| 122 | 122 | |
| 123 | - /** |
|
| 123 | + /** |
|
| 124 | 124 | The database cache time |
| 125 | - */ |
|
| 126 | - protected $dbCacheTime = 0; |
|
| 125 | + */ |
|
| 126 | + protected $dbCacheTime = 0; |
|
| 127 | 127 | |
| 128 | 128 | /** |
| 129 | 129 | * Instance of the Loader class |
@@ -151,12 +151,12 @@ discard block |
||
| 151 | 151 | } |
| 152 | 152 | else{ |
| 153 | 153 | $obj = & get_instance(); |
| 154 | - if (isset($obj->database) && is_object($obj->database)){ |
|
| 154 | + if (isset($obj->database) && is_object($obj->database)){ |
|
| 155 | 155 | /** |
| 156 | - * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
| 157 | - * to prevent duplication |
|
| 158 | - */ |
|
| 159 | - $this->setDatabaseInstance(clone $obj->database); |
|
| 156 | + * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
| 157 | + * to prevent duplication |
|
| 158 | + */ |
|
| 159 | + $this->setDatabaseInstance(clone $obj->database); |
|
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | 162 | |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | */ |
| 175 | 175 | public function get($primary_value) |
| 176 | 176 | { |
| 177 | - return $this->get_by($this->primary_key, $primary_value); |
|
| 177 | + return $this->get_by($this->primary_key, $primary_value); |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | /** |
@@ -189,12 +189,12 @@ discard block |
||
| 189 | 189 | { |
| 190 | 190 | $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
| 191 | 191 | } |
| 192 | - $this->_set_where($where); |
|
| 192 | + $this->_set_where($where); |
|
| 193 | 193 | |
| 194 | 194 | $this->trigger('before_get'); |
| 195 | - $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
|
| 195 | + $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
|
| 196 | 196 | $this->getQueryBuilder()->from($this->_table); |
| 197 | - $row = $this->_database->get($type); |
|
| 197 | + $row = $this->_database->get($type); |
|
| 198 | 198 | $this->_temporary_return_type = $this->return_type; |
| 199 | 199 | $row = $this->trigger('after_get', $row); |
| 200 | 200 | $this->_with = array(); |
@@ -231,9 +231,9 @@ discard block |
||
| 231 | 231 | { |
| 232 | 232 | $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
| 233 | 233 | } |
| 234 | - $type = $this->_temporary_return_type == 'array' ? 'array':false; |
|
| 234 | + $type = $this->_temporary_return_type == 'array' ? 'array':false; |
|
| 235 | 235 | $this->getQueryBuilder()->from($this->_table); |
| 236 | - $result = $this->_database->getAll($type); |
|
| 236 | + $result = $this->_database->getAll($type); |
|
| 237 | 237 | $this->_temporary_return_type = $this->return_type; |
| 238 | 238 | |
| 239 | 239 | foreach ($result as $key => &$row) |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | /** |
| 248 | 248 | * Insert a new row into the table. $data should be an associative array |
| 249 | 249 | * of data to be inserted. Returns newly created ID. |
| 250 | - * @see Database::insert |
|
| 250 | + * @see Database::insert |
|
| 251 | 251 | */ |
| 252 | 252 | public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
| 253 | 253 | { |
@@ -260,11 +260,11 @@ discard block |
||
| 260 | 260 | { |
| 261 | 261 | $data = $this->trigger('before_create', $data); |
| 262 | 262 | $this->getQueryBuilder()->from($this->_table); |
| 263 | - $this->_database->insert($data, $escape); |
|
| 263 | + $this->_database->insert($data, $escape); |
|
| 264 | 264 | $insert_id = $this->_database->insertId(); |
| 265 | 265 | $this->trigger('after_create', $insert_id); |
| 266 | - //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
| 267 | - return ! $insert_id ? true : $insert_id; |
|
| 266 | + //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
| 267 | + return ! $insert_id ? true : $insert_id; |
|
| 268 | 268 | } |
| 269 | 269 | else |
| 270 | 270 | { |
@@ -324,7 +324,7 @@ discard block |
||
| 324 | 324 | { |
| 325 | 325 | $this->getQueryBuilder()->in($this->primary_key, $primary_values) |
| 326 | 326 | ->from($this->_table); |
| 327 | - $result = $this->_database->update($data, $escape); |
|
| 327 | + $result = $this->_database->update($data, $escape); |
|
| 328 | 328 | $this->trigger('after_update', array($data, $result)); |
| 329 | 329 | return $result; |
| 330 | 330 | } |
@@ -356,7 +356,7 @@ discard block |
||
| 356 | 356 | { |
| 357 | 357 | $this->_set_where($args); |
| 358 | 358 | $this->getQueryBuilder()->from($this->_table); |
| 359 | - $result = $this->_database->update($data); |
|
| 359 | + $result = $this->_database->update($data); |
|
| 360 | 360 | $this->trigger('after_update', array($data, $result)); |
| 361 | 361 | return $result; |
| 362 | 362 | } |
@@ -373,7 +373,7 @@ discard block |
||
| 373 | 373 | { |
| 374 | 374 | $data = $this->trigger('before_update', $data); |
| 375 | 375 | $this->getQueryBuilder()->from($this->_table); |
| 376 | - $result = $this->_database->update($data, $escape); |
|
| 376 | + $result = $this->_database->update($data, $escape); |
|
| 377 | 377 | $this->trigger('after_update', array($data, $result)); |
| 378 | 378 | return $result; |
| 379 | 379 | } |
@@ -385,16 +385,16 @@ discard block |
||
| 385 | 385 | { |
| 386 | 386 | $this->trigger('before_delete', $id); |
| 387 | 387 | $this->getQueryBuilder()->where($this->primary_key, $id); |
| 388 | - $result = false; |
|
| 388 | + $result = false; |
|
| 389 | 389 | if ($this->soft_delete) |
| 390 | 390 | { |
| 391 | 391 | $this->getQueryBuilder()->from($this->_table); |
| 392 | - $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
| 392 | + $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
| 393 | 393 | } |
| 394 | 394 | else |
| 395 | 395 | { |
| 396 | 396 | $this->getQueryBuilder()->from($this->_table); |
| 397 | - $result = $this->_database->delete(); |
|
| 397 | + $result = $this->_database->delete(); |
|
| 398 | 398 | } |
| 399 | 399 | |
| 400 | 400 | $this->trigger('after_delete', $result); |
@@ -407,18 +407,18 @@ discard block |
||
| 407 | 407 | public function delete_by() |
| 408 | 408 | { |
| 409 | 409 | $where = func_get_args(); |
| 410 | - $where = $this->trigger('before_delete', $where); |
|
| 410 | + $where = $this->trigger('before_delete', $where); |
|
| 411 | 411 | $this->_set_where($where); |
| 412 | - $result = false; |
|
| 412 | + $result = false; |
|
| 413 | 413 | if ($this->soft_delete) |
| 414 | 414 | { |
| 415 | 415 | $this->getQueryBuilder()->from($this->_table); |
| 416 | - $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
| 416 | + $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
| 417 | 417 | } |
| 418 | 418 | else |
| 419 | 419 | { |
| 420 | 420 | $this->getQueryBuilder()->from($this->_table); |
| 421 | - $result = $this->_database->delete(); |
|
| 421 | + $result = $this->_database->delete(); |
|
| 422 | 422 | } |
| 423 | 423 | $this->trigger('after_delete', $result); |
| 424 | 424 | return $result; |
@@ -431,16 +431,16 @@ discard block |
||
| 431 | 431 | { |
| 432 | 432 | $primary_values = $this->trigger('before_delete', $primary_values); |
| 433 | 433 | $this->getQueryBuilder()->in($this->primary_key, $primary_values); |
| 434 | - $result = false; |
|
| 434 | + $result = false; |
|
| 435 | 435 | if ($this->soft_delete) |
| 436 | 436 | { |
| 437 | 437 | $this->getQueryBuilder()->from($this->_table); |
| 438 | - $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
| 438 | + $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
| 439 | 439 | } |
| 440 | 440 | else |
| 441 | 441 | { |
| 442 | 442 | $this->getQueryBuilder()->from($this->_table); |
| 443 | - $result = $this->_database->delete(); |
|
| 443 | + $result = $this->_database->delete(); |
|
| 444 | 444 | } |
| 445 | 445 | $this->trigger('after_delete', $result); |
| 446 | 446 | return $result; |
@@ -452,8 +452,8 @@ discard block |
||
| 452 | 452 | */ |
| 453 | 453 | public function truncate() |
| 454 | 454 | { |
| 455 | - $this->getQueryBuilder()->from($this->_table); |
|
| 456 | - $result = $this->_database->delete(); |
|
| 455 | + $this->getQueryBuilder()->from($this->_table); |
|
| 456 | + $result = $this->_database->delete(); |
|
| 457 | 457 | return $result; |
| 458 | 458 | } |
| 459 | 459 | |
@@ -471,14 +471,14 @@ discard block |
||
| 471 | 471 | return $this; |
| 472 | 472 | } |
| 473 | 473 | |
| 474 | - /** |
|
| 475 | - * Relationship |
|
| 476 | - */ |
|
| 474 | + /** |
|
| 475 | + * Relationship |
|
| 476 | + */ |
|
| 477 | 477 | public function relate($row) |
| 478 | 478 | { |
| 479 | - if (empty($row)) |
|
| 479 | + if (empty($row)) |
|
| 480 | 480 | { |
| 481 | - return $row; |
|
| 481 | + return $row; |
|
| 482 | 482 | } |
| 483 | 483 | |
| 484 | 484 | $row = $this->relateBelongsTo($row); |
@@ -511,9 +511,9 @@ discard block |
||
| 511 | 511 | $this->getQueryBuilder()->where($this->soft_delete_key, FALSE); |
| 512 | 512 | } |
| 513 | 513 | $this->getQueryBuilder() |
| 514 | - ->select(array($key, $value)) |
|
| 515 | - ->from($this->_table); |
|
| 516 | - $result = $this->_database->getAll(); |
|
| 514 | + ->select(array($key, $value)) |
|
| 515 | + ->from($this->_table); |
|
| 516 | + $result = $this->_database->getAll(); |
|
| 517 | 517 | $options = array(); |
| 518 | 518 | foreach ($result as $row) |
| 519 | 519 | { |
@@ -535,7 +535,7 @@ discard block |
||
| 535 | 535 | $where = func_get_args(); |
| 536 | 536 | $this->_set_where($where); |
| 537 | 537 | $this->getQueryBuilder()->from($this->_table); |
| 538 | - $this->_database->getAll(); |
|
| 538 | + $this->_database->getAll(); |
|
| 539 | 539 | return $this->_database->numRows(); |
| 540 | 540 | } |
| 541 | 541 | |
@@ -548,20 +548,20 @@ discard block |
||
| 548 | 548 | { |
| 549 | 549 | $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
| 550 | 550 | } |
| 551 | - $this->getQueryBuilder()->from($this->_table); |
|
| 552 | - $this->_database->getAll(); |
|
| 551 | + $this->getQueryBuilder()->from($this->_table); |
|
| 552 | + $this->_database->getAll(); |
|
| 553 | 553 | return $this->_database->numRows(); |
| 554 | 554 | } |
| 555 | 555 | |
| 556 | - /** |
|
| 557 | - * Enabled cache temporary |
|
| 558 | - */ |
|
| 559 | - public function cached($ttl = 0){ |
|
| 560 | - if ($ttl > 0){ |
|
| 561 | - $this->_database = $this->_database->cached($ttl); |
|
| 562 | - } |
|
| 563 | - return $this; |
|
| 564 | - } |
|
| 556 | + /** |
|
| 557 | + * Enabled cache temporary |
|
| 558 | + */ |
|
| 559 | + public function cached($ttl = 0){ |
|
| 560 | + if ($ttl > 0){ |
|
| 561 | + $this->_database = $this->_database->cached($ttl); |
|
| 562 | + } |
|
| 563 | + return $this; |
|
| 564 | + } |
|
| 565 | 565 | |
| 566 | 566 | /** |
| 567 | 567 | * Tell the class to skip the insert validation |
@@ -585,10 +585,10 @@ discard block |
||
| 585 | 585 | */ |
| 586 | 586 | public function get_next_id() |
| 587 | 587 | { |
| 588 | - $this->getQueryBuilder()->select('AUTO_INCREMENT') |
|
| 589 | - ->from('information_schema.TABLES') |
|
| 590 | - ->where('TABLE_NAME', $this->_table) |
|
| 591 | - ->where('TABLE_SCHEMA', $this->_database->getDatabaseName()); |
|
| 588 | + $this->getQueryBuilder()->select('AUTO_INCREMENT') |
|
| 589 | + ->from('information_schema.TABLES') |
|
| 590 | + ->where('TABLE_NAME', $this->_table) |
|
| 591 | + ->where('TABLE_SCHEMA', $this->_database->getDatabaseName()); |
|
| 592 | 592 | return (int) $this->_database->get()->AUTO_INCREMENT; |
| 593 | 593 | } |
| 594 | 594 | |
@@ -711,24 +711,24 @@ discard block |
||
| 711 | 711 | { |
| 712 | 712 | if (is_object($row)) |
| 713 | 713 | { |
| 714 | - if (isset($row->$attr)){ |
|
| 715 | - unset($row->$attr); |
|
| 716 | - } |
|
| 714 | + if (isset($row->$attr)){ |
|
| 715 | + unset($row->$attr); |
|
| 716 | + } |
|
| 717 | 717 | } |
| 718 | 718 | else |
| 719 | 719 | { |
| 720 | - if (isset($row[$attr])){ |
|
| 721 | - unset($row[$attr]); |
|
| 722 | - } |
|
| 720 | + if (isset($row[$attr])){ |
|
| 721 | + unset($row[$attr]); |
|
| 722 | + } |
|
| 723 | 723 | } |
| 724 | 724 | } |
| 725 | 725 | return $row; |
| 726 | 726 | } |
| 727 | 727 | |
| 728 | - /** |
|
| 729 | - * Return the database instance |
|
| 730 | - * @return Database the database instance |
|
| 731 | - */ |
|
| 728 | + /** |
|
| 729 | + * Return the database instance |
|
| 730 | + * @return Database the database instance |
|
| 731 | + */ |
|
| 732 | 732 | public function getDatabaseInstance(){ |
| 733 | 733 | return $this->_database; |
| 734 | 734 | } |
@@ -737,7 +737,7 @@ discard block |
||
| 737 | 737 | * set the Database instance for future use |
| 738 | 738 | * @param Database $db the database object |
| 739 | 739 | */ |
| 740 | - public function setDatabaseInstance($db){ |
|
| 740 | + public function setDatabaseInstance($db){ |
|
| 741 | 741 | $this->_database = $db; |
| 742 | 742 | if ($this->dbCacheTime > 0){ |
| 743 | 743 | $this->_database->setCache($this->dbCacheTime); |
@@ -756,14 +756,14 @@ discard block |
||
| 756 | 756 | /** |
| 757 | 757 | * Set the loader instance for future use |
| 758 | 758 | * @param Loader $loader the loader object |
| 759 | - * @return object |
|
| 759 | + * @return object |
|
| 760 | 760 | */ |
| 761 | - public function setLoader($loader){ |
|
| 761 | + public function setLoader($loader){ |
|
| 762 | 762 | $this->loaderInstance = $loader; |
| 763 | 763 | return $this; |
| 764 | 764 | } |
| 765 | 765 | |
| 766 | - /** |
|
| 766 | + /** |
|
| 767 | 767 | * Return the queryBuilder instance this is the shortcut to database queryBuilder |
| 768 | 768 | * @return object the DatabaseQueryBuilder instance |
| 769 | 769 | */ |
@@ -774,9 +774,9 @@ discard block |
||
| 774 | 774 | /** |
| 775 | 775 | * Set the DatabaseQueryBuilder instance for future use |
| 776 | 776 | * @param object $queryBuilder the DatabaseQueryBuilder object |
| 777 | - * @return object |
|
| 777 | + * @return object |
|
| 778 | 778 | */ |
| 779 | - public function setQueryBuilder($queryBuilder){ |
|
| 779 | + public function setQueryBuilder($queryBuilder){ |
|
| 780 | 780 | $this->_database->setQueryBuilder($queryBuilder); |
| 781 | 781 | return $this; |
| 782 | 782 | } |
@@ -793,9 +793,9 @@ discard block |
||
| 793 | 793 | /** |
| 794 | 794 | * Set the form validation instance for future use |
| 795 | 795 | * @param FormValidation $fv the form validation object |
| 796 | - * @return object |
|
| 796 | + * @return object |
|
| 797 | 797 | */ |
| 798 | - public function setFormValidation($fv){ |
|
| 798 | + public function setFormValidation($fv){ |
|
| 799 | 799 | $this->formValidationInstance = $fv; |
| 800 | 800 | return $this; |
| 801 | 801 | } |
@@ -836,12 +836,12 @@ discard block |
||
| 836 | 836 | * INTERNAL METHODS |
| 837 | 837 | * ------------------------------------------------------------ */ |
| 838 | 838 | |
| 839 | - /** |
|
| 840 | - * relate for the relation "belongs_to" |
|
| 841 | - * @return mixed |
|
| 842 | - */ |
|
| 843 | - protected function relateBelongsTo($row){ |
|
| 844 | - foreach ($this->belongs_to as $key => $value) |
|
| 839 | + /** |
|
| 840 | + * relate for the relation "belongs_to" |
|
| 841 | + * @return mixed |
|
| 842 | + */ |
|
| 843 | + protected function relateBelongsTo($row){ |
|
| 844 | + foreach ($this->belongs_to as $key => $value) |
|
| 845 | 845 | { |
| 846 | 846 | if (is_string($value)) |
| 847 | 847 | { |
@@ -872,15 +872,15 @@ discard block |
||
| 872 | 872 | } |
| 873 | 873 | } |
| 874 | 874 | } |
| 875 | - return $row; |
|
| 876 | - } |
|
| 877 | - |
|
| 878 | - /** |
|
| 879 | - * relate for the relation "has_many" |
|
| 880 | - * @return mixed |
|
| 881 | - */ |
|
| 882 | - protected function relateHasMany($row){ |
|
| 883 | - foreach ($this->has_many as $key => $value) |
|
| 875 | + return $row; |
|
| 876 | + } |
|
| 877 | + |
|
| 878 | + /** |
|
| 879 | + * relate for the relation "has_many" |
|
| 880 | + * @return mixed |
|
| 881 | + */ |
|
| 882 | + protected function relateHasMany($row){ |
|
| 883 | + foreach ($this->has_many as $key => $value) |
|
| 884 | 884 | { |
| 885 | 885 | if (is_string($value)) |
| 886 | 886 | { |
@@ -911,8 +911,8 @@ discard block |
||
| 911 | 911 | } |
| 912 | 912 | } |
| 913 | 913 | } |
| 914 | - return $row; |
|
| 915 | - } |
|
| 914 | + return $row; |
|
| 915 | + } |
|
| 916 | 916 | |
| 917 | 917 | /** |
| 918 | 918 | * Trigger an event and call its observers. Pass through the event name |
@@ -977,30 +977,30 @@ discard block |
||
| 977 | 977 | } |
| 978 | 978 | |
| 979 | 979 | |
| 980 | - /** |
|
| 981 | - * Set WHERE parameters, when is array |
|
| 982 | - * @param array $params |
|
| 983 | - */ |
|
| 984 | - protected function _set_where_array(array $params){ |
|
| 985 | - foreach ($params as $field => $filter) |
|
| 986 | - { |
|
| 987 | - if (is_array($filter)) |
|
| 988 | - { |
|
| 989 | - $this->getQueryBuilder()->in($field, $filter); |
|
| 990 | - } |
|
| 991 | - else |
|
| 992 | - { |
|
| 993 | - if (is_int($field)) |
|
| 994 | - { |
|
| 995 | - $this->getQueryBuilder()->where($filter); |
|
| 996 | - } |
|
| 997 | - else |
|
| 998 | - { |
|
| 999 | - $this->getQueryBuilder()->where($field, $filter); |
|
| 1000 | - } |
|
| 1001 | - } |
|
| 1002 | - } |
|
| 1003 | - } |
|
| 980 | + /** |
|
| 981 | + * Set WHERE parameters, when is array |
|
| 982 | + * @param array $params |
|
| 983 | + */ |
|
| 984 | + protected function _set_where_array(array $params){ |
|
| 985 | + foreach ($params as $field => $filter) |
|
| 986 | + { |
|
| 987 | + if (is_array($filter)) |
|
| 988 | + { |
|
| 989 | + $this->getQueryBuilder()->in($field, $filter); |
|
| 990 | + } |
|
| 991 | + else |
|
| 992 | + { |
|
| 993 | + if (is_int($field)) |
|
| 994 | + { |
|
| 995 | + $this->getQueryBuilder()->where($filter); |
|
| 996 | + } |
|
| 997 | + else |
|
| 998 | + { |
|
| 999 | + $this->getQueryBuilder()->where($field, $filter); |
|
| 1000 | + } |
|
| 1001 | + } |
|
| 1002 | + } |
|
| 1003 | + } |
|
| 1004 | 1004 | |
| 1005 | 1005 | |
| 1006 | 1006 | /** |
@@ -1016,8 +1016,8 @@ discard block |
||
| 1016 | 1016 | { |
| 1017 | 1017 | $this->getQueryBuilder()->where($params[0]); |
| 1018 | 1018 | } |
| 1019 | - else if (count($params) == 2) |
|
| 1020 | - { |
|
| 1019 | + else if (count($params) == 2) |
|
| 1020 | + { |
|
| 1021 | 1021 | if (is_array($params[1])) |
| 1022 | 1022 | { |
| 1023 | 1023 | $this->getQueryBuilder()->in($params[0], $params[1]); |
@@ -1026,11 +1026,11 @@ discard block |
||
| 1026 | 1026 | { |
| 1027 | 1027 | $this->getQueryBuilder()->where($params[0], $params[1]); |
| 1028 | 1028 | } |
| 1029 | - } |
|
| 1030 | - else if (count($params) == 3) |
|
| 1031 | - { |
|
| 1032 | - $this->getQueryBuilder()->where($params[0], $params[1], $params[2]); |
|
| 1033 | - } |
|
| 1029 | + } |
|
| 1030 | + else if (count($params) == 3) |
|
| 1031 | + { |
|
| 1032 | + $this->getQueryBuilder()->where($params[0], $params[1], $params[2]); |
|
| 1033 | + } |
|
| 1034 | 1034 | else |
| 1035 | 1035 | { |
| 1036 | 1036 | if (is_array($params[1])) |
@@ -1046,7 +1046,7 @@ discard block |
||
| 1046 | 1046 | |
| 1047 | 1047 | /** |
| 1048 | 1048 | Shortcut to controller |
| 1049 | - */ |
|
| 1049 | + */ |
|
| 1050 | 1050 | public function __get($key){ |
| 1051 | 1051 | return get_instance()->{$key}; |
| 1052 | 1052 | } |
@@ -1,208 +1,208 @@ |
||
| 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 | - |
|
| 27 | - /** |
|
| 28 | - * For application languages management |
|
| 29 | - */ |
|
| 30 | - class Lang{ |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * For application languages management |
|
| 29 | + */ |
|
| 30 | + class Lang{ |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * The supported available language for this application. |
|
| 34 | - * @example "en" => "english" |
|
| 35 | - * @see Lang::addLang() |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - protected $availables = array(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * The all messages language |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 44 | - protected $languages = array(); |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The default language to use if can not |
|
| 48 | - * determine the client language |
|
| 49 | - * |
|
| 50 | - * @example $default = 'en' |
|
| 51 | - * @var string |
|
| 52 | - */ |
|
| 53 | - protected $default = null; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * The current client language |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - protected $current = null; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * The logger instance |
|
| 63 | - * @var Log |
|
| 64 | - */ |
|
| 65 | - private $logger; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Construct new Lang instance |
|
| 69 | - */ |
|
| 70 | - public function __construct(){ |
|
| 71 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 72 | - $this->logger->setLogger('Library::Lang'); |
|
| 73 | - |
|
| 74 | - $this->default = get_config('default_language', 'en'); |
|
| 75 | - $this->logger->debug('Setting the supported languages'); |
|
| 32 | + /** |
|
| 33 | + * The supported available language for this application. |
|
| 34 | + * @example "en" => "english" |
|
| 35 | + * @see Lang::addLang() |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + protected $availables = array(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * The all messages language |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | + protected $languages = array(); |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The default language to use if can not |
|
| 48 | + * determine the client language |
|
| 49 | + * |
|
| 50 | + * @example $default = 'en' |
|
| 51 | + * @var string |
|
| 52 | + */ |
|
| 53 | + protected $default = null; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * The current client language |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + protected $current = null; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * The logger instance |
|
| 63 | + * @var Log |
|
| 64 | + */ |
|
| 65 | + private $logger; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Construct new Lang instance |
|
| 69 | + */ |
|
| 70 | + public function __construct(){ |
|
| 71 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 72 | + $this->logger->setLogger('Library::Lang'); |
|
| 73 | + |
|
| 74 | + $this->default = get_config('default_language', 'en'); |
|
| 75 | + $this->logger->debug('Setting the supported languages'); |
|
| 76 | 76 | |
| 77 | - //add the supported languages ('key', 'display name') |
|
| 78 | - $languages = get_config('languages', null); |
|
| 79 | - if(! empty($languages)){ |
|
| 80 | - foreach($languages as $key => $displayName){ |
|
| 81 | - $this->addLang($key, $displayName); |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - unset($languages); |
|
| 85 | - |
|
| 86 | - //if the language exists in cookie use it |
|
| 87 | - $cfgKey = get_config('language_cookie_name'); |
|
| 88 | - $this->logger->debug('Getting current language from cookie [' .$cfgKey. ']'); |
|
| 89 | - $objCookie = & class_loader('Cookie'); |
|
| 90 | - $cookieLang = $objCookie->get($cfgKey); |
|
| 91 | - if($cookieLang && $this->isValid($cookieLang)){ |
|
| 92 | - $this->current = $cookieLang; |
|
| 93 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is valid so we will set the language using the cookie value [' .$cookieLang. ']'); |
|
| 94 | - } |
|
| 95 | - else{ |
|
| 96 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is not set, use the default value [' .$this->getDefault(). ']'); |
|
| 97 | - $this->current = $this->getDefault(); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Get the all languages messages |
|
| 103 | - * |
|
| 104 | - * @return array the language message list |
|
| 105 | - */ |
|
| 106 | - public function getAll(){ |
|
| 107 | - return $this->languages; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Set the language message |
|
| 112 | - * |
|
| 113 | - * @param string $key the language key to identify |
|
| 114 | - * @param string $value the language message value |
|
| 115 | - */ |
|
| 116 | - public function set($key, $value){ |
|
| 117 | - $this->languages[$key] = $value; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Get the language message for the given key. If can't find return the default value |
|
| 122 | - * |
|
| 123 | - * @param string $key the message language key |
|
| 124 | - * @param string $default the default value to return if can not found the language message key |
|
| 125 | - * |
|
| 126 | - * @return string the language message value |
|
| 127 | - */ |
|
| 128 | - public function get($key, $default = 'LANGUAGE_ERROR'){ |
|
| 129 | - if(isset($this->languages[$key])){ |
|
| 130 | - return $this->languages[$key]; |
|
| 131 | - } |
|
| 132 | - $this->logger->warning('Language key [' .$key. '] does not exist use the default value [' .$default. ']'); |
|
| 133 | - return $default; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Check whether the language file for given name exists |
|
| 138 | - * |
|
| 139 | - * @param string $language the language name like "fr", "en", etc. |
|
| 140 | - * |
|
| 141 | - * @return boolean true if the language directory exists, false or not |
|
| 142 | - */ |
|
| 143 | - public function isValid($language){ |
|
| 144 | - $searchDir = array(CORE_LANG_PATH, APP_LANG_PATH); |
|
| 145 | - foreach($searchDir as $dir){ |
|
| 146 | - if(file_exists($dir . $language) && is_dir($dir . $language)){ |
|
| 147 | - return true; |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Get the default language value like "en" , "fr", etc. |
|
| 155 | - * |
|
| 156 | - * @return string the default language |
|
| 157 | - */ |
|
| 158 | - public function getDefault(){ |
|
| 159 | - return $this->default; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Get the current language defined by cookie or the default value |
|
| 164 | - * |
|
| 165 | - * @return string the current language |
|
| 166 | - */ |
|
| 167 | - public function getCurrent(){ |
|
| 168 | - return $this->current; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Add new supported or available language |
|
| 173 | - * |
|
| 174 | - * @param string $name the short language name like "en", "fr". |
|
| 175 | - * @param string $description the human readable description of this language |
|
| 176 | - */ |
|
| 177 | - public function addLang($name, $description){ |
|
| 178 | - if(isset($this->availables[$name])){ |
|
| 179 | - return; //already added cost in performance |
|
| 180 | - } |
|
| 181 | - if($this->isValid($name)){ |
|
| 182 | - $this->availables[$name] = $description; |
|
| 183 | - } |
|
| 184 | - else{ |
|
| 185 | - show_error('The language [' . $name . '] is not valid or does not exists.'); |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Get the list of the application supported language |
|
| 191 | - * |
|
| 192 | - * @return array the list of the application language |
|
| 193 | - */ |
|
| 194 | - public function getSupported(){ |
|
| 195 | - return $this->availables; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Add new language messages |
|
| 200 | - * |
|
| 201 | - * @param array $langs the languages array of the messages to be added |
|
| 202 | - */ |
|
| 203 | - public function addLangMessages(array $langs){ |
|
| 204 | - foreach ($langs as $key => $value) { |
|
| 205 | - $this->set($key, $value); |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - } |
|
| 77 | + //add the supported languages ('key', 'display name') |
|
| 78 | + $languages = get_config('languages', null); |
|
| 79 | + if(! empty($languages)){ |
|
| 80 | + foreach($languages as $key => $displayName){ |
|
| 81 | + $this->addLang($key, $displayName); |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + unset($languages); |
|
| 85 | + |
|
| 86 | + //if the language exists in cookie use it |
|
| 87 | + $cfgKey = get_config('language_cookie_name'); |
|
| 88 | + $this->logger->debug('Getting current language from cookie [' .$cfgKey. ']'); |
|
| 89 | + $objCookie = & class_loader('Cookie'); |
|
| 90 | + $cookieLang = $objCookie->get($cfgKey); |
|
| 91 | + if($cookieLang && $this->isValid($cookieLang)){ |
|
| 92 | + $this->current = $cookieLang; |
|
| 93 | + $this->logger->info('Language from cookie [' .$cfgKey. '] is valid so we will set the language using the cookie value [' .$cookieLang. ']'); |
|
| 94 | + } |
|
| 95 | + else{ |
|
| 96 | + $this->logger->info('Language from cookie [' .$cfgKey. '] is not set, use the default value [' .$this->getDefault(). ']'); |
|
| 97 | + $this->current = $this->getDefault(); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Get the all languages messages |
|
| 103 | + * |
|
| 104 | + * @return array the language message list |
|
| 105 | + */ |
|
| 106 | + public function getAll(){ |
|
| 107 | + return $this->languages; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Set the language message |
|
| 112 | + * |
|
| 113 | + * @param string $key the language key to identify |
|
| 114 | + * @param string $value the language message value |
|
| 115 | + */ |
|
| 116 | + public function set($key, $value){ |
|
| 117 | + $this->languages[$key] = $value; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Get the language message for the given key. If can't find return the default value |
|
| 122 | + * |
|
| 123 | + * @param string $key the message language key |
|
| 124 | + * @param string $default the default value to return if can not found the language message key |
|
| 125 | + * |
|
| 126 | + * @return string the language message value |
|
| 127 | + */ |
|
| 128 | + public function get($key, $default = 'LANGUAGE_ERROR'){ |
|
| 129 | + if(isset($this->languages[$key])){ |
|
| 130 | + return $this->languages[$key]; |
|
| 131 | + } |
|
| 132 | + $this->logger->warning('Language key [' .$key. '] does not exist use the default value [' .$default. ']'); |
|
| 133 | + return $default; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Check whether the language file for given name exists |
|
| 138 | + * |
|
| 139 | + * @param string $language the language name like "fr", "en", etc. |
|
| 140 | + * |
|
| 141 | + * @return boolean true if the language directory exists, false or not |
|
| 142 | + */ |
|
| 143 | + public function isValid($language){ |
|
| 144 | + $searchDir = array(CORE_LANG_PATH, APP_LANG_PATH); |
|
| 145 | + foreach($searchDir as $dir){ |
|
| 146 | + if(file_exists($dir . $language) && is_dir($dir . $language)){ |
|
| 147 | + return true; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Get the default language value like "en" , "fr", etc. |
|
| 155 | + * |
|
| 156 | + * @return string the default language |
|
| 157 | + */ |
|
| 158 | + public function getDefault(){ |
|
| 159 | + return $this->default; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Get the current language defined by cookie or the default value |
|
| 164 | + * |
|
| 165 | + * @return string the current language |
|
| 166 | + */ |
|
| 167 | + public function getCurrent(){ |
|
| 168 | + return $this->current; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Add new supported or available language |
|
| 173 | + * |
|
| 174 | + * @param string $name the short language name like "en", "fr". |
|
| 175 | + * @param string $description the human readable description of this language |
|
| 176 | + */ |
|
| 177 | + public function addLang($name, $description){ |
|
| 178 | + if(isset($this->availables[$name])){ |
|
| 179 | + return; //already added cost in performance |
|
| 180 | + } |
|
| 181 | + if($this->isValid($name)){ |
|
| 182 | + $this->availables[$name] = $description; |
|
| 183 | + } |
|
| 184 | + else{ |
|
| 185 | + show_error('The language [' . $name . '] is not valid or does not exists.'); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Get the list of the application supported language |
|
| 191 | + * |
|
| 192 | + * @return array the list of the application language |
|
| 193 | + */ |
|
| 194 | + public function getSupported(){ |
|
| 195 | + return $this->availables; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Add new language messages |
|
| 200 | + * |
|
| 201 | + * @param array $langs the languages array of the messages to be added |
|
| 202 | + */ |
|
| 203 | + public function addLangMessages(array $langs){ |
|
| 204 | + foreach ($langs as $key => $value) { |
|
| 205 | + $this->set($key, $value); |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + } |
|