@@ -1,90 +1,90 @@ 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 DatabaseQueryRunner{ |
|
| 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 DatabaseQueryRunner{ |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | - * The logger instance |
|
| 30 | - * @var object |
|
| 31 | - */ |
|
| 29 | + * The logger instance |
|
| 30 | + * @var object |
|
| 31 | + */ |
|
| 32 | 32 | private $logger = null; |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The last query result |
|
| 36 | - * @var object |
|
| 37 | - */ |
|
| 38 | - private $queryResult = null; |
|
| 34 | + /** |
|
| 35 | + * The last query result |
|
| 36 | + * @var object |
|
| 37 | + */ |
|
| 38 | + private $queryResult = null; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * The benchmark instance |
|
| 42 | - * @var object |
|
| 43 | - */ |
|
| 40 | + /** |
|
| 41 | + * The benchmark instance |
|
| 42 | + * @var object |
|
| 43 | + */ |
|
| 44 | 44 | private $benchmarkInstance = null; |
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | - * The SQL query statment to execute |
|
| 48 | - * @var string |
|
| 49 | - */ |
|
| 47 | + * The SQL query statment to execute |
|
| 48 | + * @var string |
|
| 49 | + */ |
|
| 50 | 50 | private $query = null; |
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | - * Indicate if we need return result as list (boolean) |
|
| 53 | + * Indicate if we need return result as list (boolean) |
|
| 54 | 54 | * or the data used to replace the placeholder (array) |
| 55 | - * @var array|boolean |
|
| 56 | - */ |
|
| 57 | - private $returnAsList = true; |
|
| 55 | + * @var array|boolean |
|
| 56 | + */ |
|
| 57 | + private $returnAsList = true; |
|
| 58 | 58 | |
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Indicate if we need return result as array or not |
|
| 62 | - * @var boolean |
|
| 63 | - */ |
|
| 64 | - private $returnAsArray = true; |
|
| 60 | + /** |
|
| 61 | + * Indicate if we need return result as array or not |
|
| 62 | + * @var boolean |
|
| 63 | + */ |
|
| 64 | + private $returnAsArray = true; |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * The last PDOStatment instance |
|
| 68 | - * @var object |
|
| 69 | - */ |
|
| 70 | - private $pdoStatment = null; |
|
| 66 | + /** |
|
| 67 | + * The last PDOStatment instance |
|
| 68 | + * @var object |
|
| 69 | + */ |
|
| 70 | + private $pdoStatment = null; |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * The error returned for the last query |
|
| 74 | - * @var string |
|
| 75 | - */ |
|
| 76 | - private $error = null; |
|
| 72 | + /** |
|
| 73 | + * The error returned for the last query |
|
| 74 | + * @var string |
|
| 75 | + */ |
|
| 76 | + private $error = null; |
|
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | 79 | * The PDO instance |
| 80 | 80 | * @var object |
| 81 | - */ |
|
| 81 | + */ |
|
| 82 | 82 | private $pdo = null; |
| 83 | 83 | |
| 84 | 84 | /** |
| 85 | 85 | * The database driver name used |
| 86 | 86 | * @var string |
| 87 | - */ |
|
| 87 | + */ |
|
| 88 | 88 | private $driver = null; |
| 89 | 89 | |
| 90 | 90 | |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | */ |
| 99 | 99 | public function __construct(PDO $pdo = null, $query = null, $returnAsList = true, $returnAsArray = false){ |
| 100 | 100 | if (is_object($pdo)){ |
| 101 | - $this->pdo = $pdo; |
|
| 101 | + $this->pdo = $pdo; |
|
| 102 | 102 | } |
| 103 | 103 | $this->query = $query; |
| 104 | 104 | $this->returnAsList = $returnAsList; |
@@ -116,10 +116,10 @@ discard block |
||
| 116 | 116 | //reset instance |
| 117 | 117 | $this->reset(); |
| 118 | 118 | |
| 119 | - //for database query execution time |
|
| 119 | + //for database query execution time |
|
| 120 | 120 | $benchmarkMarkerKey = $this->getBenchmarkKey(); |
| 121 | 121 | if (! is_object($this->benchmarkInstance)){ |
| 122 | - $this->benchmarkInstance = & class_loader('Benchmark'); |
|
| 122 | + $this->benchmarkInstance = & class_loader('Benchmark'); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | $this->logger->info('Execute SQL query [' . $this->query . ']'); |
@@ -132,101 +132,101 @@ discard block |
||
| 132 | 132 | $responseTime = $this->benchmarkInstance->elapsedTime( |
| 133 | 133 | 'DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', |
| 134 | 134 | 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')' |
| 135 | - ); |
|
| 136 | - //TODO use the configuration value for the high response time currently is 1 second |
|
| 135 | + ); |
|
| 136 | + //TODO use the configuration value for the high response time currently is 1 second |
|
| 137 | 137 | if ($responseTime >= 1 ){ |
| 138 | 138 | $this->logger->warning( |
| 139 | 139 | 'High response time while processing database query [' . $this->query . ']. |
| 140 | 140 | The response time is [' .$responseTime. '] sec.' |
| 141 | - ); |
|
| 141 | + ); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | if ($this->pdoStatment !== false){ |
| 145 | - $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 146 | - if($isSqlSELECTQuery){ |
|
| 147 | - $this->setResultForSelect(); |
|
| 148 | - } |
|
| 149 | - else{ |
|
| 150 | - $this->setResultForNonSelect(); |
|
| 151 | - } |
|
| 152 | - return $this->queryResult; |
|
| 145 | + $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 146 | + if($isSqlSELECTQuery){ |
|
| 147 | + $this->setResultForSelect(); |
|
| 148 | + } |
|
| 149 | + else{ |
|
| 150 | + $this->setResultForNonSelect(); |
|
| 151 | + } |
|
| 152 | + return $this->queryResult; |
|
| 153 | 153 | } |
| 154 | 154 | $this->setQueryRunnerError(); |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - /** |
|
| 158 | - * Return the result for SELECT query |
|
| 159 | - * @see DatabaseQueryRunner::execute |
|
| 160 | - */ |
|
| 157 | + /** |
|
| 158 | + * Return the result for SELECT query |
|
| 159 | + * @see DatabaseQueryRunner::execute |
|
| 160 | + */ |
|
| 161 | 161 | protected function setResultForSelect(){ |
| 162 | - //if need return all result like list of record |
|
| 163 | - $result = null; |
|
| 164 | - $numRows = 0; |
|
| 165 | - $fetchMode = PDO::FETCH_OBJ; |
|
| 166 | - if($this->returnAsArray){ |
|
| 162 | + //if need return all result like list of record |
|
| 163 | + $result = null; |
|
| 164 | + $numRows = 0; |
|
| 165 | + $fetchMode = PDO::FETCH_OBJ; |
|
| 166 | + if($this->returnAsArray){ |
|
| 167 | 167 | $fetchMode = PDO::FETCH_ASSOC; |
| 168 | - } |
|
| 169 | - if ($this->returnAsList){ |
|
| 170 | - $result = $this->pdoStatment->fetchAll($fetchMode); |
|
| 171 | - } |
|
| 172 | - else{ |
|
| 173 | - $result = $this->pdoStatment->fetch($fetchMode); |
|
| 174 | - } |
|
| 175 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 176 | - if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
| 168 | + } |
|
| 169 | + if ($this->returnAsList){ |
|
| 170 | + $result = $this->pdoStatment->fetchAll($fetchMode); |
|
| 171 | + } |
|
| 172 | + else{ |
|
| 173 | + $result = $this->pdoStatment->fetch($fetchMode); |
|
| 174 | + } |
|
| 175 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 176 | + if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
| 177 | 177 | $numRows = count($result); |
| 178 | - } |
|
| 179 | - else{ |
|
| 178 | + } |
|
| 179 | + else{ |
|
| 180 | 180 | $numRows = $this->pdoStatment->rowCount(); |
| 181 | - } |
|
| 182 | - if(! is_object($this->queryResult)){ |
|
| 183 | - $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
| 184 | - } |
|
| 185 | - $this->queryResult->setResult($result); |
|
| 186 | - $this->queryResult->setNumRows($numRows); |
|
| 181 | + } |
|
| 182 | + if(! is_object($this->queryResult)){ |
|
| 183 | + $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
| 184 | + } |
|
| 185 | + $this->queryResult->setResult($result); |
|
| 186 | + $this->queryResult->setNumRows($numRows); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | /** |
| 190 | - * Return the result for non SELECT query |
|
| 191 | - * @see DatabaseQueryRunner::execute |
|
| 192 | - */ |
|
| 190 | + * Return the result for non SELECT query |
|
| 191 | + * @see DatabaseQueryRunner::execute |
|
| 192 | + */ |
|
| 193 | 193 | protected function setResultForNonSelect(){ |
| 194 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
| 195 | - $result = false; |
|
| 196 | - $numRows = 0; |
|
| 197 | - if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
| 194 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
| 195 | + $result = false; |
|
| 196 | + $numRows = 0; |
|
| 197 | + if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
| 198 | 198 | $result = true; //to test the result for the query like UPDATE, INSERT, DELETE |
| 199 | 199 | $numRows = 1; //TODO use the correct method to get the exact affected row |
| 200 | - } |
|
| 201 | - else{ |
|
| 202 | - //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 203 | - $result = $this->pdoStatment->rowCount() >= 0; |
|
| 204 | - $numRows = $this->pdoStatment->rowCount(); |
|
| 205 | - } |
|
| 206 | - if(! is_object($this->queryResult)){ |
|
| 207 | - $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
| 208 | - } |
|
| 209 | - $this->queryResult->setResult($result); |
|
| 210 | - $this->queryResult->setNumRows($numRows); |
|
| 200 | + } |
|
| 201 | + else{ |
|
| 202 | + //to test the result for the query like UPDATE, INSERT, DELETE |
|
| 203 | + $result = $this->pdoStatment->rowCount() >= 0; |
|
| 204 | + $numRows = $this->pdoStatment->rowCount(); |
|
| 205 | + } |
|
| 206 | + if(! is_object($this->queryResult)){ |
|
| 207 | + $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
| 208 | + } |
|
| 209 | + $this->queryResult->setResult($result); |
|
| 210 | + $this->queryResult->setNumRows($numRows); |
|
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | |
| 214 | - /** |
|
| 214 | + /** |
|
| 215 | 215 | * Return the benchmark instance |
| 216 | 216 | * @return Benchmark |
| 217 | 217 | */ |
| 218 | 218 | public function getBenchmark(){ |
| 219 | - return $this->benchmarkInstance; |
|
| 219 | + return $this->benchmarkInstance; |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | /** |
| 223 | 223 | * Set the benchmark instance |
| 224 | 224 | * @param Benchmark $benchmark the benchmark object |
| 225 | - * @return object DatabaseQueryRunner |
|
| 225 | + * @return object DatabaseQueryRunner |
|
| 226 | 226 | */ |
| 227 | 227 | public function setBenchmark($benchmark){ |
| 228 | - $this->benchmarkInstance = $benchmark; |
|
| 229 | - return $this; |
|
| 228 | + $this->benchmarkInstance = $benchmark; |
|
| 229 | + return $this; |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | /** |
@@ -235,18 +235,18 @@ discard block |
||
| 235 | 235 | * @return object DatabaseQueryResult |
| 236 | 236 | */ |
| 237 | 237 | public function getQueryResult(){ |
| 238 | - return $this->queryResult; |
|
| 238 | + return $this->queryResult; |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | 241 | /** |
| 242 | 242 | * Set the database query result instance |
| 243 | 243 | * @param object $queryResult the query result |
| 244 | 244 | * |
| 245 | - * @return object DatabaseQueryRunner |
|
| 245 | + * @return object DatabaseQueryRunner |
|
| 246 | 246 | */ |
| 247 | 247 | public function setQueryResult(DatabaseQueryResult $queryResult){ |
| 248 | - $this->queryResult = $queryResult; |
|
| 249 | - return $this; |
|
| 248 | + $this->queryResult = $queryResult; |
|
| 249 | + return $this; |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | /** |
@@ -254,17 +254,17 @@ discard block |
||
| 254 | 254 | * @return Log |
| 255 | 255 | */ |
| 256 | 256 | public function getLogger(){ |
| 257 | - return $this->logger; |
|
| 257 | + return $this->logger; |
|
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | /** |
| 261 | 261 | * Set the log instance |
| 262 | 262 | * @param Log $logger the log object |
| 263 | - * @return object DatabaseQueryRunner |
|
| 263 | + * @return object DatabaseQueryRunner |
|
| 264 | 264 | */ |
| 265 | 265 | public function setLogger($logger){ |
| 266 | - $this->logger = $logger; |
|
| 267 | - return $this; |
|
| 266 | + $this->logger = $logger; |
|
| 267 | + return $this; |
|
| 268 | 268 | } |
| 269 | 269 | |
| 270 | 270 | /** |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | * @return string |
| 273 | 273 | */ |
| 274 | 274 | public function getQuery(){ |
| 275 | - return $this->query; |
|
| 275 | + return $this->query; |
|
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | /** |
@@ -281,8 +281,8 @@ discard block |
||
| 281 | 281 | * @return object DatabaseQueryRunner |
| 282 | 282 | */ |
| 283 | 283 | public function setQuery($query){ |
| 284 | - $this->query = $query; |
|
| 285 | - return $this; |
|
| 284 | + $this->query = $query; |
|
| 285 | + return $this; |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | /** |
@@ -291,8 +291,8 @@ discard block |
||
| 291 | 291 | * @return object DatabaseQueryRunner |
| 292 | 292 | */ |
| 293 | 293 | public function setReturnType($returnType){ |
| 294 | - $this->returnAsList = $returnType; |
|
| 295 | - return $this; |
|
| 294 | + $this->returnAsList = $returnType; |
|
| 295 | + return $this; |
|
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | /** |
@@ -301,8 +301,8 @@ discard block |
||
| 301 | 301 | * @return object DatabaseQueryRunner |
| 302 | 302 | */ |
| 303 | 303 | public function setReturnAsArray($status = true){ |
| 304 | - $this->returnAsArray = $status; |
|
| 305 | - return $this; |
|
| 304 | + $this->returnAsArray = $status; |
|
| 305 | + return $this; |
|
| 306 | 306 | } |
| 307 | 307 | |
| 308 | 308 | /** |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | * @return string |
| 311 | 311 | */ |
| 312 | 312 | public function getQueryError(){ |
| 313 | - return $this->error; |
|
| 313 | + return $this->error; |
|
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | /** |
@@ -318,7 +318,7 @@ discard block |
||
| 318 | 318 | * @return object |
| 319 | 319 | */ |
| 320 | 320 | public function getPdo(){ |
| 321 | - return $this->pdo; |
|
| 321 | + return $this->pdo; |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | /** |
@@ -327,16 +327,16 @@ discard block |
||
| 327 | 327 | * @return object DatabaseQueryRunner |
| 328 | 328 | */ |
| 329 | 329 | public function setPdo(PDO $pdo = null){ |
| 330 | - $this->pdo = $pdo; |
|
| 331 | - return $this; |
|
| 330 | + $this->pdo = $pdo; |
|
| 331 | + return $this; |
|
| 332 | 332 | } |
| 333 | 333 | |
| 334 | - /** |
|
| 335 | - * Return the database driver |
|
| 336 | - * @return string |
|
| 337 | - */ |
|
| 334 | + /** |
|
| 335 | + * Return the database driver |
|
| 336 | + * @return string |
|
| 337 | + */ |
|
| 338 | 338 | public function getDriver(){ |
| 339 | - return $this->driver; |
|
| 339 | + return $this->driver; |
|
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | /** |
@@ -345,8 +345,8 @@ discard block |
||
| 345 | 345 | * @return object DatabaseQueryRunner |
| 346 | 346 | */ |
| 347 | 347 | public function setDriver($driver){ |
| 348 | - $this->driver = $driver; |
|
| 349 | - return $this; |
|
| 348 | + $this->driver = $driver; |
|
| 349 | + return $this; |
|
| 350 | 350 | } |
| 351 | 351 | |
| 352 | 352 | /** |
@@ -355,18 +355,18 @@ discard block |
||
| 355 | 355 | * @return string |
| 356 | 356 | */ |
| 357 | 357 | protected function getBenchmarkKey(){ |
| 358 | - return md5($this->query . $this->returnAsList . $this->returnAsArray); |
|
| 358 | + return md5($this->query . $this->returnAsList . $this->returnAsArray); |
|
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | /** |
| 362 | 362 | * Set error for database query execution |
| 363 | 363 | */ |
| 364 | 364 | protected function setQueryRunnerError(){ |
| 365 | - $error = $this->pdo->errorInfo(); |
|
| 366 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 367 | - $this->logger->error('The database query execution got an error: ' . stringfy_vars($error)); |
|
| 368 | - //show error message |
|
| 369 | - show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
|
| 365 | + $error = $this->pdo->errorInfo(); |
|
| 366 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
| 367 | + $this->logger->error('The database query execution got an error: ' . stringfy_vars($error)); |
|
| 368 | + //show error message |
|
| 369 | + show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
|
| 370 | 370 | } |
| 371 | 371 | |
| 372 | 372 | /** |
@@ -374,19 +374,19 @@ discard block |
||
| 374 | 374 | * @param object $logger the Log instance if not null |
| 375 | 375 | */ |
| 376 | 376 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
| 377 | - if ($logger !== null){ |
|
| 377 | + if ($logger !== null){ |
|
| 378 | 378 | $this->logger = $logger; |
| 379 | - } |
|
| 380 | - else{ |
|
| 381 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 382 | - $this->logger->setLogger('Library::DatabaseQueryRunner'); |
|
| 383 | - } |
|
| 379 | + } |
|
| 380 | + else{ |
|
| 381 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 382 | + $this->logger->setLogger('Library::DatabaseQueryRunner'); |
|
| 383 | + } |
|
| 384 | 384 | } |
| 385 | 385 | |
| 386 | 386 | |
| 387 | 387 | /** |
| 388 | - * Reset the instance before run each query |
|
| 389 | - */ |
|
| 388 | + * Reset the instance before run each query |
|
| 389 | + */ |
|
| 390 | 390 | private function reset(){ |
| 391 | 391 | $this->error = null; |
| 392 | 392 | $this->pdoStatment = null; |
@@ -1,119 +1,119 @@ 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 Database{ |
|
| 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 Database{ |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * The PDO instance |
|
| 30 | - * @var object |
|
| 31 | - */ |
|
| 28 | + /** |
|
| 29 | + * The PDO instance |
|
| 30 | + * @var object |
|
| 31 | + */ |
|
| 32 | 32 | private $pdo = null; |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The database name used for the application |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - private $databaseName = null; |
|
| 34 | + /** |
|
| 35 | + * The database name used for the application |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + private $databaseName = null; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * The number of rows returned by the last query |
|
| 42 | - * @var int |
|
| 43 | - */ |
|
| 40 | + /** |
|
| 41 | + * The number of rows returned by the last query |
|
| 42 | + * @var int |
|
| 43 | + */ |
|
| 44 | 44 | private $numRows = 0; |
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * The last insert id for the primary key column that have auto increment or sequence |
|
| 48 | - * @var mixed |
|
| 49 | - */ |
|
| 46 | + /** |
|
| 47 | + * The last insert id for the primary key column that have auto increment or sequence |
|
| 48 | + * @var mixed |
|
| 49 | + */ |
|
| 50 | 50 | private $insertId = null; |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * The full SQL query statment after build for each command |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 52 | + /** |
|
| 53 | + * The full SQL query statment after build for each command |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | 56 | private $query = null; |
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * The result returned for the last query |
|
| 60 | - * @var mixed |
|
| 61 | - */ |
|
| 58 | + /** |
|
| 59 | + * The result returned for the last query |
|
| 60 | + * @var mixed |
|
| 61 | + */ |
|
| 62 | 62 | private $result = array(); |
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * The cache default time to live in second. 0 means no need to use the cache feature |
|
| 66 | - * @var int |
|
| 67 | - */ |
|
| 68 | - private $cacheTtl = 0; |
|
| 64 | + /** |
|
| 65 | + * The cache default time to live in second. 0 means no need to use the cache feature |
|
| 66 | + * @var int |
|
| 67 | + */ |
|
| 68 | + private $cacheTtl = 0; |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * The cache current time to live. 0 means no need to use the cache feature |
|
| 72 | - * @var int |
|
| 73 | - */ |
|
| 70 | + /** |
|
| 71 | + * The cache current time to live. 0 means no need to use the cache feature |
|
| 72 | + * @var int |
|
| 73 | + */ |
|
| 74 | 74 | private $temporaryCacheTtl = 0; |
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * The number of executed query for the current request |
|
| 78 | - * @var int |
|
| 79 | - */ |
|
| 76 | + /** |
|
| 77 | + * The number of executed query for the current request |
|
| 78 | + * @var int |
|
| 79 | + */ |
|
| 80 | 80 | private $queryCount = 0; |
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * The default data to be used in the statments query INSERT, UPDATE |
|
| 84 | - * @var array |
|
| 85 | - */ |
|
| 82 | + /** |
|
| 83 | + * The default data to be used in the statments query INSERT, UPDATE |
|
| 84 | + * @var array |
|
| 85 | + */ |
|
| 86 | 86 | private $data = array(); |
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * The database configuration |
|
| 90 | - * @var array |
|
| 91 | - */ |
|
| 88 | + /** |
|
| 89 | + * The database configuration |
|
| 90 | + * @var array |
|
| 91 | + */ |
|
| 92 | 92 | private $config = array(); |
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * The logger instance |
|
| 96 | - * @var object |
|
| 97 | - */ |
|
| 94 | + /** |
|
| 95 | + * The logger instance |
|
| 96 | + * @var object |
|
| 97 | + */ |
|
| 98 | 98 | private $logger = null; |
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | - * The cache instance |
|
| 102 | - * @var object |
|
| 103 | - */ |
|
| 101 | + * The cache instance |
|
| 102 | + * @var object |
|
| 103 | + */ |
|
| 104 | 104 | private $cacheInstance = null; |
| 105 | 105 | |
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * The DatabaseQueryBuilder instance |
|
| 109 | - * @var object |
|
| 110 | - */ |
|
| 107 | + /** |
|
| 108 | + * The DatabaseQueryBuilder instance |
|
| 109 | + * @var object |
|
| 110 | + */ |
|
| 111 | 111 | private $queryBuilder = null; |
| 112 | 112 | |
| 113 | 113 | /** |
| 114 | - * The DatabaseQueryRunner instance |
|
| 115 | - * @var object |
|
| 116 | - */ |
|
| 114 | + * The DatabaseQueryRunner instance |
|
| 115 | + * @var object |
|
| 116 | + */ |
|
| 117 | 117 | private $queryRunner = null; |
| 118 | 118 | |
| 119 | 119 | |
@@ -125,8 +125,8 @@ discard block |
||
| 125 | 125 | //Set Log instance to use |
| 126 | 126 | $this->setLoggerFromParamOrCreateNewInstance(null); |
| 127 | 127 | |
| 128 | - //Set DatabaseQueryBuilder instance to use |
|
| 129 | - $this->setQueryBuilderFromParamOrCreateNewInstance(null); |
|
| 128 | + //Set DatabaseQueryBuilder instance to use |
|
| 129 | + $this->setQueryBuilderFromParamOrCreateNewInstance(null); |
|
| 130 | 130 | |
| 131 | 131 | //Set DatabaseQueryRunner instance to use |
| 132 | 132 | $this->setQueryRunnerFromParamOrCreateNewInstance(null); |
@@ -143,22 +143,22 @@ discard block |
||
| 143 | 143 | * @return bool |
| 144 | 144 | */ |
| 145 | 145 | public function connect(){ |
| 146 | - $config = $this->getDatabaseConfiguration(); |
|
| 147 | - if (! empty($config)){ |
|
| 146 | + $config = $this->getDatabaseConfiguration(); |
|
| 147 | + if (! empty($config)){ |
|
| 148 | 148 | try{ |
| 149 | 149 | $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
| 150 | 150 | $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
| 151 | 151 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
| 152 | 152 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
| 153 | 153 | return true; |
| 154 | - } |
|
| 155 | - catch (PDOException $e){ |
|
| 154 | + } |
|
| 155 | + catch (PDOException $e){ |
|
| 156 | 156 | $this->logger->fatal($e->getMessage()); |
| 157 | 157 | show_error('Cannot connect to Database.'); |
| 158 | 158 | return false; |
| 159 | - } |
|
| 160 | - } |
|
| 161 | - return false; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + return false; |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | * @return int |
| 168 | 168 | */ |
| 169 | 169 | public function numRows(){ |
| 170 | - return $this->numRows; |
|
| 170 | + return $this->numRows; |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /** |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | * @return mixed |
| 176 | 176 | */ |
| 177 | 177 | public function insertId(){ |
| 178 | - return $this->insertId; |
|
| 178 | + return $this->insertId; |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | |
@@ -186,13 +186,13 @@ discard block |
||
| 186 | 186 | * @return mixed the query SQL string or the record result |
| 187 | 187 | */ |
| 188 | 188 | public function get($returnSQLQueryOrResultType = false){ |
| 189 | - $this->getQueryBuilder()->limit(1); |
|
| 190 | - $query = $this->getAll(true); |
|
| 191 | - if ($returnSQLQueryOrResultType === true){ |
|
| 189 | + $this->getQueryBuilder()->limit(1); |
|
| 190 | + $query = $this->getAll(true); |
|
| 191 | + if ($returnSQLQueryOrResultType === true){ |
|
| 192 | 192 | return $query; |
| 193 | - } else { |
|
| 193 | + } else { |
|
| 194 | 194 | return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
| 195 | - } |
|
| 195 | + } |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -202,11 +202,11 @@ discard block |
||
| 202 | 202 | * @return mixed the query SQL string or the record result |
| 203 | 203 | */ |
| 204 | 204 | public function getAll($returnSQLQueryOrResultType = false){ |
| 205 | - $query = $this->getQueryBuilder()->getQuery(); |
|
| 206 | - if ($returnSQLQueryOrResultType === true){ |
|
| 207 | - return $query; |
|
| 208 | - } |
|
| 209 | - return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
| 205 | + $query = $this->getQueryBuilder()->getQuery(); |
|
| 206 | + if ($returnSQLQueryOrResultType === true){ |
|
| 207 | + return $query; |
|
| 208 | + } |
|
| 209 | + return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | /** |
@@ -216,19 +216,19 @@ discard block |
||
| 216 | 216 | * @return mixed the insert id of the new record or null |
| 217 | 217 | */ |
| 218 | 218 | public function insert($data = array(), $escape = true){ |
| 219 | - if (empty($data) && $this->getData()){ |
|
| 219 | + if (empty($data) && $this->getData()){ |
|
| 220 | 220 | //as when using $this->setData() may be the data already escaped |
| 221 | 221 | $escape = false; |
| 222 | 222 | $data = $this->getData(); |
| 223 | - } |
|
| 224 | - $query = $this->getQueryBuilder()->insert($data, $escape)->getQuery(); |
|
| 225 | - $result = $this->query($query); |
|
| 226 | - if ($result){ |
|
| 223 | + } |
|
| 224 | + $query = $this->getQueryBuilder()->insert($data, $escape)->getQuery(); |
|
| 225 | + $result = $this->query($query); |
|
| 226 | + if ($result){ |
|
| 227 | 227 | $this->insertId = $this->pdo->lastInsertId(); |
| 228 | - //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
| 228 | + //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
| 229 | 229 | return ! $this->insertId() ? true : $this->insertId(); |
| 230 | - } |
|
| 231 | - return false; |
|
| 230 | + } |
|
| 231 | + return false; |
|
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | /** |
@@ -238,13 +238,13 @@ discard block |
||
| 238 | 238 | * @return mixed the update status |
| 239 | 239 | */ |
| 240 | 240 | public function update($data = array(), $escape = true){ |
| 241 | - if (empty($data) && $this->getData()){ |
|
| 241 | + if (empty($data) && $this->getData()){ |
|
| 242 | 242 | //as when using $this->setData() may be the data already escaped |
| 243 | 243 | $escape = false; |
| 244 | 244 | $data = $this->getData(); |
| 245 | - } |
|
| 246 | - $query = $this->getQueryBuilder()->update($data, $escape)->getQuery(); |
|
| 247 | - return $this->query($query); |
|
| 245 | + } |
|
| 246 | + $query = $this->getQueryBuilder()->update($data, $escape)->getQuery(); |
|
| 247 | + return $this->query($query); |
|
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | /** |
@@ -252,8 +252,8 @@ discard block |
||
| 252 | 252 | * @return mixed the delete status |
| 253 | 253 | */ |
| 254 | 254 | public function delete(){ |
| 255 | - $query = $this->getQueryBuilder()->delete()->getQuery(); |
|
| 256 | - return $this->query($query); |
|
| 255 | + $query = $this->getQueryBuilder()->delete()->getQuery(); |
|
| 256 | + return $this->query($query); |
|
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | /** |
@@ -262,21 +262,21 @@ discard block |
||
| 262 | 262 | * @return object the current Database instance |
| 263 | 263 | */ |
| 264 | 264 | public function setCache($ttl = 0){ |
| 265 | - if ($ttl > 0){ |
|
| 265 | + if ($ttl > 0){ |
|
| 266 | 266 | $this->cacheTtl = $ttl; |
| 267 | 267 | $this->temporaryCacheTtl = $ttl; |
| 268 | - } |
|
| 269 | - return $this; |
|
| 268 | + } |
|
| 269 | + return $this; |
|
| 270 | 270 | } |
| 271 | 271 | |
| 272 | - /** |
|
| 273 | - * Enabled cache temporary for the current query not globally |
|
| 274 | - * @param integer $ttl the cache time to live in second |
|
| 275 | - * @return object the current Database instance |
|
| 276 | - */ |
|
| 277 | - public function cached($ttl = 0){ |
|
| 272 | + /** |
|
| 273 | + * Enabled cache temporary for the current query not globally |
|
| 274 | + * @param integer $ttl the cache time to live in second |
|
| 275 | + * @return object the current Database instance |
|
| 276 | + */ |
|
| 277 | + public function cached($ttl = 0){ |
|
| 278 | 278 | if ($ttl > 0){ |
| 279 | - $this->temporaryCacheTtl = $ttl; |
|
| 279 | + $this->temporaryCacheTtl = $ttl; |
|
| 280 | 280 | } |
| 281 | 281 | return $this; |
| 282 | 282 | } |
@@ -288,7 +288,7 @@ discard block |
||
| 288 | 288 | * @return mixed the data after escaped or the same data if not |
| 289 | 289 | */ |
| 290 | 290 | public function escape($data, $escaped = true){ |
| 291 | - return $escaped ? |
|
| 291 | + return $escaped ? |
|
| 292 | 292 | $this->pdo->quote(trim($data)) |
| 293 | 293 | : $data; |
| 294 | 294 | } |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | * @return int |
| 299 | 299 | */ |
| 300 | 300 | public function queryCount(){ |
| 301 | - return $this->queryCount; |
|
| 301 | + return $this->queryCount; |
|
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | /** |
@@ -306,7 +306,7 @@ discard block |
||
| 306 | 306 | * @return string |
| 307 | 307 | */ |
| 308 | 308 | public function getQuery(){ |
| 309 | - return $this->query; |
|
| 309 | + return $this->query; |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | /** |
@@ -314,7 +314,7 @@ discard block |
||
| 314 | 314 | * @return string |
| 315 | 315 | */ |
| 316 | 316 | public function getDatabaseName(){ |
| 317 | - return $this->databaseName; |
|
| 317 | + return $this->databaseName; |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | /** |
@@ -322,17 +322,17 @@ discard block |
||
| 322 | 322 | * @return object |
| 323 | 323 | */ |
| 324 | 324 | public function getPdo(){ |
| 325 | - return $this->pdo; |
|
| 325 | + return $this->pdo; |
|
| 326 | 326 | } |
| 327 | 327 | |
| 328 | 328 | /** |
| 329 | 329 | * Set the PDO instance |
| 330 | 330 | * @param object $pdo the pdo object |
| 331 | - * @return object Database |
|
| 331 | + * @return object Database |
|
| 332 | 332 | */ |
| 333 | 333 | public function setPdo(PDO $pdo){ |
| 334 | - $this->pdo = $pdo; |
|
| 335 | - return $this; |
|
| 334 | + $this->pdo = $pdo; |
|
| 335 | + return $this; |
|
| 336 | 336 | } |
| 337 | 337 | |
| 338 | 338 | |
@@ -341,44 +341,44 @@ discard block |
||
| 341 | 341 | * @return Log |
| 342 | 342 | */ |
| 343 | 343 | public function getLogger(){ |
| 344 | - return $this->logger; |
|
| 344 | + return $this->logger; |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | /** |
| 348 | 348 | * Set the log instance |
| 349 | 349 | * @param Log $logger the log object |
| 350 | - * @return object Database |
|
| 350 | + * @return object Database |
|
| 351 | 351 | */ |
| 352 | 352 | public function setLogger($logger){ |
| 353 | - $this->logger = $logger; |
|
| 354 | - return $this; |
|
| 353 | + $this->logger = $logger; |
|
| 354 | + return $this; |
|
| 355 | 355 | } |
| 356 | 356 | |
| 357 | - /** |
|
| 358 | - * Return the cache instance |
|
| 359 | - * @return CacheInterface |
|
| 360 | - */ |
|
| 357 | + /** |
|
| 358 | + * Return the cache instance |
|
| 359 | + * @return CacheInterface |
|
| 360 | + */ |
|
| 361 | 361 | public function getCacheInstance(){ |
| 362 | - return $this->cacheInstance; |
|
| 362 | + return $this->cacheInstance; |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | /** |
| 366 | 366 | * Set the cache instance |
| 367 | 367 | * @param CacheInterface $cache the cache object |
| 368 | - * @return object Database |
|
| 368 | + * @return object Database |
|
| 369 | 369 | */ |
| 370 | 370 | public function setCacheInstance($cache){ |
| 371 | - $this->cacheInstance = $cache; |
|
| 372 | - return $this; |
|
| 371 | + $this->cacheInstance = $cache; |
|
| 372 | + return $this; |
|
| 373 | 373 | } |
| 374 | 374 | |
| 375 | 375 | |
| 376 | - /** |
|
| 377 | - * Return the DatabaseQueryBuilder instance |
|
| 378 | - * @return object DatabaseQueryBuilder |
|
| 379 | - */ |
|
| 376 | + /** |
|
| 377 | + * Return the DatabaseQueryBuilder instance |
|
| 378 | + * @return object DatabaseQueryBuilder |
|
| 379 | + */ |
|
| 380 | 380 | public function getQueryBuilder(){ |
| 381 | - return $this->queryBuilder; |
|
| 381 | + return $this->queryBuilder; |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -386,8 +386,8 @@ discard block |
||
| 386 | 386 | * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
| 387 | 387 | */ |
| 388 | 388 | public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder){ |
| 389 | - $this->queryBuilder = $queryBuilder; |
|
| 390 | - return $this; |
|
| 389 | + $this->queryBuilder = $queryBuilder; |
|
| 390 | + return $this; |
|
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | /** |
@@ -395,7 +395,7 @@ discard block |
||
| 395 | 395 | * @return object DatabaseQueryRunner |
| 396 | 396 | */ |
| 397 | 397 | public function getQueryRunner(){ |
| 398 | - return $this->queryRunner; |
|
| 398 | + return $this->queryRunner; |
|
| 399 | 399 | } |
| 400 | 400 | |
| 401 | 401 | /** |
@@ -403,8 +403,8 @@ discard block |
||
| 403 | 403 | * @param object DatabaseQueryRunner $queryRunner the DatabaseQueryRunner object |
| 404 | 404 | */ |
| 405 | 405 | public function setQueryRunner(DatabaseQueryRunner $queryRunner){ |
| 406 | - $this->queryRunner = $queryRunner; |
|
| 407 | - return $this; |
|
| 406 | + $this->queryRunner = $queryRunner; |
|
| 407 | + return $this; |
|
| 408 | 408 | } |
| 409 | 409 | |
| 410 | 410 | /** |
@@ -412,7 +412,7 @@ discard block |
||
| 412 | 412 | * @return array |
| 413 | 413 | */ |
| 414 | 414 | public function getData(){ |
| 415 | - return $this->data; |
|
| 415 | + return $this->data; |
|
| 416 | 416 | } |
| 417 | 417 | |
| 418 | 418 | /** |
@@ -423,51 +423,51 @@ discard block |
||
| 423 | 423 | * @return object the current Database instance |
| 424 | 424 | */ |
| 425 | 425 | public function setData($key, $value = null, $escape = true){ |
| 426 | - if(is_array($key)){ |
|
| 427 | - foreach($key as $k => $v){ |
|
| 428 | - $this->setData($k, $v, $escape); |
|
| 429 | - } |
|
| 430 | - } else { |
|
| 426 | + if(is_array($key)){ |
|
| 427 | + foreach($key as $k => $v){ |
|
| 428 | + $this->setData($k, $v, $escape); |
|
| 429 | + } |
|
| 430 | + } else { |
|
| 431 | 431 | $this->data[$key] = $this->escape($value, $escape); |
| 432 | - } |
|
| 433 | - return $this; |
|
| 432 | + } |
|
| 433 | + return $this; |
|
| 434 | 434 | } |
| 435 | 435 | |
| 436 | - /** |
|
| 437 | - * Execute an SQL query |
|
| 438 | - * @param string $query the query SQL string |
|
| 439 | - * @param boolean $returnAsList indicate whether to return all record or just one row |
|
| 440 | - * @param boolean $returnAsArray return the result as array or not |
|
| 441 | - * @return mixed the query result |
|
| 442 | - */ |
|
| 436 | + /** |
|
| 437 | + * Execute an SQL query |
|
| 438 | + * @param string $query the query SQL string |
|
| 439 | + * @param boolean $returnAsList indicate whether to return all record or just one row |
|
| 440 | + * @param boolean $returnAsArray return the result as array or not |
|
| 441 | + * @return mixed the query result |
|
| 442 | + */ |
|
| 443 | 443 | public function query($query, $returnAsList = true, $returnAsArray = false){ |
| 444 | - $this->reset(); |
|
| 445 | - $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 446 | - //If is the SELECT query |
|
| 447 | - $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 444 | + $this->reset(); |
|
| 445 | + $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 446 | + //If is the SELECT query |
|
| 447 | + $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
| 448 | 448 | |
| 449 | - //cache expire time |
|
| 450 | - $cacheExpire = $this->temporaryCacheTtl; |
|
| 449 | + //cache expire time |
|
| 450 | + $cacheExpire = $this->temporaryCacheTtl; |
|
| 451 | 451 | |
| 452 | - //return to the initial cache time |
|
| 453 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 452 | + //return to the initial cache time |
|
| 453 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 454 | 454 | |
| 455 | - //config for cache |
|
| 456 | - $cacheEnable = get_config('cache_enable'); |
|
| 455 | + //config for cache |
|
| 456 | + $cacheEnable = get_config('cache_enable'); |
|
| 457 | 457 | |
| 458 | - //the database cache content |
|
| 459 | - $cacheContent = null; |
|
| 458 | + //the database cache content |
|
| 459 | + $cacheContent = null; |
|
| 460 | 460 | |
| 461 | - //if can use cache feature for this query |
|
| 462 | - $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
| 461 | + //if can use cache feature for this query |
|
| 462 | + $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
| 463 | 463 | |
| 464 | - if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 465 | - $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 466 | - $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
|
| 467 | - } |
|
| 464 | + if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 465 | + $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 466 | + $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
|
| 467 | + } |
|
| 468 | 468 | |
| 469 | - if ( !$cacheContent){ |
|
| 470 | - //count the number of query execution to server |
|
| 469 | + if ( !$cacheContent){ |
|
| 470 | + //count the number of query execution to server |
|
| 471 | 471 | $this->queryCount++; |
| 472 | 472 | |
| 473 | 473 | $this->queryRunner->setQuery($query); |
@@ -482,33 +482,33 @@ discard block |
||
| 482 | 482 | $key = $this->getCacheKeyForQuery($this->query, $returnAsList, $returnAsArray); |
| 483 | 483 | $this->setCacheContentForQuery($this->query, $key, $this->result, $cacheExpire); |
| 484 | 484 | if (! $this->result){ |
| 485 | - $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 485 | + $this->logger->info('No result where found for the query [' . $query . ']'); |
|
| 486 | 486 | } |
| 487 | - } |
|
| 487 | + } |
|
| 488 | + } |
|
| 489 | + } else if ($isSqlSELECTQuery){ |
|
| 490 | + $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 491 | + $this->result = $cacheContent; |
|
| 492 | + $this->numRows = count($this->result); |
|
| 488 | 493 | } |
| 489 | - } else if ($isSqlSELECTQuery){ |
|
| 490 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 491 | - $this->result = $cacheContent; |
|
| 492 | - $this->numRows = count($this->result); |
|
| 493 | - } |
|
| 494 | - return $this->result; |
|
| 494 | + return $this->result; |
|
| 495 | 495 | } |
| 496 | 496 | |
| 497 | 497 | |
| 498 | - /** |
|
| 499 | - * Return the database configuration |
|
| 500 | - * @return array |
|
| 501 | - */ |
|
| 502 | - public function getDatabaseConfiguration(){ |
|
| 503 | - return $this->config; |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - /** |
|
| 507 | - * Setting the database configuration using the configuration file and additional configuration from param |
|
| 508 | - * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 509 | - * @param boolean $useConfigFile whether to use database configuration file |
|
| 510 | - * @return object Database |
|
| 511 | - */ |
|
| 498 | + /** |
|
| 499 | + * Return the database configuration |
|
| 500 | + * @return array |
|
| 501 | + */ |
|
| 502 | + public function getDatabaseConfiguration(){ |
|
| 503 | + return $this->config; |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + /** |
|
| 507 | + * Setting the database configuration using the configuration file and additional configuration from param |
|
| 508 | + * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 509 | + * @param boolean $useConfigFile whether to use database configuration file |
|
| 510 | + * @return object Database |
|
| 511 | + */ |
|
| 512 | 512 | public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
| 513 | 513 | $db = array(); |
| 514 | 514 | if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
@@ -521,50 +521,50 @@ discard block |
||
| 521 | 521 | |
| 522 | 522 | //default configuration |
| 523 | 523 | $config = array( |
| 524 | - 'driver' => 'mysql', |
|
| 525 | - 'username' => 'root', |
|
| 526 | - 'password' => '', |
|
| 527 | - 'database' => '', |
|
| 528 | - 'hostname' => 'localhost', |
|
| 529 | - 'charset' => 'utf8', |
|
| 530 | - 'collation' => 'utf8_general_ci', |
|
| 531 | - 'prefix' => '', |
|
| 532 | - 'port' => '' |
|
| 524 | + 'driver' => 'mysql', |
|
| 525 | + 'username' => 'root', |
|
| 526 | + 'password' => '', |
|
| 527 | + 'database' => '', |
|
| 528 | + 'hostname' => 'localhost', |
|
| 529 | + 'charset' => 'utf8', |
|
| 530 | + 'collation' => 'utf8_general_ci', |
|
| 531 | + 'prefix' => '', |
|
| 532 | + 'port' => '' |
|
| 533 | 533 | ); |
| 534 | 534 | |
| 535 | - $config = array_merge($config, $db); |
|
| 536 | - //determine the port using the hostname like localhost:3307 |
|
| 537 | - //hostname will be "localhost", and port "3307" |
|
| 538 | - $p = explode(':', $config['hostname']); |
|
| 539 | - if (count($p) >= 2){ |
|
| 540 | - $config['hostname'] = $p[0]; |
|
| 541 | - $config['port'] = $p[1]; |
|
| 542 | - } |
|
| 535 | + $config = array_merge($config, $db); |
|
| 536 | + //determine the port using the hostname like localhost:3307 |
|
| 537 | + //hostname will be "localhost", and port "3307" |
|
| 538 | + $p = explode(':', $config['hostname']); |
|
| 539 | + if (count($p) >= 2){ |
|
| 540 | + $config['hostname'] = $p[0]; |
|
| 541 | + $config['port'] = $p[1]; |
|
| 542 | + } |
|
| 543 | 543 | |
| 544 | - $this->databaseName = $config['database']; |
|
| 545 | - $this->config = $config; |
|
| 546 | - $this->logger->info( |
|
| 547 | - 'The database configuration are listed below: ' |
|
| 548 | - . stringfy_vars(array_merge( |
|
| 549 | - $this->config, |
|
| 550 | - array('password' => string_hidden($this->config['password'])) |
|
| 551 | - )) |
|
| 552 | - ); |
|
| 544 | + $this->databaseName = $config['database']; |
|
| 545 | + $this->config = $config; |
|
| 546 | + $this->logger->info( |
|
| 547 | + 'The database configuration are listed below: ' |
|
| 548 | + . stringfy_vars(array_merge( |
|
| 549 | + $this->config, |
|
| 550 | + array('password' => string_hidden($this->config['password'])) |
|
| 551 | + )) |
|
| 552 | + ); |
|
| 553 | 553 | |
| 554 | - //Now connect to the database |
|
| 555 | - $this->connect(); |
|
| 554 | + //Now connect to the database |
|
| 555 | + $this->connect(); |
|
| 556 | 556 | |
| 557 | - //do update of QueryRunner and Builder |
|
| 558 | - $this->updateQueryBuilderAndRunnerProperties(); |
|
| 557 | + //do update of QueryRunner and Builder |
|
| 558 | + $this->updateQueryBuilderAndRunnerProperties(); |
|
| 559 | 559 | |
| 560 | - return $this; |
|
| 560 | + return $this; |
|
| 561 | 561 | } |
| 562 | 562 | |
| 563 | 563 | /** |
| 564 | 564 | * Close the connexion |
| 565 | 565 | */ |
| 566 | 566 | public function close(){ |
| 567 | - $this->pdo = null; |
|
| 567 | + $this->pdo = null; |
|
| 568 | 568 | } |
| 569 | 569 | |
| 570 | 570 | /** |
@@ -572,18 +572,18 @@ discard block |
||
| 572 | 572 | * @return void |
| 573 | 573 | */ |
| 574 | 574 | protected function updateQueryBuilderAndRunnerProperties(){ |
| 575 | - //update queryBuilder with some properties needed |
|
| 576 | - if(is_object($this->queryBuilder)){ |
|
| 575 | + //update queryBuilder with some properties needed |
|
| 576 | + if(is_object($this->queryBuilder)){ |
|
| 577 | 577 | $this->queryBuilder->setDriver($this->config['driver']); |
| 578 | 578 | $this->queryBuilder->setPrefix($this->config['prefix']); |
| 579 | 579 | $this->queryBuilder->setPdo($this->pdo); |
| 580 | - } |
|
| 580 | + } |
|
| 581 | 581 | |
| 582 | - //update queryRunner with some properties needed |
|
| 583 | - if(is_object($this->queryRunner)){ |
|
| 582 | + //update queryRunner with some properties needed |
|
| 583 | + if(is_object($this->queryRunner)){ |
|
| 584 | 584 | $this->queryRunner->setDriver($this->config['driver']); |
| 585 | 585 | $this->queryRunner->setPdo($this->pdo); |
| 586 | - } |
|
| 586 | + } |
|
| 587 | 587 | } |
| 588 | 588 | |
| 589 | 589 | |
@@ -592,24 +592,24 @@ discard block |
||
| 592 | 592 | * @return string the DSN string |
| 593 | 593 | */ |
| 594 | 594 | protected function getDsnFromDriver(){ |
| 595 | - $config = $this->getDatabaseConfiguration(); |
|
| 596 | - if (! empty($config)){ |
|
| 595 | + $config = $this->getDatabaseConfiguration(); |
|
| 596 | + if (! empty($config)){ |
|
| 597 | 597 | $driver = $config['driver']; |
| 598 | 598 | $driverDsnMap = array( |
| 599 | - 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 600 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 601 | - . 'dbname=' . $config['database'], |
|
| 602 | - 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 603 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 604 | - . 'dbname=' . $config['database'], |
|
| 605 | - 'sqlite' => 'sqlite:' . $config['database'], |
|
| 606 | - 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 599 | + 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
| 600 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 601 | + . 'dbname=' . $config['database'], |
|
| 602 | + 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
| 603 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
| 604 | + . 'dbname=' . $config['database'], |
|
| 605 | + 'sqlite' => 'sqlite:' . $config['database'], |
|
| 606 | + 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
| 607 | 607 | . (($config['port']) != '' ? ':' . $config['port'] : '') |
| 608 | 608 | . '/' . $config['database'] |
| 609 | - ); |
|
| 609 | + ); |
|
| 610 | 610 | return isset($driverDsnMap[$driver]) ? $driverDsnMap[$driver] : ''; |
| 611 | - } |
|
| 612 | - return null; |
|
| 611 | + } |
|
| 612 | + return null; |
|
| 613 | 613 | } |
| 614 | 614 | |
| 615 | 615 | /** |
@@ -621,11 +621,11 @@ discard block |
||
| 621 | 621 | protected function getCacheContentForQuery($query, $returnAsList, $returnAsArray){ |
| 622 | 622 | $cacheKey = $this->getCacheKeyForQuery($query, $returnAsList, $returnAsArray); |
| 623 | 623 | if (! is_object($this->cacheInstance)){ |
| 624 | - //can not call method with reference in argument |
|
| 625 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
| 626 | - //use temporary variable |
|
| 627 | - $instance = & get_instance()->cache; |
|
| 628 | - $this->cacheInstance = $instance; |
|
| 624 | + //can not call method with reference in argument |
|
| 625 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
| 626 | + //use temporary variable |
|
| 627 | + $instance = & get_instance()->cache; |
|
| 628 | + $this->cacheInstance = $instance; |
|
| 629 | 629 | } |
| 630 | 630 | return $this->cacheInstance->get($cacheKey); |
| 631 | 631 | } |
@@ -637,87 +637,87 @@ discard block |
||
| 637 | 637 | * @param mixed $result the query result to save |
| 638 | 638 | * @param int $expire the cache TTL |
| 639 | 639 | */ |
| 640 | - protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
| 640 | + protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
| 641 | 641 | $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
| 642 | 642 | if (! is_object($this->cacheInstance)){ |
| 643 | - //can not call method with reference in argument |
|
| 644 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
| 645 | - //use temporary variable |
|
| 646 | - $instance = & get_instance()->cache; |
|
| 647 | - $this->cacheInstance = $instance; |
|
| 648 | - } |
|
| 643 | + //can not call method with reference in argument |
|
| 644 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
| 645 | + //use temporary variable |
|
| 646 | + $instance = & get_instance()->cache; |
|
| 647 | + $this->cacheInstance = $instance; |
|
| 648 | + } |
|
| 649 | 649 | $this->cacheInstance->set($key, $result, $expire); |
| 650 | - } |
|
| 650 | + } |
|
| 651 | 651 | |
| 652 | 652 | |
| 653 | - /** |
|
| 654 | - * Return the cache key for the given query |
|
| 655 | - * @see Database::query |
|
| 656 | - * |
|
| 657 | - * @return string |
|
| 658 | - */ |
|
| 653 | + /** |
|
| 654 | + * Return the cache key for the given query |
|
| 655 | + * @see Database::query |
|
| 656 | + * |
|
| 657 | + * @return string |
|
| 658 | + */ |
|
| 659 | 659 | protected function getCacheKeyForQuery($query, $returnAsList, $returnAsArray){ |
| 660 | - return md5($query . $returnAsList . $returnAsArray); |
|
| 660 | + return md5($query . $returnAsList . $returnAsArray); |
|
| 661 | 661 | } |
| 662 | 662 | |
| 663 | - /** |
|
| 664 | - * Set the Log instance using argument or create new instance |
|
| 665 | - * @param object $logger the Log instance if not null |
|
| 666 | - */ |
|
| 663 | + /** |
|
| 664 | + * Set the Log instance using argument or create new instance |
|
| 665 | + * @param object $logger the Log instance if not null |
|
| 666 | + */ |
|
| 667 | 667 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
| 668 | - if ($logger !== null){ |
|
| 668 | + if ($logger !== null){ |
|
| 669 | 669 | $this->logger = $logger; |
| 670 | - } |
|
| 671 | - else{ |
|
| 672 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 673 | - $this->logger->setLogger('Library::Database'); |
|
| 674 | - } |
|
| 670 | + } |
|
| 671 | + else{ |
|
| 672 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 673 | + $this->logger->setLogger('Library::Database'); |
|
| 674 | + } |
|
| 675 | 675 | } |
| 676 | 676 | |
| 677 | - /** |
|
| 678 | - * Set the DatabaseQueryBuilder instance using argument or create new instance |
|
| 679 | - * @param object $queryBuilder the DatabaseQueryBuilder instance if not null |
|
| 680 | - */ |
|
| 681 | - protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null){ |
|
| 682 | - if ($queryBuilder !== null){ |
|
| 677 | + /** |
|
| 678 | + * Set the DatabaseQueryBuilder instance using argument or create new instance |
|
| 679 | + * @param object $queryBuilder the DatabaseQueryBuilder instance if not null |
|
| 680 | + */ |
|
| 681 | + protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null){ |
|
| 682 | + if ($queryBuilder !== null){ |
|
| 683 | 683 | $this->queryBuilder = $queryBuilder; |
| 684 | - } |
|
| 685 | - else{ |
|
| 686 | - $this->queryBuilder =& class_loader('DatabaseQueryBuilder', 'classes/database'); |
|
| 687 | - } |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * Set the DatabaseQueryRunner instance using argument or create new instance |
|
| 692 | - * @param object $queryRunner the DatabaseQueryRunner instance if not null |
|
| 693 | - */ |
|
| 694 | - protected function setQueryRunnerFromParamOrCreateNewInstance(DatabaseQueryRunner $queryRunner = null){ |
|
| 684 | + } |
|
| 685 | + else{ |
|
| 686 | + $this->queryBuilder =& class_loader('DatabaseQueryBuilder', 'classes/database'); |
|
| 687 | + } |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * Set the DatabaseQueryRunner instance using argument or create new instance |
|
| 692 | + * @param object $queryRunner the DatabaseQueryRunner instance if not null |
|
| 693 | + */ |
|
| 694 | + protected function setQueryRunnerFromParamOrCreateNewInstance(DatabaseQueryRunner $queryRunner = null){ |
|
| 695 | 695 | if ($queryRunner !== null){ |
| 696 | 696 | $this->queryRunner = $queryRunner; |
| 697 | 697 | } |
| 698 | 698 | else{ |
| 699 | - $this->queryRunner =& class_loader('DatabaseQueryRunner', 'classes/database'); |
|
| 699 | + $this->queryRunner =& class_loader('DatabaseQueryRunner', 'classes/database'); |
|
| 700 | + } |
|
| 700 | 701 | } |
| 701 | - } |
|
| 702 | 702 | |
| 703 | 703 | /** |
| 704 | 704 | * Reset the database class attributs to the initail values before each query. |
| 705 | 705 | */ |
| 706 | 706 | private function reset(){ |
| 707 | - //query builder reset |
|
| 708 | - $this->getQueryBuilder()->reset(); |
|
| 709 | - $this->numRows = 0; |
|
| 710 | - $this->insertId = null; |
|
| 711 | - $this->query = null; |
|
| 712 | - $this->result = array(); |
|
| 713 | - $this->data = array(); |
|
| 707 | + //query builder reset |
|
| 708 | + $this->getQueryBuilder()->reset(); |
|
| 709 | + $this->numRows = 0; |
|
| 710 | + $this->insertId = null; |
|
| 711 | + $this->query = null; |
|
| 712 | + $this->result = array(); |
|
| 713 | + $this->data = array(); |
|
| 714 | 714 | } |
| 715 | 715 | |
| 716 | 716 | /** |
| 717 | 717 | * The class destructor |
| 718 | 718 | */ |
| 719 | 719 | public function __destruct(){ |
| 720 | - $this->pdo = null; |
|
| 720 | + $this->pdo = null; |
|
| 721 | 721 | } |
| 722 | 722 | |
| 723 | 723 | } |
@@ -1,154 +1,154 @@ |
||
| 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 | 27 | |
| 28 | - class Url{ |
|
| 28 | + class Url{ |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Return the link using base_url config without front controller "index.php" |
|
| 32 | - * @param string $path the link path or full URL |
|
| 33 | - * @return string the full link URL |
|
| 34 | - */ |
|
| 35 | - public static function base_url($path = ''){ |
|
| 36 | - if(is_url($path)){ |
|
| 37 | - return $path; |
|
| 38 | - } |
|
| 39 | - return get_config('base_url') . $path; |
|
| 40 | - } |
|
| 30 | + /** |
|
| 31 | + * Return the link using base_url config without front controller "index.php" |
|
| 32 | + * @param string $path the link path or full URL |
|
| 33 | + * @return string the full link URL |
|
| 34 | + */ |
|
| 35 | + public static function base_url($path = ''){ |
|
| 36 | + if(is_url($path)){ |
|
| 37 | + return $path; |
|
| 38 | + } |
|
| 39 | + return get_config('base_url') . $path; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Return the link using base_url config with front controller "index.php" |
|
| 44 | - * @param string $path the link path or full URL |
|
| 45 | - * @return string the full link URL |
|
| 46 | - */ |
|
| 47 | - public static function site_url($path = ''){ |
|
| 48 | - if(is_url($path)){ |
|
| 49 | - return $path; |
|
| 50 | - } |
|
| 51 | - $path = rtrim($path, '/'); |
|
| 52 | - $baseUrl = get_config('base_url'); |
|
| 53 | - $frontController = get_config('front_controller'); |
|
| 54 | - $url = $baseUrl; |
|
| 55 | - if($frontController){ |
|
| 56 | - $url .= $frontController . '/'; |
|
| 57 | - } |
|
| 58 | - if(($suffix = get_config('url_suffix')) && $path){ |
|
| 59 | - if(strpos($path, '?') !== false){ |
|
| 60 | - $query = explode('?', $path); |
|
| 61 | - $query[0] = str_ireplace($suffix, '', $query[0]); |
|
| 62 | - $query[0] = rtrim($query[0], '/'); |
|
| 63 | - $query[0] .= $suffix; |
|
| 64 | - $path = implode('?', $query); |
|
| 65 | - } |
|
| 66 | - else{ |
|
| 67 | - $path .= $suffix; |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - return $url . $path; |
|
| 71 | - } |
|
| 42 | + /** |
|
| 43 | + * Return the link using base_url config with front controller "index.php" |
|
| 44 | + * @param string $path the link path or full URL |
|
| 45 | + * @return string the full link URL |
|
| 46 | + */ |
|
| 47 | + public static function site_url($path = ''){ |
|
| 48 | + if(is_url($path)){ |
|
| 49 | + return $path; |
|
| 50 | + } |
|
| 51 | + $path = rtrim($path, '/'); |
|
| 52 | + $baseUrl = get_config('base_url'); |
|
| 53 | + $frontController = get_config('front_controller'); |
|
| 54 | + $url = $baseUrl; |
|
| 55 | + if($frontController){ |
|
| 56 | + $url .= $frontController . '/'; |
|
| 57 | + } |
|
| 58 | + if(($suffix = get_config('url_suffix')) && $path){ |
|
| 59 | + if(strpos($path, '?') !== false){ |
|
| 60 | + $query = explode('?', $path); |
|
| 61 | + $query[0] = str_ireplace($suffix, '', $query[0]); |
|
| 62 | + $query[0] = rtrim($query[0], '/'); |
|
| 63 | + $query[0] .= $suffix; |
|
| 64 | + $path = implode('?', $query); |
|
| 65 | + } |
|
| 66 | + else{ |
|
| 67 | + $path .= $suffix; |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + return $url . $path; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Return the current site URL |
|
| 75 | - * @return string |
|
| 76 | - */ |
|
| 77 | - public static function current(){ |
|
| 78 | - $current = '/'; |
|
| 79 | - $requestUri = get_instance()->request->requestUri(); |
|
| 80 | - if($requestUri){ |
|
| 81 | - $current = $requestUri; |
|
| 82 | - } |
|
| 83 | - return static::domain() . $current; |
|
| 84 | - } |
|
| 73 | + /** |
|
| 74 | + * Return the current site URL |
|
| 75 | + * @return string |
|
| 76 | + */ |
|
| 77 | + public static function current(){ |
|
| 78 | + $current = '/'; |
|
| 79 | + $requestUri = get_instance()->request->requestUri(); |
|
| 80 | + if($requestUri){ |
|
| 81 | + $current = $requestUri; |
|
| 82 | + } |
|
| 83 | + return static::domain() . $current; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Generate a friendly text to use in link (slugs) |
|
| 88 | - * @param string $str the title or text to use to get the friendly text |
|
| 89 | - * @param string $separator the caracters separator |
|
| 90 | - * @param boolean $lowercase whether to set the final text to lowe case or not |
|
| 91 | - * @return string the friendly generated text |
|
| 92 | - */ |
|
| 93 | - public static function title($str = null, $separator = '-', $lowercase = true){ |
|
| 94 | - $str = trim($str); |
|
| 95 | - $from = array('ç','À','Á','Â','Ã','Ä','Å','à','á','â','ã','ä','å','Ò','Ó','Ô','Õ','Ö','Ø','ò','ó','ô','õ','ö','ø','È','É','Ê','Ë','è','é','ê','ë','Ç','ç','Ì','Í','Î','Ï','ì','í','î','ï','Ù','Ú','Û','Ü','ù','ú','û','ü','ÿ','Ñ','ñ'); |
|
| 96 | - $to = array('c','a','a','a','a','a','a','a','a','a','a','a','a','o','o','o','o','o','o','o','o','o','o','o','o','e','e','e','e','e','e','e','e','e','e','i','i','i','i','i','i','i','i','u','u','u','u','u','u','u','u','y','n','n'); |
|
| 97 | - $str = str_replace($from, $to, $str); |
|
| 98 | - $str = preg_replace('#([^a-z0-9]+)#i', $separator, $str); |
|
| 99 | - $str = str_replace('--', $separator, $str); |
|
| 100 | - //if after process we get something like one-two-three-, need truncate the last separator "-" |
|
| 101 | - if(substr($str, -1) == $separator){ |
|
| 102 | - $str = substr($str, 0, -1); |
|
| 103 | - } |
|
| 104 | - if($lowercase){ |
|
| 105 | - $str = strtolower($str); |
|
| 106 | - } |
|
| 107 | - return $str; |
|
| 108 | - } |
|
| 86 | + /** |
|
| 87 | + * Generate a friendly text to use in link (slugs) |
|
| 88 | + * @param string $str the title or text to use to get the friendly text |
|
| 89 | + * @param string $separator the caracters separator |
|
| 90 | + * @param boolean $lowercase whether to set the final text to lowe case or not |
|
| 91 | + * @return string the friendly generated text |
|
| 92 | + */ |
|
| 93 | + public static function title($str = null, $separator = '-', $lowercase = true){ |
|
| 94 | + $str = trim($str); |
|
| 95 | + $from = array('ç','À','Á','Â','Ã','Ä','Å','à','á','â','ã','ä','å','Ò','Ó','Ô','Õ','Ö','Ø','ò','ó','ô','õ','ö','ø','È','É','Ê','Ë','è','é','ê','ë','Ç','ç','Ì','Í','Î','Ï','ì','í','î','ï','Ù','Ú','Û','Ü','ù','ú','û','ü','ÿ','Ñ','ñ'); |
|
| 96 | + $to = array('c','a','a','a','a','a','a','a','a','a','a','a','a','o','o','o','o','o','o','o','o','o','o','o','o','e','e','e','e','e','e','e','e','e','e','i','i','i','i','i','i','i','i','u','u','u','u','u','u','u','u','y','n','n'); |
|
| 97 | + $str = str_replace($from, $to, $str); |
|
| 98 | + $str = preg_replace('#([^a-z0-9]+)#i', $separator, $str); |
|
| 99 | + $str = str_replace('--', $separator, $str); |
|
| 100 | + //if after process we get something like one-two-three-, need truncate the last separator "-" |
|
| 101 | + if(substr($str, -1) == $separator){ |
|
| 102 | + $str = substr($str, 0, -1); |
|
| 103 | + } |
|
| 104 | + if($lowercase){ |
|
| 105 | + $str = strtolower($str); |
|
| 106 | + } |
|
| 107 | + return $str; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Get the current application domain with protocol |
|
| 112 | - * @return string the domain name |
|
| 113 | - */ |
|
| 114 | - public static function domain(){ |
|
| 115 | - $obj = & get_instance(); |
|
| 116 | - $domain = 'localhost'; |
|
| 117 | - $port = $obj->request->server('SERVER_PORT'); |
|
| 118 | - $protocol = 'http'; |
|
| 119 | - if(is_https()){ |
|
| 120 | - $protocol = 'https'; |
|
| 121 | - } |
|
| 110 | + /** |
|
| 111 | + * Get the current application domain with protocol |
|
| 112 | + * @return string the domain name |
|
| 113 | + */ |
|
| 114 | + public static function domain(){ |
|
| 115 | + $obj = & get_instance(); |
|
| 116 | + $domain = 'localhost'; |
|
| 117 | + $port = $obj->request->server('SERVER_PORT'); |
|
| 118 | + $protocol = 'http'; |
|
| 119 | + if(is_https()){ |
|
| 120 | + $protocol = 'https'; |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - $domainserverVars = array( |
|
| 124 | - 'HTTP_HOST', |
|
| 125 | - 'SERVER_NAME', |
|
| 126 | - 'SERVER_ADDR' |
|
| 127 | - ); |
|
| 123 | + $domainserverVars = array( |
|
| 124 | + 'HTTP_HOST', |
|
| 125 | + 'SERVER_NAME', |
|
| 126 | + 'SERVER_ADDR' |
|
| 127 | + ); |
|
| 128 | 128 | |
| 129 | - foreach ($domainserverVars as $var) { |
|
| 130 | - $value = $obj->request->server($var); |
|
| 131 | - if($value){ |
|
| 132 | - $domain = $value; |
|
| 133 | - break; |
|
| 134 | - } |
|
| 135 | - } |
|
| 129 | + foreach ($domainserverVars as $var) { |
|
| 130 | + $value = $obj->request->server($var); |
|
| 131 | + if($value){ |
|
| 132 | + $domain = $value; |
|
| 133 | + break; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - if($port && ((is_https() && $port != 443) || (!is_https() && $port != 80))){ |
|
| 138 | - //some server use SSL but the port doesn't equal 443 sometime is 80 if is the case put the port at this end |
|
| 139 | - //of the domain like https://my.domain.com:787 |
|
| 140 | - if(is_https() && $port != 80){ |
|
| 141 | - $domain .= ':'.$port; |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - return $protocol.'://'.$domain; |
|
| 145 | - } |
|
| 137 | + if($port && ((is_https() && $port != 443) || (!is_https() && $port != 80))){ |
|
| 138 | + //some server use SSL but the port doesn't equal 443 sometime is 80 if is the case put the port at this end |
|
| 139 | + //of the domain like https://my.domain.com:787 |
|
| 140 | + if(is_https() && $port != 80){ |
|
| 141 | + $domain .= ':'.$port; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + return $protocol.'://'.$domain; |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * Get the current request query string |
|
| 149 | - * @return string |
|
| 150 | - */ |
|
| 151 | - public static function queryString(){ |
|
| 152 | - return get_instance()->request->server('QUERY_STRING'); |
|
| 153 | - } |
|
| 154 | - } |
|
| 147 | + /** |
|
| 148 | + * Get the current request query string |
|
| 149 | + * @return string |
|
| 150 | + */ |
|
| 151 | + public static function queryString(){ |
|
| 152 | + return get_instance()->request->server('QUERY_STRING'); |
|
| 153 | + } |
|
| 154 | + } |
|
@@ -1,178 +1,178 @@ |
||
| 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 Config{ |
|
| 27 | + class Config{ |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The list of loaded configuration |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private static $config = array(); |
|
| 29 | + /** |
|
| 30 | + * The list of loaded configuration |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private static $config = array(); |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The logger instance |
|
| 37 | - * @var Log |
|
| 38 | - */ |
|
| 39 | - private static $logger; |
|
| 35 | + /** |
|
| 36 | + * The logger instance |
|
| 37 | + * @var Log |
|
| 38 | + */ |
|
| 39 | + private static $logger; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The signleton of the logger |
|
| 43 | - * @return Object the Log instance |
|
| 44 | - */ |
|
| 45 | - private static function getLogger(){ |
|
| 46 | - if(self::$logger == null){ |
|
| 47 | - $logger = array(); |
|
| 48 | - $logger[0] =& class_loader('Log', 'classes'); |
|
| 49 | - $logger[0]->setLogger('Library::Config'); |
|
| 50 | - self::$logger = $logger[0]; |
|
| 51 | - } |
|
| 52 | - return self::$logger; |
|
| 53 | - } |
|
| 41 | + /** |
|
| 42 | + * The signleton of the logger |
|
| 43 | + * @return Object the Log instance |
|
| 44 | + */ |
|
| 45 | + private static function getLogger(){ |
|
| 46 | + if(self::$logger == null){ |
|
| 47 | + $logger = array(); |
|
| 48 | + $logger[0] =& class_loader('Log', 'classes'); |
|
| 49 | + $logger[0]->setLogger('Library::Config'); |
|
| 50 | + self::$logger = $logger[0]; |
|
| 51 | + } |
|
| 52 | + return self::$logger; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Set the log instance for future use |
|
| 57 | - * @param Log $logger the log object |
|
| 58 | - * @return Log the log instance |
|
| 59 | - */ |
|
| 60 | - public static function setLogger($logger){ |
|
| 61 | - self::$logger = $logger; |
|
| 62 | - return self::$logger; |
|
| 63 | - } |
|
| 55 | + /** |
|
| 56 | + * Set the log instance for future use |
|
| 57 | + * @param Log $logger the log object |
|
| 58 | + * @return Log the log instance |
|
| 59 | + */ |
|
| 60 | + public static function setLogger($logger){ |
|
| 61 | + self::$logger = $logger; |
|
| 62 | + return self::$logger; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Initialize the configuration by loading all the configuration from config file |
|
| 67 | - */ |
|
| 68 | - public static function init(){ |
|
| 69 | - $logger = static::getLogger(); |
|
| 70 | - $logger->debug('Initialization of the configuration'); |
|
| 71 | - self::$config = & load_configurations(); |
|
| 72 | - self::setBaseUrlUsingServerVar(); |
|
| 73 | - if(ENVIRONMENT == 'production' && in_array(strtolower(self::$config['log_level']), array('debug', 'info','all'))){ |
|
| 74 | - $logger->warning('You are in production environment, please set log level to WARNING, ERROR, FATAL to increase the application performance'); |
|
| 75 | - } |
|
| 76 | - $logger->info('Configuration initialized successfully'); |
|
| 77 | - $logger->info('The application configuration are listed below: ' . stringfy_vars(self::$config)); |
|
| 78 | - } |
|
| 65 | + /** |
|
| 66 | + * Initialize the configuration by loading all the configuration from config file |
|
| 67 | + */ |
|
| 68 | + public static function init(){ |
|
| 69 | + $logger = static::getLogger(); |
|
| 70 | + $logger->debug('Initialization of the configuration'); |
|
| 71 | + self::$config = & load_configurations(); |
|
| 72 | + self::setBaseUrlUsingServerVar(); |
|
| 73 | + if(ENVIRONMENT == 'production' && in_array(strtolower(self::$config['log_level']), array('debug', 'info','all'))){ |
|
| 74 | + $logger->warning('You are in production environment, please set log level to WARNING, ERROR, FATAL to increase the application performance'); |
|
| 75 | + } |
|
| 76 | + $logger->info('Configuration initialized successfully'); |
|
| 77 | + $logger->info('The application configuration are listed below: ' . stringfy_vars(self::$config)); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Get the configuration item value |
|
| 82 | - * @param string $item the configuration item name to get |
|
| 83 | - * @param mixed $default the default value to use if can not find the config item in the list |
|
| 84 | - * @return mixed the config value if exist or the default value |
|
| 85 | - */ |
|
| 86 | - public static function get($item, $default = null){ |
|
| 87 | - $logger = static::getLogger(); |
|
| 88 | - if(array_key_exists($item, self::$config)){ |
|
| 89 | - return self::$config[$item]; |
|
| 90 | - } |
|
| 91 | - $logger->warning('Cannot find config item ['.$item.'] using the default value ['.$default.']'); |
|
| 92 | - return $default; |
|
| 93 | - } |
|
| 80 | + /** |
|
| 81 | + * Get the configuration item value |
|
| 82 | + * @param string $item the configuration item name to get |
|
| 83 | + * @param mixed $default the default value to use if can not find the config item in the list |
|
| 84 | + * @return mixed the config value if exist or the default value |
|
| 85 | + */ |
|
| 86 | + public static function get($item, $default = null){ |
|
| 87 | + $logger = static::getLogger(); |
|
| 88 | + if(array_key_exists($item, self::$config)){ |
|
| 89 | + return self::$config[$item]; |
|
| 90 | + } |
|
| 91 | + $logger->warning('Cannot find config item ['.$item.'] using the default value ['.$default.']'); |
|
| 92 | + return $default; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * Set the configuration item value |
|
| 97 | - * @param string $item the config item name to set |
|
| 98 | - * @param mixed $value the config item value |
|
| 99 | - */ |
|
| 100 | - public static function set($item, $value){ |
|
| 101 | - self::$config[$item] = $value; |
|
| 102 | - } |
|
| 95 | + /** |
|
| 96 | + * Set the configuration item value |
|
| 97 | + * @param string $item the config item name to set |
|
| 98 | + * @param mixed $value the config item value |
|
| 99 | + */ |
|
| 100 | + public static function set($item, $value){ |
|
| 101 | + self::$config[$item] = $value; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Get all the configuration values |
|
| 106 | - * @return array the config values |
|
| 107 | - */ |
|
| 108 | - public static function getAll(){ |
|
| 109 | - return self::$config; |
|
| 110 | - } |
|
| 104 | + /** |
|
| 105 | + * Get all the configuration values |
|
| 106 | + * @return array the config values |
|
| 107 | + */ |
|
| 108 | + public static function getAll(){ |
|
| 109 | + return self::$config; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Set the configuration values bu merged with the existing configuration |
|
| 114 | - * @param array $config the config values to add in the configuration list |
|
| 115 | - */ |
|
| 116 | - public static function setAll(array $config = array()){ |
|
| 117 | - self::$config = array_merge(self::$config, $config); |
|
| 118 | - } |
|
| 112 | + /** |
|
| 113 | + * Set the configuration values bu merged with the existing configuration |
|
| 114 | + * @param array $config the config values to add in the configuration list |
|
| 115 | + */ |
|
| 116 | + public static function setAll(array $config = array()){ |
|
| 117 | + self::$config = array_merge(self::$config, $config); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Delete the configuration item in the list |
|
| 122 | - * @param string $item the config item name to be deleted |
|
| 123 | - * @return boolean true if the item exists and is deleted successfully otherwise will return false. |
|
| 124 | - */ |
|
| 125 | - public static function delete($item){ |
|
| 126 | - $logger = static::getLogger(); |
|
| 127 | - if(array_key_exists($item, self::$config)){ |
|
| 128 | - $logger->info('Delete config item ['.$item.']'); |
|
| 129 | - unset(self::$config[$item]); |
|
| 130 | - return true; |
|
| 131 | - } |
|
| 132 | - else{ |
|
| 133 | - $logger->warning('Config item ['.$item.'] to be deleted does not exists'); |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - } |
|
| 120 | + /** |
|
| 121 | + * Delete the configuration item in the list |
|
| 122 | + * @param string $item the config item name to be deleted |
|
| 123 | + * @return boolean true if the item exists and is deleted successfully otherwise will return false. |
|
| 124 | + */ |
|
| 125 | + public static function delete($item){ |
|
| 126 | + $logger = static::getLogger(); |
|
| 127 | + if(array_key_exists($item, self::$config)){ |
|
| 128 | + $logger->info('Delete config item ['.$item.']'); |
|
| 129 | + unset(self::$config[$item]); |
|
| 130 | + return true; |
|
| 131 | + } |
|
| 132 | + else{ |
|
| 133 | + $logger->warning('Config item ['.$item.'] to be deleted does not exists'); |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * Load the configuration file. This an alias to Loader::config() |
|
| 140 | - * @param string $config the config name to be loaded |
|
| 141 | - */ |
|
| 142 | - public static function load($config){ |
|
| 143 | - Loader::config($config); |
|
| 144 | - } |
|
| 138 | + /** |
|
| 139 | + * Load the configuration file. This an alias to Loader::config() |
|
| 140 | + * @param string $config the config name to be loaded |
|
| 141 | + */ |
|
| 142 | + public static function load($config){ |
|
| 143 | + Loader::config($config); |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Set the configuration for "base_url" if is not set in the configuration |
|
| 148 | - */ |
|
| 149 | - private static function setBaseUrlUsingServerVar(){ |
|
| 150 | - $logger = static::getLogger(); |
|
| 151 | - if (! isset(self::$config['base_url']) || ! is_url(self::$config['base_url'])){ |
|
| 152 | - if(ENVIRONMENT == 'production'){ |
|
| 153 | - $logger->warning('Application base URL is not set or invalid, please set application base URL to increase the application loading time'); |
|
| 154 | - } |
|
| 155 | - $baseUrl = null; |
|
| 156 | - if (isset($_SERVER['SERVER_ADDR'])){ |
|
| 157 | - $baseUrl = $_SERVER['SERVER_ADDR']; |
|
| 158 | - //check if the server is running under IPv6 |
|
| 159 | - if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE){ |
|
| 160 | - $baseUrl = '['.$_SERVER['SERVER_ADDR'].']'; |
|
| 161 | - } |
|
| 162 | - $serverPort = 80; |
|
| 163 | - if (isset($_SERVER['SERVER_PORT'])) { |
|
| 164 | - $serverPort = $_SERVER['SERVER_PORT']; |
|
| 165 | - } |
|
| 166 | - $port = ((($serverPort != '80' && ! is_https()) || ($serverPort != '443' && is_https())) ? ':' . $serverPort : ''); |
|
| 167 | - $baseUrl = (is_https() ? 'https' : 'http').'://' . $baseUrl . $port |
|
| 168 | - . substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME']))); |
|
| 169 | - } |
|
| 170 | - else{ |
|
| 171 | - $logger->warning('Can not determine the application base URL automatically, use http://localhost as default'); |
|
| 172 | - $baseUrl = 'http://localhost/'; |
|
| 173 | - } |
|
| 174 | - self::set('base_url', $baseUrl); |
|
| 175 | - } |
|
| 176 | - self::$config['base_url'] = rtrim(self::$config['base_url'], '/') .'/'; |
|
| 177 | - } |
|
| 178 | - } |
|
| 146 | + /** |
|
| 147 | + * Set the configuration for "base_url" if is not set in the configuration |
|
| 148 | + */ |
|
| 149 | + private static function setBaseUrlUsingServerVar(){ |
|
| 150 | + $logger = static::getLogger(); |
|
| 151 | + if (! isset(self::$config['base_url']) || ! is_url(self::$config['base_url'])){ |
|
| 152 | + if(ENVIRONMENT == 'production'){ |
|
| 153 | + $logger->warning('Application base URL is not set or invalid, please set application base URL to increase the application loading time'); |
|
| 154 | + } |
|
| 155 | + $baseUrl = null; |
|
| 156 | + if (isset($_SERVER['SERVER_ADDR'])){ |
|
| 157 | + $baseUrl = $_SERVER['SERVER_ADDR']; |
|
| 158 | + //check if the server is running under IPv6 |
|
| 159 | + if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE){ |
|
| 160 | + $baseUrl = '['.$_SERVER['SERVER_ADDR'].']'; |
|
| 161 | + } |
|
| 162 | + $serverPort = 80; |
|
| 163 | + if (isset($_SERVER['SERVER_PORT'])) { |
|
| 164 | + $serverPort = $_SERVER['SERVER_PORT']; |
|
| 165 | + } |
|
| 166 | + $port = ((($serverPort != '80' && ! is_https()) || ($serverPort != '443' && is_https())) ? ':' . $serverPort : ''); |
|
| 167 | + $baseUrl = (is_https() ? 'https' : 'http').'://' . $baseUrl . $port |
|
| 168 | + . substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME']))); |
|
| 169 | + } |
|
| 170 | + else{ |
|
| 171 | + $logger->warning('Can not determine the application base URL automatically, use http://localhost as default'); |
|
| 172 | + $baseUrl = 'http://localhost/'; |
|
| 173 | + } |
|
| 174 | + self::set('base_url', $baseUrl); |
|
| 175 | + } |
|
| 176 | + self::$config['base_url'] = rtrim(self::$config['base_url'], '/') .'/'; |
|
| 177 | + } |
|
| 178 | + } |
|
@@ -1,75 +1,75 @@ |
||
| 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 | 27 | |
| 28 | - /** |
|
| 29 | - * @file function_user_agent.php |
|
| 30 | - * |
|
| 31 | - * Contains most of the utility functions for agent, platform, mobile, browser, and other management. |
|
| 32 | - * |
|
| 33 | - * @package core |
|
| 34 | - * @author Tony NGUEREZA |
|
| 35 | - * @copyright Copyright (c) 2017 |
|
| 36 | - * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
| 37 | - * @link http://www.iacademy.cf |
|
| 38 | - * @version 1.0.0 |
|
| 39 | - * @since 1.0.0 |
|
| 40 | - * @filesource |
|
| 41 | - */ |
|
| 28 | + /** |
|
| 29 | + * @file function_user_agent.php |
|
| 30 | + * |
|
| 31 | + * Contains most of the utility functions for agent, platform, mobile, browser, and other management. |
|
| 32 | + * |
|
| 33 | + * @package core |
|
| 34 | + * @author Tony NGUEREZA |
|
| 35 | + * @copyright Copyright (c) 2017 |
|
| 36 | + * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
| 37 | + * @link http://www.iacademy.cf |
|
| 38 | + * @version 1.0.0 |
|
| 39 | + * @since 1.0.0 |
|
| 40 | + * @filesource |
|
| 41 | + */ |
|
| 42 | 42 | |
| 43 | 43 | |
| 44 | - if(! function_exists('get_ip')){ |
|
| 45 | - /** |
|
| 46 | - * Retrieves the user's IP address |
|
| 47 | - * |
|
| 48 | - * This function allows to retrieve the IP address of the client |
|
| 49 | - * even if it uses a proxy, the actual IP address is retrieved. |
|
| 50 | - * |
|
| 51 | - * @return string the IP address. |
|
| 52 | - */ |
|
| 53 | - function get_ip(){ |
|
| 54 | - $ip = '127.0.0.1'; |
|
| 55 | - $ipServerVars = array( |
|
| 56 | - 'REMOTE_ADDR', |
|
| 57 | - 'HTTP_CLIENT_IP', |
|
| 58 | - 'HTTP_X_FORWARDED_FOR', |
|
| 59 | - 'HTTP_X_FORWARDED', |
|
| 60 | - 'HTTP_FORWARDED_FOR', |
|
| 61 | - 'HTTP_FORWARDED' |
|
| 62 | - ); |
|
| 63 | - foreach ($ipServerVars as $var) { |
|
| 64 | - if(isset($_SERVER[$var])){ |
|
| 65 | - $ip = $_SERVER[$var]; |
|
| 66 | - break; |
|
| 67 | - } |
|
| 68 | - } |
|
| 69 | - // Strip any secondary IP etc from the IP address |
|
| 70 | - if (strpos($ip, ',') > 0) { |
|
| 71 | - $ip = substr($ip, 0, strpos($ip, ',')); |
|
| 72 | - } |
|
| 73 | - return $ip; |
|
| 74 | - } |
|
| 75 | - } |
|
| 44 | + if(! function_exists('get_ip')){ |
|
| 45 | + /** |
|
| 46 | + * Retrieves the user's IP address |
|
| 47 | + * |
|
| 48 | + * This function allows to retrieve the IP address of the client |
|
| 49 | + * even if it uses a proxy, the actual IP address is retrieved. |
|
| 50 | + * |
|
| 51 | + * @return string the IP address. |
|
| 52 | + */ |
|
| 53 | + function get_ip(){ |
|
| 54 | + $ip = '127.0.0.1'; |
|
| 55 | + $ipServerVars = array( |
|
| 56 | + 'REMOTE_ADDR', |
|
| 57 | + 'HTTP_CLIENT_IP', |
|
| 58 | + 'HTTP_X_FORWARDED_FOR', |
|
| 59 | + 'HTTP_X_FORWARDED', |
|
| 60 | + 'HTTP_FORWARDED_FOR', |
|
| 61 | + 'HTTP_FORWARDED' |
|
| 62 | + ); |
|
| 63 | + foreach ($ipServerVars as $var) { |
|
| 64 | + if(isset($_SERVER[$var])){ |
|
| 65 | + $ip = $_SERVER[$var]; |
|
| 66 | + break; |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | + // Strip any secondary IP etc from the IP address |
|
| 70 | + if (strpos($ip, ',') > 0) { |
|
| 71 | + $ip = substr($ip, 0, strpos($ip, ',')); |
|
| 72 | + } |
|
| 73 | + return $ip; |
|
| 74 | + } |
|
| 75 | + } |
|
@@ -1,168 +1,168 @@ |
||
| 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 | - * @file Assets.php |
|
| 29 | - * |
|
| 30 | - * This class contains static methods for generating static content links (images, Javascript, CSS, etc.). |
|
| 31 | - * |
|
| 32 | - * @package core |
|
| 33 | - * @author Tony NGUEREZA |
|
| 34 | - * @copyright Copyright (c) 2017 |
|
| 35 | - * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
| 36 | - * @link http://www.iacademy.cf |
|
| 37 | - * @version 1.0.0 |
|
| 38 | - * @since 1.0.0 |
|
| 39 | - * @filesource |
|
| 40 | - */ |
|
| 41 | - class Assets{ |
|
| 27 | + /** |
|
| 28 | + * @file Assets.php |
|
| 29 | + * |
|
| 30 | + * This class contains static methods for generating static content links (images, Javascript, CSS, etc.). |
|
| 31 | + * |
|
| 32 | + * @package core |
|
| 33 | + * @author Tony NGUEREZA |
|
| 34 | + * @copyright Copyright (c) 2017 |
|
| 35 | + * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
| 36 | + * @link http://www.iacademy.cf |
|
| 37 | + * @version 1.0.0 |
|
| 38 | + * @since 1.0.0 |
|
| 39 | + * @filesource |
|
| 40 | + */ |
|
| 41 | + class Assets{ |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * The logger instance |
|
| 45 | - * @var object |
|
| 46 | - */ |
|
| 47 | - private static $logger; |
|
| 43 | + /** |
|
| 44 | + * The logger instance |
|
| 45 | + * @var object |
|
| 46 | + */ |
|
| 47 | + private static $logger; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * The signleton of the logger |
|
| 51 | - * @return Object the Log instance |
|
| 52 | - */ |
|
| 53 | - private static function getLogger(){ |
|
| 54 | - if(self::$logger == null){ |
|
| 55 | - //can't assign reference to static variable |
|
| 56 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 57 | - self::$logger[0]->setLogger('Library::Assets'); |
|
| 58 | - } |
|
| 59 | - return self::$logger[0]; |
|
| 60 | - } |
|
| 49 | + /** |
|
| 50 | + * The signleton of the logger |
|
| 51 | + * @return Object the Log instance |
|
| 52 | + */ |
|
| 53 | + private static function getLogger(){ |
|
| 54 | + if(self::$logger == null){ |
|
| 55 | + //can't assign reference to static variable |
|
| 56 | + self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 57 | + self::$logger[0]->setLogger('Library::Assets'); |
|
| 58 | + } |
|
| 59 | + return self::$logger[0]; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Generate the link of the assets file. |
|
| 65 | - * |
|
| 66 | - * Generates the absolute link of a file inside ASSETS_PATH folder. |
|
| 67 | - * For example : |
|
| 68 | - * echo Assets::path('foo/bar/css/style.css'); => http://mysite.com/assets/foo/bar/css/style.css |
|
| 69 | - * Note: |
|
| 70 | - * The argument passed to this function must be the relative link to the folder that contains the static contents defined by the constant ASSETS_PATH. |
|
| 71 | - * |
|
| 72 | - * @param string $asset the name of the assets file path with the extension. |
|
| 73 | - * @return string|null the absolute path of the assets file, if it exists otherwise returns null if the file does not exist. |
|
| 74 | - */ |
|
| 75 | - public static function path($asset){ |
|
| 76 | - $logger = self::getLogger(); |
|
| 77 | - $path = ASSETS_PATH . $asset; |
|
| 63 | + /** |
|
| 64 | + * Generate the link of the assets file. |
|
| 65 | + * |
|
| 66 | + * Generates the absolute link of a file inside ASSETS_PATH folder. |
|
| 67 | + * For example : |
|
| 68 | + * echo Assets::path('foo/bar/css/style.css'); => http://mysite.com/assets/foo/bar/css/style.css |
|
| 69 | + * Note: |
|
| 70 | + * The argument passed to this function must be the relative link to the folder that contains the static contents defined by the constant ASSETS_PATH. |
|
| 71 | + * |
|
| 72 | + * @param string $asset the name of the assets file path with the extension. |
|
| 73 | + * @return string|null the absolute path of the assets file, if it exists otherwise returns null if the file does not exist. |
|
| 74 | + */ |
|
| 75 | + public static function path($asset){ |
|
| 76 | + $logger = self::getLogger(); |
|
| 77 | + $path = ASSETS_PATH . $asset; |
|
| 78 | 78 | |
| 79 | - $logger->debug('Including the Assets file [' . $path . ']'); |
|
| 80 | - //Check if the file exists |
|
| 81 | - if(file_exists($path)){ |
|
| 82 | - $logger->info('Assets file [' . $path . '] included successfully'); |
|
| 83 | - return Url::base_url($path); |
|
| 84 | - } |
|
| 85 | - $logger->warning('Assets file [' . $path . '] does not exist'); |
|
| 86 | - return null; |
|
| 87 | - } |
|
| 79 | + $logger->debug('Including the Assets file [' . $path . ']'); |
|
| 80 | + //Check if the file exists |
|
| 81 | + if(file_exists($path)){ |
|
| 82 | + $logger->info('Assets file [' . $path . '] included successfully'); |
|
| 83 | + return Url::base_url($path); |
|
| 84 | + } |
|
| 85 | + $logger->warning('Assets file [' . $path . '] does not exist'); |
|
| 86 | + return null; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Generate the link of the css file. |
|
| 91 | - * |
|
| 92 | - * Generates the absolute link of a file containing the CSS style. |
|
| 93 | - * For example : |
|
| 94 | - * echo Assets::css('mystyle'); => http://mysite.com/assets/css/mystyle.css |
|
| 95 | - * Note: |
|
| 96 | - * The argument passed to this function must be the relative link to the folder that contains the static contents defined by the constant ASSETS_PATH. |
|
| 97 | - * |
|
| 98 | - * @param string $path the name of the css file without the extension. |
|
| 99 | - * @return string|null the absolute path of the css file, if it exists otherwise returns null if the file does not exist. |
|
| 100 | - */ |
|
| 101 | - public static function css($path){ |
|
| 102 | - $logger = self::getLogger(); |
|
| 103 | - /* |
|
| 89 | + /** |
|
| 90 | + * Generate the link of the css file. |
|
| 91 | + * |
|
| 92 | + * Generates the absolute link of a file containing the CSS style. |
|
| 93 | + * For example : |
|
| 94 | + * echo Assets::css('mystyle'); => http://mysite.com/assets/css/mystyle.css |
|
| 95 | + * Note: |
|
| 96 | + * The argument passed to this function must be the relative link to the folder that contains the static contents defined by the constant ASSETS_PATH. |
|
| 97 | + * |
|
| 98 | + * @param string $path the name of the css file without the extension. |
|
| 99 | + * @return string|null the absolute path of the css file, if it exists otherwise returns null if the file does not exist. |
|
| 100 | + */ |
|
| 101 | + public static function css($path){ |
|
| 102 | + $logger = self::getLogger(); |
|
| 103 | + /* |
|
| 104 | 104 | * if the file name contains the ".css" extension, replace it with |
| 105 | 105 | * an empty string for better processing. |
| 106 | 106 | */ |
| 107 | - $path = str_ireplace('.css', '', $path); |
|
| 108 | - $path = ASSETS_PATH . 'css/' . $path . '.css'; |
|
| 107 | + $path = str_ireplace('.css', '', $path); |
|
| 108 | + $path = ASSETS_PATH . 'css/' . $path . '.css'; |
|
| 109 | 109 | |
| 110 | - $logger->debug('Including the Assets file [' . $path . '] for CSS'); |
|
| 111 | - //Check if the file exists |
|
| 112 | - if(file_exists($path)){ |
|
| 113 | - $logger->info('Assets file [' . $path . '] for CSS included successfully'); |
|
| 114 | - return Url::base_url($path); |
|
| 115 | - } |
|
| 116 | - $logger->warning('Assets file [' . $path . '] for CSS does not exist'); |
|
| 117 | - return null; |
|
| 118 | - } |
|
| 110 | + $logger->debug('Including the Assets file [' . $path . '] for CSS'); |
|
| 111 | + //Check if the file exists |
|
| 112 | + if(file_exists($path)){ |
|
| 113 | + $logger->info('Assets file [' . $path . '] for CSS included successfully'); |
|
| 114 | + return Url::base_url($path); |
|
| 115 | + } |
|
| 116 | + $logger->warning('Assets file [' . $path . '] for CSS does not exist'); |
|
| 117 | + return null; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Generate the link of the javascript file. |
|
| 122 | - * |
|
| 123 | - * Generates the absolute link of a file containing the javascript. |
|
| 124 | - * For example : |
|
| 125 | - * echo Assets::js('myscript'); => http://mysite.com/assets/js/myscript.js |
|
| 126 | - * Note: |
|
| 127 | - * The argument passed to this function must be the relative link to the folder that contains the static contents defined by the constant ASSETS_PATH. |
|
| 128 | - * |
|
| 129 | - * @param string $path the name of the javascript file without the extension. |
|
| 130 | - * @return string|null the absolute path of the javascript file, if it exists otherwise returns null if the file does not exist. |
|
| 131 | - */ |
|
| 132 | - public static function js($path){ |
|
| 133 | - $logger = self::getLogger(); |
|
| 134 | - $path = str_ireplace('.js', '', $path); |
|
| 135 | - $path = ASSETS_PATH . 'js/' . $path . '.js'; |
|
| 136 | - $logger->debug('Including the Assets file [' . $path . '] for javascript'); |
|
| 137 | - if(file_exists($path)){ |
|
| 138 | - $logger->info('Assets file [' . $path . '] for Javascript included successfully'); |
|
| 139 | - return Url::base_url($path); |
|
| 140 | - } |
|
| 141 | - $logger->warning('Assets file [' . $path . '] for Javascript does not exist'); |
|
| 142 | - return null; |
|
| 143 | - } |
|
| 120 | + /** |
|
| 121 | + * Generate the link of the javascript file. |
|
| 122 | + * |
|
| 123 | + * Generates the absolute link of a file containing the javascript. |
|
| 124 | + * For example : |
|
| 125 | + * echo Assets::js('myscript'); => http://mysite.com/assets/js/myscript.js |
|
| 126 | + * Note: |
|
| 127 | + * The argument passed to this function must be the relative link to the folder that contains the static contents defined by the constant ASSETS_PATH. |
|
| 128 | + * |
|
| 129 | + * @param string $path the name of the javascript file without the extension. |
|
| 130 | + * @return string|null the absolute path of the javascript file, if it exists otherwise returns null if the file does not exist. |
|
| 131 | + */ |
|
| 132 | + public static function js($path){ |
|
| 133 | + $logger = self::getLogger(); |
|
| 134 | + $path = str_ireplace('.js', '', $path); |
|
| 135 | + $path = ASSETS_PATH . 'js/' . $path . '.js'; |
|
| 136 | + $logger->debug('Including the Assets file [' . $path . '] for javascript'); |
|
| 137 | + if(file_exists($path)){ |
|
| 138 | + $logger->info('Assets file [' . $path . '] for Javascript included successfully'); |
|
| 139 | + return Url::base_url($path); |
|
| 140 | + } |
|
| 141 | + $logger->warning('Assets file [' . $path . '] for Javascript does not exist'); |
|
| 142 | + return null; |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * Generate the link of the image file. |
|
| 147 | - * |
|
| 148 | - * Generates the absolute link of a file containing the image. |
|
| 149 | - * For example : |
|
| 150 | - * echo Assets::img('myimage.png'); => http://mysite.com/assets/images/myimage.png |
|
| 151 | - * Note: |
|
| 152 | - * The argument passed to this function must be the relative link to the folder that contains the static contents defined by the constant ASSETS_PATH. |
|
| 153 | - * |
|
| 154 | - * @param string $path the name of the image file with the extension. |
|
| 155 | - * @return string|null the absolute path of the image file, if it exists otherwise returns null if the file does not exist. |
|
| 156 | - */ |
|
| 157 | - public static function img($path){ |
|
| 158 | - $logger = self::getLogger(); |
|
| 159 | - $path = ASSETS_PATH . 'images/' . $path; |
|
| 160 | - $logger->debug('Including the Assets file [' . $path . '] for image'); |
|
| 161 | - if(file_exists($path)){ |
|
| 162 | - $logger->info('Assets file [' . $path . '] for image included successfully'); |
|
| 163 | - return Url::base_url($path); |
|
| 164 | - } |
|
| 165 | - $logger->warning('Assets file [' . $path . '] for image does not exist'); |
|
| 166 | - return null; |
|
| 167 | - } |
|
| 168 | - } |
|
| 145 | + /** |
|
| 146 | + * Generate the link of the image file. |
|
| 147 | + * |
|
| 148 | + * Generates the absolute link of a file containing the image. |
|
| 149 | + * For example : |
|
| 150 | + * echo Assets::img('myimage.png'); => http://mysite.com/assets/images/myimage.png |
|
| 151 | + * Note: |
|
| 152 | + * The argument passed to this function must be the relative link to the folder that contains the static contents defined by the constant ASSETS_PATH. |
|
| 153 | + * |
|
| 154 | + * @param string $path the name of the image file with the extension. |
|
| 155 | + * @return string|null the absolute path of the image file, if it exists otherwise returns null if the file does not exist. |
|
| 156 | + */ |
|
| 157 | + public static function img($path){ |
|
| 158 | + $logger = self::getLogger(); |
|
| 159 | + $path = ASSETS_PATH . 'images/' . $path; |
|
| 160 | + $logger->debug('Including the Assets file [' . $path . '] for image'); |
|
| 161 | + if(file_exists($path)){ |
|
| 162 | + $logger->info('Assets file [' . $path . '] for image included successfully'); |
|
| 163 | + return Url::base_url($path); |
|
| 164 | + } |
|
| 165 | + $logger->warning('Assets file [' . $path . '] for image does not exist'); |
|
| 166 | + return null; |
|
| 167 | + } |
|
| 168 | + } |
|