@@ -1,267 +1,267 @@ |
||
| 1 | 1 | <?php |
| 2 | - /** |
|
| 3 | - * TNH Framework |
|
| 4 | - * |
|
| 5 | - * A simple PHP framework using HMVC architecture |
|
| 6 | - * |
|
| 7 | - * This content is released under the GNU GPL License (GPL) |
|
| 8 | - * |
|
| 9 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 10 | - * |
|
| 11 | - * This program is free software; you can redistribute it and/or |
|
| 12 | - * modify it under the terms of the GNU General Public License |
|
| 13 | - * as published by the Free Software Foundation; either version 3 |
|
| 14 | - * of the License, or (at your option) any later version. |
|
| 15 | - * |
|
| 16 | - * This program is distributed in the hope that it will be useful, |
|
| 17 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 18 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 19 | - * GNU General Public License for more details. |
|
| 20 | - * |
|
| 21 | - * You should have received a copy of the GNU General Public License |
|
| 22 | - * along with this program; if not, write to the Free Software |
|
| 23 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 24 | - */ |
|
| 2 | + /** |
|
| 3 | + * TNH Framework |
|
| 4 | + * |
|
| 5 | + * A simple PHP framework using HMVC architecture |
|
| 6 | + * |
|
| 7 | + * This content is released under the GNU GPL License (GPL) |
|
| 8 | + * |
|
| 9 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 10 | + * |
|
| 11 | + * This program is free software; you can redistribute it and/or |
|
| 12 | + * modify it under the terms of the GNU General Public License |
|
| 13 | + * as published by the Free Software Foundation; either version 3 |
|
| 14 | + * of the License, or (at your option) any later version. |
|
| 15 | + * |
|
| 16 | + * This program is distributed in the hope that it will be useful, |
|
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 19 | + * GNU General Public License for more details. |
|
| 20 | + * |
|
| 21 | + * You should have received a copy of the GNU General Public License |
|
| 22 | + * along with this program; if not, write to the Free Software |
|
| 23 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 24 | + */ |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * the directory separator, under windows it is \ and unix, linux / |
|
| 28 | - */ |
|
| 29 | - define('DS', DIRECTORY_SEPARATOR); |
|
| 26 | + /** |
|
| 27 | + * the directory separator, under windows it is \ and unix, linux / |
|
| 28 | + */ |
|
| 29 | + define('DS', DIRECTORY_SEPARATOR); |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * The root directory of the application. |
|
| 33 | - * |
|
| 34 | - * you can place this directory outside of your web directory, for example "/home/your_app", etc. |
|
| 35 | - */ |
|
| 36 | - define('ROOT_PATH', dirname(realpath(__FILE__)) . DS); |
|
| 31 | + /** |
|
| 32 | + * The root directory of the application. |
|
| 33 | + * |
|
| 34 | + * you can place this directory outside of your web directory, for example "/home/your_app", etc. |
|
| 35 | + */ |
|
| 36 | + define('ROOT_PATH', dirname(realpath(__FILE__)) . DS); |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * The path to the directory. |
|
| 40 | - * |
|
| 41 | - * That contains your static files (javascript, css, images, etc.) |
|
| 42 | - * Note: the path must be relative to the file index.php (the front-end controller). |
|
| 43 | - */ |
|
| 44 | - define('ASSETS_PATH', 'assets/'); |
|
| 38 | + /** |
|
| 39 | + * The path to the directory. |
|
| 40 | + * |
|
| 41 | + * That contains your static files (javascript, css, images, etc.) |
|
| 42 | + * Note: the path must be relative to the file index.php (the front-end controller). |
|
| 43 | + */ |
|
| 44 | + define('ASSETS_PATH', 'assets/'); |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * The path to the directory of your cache files. |
|
| 48 | - * |
|
| 49 | - * This feature is available currently for database and views. |
|
| 50 | - */ |
|
| 51 | - define('CACHE_PATH', ROOT_PATH . 'cache' . DS); |
|
| 46 | + /** |
|
| 47 | + * The path to the directory of your cache files. |
|
| 48 | + * |
|
| 49 | + * This feature is available currently for database and views. |
|
| 50 | + */ |
|
| 51 | + define('CACHE_PATH', ROOT_PATH . 'cache' . DS); |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * The path to the application directory. |
|
| 55 | - * |
|
| 56 | - * It contains your most often used files that is to say which contains your files of the application, |
|
| 57 | - * in MVC architecture (controllers, models, views). |
|
| 58 | - */ |
|
| 59 | - define('APPS_PATH', ROOT_PATH . 'classes' . DS); |
|
| 53 | + /** |
|
| 54 | + * The path to the application directory. |
|
| 55 | + * |
|
| 56 | + * It contains your most often used files that is to say which contains your files of the application, |
|
| 57 | + * in MVC architecture (controllers, models, views). |
|
| 58 | + */ |
|
| 59 | + define('APPS_PATH', ROOT_PATH . 'classes' . DS); |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * The path to the controller directory of your application. |
|
| 63 | - * |
|
| 64 | - * If you already know the MVC architecture you know what a controller means; |
|
| 65 | - * it is he who makes the business logic of your application in general. |
|
| 66 | - */ |
|
| 67 | - define('APPS_CONTROLLER_PATH', APPS_PATH . 'controllers' . DS); |
|
| 61 | + /** |
|
| 62 | + * The path to the controller directory of your application. |
|
| 63 | + * |
|
| 64 | + * If you already know the MVC architecture you know what a controller means; |
|
| 65 | + * it is he who makes the business logic of your application in general. |
|
| 66 | + */ |
|
| 67 | + define('APPS_CONTROLLER_PATH', APPS_PATH . 'controllers' . DS); |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * The path to the directory of your model classes of your application. |
|
| 71 | - * |
|
| 72 | - * If you already know the MVC architecture you know what a model means; |
|
| 73 | - * it's the one who interacts with the database, in one word persistent data from your application. |
|
| 74 | - */ |
|
| 75 | - define('APPS_MODEL_PATH', APPS_PATH . 'models' . DS); |
|
| 69 | + /** |
|
| 70 | + * The path to the directory of your model classes of your application. |
|
| 71 | + * |
|
| 72 | + * If you already know the MVC architecture you know what a model means; |
|
| 73 | + * it's the one who interacts with the database, in one word persistent data from your application. |
|
| 74 | + */ |
|
| 75 | + define('APPS_MODEL_PATH', APPS_PATH . 'models' . DS); |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * The path to the directory of your views. |
|
| 79 | - * |
|
| 80 | - * If you already know the MVC architecture you know what a view means, |
|
| 81 | - * a view is just a user interface (html page, form, etc.) that is to say |
|
| 82 | - * everything displayed in the browser interface, etc. |
|
| 83 | - */ |
|
| 84 | - define('APPS_VIEWS_PATH', APPS_PATH . 'views' . DS); |
|
| 77 | + /** |
|
| 78 | + * The path to the directory of your views. |
|
| 79 | + * |
|
| 80 | + * If you already know the MVC architecture you know what a view means, |
|
| 81 | + * a view is just a user interface (html page, form, etc.) that is to say |
|
| 82 | + * everything displayed in the browser interface, etc. |
|
| 83 | + */ |
|
| 84 | + define('APPS_VIEWS_PATH', APPS_PATH . 'views' . DS); |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * The path to the configuration directory. |
|
| 88 | - * |
|
| 89 | - * That contains most of the configuration files for your |
|
| 90 | - * application (database, class loading file, functions, etc.) |
|
| 91 | - */ |
|
| 92 | - define('CONFIG_PATH', ROOT_PATH . 'config' . DS); |
|
| 86 | + /** |
|
| 87 | + * The path to the configuration directory. |
|
| 88 | + * |
|
| 89 | + * That contains most of the configuration files for your |
|
| 90 | + * application (database, class loading file, functions, etc.) |
|
| 91 | + */ |
|
| 92 | + define('CONFIG_PATH', ROOT_PATH . 'config' . DS); |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * The core directory |
|
| 96 | - * |
|
| 97 | - * It is recommended to put this folder out of the web directory of your server and |
|
| 98 | - * you should not change its content because in case of update you could lose the modified files. |
|
| 99 | - */ |
|
| 100 | - define('CORE_PATH', ROOT_PATH . 'core' . DS); |
|
| 94 | + /** |
|
| 95 | + * The core directory |
|
| 96 | + * |
|
| 97 | + * It is recommended to put this folder out of the web directory of your server and |
|
| 98 | + * you should not change its content because in case of update you could lose the modified files. |
|
| 99 | + */ |
|
| 100 | + define('CORE_PATH', ROOT_PATH . 'core' . DS); |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * The path to the directory of core classes that used by the system. |
|
| 104 | - * |
|
| 105 | - * It contains PHP classes that are used by the framework internally. |
|
| 106 | - */ |
|
| 107 | - define('CORE_CLASSES_PATH', CORE_PATH . 'classes' . DS); |
|
| 102 | + /** |
|
| 103 | + * The path to the directory of core classes that used by the system. |
|
| 104 | + * |
|
| 105 | + * It contains PHP classes that are used by the framework internally. |
|
| 106 | + */ |
|
| 107 | + define('CORE_CLASSES_PATH', CORE_PATH . 'classes' . DS); |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * The path to the directory of core classes for the cache used by the system. |
|
| 111 | - * |
|
| 112 | - * It contains PHP classes for the cache drivers. |
|
| 113 | - */ |
|
| 114 | - define('CORE_CLASSES_CACHE_PATH', CORE_CLASSES_PATH . 'cache' . DS); |
|
| 109 | + /** |
|
| 110 | + * The path to the directory of core classes for the cache used by the system. |
|
| 111 | + * |
|
| 112 | + * It contains PHP classes for the cache drivers. |
|
| 113 | + */ |
|
| 114 | + define('CORE_CLASSES_CACHE_PATH', CORE_CLASSES_PATH . 'cache' . DS); |
|
| 115 | 115 | |
| 116 | 116 | /** |
| 117 | - * The path to the directory of core classes for the database used by the system. |
|
| 118 | - * |
|
| 119 | - * It contains PHP classes for the database library, drivers, etc. |
|
| 120 | - */ |
|
| 121 | - define('CORE_CLASSES_DATABASE_PATH', CORE_CLASSES_PATH . 'database' . DS); |
|
| 117 | + * The path to the directory of core classes for the database used by the system. |
|
| 118 | + * |
|
| 119 | + * It contains PHP classes for the database library, drivers, etc. |
|
| 120 | + */ |
|
| 121 | + define('CORE_CLASSES_DATABASE_PATH', CORE_CLASSES_PATH . 'database' . DS); |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * The path to the directory of core classes for the model used by the system. |
|
| 125 | - * |
|
| 126 | - * It contains PHP classes for the models. |
|
| 127 | - */ |
|
| 128 | - define('CORE_CLASSES_MODEL_PATH', CORE_CLASSES_PATH . 'model' . DS); |
|
| 123 | + /** |
|
| 124 | + * The path to the directory of core classes for the model used by the system. |
|
| 125 | + * |
|
| 126 | + * It contains PHP classes for the models. |
|
| 127 | + */ |
|
| 128 | + define('CORE_CLASSES_MODEL_PATH', CORE_CLASSES_PATH . 'model' . DS); |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * The path to the directory of functions or helper systems. |
|
| 132 | - * |
|
| 133 | - * It contains PHP functions that perform a particular task: character string processing, URL, etc. |
|
| 134 | - */ |
|
| 135 | - define('CORE_FUNCTIONS_PATH', CORE_PATH . 'functions' . DS); |
|
| 130 | + /** |
|
| 131 | + * The path to the directory of functions or helper systems. |
|
| 132 | + * |
|
| 133 | + * It contains PHP functions that perform a particular task: character string processing, URL, etc. |
|
| 134 | + */ |
|
| 135 | + define('CORE_FUNCTIONS_PATH', CORE_PATH . 'functions' . DS); |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * The path to the core directory of languages files. |
|
| 139 | - * |
|
| 140 | - */ |
|
| 141 | - define('CORE_LANG_PATH', CORE_PATH . 'lang' . DS); |
|
| 137 | + /** |
|
| 138 | + * The path to the core directory of languages files. |
|
| 139 | + * |
|
| 140 | + */ |
|
| 141 | + define('CORE_LANG_PATH', CORE_PATH . 'lang' . DS); |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * The path to the system library directory. |
|
| 145 | - * |
|
| 146 | - * Which contains the libraries most often used in your web application, as for the |
|
| 147 | - * core directory it is advisable to put it out of the root directory of your application. |
|
| 148 | - */ |
|
| 149 | - define('CORE_LIBRARY_PATH', CORE_PATH . 'libraries' . DS); |
|
| 143 | + /** |
|
| 144 | + * The path to the system library directory. |
|
| 145 | + * |
|
| 146 | + * Which contains the libraries most often used in your web application, as for the |
|
| 147 | + * core directory it is advisable to put it out of the root directory of your application. |
|
| 148 | + */ |
|
| 149 | + define('CORE_LIBRARY_PATH', CORE_PATH . 'libraries' . DS); |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * The path to the system view directory. |
|
| 153 | - * |
|
| 154 | - * That contains the views used for the system, such as error messages, and so on. |
|
| 155 | - */ |
|
| 156 | - define('CORE_VIEWS_PATH', CORE_PATH . 'views' . DS); |
|
| 151 | + /** |
|
| 152 | + * The path to the system view directory. |
|
| 153 | + * |
|
| 154 | + * That contains the views used for the system, such as error messages, and so on. |
|
| 155 | + */ |
|
| 156 | + define('CORE_VIEWS_PATH', CORE_PATH . 'views' . DS); |
|
| 157 | 157 | |
| 158 | - /** |
|
| 159 | - * The path to the directory of your PHP personal functions or helper. |
|
| 160 | - * |
|
| 161 | - * It contains your PHP functions that perform a particular task: utilities, etc. |
|
| 162 | - * Note: Do not put your personal functions or helpers in the system functions directory, |
|
| 163 | - * because if you update the system you may lose them. |
|
| 164 | - */ |
|
| 165 | - define('FUNCTIONS_PATH', ROOT_PATH . 'functions' . DS); |
|
| 158 | + /** |
|
| 159 | + * The path to the directory of your PHP personal functions or helper. |
|
| 160 | + * |
|
| 161 | + * It contains your PHP functions that perform a particular task: utilities, etc. |
|
| 162 | + * Note: Do not put your personal functions or helpers in the system functions directory, |
|
| 163 | + * because if you update the system you may lose them. |
|
| 164 | + */ |
|
| 165 | + define('FUNCTIONS_PATH', ROOT_PATH . 'functions' . DS); |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * The path to the app directory of personal language. |
|
| 169 | - * |
|
| 170 | - * This feature is not yet available. |
|
| 171 | - * You can help us do this if you are nice or wish to see the developed framework. |
|
| 172 | - */ |
|
| 173 | - define('APP_LANG_PATH', ROOT_PATH . 'lang' . DS); |
|
| 167 | + /** |
|
| 168 | + * The path to the app directory of personal language. |
|
| 169 | + * |
|
| 170 | + * This feature is not yet available. |
|
| 171 | + * You can help us do this if you are nice or wish to see the developed framework. |
|
| 172 | + */ |
|
| 173 | + define('APP_LANG_PATH', ROOT_PATH . 'lang' . DS); |
|
| 174 | 174 | |
| 175 | - /** |
|
| 176 | - * The path to the directory of your personal libraries |
|
| 177 | - * |
|
| 178 | - * It contains your PHP classes, package, etc. |
|
| 179 | - * Note: you should not put your personal libraries in the system library directory, |
|
| 180 | - * because it is recalled in case of updating the system you might have surprises. |
|
| 181 | - */ |
|
| 182 | - define('LIBRARY_PATH', ROOT_PATH . 'libraries' . DS); |
|
| 175 | + /** |
|
| 176 | + * The path to the directory of your personal libraries |
|
| 177 | + * |
|
| 178 | + * It contains your PHP classes, package, etc. |
|
| 179 | + * Note: you should not put your personal libraries in the system library directory, |
|
| 180 | + * because it is recalled in case of updating the system you might have surprises. |
|
| 181 | + */ |
|
| 182 | + define('LIBRARY_PATH', ROOT_PATH . 'libraries' . DS); |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * The path to the directory that contains the log files. |
|
| 186 | - * |
|
| 187 | - * Note: This directory must be available in writing and if possible must have as owner the user who launches your web server, |
|
| 188 | - * under unix or linux most often with the apache web server it is "www-data" or "httpd" even "nobody" for more |
|
| 189 | - * details see the documentation of your web server. |
|
| 190 | - * Example for Unix or linux with apache web server: |
|
| 191 | - * # chmod -R 700 /path/to/your/logs/directory/ |
|
| 192 | - * # chown -R www-data:www-data /path/to/your/logs/directory/ |
|
| 193 | - */ |
|
| 194 | - define('LOGS_PATH', ROOT_PATH . 'logs' . DS); |
|
| 184 | + /** |
|
| 185 | + * The path to the directory that contains the log files. |
|
| 186 | + * |
|
| 187 | + * Note: This directory must be available in writing and if possible must have as owner the user who launches your web server, |
|
| 188 | + * under unix or linux most often with the apache web server it is "www-data" or "httpd" even "nobody" for more |
|
| 189 | + * details see the documentation of your web server. |
|
| 190 | + * Example for Unix or linux with apache web server: |
|
| 191 | + * # chmod -R 700 /path/to/your/logs/directory/ |
|
| 192 | + * # chown -R www-data:www-data /path/to/your/logs/directory/ |
|
| 193 | + */ |
|
| 194 | + define('LOGS_PATH', ROOT_PATH . 'logs' . DS); |
|
| 195 | 195 | |
| 196 | - /** |
|
| 197 | - * The path to the modules directory. |
|
| 198 | - * |
|
| 199 | - * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
|
| 200 | - * in HMVC architecture (hierichical, controllers, models, views). |
|
| 201 | - */ |
|
| 202 | - define('MODULE_PATH', ROOT_PATH . 'modules' . DS); |
|
| 196 | + /** |
|
| 197 | + * The path to the modules directory. |
|
| 198 | + * |
|
| 199 | + * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
|
| 200 | + * in HMVC architecture (hierichical, controllers, models, views). |
|
| 201 | + */ |
|
| 202 | + define('MODULE_PATH', ROOT_PATH . 'modules' . DS); |
|
| 203 | 203 | |
| 204 | - /** |
|
| 205 | - * The path to the directory of sources external to your application. |
|
| 206 | - * |
|
| 207 | - * If you have already used "composer" you know what that means. |
|
| 208 | - */ |
|
| 209 | - define('VENDOR_PATH', ROOT_PATH . 'vendor' . DS); |
|
| 204 | + /** |
|
| 205 | + * The path to the directory of sources external to your application. |
|
| 206 | + * |
|
| 207 | + * If you have already used "composer" you know what that means. |
|
| 208 | + */ |
|
| 209 | + define('VENDOR_PATH', ROOT_PATH . 'vendor' . DS); |
|
| 210 | 210 | |
| 211 | - /** |
|
| 212 | - * The front controller of your application. |
|
| 213 | - * |
|
| 214 | - * "index.php" it is through this file that all the requests come, there is a possibility to hidden it in the url of |
|
| 215 | - * your application by using the rewrite module URL of your web server . |
|
| 216 | - * For example, under apache web server, there is a configuration example file that is located at the root |
|
| 217 | - * of your framework folder : "htaccess.txt" rename it to ".htaccess". |
|
| 218 | - */ |
|
| 219 | - define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME)); |
|
| 211 | + /** |
|
| 212 | + * The front controller of your application. |
|
| 213 | + * |
|
| 214 | + * "index.php" it is through this file that all the requests come, there is a possibility to hidden it in the url of |
|
| 215 | + * your application by using the rewrite module URL of your web server . |
|
| 216 | + * For example, under apache web server, there is a configuration example file that is located at the root |
|
| 217 | + * of your framework folder : "htaccess.txt" rename it to ".htaccess". |
|
| 218 | + */ |
|
| 219 | + define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME)); |
|
| 220 | 220 | |
| 221 | - /** |
|
| 222 | - * Check if user run the application under CLI |
|
| 223 | - */ |
|
| 224 | - define('IS_CLI', stripos('cli', php_sapi_name()) !== false); |
|
| 221 | + /** |
|
| 222 | + * Check if user run the application under CLI |
|
| 223 | + */ |
|
| 224 | + define('IS_CLI', stripos('cli', php_sapi_name()) !== false); |
|
| 225 | 225 | |
| 226 | - /** |
|
| 227 | - * The environment of your application (production, test, development). |
|
| 228 | - * |
|
| 229 | - * if your application is still in development you use the value "development" |
|
| 230 | - * so you will have the display of the error messages, etc. |
|
| 231 | - * Once you finish the development of your application that is to put it online |
|
| 232 | - * you change this value to "production" or "testing", in this case there will be deactivation of error messages, |
|
| 233 | - * the loading of the system, will be fast. |
|
| 234 | - */ |
|
| 235 | - define('ENVIRONMENT', 'development'); |
|
| 226 | + /** |
|
| 227 | + * The environment of your application (production, test, development). |
|
| 228 | + * |
|
| 229 | + * if your application is still in development you use the value "development" |
|
| 230 | + * so you will have the display of the error messages, etc. |
|
| 231 | + * Once you finish the development of your application that is to put it online |
|
| 232 | + * you change this value to "production" or "testing", in this case there will be deactivation of error messages, |
|
| 233 | + * the loading of the system, will be fast. |
|
| 234 | + */ |
|
| 235 | + define('ENVIRONMENT', 'development'); |
|
| 236 | 236 | |
| 237 | - /* ---------------------------------------------------------------------------------- */ |
|
| 238 | - /////////////////////////////////////////////////////////////////////////////////////// |
|
| 239 | - /******************** DO NOT CHANGE THE LINES BELOW *********************************/ |
|
| 240 | - ///////////////////////////////////////////////////////////////////////////////////// |
|
| 237 | + /* ---------------------------------------------------------------------------------- */ |
|
| 238 | + /////////////////////////////////////////////////////////////////////////////////////// |
|
| 239 | + /******************** DO NOT CHANGE THE LINES BELOW *********************************/ |
|
| 240 | + ///////////////////////////////////////////////////////////////////////////////////// |
|
| 241 | 241 | |
| 242 | - switch (ENVIRONMENT) { |
|
| 243 | - case 'development': |
|
| 244 | - error_reporting(-1); |
|
| 245 | - ini_set('display_errors', 1); |
|
| 246 | - break; |
|
| 247 | - case 'testing': |
|
| 248 | - case 'production': |
|
| 249 | - ini_set('display_errors', 0); |
|
| 250 | - if (version_compare(PHP_VERSION, '5.3', '>=')) { |
|
| 251 | - error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED); |
|
| 252 | - } |
|
| 253 | - else{ |
|
| 254 | - error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE); |
|
| 255 | - } |
|
| 256 | - break; |
|
| 257 | - default: |
|
| 258 | - header('HTTP/1.1 503 Service Unavailable.', TRUE, 503); |
|
| 259 | - echo 'The application environment is not set correctly.'; |
|
| 260 | - exit(1); |
|
| 261 | - } |
|
| 242 | + switch (ENVIRONMENT) { |
|
| 243 | + case 'development': |
|
| 244 | + error_reporting(-1); |
|
| 245 | + ini_set('display_errors', 1); |
|
| 246 | + break; |
|
| 247 | + case 'testing': |
|
| 248 | + case 'production': |
|
| 249 | + ini_set('display_errors', 0); |
|
| 250 | + if (version_compare(PHP_VERSION, '5.3', '>=')) { |
|
| 251 | + error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED); |
|
| 252 | + } |
|
| 253 | + else{ |
|
| 254 | + error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE); |
|
| 255 | + } |
|
| 256 | + break; |
|
| 257 | + default: |
|
| 258 | + header('HTTP/1.1 503 Service Unavailable.', TRUE, 503); |
|
| 259 | + echo 'The application environment is not set correctly.'; |
|
| 260 | + exit(1); |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - /** |
|
| 264 | - * let's go. |
|
| 265 | - * Everything is OK now we launch our application. |
|
| 266 | - */ |
|
| 267 | - require_once CORE_PATH . 'bootstrap.php'; |
|
| 268 | 263 | \ No newline at end of file |
| 264 | + /** |
|
| 265 | + * let's go. |
|
| 266 | + * Everything is OK now we launch our application. |
|
| 267 | + */ |
|
| 268 | + require_once CORE_PATH . 'bootstrap.php'; |
|
| 269 | 269 | \ No newline at end of file |
@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | /** |
| 4 | 4 | * Function to test private & protected method |
| 5 | 5 | */ |
| 6 | - function run_private_protected_method($object, $method, array $args = array()){ |
|
| 6 | + function run_private_protected_method($object, $method, array $args = array()) { |
|
| 7 | 7 | $r = new ReflectionClass(get_class($object)); |
| 8 | 8 | $m = $r->getMethod($method); |
| 9 | 9 | $m->setAccessible(true); |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | /** |
| 14 | 14 | * Function to return the correct database configuration |
| 15 | 15 | */ |
| 16 | - function get_db_config(){ |
|
| 16 | + function get_db_config() { |
|
| 17 | 17 | return array( |
| 18 | 18 | 'driver' => 'sqlite', |
| 19 | 19 | 'database' => TESTS_PATH . 'assets' . DS . 'db_tests.db', |
@@ -1,52 +1,52 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - use PHPUnit\Framework\TestCase; |
|
| 3 | + use PHPUnit\Framework\TestCase; |
|
| 4 | 4 | |
| 5 | - class DatabaseTest extends TestCase |
|
| 6 | - { |
|
| 5 | + class DatabaseTest extends TestCase |
|
| 6 | + { |
|
| 7 | 7 | |
| 8 | - public static function setUpBeforeClass() |
|
| 9 | - { |
|
| 8 | + public static function setUpBeforeClass() |
|
| 9 | + { |
|
| 10 | 10 | |
| 11 | - } |
|
| 11 | + } |
|
| 12 | 12 | |
| 13 | - public static function tearDownAfterClass() |
|
| 14 | - { |
|
| 13 | + public static function tearDownAfterClass() |
|
| 14 | + { |
|
| 15 | 15 | |
| 16 | - } |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - protected function setUp() |
|
| 19 | - { |
|
| 20 | - } |
|
| 18 | + protected function setUp() |
|
| 19 | + { |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - protected function tearDown() |
|
| 23 | - { |
|
| 24 | - } |
|
| 22 | + protected function tearDown() |
|
| 23 | + { |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public function testConnectToDatabaseSuccessfully() |
|
| 27 | - { |
|
| 26 | + public function testConnectToDatabaseSuccessfully() |
|
| 27 | + { |
|
| 28 | 28 | $cfg = get_db_config(); |
| 29 | 29 | $db = new Database($cfg, false); |
| 30 | 30 | $isConnected = $db->connect(); |
| 31 | 31 | $this->assertTrue($isConnected); |
| 32 | - } |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | 34 | public function testCannotConnectToDatabase() |
| 35 | - { |
|
| 36 | - $db = new Database(array( |
|
| 37 | - 'driver' => '', |
|
| 38 | - 'username' => '', |
|
| 39 | - 'password' => '', |
|
| 40 | - 'database' => '', |
|
| 41 | - 'hostname' => '', |
|
| 42 | - 'charset' => '', |
|
| 43 | - 'collation' => '', |
|
| 44 | - 'prefix' => '', |
|
| 45 | - 'port' => '' |
|
| 35 | + { |
|
| 36 | + $db = new Database(array( |
|
| 37 | + 'driver' => '', |
|
| 38 | + 'username' => '', |
|
| 39 | + 'password' => '', |
|
| 40 | + 'database' => '', |
|
| 41 | + 'hostname' => '', |
|
| 42 | + 'charset' => '', |
|
| 43 | + 'collation' => '', |
|
| 44 | + 'prefix' => '', |
|
| 45 | + 'port' => '' |
|
| 46 | 46 | ), |
| 47 | 47 | false); |
| 48 | - $isConnected = $db->connect(); |
|
| 49 | - $this->assertFalse($isConnected); |
|
| 50 | - } |
|
| 48 | + $isConnected = $db->connect(); |
|
| 49 | + $this->assertFalse($isConnected); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - } |
|
| 53 | 52 | \ No newline at end of file |
| 53 | + } |
|
| 54 | 54 | \ No newline at end of file |
@@ -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 | |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | //Set Log instance to use |
| 127 | 127 | $this->setLoggerFromParamOrCreate(null); |
| 128 | 128 | |
| 129 | - //Set DatabaseQueryBuilder instance to use |
|
| 129 | + //Set DatabaseQueryBuilder instance to use |
|
| 130 | 130 | $this->setDependencyInstanceFromParamOrCreate('queryBuilder', null, 'DatabaseQueryBuilder', 'classes/database'); |
| 131 | 131 | |
| 132 | 132 | //Set DatabaseQueryRunner instance to use |
@@ -144,8 +144,8 @@ discard block |
||
| 144 | 144 | * @return bool |
| 145 | 145 | */ |
| 146 | 146 | public function connect(){ |
| 147 | - $config = $this->getDatabaseConfiguration(); |
|
| 148 | - if (! empty($config)){ |
|
| 147 | + $config = $this->getDatabaseConfiguration(); |
|
| 148 | + if (! empty($config)){ |
|
| 149 | 149 | try{ |
| 150 | 150 | $this->pdo = new PDO($this->getDsnValueFromConfig(), $config['username'], $config['password']); |
| 151 | 151 | $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
@@ -156,14 +156,14 @@ discard block |
||
| 156 | 156 | $this->updateQueryBuilderAndRunnerProperties(); |
| 157 | 157 | |
| 158 | 158 | return is_object($this->pdo); |
| 159 | - } |
|
| 160 | - catch (PDOException $e){ |
|
| 159 | + } |
|
| 160 | + catch (PDOException $e){ |
|
| 161 | 161 | $this->logger->fatal($e->getMessage()); |
| 162 | 162 | show_error('Cannot connect to Database.'); |
| 163 | 163 | return false; |
| 164 | - } |
|
| 165 | - } |
|
| 166 | - return false; |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + return false; |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | * @return int |
| 173 | 173 | */ |
| 174 | 174 | public function numRows(){ |
| 175 | - return $this->numRows; |
|
| 175 | + return $this->numRows; |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | /** |
@@ -180,7 +180,7 @@ discard block |
||
| 180 | 180 | * @return mixed |
| 181 | 181 | */ |
| 182 | 182 | public function insertId(){ |
| 183 | - return $this->insertId; |
|
| 183 | + return $this->insertId; |
|
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | |
@@ -191,13 +191,13 @@ discard block |
||
| 191 | 191 | * @return mixed the query SQL string or the record result |
| 192 | 192 | */ |
| 193 | 193 | public function get($returnSQLQueryOrResultType = false){ |
| 194 | - $this->queryBuilder->limit(1); |
|
| 195 | - $query = $this->getAll(true); |
|
| 196 | - if ($returnSQLQueryOrResultType === true){ |
|
| 194 | + $this->queryBuilder->limit(1); |
|
| 195 | + $query = $this->getAll(true); |
|
| 196 | + if ($returnSQLQueryOrResultType === true){ |
|
| 197 | 197 | return $query; |
| 198 | - } else { |
|
| 198 | + } else { |
|
| 199 | 199 | return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
| 200 | - } |
|
| 200 | + } |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | /** |
@@ -207,11 +207,11 @@ discard block |
||
| 207 | 207 | * @return mixed the query SQL string or the record result |
| 208 | 208 | */ |
| 209 | 209 | public function getAll($returnSQLQueryOrResultType = false){ |
| 210 | - $query = $this->queryBuilder->getQuery(); |
|
| 211 | - if ($returnSQLQueryOrResultType === true){ |
|
| 212 | - return $query; |
|
| 213 | - } |
|
| 214 | - return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
| 210 | + $query = $this->queryBuilder->getQuery(); |
|
| 211 | + if ($returnSQLQueryOrResultType === true){ |
|
| 212 | + return $query; |
|
| 213 | + } |
|
| 214 | + return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | /** |
@@ -221,19 +221,19 @@ discard block |
||
| 221 | 221 | * @return mixed the insert id of the new record or null |
| 222 | 222 | */ |
| 223 | 223 | public function insert($data = array(), $escape = true){ |
| 224 | - if (empty($data) && ! empty($this->data)){ |
|
| 224 | + if (empty($data) && ! empty($this->data)){ |
|
| 225 | 225 | //as when using $this->setData() may be the data already escaped |
| 226 | 226 | $escape = false; |
| 227 | 227 | $data = $this->data; |
| 228 | - } |
|
| 229 | - $query = $this->queryBuilder->insert($data, $escape)->getQuery(); |
|
| 230 | - $result = $this->query($query); |
|
| 231 | - if ($result){ |
|
| 228 | + } |
|
| 229 | + $query = $this->queryBuilder->insert($data, $escape)->getQuery(); |
|
| 230 | + $result = $this->query($query); |
|
| 231 | + if ($result){ |
|
| 232 | 232 | $this->insertId = $this->pdo->lastInsertId(); |
| 233 | - //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
| 233 | + //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
| 234 | 234 | return ! ($this->insertId) ? true : $this->insertId; |
| 235 | - } |
|
| 236 | - return false; |
|
| 235 | + } |
|
| 236 | + return false; |
|
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | /** |
@@ -243,13 +243,13 @@ discard block |
||
| 243 | 243 | * @return mixed the update status |
| 244 | 244 | */ |
| 245 | 245 | public function update($data = array(), $escape = true){ |
| 246 | - if (empty($data) && ! empty($this->data)){ |
|
| 246 | + if (empty($data) && ! empty($this->data)){ |
|
| 247 | 247 | //as when using $this->setData() may be the data already escaped |
| 248 | 248 | $escape = false; |
| 249 | 249 | $data = $this->data; |
| 250 | - } |
|
| 251 | - $query = $this->queryBuilder->update($data, $escape)->getQuery(); |
|
| 252 | - return $this->query($query); |
|
| 250 | + } |
|
| 251 | + $query = $this->queryBuilder->update($data, $escape)->getQuery(); |
|
| 252 | + return $this->query($query); |
|
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | /** |
@@ -257,8 +257,8 @@ discard block |
||
| 257 | 257 | * @return mixed the delete status |
| 258 | 258 | */ |
| 259 | 259 | public function delete(){ |
| 260 | - $query = $this->queryBuilder->delete()->getQuery(); |
|
| 261 | - return $this->query($query); |
|
| 260 | + $query = $this->queryBuilder->delete()->getQuery(); |
|
| 261 | + return $this->query($query); |
|
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | /** |
@@ -267,17 +267,17 @@ discard block |
||
| 267 | 267 | * @return object the current Database instance |
| 268 | 268 | */ |
| 269 | 269 | public function setCache($ttl = 0){ |
| 270 | - $this->cacheTtl = $ttl; |
|
| 271 | - $this->temporaryCacheTtl = $ttl; |
|
| 272 | - return $this; |
|
| 270 | + $this->cacheTtl = $ttl; |
|
| 271 | + $this->temporaryCacheTtl = $ttl; |
|
| 272 | + return $this; |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | - /** |
|
| 276 | - * Enabled cache temporary for the current query not globally |
|
| 277 | - * @param integer $ttl the cache time to live in second |
|
| 278 | - * @return object the current Database instance |
|
| 279 | - */ |
|
| 280 | - public function cached($ttl = 0){ |
|
| 275 | + /** |
|
| 276 | + * Enabled cache temporary for the current query not globally |
|
| 277 | + * @param integer $ttl the cache time to live in second |
|
| 278 | + * @return object the current Database instance |
|
| 279 | + */ |
|
| 280 | + public function cached($ttl = 0){ |
|
| 281 | 281 | $this->temporaryCacheTtl = $ttl; |
| 282 | 282 | return $this; |
| 283 | 283 | } |
@@ -290,11 +290,11 @@ discard block |
||
| 290 | 290 | * need escaped |
| 291 | 291 | */ |
| 292 | 292 | public function escape($data, $escaped = true){ |
| 293 | - $data = trim($data); |
|
| 294 | - if($escaped){ |
|
| 293 | + $data = trim($data); |
|
| 294 | + if($escaped){ |
|
| 295 | 295 | return $this->pdo->quote($data); |
| 296 | - } |
|
| 297 | - return $data; |
|
| 296 | + } |
|
| 297 | + return $data; |
|
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | /** |
@@ -302,7 +302,7 @@ discard block |
||
| 302 | 302 | * @return int |
| 303 | 303 | */ |
| 304 | 304 | public function queryCount(){ |
| 305 | - return $this->queryCount; |
|
| 305 | + return $this->queryCount; |
|
| 306 | 306 | } |
| 307 | 307 | |
| 308 | 308 | /** |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | * @return string |
| 311 | 311 | */ |
| 312 | 312 | public function getQuery(){ |
| 313 | - return $this->query; |
|
| 313 | + return $this->query; |
|
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | /** |
@@ -318,7 +318,7 @@ discard block |
||
| 318 | 318 | * @return string |
| 319 | 319 | */ |
| 320 | 320 | public function getDatabaseName(){ |
| 321 | - return $this->databaseName; |
|
| 321 | + return $this->databaseName; |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | /** |
@@ -326,17 +326,17 @@ discard block |
||
| 326 | 326 | * @return object |
| 327 | 327 | */ |
| 328 | 328 | public function getPdo(){ |
| 329 | - return $this->pdo; |
|
| 329 | + return $this->pdo; |
|
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | /** |
| 333 | 333 | * Set the PDO instance |
| 334 | 334 | * @param object $pdo the pdo object |
| 335 | - * @return object Database |
|
| 335 | + * @return object Database |
|
| 336 | 336 | */ |
| 337 | 337 | public function setPdo(PDO $pdo){ |
| 338 | - $this->pdo = $pdo; |
|
| 339 | - return $this; |
|
| 338 | + $this->pdo = $pdo; |
|
| 339 | + return $this; |
|
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | |
@@ -345,44 +345,44 @@ discard block |
||
| 345 | 345 | * @return Log |
| 346 | 346 | */ |
| 347 | 347 | public function getLogger(){ |
| 348 | - return $this->logger; |
|
| 348 | + return $this->logger; |
|
| 349 | 349 | } |
| 350 | 350 | |
| 351 | 351 | /** |
| 352 | 352 | * Set the log instance |
| 353 | 353 | * @param Log $logger the log object |
| 354 | - * @return object Database |
|
| 354 | + * @return object Database |
|
| 355 | 355 | */ |
| 356 | 356 | public function setLogger($logger){ |
| 357 | - $this->logger = $logger; |
|
| 358 | - return $this; |
|
| 357 | + $this->logger = $logger; |
|
| 358 | + return $this; |
|
| 359 | 359 | } |
| 360 | 360 | |
| 361 | - /** |
|
| 362 | - * Return the cache instance |
|
| 363 | - * @return CacheInterface |
|
| 364 | - */ |
|
| 361 | + /** |
|
| 362 | + * Return the cache instance |
|
| 363 | + * @return CacheInterface |
|
| 364 | + */ |
|
| 365 | 365 | public function getCacheInstance(){ |
| 366 | - return $this->cacheInstance; |
|
| 366 | + return $this->cacheInstance; |
|
| 367 | 367 | } |
| 368 | 368 | |
| 369 | 369 | /** |
| 370 | 370 | * Set the cache instance |
| 371 | 371 | * @param CacheInterface $cache the cache object |
| 372 | - * @return object Database |
|
| 372 | + * @return object Database |
|
| 373 | 373 | */ |
| 374 | 374 | public function setCacheInstance($cache){ |
| 375 | - $this->cacheInstance = $cache; |
|
| 376 | - return $this; |
|
| 375 | + $this->cacheInstance = $cache; |
|
| 376 | + return $this; |
|
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | |
| 380 | - /** |
|
| 381 | - * Return the DatabaseQueryBuilder instance |
|
| 382 | - * @return object DatabaseQueryBuilder |
|
| 383 | - */ |
|
| 380 | + /** |
|
| 381 | + * Return the DatabaseQueryBuilder instance |
|
| 382 | + * @return object DatabaseQueryBuilder |
|
| 383 | + */ |
|
| 384 | 384 | public function getQueryBuilder(){ |
| 385 | - return $this->queryBuilder; |
|
| 385 | + return $this->queryBuilder; |
|
| 386 | 386 | } |
| 387 | 387 | |
| 388 | 388 | /** |
@@ -390,8 +390,8 @@ discard block |
||
| 390 | 390 | * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
| 391 | 391 | */ |
| 392 | 392 | public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder){ |
| 393 | - $this->queryBuilder = $queryBuilder; |
|
| 394 | - return $this; |
|
| 393 | + $this->queryBuilder = $queryBuilder; |
|
| 394 | + return $this; |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | /** |
@@ -399,7 +399,7 @@ discard block |
||
| 399 | 399 | * @return object DatabaseQueryRunner |
| 400 | 400 | */ |
| 401 | 401 | public function getQueryRunner(){ |
| 402 | - return $this->queryRunner; |
|
| 402 | + return $this->queryRunner; |
|
| 403 | 403 | } |
| 404 | 404 | |
| 405 | 405 | /** |
@@ -407,8 +407,8 @@ discard block |
||
| 407 | 407 | * @param object DatabaseQueryRunner $queryRunner the DatabaseQueryRunner object |
| 408 | 408 | */ |
| 409 | 409 | public function setQueryRunner(DatabaseQueryRunner $queryRunner){ |
| 410 | - $this->queryRunner = $queryRunner; |
|
| 411 | - return $this; |
|
| 410 | + $this->queryRunner = $queryRunner; |
|
| 411 | + return $this; |
|
| 412 | 412 | } |
| 413 | 413 | |
| 414 | 414 | /** |
@@ -416,7 +416,7 @@ discard block |
||
| 416 | 416 | * @return array |
| 417 | 417 | */ |
| 418 | 418 | public function getData(){ |
| 419 | - return $this->data; |
|
| 419 | + return $this->data; |
|
| 420 | 420 | } |
| 421 | 421 | |
| 422 | 422 | /** |
@@ -427,62 +427,62 @@ discard block |
||
| 427 | 427 | * @return object the current Database instance |
| 428 | 428 | */ |
| 429 | 429 | public function setData($key, $value = null, $escape = true){ |
| 430 | - if (is_array($key)){ |
|
| 431 | - foreach($key as $k => $v){ |
|
| 432 | - $this->setData($k, $v, $escape); |
|
| 433 | - } |
|
| 434 | - } else { |
|
| 430 | + if (is_array($key)){ |
|
| 431 | + foreach($key as $k => $v){ |
|
| 432 | + $this->setData($k, $v, $escape); |
|
| 433 | + } |
|
| 434 | + } else { |
|
| 435 | 435 | $this->data[$key] = $this->escape($value, $escape); |
| 436 | - } |
|
| 437 | - return $this; |
|
| 436 | + } |
|
| 437 | + return $this; |
|
| 438 | 438 | } |
| 439 | 439 | |
| 440 | - /** |
|
| 441 | - * Execute an SQL query |
|
| 442 | - * @param string $query the query SQL string |
|
| 443 | - * @param boolean $returnAsList indicate whether to return all record or just one row |
|
| 444 | - * @param boolean $returnAsArray return the result as array or not |
|
| 445 | - * @return mixed the query result |
|
| 446 | - */ |
|
| 440 | + /** |
|
| 441 | + * Execute an SQL query |
|
| 442 | + * @param string $query the query SQL string |
|
| 443 | + * @param boolean $returnAsList indicate whether to return all record or just one row |
|
| 444 | + * @param boolean $returnAsArray return the result as array or not |
|
| 445 | + * @return mixed the query result |
|
| 446 | + */ |
|
| 447 | 447 | public function query($query, $returnAsList = true, $returnAsArray = false){ |
| 448 | - $this->reset(); |
|
| 449 | - $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 450 | - //If is the SELECT query |
|
| 451 | - $isSqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
| 448 | + $this->reset(); |
|
| 449 | + $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
| 450 | + //If is the SELECT query |
|
| 451 | + $isSqlSELECTQuery = stristr($this->query, 'SELECT'); |
|
| 452 | 452 | |
| 453 | - //cache expire time |
|
| 454 | - $cacheExpire = $this->temporaryCacheTtl; |
|
| 453 | + //cache expire time |
|
| 454 | + $cacheExpire = $this->temporaryCacheTtl; |
|
| 455 | 455 | |
| 456 | - //return to the initial cache time |
|
| 457 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 456 | + //return to the initial cache time |
|
| 457 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
| 458 | 458 | |
| 459 | - //config for cache |
|
| 460 | - $cacheEnable = get_config('cache_enable'); |
|
| 459 | + //config for cache |
|
| 460 | + $cacheEnable = get_config('cache_enable'); |
|
| 461 | 461 | |
| 462 | - //the database cache content |
|
| 463 | - $cacheContent = null; |
|
| 462 | + //the database cache content |
|
| 463 | + $cacheContent = null; |
|
| 464 | 464 | |
| 465 | - //if can use cache feature for this query |
|
| 466 | - $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
| 465 | + //if can use cache feature for this query |
|
| 466 | + $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
| 467 | 467 | |
| 468 | - if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 469 | - $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 470 | - $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
|
| 471 | - } |
|
| 468 | + if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 469 | + $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
| 470 | + $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
|
| 471 | + } |
|
| 472 | 472 | |
| 473 | - if (!$cacheContent){ |
|
| 474 | - //count the number of query execution to server |
|
| 473 | + if (!$cacheContent){ |
|
| 474 | + //count the number of query execution to server |
|
| 475 | 475 | $this->queryCount++; |
| 476 | 476 | |
| 477 | 477 | $queryResult = $this->queryRunner->setQuery($query) |
| 478 | - ->setReturnType($returnAsList) |
|
| 479 | - ->setReturnAsArray($returnAsArray) |
|
| 480 | - ->execute(); |
|
| 478 | + ->setReturnType($returnAsList) |
|
| 479 | + ->setReturnAsArray($returnAsArray) |
|
| 480 | + ->execute(); |
|
| 481 | 481 | |
| 482 | 482 | if (!is_object($queryResult)){ |
| 483 | - $this->result = false; |
|
| 484 | - $this->numRows = 0; |
|
| 485 | - return $this->result; |
|
| 483 | + $this->result = false; |
|
| 484 | + $this->numRows = 0; |
|
| 485 | + return $this->result; |
|
| 486 | 486 | } |
| 487 | 487 | $this->result = $queryResult->getResult(); |
| 488 | 488 | $this->numRows = $queryResult->getNumRows(); |
@@ -490,72 +490,72 @@ discard block |
||
| 490 | 490 | $key = $this->getCacheKeyForQuery($this->query, $returnAsList, $returnAsArray); |
| 491 | 491 | $this->setCacheContentForQuery($this->query, $key, $this->result, $cacheExpire); |
| 492 | 492 | } |
| 493 | - } else if ($isSqlSELECTQuery){ |
|
| 494 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 495 | - $this->result = $cacheContent; |
|
| 496 | - $this->numRows = count($this->result); |
|
| 497 | - } |
|
| 498 | - return $this->result; |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - /** |
|
| 502 | - * Setting the database configuration using the configuration file and additional configuration from param |
|
| 503 | - * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 504 | - * @param boolean $useConfigFile whether to use database configuration file |
|
| 505 | - * @param boolean $autoConnect whether to connect to database after set the configuration |
|
| 506 | - * @return object Database |
|
| 507 | - */ |
|
| 493 | + } else if ($isSqlSELECTQuery){ |
|
| 494 | + $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 495 | + $this->result = $cacheContent; |
|
| 496 | + $this->numRows = count($this->result); |
|
| 497 | + } |
|
| 498 | + return $this->result; |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + /** |
|
| 502 | + * Setting the database configuration using the configuration file and additional configuration from param |
|
| 503 | + * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
| 504 | + * @param boolean $useConfigFile whether to use database configuration file |
|
| 505 | + * @param boolean $autoConnect whether to connect to database after set the configuration |
|
| 506 | + * @return object Database |
|
| 507 | + */ |
|
| 508 | 508 | public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true, $autoConnect = false){ |
| 509 | - $db = array(); |
|
| 510 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
|
| 511 | - //here don't use require_once because somewhere user can create database instance directly |
|
| 512 | - require CONFIG_PATH . 'database.php'; |
|
| 513 | - } |
|
| 509 | + $db = array(); |
|
| 510 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
|
| 511 | + //here don't use require_once because somewhere user can create database instance directly |
|
| 512 | + require CONFIG_PATH . 'database.php'; |
|
| 513 | + } |
|
| 514 | 514 | |
| 515 | - //merge with the parameter |
|
| 516 | - $db = array_merge($db, $overwriteConfig); |
|
| 515 | + //merge with the parameter |
|
| 516 | + $db = array_merge($db, $overwriteConfig); |
|
| 517 | 517 | |
| 518 | - //get the default configuration |
|
| 519 | - $config = $this->getDatabaseDefaultConfiguration(); |
|
| 518 | + //get the default configuration |
|
| 519 | + $config = $this->getDatabaseDefaultConfiguration(); |
|
| 520 | 520 | |
| 521 | - $config = array_merge($config, $db); |
|
| 522 | - //determine the port using the hostname like localhost:3307 |
|
| 523 | - //hostname will be "localhost", and port "3307" |
|
| 524 | - $p = explode(':', $config['hostname']); |
|
| 525 | - if (count($p) >= 2){ |
|
| 526 | - $config['hostname'] = $p[0]; |
|
| 527 | - $config['port'] = $p[1]; |
|
| 528 | - } |
|
| 521 | + $config = array_merge($config, $db); |
|
| 522 | + //determine the port using the hostname like localhost:3307 |
|
| 523 | + //hostname will be "localhost", and port "3307" |
|
| 524 | + $p = explode(':', $config['hostname']); |
|
| 525 | + if (count($p) >= 2){ |
|
| 526 | + $config['hostname'] = $p[0]; |
|
| 527 | + $config['port'] = $p[1]; |
|
| 528 | + } |
|
| 529 | 529 | |
| 530 | - $this->databaseName = $config['database']; |
|
| 531 | - $this->config = $config; |
|
| 532 | - $this->logger->info( |
|
| 533 | - 'The database configuration are listed below: ' |
|
| 534 | - . stringfy_vars(array_merge( |
|
| 535 | - $this->config, |
|
| 536 | - array('password' => string_hidden($this->config['password'])) |
|
| 537 | - )) |
|
| 538 | - ); |
|
| 539 | - if($autoConnect){ |
|
| 540 | - //Now connect to the database |
|
| 541 | - $this->connect(); |
|
| 542 | - } |
|
| 543 | - return $this; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * Return the database configuration |
|
| 548 | - * @return array |
|
| 549 | - */ |
|
| 530 | + $this->databaseName = $config['database']; |
|
| 531 | + $this->config = $config; |
|
| 532 | + $this->logger->info( |
|
| 533 | + 'The database configuration are listed below: ' |
|
| 534 | + . stringfy_vars(array_merge( |
|
| 535 | + $this->config, |
|
| 536 | + array('password' => string_hidden($this->config['password'])) |
|
| 537 | + )) |
|
| 538 | + ); |
|
| 539 | + if($autoConnect){ |
|
| 540 | + //Now connect to the database |
|
| 541 | + $this->connect(); |
|
| 542 | + } |
|
| 543 | + return $this; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * Return the database configuration |
|
| 548 | + * @return array |
|
| 549 | + */ |
|
| 550 | 550 | public function getDatabaseConfiguration(){ |
| 551 | - return $this->config; |
|
| 551 | + return $this->config; |
|
| 552 | 552 | } |
| 553 | 553 | |
| 554 | 554 | /** |
| 555 | 555 | * Close the connexion |
| 556 | 556 | */ |
| 557 | 557 | public function close(){ |
| 558 | - $this->pdo = null; |
|
| 558 | + $this->pdo = null; |
|
| 559 | 559 | } |
| 560 | 560 | |
| 561 | 561 | /** |
@@ -563,16 +563,16 @@ discard block |
||
| 563 | 563 | * @return array |
| 564 | 564 | */ |
| 565 | 565 | protected function getDatabaseDefaultConfiguration(){ |
| 566 | - return array( |
|
| 567 | - 'driver' => '', |
|
| 568 | - 'username' => '', |
|
| 569 | - 'password' => '', |
|
| 570 | - 'database' => '', |
|
| 571 | - 'hostname' => 'localhost', |
|
| 572 | - 'charset' => 'utf8', |
|
| 573 | - 'collation' => 'utf8_general_ci', |
|
| 574 | - 'prefix' => '', |
|
| 575 | - 'port' => '' |
|
| 566 | + return array( |
|
| 567 | + 'driver' => '', |
|
| 568 | + 'username' => '', |
|
| 569 | + 'password' => '', |
|
| 570 | + 'database' => '', |
|
| 571 | + 'hostname' => 'localhost', |
|
| 572 | + 'charset' => 'utf8', |
|
| 573 | + 'collation' => 'utf8_general_ci', |
|
| 574 | + 'prefix' => '', |
|
| 575 | + 'port' => '' |
|
| 576 | 576 | ); |
| 577 | 577 | } |
| 578 | 578 | |
@@ -581,18 +581,18 @@ discard block |
||
| 581 | 581 | * @return void |
| 582 | 582 | */ |
| 583 | 583 | protected function updateQueryBuilderAndRunnerProperties(){ |
| 584 | - //update queryBuilder with some properties needed |
|
| 585 | - if (is_object($this->queryBuilder)){ |
|
| 584 | + //update queryBuilder with some properties needed |
|
| 585 | + if (is_object($this->queryBuilder)){ |
|
| 586 | 586 | $this->queryBuilder->setDriver($this->config['driver']) |
| 587 | - ->setPrefix($this->config['prefix']) |
|
| 588 | - ->setPdo($this->pdo); |
|
| 589 | - } |
|
| 587 | + ->setPrefix($this->config['prefix']) |
|
| 588 | + ->setPdo($this->pdo); |
|
| 589 | + } |
|
| 590 | 590 | |
| 591 | - //update queryRunner with some properties needed |
|
| 592 | - if (is_object($this->queryRunner)){ |
|
| 591 | + //update queryRunner with some properties needed |
|
| 592 | + if (is_object($this->queryRunner)){ |
|
| 593 | 593 | $this->queryRunner->setDriver($this->config['driver']) |
| 594 | - ->setPdo($this->pdo); |
|
| 595 | - } |
|
| 594 | + ->setPdo($this->pdo); |
|
| 595 | + } |
|
| 596 | 596 | } |
| 597 | 597 | |
| 598 | 598 | |
@@ -601,21 +601,21 @@ discard block |
||
| 601 | 601 | * @return string|null the DSN string or null if can not find it |
| 602 | 602 | */ |
| 603 | 603 | protected function getDsnValueFromConfig(){ |
| 604 | - $dsn = null; |
|
| 605 | - $config = $this->getDatabaseConfiguration(); |
|
| 606 | - if (! empty($config)){ |
|
| 604 | + $dsn = null; |
|
| 605 | + $config = $this->getDatabaseConfiguration(); |
|
| 606 | + if (! empty($config)){ |
|
| 607 | 607 | $driver = $config['driver']; |
| 608 | 608 | $driverDsnMap = array( |
| 609 | - 'mysql' => $this->getDsnValueForDriver('mysql'), |
|
| 610 | - 'pgsql' => $this->getDsnValueForDriver('pgsql'), |
|
| 611 | - 'sqlite' => $this->getDsnValueForDriver('sqlite'), |
|
| 612 | - 'oracle' => $this->getDsnValueForDriver('oracle') |
|
| 613 | - ); |
|
| 609 | + 'mysql' => $this->getDsnValueForDriver('mysql'), |
|
| 610 | + 'pgsql' => $this->getDsnValueForDriver('pgsql'), |
|
| 611 | + 'sqlite' => $this->getDsnValueForDriver('sqlite'), |
|
| 612 | + 'oracle' => $this->getDsnValueForDriver('oracle') |
|
| 613 | + ); |
|
| 614 | 614 | if (isset($driverDsnMap[$driver])){ |
| 615 | - $dsn = $driverDsnMap[$driver]; |
|
| 615 | + $dsn = $driverDsnMap[$driver]; |
|
| 616 | 616 | } |
| 617 | - } |
|
| 618 | - return $dsn; |
|
| 617 | + } |
|
| 618 | + return $dsn; |
|
| 619 | 619 | } |
| 620 | 620 | |
| 621 | 621 | /** |
@@ -624,32 +624,32 @@ discard block |
||
| 624 | 624 | * @return string|null the dsn name |
| 625 | 625 | */ |
| 626 | 626 | protected function getDsnValueForDriver($driver){ |
| 627 | - $dsn = ''; |
|
| 628 | - $config = $this->getDatabaseConfiguration(); |
|
| 629 | - if (empty($config)){ |
|
| 627 | + $dsn = ''; |
|
| 628 | + $config = $this->getDatabaseConfiguration(); |
|
| 629 | + if (empty($config)){ |
|
| 630 | 630 | return null; |
| 631 | - } |
|
| 632 | - switch ($driver) { |
|
| 631 | + } |
|
| 632 | + switch ($driver) { |
|
| 633 | 633 | case 'mysql': |
| 634 | 634 | case 'pgsql': |
| 635 | 635 | $port = ''; |
| 636 | - if (! empty($config['port'])) { |
|
| 636 | + if (! empty($config['port'])) { |
|
| 637 | 637 | $port = 'port=' . $config['port'] . ';'; |
| 638 | - } |
|
| 639 | - $dsn = $driver . ':host=' . $config['hostname'] . ';' . $port . 'dbname=' . $config['database']; |
|
| 640 | - break; |
|
| 638 | + } |
|
| 639 | + $dsn = $driver . ':host=' . $config['hostname'] . ';' . $port . 'dbname=' . $config['database']; |
|
| 640 | + break; |
|
| 641 | 641 | case 'sqlite': |
| 642 | 642 | $dsn = 'sqlite:' . $config['database']; |
| 643 | - break; |
|
| 644 | - case 'oracle': |
|
| 643 | + break; |
|
| 644 | + case 'oracle': |
|
| 645 | 645 | $port = ''; |
| 646 | - if (! empty($config['port'])) { |
|
| 646 | + if (! empty($config['port'])) { |
|
| 647 | 647 | $port = ':' . $config['port']; |
| 648 | - } |
|
| 649 | - $dsn = 'oci:dbname=' . $config['hostname'] . $port . '/' . $config['database']; |
|
| 650 | - break; |
|
| 651 | - } |
|
| 652 | - return $dsn; |
|
| 648 | + } |
|
| 649 | + $dsn = 'oci:dbname=' . $config['hostname'] . $port . '/' . $config['database']; |
|
| 650 | + break; |
|
| 651 | + } |
|
| 652 | + return $dsn; |
|
| 653 | 653 | } |
| 654 | 654 | |
| 655 | 655 | /** |
@@ -661,11 +661,11 @@ discard block |
||
| 661 | 661 | protected function getCacheContentForQuery($query, $returnAsList, $returnAsArray){ |
| 662 | 662 | $cacheKey = $this->getCacheKeyForQuery($query, $returnAsList, $returnAsArray); |
| 663 | 663 | if (! is_object($this->cacheInstance)){ |
| 664 | - //can not call method with reference in argument |
|
| 665 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
| 666 | - //use temporary variable |
|
| 667 | - $instance = & get_instance()->cache; |
|
| 668 | - $this->cacheInstance = $instance; |
|
| 664 | + //can not call method with reference in argument |
|
| 665 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
| 666 | + //use temporary variable |
|
| 667 | + $instance = & get_instance()->cache; |
|
| 668 | + $this->cacheInstance = $instance; |
|
| 669 | 669 | } |
| 670 | 670 | return $this->cacheInstance->get($cacheKey); |
| 671 | 671 | } |
@@ -677,80 +677,80 @@ discard block |
||
| 677 | 677 | * @param mixed $result the query result to save |
| 678 | 678 | * @param int $expire the cache TTL |
| 679 | 679 | */ |
| 680 | - protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
| 680 | + protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
| 681 | 681 | $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
| 682 | 682 | if (! is_object($this->cacheInstance)){ |
| 683 | - //can not call method with reference in argument |
|
| 684 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
| 685 | - //use temporary variable |
|
| 686 | - $instance = & get_instance()->cache; |
|
| 687 | - $this->cacheInstance = $instance; |
|
| 688 | - } |
|
| 683 | + //can not call method with reference in argument |
|
| 684 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
| 685 | + //use temporary variable |
|
| 686 | + $instance = & get_instance()->cache; |
|
| 687 | + $this->cacheInstance = $instance; |
|
| 688 | + } |
|
| 689 | 689 | $this->cacheInstance->set($key, $result, $expire); |
| 690 | - } |
|
| 690 | + } |
|
| 691 | 691 | |
| 692 | 692 | |
| 693 | - /** |
|
| 694 | - * Return the cache key for the given query |
|
| 695 | - * @see Database::query |
|
| 696 | - * |
|
| 697 | - * @return string |
|
| 698 | - */ |
|
| 693 | + /** |
|
| 694 | + * Return the cache key for the given query |
|
| 695 | + * @see Database::query |
|
| 696 | + * |
|
| 697 | + * @return string |
|
| 698 | + */ |
|
| 699 | 699 | protected function getCacheKeyForQuery($query, $returnAsList, $returnAsArray){ |
| 700 | - return md5($query . $returnAsList . $returnAsArray); |
|
| 701 | - } |
|
| 702 | - |
|
| 703 | - /** |
|
| 704 | - * Set the dependencies instance using argument or create new instance if is null |
|
| 705 | - * @param string $name this class property name. |
|
| 706 | - * @param object $instance the instance. If is not null will use it |
|
| 707 | - * otherwise will create new instance. |
|
| 708 | - * @param string $loadClassName the name of class to load using class_loader function. |
|
| 709 | - * @param string $loadClassPath the path of class to load using class_loader function. |
|
| 710 | - * |
|
| 711 | - * @return object this current instance |
|
| 712 | - */ |
|
| 700 | + return md5($query . $returnAsList . $returnAsArray); |
|
| 701 | + } |
|
| 702 | + |
|
| 703 | + /** |
|
| 704 | + * Set the dependencies instance using argument or create new instance if is null |
|
| 705 | + * @param string $name this class property name. |
|
| 706 | + * @param object $instance the instance. If is not null will use it |
|
| 707 | + * otherwise will create new instance. |
|
| 708 | + * @param string $loadClassName the name of class to load using class_loader function. |
|
| 709 | + * @param string $loadClassPath the path of class to load using class_loader function. |
|
| 710 | + * |
|
| 711 | + * @return object this current instance |
|
| 712 | + */ |
|
| 713 | 713 | protected function setDependencyInstanceFromParamOrCreate($name, $instance = null, $loadClassName = null, $loadClassePath = 'classes'){ |
| 714 | - if ($instance !== null){ |
|
| 714 | + if ($instance !== null){ |
|
| 715 | 715 | $this->{$name} = $instance; |
| 716 | 716 | return $this; |
| 717 | - } |
|
| 718 | - $this->{$name} =& class_loader($loadClassName, $loadClassePath); |
|
| 719 | - return $this; |
|
| 717 | + } |
|
| 718 | + $this->{$name} =& class_loader($loadClassName, $loadClassePath); |
|
| 719 | + return $this; |
|
| 720 | 720 | } |
| 721 | 721 | |
| 722 | - /** |
|
| 723 | - * Set the Log instance using argument or create new instance |
|
| 724 | - * @param object $logger the Log instance if not null |
|
| 725 | - * |
|
| 726 | - * @return object this current instance |
|
| 727 | - */ |
|
| 722 | + /** |
|
| 723 | + * Set the Log instance using argument or create new instance |
|
| 724 | + * @param object $logger the Log instance if not null |
|
| 725 | + * |
|
| 726 | + * @return object this current instance |
|
| 727 | + */ |
|
| 728 | 728 | protected function setLoggerFromParamOrCreate(Log $logger = null){ |
| 729 | - $this->setDependencyInstanceFromParamOrCreate('logger', $logger, 'Log', 'classes'); |
|
| 730 | - if ($logger === null){ |
|
| 729 | + $this->setDependencyInstanceFromParamOrCreate('logger', $logger, 'Log', 'classes'); |
|
| 730 | + if ($logger === null){ |
|
| 731 | 731 | $this->logger->setLogger('Library::Database'); |
| 732 | - } |
|
| 733 | - return $this; |
|
| 732 | + } |
|
| 733 | + return $this; |
|
| 734 | 734 | } |
| 735 | 735 | |
| 736 | 736 | /** |
| 737 | 737 | * Reset the database class attributs to the initail values before each query. |
| 738 | 738 | */ |
| 739 | 739 | private function reset(){ |
| 740 | - //query builder reset |
|
| 741 | - $this->queryBuilder->reset(); |
|
| 742 | - $this->numRows = 0; |
|
| 743 | - $this->insertId = null; |
|
| 744 | - $this->query = null; |
|
| 745 | - $this->result = array(); |
|
| 746 | - $this->data = array(); |
|
| 740 | + //query builder reset |
|
| 741 | + $this->queryBuilder->reset(); |
|
| 742 | + $this->numRows = 0; |
|
| 743 | + $this->insertId = null; |
|
| 744 | + $this->query = null; |
|
| 745 | + $this->result = array(); |
|
| 746 | + $this->data = array(); |
|
| 747 | 747 | } |
| 748 | 748 | |
| 749 | 749 | /** |
| 750 | 750 | * The class destructor |
| 751 | 751 | */ |
| 752 | 752 | public function __destruct(){ |
| 753 | - $this->pdo = null; |
|
| 753 | + $this->pdo = null; |
|
| 754 | 754 | } |
| 755 | 755 | |
| 756 | 756 | } |
@@ -23,98 +23,98 @@ discard block |
||
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | - class Database{ |
|
| 26 | + class Database { |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * The PDO instance |
| 30 | 30 | * @var object |
| 31 | 31 | */ |
| 32 | - private $pdo = null; |
|
| 32 | + private $pdo = null; |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * The database name used for the application |
| 36 | 36 | * @var string |
| 37 | 37 | */ |
| 38 | - private $databaseName = null; |
|
| 38 | + private $databaseName = null; |
|
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * The number of rows returned by the last query |
| 42 | 42 | * @var int |
| 43 | 43 | */ |
| 44 | - private $numRows = 0; |
|
| 44 | + private $numRows = 0; |
|
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * The last insert id for the primary key column that have auto increment or sequence |
| 48 | 48 | * @var mixed |
| 49 | 49 | */ |
| 50 | - private $insertId = null; |
|
| 50 | + private $insertId = null; |
|
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * The full SQL query statment after build for each command |
| 54 | 54 | * @var string |
| 55 | 55 | */ |
| 56 | - private $query = null; |
|
| 56 | + private $query = null; |
|
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * The result returned for the last query |
| 60 | 60 | * @var mixed |
| 61 | 61 | */ |
| 62 | - private $result = array(); |
|
| 62 | + private $result = array(); |
|
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * The cache default time to live in second. 0 means no need to use the cache feature |
| 66 | 66 | * @var int |
| 67 | 67 | */ |
| 68 | - private $cacheTtl = 0; |
|
| 68 | + private $cacheTtl = 0; |
|
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | 71 | * The cache current time to live. 0 means no need to use the cache feature |
| 72 | 72 | * @var int |
| 73 | 73 | */ |
| 74 | - private $temporaryCacheTtl = 0; |
|
| 74 | + private $temporaryCacheTtl = 0; |
|
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * The number of executed query for the current request |
| 78 | 78 | * @var int |
| 79 | 79 | */ |
| 80 | - private $queryCount = 0; |
|
| 80 | + private $queryCount = 0; |
|
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * The default data to be used in the statments query INSERT, UPDATE |
| 84 | 84 | * @var array |
| 85 | 85 | */ |
| 86 | - private $data = array(); |
|
| 86 | + private $data = array(); |
|
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * The database configuration |
| 90 | 90 | * @var array |
| 91 | 91 | */ |
| 92 | - private $config = array(); |
|
| 92 | + private $config = array(); |
|
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * The logger instance |
| 96 | 96 | * @var object |
| 97 | 97 | */ |
| 98 | - private $logger = null; |
|
| 98 | + private $logger = null; |
|
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * The cache instance |
| 102 | 102 | * @var object |
| 103 | 103 | */ |
| 104 | - private $cacheInstance = null; |
|
| 104 | + private $cacheInstance = null; |
|
| 105 | 105 | |
| 106 | 106 | |
| 107 | 107 | /** |
| 108 | 108 | * The DatabaseQueryBuilder instance |
| 109 | 109 | * @var object |
| 110 | 110 | */ |
| 111 | - private $queryBuilder = null; |
|
| 111 | + private $queryBuilder = null; |
|
| 112 | 112 | |
| 113 | 113 | /** |
| 114 | 114 | * The DatabaseQueryRunner instance |
| 115 | 115 | * @var object |
| 116 | 116 | */ |
| 117 | - private $queryRunner = null; |
|
| 117 | + private $queryRunner = null; |
|
| 118 | 118 | |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | * @param array $overwriteConfig the config to overwrite with the config set in database.php |
| 123 | 123 | * @param boolean $autoConnect whether to connect to database automatically |
| 124 | 124 | */ |
| 125 | - public function __construct($overwriteConfig = array(), $autoConnect = true){ |
|
| 125 | + public function __construct($overwriteConfig = array(), $autoConnect = true) { |
|
| 126 | 126 | //Set Log instance to use |
| 127 | 127 | $this->setLoggerFromParamOrCreate(null); |
| 128 | 128 | |
@@ -143,10 +143,10 @@ discard block |
||
| 143 | 143 | * This is used to connect to database |
| 144 | 144 | * @return bool |
| 145 | 145 | */ |
| 146 | - public function connect(){ |
|
| 146 | + public function connect() { |
|
| 147 | 147 | $config = $this->getDatabaseConfiguration(); |
| 148 | - if (! empty($config)){ |
|
| 149 | - try{ |
|
| 148 | + if (!empty($config)) { |
|
| 149 | + try { |
|
| 150 | 150 | $this->pdo = new PDO($this->getDsnValueFromConfig(), $config['username'], $config['password']); |
| 151 | 151 | $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
| 152 | 152 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | |
| 158 | 158 | return is_object($this->pdo); |
| 159 | 159 | } |
| 160 | - catch (PDOException $e){ |
|
| 160 | + catch (PDOException $e) { |
|
| 161 | 161 | $this->logger->fatal($e->getMessage()); |
| 162 | 162 | show_error('Cannot connect to Database.'); |
| 163 | 163 | return false; |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | * Return the number of rows returned by the current query |
| 172 | 172 | * @return int |
| 173 | 173 | */ |
| 174 | - public function numRows(){ |
|
| 174 | + public function numRows() { |
|
| 175 | 175 | return $this->numRows; |
| 176 | 176 | } |
| 177 | 177 | |
@@ -179,7 +179,7 @@ discard block |
||
| 179 | 179 | * Return the last insert id value |
| 180 | 180 | * @return mixed |
| 181 | 181 | */ |
| 182 | - public function insertId(){ |
|
| 182 | + public function insertId() { |
|
| 183 | 183 | return $this->insertId; |
| 184 | 184 | } |
| 185 | 185 | |
@@ -190,10 +190,10 @@ discard block |
||
| 190 | 190 | * If is string will determine the result type "array" or "object" |
| 191 | 191 | * @return mixed the query SQL string or the record result |
| 192 | 192 | */ |
| 193 | - public function get($returnSQLQueryOrResultType = false){ |
|
| 193 | + public function get($returnSQLQueryOrResultType = false) { |
|
| 194 | 194 | $this->queryBuilder->limit(1); |
| 195 | 195 | $query = $this->getAll(true); |
| 196 | - if ($returnSQLQueryOrResultType === true){ |
|
| 196 | + if ($returnSQLQueryOrResultType === true) { |
|
| 197 | 197 | return $query; |
| 198 | 198 | } else { |
| 199 | 199 | return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
@@ -206,9 +206,9 @@ discard block |
||
| 206 | 206 | * If is string will determine the result type "array" or "object" |
| 207 | 207 | * @return mixed the query SQL string or the record result |
| 208 | 208 | */ |
| 209 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
| 209 | + public function getAll($returnSQLQueryOrResultType = false) { |
|
| 210 | 210 | $query = $this->queryBuilder->getQuery(); |
| 211 | - if ($returnSQLQueryOrResultType === true){ |
|
| 211 | + if ($returnSQLQueryOrResultType === true) { |
|
| 212 | 212 | return $query; |
| 213 | 213 | } |
| 214 | 214 | return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
@@ -220,18 +220,18 @@ discard block |
||
| 220 | 220 | * @param boolean $escape whether to escape or not the values |
| 221 | 221 | * @return mixed the insert id of the new record or null |
| 222 | 222 | */ |
| 223 | - public function insert($data = array(), $escape = true){ |
|
| 224 | - if (empty($data) && ! empty($this->data)){ |
|
| 223 | + public function insert($data = array(), $escape = true) { |
|
| 224 | + if (empty($data) && !empty($this->data)) { |
|
| 225 | 225 | //as when using $this->setData() may be the data already escaped |
| 226 | 226 | $escape = false; |
| 227 | 227 | $data = $this->data; |
| 228 | 228 | } |
| 229 | 229 | $query = $this->queryBuilder->insert($data, $escape)->getQuery(); |
| 230 | 230 | $result = $this->query($query); |
| 231 | - if ($result){ |
|
| 231 | + if ($result) { |
|
| 232 | 232 | $this->insertId = $this->pdo->lastInsertId(); |
| 233 | 233 | //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
| 234 | - return ! ($this->insertId) ? true : $this->insertId; |
|
| 234 | + return !($this->insertId) ? true : $this->insertId; |
|
| 235 | 235 | } |
| 236 | 236 | return false; |
| 237 | 237 | } |
@@ -242,8 +242,8 @@ discard block |
||
| 242 | 242 | * @param boolean $escape whether to escape or not the values |
| 243 | 243 | * @return mixed the update status |
| 244 | 244 | */ |
| 245 | - public function update($data = array(), $escape = true){ |
|
| 246 | - if (empty($data) && ! empty($this->data)){ |
|
| 245 | + public function update($data = array(), $escape = true) { |
|
| 246 | + if (empty($data) && !empty($this->data)) { |
|
| 247 | 247 | //as when using $this->setData() may be the data already escaped |
| 248 | 248 | $escape = false; |
| 249 | 249 | $data = $this->data; |
@@ -256,7 +256,7 @@ discard block |
||
| 256 | 256 | * Delete the record in database |
| 257 | 257 | * @return mixed the delete status |
| 258 | 258 | */ |
| 259 | - public function delete(){ |
|
| 259 | + public function delete() { |
|
| 260 | 260 | $query = $this->queryBuilder->delete()->getQuery(); |
| 261 | 261 | return $this->query($query); |
| 262 | 262 | } |
@@ -266,7 +266,7 @@ discard block |
||
| 266 | 266 | * @param integer $ttl the cache time to live in second |
| 267 | 267 | * @return object the current Database instance |
| 268 | 268 | */ |
| 269 | - public function setCache($ttl = 0){ |
|
| 269 | + public function setCache($ttl = 0) { |
|
| 270 | 270 | $this->cacheTtl = $ttl; |
| 271 | 271 | $this->temporaryCacheTtl = $ttl; |
| 272 | 272 | return $this; |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | * @param integer $ttl the cache time to live in second |
| 278 | 278 | * @return object the current Database instance |
| 279 | 279 | */ |
| 280 | - public function cached($ttl = 0){ |
|
| 280 | + public function cached($ttl = 0) { |
|
| 281 | 281 | $this->temporaryCacheTtl = $ttl; |
| 282 | 282 | return $this; |
| 283 | 283 | } |
@@ -289,9 +289,9 @@ discard block |
||
| 289 | 289 | * @return mixed the data after escaped or the same data if no |
| 290 | 290 | * need escaped |
| 291 | 291 | */ |
| 292 | - public function escape($data, $escaped = true){ |
|
| 292 | + public function escape($data, $escaped = true) { |
|
| 293 | 293 | $data = trim($data); |
| 294 | - if($escaped){ |
|
| 294 | + if ($escaped) { |
|
| 295 | 295 | return $this->pdo->quote($data); |
| 296 | 296 | } |
| 297 | 297 | return $data; |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | * Return the number query executed count for the current request |
| 302 | 302 | * @return int |
| 303 | 303 | */ |
| 304 | - public function queryCount(){ |
|
| 304 | + public function queryCount() { |
|
| 305 | 305 | return $this->queryCount; |
| 306 | 306 | } |
| 307 | 307 | |
@@ -309,7 +309,7 @@ discard block |
||
| 309 | 309 | * Return the current query SQL string |
| 310 | 310 | * @return string |
| 311 | 311 | */ |
| 312 | - public function getQuery(){ |
|
| 312 | + public function getQuery() { |
|
| 313 | 313 | return $this->query; |
| 314 | 314 | } |
| 315 | 315 | |
@@ -317,7 +317,7 @@ discard block |
||
| 317 | 317 | * Return the application database name |
| 318 | 318 | * @return string |
| 319 | 319 | */ |
| 320 | - public function getDatabaseName(){ |
|
| 320 | + public function getDatabaseName() { |
|
| 321 | 321 | return $this->databaseName; |
| 322 | 322 | } |
| 323 | 323 | |
@@ -325,7 +325,7 @@ discard block |
||
| 325 | 325 | * Return the PDO instance |
| 326 | 326 | * @return object |
| 327 | 327 | */ |
| 328 | - public function getPdo(){ |
|
| 328 | + public function getPdo() { |
|
| 329 | 329 | return $this->pdo; |
| 330 | 330 | } |
| 331 | 331 | |
@@ -334,7 +334,7 @@ discard block |
||
| 334 | 334 | * @param object $pdo the pdo object |
| 335 | 335 | * @return object Database |
| 336 | 336 | */ |
| 337 | - public function setPdo(PDO $pdo){ |
|
| 337 | + public function setPdo(PDO $pdo) { |
|
| 338 | 338 | $this->pdo = $pdo; |
| 339 | 339 | return $this; |
| 340 | 340 | } |
@@ -344,7 +344,7 @@ discard block |
||
| 344 | 344 | * Return the Log instance |
| 345 | 345 | * @return Log |
| 346 | 346 | */ |
| 347 | - public function getLogger(){ |
|
| 347 | + public function getLogger() { |
|
| 348 | 348 | return $this->logger; |
| 349 | 349 | } |
| 350 | 350 | |
@@ -353,7 +353,7 @@ discard block |
||
| 353 | 353 | * @param Log $logger the log object |
| 354 | 354 | * @return object Database |
| 355 | 355 | */ |
| 356 | - public function setLogger($logger){ |
|
| 356 | + public function setLogger($logger) { |
|
| 357 | 357 | $this->logger = $logger; |
| 358 | 358 | return $this; |
| 359 | 359 | } |
@@ -362,7 +362,7 @@ discard block |
||
| 362 | 362 | * Return the cache instance |
| 363 | 363 | * @return CacheInterface |
| 364 | 364 | */ |
| 365 | - public function getCacheInstance(){ |
|
| 365 | + public function getCacheInstance() { |
|
| 366 | 366 | return $this->cacheInstance; |
| 367 | 367 | } |
| 368 | 368 | |
@@ -371,7 +371,7 @@ discard block |
||
| 371 | 371 | * @param CacheInterface $cache the cache object |
| 372 | 372 | * @return object Database |
| 373 | 373 | */ |
| 374 | - public function setCacheInstance($cache){ |
|
| 374 | + public function setCacheInstance($cache) { |
|
| 375 | 375 | $this->cacheInstance = $cache; |
| 376 | 376 | return $this; |
| 377 | 377 | } |
@@ -381,7 +381,7 @@ discard block |
||
| 381 | 381 | * Return the DatabaseQueryBuilder instance |
| 382 | 382 | * @return object DatabaseQueryBuilder |
| 383 | 383 | */ |
| 384 | - public function getQueryBuilder(){ |
|
| 384 | + public function getQueryBuilder() { |
|
| 385 | 385 | return $this->queryBuilder; |
| 386 | 386 | } |
| 387 | 387 | |
@@ -389,7 +389,7 @@ discard block |
||
| 389 | 389 | * Set the DatabaseQueryBuilder instance |
| 390 | 390 | * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
| 391 | 391 | */ |
| 392 | - public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder){ |
|
| 392 | + public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder) { |
|
| 393 | 393 | $this->queryBuilder = $queryBuilder; |
| 394 | 394 | return $this; |
| 395 | 395 | } |
@@ -398,7 +398,7 @@ discard block |
||
| 398 | 398 | * Return the DatabaseQueryRunner instance |
| 399 | 399 | * @return object DatabaseQueryRunner |
| 400 | 400 | */ |
| 401 | - public function getQueryRunner(){ |
|
| 401 | + public function getQueryRunner() { |
|
| 402 | 402 | return $this->queryRunner; |
| 403 | 403 | } |
| 404 | 404 | |
@@ -406,7 +406,7 @@ discard block |
||
| 406 | 406 | * Set the DatabaseQueryRunner instance |
| 407 | 407 | * @param object DatabaseQueryRunner $queryRunner the DatabaseQueryRunner object |
| 408 | 408 | */ |
| 409 | - public function setQueryRunner(DatabaseQueryRunner $queryRunner){ |
|
| 409 | + public function setQueryRunner(DatabaseQueryRunner $queryRunner) { |
|
| 410 | 410 | $this->queryRunner = $queryRunner; |
| 411 | 411 | return $this; |
| 412 | 412 | } |
@@ -415,7 +415,7 @@ discard block |
||
| 415 | 415 | * Return the data to be used for insert, update, etc. |
| 416 | 416 | * @return array |
| 417 | 417 | */ |
| 418 | - public function getData(){ |
|
| 418 | + public function getData() { |
|
| 419 | 419 | return $this->data; |
| 420 | 420 | } |
| 421 | 421 | |
@@ -426,9 +426,9 @@ discard block |
||
| 426 | 426 | * @param boolean $escape whether to escape or not the $value |
| 427 | 427 | * @return object the current Database instance |
| 428 | 428 | */ |
| 429 | - public function setData($key, $value = null, $escape = true){ |
|
| 430 | - if (is_array($key)){ |
|
| 431 | - foreach($key as $k => $v){ |
|
| 429 | + public function setData($key, $value = null, $escape = true) { |
|
| 430 | + if (is_array($key)) { |
|
| 431 | + foreach ($key as $k => $v) { |
|
| 432 | 432 | $this->setData($k, $v, $escape); |
| 433 | 433 | } |
| 434 | 434 | } else { |
@@ -444,7 +444,7 @@ discard block |
||
| 444 | 444 | * @param boolean $returnAsArray return the result as array or not |
| 445 | 445 | * @return mixed the query result |
| 446 | 446 | */ |
| 447 | - public function query($query, $returnAsList = true, $returnAsArray = false){ |
|
| 447 | + public function query($query, $returnAsList = true, $returnAsArray = false) { |
|
| 448 | 448 | $this->reset(); |
| 449 | 449 | $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
| 450 | 450 | //If is the SELECT query |
@@ -465,12 +465,12 @@ discard block |
||
| 465 | 465 | //if can use cache feature for this query |
| 466 | 466 | $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
| 467 | 467 | |
| 468 | - if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
| 468 | + if ($dbCacheStatus && $isSqlSELECTQuery) { |
|
| 469 | 469 | $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
| 470 | 470 | $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
| 471 | 471 | } |
| 472 | 472 | |
| 473 | - if (!$cacheContent){ |
|
| 473 | + if (!$cacheContent) { |
|
| 474 | 474 | //count the number of query execution to server |
| 475 | 475 | $this->queryCount++; |
| 476 | 476 | |
@@ -479,19 +479,19 @@ discard block |
||
| 479 | 479 | ->setReturnAsArray($returnAsArray) |
| 480 | 480 | ->execute(); |
| 481 | 481 | |
| 482 | - if (!is_object($queryResult)){ |
|
| 482 | + if (!is_object($queryResult)) { |
|
| 483 | 483 | $this->result = false; |
| 484 | 484 | $this->numRows = 0; |
| 485 | 485 | return $this->result; |
| 486 | 486 | } |
| 487 | 487 | $this->result = $queryResult->getResult(); |
| 488 | 488 | $this->numRows = $queryResult->getNumRows(); |
| 489 | - if ($isSqlSELECTQuery && $dbCacheStatus){ |
|
| 489 | + if ($isSqlSELECTQuery && $dbCacheStatus) { |
|
| 490 | 490 | $key = $this->getCacheKeyForQuery($this->query, $returnAsList, $returnAsArray); |
| 491 | 491 | $this->setCacheContentForQuery($this->query, $key, $this->result, $cacheExpire); |
| 492 | 492 | } |
| 493 | - } else if ($isSqlSELECTQuery){ |
|
| 494 | - $this->logger->info('The result for query [' .$this->query. '] already cached use it'); |
|
| 493 | + } else if ($isSqlSELECTQuery) { |
|
| 494 | + $this->logger->info('The result for query [' . $this->query . '] already cached use it'); |
|
| 495 | 495 | $this->result = $cacheContent; |
| 496 | 496 | $this->numRows = count($this->result); |
| 497 | 497 | } |
@@ -505,9 +505,9 @@ discard block |
||
| 505 | 505 | * @param boolean $autoConnect whether to connect to database after set the configuration |
| 506 | 506 | * @return object Database |
| 507 | 507 | */ |
| 508 | - public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true, $autoConnect = false){ |
|
| 508 | + public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true, $autoConnect = false) { |
|
| 509 | 509 | $db = array(); |
| 510 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
|
| 510 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')) { |
|
| 511 | 511 | //here don't use require_once because somewhere user can create database instance directly |
| 512 | 512 | require CONFIG_PATH . 'database.php'; |
| 513 | 513 | } |
@@ -522,7 +522,7 @@ discard block |
||
| 522 | 522 | //determine the port using the hostname like localhost:3307 |
| 523 | 523 | //hostname will be "localhost", and port "3307" |
| 524 | 524 | $p = explode(':', $config['hostname']); |
| 525 | - if (count($p) >= 2){ |
|
| 525 | + if (count($p) >= 2) { |
|
| 526 | 526 | $config['hostname'] = $p[0]; |
| 527 | 527 | $config['port'] = $p[1]; |
| 528 | 528 | } |
@@ -536,7 +536,7 @@ discard block |
||
| 536 | 536 | array('password' => string_hidden($this->config['password'])) |
| 537 | 537 | )) |
| 538 | 538 | ); |
| 539 | - if($autoConnect){ |
|
| 539 | + if ($autoConnect) { |
|
| 540 | 540 | //Now connect to the database |
| 541 | 541 | $this->connect(); |
| 542 | 542 | } |
@@ -547,14 +547,14 @@ discard block |
||
| 547 | 547 | * Return the database configuration |
| 548 | 548 | * @return array |
| 549 | 549 | */ |
| 550 | - public function getDatabaseConfiguration(){ |
|
| 550 | + public function getDatabaseConfiguration() { |
|
| 551 | 551 | return $this->config; |
| 552 | 552 | } |
| 553 | 553 | |
| 554 | 554 | /** |
| 555 | 555 | * Close the connexion |
| 556 | 556 | */ |
| 557 | - public function close(){ |
|
| 557 | + public function close() { |
|
| 558 | 558 | $this->pdo = null; |
| 559 | 559 | } |
| 560 | 560 | |
@@ -562,7 +562,7 @@ discard block |
||
| 562 | 562 | * Return the database default configuration |
| 563 | 563 | * @return array |
| 564 | 564 | */ |
| 565 | - protected function getDatabaseDefaultConfiguration(){ |
|
| 565 | + protected function getDatabaseDefaultConfiguration() { |
|
| 566 | 566 | return array( |
| 567 | 567 | 'driver' => '', |
| 568 | 568 | 'username' => '', |
@@ -580,16 +580,16 @@ discard block |
||
| 580 | 580 | * Update the DatabaseQueryBuilder and DatabaseQueryRunner properties |
| 581 | 581 | * @return void |
| 582 | 582 | */ |
| 583 | - protected function updateQueryBuilderAndRunnerProperties(){ |
|
| 583 | + protected function updateQueryBuilderAndRunnerProperties() { |
|
| 584 | 584 | //update queryBuilder with some properties needed |
| 585 | - if (is_object($this->queryBuilder)){ |
|
| 585 | + if (is_object($this->queryBuilder)) { |
|
| 586 | 586 | $this->queryBuilder->setDriver($this->config['driver']) |
| 587 | 587 | ->setPrefix($this->config['prefix']) |
| 588 | 588 | ->setPdo($this->pdo); |
| 589 | 589 | } |
| 590 | 590 | |
| 591 | 591 | //update queryRunner with some properties needed |
| 592 | - if (is_object($this->queryRunner)){ |
|
| 592 | + if (is_object($this->queryRunner)) { |
|
| 593 | 593 | $this->queryRunner->setDriver($this->config['driver']) |
| 594 | 594 | ->setPdo($this->pdo); |
| 595 | 595 | } |
@@ -600,10 +600,10 @@ discard block |
||
| 600 | 600 | * This method is used to get the PDO DSN string using the configured driver |
| 601 | 601 | * @return string|null the DSN string or null if can not find it |
| 602 | 602 | */ |
| 603 | - protected function getDsnValueFromConfig(){ |
|
| 603 | + protected function getDsnValueFromConfig() { |
|
| 604 | 604 | $dsn = null; |
| 605 | 605 | $config = $this->getDatabaseConfiguration(); |
| 606 | - if (! empty($config)){ |
|
| 606 | + if (!empty($config)) { |
|
| 607 | 607 | $driver = $config['driver']; |
| 608 | 608 | $driverDsnMap = array( |
| 609 | 609 | 'mysql' => $this->getDsnValueForDriver('mysql'), |
@@ -611,7 +611,7 @@ discard block |
||
| 611 | 611 | 'sqlite' => $this->getDsnValueForDriver('sqlite'), |
| 612 | 612 | 'oracle' => $this->getDsnValueForDriver('oracle') |
| 613 | 613 | ); |
| 614 | - if (isset($driverDsnMap[$driver])){ |
|
| 614 | + if (isset($driverDsnMap[$driver])) { |
|
| 615 | 615 | $dsn = $driverDsnMap[$driver]; |
| 616 | 616 | } |
| 617 | 617 | } |
@@ -623,17 +623,17 @@ discard block |
||
| 623 | 623 | * @param string $driver the driver name |
| 624 | 624 | * @return string|null the dsn name |
| 625 | 625 | */ |
| 626 | - protected function getDsnValueForDriver($driver){ |
|
| 626 | + protected function getDsnValueForDriver($driver) { |
|
| 627 | 627 | $dsn = ''; |
| 628 | 628 | $config = $this->getDatabaseConfiguration(); |
| 629 | - if (empty($config)){ |
|
| 629 | + if (empty($config)) { |
|
| 630 | 630 | return null; |
| 631 | 631 | } |
| 632 | 632 | switch ($driver) { |
| 633 | 633 | case 'mysql': |
| 634 | 634 | case 'pgsql': |
| 635 | 635 | $port = ''; |
| 636 | - if (! empty($config['port'])) { |
|
| 636 | + if (!empty($config['port'])) { |
|
| 637 | 637 | $port = 'port=' . $config['port'] . ';'; |
| 638 | 638 | } |
| 639 | 639 | $dsn = $driver . ':host=' . $config['hostname'] . ';' . $port . 'dbname=' . $config['database']; |
@@ -643,10 +643,10 @@ discard block |
||
| 643 | 643 | break; |
| 644 | 644 | case 'oracle': |
| 645 | 645 | $port = ''; |
| 646 | - if (! empty($config['port'])) { |
|
| 646 | + if (!empty($config['port'])) { |
|
| 647 | 647 | $port = ':' . $config['port']; |
| 648 | 648 | } |
| 649 | - $dsn = 'oci:dbname=' . $config['hostname'] . $port . '/' . $config['database']; |
|
| 649 | + $dsn = 'oci:dbname=' . $config['hostname'] . $port . '/' . $config['database']; |
|
| 650 | 650 | break; |
| 651 | 651 | } |
| 652 | 652 | return $dsn; |
@@ -658,9 +658,9 @@ discard block |
||
| 658 | 658 | * |
| 659 | 659 | * @return mixed |
| 660 | 660 | */ |
| 661 | - protected function getCacheContentForQuery($query, $returnAsList, $returnAsArray){ |
|
| 661 | + protected function getCacheContentForQuery($query, $returnAsList, $returnAsArray) { |
|
| 662 | 662 | $cacheKey = $this->getCacheKeyForQuery($query, $returnAsList, $returnAsArray); |
| 663 | - if (! is_object($this->cacheInstance)){ |
|
| 663 | + if (!is_object($this->cacheInstance)) { |
|
| 664 | 664 | //can not call method with reference in argument |
| 665 | 665 | //like $this->setCacheInstance(& get_instance()->cache); |
| 666 | 666 | //use temporary variable |
@@ -677,9 +677,9 @@ discard block |
||
| 677 | 677 | * @param mixed $result the query result to save |
| 678 | 678 | * @param int $expire the cache TTL |
| 679 | 679 | */ |
| 680 | - protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
| 681 | - $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
|
| 682 | - if (! is_object($this->cacheInstance)){ |
|
| 680 | + protected function setCacheContentForQuery($query, $key, $result, $expire) { |
|
| 681 | + $this->logger->info('Save the result for query [' . $query . '] into cache for future use'); |
|
| 682 | + if (!is_object($this->cacheInstance)) { |
|
| 683 | 683 | //can not call method with reference in argument |
| 684 | 684 | //like $this->setCacheInstance(& get_instance()->cache); |
| 685 | 685 | //use temporary variable |
@@ -696,7 +696,7 @@ discard block |
||
| 696 | 696 | * |
| 697 | 697 | * @return string |
| 698 | 698 | */ |
| 699 | - protected function getCacheKeyForQuery($query, $returnAsList, $returnAsArray){ |
|
| 699 | + protected function getCacheKeyForQuery($query, $returnAsList, $returnAsArray) { |
|
| 700 | 700 | return md5($query . $returnAsList . $returnAsArray); |
| 701 | 701 | } |
| 702 | 702 | |
@@ -710,12 +710,12 @@ discard block |
||
| 710 | 710 | * |
| 711 | 711 | * @return object this current instance |
| 712 | 712 | */ |
| 713 | - protected function setDependencyInstanceFromParamOrCreate($name, $instance = null, $loadClassName = null, $loadClassePath = 'classes'){ |
|
| 714 | - if ($instance !== null){ |
|
| 713 | + protected function setDependencyInstanceFromParamOrCreate($name, $instance = null, $loadClassName = null, $loadClassePath = 'classes') { |
|
| 714 | + if ($instance !== null) { |
|
| 715 | 715 | $this->{$name} = $instance; |
| 716 | 716 | return $this; |
| 717 | 717 | } |
| 718 | - $this->{$name} =& class_loader($loadClassName, $loadClassePath); |
|
| 718 | + $this->{$name} = & class_loader($loadClassName, $loadClassePath); |
|
| 719 | 719 | return $this; |
| 720 | 720 | } |
| 721 | 721 | |
@@ -725,9 +725,9 @@ discard block |
||
| 725 | 725 | * |
| 726 | 726 | * @return object this current instance |
| 727 | 727 | */ |
| 728 | - protected function setLoggerFromParamOrCreate(Log $logger = null){ |
|
| 728 | + protected function setLoggerFromParamOrCreate(Log $logger = null) { |
|
| 729 | 729 | $this->setDependencyInstanceFromParamOrCreate('logger', $logger, 'Log', 'classes'); |
| 730 | - if ($logger === null){ |
|
| 730 | + if ($logger === null) { |
|
| 731 | 731 | $this->logger->setLogger('Library::Database'); |
| 732 | 732 | } |
| 733 | 733 | return $this; |
@@ -736,7 +736,7 @@ discard block |
||
| 736 | 736 | /** |
| 737 | 737 | * Reset the database class attributs to the initail values before each query. |
| 738 | 738 | */ |
| 739 | - private function reset(){ |
|
| 739 | + private function reset() { |
|
| 740 | 740 | //query builder reset |
| 741 | 741 | $this->queryBuilder->reset(); |
| 742 | 742 | $this->numRows = 0; |
@@ -749,7 +749,7 @@ discard block |
||
| 749 | 749 | /** |
| 750 | 750 | * The class destructor |
| 751 | 751 | */ |
| 752 | - public function __destruct(){ |
|
| 752 | + public function __destruct() { |
|
| 753 | 753 | $this->pdo = null; |
| 754 | 754 | } |
| 755 | 755 | |
@@ -156,8 +156,7 @@ |
||
| 156 | 156 | $this->updateQueryBuilderAndRunnerProperties(); |
| 157 | 157 | |
| 158 | 158 | return is_object($this->pdo); |
| 159 | - } |
|
| 160 | - catch (PDOException $e){ |
|
| 159 | + } catch (PDOException $e){ |
|
| 161 | 160 | $this->logger->fatal($e->getMessage()); |
| 162 | 161 | show_error('Cannot connect to Database.'); |
| 163 | 162 | return false; |