@@ -1,292 +1,292 @@ |
||
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 | - error_reporting(E_ALL | E_STRICT); |
|
27 | - ini_set('display_errors', 1); |
|
26 | + error_reporting(E_ALL | E_STRICT); |
|
27 | + ini_set('display_errors', 1); |
|
28 | 28 | |
29 | - /** |
|
30 | - * the directory separator, under windows it is \ and unix, linux / |
|
31 | - */ |
|
32 | - define('DS', DIRECTORY_SEPARATOR); |
|
29 | + /** |
|
30 | + * the directory separator, under windows it is \ and unix, linux / |
|
31 | + */ |
|
32 | + define('DS', DIRECTORY_SEPARATOR); |
|
33 | 33 | |
34 | - /** |
|
35 | - * The root directory of the application. |
|
36 | - * |
|
37 | - * you can place this directory outside of your web directory, for example "/home/your_app", etc. |
|
38 | - */ |
|
39 | - define('ROOT_PATH', dirname(realpath(__FILE__)) . DS . '..' . DS); |
|
34 | + /** |
|
35 | + * The root directory of the application. |
|
36 | + * |
|
37 | + * you can place this directory outside of your web directory, for example "/home/your_app", etc. |
|
38 | + */ |
|
39 | + define('ROOT_PATH', dirname(realpath(__FILE__)) . DS . '..' . DS); |
|
40 | 40 | |
41 | - //tests dir path |
|
42 | - define('TESTS_PATH', dirname(realpath(__FILE__)) . DS); |
|
41 | + //tests dir path |
|
42 | + define('TESTS_PATH', dirname(realpath(__FILE__)) . DS); |
|
43 | 43 | |
44 | 44 | |
45 | - /** |
|
46 | - * The path to the directory. |
|
47 | - * |
|
48 | - * That contains your static files (javascript, css, images, etc.) |
|
49 | - * Note: the path must be relative to the file index.php (the front-end controller). |
|
50 | - */ |
|
51 | - define('ASSETS_PATH', 'assets/'); |
|
45 | + /** |
|
46 | + * The path to the directory. |
|
47 | + * |
|
48 | + * That contains your static files (javascript, css, images, etc.) |
|
49 | + * Note: the path must be relative to the file index.php (the front-end controller). |
|
50 | + */ |
|
51 | + define('ASSETS_PATH', 'assets/'); |
|
52 | 52 | |
53 | - /** |
|
54 | - * The path to the directory of your cache files. |
|
55 | - * |
|
56 | - * This feature is available currently for database and views. |
|
57 | - */ |
|
58 | - define('CACHE_PATH', ROOT_PATH . 'cache' . DS); |
|
53 | + /** |
|
54 | + * The path to the directory of your cache files. |
|
55 | + * |
|
56 | + * This feature is available currently for database and views. |
|
57 | + */ |
|
58 | + define('CACHE_PATH', ROOT_PATH . 'cache' . DS); |
|
59 | 59 | |
60 | - /** |
|
61 | - * Custom application path for tests |
|
62 | - */ |
|
63 | - define('APPS_PATH', TESTS_PATH .'hmvc' . DS); |
|
60 | + /** |
|
61 | + * Custom application path for tests |
|
62 | + */ |
|
63 | + define('APPS_PATH', TESTS_PATH .'hmvc' . DS); |
|
64 | 64 | |
65 | - /** |
|
66 | - * The path to the controller directory of your application. |
|
67 | - * |
|
68 | - * If you already know the MVC architecture you know what a controller means; |
|
69 | - * it is he who makes the business logic of your application in general. |
|
70 | - */ |
|
71 | - define('APPS_CONTROLLER_PATH', APPS_PATH . 'controllers' . DS); |
|
65 | + /** |
|
66 | + * The path to the controller directory of your application. |
|
67 | + * |
|
68 | + * If you already know the MVC architecture you know what a controller means; |
|
69 | + * it is he who makes the business logic of your application in general. |
|
70 | + */ |
|
71 | + define('APPS_CONTROLLER_PATH', APPS_PATH . 'controllers' . DS); |
|
72 | 72 | |
73 | - /** |
|
74 | - * The path to the directory of your model classes of your application. |
|
75 | - * |
|
76 | - * If you already know the MVC architecture you know what a model means; |
|
77 | - * it's the one who interacts with the database, in one word persistent data from your application. |
|
78 | - */ |
|
79 | - define('APPS_MODEL_PATH', APPS_PATH . 'models' . DS); |
|
73 | + /** |
|
74 | + * The path to the directory of your model classes of your application. |
|
75 | + * |
|
76 | + * If you already know the MVC architecture you know what a model means; |
|
77 | + * it's the one who interacts with the database, in one word persistent data from your application. |
|
78 | + */ |
|
79 | + define('APPS_MODEL_PATH', APPS_PATH . 'models' . DS); |
|
80 | 80 | |
81 | - /** |
|
82 | - * The path to the directory of your views. |
|
83 | - * |
|
84 | - * If you already know the MVC architecture you know what a view means, |
|
85 | - * a view is just a user interface (html page, form, etc.) that is to say |
|
86 | - * everything displayed in the browser interface, etc. |
|
87 | - */ |
|
88 | - define('APPS_VIEWS_PATH', APPS_PATH . 'views' . DS); |
|
81 | + /** |
|
82 | + * The path to the directory of your views. |
|
83 | + * |
|
84 | + * If you already know the MVC architecture you know what a view means, |
|
85 | + * a view is just a user interface (html page, form, etc.) that is to say |
|
86 | + * everything displayed in the browser interface, etc. |
|
87 | + */ |
|
88 | + define('APPS_VIEWS_PATH', APPS_PATH . 'views' . DS); |
|
89 | 89 | |
90 | - /** |
|
91 | - * The path to the configuration directory. |
|
92 | - * |
|
93 | - * That contains most of the configuration files for your |
|
94 | - * application (database, class loading file, functions, etc.) |
|
95 | - */ |
|
96 | - define('CONFIG_PATH', ROOT_PATH . 'config' . DS); |
|
90 | + /** |
|
91 | + * The path to the configuration directory. |
|
92 | + * |
|
93 | + * That contains most of the configuration files for your |
|
94 | + * application (database, class loading file, functions, etc.) |
|
95 | + */ |
|
96 | + define('CONFIG_PATH', ROOT_PATH . 'config' . DS); |
|
97 | 97 | |
98 | - /** |
|
99 | - * The core directory |
|
100 | - * |
|
101 | - * It is recommended to put this folder out of the web directory of your server and |
|
102 | - * you should not change its content because in case of update you could lose the modified files. |
|
103 | - */ |
|
104 | - define('CORE_PATH', ROOT_PATH . 'core' . DS); |
|
98 | + /** |
|
99 | + * The core directory |
|
100 | + * |
|
101 | + * It is recommended to put this folder out of the web directory of your server and |
|
102 | + * you should not change its content because in case of update you could lose the modified files. |
|
103 | + */ |
|
104 | + define('CORE_PATH', ROOT_PATH . 'core' . DS); |
|
105 | 105 | |
106 | - /** |
|
107 | - * The path to the directory of core classes that used by the system. |
|
108 | - * |
|
109 | - * It contains PHP classes that are used by the framework internally. |
|
110 | - */ |
|
111 | - define('CORE_CLASSES_PATH', CORE_PATH . 'classes' . DS); |
|
106 | + /** |
|
107 | + * The path to the directory of core classes that used by the system. |
|
108 | + * |
|
109 | + * It contains PHP classes that are used by the framework internally. |
|
110 | + */ |
|
111 | + define('CORE_CLASSES_PATH', CORE_PATH . 'classes' . DS); |
|
112 | 112 | |
113 | - /** |
|
114 | - * The path to the directory of core classes for the cache used by the system. |
|
115 | - * |
|
116 | - * It contains PHP classes for the cache drivers. |
|
117 | - */ |
|
118 | - define('CORE_CLASSES_CACHE_PATH', CORE_CLASSES_PATH . 'cache' . DS); |
|
113 | + /** |
|
114 | + * The path to the directory of core classes for the cache used by the system. |
|
115 | + * |
|
116 | + * It contains PHP classes for the cache drivers. |
|
117 | + */ |
|
118 | + define('CORE_CLASSES_CACHE_PATH', CORE_CLASSES_PATH . 'cache' . DS); |
|
119 | 119 | |
120 | 120 | /** |
121 | - * The path to the directory of core classes for the database used by the system. |
|
122 | - * |
|
123 | - * It contains PHP classes for the database library, drivers, etc. |
|
124 | - */ |
|
125 | - define('CORE_CLASSES_DATABASE_PATH', CORE_CLASSES_PATH . 'database' . DS); |
|
121 | + * The path to the directory of core classes for the database used by the system. |
|
122 | + * |
|
123 | + * It contains PHP classes for the database library, drivers, etc. |
|
124 | + */ |
|
125 | + define('CORE_CLASSES_DATABASE_PATH', CORE_CLASSES_PATH . 'database' . DS); |
|
126 | 126 | |
127 | - /** |
|
128 | - * The path to the directory of core classes for the model used by the system. |
|
129 | - * |
|
130 | - * It contains PHP classes for the models. |
|
131 | - */ |
|
132 | - define('CORE_CLASSES_MODEL_PATH', CORE_CLASSES_PATH . 'model' . DS); |
|
127 | + /** |
|
128 | + * The path to the directory of core classes for the model used by the system. |
|
129 | + * |
|
130 | + * It contains PHP classes for the models. |
|
131 | + */ |
|
132 | + define('CORE_CLASSES_MODEL_PATH', CORE_CLASSES_PATH . 'model' . DS); |
|
133 | 133 | |
134 | - /** |
|
135 | - * The path to the directory of functions or helper systems. |
|
136 | - * |
|
137 | - * It contains PHP functions that perform a particular task: character string processing, URL, etc. |
|
138 | - */ |
|
139 | - define('CORE_FUNCTIONS_PATH', CORE_PATH . 'functions' . DS); |
|
134 | + /** |
|
135 | + * The path to the directory of functions or helper systems. |
|
136 | + * |
|
137 | + * It contains PHP functions that perform a particular task: character string processing, URL, etc. |
|
138 | + */ |
|
139 | + define('CORE_FUNCTIONS_PATH', CORE_PATH . 'functions' . DS); |
|
140 | 140 | |
141 | - /** |
|
142 | - * The path to the core directory of languages files. |
|
143 | - * |
|
144 | - */ |
|
145 | - define('CORE_LANG_PATH', CORE_PATH . 'lang' . DS); |
|
141 | + /** |
|
142 | + * The path to the core directory of languages files. |
|
143 | + * |
|
144 | + */ |
|
145 | + define('CORE_LANG_PATH', CORE_PATH . 'lang' . DS); |
|
146 | 146 | |
147 | - /** |
|
148 | - * The path to the system library directory. |
|
149 | - * |
|
150 | - * Which contains the libraries most often used in your web application, as for the |
|
151 | - * core directory it is advisable to put it out of the root directory of your application. |
|
152 | - */ |
|
153 | - define('CORE_LIBRARY_PATH', CORE_PATH . 'libraries' . DS); |
|
147 | + /** |
|
148 | + * The path to the system library directory. |
|
149 | + * |
|
150 | + * Which contains the libraries most often used in your web application, as for the |
|
151 | + * core directory it is advisable to put it out of the root directory of your application. |
|
152 | + */ |
|
153 | + define('CORE_LIBRARY_PATH', CORE_PATH . 'libraries' . DS); |
|
154 | 154 | |
155 | - /** |
|
156 | - * The path to the system view directory. |
|
157 | - * |
|
158 | - * That contains the views used for the system, such as error messages, and so on. |
|
159 | - */ |
|
160 | - define('CORE_VIEWS_PATH', CORE_PATH . 'views' . DS); |
|
155 | + /** |
|
156 | + * The path to the system view directory. |
|
157 | + * |
|
158 | + * That contains the views used for the system, such as error messages, and so on. |
|
159 | + */ |
|
160 | + define('CORE_VIEWS_PATH', CORE_PATH . 'views' . DS); |
|
161 | 161 | |
162 | - /** |
|
163 | - * The path to the directory of your PHP personal functions or helper. |
|
164 | - * |
|
165 | - * It contains your PHP functions that perform a particular task: utilities, etc. |
|
166 | - * Note: Do not put your personal functions or helpers in the system functions directory, |
|
167 | - * because if you update the system you may lose them. |
|
168 | - */ |
|
169 | - define('FUNCTIONS_PATH', ROOT_PATH . 'functions' . DS); |
|
162 | + /** |
|
163 | + * The path to the directory of your PHP personal functions or helper. |
|
164 | + * |
|
165 | + * It contains your PHP functions that perform a particular task: utilities, etc. |
|
166 | + * Note: Do not put your personal functions or helpers in the system functions directory, |
|
167 | + * because if you update the system you may lose them. |
|
168 | + */ |
|
169 | + define('FUNCTIONS_PATH', ROOT_PATH . 'functions' . DS); |
|
170 | 170 | |
171 | - /** |
|
172 | - * The path to the app directory of personal language. |
|
173 | - * |
|
174 | - * This feature is not yet available. |
|
175 | - * You can help us do this if you are nice or wish to see the developed framework. |
|
176 | - */ |
|
177 | - define('APP_LANG_PATH', ROOT_PATH . 'lang' . DS); |
|
171 | + /** |
|
172 | + * The path to the app directory of personal language. |
|
173 | + * |
|
174 | + * This feature is not yet available. |
|
175 | + * You can help us do this if you are nice or wish to see the developed framework. |
|
176 | + */ |
|
177 | + define('APP_LANG_PATH', ROOT_PATH . 'lang' . DS); |
|
178 | 178 | |
179 | - /** |
|
180 | - * The path to the directory of your personal libraries |
|
181 | - * |
|
182 | - * It contains your PHP classes, package, etc. |
|
183 | - * Note: you should not put your personal libraries in the system library directory, |
|
184 | - * because it is recalled in case of updating the system you might have surprises. |
|
185 | - */ |
|
186 | - define('LIBRARY_PATH', ROOT_PATH . 'libraries' . DS); |
|
179 | + /** |
|
180 | + * The path to the directory of your personal libraries |
|
181 | + * |
|
182 | + * It contains your PHP classes, package, etc. |
|
183 | + * Note: you should not put your personal libraries in the system library directory, |
|
184 | + * because it is recalled in case of updating the system you might have surprises. |
|
185 | + */ |
|
186 | + define('LIBRARY_PATH', ROOT_PATH . 'libraries' . DS); |
|
187 | 187 | |
188 | - /** |
|
189 | - * The path to the directory that contains the log files. |
|
190 | - * |
|
191 | - * Note: This directory must be available in writing and if possible must have as owner the user who launches your web server, |
|
192 | - * under unix or linux most often with the apache web server it is "www-data" or "httpd" even "nobody" for more |
|
193 | - * details see the documentation of your web server. |
|
194 | - * Example for Unix or linux with apache web server: |
|
195 | - * # chmod -R 700 /path/to/your/logs/directory/ |
|
196 | - * # chown -R www-data:www-data /path/to/your/logs/directory/ |
|
197 | - */ |
|
198 | - define('LOGS_PATH', ROOT_PATH . 'logs' . DS); |
|
188 | + /** |
|
189 | + * The path to the directory that contains the log files. |
|
190 | + * |
|
191 | + * Note: This directory must be available in writing and if possible must have as owner the user who launches your web server, |
|
192 | + * under unix or linux most often with the apache web server it is "www-data" or "httpd" even "nobody" for more |
|
193 | + * details see the documentation of your web server. |
|
194 | + * Example for Unix or linux with apache web server: |
|
195 | + * # chmod -R 700 /path/to/your/logs/directory/ |
|
196 | + * # chown -R www-data:www-data /path/to/your/logs/directory/ |
|
197 | + */ |
|
198 | + define('LOGS_PATH', ROOT_PATH . 'logs' . DS); |
|
199 | 199 | |
200 | - /** |
|
201 | - * The path to the modules directory. |
|
202 | - * |
|
203 | - * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
|
204 | - * in HMVC architecture (hierichical, controllers, models, views). |
|
205 | - */ |
|
206 | - define('MODULE_PATH', dirname(realpath(__FILE__)) . DS .'hmvc' . DS . 'modules' . DS); |
|
200 | + /** |
|
201 | + * The path to the modules directory. |
|
202 | + * |
|
203 | + * It contains your modules used files (config, controllers, libraries, etc.) that is to say which contains your files of the modules, |
|
204 | + * in HMVC architecture (hierichical, controllers, models, views). |
|
205 | + */ |
|
206 | + define('MODULE_PATH', dirname(realpath(__FILE__)) . DS .'hmvc' . DS . 'modules' . DS); |
|
207 | 207 | |
208 | - /** |
|
209 | - * The path to the directory of sources external to your application. |
|
210 | - * |
|
211 | - * If you have already used "composer" you know what that means. |
|
212 | - */ |
|
213 | - define('VENDOR_PATH', ROOT_PATH . 'vendor' . DS); |
|
208 | + /** |
|
209 | + * The path to the directory of sources external to your application. |
|
210 | + * |
|
211 | + * If you have already used "composer" you know what that means. |
|
212 | + */ |
|
213 | + define('VENDOR_PATH', ROOT_PATH . 'vendor' . DS); |
|
214 | 214 | |
215 | - /** |
|
216 | - * The front controller of your application. |
|
217 | - * |
|
218 | - * "index.php" it is through this file that all the requests come, there is a possibility to hidden it in the url of |
|
219 | - * your application by using the rewrite module URL of your web server . |
|
220 | - * For example, under apache web server, there is a configuration example file that is located at the root |
|
221 | - * of your framework folder : "htaccess.txt" rename it to ".htaccess". |
|
222 | - */ |
|
223 | - define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME)); |
|
215 | + /** |
|
216 | + * The front controller of your application. |
|
217 | + * |
|
218 | + * "index.php" it is through this file that all the requests come, there is a possibility to hidden it in the url of |
|
219 | + * your application by using the rewrite module URL of your web server . |
|
220 | + * For example, under apache web server, there is a configuration example file that is located at the root |
|
221 | + * of your framework folder : "htaccess.txt" rename it to ".htaccess". |
|
222 | + */ |
|
223 | + define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME)); |
|
224 | 224 | |
225 | - /** |
|
226 | - * Check if user run the application under CLI |
|
227 | - */ |
|
228 | - define('IS_CLI', stripos('cli', php_sapi_name()) !== false); |
|
225 | + /** |
|
226 | + * Check if user run the application under CLI |
|
227 | + */ |
|
228 | + define('IS_CLI', stripos('cli', php_sapi_name()) !== false); |
|
229 | 229 | |
230 | - /** |
|
231 | - * The environment of your application (production, test, development). |
|
232 | - * |
|
233 | - * if your application is still in development you use the value "development" |
|
234 | - * so you will have the display of the error messages, etc. |
|
235 | - * Once you finish the development of your application that is to put it online |
|
236 | - * you change this value to "production" or "testing", in this case there will be deactivation of error messages, |
|
237 | - * the loading of the system, will be fast. |
|
238 | - */ |
|
239 | - define('ENVIRONMENT', 'testing'); |
|
230 | + /** |
|
231 | + * The environment of your application (production, test, development). |
|
232 | + * |
|
233 | + * if your application is still in development you use the value "development" |
|
234 | + * so you will have the display of the error messages, etc. |
|
235 | + * Once you finish the development of your application that is to put it online |
|
236 | + * you change this value to "production" or "testing", in this case there will be deactivation of error messages, |
|
237 | + * the loading of the system, will be fast. |
|
238 | + */ |
|
239 | + define('ENVIRONMENT', 'testing'); |
|
240 | 240 | |
241 | 241 | |
242 | - //Fix to allow test as if application is running in CLI mode $_SESSION global variable is not available |
|
243 | - $_SESSION = array(); |
|
242 | + //Fix to allow test as if application is running in CLI mode $_SESSION global variable is not available |
|
243 | + $_SESSION = array(); |
|
244 | 244 | |
245 | - if(! isset($_SERVER['REMOTE_ADDR'])){ |
|
246 | - $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
|
247 | - } |
|
245 | + if(! isset($_SERVER['REMOTE_ADDR'])){ |
|
246 | + $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
|
247 | + } |
|
248 | 248 | |
249 | - //check for composer autoload file if exists include it |
|
250 | - if (file_exists(VENDOR_PATH . 'autoload.php')){ |
|
251 | - require_once VENDOR_PATH . 'autoload.php'; |
|
249 | + //check for composer autoload file if exists include it |
|
250 | + if (file_exists(VENDOR_PATH . 'autoload.php')){ |
|
251 | + require_once VENDOR_PATH . 'autoload.php'; |
|
252 | 252 | |
253 | - //define the class alias for vstream |
|
254 | - class_alias('org\bovigo\vfs\vfsStream', 'vfsStream'); |
|
255 | - class_alias('org\bovigo\vfs\vfsStreamDirectory', 'vfsStreamDirectory'); |
|
256 | - class_alias('org\bovigo\vfs\vfsStreamWrapper', 'vfsStreamWrapper'); |
|
253 | + //define the class alias for vstream |
|
254 | + class_alias('org\bovigo\vfs\vfsStream', 'vfsStream'); |
|
255 | + class_alias('org\bovigo\vfs\vfsStreamDirectory', 'vfsStreamDirectory'); |
|
256 | + class_alias('org\bovigo\vfs\vfsStreamWrapper', 'vfsStreamWrapper'); |
|
257 | 257 | |
258 | - } |
|
258 | + } |
|
259 | 259 | |
260 | - //require autoloader for test |
|
261 | - require_once 'include/autoloader.php'; |
|
260 | + //require autoloader for test |
|
261 | + require_once 'include/autoloader.php'; |
|
262 | 262 | |
263 | 263 | |
264 | 264 | |
265 | - //grap from core/common.php functions and mock some functions for tests |
|
266 | - require_once 'include/common.php'; |
|
265 | + //grap from core/common.php functions and mock some functions for tests |
|
266 | + require_once 'include/common.php'; |
|
267 | 267 | |
268 | - //Some utilities functions, classes, etc. |
|
269 | - require_once 'include/testsUtil.php'; |
|
268 | + //Some utilities functions, classes, etc. |
|
269 | + require_once 'include/testsUtil.php'; |
|
270 | 270 | |
271 | - /** |
|
272 | - * Setting of the PHP error message handling function |
|
273 | - */ |
|
274 | - set_error_handler('php_error_handler'); |
|
271 | + /** |
|
272 | + * Setting of the PHP error message handling function |
|
273 | + */ |
|
274 | + set_error_handler('php_error_handler'); |
|
275 | 275 | |
276 | - /** |
|
277 | - * Setting of the PHP error exception handling function |
|
278 | - */ |
|
279 | - set_exception_handler('php_exception_handler'); |
|
276 | + /** |
|
277 | + * Setting of the PHP error exception handling function |
|
278 | + */ |
|
279 | + set_exception_handler('php_exception_handler'); |
|
280 | 280 | |
281 | - /** |
|
282 | - * Setting of the PHP shutdown handling function |
|
283 | - */ |
|
284 | - register_shutdown_function('php_shudown_handler'); |
|
281 | + /** |
|
282 | + * Setting of the PHP shutdown handling function |
|
283 | + */ |
|
284 | + register_shutdown_function('php_shudown_handler'); |
|
285 | 285 | |
286 | - /** |
|
287 | - * Register the tests autoload |
|
288 | - */ |
|
289 | - spl_autoload_register('tests_autoload'); |
|
286 | + /** |
|
287 | + * Register the tests autoload |
|
288 | + */ |
|
289 | + spl_autoload_register('tests_autoload'); |
|
290 | 290 | |
291 | 291 | |
292 | - |
|
293 | 292 | \ No newline at end of file |
293 | + |
|
294 | 294 | \ No newline at end of file |
@@ -1,226 +1,226 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | - use PHPUnit\Framework\TestCase; |
|
3 | + use PHPUnit\Framework\TestCase; |
|
4 | 4 | |
5 | - class DBSessionHandlerTest extends TestCase |
|
6 | - { |
|
5 | + class DBSessionHandlerTest extends TestCase |
|
6 | + { |
|
7 | 7 | |
8 | - private $db = null; |
|
8 | + private $db = null; |
|
9 | 9 | |
10 | - private $model = null; |
|
10 | + private $model = null; |
|
11 | 11 | |
12 | - private $secret = 'bXlzZWNyZXQ'; |
|
12 | + private $secret = 'bXlzZWNyZXQ'; |
|
13 | 13 | |
14 | - private static $config = null; |
|
14 | + private static $config = null; |
|
15 | 15 | |
16 | - public function __construct(){ |
|
17 | - $this->db = new Database(array( |
|
18 | - 'driver' => 'sqlite', |
|
19 | - 'database' => TESTS_PATH . 'assets/db_tests.db', |
|
20 | - 'charset' => 'utf8', |
|
21 | - 'collation' => 'utf8_general_ci', |
|
22 | - )); |
|
16 | + public function __construct(){ |
|
17 | + $this->db = new Database(array( |
|
18 | + 'driver' => 'sqlite', |
|
19 | + 'database' => TESTS_PATH . 'assets/db_tests.db', |
|
20 | + 'charset' => 'utf8', |
|
21 | + 'collation' => 'utf8_general_ci', |
|
22 | + )); |
|
23 | 23 | $qr = new DatabaseQueryRunner($this->db->getPdo()); |
24 | 24 | $qr->setBenchmark(new Benchmark()); |
25 | 25 | $qr->setDriver('sqlite'); |
26 | 26 | $this->db->setQueryRunner($qr); |
27 | - } |
|
27 | + } |
|
28 | 28 | |
29 | - public static function setUpBeforeClass() |
|
30 | - { |
|
31 | - require APPS_MODEL_PATH . 'DBSessionModel.php'; |
|
32 | - self::$config = new Config(); |
|
33 | - self::$config->init(); |
|
34 | - } |
|
29 | + public static function setUpBeforeClass() |
|
30 | + { |
|
31 | + require APPS_MODEL_PATH . 'DBSessionModel.php'; |
|
32 | + self::$config = new Config(); |
|
33 | + self::$config->init(); |
|
34 | + } |
|
35 | 35 | |
36 | 36 | |
37 | - public static function tearDownAfterClass() |
|
38 | - { |
|
37 | + public static function tearDownAfterClass() |
|
38 | + { |
|
39 | 39 | |
40 | - } |
|
40 | + } |
|
41 | 41 | |
42 | - protected function setUp() |
|
43 | - { |
|
44 | - $this->model = new DBSessionModel($this->db); |
|
42 | + protected function setUp() |
|
43 | + { |
|
44 | + $this->model = new DBSessionModel($this->db); |
|
45 | 45 | //to prevent old data conflict |
46 | - $this->model->truncate(); |
|
47 | - } |
|
46 | + $this->model->truncate(); |
|
47 | + } |
|
48 | 48 | |
49 | - protected function tearDown() |
|
50 | - { |
|
51 | - } |
|
49 | + protected function tearDown() |
|
50 | + { |
|
51 | + } |
|
52 | 52 | |
53 | 53 | |
54 | 54 | |
55 | - public function testUsingSessionConfiguration(){ |
|
56 | - //using value in the configuration |
|
57 | - self::$config->set('session_save_path', 'DBSessionModel'); |
|
58 | - self::$config->set('session_secret', $this->secret); |
|
59 | - $dbsh = new DBSessionHandler(); |
|
60 | - //assign Database instance manually |
|
61 | - $o = &get_instance(); |
|
62 | - $o->database = $this->db; |
|
63 | - |
|
64 | - $this->assertTrue($dbsh->open(null, null)); |
|
65 | - $this->assertTrue($dbsh->close()); |
|
66 | - $this->assertNull($dbsh->read('foo')); |
|
67 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
68 | - $this->assertNotEmpty($dbsh->read('foo')); |
|
69 | - $this->assertEquals($dbsh->read('foo'), '444'); |
|
70 | - //do update of existing data |
|
71 | - $this->assertTrue($dbsh->write('foo', '445')); |
|
72 | - $this->assertEquals($dbsh->read('foo'), '445'); |
|
73 | - $this->assertTrue($dbsh->destroy('foo')); |
|
74 | - $this->assertNull($dbsh->read('foo')); |
|
75 | - $this->assertTrue($dbsh->gc(13)); |
|
76 | - $encoded = $dbsh->encode('foo'); |
|
77 | - $this->assertNotEmpty($encoded); |
|
78 | - $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
79 | - } |
|
80 | - |
|
81 | - public function testWhenDataIsExpired(){ |
|
82 | - $dbsh = new DBSessionHandler($this->model); |
|
83 | - $dbsh->setSessionSecret($this->secret); |
|
84 | - |
|
85 | - $this->assertTrue($dbsh->open(null, null)); |
|
86 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
87 | - $this->assertNotEmpty($dbsh->read('foo')); |
|
88 | - $this->assertEquals($dbsh->read('foo'), '444'); |
|
89 | - //put it in expired |
|
90 | - $this->model->update('foo', array('s_time' => 1234567)); |
|
91 | - $this->assertNull($dbsh->read('foo')); |
|
92 | - } |
|
93 | - |
|
94 | - public function testWhenDataAlreadyExistDoUpdate(){ |
|
95 | - $dbsh = new DBSessionHandler($this->model); |
|
96 | - $dbsh->setSessionSecret($this->secret); |
|
97 | - |
|
98 | - $this->assertTrue($dbsh->open(null, null)); |
|
99 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
100 | - $this->assertNotEmpty($dbsh->read('foo')); |
|
101 | - $this->assertEquals($dbsh->read('foo'), '444'); |
|
102 | - //do update of existing data |
|
103 | - $this->assertTrue($dbsh->write('foo', '445')); |
|
104 | - $this->assertEquals($dbsh->read('foo'), '445'); |
|
105 | - } |
|
106 | - |
|
107 | - public function testUsingCustomModelInstance(){ |
|
108 | - $dbsh = new DBSessionHandler($this->model); |
|
109 | - $dbsh->setSessionSecret($this->secret); |
|
110 | - |
|
111 | - $this->assertTrue($dbsh->open(null, null)); |
|
112 | - $this->assertTrue($dbsh->close()); |
|
113 | - $this->assertNull($dbsh->read('foo')); |
|
114 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
115 | - $this->assertNotEmpty($dbsh->read('foo')); |
|
116 | - $this->assertEquals($dbsh->read('foo'), '444'); |
|
117 | - //put it in expired |
|
118 | - $this->model->update('foo', array('s_time' => 1234567)); |
|
119 | - |
|
120 | - $this->assertNull($dbsh->read('foo')); |
|
121 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
122 | - |
|
123 | - //do update of existing data |
|
124 | - $this->assertTrue($dbsh->write('foo', '445')); |
|
125 | - $this->assertEquals($dbsh->read('foo'), '445'); |
|
126 | - $this->assertTrue($dbsh->destroy('foo')); |
|
127 | - $this->assertNull($dbsh->read('foo')); |
|
128 | - $this->assertTrue($dbsh->gc(13)); |
|
129 | - $encoded = $dbsh->encode('foo'); |
|
130 | - $this->assertNotEmpty($encoded); |
|
131 | - $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - public function testUsingCustomLogInstance(){ |
|
136 | - $dbsh = new DBSessionHandler($this->model, new Log()); |
|
137 | - $dbsh->setSessionSecret($this->secret); |
|
138 | - |
|
139 | - $this->assertTrue($dbsh->open(null, null)); |
|
140 | - $this->assertTrue($dbsh->close()); |
|
141 | - $this->assertNull($dbsh->read('foo')); |
|
142 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
143 | - $this->assertNotEmpty($dbsh->read('foo')); |
|
144 | - $this->assertEquals($dbsh->read('foo'), '444'); |
|
145 | - //put it in expired |
|
146 | - $this->model->update('foo', array('s_time' => 1234567)); |
|
147 | - |
|
148 | - $this->assertNull($dbsh->read('foo')); |
|
149 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
150 | - |
|
151 | - //do update of existing data |
|
152 | - $this->assertTrue($dbsh->write('foo', '445')); |
|
153 | - $this->assertEquals($dbsh->read('foo'), '445'); |
|
154 | - $this->assertTrue($dbsh->destroy('foo')); |
|
155 | - $this->assertNull($dbsh->read('foo')); |
|
156 | - $this->assertTrue($dbsh->gc(13)); |
|
157 | - $encoded = $dbsh->encode('foo'); |
|
158 | - $this->assertNotEmpty($encoded); |
|
159 | - $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
160 | - } |
|
161 | - |
|
162 | - public function testUsingCustomLoaderInstance(){ |
|
163 | - $dbsh = new DBSessionHandler($this->model, null, new Loader()); |
|
164 | - $dbsh->setSessionSecret($this->secret); |
|
165 | - |
|
166 | - $this->assertTrue($dbsh->open(null, null)); |
|
167 | - $this->assertTrue($dbsh->close()); |
|
168 | - $this->assertNull($dbsh->read('foo')); |
|
169 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
170 | - $this->assertNotEmpty($dbsh->read('foo')); |
|
171 | - $this->assertEquals($dbsh->read('foo'), '444'); |
|
172 | - //put it in expired |
|
173 | - $this->model->update('foo', array('s_time' => 1234567)); |
|
174 | - |
|
175 | - $this->assertNull($dbsh->read('foo')); |
|
176 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
177 | - |
|
178 | - //do update of existing data |
|
179 | - $this->assertTrue($dbsh->write('foo', '445')); |
|
180 | - $this->assertEquals($dbsh->read('foo'), '445'); |
|
181 | - $this->assertTrue($dbsh->destroy('foo')); |
|
182 | - $this->assertNull($dbsh->read('foo')); |
|
183 | - $this->assertTrue($dbsh->gc(13)); |
|
184 | - $encoded = $dbsh->encode('foo'); |
|
185 | - $this->assertNotEmpty($encoded); |
|
186 | - $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
187 | - } |
|
188 | - |
|
189 | - |
|
190 | - public function testWhenModelInsanceIsNotSet(){ |
|
191 | - $dbsh = new DBSessionHandler(null, null, new Loader()); |
|
192 | - $dbsh->setSessionSecret($this->secret); |
|
193 | - |
|
194 | - $this->assertTrue($dbsh->open(null, null)); |
|
195 | - $this->assertTrue($dbsh->close()); |
|
196 | - $this->assertNull($dbsh->read('foo')); |
|
197 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
198 | - $this->assertNotEmpty($dbsh->read('foo')); |
|
199 | - $this->assertEquals($dbsh->read('foo'), '444'); |
|
200 | - //put it in expired |
|
201 | - $this->model->update('foo', array('s_time' => 1234567)); |
|
202 | - |
|
203 | - $this->assertNull($dbsh->read('foo')); |
|
204 | - $this->assertTrue($dbsh->write('foo', '444')); |
|
205 | - |
|
206 | - //do update of existing data |
|
207 | - $this->assertTrue($dbsh->write('tnh', '445')); |
|
208 | - $this->assertTrue($dbsh->write('foo', '445')); |
|
209 | - $this->assertEquals($dbsh->read('foo'), '445'); |
|
210 | - $this->assertTrue($dbsh->destroy('foo')); |
|
211 | - $this->assertNull($dbsh->read('foo')); |
|
212 | - $this->assertTrue($dbsh->gc(13)); |
|
213 | - $encoded = $dbsh->encode('foo'); |
|
214 | - $this->assertNotEmpty($encoded); |
|
215 | - $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
216 | - } |
|
217 | - |
|
218 | - public function testWhenModelTableColumnsIsNotSet(){ |
|
219 | - //session table is empty |
|
220 | - $this->model->setSessionTableColumns(array()); |
|
221 | - $dbsh = new DBSessionHandler($this->model); |
|
222 | - $this->assertTrue($dbsh->open(null, null)); |
|
223 | - } |
|
224 | - |
|
225 | - |
|
226 | - } |
|
227 | 55 | \ No newline at end of file |
56 | + public function testUsingSessionConfiguration(){ |
|
57 | + //using value in the configuration |
|
58 | + self::$config->set('session_save_path', 'DBSessionModel'); |
|
59 | + self::$config->set('session_secret', $this->secret); |
|
60 | + $dbsh = new DBSessionHandler(); |
|
61 | + //assign Database instance manually |
|
62 | + $o = &get_instance(); |
|
63 | + $o->database = $this->db; |
|
64 | + |
|
65 | + $this->assertTrue($dbsh->open(null, null)); |
|
66 | + $this->assertTrue($dbsh->close()); |
|
67 | + $this->assertNull($dbsh->read('foo')); |
|
68 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
69 | + $this->assertNotEmpty($dbsh->read('foo')); |
|
70 | + $this->assertEquals($dbsh->read('foo'), '444'); |
|
71 | + //do update of existing data |
|
72 | + $this->assertTrue($dbsh->write('foo', '445')); |
|
73 | + $this->assertEquals($dbsh->read('foo'), '445'); |
|
74 | + $this->assertTrue($dbsh->destroy('foo')); |
|
75 | + $this->assertNull($dbsh->read('foo')); |
|
76 | + $this->assertTrue($dbsh->gc(13)); |
|
77 | + $encoded = $dbsh->encode('foo'); |
|
78 | + $this->assertNotEmpty($encoded); |
|
79 | + $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
80 | + } |
|
81 | + |
|
82 | + public function testWhenDataIsExpired(){ |
|
83 | + $dbsh = new DBSessionHandler($this->model); |
|
84 | + $dbsh->setSessionSecret($this->secret); |
|
85 | + |
|
86 | + $this->assertTrue($dbsh->open(null, null)); |
|
87 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
88 | + $this->assertNotEmpty($dbsh->read('foo')); |
|
89 | + $this->assertEquals($dbsh->read('foo'), '444'); |
|
90 | + //put it in expired |
|
91 | + $this->model->update('foo', array('s_time' => 1234567)); |
|
92 | + $this->assertNull($dbsh->read('foo')); |
|
93 | + } |
|
94 | + |
|
95 | + public function testWhenDataAlreadyExistDoUpdate(){ |
|
96 | + $dbsh = new DBSessionHandler($this->model); |
|
97 | + $dbsh->setSessionSecret($this->secret); |
|
98 | + |
|
99 | + $this->assertTrue($dbsh->open(null, null)); |
|
100 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
101 | + $this->assertNotEmpty($dbsh->read('foo')); |
|
102 | + $this->assertEquals($dbsh->read('foo'), '444'); |
|
103 | + //do update of existing data |
|
104 | + $this->assertTrue($dbsh->write('foo', '445')); |
|
105 | + $this->assertEquals($dbsh->read('foo'), '445'); |
|
106 | + } |
|
107 | + |
|
108 | + public function testUsingCustomModelInstance(){ |
|
109 | + $dbsh = new DBSessionHandler($this->model); |
|
110 | + $dbsh->setSessionSecret($this->secret); |
|
111 | + |
|
112 | + $this->assertTrue($dbsh->open(null, null)); |
|
113 | + $this->assertTrue($dbsh->close()); |
|
114 | + $this->assertNull($dbsh->read('foo')); |
|
115 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
116 | + $this->assertNotEmpty($dbsh->read('foo')); |
|
117 | + $this->assertEquals($dbsh->read('foo'), '444'); |
|
118 | + //put it in expired |
|
119 | + $this->model->update('foo', array('s_time' => 1234567)); |
|
120 | + |
|
121 | + $this->assertNull($dbsh->read('foo')); |
|
122 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
123 | + |
|
124 | + //do update of existing data |
|
125 | + $this->assertTrue($dbsh->write('foo', '445')); |
|
126 | + $this->assertEquals($dbsh->read('foo'), '445'); |
|
127 | + $this->assertTrue($dbsh->destroy('foo')); |
|
128 | + $this->assertNull($dbsh->read('foo')); |
|
129 | + $this->assertTrue($dbsh->gc(13)); |
|
130 | + $encoded = $dbsh->encode('foo'); |
|
131 | + $this->assertNotEmpty($encoded); |
|
132 | + $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
133 | + } |
|
134 | + |
|
135 | + |
|
136 | + public function testUsingCustomLogInstance(){ |
|
137 | + $dbsh = new DBSessionHandler($this->model, new Log()); |
|
138 | + $dbsh->setSessionSecret($this->secret); |
|
139 | + |
|
140 | + $this->assertTrue($dbsh->open(null, null)); |
|
141 | + $this->assertTrue($dbsh->close()); |
|
142 | + $this->assertNull($dbsh->read('foo')); |
|
143 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
144 | + $this->assertNotEmpty($dbsh->read('foo')); |
|
145 | + $this->assertEquals($dbsh->read('foo'), '444'); |
|
146 | + //put it in expired |
|
147 | + $this->model->update('foo', array('s_time' => 1234567)); |
|
148 | + |
|
149 | + $this->assertNull($dbsh->read('foo')); |
|
150 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
151 | + |
|
152 | + //do update of existing data |
|
153 | + $this->assertTrue($dbsh->write('foo', '445')); |
|
154 | + $this->assertEquals($dbsh->read('foo'), '445'); |
|
155 | + $this->assertTrue($dbsh->destroy('foo')); |
|
156 | + $this->assertNull($dbsh->read('foo')); |
|
157 | + $this->assertTrue($dbsh->gc(13)); |
|
158 | + $encoded = $dbsh->encode('foo'); |
|
159 | + $this->assertNotEmpty($encoded); |
|
160 | + $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
161 | + } |
|
162 | + |
|
163 | + public function testUsingCustomLoaderInstance(){ |
|
164 | + $dbsh = new DBSessionHandler($this->model, null, new Loader()); |
|
165 | + $dbsh->setSessionSecret($this->secret); |
|
166 | + |
|
167 | + $this->assertTrue($dbsh->open(null, null)); |
|
168 | + $this->assertTrue($dbsh->close()); |
|
169 | + $this->assertNull($dbsh->read('foo')); |
|
170 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
171 | + $this->assertNotEmpty($dbsh->read('foo')); |
|
172 | + $this->assertEquals($dbsh->read('foo'), '444'); |
|
173 | + //put it in expired |
|
174 | + $this->model->update('foo', array('s_time' => 1234567)); |
|
175 | + |
|
176 | + $this->assertNull($dbsh->read('foo')); |
|
177 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
178 | + |
|
179 | + //do update of existing data |
|
180 | + $this->assertTrue($dbsh->write('foo', '445')); |
|
181 | + $this->assertEquals($dbsh->read('foo'), '445'); |
|
182 | + $this->assertTrue($dbsh->destroy('foo')); |
|
183 | + $this->assertNull($dbsh->read('foo')); |
|
184 | + $this->assertTrue($dbsh->gc(13)); |
|
185 | + $encoded = $dbsh->encode('foo'); |
|
186 | + $this->assertNotEmpty($encoded); |
|
187 | + $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + public function testWhenModelInsanceIsNotSet(){ |
|
192 | + $dbsh = new DBSessionHandler(null, null, new Loader()); |
|
193 | + $dbsh->setSessionSecret($this->secret); |
|
194 | + |
|
195 | + $this->assertTrue($dbsh->open(null, null)); |
|
196 | + $this->assertTrue($dbsh->close()); |
|
197 | + $this->assertNull($dbsh->read('foo')); |
|
198 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
199 | + $this->assertNotEmpty($dbsh->read('foo')); |
|
200 | + $this->assertEquals($dbsh->read('foo'), '444'); |
|
201 | + //put it in expired |
|
202 | + $this->model->update('foo', array('s_time' => 1234567)); |
|
203 | + |
|
204 | + $this->assertNull($dbsh->read('foo')); |
|
205 | + $this->assertTrue($dbsh->write('foo', '444')); |
|
206 | + |
|
207 | + //do update of existing data |
|
208 | + $this->assertTrue($dbsh->write('tnh', '445')); |
|
209 | + $this->assertTrue($dbsh->write('foo', '445')); |
|
210 | + $this->assertEquals($dbsh->read('foo'), '445'); |
|
211 | + $this->assertTrue($dbsh->destroy('foo')); |
|
212 | + $this->assertNull($dbsh->read('foo')); |
|
213 | + $this->assertTrue($dbsh->gc(13)); |
|
214 | + $encoded = $dbsh->encode('foo'); |
|
215 | + $this->assertNotEmpty($encoded); |
|
216 | + $this->assertEquals($dbsh->decode($encoded), 'foo'); |
|
217 | + } |
|
218 | + |
|
219 | + public function testWhenModelTableColumnsIsNotSet(){ |
|
220 | + //session table is empty |
|
221 | + $this->model->setSessionTableColumns(array()); |
|
222 | + $dbsh = new DBSessionHandler($this->model); |
|
223 | + $this->assertTrue($dbsh->open(null, null)); |
|
224 | + } |
|
225 | + |
|
226 | + |
|
227 | + } |
|
228 | 228 | \ No newline at end of file |
@@ -1,54 +1,54 @@ |
||
1 | 1 | <?php |
2 | - //Autoload function |
|
3 | - function tests_autoload($class){ |
|
4 | - $classesMap = array( |
|
5 | - //Caches |
|
6 | - 'ApcCache' => CORE_CLASSES_CACHE_PATH . 'ApcCache.php', |
|
7 | - 'CacheInterface' => CORE_CLASSES_CACHE_PATH . 'CacheInterface.php', |
|
8 | - 'FileCache' => CORE_CLASSES_CACHE_PATH . 'FileCache.php', |
|
9 | - //models |
|
10 | - 'DBSessionHandlerModel' => CORE_CLASSES_MODEL_PATH . 'DBSessionHandlerModel.php', |
|
11 | - 'Model' => CORE_CLASSES_MODEL_PATH . 'Model.php', |
|
12 | - //Core classes |
|
13 | - 'Config' => CORE_CLASSES_PATH . 'Config.php', |
|
14 | - 'Controller' => CORE_CLASSES_PATH . 'Controller.php', |
|
15 | - 'Database' => CORE_CLASSES_DATABASE_PATH . 'Database.php', |
|
16 | - 'DatabaseQueryBuilder' => CORE_CLASSES_DATABASE_PATH . 'DatabaseQueryBuilder.php', |
|
17 | - 'DatabaseQueryResult' => CORE_CLASSES_DATABASE_PATH . 'DatabaseQueryResult.php', |
|
18 | - 'DatabaseQueryRunner' => CORE_CLASSES_DATABASE_PATH . 'DatabaseQueryRunner.php', |
|
19 | - 'DBSessionHandler' => CORE_CLASSES_PATH . 'DBSessionHandler.php', |
|
20 | - 'EventInfo' => CORE_CLASSES_PATH . 'EventInfo.php', |
|
21 | - 'EventDispatcher' => CORE_CLASSES_PATH . 'EventDispatcher.php', |
|
22 | - 'Lang' => CORE_CLASSES_PATH . 'Lang.php', |
|
23 | - 'Loader' => CORE_CLASSES_PATH . 'Loader.php', |
|
24 | - 'Log' => CORE_CLASSES_PATH . 'Log.php', |
|
25 | - 'Module' => CORE_CLASSES_PATH . 'Module.php', |
|
26 | - 'Request' => CORE_CLASSES_PATH . 'Request.php', |
|
27 | - 'Response' => CORE_CLASSES_PATH . 'Response.php', |
|
28 | - 'Router' => CORE_CLASSES_PATH . 'Router.php', |
|
29 | - 'Security' => CORE_CLASSES_PATH . 'Security.php', |
|
30 | - 'Session' => CORE_CLASSES_PATH . 'Session.php', |
|
31 | - 'Url' => CORE_CLASSES_PATH . 'Url.php', |
|
32 | - //Core libraries |
|
33 | - 'Assets' => CORE_LIBRARY_PATH . 'Assets.php', |
|
34 | - 'Benchmark' => CORE_LIBRARY_PATH . 'Benchmark.php', |
|
35 | - 'Browser' => CORE_LIBRARY_PATH . 'Browser.php', |
|
36 | - 'Cookie' => CORE_LIBRARY_PATH . 'Cookie.php', |
|
37 | - 'Email' => CORE_LIBRARY_PATH . 'Email.php', |
|
38 | - 'Form' => CORE_LIBRARY_PATH . 'Form.php', |
|
39 | - 'FormValidation' => CORE_LIBRARY_PATH . 'FormValidation.php', |
|
40 | - 'Html' => CORE_LIBRARY_PATH . 'Html.php', |
|
41 | - 'Pagination' => CORE_LIBRARY_PATH . 'Pagination.php', |
|
42 | - 'PDF' => CORE_LIBRARY_PATH . 'PDF.php', |
|
43 | - 'StringHash' => CORE_LIBRARY_PATH . 'StringHash.php', |
|
44 | - 'Upload' => CORE_LIBRARY_PATH . 'Upload.php', |
|
45 | - ); |
|
46 | - if(isset($classesMap[$class])){ |
|
47 | - if(file_exists($classesMap[$class])){ |
|
48 | - require_once $classesMap[$class]; |
|
49 | - } |
|
50 | - else{ |
|
51 | - echo 'File for class ' . $class . ' not found'; |
|
52 | - } |
|
53 | - } |
|
54 | - } |
|
55 | 2 | \ No newline at end of file |
3 | + //Autoload function |
|
4 | + function tests_autoload($class){ |
|
5 | + $classesMap = array( |
|
6 | + //Caches |
|
7 | + 'ApcCache' => CORE_CLASSES_CACHE_PATH . 'ApcCache.php', |
|
8 | + 'CacheInterface' => CORE_CLASSES_CACHE_PATH . 'CacheInterface.php', |
|
9 | + 'FileCache' => CORE_CLASSES_CACHE_PATH . 'FileCache.php', |
|
10 | + //models |
|
11 | + 'DBSessionHandlerModel' => CORE_CLASSES_MODEL_PATH . 'DBSessionHandlerModel.php', |
|
12 | + 'Model' => CORE_CLASSES_MODEL_PATH . 'Model.php', |
|
13 | + //Core classes |
|
14 | + 'Config' => CORE_CLASSES_PATH . 'Config.php', |
|
15 | + 'Controller' => CORE_CLASSES_PATH . 'Controller.php', |
|
16 | + 'Database' => CORE_CLASSES_DATABASE_PATH . 'Database.php', |
|
17 | + 'DatabaseQueryBuilder' => CORE_CLASSES_DATABASE_PATH . 'DatabaseQueryBuilder.php', |
|
18 | + 'DatabaseQueryResult' => CORE_CLASSES_DATABASE_PATH . 'DatabaseQueryResult.php', |
|
19 | + 'DatabaseQueryRunner' => CORE_CLASSES_DATABASE_PATH . 'DatabaseQueryRunner.php', |
|
20 | + 'DBSessionHandler' => CORE_CLASSES_PATH . 'DBSessionHandler.php', |
|
21 | + 'EventInfo' => CORE_CLASSES_PATH . 'EventInfo.php', |
|
22 | + 'EventDispatcher' => CORE_CLASSES_PATH . 'EventDispatcher.php', |
|
23 | + 'Lang' => CORE_CLASSES_PATH . 'Lang.php', |
|
24 | + 'Loader' => CORE_CLASSES_PATH . 'Loader.php', |
|
25 | + 'Log' => CORE_CLASSES_PATH . 'Log.php', |
|
26 | + 'Module' => CORE_CLASSES_PATH . 'Module.php', |
|
27 | + 'Request' => CORE_CLASSES_PATH . 'Request.php', |
|
28 | + 'Response' => CORE_CLASSES_PATH . 'Response.php', |
|
29 | + 'Router' => CORE_CLASSES_PATH . 'Router.php', |
|
30 | + 'Security' => CORE_CLASSES_PATH . 'Security.php', |
|
31 | + 'Session' => CORE_CLASSES_PATH . 'Session.php', |
|
32 | + 'Url' => CORE_CLASSES_PATH . 'Url.php', |
|
33 | + //Core libraries |
|
34 | + 'Assets' => CORE_LIBRARY_PATH . 'Assets.php', |
|
35 | + 'Benchmark' => CORE_LIBRARY_PATH . 'Benchmark.php', |
|
36 | + 'Browser' => CORE_LIBRARY_PATH . 'Browser.php', |
|
37 | + 'Cookie' => CORE_LIBRARY_PATH . 'Cookie.php', |
|
38 | + 'Email' => CORE_LIBRARY_PATH . 'Email.php', |
|
39 | + 'Form' => CORE_LIBRARY_PATH . 'Form.php', |
|
40 | + 'FormValidation' => CORE_LIBRARY_PATH . 'FormValidation.php', |
|
41 | + 'Html' => CORE_LIBRARY_PATH . 'Html.php', |
|
42 | + 'Pagination' => CORE_LIBRARY_PATH . 'Pagination.php', |
|
43 | + 'PDF' => CORE_LIBRARY_PATH . 'PDF.php', |
|
44 | + 'StringHash' => CORE_LIBRARY_PATH . 'StringHash.php', |
|
45 | + 'Upload' => CORE_LIBRARY_PATH . 'Upload.php', |
|
46 | + ); |
|
47 | + if(isset($classesMap[$class])){ |
|
48 | + if(file_exists($classesMap[$class])){ |
|
49 | + require_once $classesMap[$class]; |
|
50 | + } |
|
51 | + else{ |
|
52 | + echo 'File for class ' . $class . ' not found'; |
|
53 | + } |
|
54 | + } |
|
55 | + } |
|
56 | 56 | \ No newline at end of file |
@@ -1,43 +1,43 @@ 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 | - */ |
|
3 | + /** |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | 26 | |
27 | - class DatabaseQueryResult{ |
|
27 | + class DatabaseQueryResult{ |
|
28 | 28 | |
29 | - /** |
|
30 | - * The database query result |
|
31 | - * @var mixed |
|
32 | - */ |
|
33 | - private $result = null; |
|
29 | + /** |
|
30 | + * The database query result |
|
31 | + * @var mixed |
|
32 | + */ |
|
33 | + private $result = null; |
|
34 | 34 | |
35 | 35 | |
36 | - /** |
|
37 | - * The number of rows returned by the last query |
|
38 | - * @var int |
|
39 | - */ |
|
40 | - private $numRows = 0; |
|
36 | + /** |
|
37 | + * The number of rows returned by the last query |
|
38 | + * @var int |
|
39 | + */ |
|
40 | + private $numRows = 0; |
|
41 | 41 | |
42 | 42 | |
43 | 43 | /** |
@@ -51,24 +51,24 @@ discard block |
||
51 | 51 | } |
52 | 52 | |
53 | 53 | |
54 | - /** |
|
55 | - * Return the query result |
|
56 | - * |
|
57 | - * @return mixed |
|
58 | - */ |
|
54 | + /** |
|
55 | + * Return the query result |
|
56 | + * |
|
57 | + * @return mixed |
|
58 | + */ |
|
59 | 59 | public function getResult(){ |
60 | - return $this->result; |
|
60 | + return $this->result; |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | /** |
64 | 64 | * Set the query result |
65 | 65 | * @param mixed $result the query result |
66 | 66 | * |
67 | - * @return object DatabaseQueryResult |
|
67 | + * @return object DatabaseQueryResult |
|
68 | 68 | */ |
69 | 69 | public function setResult($result){ |
70 | - $this->result = $result; |
|
71 | - return $this; |
|
70 | + $this->result = $result; |
|
71 | + return $this; |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -77,18 +77,18 @@ discard block |
||
77 | 77 | * @return int |
78 | 78 | */ |
79 | 79 | public function getNumRows(){ |
80 | - return $this->numRows; |
|
80 | + return $this->numRows; |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | /** |
84 | 84 | * Set the number of rows returned by the query |
85 | 85 | * @param int $numRows the number of rows returned |
86 | 86 | * |
87 | - * @return object DatabaseQueryResult |
|
87 | + * @return object DatabaseQueryResult |
|
88 | 88 | */ |
89 | 89 | public function setNumRows($numRows){ |
90 | - $this->numRows = $numRows; |
|
91 | - return $this; |
|
90 | + $this->numRows = $numRows; |
|
91 | + return $this; |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | } |
@@ -24,13 +24,13 @@ discard block |
||
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | 25 | */ |
26 | 26 | |
27 | - class DatabaseQueryResult{ |
|
27 | + class DatabaseQueryResult { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * The database query result |
31 | 31 | * @var mixed |
32 | 32 | */ |
33 | - private $result = null; |
|
33 | + private $result = null; |
|
34 | 34 | |
35 | 35 | |
36 | 36 | /** |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | * @param mixed $result the query result |
46 | 46 | * @param int $numRows the number of rows returned by the query |
47 | 47 | */ |
48 | - public function __construct($result = null, $numRows = 0){ |
|
48 | + public function __construct($result = null, $numRows = 0) { |
|
49 | 49 | $this->result = $result; |
50 | 50 | $this->numRows = $numRows; |
51 | 51 | } |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | * |
57 | 57 | * @return mixed |
58 | 58 | */ |
59 | - public function getResult(){ |
|
59 | + public function getResult() { |
|
60 | 60 | return $this->result; |
61 | 61 | } |
62 | 62 | |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | * |
67 | 67 | * @return object DatabaseQueryResult |
68 | 68 | */ |
69 | - public function setResult($result){ |
|
69 | + public function setResult($result) { |
|
70 | 70 | $this->result = $result; |
71 | 71 | return $this; |
72 | 72 | } |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | * |
77 | 77 | * @return int |
78 | 78 | */ |
79 | - public function getNumRows(){ |
|
79 | + public function getNumRows() { |
|
80 | 80 | return $this->numRows; |
81 | 81 | } |
82 | 82 | |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | * |
87 | 87 | * @return object DatabaseQueryResult |
88 | 88 | */ |
89 | - public function setNumRows($numRows){ |
|
89 | + public function setNumRows($numRows) { |
|
90 | 90 | $this->numRows = $numRows; |
91 | 91 | return $this; |
92 | 92 | } |
@@ -1,108 +1,108 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | defined('ROOT_PATH') || exit('Access denied'); |
3 | - /** |
|
4 | - * TNH Framework |
|
5 | - * |
|
6 | - * A simple PHP framework using HMVC architecture |
|
7 | - * |
|
8 | - * This content is released under the GNU GPL License (GPL) |
|
9 | - * |
|
10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
11 | - * |
|
12 | - * This program is free software; you can redistribute it and/or |
|
13 | - * modify it under the terms of the GNU General Public License |
|
14 | - * as published by the Free Software Foundation; either version 3 |
|
15 | - * of the License, or (at your option) any later version. |
|
16 | - * |
|
17 | - * This program is distributed in the hope that it will be useful, |
|
18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | - * GNU General Public License for more details. |
|
21 | - * |
|
22 | - * You should have received a copy of the GNU General Public License |
|
23 | - * along with this program; if not, write to the Free Software |
|
24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | - */ |
|
26 | - class DatabaseQueryBuilder{ |
|
27 | - /** |
|
28 | - * The SQL SELECT statment |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - private $select = '*'; |
|
3 | + /** |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | + class DatabaseQueryBuilder{ |
|
27 | + /** |
|
28 | + * The SQL SELECT statment |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + private $select = '*'; |
|
32 | 32 | |
33 | - /** |
|
34 | - * The SQL FROM statment |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - private $from = null; |
|
33 | + /** |
|
34 | + * The SQL FROM statment |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + private $from = null; |
|
38 | 38 | |
39 | - /** |
|
40 | - * The SQL WHERE statment |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - private $where = null; |
|
39 | + /** |
|
40 | + * The SQL WHERE statment |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + private $where = null; |
|
44 | 44 | |
45 | - /** |
|
46 | - * The SQL LIMIT statment |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - private $limit = null; |
|
45 | + /** |
|
46 | + * The SQL LIMIT statment |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + private $limit = null; |
|
50 | 50 | |
51 | - /** |
|
52 | - * The SQL JOIN statment |
|
53 | - * @var string |
|
54 | - */ |
|
55 | - private $join = null; |
|
51 | + /** |
|
52 | + * The SQL JOIN statment |
|
53 | + * @var string |
|
54 | + */ |
|
55 | + private $join = null; |
|
56 | 56 | |
57 | - /** |
|
58 | - * The SQL ORDER BY statment |
|
59 | - * @var string |
|
60 | - */ |
|
61 | - private $orderBy = null; |
|
57 | + /** |
|
58 | + * The SQL ORDER BY statment |
|
59 | + * @var string |
|
60 | + */ |
|
61 | + private $orderBy = null; |
|
62 | 62 | |
63 | - /** |
|
64 | - * The SQL GROUP BY statment |
|
65 | - * @var string |
|
66 | - */ |
|
67 | - private $groupBy = null; |
|
63 | + /** |
|
64 | + * The SQL GROUP BY statment |
|
65 | + * @var string |
|
66 | + */ |
|
67 | + private $groupBy = null; |
|
68 | 68 | |
69 | - /** |
|
70 | - * The SQL HAVING statment |
|
71 | - * @var string |
|
72 | - */ |
|
73 | - private $having = null; |
|
69 | + /** |
|
70 | + * The SQL HAVING statment |
|
71 | + * @var string |
|
72 | + */ |
|
73 | + private $having = null; |
|
74 | 74 | |
75 | - /** |
|
76 | - * The full SQL query statment after build for each command |
|
77 | - * @var string |
|
78 | - */ |
|
79 | - private $query = null; |
|
75 | + /** |
|
76 | + * The full SQL query statment after build for each command |
|
77 | + * @var string |
|
78 | + */ |
|
79 | + private $query = null; |
|
80 | 80 | |
81 | - /** |
|
82 | - * The list of SQL valid operators |
|
83 | - * @var array |
|
84 | - */ |
|
81 | + /** |
|
82 | + * The list of SQL valid operators |
|
83 | + * @var array |
|
84 | + */ |
|
85 | 85 | private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
86 | 86 | |
87 | 87 | |
88 | - /** |
|
89 | - * The prefix used in each database table |
|
90 | - * @var string |
|
91 | - */ |
|
88 | + /** |
|
89 | + * The prefix used in each database table |
|
90 | + * @var string |
|
91 | + */ |
|
92 | 92 | private $prefix = null; |
93 | 93 | |
94 | 94 | |
95 | 95 | /** |
96 | - * The PDO instance |
|
97 | - * @var object |
|
98 | - */ |
|
96 | + * The PDO instance |
|
97 | + * @var object |
|
98 | + */ |
|
99 | 99 | private $pdo = null; |
100 | 100 | |
101 | - /** |
|
102 | - * The database driver name used |
|
103 | - * @var string |
|
104 | - */ |
|
105 | - private $driver = null; |
|
101 | + /** |
|
102 | + * The database driver name used |
|
103 | + * @var string |
|
104 | + */ |
|
105 | + private $driver = null; |
|
106 | 106 | |
107 | 107 | |
108 | 108 | /** |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | */ |
112 | 112 | public function __construct(PDO $pdo = null){ |
113 | 113 | if (is_object($pdo)){ |
114 | - $this->setPdo($pdo); |
|
114 | + $this->setPdo($pdo); |
|
115 | 115 | } |
116 | 116 | } |
117 | 117 | |
@@ -121,16 +121,16 @@ discard block |
||
121 | 121 | * @return object the current DatabaseQueryBuilder instance |
122 | 122 | */ |
123 | 123 | public function from($table){ |
124 | - if (is_array($table)){ |
|
124 | + if (is_array($table)){ |
|
125 | 125 | $froms = ''; |
126 | 126 | foreach($table as $key){ |
127 | - $froms .= $this->getPrefix() . $key . ', '; |
|
127 | + $froms .= $this->getPrefix() . $key . ', '; |
|
128 | 128 | } |
129 | 129 | $this->from = rtrim($froms, ', '); |
130 | - } else { |
|
130 | + } else { |
|
131 | 131 | $this->from = $this->getPrefix() . $table; |
132 | - } |
|
133 | - return $this; |
|
132 | + } |
|
133 | + return $this; |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | /** |
@@ -139,9 +139,9 @@ discard block |
||
139 | 139 | * @return object the current DatabaseQueryBuilder instance |
140 | 140 | */ |
141 | 141 | public function select($fields){ |
142 | - $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
143 | - $this->select = (($this->select == '*' || empty($this->select)) ? $select : $this->select . ', ' . $select); |
|
144 | - return $this; |
|
142 | + $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
|
143 | + $this->select = (($this->select == '*' || empty($this->select)) ? $select : $this->select . ', ' . $select); |
|
144 | + return $this; |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | /** |
@@ -150,19 +150,19 @@ discard block |
||
150 | 150 | * @return object the current DatabaseQueryBuilder instance |
151 | 151 | */ |
152 | 152 | public function distinct($field){ |
153 | - $distinct = ' DISTINCT ' . $field; |
|
154 | - $this->select = (($this->select == '*' || empty($this->select)) ? $distinct : $this->select . ', ' . $distinct); |
|
155 | - return $this; |
|
153 | + $distinct = ' DISTINCT ' . $field; |
|
154 | + $this->select = (($this->select == '*' || empty($this->select)) ? $distinct : $this->select . ', ' . $distinct); |
|
155 | + return $this; |
|
156 | 156 | } |
157 | 157 | |
158 | - /** |
|
159 | - * Set the SQL function COUNT in SELECT statment |
|
160 | - * @param string $field the field name |
|
161 | - * @param string $name if is not null represent the alias used for this field in the result |
|
162 | - * @return object the current DatabaseQueryBuilder instance |
|
163 | - */ |
|
158 | + /** |
|
159 | + * Set the SQL function COUNT in SELECT statment |
|
160 | + * @param string $field the field name |
|
161 | + * @param string $name if is not null represent the alias used for this field in the result |
|
162 | + * @return object the current DatabaseQueryBuilder instance |
|
163 | + */ |
|
164 | 164 | public function count($field = '*', $name = null){ |
165 | - return $this->select_min_max_sum_count_avg('COUNT', $field, $name); |
|
165 | + return $this->select_min_max_sum_count_avg('COUNT', $field, $name); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | /** |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | * @return object the current DatabaseQueryBuilder instance |
173 | 173 | */ |
174 | 174 | public function min($field, $name = null){ |
175 | - return $this->select_min_max_sum_count_avg('MIN', $field, $name); |
|
175 | + return $this->select_min_max_sum_count_avg('MIN', $field, $name); |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | /** |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | * @return object the current DatabaseQueryBuilder instance |
183 | 183 | */ |
184 | 184 | public function max($field, $name = null){ |
185 | - return $this->select_min_max_sum_count_avg('MAX', $field, $name); |
|
185 | + return $this->select_min_max_sum_count_avg('MAX', $field, $name); |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | /** |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | * @return object the current DatabaseQueryBuilder instance |
193 | 193 | */ |
194 | 194 | public function sum($field, $name = null){ |
195 | - return $this->select_min_max_sum_count_avg('SUM', $field, $name); |
|
195 | + return $this->select_min_max_sum_count_avg('SUM', $field, $name); |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | /** |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | * @return object the current DatabaseQueryBuilder instance |
203 | 203 | */ |
204 | 204 | public function avg($field, $name = null){ |
205 | - return $this->select_min_max_sum_count_avg('AVG', $field, $name); |
|
205 | + return $this->select_min_max_sum_count_avg('AVG', $field, $name); |
|
206 | 206 | } |
207 | 207 | |
208 | 208 | |
@@ -216,20 +216,20 @@ discard block |
||
216 | 216 | * @return object the current DatabaseQueryBuilder instance |
217 | 217 | */ |
218 | 218 | public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
219 | - $on = $field1; |
|
220 | - $table = $this->getPrefix() . $table; |
|
221 | - if (! is_null($op)){ |
|
219 | + $on = $field1; |
|
220 | + $table = $this->getPrefix() . $table; |
|
221 | + if (! is_null($op)){ |
|
222 | 222 | $on = (! in_array($op, $this->operatorList) |
223 | - ? ($this->getPrefix() . $field1 . ' = ' . $this->getPrefix() . $op) |
|
224 | - : ($this->getPrefix() . $field1 . ' ' . $op . ' ' . $this->getPrefix() . $field2)); |
|
225 | - } |
|
226 | - if (empty($this->join)){ |
|
223 | + ? ($this->getPrefix() . $field1 . ' = ' . $this->getPrefix() . $op) |
|
224 | + : ($this->getPrefix() . $field1 . ' ' . $op . ' ' . $this->getPrefix() . $field2)); |
|
225 | + } |
|
226 | + if (empty($this->join)){ |
|
227 | 227 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
228 | - } |
|
229 | - else{ |
|
228 | + } |
|
229 | + else{ |
|
230 | 230 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
231 | - } |
|
232 | - return $this; |
|
231 | + } |
|
232 | + return $this; |
|
233 | 233 | } |
234 | 234 | |
235 | 235 | /** |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | * @return object the current DatabaseQueryBuilder instance |
239 | 239 | */ |
240 | 240 | public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
241 | - return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
241 | + return $this->join($table, $field1, $op, $field2, 'INNER '); |
|
242 | 242 | } |
243 | 243 | |
244 | 244 | /** |
@@ -247,16 +247,16 @@ discard block |
||
247 | 247 | * @return object the current DatabaseQueryBuilder instance |
248 | 248 | */ |
249 | 249 | public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
250 | - return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
251 | - } |
|
250 | + return $this->join($table, $field1, $op, $field2, 'LEFT '); |
|
251 | + } |
|
252 | 252 | |
253 | - /** |
|
253 | + /** |
|
254 | 254 | * Set the SQL RIGHT JOIN statment |
255 | 255 | * @see DatabaseQueryBuilder::join() |
256 | 256 | * @return object the current DatabaseQueryBuilder instance |
257 | 257 | */ |
258 | 258 | public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
259 | - return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
259 | + return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
|
260 | 260 | } |
261 | 261 | |
262 | 262 | /** |
@@ -265,7 +265,7 @@ discard block |
||
265 | 265 | * @return object the current DatabaseQueryBuilder instance |
266 | 266 | */ |
267 | 267 | public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
268 | - return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
268 | + return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
|
269 | 269 | } |
270 | 270 | |
271 | 271 | /** |
@@ -274,7 +274,7 @@ discard block |
||
274 | 274 | * @return object the current DatabaseQueryBuilder instance |
275 | 275 | */ |
276 | 276 | public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
277 | - return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
277 | + return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
|
278 | 278 | } |
279 | 279 | |
280 | 280 | /** |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | * @return object the current DatabaseQueryBuilder instance |
284 | 284 | */ |
285 | 285 | public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
286 | - return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
286 | + return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
|
287 | 287 | } |
288 | 288 | |
289 | 289 | /** |
@@ -293,14 +293,14 @@ discard block |
||
293 | 293 | * @return object the current DatabaseQueryBuilder instance |
294 | 294 | */ |
295 | 295 | public function whereIsNull($field, $andOr = 'AND'){ |
296 | - if (is_array($field)){ |
|
296 | + if (is_array($field)){ |
|
297 | 297 | foreach($field as $f){ |
298 | - $this->whereIsNull($f, $andOr); |
|
298 | + $this->whereIsNull($f, $andOr); |
|
299 | 299 | } |
300 | - } else { |
|
301 | - $this->setWhereStr($field.' IS NULL ', $andOr); |
|
302 | - } |
|
303 | - return $this; |
|
300 | + } else { |
|
301 | + $this->setWhereStr($field.' IS NULL ', $andOr); |
|
302 | + } |
|
303 | + return $this; |
|
304 | 304 | } |
305 | 305 | |
306 | 306 | /** |
@@ -310,14 +310,14 @@ discard block |
||
310 | 310 | * @return object the current DatabaseQueryBuilder instance |
311 | 311 | */ |
312 | 312 | public function whereIsNotNull($field, $andOr = 'AND'){ |
313 | - if (is_array($field)){ |
|
313 | + if (is_array($field)){ |
|
314 | 314 | foreach($field as $f){ |
315 | - $this->whereIsNotNull($f, $andOr); |
|
315 | + $this->whereIsNotNull($f, $andOr); |
|
316 | + } |
|
317 | + } else { |
|
318 | + $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
316 | 319 | } |
317 | - } else { |
|
318 | - $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
319 | - } |
|
320 | - return $this; |
|
320 | + return $this; |
|
321 | 321 | } |
322 | 322 | |
323 | 323 | /** |
@@ -331,19 +331,19 @@ discard block |
||
331 | 331 | * @return object the current DatabaseQueryBuilder instance |
332 | 332 | */ |
333 | 333 | public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
334 | - $whereStr = ''; |
|
335 | - if (is_array($where)){ |
|
334 | + $whereStr = ''; |
|
335 | + if (is_array($where)){ |
|
336 | 336 | $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
337 | - } |
|
338 | - else{ |
|
337 | + } |
|
338 | + else{ |
|
339 | 339 | if (is_array($op)){ |
340 | - $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
340 | + $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
|
341 | 341 | } else { |
342 | - $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
342 | + $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
|
343 | + } |
|
343 | 344 | } |
344 | - } |
|
345 | - $this->setWhereStr($whereStr, $andOr); |
|
346 | - return $this; |
|
345 | + $this->setWhereStr($whereStr, $andOr); |
|
346 | + return $this; |
|
347 | 347 | } |
348 | 348 | |
349 | 349 | /** |
@@ -352,7 +352,7 @@ discard block |
||
352 | 352 | * @return object the current DatabaseQueryBuilder instance |
353 | 353 | */ |
354 | 354 | public function orWhere($where, $op = null, $val = null, $escape = true){ |
355 | - return $this->where($where, $op, $val, '', 'OR', $escape); |
|
355 | + return $this->where($where, $op, $val, '', 'OR', $escape); |
|
356 | 356 | } |
357 | 357 | |
358 | 358 | |
@@ -362,7 +362,7 @@ discard block |
||
362 | 362 | * @return object the current DatabaseQueryBuilder instance |
363 | 363 | */ |
364 | 364 | public function notWhere($where, $op = null, $val = null, $escape = true){ |
365 | - return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
365 | + return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
|
366 | 366 | } |
367 | 367 | |
368 | 368 | /** |
@@ -371,7 +371,7 @@ discard block |
||
371 | 371 | * @return object the current DatabaseQueryBuilder instance |
372 | 372 | */ |
373 | 373 | public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
374 | - return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
374 | + return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
|
375 | 375 | } |
376 | 376 | |
377 | 377 | /** |
@@ -381,16 +381,16 @@ discard block |
||
381 | 381 | * @return object the current DatabaseQueryBuilder instance |
382 | 382 | */ |
383 | 383 | public function groupStart($type = '', $andOr = ' AND'){ |
384 | - if (empty($this->where)){ |
|
384 | + if (empty($this->where)){ |
|
385 | 385 | $this->where = $type . ' ('; |
386 | - } else { |
|
387 | - if (substr(trim($this->where), -1) == '('){ |
|
386 | + } else { |
|
387 | + if (substr(trim($this->where), -1) == '('){ |
|
388 | 388 | $this->where .= $type . ' ('; |
389 | - } else { |
|
390 | - $this->where .= $andOr . ' ' . $type . ' ('; |
|
391 | - } |
|
392 | - } |
|
393 | - return $this; |
|
389 | + } else { |
|
390 | + $this->where .= $andOr . ' ' . $type . ' ('; |
|
391 | + } |
|
392 | + } |
|
393 | + return $this; |
|
394 | 394 | } |
395 | 395 | |
396 | 396 | /** |
@@ -399,7 +399,7 @@ discard block |
||
399 | 399 | * @return object the current DatabaseQueryBuilder instance |
400 | 400 | */ |
401 | 401 | public function notGroupStart(){ |
402 | - return $this->groupStart('NOT'); |
|
402 | + return $this->groupStart('NOT'); |
|
403 | 403 | } |
404 | 404 | |
405 | 405 | /** |
@@ -408,16 +408,16 @@ discard block |
||
408 | 408 | * @return object the current DatabaseQueryBuilder instance |
409 | 409 | */ |
410 | 410 | public function orGroupStart(){ |
411 | - return $this->groupStart('', ' OR'); |
|
411 | + return $this->groupStart('', ' OR'); |
|
412 | 412 | } |
413 | 413 | |
414 | - /** |
|
415 | - * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
416 | - * @see DatabaseQueryBuilder::groupStart() |
|
417 | - * @return object the current DatabaseQueryBuilder instance |
|
418 | - */ |
|
414 | + /** |
|
415 | + * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type |
|
416 | + * @see DatabaseQueryBuilder::groupStart() |
|
417 | + * @return object the current DatabaseQueryBuilder instance |
|
418 | + */ |
|
419 | 419 | public function orNotGroupStart(){ |
420 | - return $this->groupStart('NOT', ' OR'); |
|
420 | + return $this->groupStart('NOT', ' OR'); |
|
421 | 421 | } |
422 | 422 | |
423 | 423 | /** |
@@ -425,8 +425,8 @@ discard block |
||
425 | 425 | * @return object the current DatabaseQueryBuilder instance |
426 | 426 | */ |
427 | 427 | public function groupEnd(){ |
428 | - $this->where .= ')'; |
|
429 | - return $this; |
|
428 | + $this->where .= ')'; |
|
429 | + return $this; |
|
430 | 430 | } |
431 | 431 | |
432 | 432 | /** |
@@ -439,17 +439,17 @@ discard block |
||
439 | 439 | * @return object the current DatabaseQueryBuilder instance |
440 | 440 | */ |
441 | 441 | public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
442 | - $_keys = array(); |
|
443 | - foreach ($keys as $k => $v){ |
|
442 | + $_keys = array(); |
|
443 | + foreach ($keys as $k => $v){ |
|
444 | 444 | if (is_null($v)){ |
445 | - $v = ''; |
|
445 | + $v = ''; |
|
446 | 446 | } |
447 | 447 | $_keys[] = (is_numeric($v) ? $v : $this->escape($v, $escape)); |
448 | - } |
|
449 | - $keys = implode(', ', $_keys); |
|
450 | - $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
451 | - $this->setWhereStr($whereStr, $andOr); |
|
452 | - return $this; |
|
448 | + } |
|
449 | + $keys = implode(', ', $_keys); |
|
450 | + $whereStr = $field . ' ' . $type . ' IN (' . $keys . ')'; |
|
451 | + $this->setWhereStr($whereStr, $andOr); |
|
452 | + return $this; |
|
453 | 453 | } |
454 | 454 | |
455 | 455 | /** |
@@ -458,7 +458,7 @@ discard block |
||
458 | 458 | * @return object the current DatabaseQueryBuilder instance |
459 | 459 | */ |
460 | 460 | public function notIn($field, array $keys, $escape = true){ |
461 | - return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
461 | + return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
|
462 | 462 | } |
463 | 463 | |
464 | 464 | /** |
@@ -467,7 +467,7 @@ discard block |
||
467 | 467 | * @return object the current DatabaseQueryBuilder instance |
468 | 468 | */ |
469 | 469 | public function orIn($field, array $keys, $escape = true){ |
470 | - return $this->in($field, $keys, '', 'OR', $escape); |
|
470 | + return $this->in($field, $keys, '', 'OR', $escape); |
|
471 | 471 | } |
472 | 472 | |
473 | 473 | /** |
@@ -476,7 +476,7 @@ discard block |
||
476 | 476 | * @return object the current DatabaseQueryBuilder instance |
477 | 477 | */ |
478 | 478 | public function orNotIn($field, array $keys, $escape = true){ |
479 | - return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
479 | + return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
|
480 | 480 | } |
481 | 481 | |
482 | 482 | /** |
@@ -490,15 +490,15 @@ discard block |
||
490 | 490 | * @return object the current DatabaseQueryBuilder instance |
491 | 491 | */ |
492 | 492 | public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
493 | - if (is_null($value1)){ |
|
493 | + if (is_null($value1)){ |
|
494 | 494 | $value1 = ''; |
495 | - } |
|
496 | - if (is_null($value2)){ |
|
495 | + } |
|
496 | + if (is_null($value2)){ |
|
497 | 497 | $value2 = ''; |
498 | - } |
|
499 | - $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
|
500 | - $this->setWhereStr($whereStr, $andOr); |
|
501 | - return $this; |
|
498 | + } |
|
499 | + $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
|
500 | + $this->setWhereStr($whereStr, $andOr); |
|
501 | + return $this; |
|
502 | 502 | } |
503 | 503 | |
504 | 504 | /** |
@@ -507,7 +507,7 @@ discard block |
||
507 | 507 | * @return object the current DatabaseQueryBuilder instance |
508 | 508 | */ |
509 | 509 | public function notBetween($field, $value1, $value2, $escape = true){ |
510 | - return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
510 | + return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
|
511 | 511 | } |
512 | 512 | |
513 | 513 | /** |
@@ -516,7 +516,7 @@ discard block |
||
516 | 516 | * @return object the current DatabaseQueryBuilder instance |
517 | 517 | */ |
518 | 518 | public function orBetween($field, $value1, $value2, $escape = true){ |
519 | - return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
519 | + return $this->between($field, $value1, $value2, '', 'OR', $escape); |
|
520 | 520 | } |
521 | 521 | |
522 | 522 | /** |
@@ -525,7 +525,7 @@ discard block |
||
525 | 525 | * @return object the current DatabaseQueryBuilder instance |
526 | 526 | */ |
527 | 527 | public function orNotBetween($field, $value1, $value2, $escape = true){ |
528 | - return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
528 | + return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
|
529 | 529 | } |
530 | 530 | |
531 | 531 | /** |
@@ -538,11 +538,11 @@ discard block |
||
538 | 538 | * @return object the current DatabaseQueryBuilder instance |
539 | 539 | */ |
540 | 540 | public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
541 | - if (empty($data)){ |
|
541 | + if (empty($data)){ |
|
542 | 542 | $data = ''; |
543 | - } |
|
544 | - $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
|
545 | - return $this; |
|
543 | + } |
|
544 | + $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
|
545 | + return $this; |
|
546 | 546 | } |
547 | 547 | |
548 | 548 | /** |
@@ -551,7 +551,7 @@ discard block |
||
551 | 551 | * @return object the current DatabaseQueryBuilder instance |
552 | 552 | */ |
553 | 553 | public function orLike($field, $data, $escape = true){ |
554 | - return $this->like($field, $data, '', 'OR', $escape); |
|
554 | + return $this->like($field, $data, '', 'OR', $escape); |
|
555 | 555 | } |
556 | 556 | |
557 | 557 | /** |
@@ -560,7 +560,7 @@ discard block |
||
560 | 560 | * @return object the current DatabaseQueryBuilder instance |
561 | 561 | */ |
562 | 562 | public function notLike($field, $data, $escape = true){ |
563 | - return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
563 | + return $this->like($field, $data, 'NOT ', 'AND', $escape); |
|
564 | 564 | } |
565 | 565 | |
566 | 566 | /** |
@@ -569,7 +569,7 @@ discard block |
||
569 | 569 | * @return object the current DatabaseQueryBuilder instance |
570 | 570 | */ |
571 | 571 | public function orNotLike($field, $data, $escape = true){ |
572 | - return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
572 | + return $this->like($field, $data, 'NOT ', 'OR', $escape); |
|
573 | 573 | } |
574 | 574 | |
575 | 575 | /** |
@@ -580,16 +580,16 @@ discard block |
||
580 | 580 | * @return object the current DatabaseQueryBuilder instance |
581 | 581 | */ |
582 | 582 | public function limit($limit, $limitEnd = null){ |
583 | - if (empty($limit)){ |
|
583 | + if (empty($limit)){ |
|
584 | 584 | $limit = 0; |
585 | - } |
|
586 | - if (! is_null($limitEnd)){ |
|
585 | + } |
|
586 | + if (! is_null($limitEnd)){ |
|
587 | 587 | $this->limit = $limit . ', ' . $limitEnd; |
588 | - } |
|
589 | - else{ |
|
588 | + } |
|
589 | + else{ |
|
590 | 590 | $this->limit = $limit; |
591 | - } |
|
592 | - return $this; |
|
591 | + } |
|
592 | + return $this; |
|
593 | 593 | } |
594 | 594 | |
595 | 595 | /** |
@@ -599,15 +599,15 @@ discard block |
||
599 | 599 | * @return object the current DatabaseQueryBuilder instance |
600 | 600 | */ |
601 | 601 | public function orderBy($orderBy, $orderDir = ' ASC'){ |
602 | - if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
602 | + if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
603 | 603 | $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
604 | - } |
|
605 | - else{ |
|
604 | + } |
|
605 | + else{ |
|
606 | 606 | $this->orderBy = empty($this->orderBy) |
607 | - ? ($orderBy . ' ' . strtoupper($orderDir)) |
|
608 | - : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
609 | - } |
|
610 | - return $this; |
|
607 | + ? ($orderBy . ' ' . strtoupper($orderDir)) |
|
608 | + : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
|
609 | + } |
|
610 | + return $this; |
|
611 | 611 | } |
612 | 612 | |
613 | 613 | /** |
@@ -616,13 +616,13 @@ discard block |
||
616 | 616 | * @return object the current DatabaseQueryBuilder instance |
617 | 617 | */ |
618 | 618 | public function groupBy($field){ |
619 | - if (is_array($field)){ |
|
619 | + if (is_array($field)){ |
|
620 | 620 | $this->groupBy = implode(', ', $field); |
621 | - } |
|
622 | - else{ |
|
621 | + } |
|
622 | + else{ |
|
623 | 623 | $this->groupBy = $field; |
624 | - } |
|
625 | - return $this; |
|
624 | + } |
|
625 | + return $this; |
|
626 | 626 | } |
627 | 627 | |
628 | 628 | /** |
@@ -634,22 +634,22 @@ discard block |
||
634 | 634 | * @return object the current DatabaseQueryBuilder instance |
635 | 635 | */ |
636 | 636 | public function having($field, $op = null, $val = null, $escape = true){ |
637 | - if (is_array($op)){ |
|
637 | + if (is_array($op)){ |
|
638 | 638 | $this->having = $this->getHavingStrIfOperatorIsArray($field, $op, $escape); |
639 | - } |
|
640 | - else if (! in_array($op, $this->operatorList)){ |
|
639 | + } |
|
640 | + else if (! in_array($op, $this->operatorList)){ |
|
641 | 641 | if (is_null($op)){ |
642 | - $op = ''; |
|
642 | + $op = ''; |
|
643 | 643 | } |
644 | 644 | $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
645 | - } |
|
646 | - else{ |
|
645 | + } |
|
646 | + else{ |
|
647 | 647 | if (is_null($val)){ |
648 | - $val = ''; |
|
648 | + $val = ''; |
|
649 | 649 | } |
650 | 650 | $this->having = $field . ' ' . $op . ' ' . ($this->escape($val, $escape)); |
651 | - } |
|
652 | - return $this; |
|
651 | + } |
|
652 | + return $this; |
|
653 | 653 | } |
654 | 654 | |
655 | 655 | /** |
@@ -659,12 +659,12 @@ discard block |
||
659 | 659 | * @return object the current DatabaseQueryBuilder instance |
660 | 660 | */ |
661 | 661 | public function insert($data = array(), $escape = true){ |
662 | - $columns = array_keys($data); |
|
663 | - $column = implode(', ', $columns); |
|
664 | - $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
662 | + $columns = array_keys($data); |
|
663 | + $column = implode(', ', $columns); |
|
664 | + $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
|
665 | 665 | |
666 | - $this->query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
667 | - return $this; |
|
666 | + $this->query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')'; |
|
667 | + return $this; |
|
668 | 668 | } |
669 | 669 | |
670 | 670 | /** |
@@ -674,25 +674,25 @@ discard block |
||
674 | 674 | * @return object the current DatabaseQueryBuilder instance |
675 | 675 | */ |
676 | 676 | public function update($data = array(), $escape = true){ |
677 | - $query = 'UPDATE ' . $this->from . ' SET '; |
|
678 | - $values = array(); |
|
679 | - foreach ($data as $column => $val){ |
|
677 | + $query = 'UPDATE ' . $this->from . ' SET '; |
|
678 | + $values = array(); |
|
679 | + foreach ($data as $column => $val){ |
|
680 | 680 | $values[] = $column . ' = ' . ($this->escape($val, $escape)); |
681 | - } |
|
682 | - $query .= implode(', ', $values); |
|
683 | - if (! empty($this->where)){ |
|
681 | + } |
|
682 | + $query .= implode(', ', $values); |
|
683 | + if (! empty($this->where)){ |
|
684 | 684 | $query .= ' WHERE ' . $this->where; |
685 | - } |
|
685 | + } |
|
686 | 686 | |
687 | - if (! empty($this->orderBy)){ |
|
687 | + if (! empty($this->orderBy)){ |
|
688 | 688 | $query .= ' ORDER BY ' . $this->orderBy; |
689 | - } |
|
689 | + } |
|
690 | 690 | |
691 | - if (! empty($this->limit)){ |
|
691 | + if (! empty($this->limit)){ |
|
692 | 692 | $query .= ' LIMIT ' . $this->limit; |
693 | - } |
|
694 | - $this->query = $query; |
|
695 | - return $this; |
|
693 | + } |
|
694 | + $this->query = $query; |
|
695 | + return $this; |
|
696 | 696 | } |
697 | 697 | |
698 | 698 | /** |
@@ -700,25 +700,25 @@ discard block |
||
700 | 700 | * @return object the current DatabaseQueryBuilder instance |
701 | 701 | */ |
702 | 702 | public function delete(){ |
703 | - $query = 'DELETE FROM ' . $this->from; |
|
704 | - $isTruncate = $query; |
|
705 | - if (! empty($this->where)){ |
|
706 | - $query .= ' WHERE ' . $this->where; |
|
707 | - } |
|
703 | + $query = 'DELETE FROM ' . $this->from; |
|
704 | + $isTruncate = $query; |
|
705 | + if (! empty($this->where)){ |
|
706 | + $query .= ' WHERE ' . $this->where; |
|
707 | + } |
|
708 | 708 | |
709 | - if (! empty($this->orderBy)){ |
|
710 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
711 | - } |
|
709 | + if (! empty($this->orderBy)){ |
|
710 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
711 | + } |
|
712 | 712 | |
713 | - if (! empty($this->limit)){ |
|
714 | - $query .= ' LIMIT ' . $this->limit; |
|
715 | - } |
|
713 | + if (! empty($this->limit)){ |
|
714 | + $query .= ' LIMIT ' . $this->limit; |
|
715 | + } |
|
716 | 716 | |
717 | - if ($isTruncate == $query && $this->driver != 'sqlite'){ |
|
718 | - $query = 'TRUNCATE TABLE ' . $this->from; |
|
719 | - } |
|
720 | - $this->query = $query; |
|
721 | - return $this; |
|
717 | + if ($isTruncate == $query && $this->driver != 'sqlite'){ |
|
718 | + $query = 'TRUNCATE TABLE ' . $this->from; |
|
719 | + } |
|
720 | + $this->query = $query; |
|
721 | + return $this; |
|
722 | 722 | } |
723 | 723 | |
724 | 724 | /** |
@@ -728,7 +728,7 @@ discard block |
||
728 | 728 | * @return mixed the data after escaped or the same data if not |
729 | 729 | */ |
730 | 730 | public function escape($data, $escaped = true){ |
731 | - return $escaped |
|
731 | + return $escaped |
|
732 | 732 | ? $this->getPdo()->quote(trim($data)) |
733 | 733 | : $data; |
734 | 734 | } |
@@ -739,126 +739,126 @@ discard block |
||
739 | 739 | * @return string |
740 | 740 | */ |
741 | 741 | public function getQuery(){ |
742 | - //INSERT, UPDATE, DELETE already set it, if is the SELECT we need set it now |
|
743 | - if(empty($this->query)){ |
|
744 | - $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
745 | - if (! empty($this->join)){ |
|
746 | - $query .= $this->join; |
|
747 | - } |
|
742 | + //INSERT, UPDATE, DELETE already set it, if is the SELECT we need set it now |
|
743 | + if(empty($this->query)){ |
|
744 | + $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
|
745 | + if (! empty($this->join)){ |
|
746 | + $query .= $this->join; |
|
747 | + } |
|
748 | 748 | |
749 | - if (! empty($this->where)){ |
|
750 | - $query .= ' WHERE ' . $this->where; |
|
751 | - } |
|
749 | + if (! empty($this->where)){ |
|
750 | + $query .= ' WHERE ' . $this->where; |
|
751 | + } |
|
752 | 752 | |
753 | - if (! empty($this->groupBy)){ |
|
754 | - $query .= ' GROUP BY ' . $this->groupBy; |
|
755 | - } |
|
753 | + if (! empty($this->groupBy)){ |
|
754 | + $query .= ' GROUP BY ' . $this->groupBy; |
|
755 | + } |
|
756 | 756 | |
757 | - if (! empty($this->having)){ |
|
758 | - $query .= ' HAVING ' . $this->having; |
|
759 | - } |
|
757 | + if (! empty($this->having)){ |
|
758 | + $query .= ' HAVING ' . $this->having; |
|
759 | + } |
|
760 | 760 | |
761 | - if (! empty($this->orderBy)){ |
|
762 | - $query .= ' ORDER BY ' . $this->orderBy; |
|
763 | - } |
|
761 | + if (! empty($this->orderBy)){ |
|
762 | + $query .= ' ORDER BY ' . $this->orderBy; |
|
763 | + } |
|
764 | 764 | |
765 | - if (! empty($this->limit)){ |
|
766 | - $query .= ' LIMIT ' . $this->limit; |
|
767 | - } |
|
768 | - $this->query = $query; |
|
769 | - } |
|
770 | - return $this->query; |
|
765 | + if (! empty($this->limit)){ |
|
766 | + $query .= ' LIMIT ' . $this->limit; |
|
767 | + } |
|
768 | + $this->query = $query; |
|
769 | + } |
|
770 | + return $this->query; |
|
771 | 771 | } |
772 | 772 | |
773 | 773 | |
774 | - /** |
|
775 | - * Return the PDO instance |
|
776 | - * @return object |
|
777 | - */ |
|
774 | + /** |
|
775 | + * Return the PDO instance |
|
776 | + * @return object |
|
777 | + */ |
|
778 | 778 | public function getPdo(){ |
779 | - return $this->pdo; |
|
779 | + return $this->pdo; |
|
780 | 780 | } |
781 | 781 | |
782 | 782 | /** |
783 | 783 | * Set the PDO instance |
784 | 784 | * @param PDO $pdo the pdo object |
785 | - * @return object DatabaseQueryBuilder |
|
785 | + * @return object DatabaseQueryBuilder |
|
786 | 786 | */ |
787 | 787 | public function setPdo(PDO $pdo = null){ |
788 | - $this->pdo = $pdo; |
|
789 | - return $this; |
|
788 | + $this->pdo = $pdo; |
|
789 | + return $this; |
|
790 | 790 | } |
791 | 791 | |
792 | - /** |
|
793 | - * Return the database table prefix |
|
794 | - * @return string |
|
795 | - */ |
|
792 | + /** |
|
793 | + * Return the database table prefix |
|
794 | + * @return string |
|
795 | + */ |
|
796 | 796 | public function getPrefix(){ |
797 | - return $this->prefix; |
|
797 | + return $this->prefix; |
|
798 | 798 | } |
799 | 799 | |
800 | 800 | /** |
801 | 801 | * Set the database table prefix |
802 | 802 | * @param string $prefix the new prefix |
803 | - * @return object DatabaseQueryBuilder |
|
803 | + * @return object DatabaseQueryBuilder |
|
804 | 804 | */ |
805 | 805 | public function setPrefix($prefix){ |
806 | - $this->prefix = $prefix; |
|
807 | - return $this; |
|
806 | + $this->prefix = $prefix; |
|
807 | + return $this; |
|
808 | 808 | } |
809 | 809 | |
810 | - /** |
|
811 | - * Return the database driver |
|
812 | - * @return string |
|
813 | - */ |
|
810 | + /** |
|
811 | + * Return the database driver |
|
812 | + * @return string |
|
813 | + */ |
|
814 | 814 | public function getDriver(){ |
815 | - return $this->driver; |
|
815 | + return $this->driver; |
|
816 | 816 | } |
817 | 817 | |
818 | 818 | /** |
819 | 819 | * Set the database driver |
820 | 820 | * @param string $driver the new driver |
821 | - * @return object DatabaseQueryBuilder |
|
821 | + * @return object DatabaseQueryBuilder |
|
822 | 822 | */ |
823 | 823 | public function setDriver($driver){ |
824 | - $this->driver = $driver; |
|
825 | - return $this; |
|
824 | + $this->driver = $driver; |
|
825 | + return $this; |
|
826 | 826 | } |
827 | 827 | |
828 | - /** |
|
829 | - * Reset the DatabaseQueryBuilder class attributs to the initial values before each query. |
|
830 | - * @return object the current DatabaseQueryBuilder instance |
|
831 | - */ |
|
828 | + /** |
|
829 | + * Reset the DatabaseQueryBuilder class attributs to the initial values before each query. |
|
830 | + * @return object the current DatabaseQueryBuilder instance |
|
831 | + */ |
|
832 | 832 | public function reset(){ |
833 | - $this->select = '*'; |
|
834 | - $this->from = null; |
|
835 | - $this->where = null; |
|
836 | - $this->limit = null; |
|
837 | - $this->orderBy = null; |
|
838 | - $this->groupBy = null; |
|
839 | - $this->having = null; |
|
840 | - $this->join = null; |
|
841 | - $this->query = null; |
|
842 | - return $this; |
|
843 | - } |
|
844 | - |
|
845 | - /** |
|
846 | - * Get the SQL HAVING clause when operator argument is an array |
|
847 | - * @see DatabaseQueryBuilder::having |
|
848 | - * |
|
849 | - * @return string |
|
850 | - */ |
|
833 | + $this->select = '*'; |
|
834 | + $this->from = null; |
|
835 | + $this->where = null; |
|
836 | + $this->limit = null; |
|
837 | + $this->orderBy = null; |
|
838 | + $this->groupBy = null; |
|
839 | + $this->having = null; |
|
840 | + $this->join = null; |
|
841 | + $this->query = null; |
|
842 | + return $this; |
|
843 | + } |
|
844 | + |
|
845 | + /** |
|
846 | + * Get the SQL HAVING clause when operator argument is an array |
|
847 | + * @see DatabaseQueryBuilder::having |
|
848 | + * |
|
849 | + * @return string |
|
850 | + */ |
|
851 | 851 | protected function getHavingStrIfOperatorIsArray($field, $op = null, $escape = true){ |
852 | 852 | $x = explode('?', $field); |
853 | 853 | $w = ''; |
854 | 854 | foreach($x as $k => $v){ |
855 | - if (!empty($v)){ |
|
855 | + if (!empty($v)){ |
|
856 | 856 | if (! isset($op[$k])){ |
857 | - $op[$k] = ''; |
|
857 | + $op[$k] = ''; |
|
858 | + } |
|
859 | + $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
|
860 | + } |
|
858 | 861 | } |
859 | - $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
|
860 | - } |
|
861 | - } |
|
862 | 862 | return $w; |
863 | 863 | } |
864 | 864 | |
@@ -870,35 +870,35 @@ discard block |
||
870 | 870 | * @return string |
871 | 871 | */ |
872 | 872 | protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
873 | - $_where = array(); |
|
874 | - foreach ($where as $column => $data){ |
|
873 | + $_where = array(); |
|
874 | + foreach ($where as $column => $data){ |
|
875 | 875 | if (is_null($data)){ |
876 | - $data = ''; |
|
876 | + $data = ''; |
|
877 | 877 | } |
878 | 878 | $_where[] = $type . $column . ' = ' . ($this->escape($data, $escape)); |
879 | - } |
|
880 | - $where = implode(' '.$andOr.' ', $_where); |
|
881 | - return $where; |
|
879 | + } |
|
880 | + $where = implode(' '.$andOr.' ', $_where); |
|
881 | + return $where; |
|
882 | 882 | } |
883 | 883 | |
884 | - /** |
|
885 | - * Get the SQL WHERE clause when operator argument is an array |
|
886 | - * @see DatabaseQueryBuilder::where |
|
887 | - * |
|
888 | - * @return string |
|
889 | - */ |
|
884 | + /** |
|
885 | + * Get the SQL WHERE clause when operator argument is an array |
|
886 | + * @see DatabaseQueryBuilder::where |
|
887 | + * |
|
888 | + * @return string |
|
889 | + */ |
|
890 | 890 | protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
891 | - $x = explode('?', $where); |
|
892 | - $w = ''; |
|
893 | - foreach($x as $k => $v){ |
|
891 | + $x = explode('?', $where); |
|
892 | + $w = ''; |
|
893 | + foreach($x as $k => $v){ |
|
894 | 894 | if (! empty($v)){ |
895 | 895 | if (isset($op[$k]) && is_null($op[$k])){ |
896 | - $op[$k] = ''; |
|
896 | + $op[$k] = ''; |
|
897 | 897 | } |
898 | 898 | $w .= $type . $v . (isset($op[$k]) ? ($this->escape($op[$k], $escape)) : ''); |
899 | 899 | } |
900 | - } |
|
901 | - return $w; |
|
900 | + } |
|
901 | + return $w; |
|
902 | 902 | } |
903 | 903 | |
904 | 904 | /** |
@@ -908,53 +908,53 @@ discard block |
||
908 | 908 | * @return string |
909 | 909 | */ |
910 | 910 | protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
911 | - $w = ''; |
|
912 | - if (! in_array((string)$op, $this->operatorList)){ |
|
913 | - if (is_null($op)){ |
|
911 | + $w = ''; |
|
912 | + if (! in_array((string)$op, $this->operatorList)){ |
|
913 | + if (is_null($op)){ |
|
914 | 914 | $op = ''; |
915 | - } |
|
916 | - $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
|
915 | + } |
|
916 | + $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
|
917 | 917 | } else { |
918 | - if (is_null($val)){ |
|
918 | + if (is_null($val)){ |
|
919 | 919 | $val = ''; |
920 | - } |
|
921 | - $w = $type . $where . $op . ($this->escape($val, $escape)); |
|
920 | + } |
|
921 | + $w = $type . $where . $op . ($this->escape($val, $escape)); |
|
922 | 922 | } |
923 | 923 | return $w; |
924 | - } |
|
925 | - |
|
926 | - /** |
|
927 | - * Set the $this->where property |
|
928 | - * @param string $whereStr the WHERE clause string |
|
929 | - * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
930 | - */ |
|
931 | - protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
924 | + } |
|
925 | + |
|
926 | + /** |
|
927 | + * Set the $this->where property |
|
928 | + * @param string $whereStr the WHERE clause string |
|
929 | + * @param string $andOr the separator type used 'AND', 'OR', etc. |
|
930 | + */ |
|
931 | + protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
932 | 932 | if (empty($this->where)){ |
933 | - $this->where = $whereStr; |
|
933 | + $this->where = $whereStr; |
|
934 | 934 | } else { |
935 | - if (substr(trim($this->where), -1) == '('){ |
|
935 | + if (substr(trim($this->where), -1) == '('){ |
|
936 | 936 | $this->where = $this->where . ' ' . $whereStr; |
937 | - } else { |
|
937 | + } else { |
|
938 | 938 | $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
939 | - } |
|
939 | + } |
|
940 | + } |
|
940 | 941 | } |
941 | - } |
|
942 | 942 | |
943 | 943 | |
944 | - /** |
|
945 | - * Set the SQL SELECT for function MIN, MAX, SUM, AVG, COUNT, AVG |
|
946 | - * @param string $clause the clause type like MIN, MAX, etc. |
|
947 | - * @see DatabaseQueryBuilder::min |
|
948 | - * @see DatabaseQueryBuilder::max |
|
949 | - * @see DatabaseQueryBuilder::sum |
|
950 | - * @see DatabaseQueryBuilder::count |
|
951 | - * @see DatabaseQueryBuilder::avg |
|
952 | - * @return object |
|
953 | - */ |
|
944 | + /** |
|
945 | + * Set the SQL SELECT for function MIN, MAX, SUM, AVG, COUNT, AVG |
|
946 | + * @param string $clause the clause type like MIN, MAX, etc. |
|
947 | + * @see DatabaseQueryBuilder::min |
|
948 | + * @see DatabaseQueryBuilder::max |
|
949 | + * @see DatabaseQueryBuilder::sum |
|
950 | + * @see DatabaseQueryBuilder::count |
|
951 | + * @see DatabaseQueryBuilder::avg |
|
952 | + * @return object |
|
953 | + */ |
|
954 | 954 | protected function select_min_max_sum_count_avg($clause, $field, $name = null){ |
955 | - $clause = strtoupper($clause); |
|
956 | - $func = $clause . '(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
957 | - $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
958 | - return $this; |
|
955 | + $clause = strtoupper($clause); |
|
956 | + $func = $clause . '(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
|
957 | + $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
|
958 | + return $this; |
|
959 | 959 | } |
960 | 960 | } |
@@ -23,94 +23,94 @@ 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 DatabaseQueryBuilder{ |
|
26 | + class DatabaseQueryBuilder { |
|
27 | 27 | /** |
28 | 28 | * The SQL SELECT statment |
29 | 29 | * @var string |
30 | 30 | */ |
31 | - private $select = '*'; |
|
31 | + private $select = '*'; |
|
32 | 32 | |
33 | 33 | /** |
34 | 34 | * The SQL FROM statment |
35 | 35 | * @var string |
36 | 36 | */ |
37 | - private $from = null; |
|
37 | + private $from = null; |
|
38 | 38 | |
39 | 39 | /** |
40 | 40 | * The SQL WHERE statment |
41 | 41 | * @var string |
42 | 42 | */ |
43 | - private $where = null; |
|
43 | + private $where = null; |
|
44 | 44 | |
45 | 45 | /** |
46 | 46 | * The SQL LIMIT statment |
47 | 47 | * @var string |
48 | 48 | */ |
49 | - private $limit = null; |
|
49 | + private $limit = null; |
|
50 | 50 | |
51 | 51 | /** |
52 | 52 | * The SQL JOIN statment |
53 | 53 | * @var string |
54 | 54 | */ |
55 | - private $join = null; |
|
55 | + private $join = null; |
|
56 | 56 | |
57 | 57 | /** |
58 | 58 | * The SQL ORDER BY statment |
59 | 59 | * @var string |
60 | 60 | */ |
61 | - private $orderBy = null; |
|
61 | + private $orderBy = null; |
|
62 | 62 | |
63 | 63 | /** |
64 | 64 | * The SQL GROUP BY statment |
65 | 65 | * @var string |
66 | 66 | */ |
67 | - private $groupBy = null; |
|
67 | + private $groupBy = null; |
|
68 | 68 | |
69 | 69 | /** |
70 | 70 | * The SQL HAVING statment |
71 | 71 | * @var string |
72 | 72 | */ |
73 | - private $having = null; |
|
73 | + private $having = null; |
|
74 | 74 | |
75 | 75 | /** |
76 | 76 | * The full SQL query statment after build for each command |
77 | 77 | * @var string |
78 | 78 | */ |
79 | - private $query = null; |
|
79 | + private $query = null; |
|
80 | 80 | |
81 | 81 | /** |
82 | 82 | * The list of SQL valid operators |
83 | 83 | * @var array |
84 | 84 | */ |
85 | - private $operatorList = array('=','!=','<','>','<=','>=','<>'); |
|
85 | + private $operatorList = array('=', '!=', '<', '>', '<=', '>=', '<>'); |
|
86 | 86 | |
87 | 87 | |
88 | 88 | /** |
89 | 89 | * The prefix used in each database table |
90 | 90 | * @var string |
91 | 91 | */ |
92 | - private $prefix = null; |
|
92 | + private $prefix = null; |
|
93 | 93 | |
94 | 94 | |
95 | 95 | /** |
96 | 96 | * The PDO instance |
97 | 97 | * @var object |
98 | 98 | */ |
99 | - private $pdo = null; |
|
99 | + private $pdo = null; |
|
100 | 100 | |
101 | 101 | /** |
102 | 102 | * The database driver name used |
103 | 103 | * @var string |
104 | 104 | */ |
105 | - private $driver = null; |
|
105 | + private $driver = null; |
|
106 | 106 | |
107 | 107 | |
108 | 108 | /** |
109 | 109 | * Construct new DatabaseQueryBuilder |
110 | 110 | * @param object $pdo the PDO object |
111 | 111 | */ |
112 | - public function __construct(PDO $pdo = null){ |
|
113 | - if (is_object($pdo)){ |
|
112 | + public function __construct(PDO $pdo = null) { |
|
113 | + if (is_object($pdo)) { |
|
114 | 114 | $this->setPdo($pdo); |
115 | 115 | } |
116 | 116 | } |
@@ -120,10 +120,10 @@ discard block |
||
120 | 120 | * @param string|array $table the table name or array of table list |
121 | 121 | * @return object the current DatabaseQueryBuilder instance |
122 | 122 | */ |
123 | - public function from($table){ |
|
124 | - if (is_array($table)){ |
|
123 | + public function from($table) { |
|
124 | + if (is_array($table)) { |
|
125 | 125 | $froms = ''; |
126 | - foreach($table as $key){ |
|
126 | + foreach ($table as $key) { |
|
127 | 127 | $froms .= $this->getPrefix() . $key . ', '; |
128 | 128 | } |
129 | 129 | $this->from = rtrim($froms, ', '); |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | * @param string|array $fields the field name or array of field list |
139 | 139 | * @return object the current DatabaseQueryBuilder instance |
140 | 140 | */ |
141 | - public function select($fields){ |
|
141 | + public function select($fields) { |
|
142 | 142 | $select = (is_array($fields) ? implode(', ', $fields) : $fields); |
143 | 143 | $this->select = (($this->select == '*' || empty($this->select)) ? $select : $this->select . ', ' . $select); |
144 | 144 | return $this; |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | * @param string $field the field name to distinct |
150 | 150 | * @return object the current DatabaseQueryBuilder instance |
151 | 151 | */ |
152 | - public function distinct($field){ |
|
152 | + public function distinct($field) { |
|
153 | 153 | $distinct = ' DISTINCT ' . $field; |
154 | 154 | $this->select = (($this->select == '*' || empty($this->select)) ? $distinct : $this->select . ', ' . $distinct); |
155 | 155 | return $this; |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | * @param string $name if is not null represent the alias used for this field in the result |
162 | 162 | * @return object the current DatabaseQueryBuilder instance |
163 | 163 | */ |
164 | - public function count($field = '*', $name = null){ |
|
164 | + public function count($field = '*', $name = null) { |
|
165 | 165 | return $this->select_min_max_sum_count_avg('COUNT', $field, $name); |
166 | 166 | } |
167 | 167 | |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | * @param string $name if is not null represent the alias used for this field in the result |
172 | 172 | * @return object the current DatabaseQueryBuilder instance |
173 | 173 | */ |
174 | - public function min($field, $name = null){ |
|
174 | + public function min($field, $name = null) { |
|
175 | 175 | return $this->select_min_max_sum_count_avg('MIN', $field, $name); |
176 | 176 | } |
177 | 177 | |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | * @param string $name if is not null represent the alias used for this field in the result |
182 | 182 | * @return object the current DatabaseQueryBuilder instance |
183 | 183 | */ |
184 | - public function max($field, $name = null){ |
|
184 | + public function max($field, $name = null) { |
|
185 | 185 | return $this->select_min_max_sum_count_avg('MAX', $field, $name); |
186 | 186 | } |
187 | 187 | |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | * @param string $name if is not null represent the alias used for this field in the result |
192 | 192 | * @return object the current DatabaseQueryBuilder instance |
193 | 193 | */ |
194 | - public function sum($field, $name = null){ |
|
194 | + public function sum($field, $name = null) { |
|
195 | 195 | return $this->select_min_max_sum_count_avg('SUM', $field, $name); |
196 | 196 | } |
197 | 197 | |
@@ -201,7 +201,7 @@ discard block |
||
201 | 201 | * @param string $name if is not null represent the alias used for this field in the result |
202 | 202 | * @return object the current DatabaseQueryBuilder instance |
203 | 203 | */ |
204 | - public function avg($field, $name = null){ |
|
204 | + public function avg($field, $name = null) { |
|
205 | 205 | return $this->select_min_max_sum_count_avg('AVG', $field, $name); |
206 | 206 | } |
207 | 207 | |
@@ -215,18 +215,18 @@ discard block |
||
215 | 215 | * @param string $type the type of join (INNER, LEFT, RIGHT) |
216 | 216 | * @return object the current DatabaseQueryBuilder instance |
217 | 217 | */ |
218 | - public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){ |
|
218 | + public function join($table, $field1 = null, $op = null, $field2 = null, $type = '') { |
|
219 | 219 | $on = $field1; |
220 | 220 | $table = $this->getPrefix() . $table; |
221 | - if (! is_null($op)){ |
|
222 | - $on = (! in_array($op, $this->operatorList) |
|
221 | + if (!is_null($op)) { |
|
222 | + $on = (!in_array($op, $this->operatorList) |
|
223 | 223 | ? ($this->getPrefix() . $field1 . ' = ' . $this->getPrefix() . $op) |
224 | 224 | : ($this->getPrefix() . $field1 . ' ' . $op . ' ' . $this->getPrefix() . $field2)); |
225 | 225 | } |
226 | - if (empty($this->join)){ |
|
226 | + if (empty($this->join)) { |
|
227 | 227 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
228 | 228 | } |
229 | - else{ |
|
229 | + else { |
|
230 | 230 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
231 | 231 | } |
232 | 232 | return $this; |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | * @see DatabaseQueryBuilder::join() |
238 | 238 | * @return object the current DatabaseQueryBuilder instance |
239 | 239 | */ |
240 | - public function innerJoin($table, $field1, $op = null, $field2 = ''){ |
|
240 | + public function innerJoin($table, $field1, $op = null, $field2 = '') { |
|
241 | 241 | return $this->join($table, $field1, $op, $field2, 'INNER '); |
242 | 242 | } |
243 | 243 | |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | * @see DatabaseQueryBuilder::join() |
247 | 247 | * @return object the current DatabaseQueryBuilder instance |
248 | 248 | */ |
249 | - public function leftJoin($table, $field1, $op = null, $field2 = ''){ |
|
249 | + public function leftJoin($table, $field1, $op = null, $field2 = '') { |
|
250 | 250 | return $this->join($table, $field1, $op, $field2, 'LEFT '); |
251 | 251 | } |
252 | 252 | |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | * @see DatabaseQueryBuilder::join() |
256 | 256 | * @return object the current DatabaseQueryBuilder instance |
257 | 257 | */ |
258 | - public function rightJoin($table, $field1, $op = null, $field2 = ''){ |
|
258 | + public function rightJoin($table, $field1, $op = null, $field2 = '') { |
|
259 | 259 | return $this->join($table, $field1, $op, $field2, 'RIGHT '); |
260 | 260 | } |
261 | 261 | |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | * @see DatabaseQueryBuilder::join() |
265 | 265 | * @return object the current DatabaseQueryBuilder instance |
266 | 266 | */ |
267 | - public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
267 | + public function fullOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
268 | 268 | return $this->join($table, $field1, $op, $field2, 'FULL OUTER '); |
269 | 269 | } |
270 | 270 | |
@@ -273,7 +273,7 @@ discard block |
||
273 | 273 | * @see DatabaseQueryBuilder::join() |
274 | 274 | * @return object the current DatabaseQueryBuilder instance |
275 | 275 | */ |
276 | - public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
276 | + public function leftOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
277 | 277 | return $this->join($table, $field1, $op, $field2, 'LEFT OUTER '); |
278 | 278 | } |
279 | 279 | |
@@ -282,7 +282,7 @@ discard block |
||
282 | 282 | * @see DatabaseQueryBuilder::join() |
283 | 283 | * @return object the current DatabaseQueryBuilder instance |
284 | 284 | */ |
285 | - public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){ |
|
285 | + public function rightOuterJoin($table, $field1, $op = null, $field2 = '') { |
|
286 | 286 | return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER '); |
287 | 287 | } |
288 | 288 | |
@@ -292,13 +292,13 @@ discard block |
||
292 | 292 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
293 | 293 | * @return object the current DatabaseQueryBuilder instance |
294 | 294 | */ |
295 | - public function whereIsNull($field, $andOr = 'AND'){ |
|
296 | - if (is_array($field)){ |
|
297 | - foreach($field as $f){ |
|
295 | + public function whereIsNull($field, $andOr = 'AND') { |
|
296 | + if (is_array($field)) { |
|
297 | + foreach ($field as $f) { |
|
298 | 298 | $this->whereIsNull($f, $andOr); |
299 | 299 | } |
300 | 300 | } else { |
301 | - $this->setWhereStr($field.' IS NULL ', $andOr); |
|
301 | + $this->setWhereStr($field . ' IS NULL ', $andOr); |
|
302 | 302 | } |
303 | 303 | return $this; |
304 | 304 | } |
@@ -309,13 +309,13 @@ discard block |
||
309 | 309 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
310 | 310 | * @return object the current DatabaseQueryBuilder instance |
311 | 311 | */ |
312 | - public function whereIsNotNull($field, $andOr = 'AND'){ |
|
313 | - if (is_array($field)){ |
|
314 | - foreach($field as $f){ |
|
312 | + public function whereIsNotNull($field, $andOr = 'AND') { |
|
313 | + if (is_array($field)) { |
|
314 | + foreach ($field as $f) { |
|
315 | 315 | $this->whereIsNotNull($f, $andOr); |
316 | 316 | } |
317 | 317 | } else { |
318 | - $this->setWhereStr($field.' IS NOT NULL ', $andOr); |
|
318 | + $this->setWhereStr($field . ' IS NOT NULL ', $andOr); |
|
319 | 319 | } |
320 | 320 | return $this; |
321 | 321 | } |
@@ -330,13 +330,13 @@ discard block |
||
330 | 330 | * @param boolean $escape whether to escape or not the $val |
331 | 331 | * @return object the current DatabaseQueryBuilder instance |
332 | 332 | */ |
333 | - public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){ |
|
333 | + public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true) { |
|
334 | 334 | $whereStr = ''; |
335 | - if (is_array($where)){ |
|
335 | + if (is_array($where)) { |
|
336 | 336 | $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
337 | 337 | } |
338 | - else{ |
|
339 | - if (is_array($op)){ |
|
338 | + else { |
|
339 | + if (is_array($op)) { |
|
340 | 340 | $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
341 | 341 | } else { |
342 | 342 | $whereStr = $this->getWhereStrForOperator($where, $op, $val, $type, $escape = true); |
@@ -351,7 +351,7 @@ discard block |
||
351 | 351 | * @see DatabaseQueryBuilder::where() |
352 | 352 | * @return object the current DatabaseQueryBuilder instance |
353 | 353 | */ |
354 | - public function orWhere($where, $op = null, $val = null, $escape = true){ |
|
354 | + public function orWhere($where, $op = null, $val = null, $escape = true) { |
|
355 | 355 | return $this->where($where, $op, $val, '', 'OR', $escape); |
356 | 356 | } |
357 | 357 | |
@@ -361,7 +361,7 @@ discard block |
||
361 | 361 | * @see DatabaseQueryBuilder::where() |
362 | 362 | * @return object the current DatabaseQueryBuilder instance |
363 | 363 | */ |
364 | - public function notWhere($where, $op = null, $val = null, $escape = true){ |
|
364 | + public function notWhere($where, $op = null, $val = null, $escape = true) { |
|
365 | 365 | return $this->where($where, $op, $val, 'NOT ', 'AND', $escape); |
366 | 366 | } |
367 | 367 | |
@@ -370,7 +370,7 @@ discard block |
||
370 | 370 | * @see DatabaseQueryBuilder::where() |
371 | 371 | * @return object the current DatabaseQueryBuilder instance |
372 | 372 | */ |
373 | - public function orNotWhere($where, $op = null, $val = null, $escape = true){ |
|
373 | + public function orNotWhere($where, $op = null, $val = null, $escape = true) { |
|
374 | 374 | return $this->where($where, $op, $val, 'NOT ', 'OR', $escape); |
375 | 375 | } |
376 | 376 | |
@@ -380,11 +380,11 @@ discard block |
||
380 | 380 | * @param string $andOr the multiple conditions separator (AND, OR, etc.) |
381 | 381 | * @return object the current DatabaseQueryBuilder instance |
382 | 382 | */ |
383 | - public function groupStart($type = '', $andOr = ' AND'){ |
|
384 | - if (empty($this->where)){ |
|
383 | + public function groupStart($type = '', $andOr = ' AND') { |
|
384 | + if (empty($this->where)) { |
|
385 | 385 | $this->where = $type . ' ('; |
386 | 386 | } else { |
387 | - if (substr(trim($this->where), -1) == '('){ |
|
387 | + if (substr(trim($this->where), -1) == '(') { |
|
388 | 388 | $this->where .= $type . ' ('; |
389 | 389 | } else { |
390 | 390 | $this->where .= $andOr . ' ' . $type . ' ('; |
@@ -398,7 +398,7 @@ discard block |
||
398 | 398 | * @see DatabaseQueryBuilder::groupStart() |
399 | 399 | * @return object the current DatabaseQueryBuilder instance |
400 | 400 | */ |
401 | - public function notGroupStart(){ |
|
401 | + public function notGroupStart() { |
|
402 | 402 | return $this->groupStart('NOT'); |
403 | 403 | } |
404 | 404 | |
@@ -407,7 +407,7 @@ discard block |
||
407 | 407 | * @see DatabaseQueryBuilder::groupStart() |
408 | 408 | * @return object the current DatabaseQueryBuilder instance |
409 | 409 | */ |
410 | - public function orGroupStart(){ |
|
410 | + public function orGroupStart() { |
|
411 | 411 | return $this->groupStart('', ' OR'); |
412 | 412 | } |
413 | 413 | |
@@ -416,7 +416,7 @@ discard block |
||
416 | 416 | * @see DatabaseQueryBuilder::groupStart() |
417 | 417 | * @return object the current DatabaseQueryBuilder instance |
418 | 418 | */ |
419 | - public function orNotGroupStart(){ |
|
419 | + public function orNotGroupStart() { |
|
420 | 420 | return $this->groupStart('NOT', ' OR'); |
421 | 421 | } |
422 | 422 | |
@@ -424,7 +424,7 @@ discard block |
||
424 | 424 | * Close the parenthesis for the grouped SQL |
425 | 425 | * @return object the current DatabaseQueryBuilder instance |
426 | 426 | */ |
427 | - public function groupEnd(){ |
|
427 | + public function groupEnd() { |
|
428 | 428 | $this->where .= ')'; |
429 | 429 | return $this; |
430 | 430 | } |
@@ -438,10 +438,10 @@ discard block |
||
438 | 438 | * @param boolean $escape whether to escape or not the values |
439 | 439 | * @return object the current DatabaseQueryBuilder instance |
440 | 440 | */ |
441 | - public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){ |
|
441 | + public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true) { |
|
442 | 442 | $_keys = array(); |
443 | - foreach ($keys as $k => $v){ |
|
444 | - if (is_null($v)){ |
|
443 | + foreach ($keys as $k => $v) { |
|
444 | + if (is_null($v)) { |
|
445 | 445 | $v = ''; |
446 | 446 | } |
447 | 447 | $_keys[] = (is_numeric($v) ? $v : $this->escape($v, $escape)); |
@@ -457,7 +457,7 @@ discard block |
||
457 | 457 | * @see DatabaseQueryBuilder::in() |
458 | 458 | * @return object the current DatabaseQueryBuilder instance |
459 | 459 | */ |
460 | - public function notIn($field, array $keys, $escape = true){ |
|
460 | + public function notIn($field, array $keys, $escape = true) { |
|
461 | 461 | return $this->in($field, $keys, 'NOT ', 'AND', $escape); |
462 | 462 | } |
463 | 463 | |
@@ -466,7 +466,7 @@ discard block |
||
466 | 466 | * @see DatabaseQueryBuilder::in() |
467 | 467 | * @return object the current DatabaseQueryBuilder instance |
468 | 468 | */ |
469 | - public function orIn($field, array $keys, $escape = true){ |
|
469 | + public function orIn($field, array $keys, $escape = true) { |
|
470 | 470 | return $this->in($field, $keys, '', 'OR', $escape); |
471 | 471 | } |
472 | 472 | |
@@ -475,7 +475,7 @@ discard block |
||
475 | 475 | * @see DatabaseQueryBuilder::in() |
476 | 476 | * @return object the current DatabaseQueryBuilder instance |
477 | 477 | */ |
478 | - public function orNotIn($field, array $keys, $escape = true){ |
|
478 | + public function orNotIn($field, array $keys, $escape = true) { |
|
479 | 479 | return $this->in($field, $keys, 'NOT ', 'OR', $escape); |
480 | 480 | } |
481 | 481 | |
@@ -489,11 +489,11 @@ discard block |
||
489 | 489 | * @param boolean $escape whether to escape or not the values |
490 | 490 | * @return object the current DatabaseQueryBuilder instance |
491 | 491 | */ |
492 | - public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){ |
|
493 | - if (is_null($value1)){ |
|
492 | + public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true) { |
|
493 | + if (is_null($value1)) { |
|
494 | 494 | $value1 = ''; |
495 | 495 | } |
496 | - if (is_null($value2)){ |
|
496 | + if (is_null($value2)) { |
|
497 | 497 | $value2 = ''; |
498 | 498 | } |
499 | 499 | $whereStr = $field . ' ' . $type . ' BETWEEN ' . $this->escape($value1, $escape) . ' AND ' . $this->escape($value2, $escape); |
@@ -506,7 +506,7 @@ discard block |
||
506 | 506 | * @see DatabaseQueryBuilder::between() |
507 | 507 | * @return object the current DatabaseQueryBuilder instance |
508 | 508 | */ |
509 | - public function notBetween($field, $value1, $value2, $escape = true){ |
|
509 | + public function notBetween($field, $value1, $value2, $escape = true) { |
|
510 | 510 | return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape); |
511 | 511 | } |
512 | 512 | |
@@ -515,7 +515,7 @@ discard block |
||
515 | 515 | * @see DatabaseQueryBuilder::between() |
516 | 516 | * @return object the current DatabaseQueryBuilder instance |
517 | 517 | */ |
518 | - public function orBetween($field, $value1, $value2, $escape = true){ |
|
518 | + public function orBetween($field, $value1, $value2, $escape = true) { |
|
519 | 519 | return $this->between($field, $value1, $value2, '', 'OR', $escape); |
520 | 520 | } |
521 | 521 | |
@@ -524,7 +524,7 @@ discard block |
||
524 | 524 | * @see DatabaseQueryBuilder::between() |
525 | 525 | * @return object the current DatabaseQueryBuilder instance |
526 | 526 | */ |
527 | - public function orNotBetween($field, $value1, $value2, $escape = true){ |
|
527 | + public function orNotBetween($field, $value1, $value2, $escape = true) { |
|
528 | 528 | return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape); |
529 | 529 | } |
530 | 530 | |
@@ -537,8 +537,8 @@ discard block |
||
537 | 537 | * @param boolean $escape whether to escape or not the values |
538 | 538 | * @return object the current DatabaseQueryBuilder instance |
539 | 539 | */ |
540 | - public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){ |
|
541 | - if (empty($data)){ |
|
540 | + public function like($field, $data, $type = '', $andOr = 'AND', $escape = true) { |
|
541 | + if (empty($data)) { |
|
542 | 542 | $data = ''; |
543 | 543 | } |
544 | 544 | $this->setWhereStr($field . ' ' . $type . ' LIKE ' . ($this->escape($data, $escape)), $andOr); |
@@ -550,7 +550,7 @@ discard block |
||
550 | 550 | * @see DatabaseQueryBuilder::like() |
551 | 551 | * @return object the current DatabaseQueryBuilder instance |
552 | 552 | */ |
553 | - public function orLike($field, $data, $escape = true){ |
|
553 | + public function orLike($field, $data, $escape = true) { |
|
554 | 554 | return $this->like($field, $data, '', 'OR', $escape); |
555 | 555 | } |
556 | 556 | |
@@ -559,7 +559,7 @@ discard block |
||
559 | 559 | * @see DatabaseQueryBuilder::like() |
560 | 560 | * @return object the current DatabaseQueryBuilder instance |
561 | 561 | */ |
562 | - public function notLike($field, $data, $escape = true){ |
|
562 | + public function notLike($field, $data, $escape = true) { |
|
563 | 563 | return $this->like($field, $data, 'NOT ', 'AND', $escape); |
564 | 564 | } |
565 | 565 | |
@@ -568,7 +568,7 @@ discard block |
||
568 | 568 | * @see DatabaseQueryBuilder::like() |
569 | 569 | * @return object the current DatabaseQueryBuilder instance |
570 | 570 | */ |
571 | - public function orNotLike($field, $data, $escape = true){ |
|
571 | + public function orNotLike($field, $data, $escape = true) { |
|
572 | 572 | return $this->like($field, $data, 'NOT ', 'OR', $escape); |
573 | 573 | } |
574 | 574 | |
@@ -579,14 +579,14 @@ discard block |
||
579 | 579 | * @param int $limitEnd the limit count |
580 | 580 | * @return object the current DatabaseQueryBuilder instance |
581 | 581 | */ |
582 | - public function limit($limit, $limitEnd = null){ |
|
583 | - if (empty($limit)){ |
|
582 | + public function limit($limit, $limitEnd = null) { |
|
583 | + if (empty($limit)) { |
|
584 | 584 | $limit = 0; |
585 | 585 | } |
586 | - if (! is_null($limitEnd)){ |
|
586 | + if (!is_null($limitEnd)) { |
|
587 | 587 | $this->limit = $limit . ', ' . $limitEnd; |
588 | 588 | } |
589 | - else{ |
|
589 | + else { |
|
590 | 590 | $this->limit = $limit; |
591 | 591 | } |
592 | 592 | return $this; |
@@ -598,11 +598,11 @@ discard block |
||
598 | 598 | * @param string $orderDir the order direction (ASC or DESC) |
599 | 599 | * @return object the current DatabaseQueryBuilder instance |
600 | 600 | */ |
601 | - public function orderBy($orderBy, $orderDir = ' ASC'){ |
|
602 | - if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
|
601 | + public function orderBy($orderBy, $orderDir = ' ASC') { |
|
602 | + if (stristr($orderBy, ' ') || $orderBy == 'rand()') { |
|
603 | 603 | $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
604 | 604 | } |
605 | - else{ |
|
605 | + else { |
|
606 | 606 | $this->orderBy = empty($this->orderBy) |
607 | 607 | ? ($orderBy . ' ' . strtoupper($orderDir)) |
608 | 608 | : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
@@ -615,11 +615,11 @@ discard block |
||
615 | 615 | * @param string|array $field the field name used or array of field list |
616 | 616 | * @return object the current DatabaseQueryBuilder instance |
617 | 617 | */ |
618 | - public function groupBy($field){ |
|
619 | - if (is_array($field)){ |
|
618 | + public function groupBy($field) { |
|
619 | + if (is_array($field)) { |
|
620 | 620 | $this->groupBy = implode(', ', $field); |
621 | 621 | } |
622 | - else{ |
|
622 | + else { |
|
623 | 623 | $this->groupBy = $field; |
624 | 624 | } |
625 | 625 | return $this; |
@@ -633,18 +633,18 @@ discard block |
||
633 | 633 | * @param boolean $escape whether to escape or not the values |
634 | 634 | * @return object the current DatabaseQueryBuilder instance |
635 | 635 | */ |
636 | - public function having($field, $op = null, $val = null, $escape = true){ |
|
637 | - if (is_array($op)){ |
|
636 | + public function having($field, $op = null, $val = null, $escape = true) { |
|
637 | + if (is_array($op)) { |
|
638 | 638 | $this->having = $this->getHavingStrIfOperatorIsArray($field, $op, $escape); |
639 | 639 | } |
640 | - else if (! in_array($op, $this->operatorList)){ |
|
641 | - if (is_null($op)){ |
|
640 | + else if (!in_array($op, $this->operatorList)) { |
|
641 | + if (is_null($op)) { |
|
642 | 642 | $op = ''; |
643 | 643 | } |
644 | 644 | $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
645 | 645 | } |
646 | - else{ |
|
647 | - if (is_null($val)){ |
|
646 | + else { |
|
647 | + if (is_null($val)) { |
|
648 | 648 | $val = ''; |
649 | 649 | } |
650 | 650 | $this->having = $field . ' ' . $op . ' ' . ($this->escape($val, $escape)); |
@@ -658,7 +658,7 @@ discard block |
||
658 | 658 | * @param boolean $escape whether to escape or not the values |
659 | 659 | * @return object the current DatabaseQueryBuilder instance |
660 | 660 | */ |
661 | - public function insert($data = array(), $escape = true){ |
|
661 | + public function insert($data = array(), $escape = true) { |
|
662 | 662 | $columns = array_keys($data); |
663 | 663 | $column = implode(', ', $columns); |
664 | 664 | $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data)); |
@@ -673,22 +673,22 @@ discard block |
||
673 | 673 | * @param boolean $escape whether to escape or not the values |
674 | 674 | * @return object the current DatabaseQueryBuilder instance |
675 | 675 | */ |
676 | - public function update($data = array(), $escape = true){ |
|
676 | + public function update($data = array(), $escape = true) { |
|
677 | 677 | $query = 'UPDATE ' . $this->from . ' SET '; |
678 | 678 | $values = array(); |
679 | - foreach ($data as $column => $val){ |
|
679 | + foreach ($data as $column => $val) { |
|
680 | 680 | $values[] = $column . ' = ' . ($this->escape($val, $escape)); |
681 | 681 | } |
682 | 682 | $query .= implode(', ', $values); |
683 | - if (! empty($this->where)){ |
|
683 | + if (!empty($this->where)) { |
|
684 | 684 | $query .= ' WHERE ' . $this->where; |
685 | 685 | } |
686 | 686 | |
687 | - if (! empty($this->orderBy)){ |
|
687 | + if (!empty($this->orderBy)) { |
|
688 | 688 | $query .= ' ORDER BY ' . $this->orderBy; |
689 | 689 | } |
690 | 690 | |
691 | - if (! empty($this->limit)){ |
|
691 | + if (!empty($this->limit)) { |
|
692 | 692 | $query .= ' LIMIT ' . $this->limit; |
693 | 693 | } |
694 | 694 | $this->query = $query; |
@@ -699,22 +699,22 @@ discard block |
||
699 | 699 | * Delete the record in database |
700 | 700 | * @return object the current DatabaseQueryBuilder instance |
701 | 701 | */ |
702 | - public function delete(){ |
|
702 | + public function delete() { |
|
703 | 703 | $query = 'DELETE FROM ' . $this->from; |
704 | 704 | $isTruncate = $query; |
705 | - if (! empty($this->where)){ |
|
705 | + if (!empty($this->where)) { |
|
706 | 706 | $query .= ' WHERE ' . $this->where; |
707 | 707 | } |
708 | 708 | |
709 | - if (! empty($this->orderBy)){ |
|
709 | + if (!empty($this->orderBy)) { |
|
710 | 710 | $query .= ' ORDER BY ' . $this->orderBy; |
711 | 711 | } |
712 | 712 | |
713 | - if (! empty($this->limit)){ |
|
713 | + if (!empty($this->limit)) { |
|
714 | 714 | $query .= ' LIMIT ' . $this->limit; |
715 | 715 | } |
716 | 716 | |
717 | - if ($isTruncate == $query && $this->driver != 'sqlite'){ |
|
717 | + if ($isTruncate == $query && $this->driver != 'sqlite') { |
|
718 | 718 | $query = 'TRUNCATE TABLE ' . $this->from; |
719 | 719 | } |
720 | 720 | $this->query = $query; |
@@ -727,7 +727,7 @@ discard block |
||
727 | 727 | * @param boolean $escaped whether we can do escape of not |
728 | 728 | * @return mixed the data after escaped or the same data if not |
729 | 729 | */ |
730 | - public function escape($data, $escaped = true){ |
|
730 | + public function escape($data, $escaped = true) { |
|
731 | 731 | return $escaped |
732 | 732 | ? $this->getPdo()->quote(trim($data)) |
733 | 733 | : $data; |
@@ -738,31 +738,31 @@ discard block |
||
738 | 738 | * Return the current SQL query string |
739 | 739 | * @return string |
740 | 740 | */ |
741 | - public function getQuery(){ |
|
741 | + public function getQuery() { |
|
742 | 742 | //INSERT, UPDATE, DELETE already set it, if is the SELECT we need set it now |
743 | - if(empty($this->query)){ |
|
743 | + if (empty($this->query)) { |
|
744 | 744 | $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; |
745 | - if (! empty($this->join)){ |
|
745 | + if (!empty($this->join)) { |
|
746 | 746 | $query .= $this->join; |
747 | 747 | } |
748 | 748 | |
749 | - if (! empty($this->where)){ |
|
749 | + if (!empty($this->where)) { |
|
750 | 750 | $query .= ' WHERE ' . $this->where; |
751 | 751 | } |
752 | 752 | |
753 | - if (! empty($this->groupBy)){ |
|
753 | + if (!empty($this->groupBy)) { |
|
754 | 754 | $query .= ' GROUP BY ' . $this->groupBy; |
755 | 755 | } |
756 | 756 | |
757 | - if (! empty($this->having)){ |
|
757 | + if (!empty($this->having)) { |
|
758 | 758 | $query .= ' HAVING ' . $this->having; |
759 | 759 | } |
760 | 760 | |
761 | - if (! empty($this->orderBy)){ |
|
761 | + if (!empty($this->orderBy)) { |
|
762 | 762 | $query .= ' ORDER BY ' . $this->orderBy; |
763 | 763 | } |
764 | 764 | |
765 | - if (! empty($this->limit)){ |
|
765 | + if (!empty($this->limit)) { |
|
766 | 766 | $query .= ' LIMIT ' . $this->limit; |
767 | 767 | } |
768 | 768 | $this->query = $query; |
@@ -775,7 +775,7 @@ discard block |
||
775 | 775 | * Return the PDO instance |
776 | 776 | * @return object |
777 | 777 | */ |
778 | - public function getPdo(){ |
|
778 | + public function getPdo() { |
|
779 | 779 | return $this->pdo; |
780 | 780 | } |
781 | 781 | |
@@ -784,7 +784,7 @@ discard block |
||
784 | 784 | * @param PDO $pdo the pdo object |
785 | 785 | * @return object DatabaseQueryBuilder |
786 | 786 | */ |
787 | - public function setPdo(PDO $pdo = null){ |
|
787 | + public function setPdo(PDO $pdo = null) { |
|
788 | 788 | $this->pdo = $pdo; |
789 | 789 | return $this; |
790 | 790 | } |
@@ -793,7 +793,7 @@ discard block |
||
793 | 793 | * Return the database table prefix |
794 | 794 | * @return string |
795 | 795 | */ |
796 | - public function getPrefix(){ |
|
796 | + public function getPrefix() { |
|
797 | 797 | return $this->prefix; |
798 | 798 | } |
799 | 799 | |
@@ -802,7 +802,7 @@ discard block |
||
802 | 802 | * @param string $prefix the new prefix |
803 | 803 | * @return object DatabaseQueryBuilder |
804 | 804 | */ |
805 | - public function setPrefix($prefix){ |
|
805 | + public function setPrefix($prefix) { |
|
806 | 806 | $this->prefix = $prefix; |
807 | 807 | return $this; |
808 | 808 | } |
@@ -811,7 +811,7 @@ discard block |
||
811 | 811 | * Return the database driver |
812 | 812 | * @return string |
813 | 813 | */ |
814 | - public function getDriver(){ |
|
814 | + public function getDriver() { |
|
815 | 815 | return $this->driver; |
816 | 816 | } |
817 | 817 | |
@@ -820,7 +820,7 @@ discard block |
||
820 | 820 | * @param string $driver the new driver |
821 | 821 | * @return object DatabaseQueryBuilder |
822 | 822 | */ |
823 | - public function setDriver($driver){ |
|
823 | + public function setDriver($driver) { |
|
824 | 824 | $this->driver = $driver; |
825 | 825 | return $this; |
826 | 826 | } |
@@ -829,7 +829,7 @@ discard block |
||
829 | 829 | * Reset the DatabaseQueryBuilder class attributs to the initial values before each query. |
830 | 830 | * @return object the current DatabaseQueryBuilder instance |
831 | 831 | */ |
832 | - public function reset(){ |
|
832 | + public function reset() { |
|
833 | 833 | $this->select = '*'; |
834 | 834 | $this->from = null; |
835 | 835 | $this->where = null; |
@@ -848,12 +848,12 @@ discard block |
||
848 | 848 | * |
849 | 849 | * @return string |
850 | 850 | */ |
851 | - protected function getHavingStrIfOperatorIsArray($field, $op = null, $escape = true){ |
|
851 | + protected function getHavingStrIfOperatorIsArray($field, $op = null, $escape = true) { |
|
852 | 852 | $x = explode('?', $field); |
853 | 853 | $w = ''; |
854 | - foreach($x as $k => $v){ |
|
855 | - if (!empty($v)){ |
|
856 | - if (! isset($op[$k])){ |
|
854 | + foreach ($x as $k => $v) { |
|
855 | + if (!empty($v)) { |
|
856 | + if (!isset($op[$k])) { |
|
857 | 857 | $op[$k] = ''; |
858 | 858 | } |
859 | 859 | $w .= $v . (isset($op[$k]) ? $this->escape($op[$k], $escape) : ''); |
@@ -869,15 +869,15 @@ discard block |
||
869 | 869 | * |
870 | 870 | * @return string |
871 | 871 | */ |
872 | - protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true){ |
|
872 | + protected function getWhereStrIfIsArray(array $where, $type = '', $andOr = 'AND', $escape = true) { |
|
873 | 873 | $_where = array(); |
874 | - foreach ($where as $column => $data){ |
|
875 | - if (is_null($data)){ |
|
874 | + foreach ($where as $column => $data) { |
|
875 | + if (is_null($data)) { |
|
876 | 876 | $data = ''; |
877 | 877 | } |
878 | 878 | $_where[] = $type . $column . ' = ' . ($this->escape($data, $escape)); |
879 | 879 | } |
880 | - $where = implode(' '.$andOr.' ', $_where); |
|
880 | + $where = implode(' ' . $andOr . ' ', $_where); |
|
881 | 881 | return $where; |
882 | 882 | } |
883 | 883 | |
@@ -887,12 +887,12 @@ discard block |
||
887 | 887 | * |
888 | 888 | * @return string |
889 | 889 | */ |
890 | - protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true){ |
|
890 | + protected function getWhereStrIfOperatorIsArray($where, array $op, $type = '', $escape = true) { |
|
891 | 891 | $x = explode('?', $where); |
892 | 892 | $w = ''; |
893 | - foreach($x as $k => $v){ |
|
894 | - if (! empty($v)){ |
|
895 | - if (isset($op[$k]) && is_null($op[$k])){ |
|
893 | + foreach ($x as $k => $v) { |
|
894 | + if (!empty($v)) { |
|
895 | + if (isset($op[$k]) && is_null($op[$k])) { |
|
896 | 896 | $op[$k] = ''; |
897 | 897 | } |
898 | 898 | $w .= $type . $v . (isset($op[$k]) ? ($this->escape($op[$k], $escape)) : ''); |
@@ -907,15 +907,15 @@ discard block |
||
907 | 907 | * |
908 | 908 | * @return string |
909 | 909 | */ |
910 | - protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true){ |
|
910 | + protected function getWhereStrForOperator($where, $op = null, $val = null, $type = '', $escape = true) { |
|
911 | 911 | $w = ''; |
912 | - if (! in_array((string)$op, $this->operatorList)){ |
|
913 | - if (is_null($op)){ |
|
912 | + if (!in_array((string) $op, $this->operatorList)) { |
|
913 | + if (is_null($op)) { |
|
914 | 914 | $op = ''; |
915 | 915 | } |
916 | 916 | $w = $type . $where . ' = ' . ($this->escape($op, $escape)); |
917 | 917 | } else { |
918 | - if (is_null($val)){ |
|
918 | + if (is_null($val)) { |
|
919 | 919 | $val = ''; |
920 | 920 | } |
921 | 921 | $w = $type . $where . $op . ($this->escape($val, $escape)); |
@@ -928,14 +928,14 @@ discard block |
||
928 | 928 | * @param string $whereStr the WHERE clause string |
929 | 929 | * @param string $andOr the separator type used 'AND', 'OR', etc. |
930 | 930 | */ |
931 | - protected function setWhereStr($whereStr, $andOr = 'AND'){ |
|
932 | - if (empty($this->where)){ |
|
931 | + protected function setWhereStr($whereStr, $andOr = 'AND') { |
|
932 | + if (empty($this->where)) { |
|
933 | 933 | $this->where = $whereStr; |
934 | 934 | } else { |
935 | - if (substr(trim($this->where), -1) == '('){ |
|
935 | + if (substr(trim($this->where), -1) == '(') { |
|
936 | 936 | $this->where = $this->where . ' ' . $whereStr; |
937 | 937 | } else { |
938 | - $this->where = $this->where . ' '.$andOr.' ' . $whereStr; |
|
938 | + $this->where = $this->where . ' ' . $andOr . ' ' . $whereStr; |
|
939 | 939 | } |
940 | 940 | } |
941 | 941 | } |
@@ -951,7 +951,7 @@ discard block |
||
951 | 951 | * @see DatabaseQueryBuilder::avg |
952 | 952 | * @return object |
953 | 953 | */ |
954 | - protected function select_min_max_sum_count_avg($clause, $field, $name = null){ |
|
954 | + protected function select_min_max_sum_count_avg($clause, $field, $name = null) { |
|
955 | 955 | $clause = strtoupper($clause); |
956 | 956 | $func = $clause . '(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); |
957 | 957 | $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func); |
@@ -225,8 +225,7 @@ discard block |
||
225 | 225 | } |
226 | 226 | if (empty($this->join)){ |
227 | 227 | $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
228 | - } |
|
229 | - else{ |
|
228 | + } else{ |
|
230 | 229 | $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; |
231 | 230 | } |
232 | 231 | return $this; |
@@ -334,8 +333,7 @@ discard block |
||
334 | 333 | $whereStr = ''; |
335 | 334 | if (is_array($where)){ |
336 | 335 | $whereStr = $this->getWhereStrIfIsArray($where, $type, $andOr, $escape); |
337 | - } |
|
338 | - else{ |
|
336 | + } else{ |
|
339 | 337 | if (is_array($op)){ |
340 | 338 | $whereStr = $this->getWhereStrIfOperatorIsArray($where, $op, $type, $escape); |
341 | 339 | } else { |
@@ -585,8 +583,7 @@ discard block |
||
585 | 583 | } |
586 | 584 | if (! is_null($limitEnd)){ |
587 | 585 | $this->limit = $limit . ', ' . $limitEnd; |
588 | - } |
|
589 | - else{ |
|
586 | + } else{ |
|
590 | 587 | $this->limit = $limit; |
591 | 588 | } |
592 | 589 | return $this; |
@@ -601,8 +598,7 @@ discard block |
||
601 | 598 | public function orderBy($orderBy, $orderDir = ' ASC'){ |
602 | 599 | if (stristr($orderBy, ' ') || $orderBy == 'rand()'){ |
603 | 600 | $this->orderBy = empty($this->orderBy) ? $orderBy : $this->orderBy . ', ' . $orderBy; |
604 | - } |
|
605 | - else{ |
|
601 | + } else{ |
|
606 | 602 | $this->orderBy = empty($this->orderBy) |
607 | 603 | ? ($orderBy . ' ' . strtoupper($orderDir)) |
608 | 604 | : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir); |
@@ -618,8 +614,7 @@ discard block |
||
618 | 614 | public function groupBy($field){ |
619 | 615 | if (is_array($field)){ |
620 | 616 | $this->groupBy = implode(', ', $field); |
621 | - } |
|
622 | - else{ |
|
617 | + } else{ |
|
623 | 618 | $this->groupBy = $field; |
624 | 619 | } |
625 | 620 | return $this; |
@@ -636,14 +631,12 @@ discard block |
||
636 | 631 | public function having($field, $op = null, $val = null, $escape = true){ |
637 | 632 | if (is_array($op)){ |
638 | 633 | $this->having = $this->getHavingStrIfOperatorIsArray($field, $op, $escape); |
639 | - } |
|
640 | - else if (! in_array($op, $this->operatorList)){ |
|
634 | + } else if (! in_array($op, $this->operatorList)){ |
|
641 | 635 | if (is_null($op)){ |
642 | 636 | $op = ''; |
643 | 637 | } |
644 | 638 | $this->having = $field . ' > ' . ($this->escape($op, $escape)); |
645 | - } |
|
646 | - else{ |
|
639 | + } else{ |
|
647 | 640 | if (is_null($val)){ |
648 | 641 | $val = ''; |
649 | 642 | } |
@@ -1,90 +1,90 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | defined('ROOT_PATH') || exit('Access denied'); |
3 | - /** |
|
4 | - * TNH Framework |
|
5 | - * |
|
6 | - * A simple PHP framework using HMVC architecture |
|
7 | - * |
|
8 | - * This content is released under the GNU GPL License (GPL) |
|
9 | - * |
|
10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
11 | - * |
|
12 | - * This program is free software; you can redistribute it and/or |
|
13 | - * modify it under the terms of the GNU General Public License |
|
14 | - * as published by the Free Software Foundation; either version 3 |
|
15 | - * of the License, or (at your option) any later version. |
|
16 | - * |
|
17 | - * This program is distributed in the hope that it will be useful, |
|
18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | - * GNU General Public License for more details. |
|
21 | - * |
|
22 | - * You should have received a copy of the GNU General Public License |
|
23 | - * along with this program; if not, write to the Free Software |
|
24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | - */ |
|
26 | - class DatabaseQueryRunner{ |
|
3 | + /** |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | + class DatabaseQueryRunner{ |
|
27 | 27 | |
28 | 28 | /** |
29 | - * The logger instance |
|
30 | - * @var object |
|
31 | - */ |
|
29 | + * The logger instance |
|
30 | + * @var object |
|
31 | + */ |
|
32 | 32 | private $logger = null; |
33 | 33 | |
34 | - /** |
|
35 | - * The last query result |
|
36 | - * @var object |
|
37 | - */ |
|
38 | - private $queryResult = null; |
|
34 | + /** |
|
35 | + * The last query result |
|
36 | + * @var object |
|
37 | + */ |
|
38 | + private $queryResult = null; |
|
39 | 39 | |
40 | - /** |
|
41 | - * The benchmark instance |
|
42 | - * @var object |
|
43 | - */ |
|
40 | + /** |
|
41 | + * The benchmark instance |
|
42 | + * @var object |
|
43 | + */ |
|
44 | 44 | private $benchmarkInstance = null; |
45 | 45 | |
46 | 46 | /** |
47 | - * The SQL query statment to execute |
|
48 | - * @var string |
|
49 | - */ |
|
47 | + * The SQL query statment to execute |
|
48 | + * @var string |
|
49 | + */ |
|
50 | 50 | private $query = null; |
51 | 51 | |
52 | 52 | /** |
53 | - * Indicate if we need return result as list (boolean) |
|
53 | + * Indicate if we need return result as list (boolean) |
|
54 | 54 | * or the data used to replace the placeholder (array) |
55 | - * @var array|boolean |
|
56 | - */ |
|
57 | - private $all = true; |
|
55 | + * @var array|boolean |
|
56 | + */ |
|
57 | + private $all = true; |
|
58 | 58 | |
59 | 59 | |
60 | - /** |
|
61 | - * Indicate if we need return result as array or not |
|
62 | - * @var boolean |
|
63 | - */ |
|
64 | - private $returnAsArray = true; |
|
60 | + /** |
|
61 | + * Indicate if we need return result as array or not |
|
62 | + * @var boolean |
|
63 | + */ |
|
64 | + private $returnAsArray = true; |
|
65 | 65 | |
66 | - /** |
|
67 | - * The last PDOStatment instance |
|
68 | - * @var object |
|
69 | - */ |
|
70 | - private $pdoStatment = null; |
|
66 | + /** |
|
67 | + * The last PDOStatment instance |
|
68 | + * @var object |
|
69 | + */ |
|
70 | + private $pdoStatment = null; |
|
71 | 71 | |
72 | - /** |
|
73 | - * The error returned for the last query |
|
74 | - * @var string |
|
75 | - */ |
|
76 | - private $error = null; |
|
72 | + /** |
|
73 | + * The error returned for the last query |
|
74 | + * @var string |
|
75 | + */ |
|
76 | + private $error = null; |
|
77 | 77 | |
78 | 78 | /** |
79 | 79 | * The PDO instance |
80 | 80 | * @var object |
81 | - */ |
|
81 | + */ |
|
82 | 82 | private $pdo = null; |
83 | 83 | |
84 | 84 | /** |
85 | 85 | * The database driver name used |
86 | 86 | * @var string |
87 | - */ |
|
87 | + */ |
|
88 | 88 | private $driver = null; |
89 | 89 | |
90 | 90 | |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | */ |
99 | 99 | public function __construct(PDO $pdo = null, $query = null, $returnAsList = true, $returnAsArray = false){ |
100 | 100 | if (is_object($pdo)){ |
101 | - $this->pdo = $pdo; |
|
101 | + $this->pdo = $pdo; |
|
102 | 102 | } |
103 | 103 | $this->query = $query; |
104 | 104 | $this->returnAsList = $returnAsList; |
@@ -116,16 +116,16 @@ discard block |
||
116 | 116 | //reset instance |
117 | 117 | $this->reset(); |
118 | 118 | |
119 | - //for database query execution time |
|
119 | + //for database query execution time |
|
120 | 120 | $benchmarkMarkerKey = $this->getBenchmarkKey(); |
121 | 121 | if (! is_object($this->benchmarkInstance)){ |
122 | - $this->benchmarkInstance = & class_loader('Benchmark'); |
|
122 | + $this->benchmarkInstance = & class_loader('Benchmark'); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | $this->logger->info( |
126 | - 'Execute SQL query [' . $this->query . '], return type: ' |
|
127 | - . ($this->returnAsArray ? 'ARRAY' : 'OBJECT') .', return as list: ' |
|
128 | - . ($this->returnAsList ? 'YES':'NO') |
|
126 | + 'Execute SQL query [' . $this->query . '], return type: ' |
|
127 | + . ($this->returnAsArray ? 'ARRAY' : 'OBJECT') .', return as list: ' |
|
128 | + . ($this->returnAsList ? 'YES':'NO') |
|
129 | 129 | ); |
130 | 130 | |
131 | 131 | $this->benchmarkInstance->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
@@ -136,101 +136,101 @@ discard block |
||
136 | 136 | $responseTime = $this->benchmarkInstance->elapsedTime( |
137 | 137 | 'DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', |
138 | 138 | 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')' |
139 | - ); |
|
140 | - //TODO use the configuration value for the high response time currently is 1 second |
|
139 | + ); |
|
140 | + //TODO use the configuration value for the high response time currently is 1 second |
|
141 | 141 | if ($responseTime >= 1 ){ |
142 | 142 | $this->logger->warning( |
143 | 143 | 'High response time while processing database query [' . $this->query . ']. |
144 | 144 | The response time is [' .$responseTime. '] sec.' |
145 | - ); |
|
145 | + ); |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | if ($this->pdoStatment !== false){ |
149 | - $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
150 | - if($isSqlSELECTQuery){ |
|
151 | - $this->setResultForSelect(); |
|
152 | - } |
|
153 | - else{ |
|
154 | - $this->setResultForNonSelect(); |
|
155 | - } |
|
156 | - return $this->queryResult; |
|
149 | + $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
150 | + if($isSqlSELECTQuery){ |
|
151 | + $this->setResultForSelect(); |
|
152 | + } |
|
153 | + else{ |
|
154 | + $this->setResultForNonSelect(); |
|
155 | + } |
|
156 | + return $this->queryResult; |
|
157 | 157 | } |
158 | 158 | $this->setQueryRunnerError(); |
159 | 159 | } |
160 | 160 | |
161 | - /** |
|
162 | - * Return the result for SELECT query |
|
163 | - * @see DatabaseQueryRunner::execute |
|
164 | - */ |
|
161 | + /** |
|
162 | + * Return the result for SELECT query |
|
163 | + * @see DatabaseQueryRunner::execute |
|
164 | + */ |
|
165 | 165 | protected function setResultForSelect(){ |
166 | - //if need return all result like list of record |
|
167 | - $result = null; |
|
168 | - $numRows = 0; |
|
169 | - $fetchMode = PDO::FETCH_OBJ; |
|
170 | - if($this->returnAsArray){ |
|
166 | + //if need return all result like list of record |
|
167 | + $result = null; |
|
168 | + $numRows = 0; |
|
169 | + $fetchMode = PDO::FETCH_OBJ; |
|
170 | + if($this->returnAsArray){ |
|
171 | 171 | $fetchMode = PDO::FETCH_ASSOC; |
172 | - } |
|
173 | - if ($this->returnAsList){ |
|
174 | - $result = $this->pdoStatment->fetchAll($fetchMode); |
|
175 | - } |
|
176 | - else{ |
|
177 | - $result = $this->pdoStatment->fetch($fetchMode); |
|
178 | - } |
|
179 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
180 | - if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
172 | + } |
|
173 | + if ($this->returnAsList){ |
|
174 | + $result = $this->pdoStatment->fetchAll($fetchMode); |
|
175 | + } |
|
176 | + else{ |
|
177 | + $result = $this->pdoStatment->fetch($fetchMode); |
|
178 | + } |
|
179 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
180 | + if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
181 | 181 | $numRows = count($result); |
182 | - } |
|
183 | - else{ |
|
182 | + } |
|
183 | + else{ |
|
184 | 184 | $numRows = $this->pdoStatment->rowCount(); |
185 | - } |
|
186 | - if(! is_object($this->queryResult)){ |
|
187 | - $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
188 | - } |
|
189 | - $this->queryResult->setResult($result); |
|
190 | - $this->queryResult->setNumRows($numRows); |
|
185 | + } |
|
186 | + if(! is_object($this->queryResult)){ |
|
187 | + $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
188 | + } |
|
189 | + $this->queryResult->setResult($result); |
|
190 | + $this->queryResult->setNumRows($numRows); |
|
191 | 191 | } |
192 | 192 | |
193 | 193 | /** |
194 | - * Return the result for non SELECT query |
|
195 | - * @see DatabaseQueryRunner::execute |
|
196 | - */ |
|
194 | + * Return the result for non SELECT query |
|
195 | + * @see DatabaseQueryRunner::execute |
|
196 | + */ |
|
197 | 197 | protected function setResultForNonSelect(){ |
198 | - //Sqlite and pgsql always return 0 when using rowCount() |
|
199 | - $result = false; |
|
200 | - $numRows = 0; |
|
201 | - if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
198 | + //Sqlite and pgsql always return 0 when using rowCount() |
|
199 | + $result = false; |
|
200 | + $numRows = 0; |
|
201 | + if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
202 | 202 | $result = true; //to test the result for the query like UPDATE, INSERT, DELETE |
203 | 203 | $numRows = 1; //TODO use the correct method to get the exact affected row |
204 | - } |
|
205 | - else{ |
|
206 | - //to test the result for the query like UPDATE, INSERT, DELETE |
|
207 | - $result = $this->pdoStatment->rowCount() >= 0; |
|
208 | - $numRows = $this->pdoStatment->rowCount(); |
|
209 | - } |
|
210 | - if(! is_object($this->queryResult)){ |
|
211 | - $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
212 | - } |
|
213 | - $this->queryResult->setResult($result); |
|
214 | - $this->queryResult->setNumRows($numRows); |
|
204 | + } |
|
205 | + else{ |
|
206 | + //to test the result for the query like UPDATE, INSERT, DELETE |
|
207 | + $result = $this->pdoStatment->rowCount() >= 0; |
|
208 | + $numRows = $this->pdoStatment->rowCount(); |
|
209 | + } |
|
210 | + if(! is_object($this->queryResult)){ |
|
211 | + $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
|
212 | + } |
|
213 | + $this->queryResult->setResult($result); |
|
214 | + $this->queryResult->setNumRows($numRows); |
|
215 | 215 | } |
216 | 216 | |
217 | 217 | |
218 | - /** |
|
218 | + /** |
|
219 | 219 | * Return the benchmark instance |
220 | 220 | * @return Benchmark |
221 | 221 | */ |
222 | 222 | public function getBenchmark(){ |
223 | - return $this->benchmarkInstance; |
|
223 | + return $this->benchmarkInstance; |
|
224 | 224 | } |
225 | 225 | |
226 | 226 | /** |
227 | 227 | * Set the benchmark instance |
228 | 228 | * @param Benchmark $benchmark the benchmark object |
229 | - * @return object DatabaseQueryRunner |
|
229 | + * @return object DatabaseQueryRunner |
|
230 | 230 | */ |
231 | 231 | public function setBenchmark($benchmark){ |
232 | - $this->benchmarkInstance = $benchmark; |
|
233 | - return $this; |
|
232 | + $this->benchmarkInstance = $benchmark; |
|
233 | + return $this; |
|
234 | 234 | } |
235 | 235 | |
236 | 236 | /** |
@@ -239,18 +239,18 @@ discard block |
||
239 | 239 | * @return object DatabaseQueryResult |
240 | 240 | */ |
241 | 241 | public function getQueryResult(){ |
242 | - return $this->queryResult; |
|
242 | + return $this->queryResult; |
|
243 | 243 | } |
244 | 244 | |
245 | 245 | /** |
246 | 246 | * Set the database query result instance |
247 | 247 | * @param object $queryResult the query result |
248 | 248 | * |
249 | - * @return object DatabaseQueryRunner |
|
249 | + * @return object DatabaseQueryRunner |
|
250 | 250 | */ |
251 | 251 | public function setQueryResult(DatabaseQueryResult $queryResult){ |
252 | - $this->queryResult = $queryResult; |
|
253 | - return $this; |
|
252 | + $this->queryResult = $queryResult; |
|
253 | + return $this; |
|
254 | 254 | } |
255 | 255 | |
256 | 256 | /** |
@@ -258,17 +258,17 @@ discard block |
||
258 | 258 | * @return Log |
259 | 259 | */ |
260 | 260 | public function getLogger(){ |
261 | - return $this->logger; |
|
261 | + return $this->logger; |
|
262 | 262 | } |
263 | 263 | |
264 | 264 | /** |
265 | 265 | * Set the log instance |
266 | 266 | * @param Log $logger the log object |
267 | - * @return object DatabaseQueryRunner |
|
267 | + * @return object DatabaseQueryRunner |
|
268 | 268 | */ |
269 | 269 | public function setLogger($logger){ |
270 | - $this->logger = $logger; |
|
271 | - return $this; |
|
270 | + $this->logger = $logger; |
|
271 | + return $this; |
|
272 | 272 | } |
273 | 273 | |
274 | 274 | /** |
@@ -276,7 +276,7 @@ discard block |
||
276 | 276 | * @return string |
277 | 277 | */ |
278 | 278 | public function getQuery(){ |
279 | - return $this->query; |
|
279 | + return $this->query; |
|
280 | 280 | } |
281 | 281 | |
282 | 282 | /** |
@@ -285,8 +285,8 @@ discard block |
||
285 | 285 | * @return object DatabaseQueryRunner |
286 | 286 | */ |
287 | 287 | public function setQuery($query){ |
288 | - $this->query = $query; |
|
289 | - return $this; |
|
288 | + $this->query = $query; |
|
289 | + return $this; |
|
290 | 290 | } |
291 | 291 | |
292 | 292 | /** |
@@ -295,8 +295,8 @@ discard block |
||
295 | 295 | * @return object DatabaseQueryRunner |
296 | 296 | */ |
297 | 297 | public function setReturnType($returnType){ |
298 | - $this->returnAsList = $returnType; |
|
299 | - return $this; |
|
298 | + $this->returnAsList = $returnType; |
|
299 | + return $this; |
|
300 | 300 | } |
301 | 301 | |
302 | 302 | /** |
@@ -305,8 +305,8 @@ discard block |
||
305 | 305 | * @return object DatabaseQueryRunner |
306 | 306 | */ |
307 | 307 | public function setReturnAsArray($status = true){ |
308 | - $this->returnAsArray = $status; |
|
309 | - return $this; |
|
308 | + $this->returnAsArray = $status; |
|
309 | + return $this; |
|
310 | 310 | } |
311 | 311 | |
312 | 312 | /** |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | * @return string |
315 | 315 | */ |
316 | 316 | public function getQueryError(){ |
317 | - return $this->error; |
|
317 | + return $this->error; |
|
318 | 318 | } |
319 | 319 | |
320 | 320 | /** |
@@ -322,7 +322,7 @@ discard block |
||
322 | 322 | * @return object |
323 | 323 | */ |
324 | 324 | public function getPdo(){ |
325 | - return $this->pdo; |
|
325 | + return $this->pdo; |
|
326 | 326 | } |
327 | 327 | |
328 | 328 | /** |
@@ -331,16 +331,16 @@ discard block |
||
331 | 331 | * @return object DatabaseQueryRunner |
332 | 332 | */ |
333 | 333 | public function setPdo(PDO $pdo = null){ |
334 | - $this->pdo = $pdo; |
|
335 | - return $this; |
|
334 | + $this->pdo = $pdo; |
|
335 | + return $this; |
|
336 | 336 | } |
337 | 337 | |
338 | - /** |
|
339 | - * Return the database driver |
|
340 | - * @return string |
|
341 | - */ |
|
338 | + /** |
|
339 | + * Return the database driver |
|
340 | + * @return string |
|
341 | + */ |
|
342 | 342 | public function getDriver(){ |
343 | - return $this->driver; |
|
343 | + return $this->driver; |
|
344 | 344 | } |
345 | 345 | |
346 | 346 | /** |
@@ -349,8 +349,8 @@ discard block |
||
349 | 349 | * @return object DatabaseQueryRunner |
350 | 350 | */ |
351 | 351 | public function setDriver($driver){ |
352 | - $this->driver = $driver; |
|
353 | - return $this; |
|
352 | + $this->driver = $driver; |
|
353 | + return $this; |
|
354 | 354 | } |
355 | 355 | |
356 | 356 | /** |
@@ -359,18 +359,18 @@ discard block |
||
359 | 359 | * @return string |
360 | 360 | */ |
361 | 361 | protected function getBenchmarkKey(){ |
362 | - return md5($this->query . $this->returnAsList . $this->returnAsArray); |
|
362 | + return md5($this->query . $this->returnAsList . $this->returnAsArray); |
|
363 | 363 | } |
364 | 364 | |
365 | 365 | /** |
366 | 366 | * Set error for database query execution |
367 | 367 | */ |
368 | 368 | protected function setQueryRunnerError(){ |
369 | - $error = $this->pdo->errorInfo(); |
|
370 | - $this->error = isset($error[2]) ? $error[2] : ''; |
|
371 | - $this->logger->error('The database query execution got an error: ' . stringfy_vars($error)); |
|
372 | - //show error message |
|
373 | - show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
|
369 | + $error = $this->pdo->errorInfo(); |
|
370 | + $this->error = isset($error[2]) ? $error[2] : ''; |
|
371 | + $this->logger->error('The database query execution got an error: ' . stringfy_vars($error)); |
|
372 | + //show error message |
|
373 | + show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error'); |
|
374 | 374 | } |
375 | 375 | |
376 | 376 | /** |
@@ -378,19 +378,19 @@ discard block |
||
378 | 378 | * @param object $logger the Log instance if not null |
379 | 379 | */ |
380 | 380 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
381 | - if ($logger !== null){ |
|
381 | + if ($logger !== null){ |
|
382 | 382 | $this->logger = $logger; |
383 | - } |
|
384 | - else{ |
|
385 | - $this->logger =& class_loader('Log', 'classes'); |
|
386 | - $this->logger->setLogger('Library::DatabaseQueryRunner'); |
|
387 | - } |
|
383 | + } |
|
384 | + else{ |
|
385 | + $this->logger =& class_loader('Log', 'classes'); |
|
386 | + $this->logger->setLogger('Library::DatabaseQueryRunner'); |
|
387 | + } |
|
388 | 388 | } |
389 | 389 | |
390 | 390 | |
391 | 391 | /** |
392 | - * Reset the instance before run each query |
|
393 | - */ |
|
392 | + * Reset the instance before run each query |
|
393 | + */ |
|
394 | 394 | private function reset(){ |
395 | 395 | $this->error = null; |
396 | 396 | $this->pdoStatment = null; |
@@ -23,19 +23,19 @@ 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 DatabaseQueryRunner{ |
|
26 | + class DatabaseQueryRunner { |
|
27 | 27 | |
28 | 28 | /** |
29 | 29 | * The logger instance |
30 | 30 | * @var object |
31 | 31 | */ |
32 | - private $logger = null; |
|
32 | + private $logger = null; |
|
33 | 33 | |
34 | 34 | /** |
35 | 35 | * The last query result |
36 | 36 | * @var object |
37 | 37 | */ |
38 | - private $queryResult = null; |
|
38 | + private $queryResult = null; |
|
39 | 39 | |
40 | 40 | /** |
41 | 41 | * The benchmark instance |
@@ -47,45 +47,45 @@ discard block |
||
47 | 47 | * The SQL query statment to execute |
48 | 48 | * @var string |
49 | 49 | */ |
50 | - private $query = null; |
|
50 | + private $query = null; |
|
51 | 51 | |
52 | 52 | /** |
53 | 53 | * Indicate if we need return result as list (boolean) |
54 | 54 | * or the data used to replace the placeholder (array) |
55 | 55 | * @var array|boolean |
56 | 56 | */ |
57 | - private $all = true; |
|
57 | + private $all = true; |
|
58 | 58 | |
59 | 59 | |
60 | 60 | /** |
61 | 61 | * Indicate if we need return result as array or not |
62 | 62 | * @var boolean |
63 | 63 | */ |
64 | - private $returnAsArray = true; |
|
64 | + private $returnAsArray = true; |
|
65 | 65 | |
66 | 66 | /** |
67 | 67 | * The last PDOStatment instance |
68 | 68 | * @var object |
69 | 69 | */ |
70 | - private $pdoStatment = null; |
|
70 | + private $pdoStatment = null; |
|
71 | 71 | |
72 | 72 | /** |
73 | 73 | * The error returned for the last query |
74 | 74 | * @var string |
75 | 75 | */ |
76 | - private $error = null; |
|
76 | + private $error = null; |
|
77 | 77 | |
78 | 78 | /** |
79 | 79 | * The PDO instance |
80 | 80 | * @var object |
81 | 81 | */ |
82 | - private $pdo = null; |
|
82 | + private $pdo = null; |
|
83 | 83 | |
84 | 84 | /** |
85 | 85 | * The database driver name used |
86 | 86 | * @var string |
87 | 87 | */ |
88 | - private $driver = null; |
|
88 | + private $driver = null; |
|
89 | 89 | |
90 | 90 | |
91 | 91 | |
@@ -96,8 +96,8 @@ discard block |
||
96 | 96 | * @param boolean $returnAsList if need return as list or just one row |
97 | 97 | * @param boolean $returnAsArray whether to return the result as array or not |
98 | 98 | */ |
99 | - public function __construct(PDO $pdo = null, $query = null, $returnAsList = true, $returnAsArray = false){ |
|
100 | - if (is_object($pdo)){ |
|
99 | + public function __construct(PDO $pdo = null, $query = null, $returnAsList = true, $returnAsArray = false) { |
|
100 | + if (is_object($pdo)) { |
|
101 | 101 | $this->pdo = $pdo; |
102 | 102 | } |
103 | 103 | $this->query = $query; |
@@ -112,20 +112,20 @@ discard block |
||
112 | 112 | * |
113 | 113 | * @return object|void |
114 | 114 | */ |
115 | - public function execute(){ |
|
115 | + public function execute() { |
|
116 | 116 | //reset instance |
117 | 117 | $this->reset(); |
118 | 118 | |
119 | 119 | //for database query execution time |
120 | 120 | $benchmarkMarkerKey = $this->getBenchmarkKey(); |
121 | - if (! is_object($this->benchmarkInstance)){ |
|
121 | + if (!is_object($this->benchmarkInstance)) { |
|
122 | 122 | $this->benchmarkInstance = & class_loader('Benchmark'); |
123 | 123 | } |
124 | 124 | |
125 | 125 | $this->logger->info( |
126 | 126 | 'Execute SQL query [' . $this->query . '], return type: ' |
127 | - . ($this->returnAsArray ? 'ARRAY' : 'OBJECT') .', return as list: ' |
|
128 | - . ($this->returnAsList ? 'YES':'NO') |
|
127 | + . ($this->returnAsArray ? 'ARRAY' : 'OBJECT') . ', return as list: ' |
|
128 | + . ($this->returnAsList ? 'YES' : 'NO') |
|
129 | 129 | ); |
130 | 130 | |
131 | 131 | $this->benchmarkInstance->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')'); |
@@ -138,19 +138,19 @@ discard block |
||
138 | 138 | 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')' |
139 | 139 | ); |
140 | 140 | //TODO use the configuration value for the high response time currently is 1 second |
141 | - if ($responseTime >= 1 ){ |
|
141 | + if ($responseTime >= 1) { |
|
142 | 142 | $this->logger->warning( |
143 | 143 | 'High response time while processing database query [' . $this->query . ']. |
144 | - The response time is [' .$responseTime. '] sec.' |
|
144 | + The response time is [' .$responseTime . '] sec.' |
|
145 | 145 | ); |
146 | 146 | } |
147 | 147 | |
148 | - if ($this->pdoStatment !== false){ |
|
148 | + if ($this->pdoStatment !== false) { |
|
149 | 149 | $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
150 | - if($isSqlSELECTQuery){ |
|
150 | + if ($isSqlSELECTQuery) { |
|
151 | 151 | $this->setResultForSelect(); |
152 | 152 | } |
153 | - else{ |
|
153 | + else { |
|
154 | 154 | $this->setResultForNonSelect(); |
155 | 155 | } |
156 | 156 | return $this->queryResult; |
@@ -162,28 +162,28 @@ discard block |
||
162 | 162 | * Return the result for SELECT query |
163 | 163 | * @see DatabaseQueryRunner::execute |
164 | 164 | */ |
165 | - protected function setResultForSelect(){ |
|
165 | + protected function setResultForSelect() { |
|
166 | 166 | //if need return all result like list of record |
167 | 167 | $result = null; |
168 | 168 | $numRows = 0; |
169 | 169 | $fetchMode = PDO::FETCH_OBJ; |
170 | - if($this->returnAsArray){ |
|
170 | + if ($this->returnAsArray) { |
|
171 | 171 | $fetchMode = PDO::FETCH_ASSOC; |
172 | 172 | } |
173 | - if ($this->returnAsList){ |
|
173 | + if ($this->returnAsList) { |
|
174 | 174 | $result = $this->pdoStatment->fetchAll($fetchMode); |
175 | 175 | } |
176 | - else{ |
|
176 | + else { |
|
177 | 177 | $result = $this->pdoStatment->fetch($fetchMode); |
178 | 178 | } |
179 | 179 | //Sqlite and pgsql always return 0 when using rowCount() |
180 | - if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
180 | + if (in_array($this->driver, array('sqlite', 'pgsql'))) { |
|
181 | 181 | $numRows = count($result); |
182 | 182 | } |
183 | - else{ |
|
183 | + else { |
|
184 | 184 | $numRows = $this->pdoStatment->rowCount(); |
185 | 185 | } |
186 | - if(! is_object($this->queryResult)){ |
|
186 | + if (!is_object($this->queryResult)) { |
|
187 | 187 | $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
188 | 188 | } |
189 | 189 | $this->queryResult->setResult($result); |
@@ -194,20 +194,20 @@ discard block |
||
194 | 194 | * Return the result for non SELECT query |
195 | 195 | * @see DatabaseQueryRunner::execute |
196 | 196 | */ |
197 | - protected function setResultForNonSelect(){ |
|
197 | + protected function setResultForNonSelect() { |
|
198 | 198 | //Sqlite and pgsql always return 0 when using rowCount() |
199 | 199 | $result = false; |
200 | 200 | $numRows = 0; |
201 | - if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
|
201 | + if (in_array($this->driver, array('sqlite', 'pgsql'))) { |
|
202 | 202 | $result = true; //to test the result for the query like UPDATE, INSERT, DELETE |
203 | 203 | $numRows = 1; //TODO use the correct method to get the exact affected row |
204 | 204 | } |
205 | - else{ |
|
205 | + else { |
|
206 | 206 | //to test the result for the query like UPDATE, INSERT, DELETE |
207 | 207 | $result = $this->pdoStatment->rowCount() >= 0; |
208 | 208 | $numRows = $this->pdoStatment->rowCount(); |
209 | 209 | } |
210 | - if(! is_object($this->queryResult)){ |
|
210 | + if (!is_object($this->queryResult)) { |
|
211 | 211 | $this->queryResult = & class_loader('DatabaseQueryResult', 'classes/database'); |
212 | 212 | } |
213 | 213 | $this->queryResult->setResult($result); |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | * Return the benchmark instance |
220 | 220 | * @return Benchmark |
221 | 221 | */ |
222 | - public function getBenchmark(){ |
|
222 | + public function getBenchmark() { |
|
223 | 223 | return $this->benchmarkInstance; |
224 | 224 | } |
225 | 225 | |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | * @param Benchmark $benchmark the benchmark object |
229 | 229 | * @return object DatabaseQueryRunner |
230 | 230 | */ |
231 | - public function setBenchmark($benchmark){ |
|
231 | + public function setBenchmark($benchmark) { |
|
232 | 232 | $this->benchmarkInstance = $benchmark; |
233 | 233 | return $this; |
234 | 234 | } |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | * |
239 | 239 | * @return object DatabaseQueryResult |
240 | 240 | */ |
241 | - public function getQueryResult(){ |
|
241 | + public function getQueryResult() { |
|
242 | 242 | return $this->queryResult; |
243 | 243 | } |
244 | 244 | |
@@ -248,7 +248,7 @@ discard block |
||
248 | 248 | * |
249 | 249 | * @return object DatabaseQueryRunner |
250 | 250 | */ |
251 | - public function setQueryResult(DatabaseQueryResult $queryResult){ |
|
251 | + public function setQueryResult(DatabaseQueryResult $queryResult) { |
|
252 | 252 | $this->queryResult = $queryResult; |
253 | 253 | return $this; |
254 | 254 | } |
@@ -257,7 +257,7 @@ discard block |
||
257 | 257 | * Return the Log instance |
258 | 258 | * @return Log |
259 | 259 | */ |
260 | - public function getLogger(){ |
|
260 | + public function getLogger() { |
|
261 | 261 | return $this->logger; |
262 | 262 | } |
263 | 263 | |
@@ -266,7 +266,7 @@ discard block |
||
266 | 266 | * @param Log $logger the log object |
267 | 267 | * @return object DatabaseQueryRunner |
268 | 268 | */ |
269 | - public function setLogger($logger){ |
|
269 | + public function setLogger($logger) { |
|
270 | 270 | $this->logger = $logger; |
271 | 271 | return $this; |
272 | 272 | } |
@@ -275,7 +275,7 @@ discard block |
||
275 | 275 | * Return the current query SQL string |
276 | 276 | * @return string |
277 | 277 | */ |
278 | - public function getQuery(){ |
|
278 | + public function getQuery() { |
|
279 | 279 | return $this->query; |
280 | 280 | } |
281 | 281 | |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | * @param string $query the SQL query to set |
285 | 285 | * @return object DatabaseQueryRunner |
286 | 286 | */ |
287 | - public function setQuery($query){ |
|
287 | + public function setQuery($query) { |
|
288 | 288 | $this->query = $query; |
289 | 289 | return $this; |
290 | 290 | } |
@@ -294,7 +294,7 @@ discard block |
||
294 | 294 | * @param boolean $returnType |
295 | 295 | * @return object DatabaseQueryRunner |
296 | 296 | */ |
297 | - public function setReturnType($returnType){ |
|
297 | + public function setReturnType($returnType) { |
|
298 | 298 | $this->returnAsList = $returnType; |
299 | 299 | return $this; |
300 | 300 | } |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | * @param boolean $status the status if true will return as array |
305 | 305 | * @return object DatabaseQueryRunner |
306 | 306 | */ |
307 | - public function setReturnAsArray($status = true){ |
|
307 | + public function setReturnAsArray($status = true) { |
|
308 | 308 | $this->returnAsArray = $status; |
309 | 309 | return $this; |
310 | 310 | } |
@@ -313,7 +313,7 @@ discard block |
||
313 | 313 | * Return the error for last query execution |
314 | 314 | * @return string |
315 | 315 | */ |
316 | - public function getQueryError(){ |
|
316 | + public function getQueryError() { |
|
317 | 317 | return $this->error; |
318 | 318 | } |
319 | 319 | |
@@ -321,7 +321,7 @@ discard block |
||
321 | 321 | * Return the PDO instance |
322 | 322 | * @return object |
323 | 323 | */ |
324 | - public function getPdo(){ |
|
324 | + public function getPdo() { |
|
325 | 325 | return $this->pdo; |
326 | 326 | } |
327 | 327 | |
@@ -330,7 +330,7 @@ discard block |
||
330 | 330 | * @param PDO $pdo the pdo object |
331 | 331 | * @return object DatabaseQueryRunner |
332 | 332 | */ |
333 | - public function setPdo(PDO $pdo = null){ |
|
333 | + public function setPdo(PDO $pdo = null) { |
|
334 | 334 | $this->pdo = $pdo; |
335 | 335 | return $this; |
336 | 336 | } |
@@ -339,7 +339,7 @@ discard block |
||
339 | 339 | * Return the database driver |
340 | 340 | * @return string |
341 | 341 | */ |
342 | - public function getDriver(){ |
|
342 | + public function getDriver() { |
|
343 | 343 | return $this->driver; |
344 | 344 | } |
345 | 345 | |
@@ -348,7 +348,7 @@ discard block |
||
348 | 348 | * @param string $driver the new driver |
349 | 349 | * @return object DatabaseQueryRunner |
350 | 350 | */ |
351 | - public function setDriver($driver){ |
|
351 | + public function setDriver($driver) { |
|
352 | 352 | $this->driver = $driver; |
353 | 353 | return $this; |
354 | 354 | } |
@@ -358,14 +358,14 @@ discard block |
||
358 | 358 | * |
359 | 359 | * @return string |
360 | 360 | */ |
361 | - protected function getBenchmarkKey(){ |
|
361 | + protected function getBenchmarkKey() { |
|
362 | 362 | return md5($this->query . $this->returnAsList . $this->returnAsArray); |
363 | 363 | } |
364 | 364 | |
365 | 365 | /** |
366 | 366 | * Set error for database query execution |
367 | 367 | */ |
368 | - protected function setQueryRunnerError(){ |
|
368 | + protected function setQueryRunnerError() { |
|
369 | 369 | $error = $this->pdo->errorInfo(); |
370 | 370 | $this->error = isset($error[2]) ? $error[2] : ''; |
371 | 371 | $this->logger->error('The database query execution got an error: ' . stringfy_vars($error)); |
@@ -377,12 +377,12 @@ discard block |
||
377 | 377 | * Set the Log instance using argument or create new instance |
378 | 378 | * @param object $logger the Log instance if not null |
379 | 379 | */ |
380 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
381 | - if ($logger !== null){ |
|
380 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null) { |
|
381 | + if ($logger !== null) { |
|
382 | 382 | $this->logger = $logger; |
383 | 383 | } |
384 | - else{ |
|
385 | - $this->logger =& class_loader('Log', 'classes'); |
|
384 | + else { |
|
385 | + $this->logger = & class_loader('Log', 'classes'); |
|
386 | 386 | $this->logger->setLogger('Library::DatabaseQueryRunner'); |
387 | 387 | } |
388 | 388 | } |
@@ -391,7 +391,7 @@ discard block |
||
391 | 391 | /** |
392 | 392 | * Reset the instance before run each query |
393 | 393 | */ |
394 | - private function reset(){ |
|
394 | + private function reset() { |
|
395 | 395 | $this->error = null; |
396 | 396 | $this->pdoStatment = null; |
397 | 397 | } |
@@ -149,8 +149,7 @@ discard block |
||
149 | 149 | $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
150 | 150 | if($isSqlSELECTQuery){ |
151 | 151 | $this->setResultForSelect(); |
152 | - } |
|
153 | - else{ |
|
152 | + } else{ |
|
154 | 153 | $this->setResultForNonSelect(); |
155 | 154 | } |
156 | 155 | return $this->queryResult; |
@@ -172,15 +171,13 @@ discard block |
||
172 | 171 | } |
173 | 172 | if ($this->returnAsList){ |
174 | 173 | $result = $this->pdoStatment->fetchAll($fetchMode); |
175 | - } |
|
176 | - else{ |
|
174 | + } else{ |
|
177 | 175 | $result = $this->pdoStatment->fetch($fetchMode); |
178 | 176 | } |
179 | 177 | //Sqlite and pgsql always return 0 when using rowCount() |
180 | 178 | if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
181 | 179 | $numRows = count($result); |
182 | - } |
|
183 | - else{ |
|
180 | + } else{ |
|
184 | 181 | $numRows = $this->pdoStatment->rowCount(); |
185 | 182 | } |
186 | 183 | if(! is_object($this->queryResult)){ |
@@ -201,8 +198,7 @@ discard block |
||
201 | 198 | if (in_array($this->driver, array('sqlite', 'pgsql'))){ |
202 | 199 | $result = true; //to test the result for the query like UPDATE, INSERT, DELETE |
203 | 200 | $numRows = 1; //TODO use the correct method to get the exact affected row |
204 | - } |
|
205 | - else{ |
|
201 | + } else{ |
|
206 | 202 | //to test the result for the query like UPDATE, INSERT, DELETE |
207 | 203 | $result = $this->pdoStatment->rowCount() >= 0; |
208 | 204 | $numRows = $this->pdoStatment->rowCount(); |
@@ -380,8 +376,7 @@ discard block |
||
380 | 376 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
381 | 377 | if ($logger !== null){ |
382 | 378 | $this->logger = $logger; |
383 | - } |
|
384 | - else{ |
|
379 | + } else{ |
|
385 | 380 | $this->logger =& class_loader('Log', 'classes'); |
386 | 381 | $this->logger->setLogger('Library::DatabaseQueryRunner'); |
387 | 382 | } |
@@ -1,119 +1,119 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | defined('ROOT_PATH') || exit('Access denied'); |
3 | - /** |
|
4 | - * TNH Framework |
|
5 | - * |
|
6 | - * A simple PHP framework using HMVC architecture |
|
7 | - * |
|
8 | - * This content is released under the GNU GPL License (GPL) |
|
9 | - * |
|
10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
11 | - * |
|
12 | - * This program is free software; you can redistribute it and/or |
|
13 | - * modify it under the terms of the GNU General Public License |
|
14 | - * as published by the Free Software Foundation; either version 3 |
|
15 | - * of the License, or (at your option) any later version. |
|
16 | - * |
|
17 | - * This program is distributed in the hope that it will be useful, |
|
18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | - * GNU General Public License for more details. |
|
21 | - * |
|
22 | - * You should have received a copy of the GNU General Public License |
|
23 | - * along with this program; if not, write to the Free Software |
|
24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | - */ |
|
26 | - class Database{ |
|
3 | + /** |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | + class Database{ |
|
27 | 27 | |
28 | - /** |
|
29 | - * The PDO instance |
|
30 | - * @var object |
|
31 | - */ |
|
28 | + /** |
|
29 | + * The PDO instance |
|
30 | + * @var object |
|
31 | + */ |
|
32 | 32 | private $pdo = null; |
33 | 33 | |
34 | - /** |
|
35 | - * The database name used for the application |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - private $databaseName = null; |
|
34 | + /** |
|
35 | + * The database name used for the application |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + private $databaseName = null; |
|
39 | 39 | |
40 | - /** |
|
41 | - * The number of rows returned by the last query |
|
42 | - * @var int |
|
43 | - */ |
|
40 | + /** |
|
41 | + * The number of rows returned by the last query |
|
42 | + * @var int |
|
43 | + */ |
|
44 | 44 | private $numRows = 0; |
45 | 45 | |
46 | - /** |
|
47 | - * The last insert id for the primary key column that have auto increment or sequence |
|
48 | - * @var mixed |
|
49 | - */ |
|
46 | + /** |
|
47 | + * The last insert id for the primary key column that have auto increment or sequence |
|
48 | + * @var mixed |
|
49 | + */ |
|
50 | 50 | private $insertId = null; |
51 | 51 | |
52 | - /** |
|
53 | - * The full SQL query statment after build for each command |
|
54 | - * @var string |
|
55 | - */ |
|
52 | + /** |
|
53 | + * The full SQL query statment after build for each command |
|
54 | + * @var string |
|
55 | + */ |
|
56 | 56 | private $query = null; |
57 | 57 | |
58 | - /** |
|
59 | - * The result returned for the last query |
|
60 | - * @var mixed |
|
61 | - */ |
|
58 | + /** |
|
59 | + * The result returned for the last query |
|
60 | + * @var mixed |
|
61 | + */ |
|
62 | 62 | private $result = array(); |
63 | 63 | |
64 | - /** |
|
65 | - * The cache default time to live in second. 0 means no need to use the cache feature |
|
66 | - * @var int |
|
67 | - */ |
|
68 | - private $cacheTtl = 0; |
|
64 | + /** |
|
65 | + * The cache default time to live in second. 0 means no need to use the cache feature |
|
66 | + * @var int |
|
67 | + */ |
|
68 | + private $cacheTtl = 0; |
|
69 | 69 | |
70 | - /** |
|
71 | - * The cache current time to live. 0 means no need to use the cache feature |
|
72 | - * @var int |
|
73 | - */ |
|
70 | + /** |
|
71 | + * The cache current time to live. 0 means no need to use the cache feature |
|
72 | + * @var int |
|
73 | + */ |
|
74 | 74 | private $temporaryCacheTtl = 0; |
75 | 75 | |
76 | - /** |
|
77 | - * The number of executed query for the current request |
|
78 | - * @var int |
|
79 | - */ |
|
76 | + /** |
|
77 | + * The number of executed query for the current request |
|
78 | + * @var int |
|
79 | + */ |
|
80 | 80 | private $queryCount = 0; |
81 | 81 | |
82 | - /** |
|
83 | - * The default data to be used in the statments query INSERT, UPDATE |
|
84 | - * @var array |
|
85 | - */ |
|
82 | + /** |
|
83 | + * The default data to be used in the statments query INSERT, UPDATE |
|
84 | + * @var array |
|
85 | + */ |
|
86 | 86 | private $data = array(); |
87 | 87 | |
88 | - /** |
|
89 | - * The database configuration |
|
90 | - * @var array |
|
91 | - */ |
|
88 | + /** |
|
89 | + * The database configuration |
|
90 | + * @var array |
|
91 | + */ |
|
92 | 92 | private $config = array(); |
93 | 93 | |
94 | - /** |
|
95 | - * The logger instance |
|
96 | - * @var object |
|
97 | - */ |
|
94 | + /** |
|
95 | + * The logger instance |
|
96 | + * @var object |
|
97 | + */ |
|
98 | 98 | private $logger = null; |
99 | 99 | |
100 | 100 | /** |
101 | - * The cache instance |
|
102 | - * @var object |
|
103 | - */ |
|
101 | + * The cache instance |
|
102 | + * @var object |
|
103 | + */ |
|
104 | 104 | private $cacheInstance = null; |
105 | 105 | |
106 | 106 | |
107 | - /** |
|
108 | - * The DatabaseQueryBuilder instance |
|
109 | - * @var object |
|
110 | - */ |
|
107 | + /** |
|
108 | + * The DatabaseQueryBuilder instance |
|
109 | + * @var object |
|
110 | + */ |
|
111 | 111 | private $queryBuilder = null; |
112 | 112 | |
113 | 113 | /** |
114 | - * The DatabaseQueryRunner instance |
|
115 | - * @var object |
|
116 | - */ |
|
114 | + * The DatabaseQueryRunner instance |
|
115 | + * @var object |
|
116 | + */ |
|
117 | 117 | private $queryRunner = null; |
118 | 118 | |
119 | 119 | |
@@ -125,8 +125,8 @@ discard block |
||
125 | 125 | //Set Log instance to use |
126 | 126 | $this->setLoggerFromParamOrCreateNewInstance(null); |
127 | 127 | |
128 | - //Set DatabaseQueryBuilder instance to use |
|
129 | - $this->setQueryBuilderFromParamOrCreateNewInstance(null); |
|
128 | + //Set DatabaseQueryBuilder instance to use |
|
129 | + $this->setQueryBuilderFromParamOrCreateNewInstance(null); |
|
130 | 130 | |
131 | 131 | //Set DatabaseQueryRunner instance to use |
132 | 132 | $this->setQueryRunnerFromParamOrCreateNewInstance(null); |
@@ -143,22 +143,22 @@ discard block |
||
143 | 143 | * @return bool |
144 | 144 | */ |
145 | 145 | public function connect(){ |
146 | - $config = $this->getDatabaseConfiguration(); |
|
147 | - if (! empty($config)){ |
|
146 | + $config = $this->getDatabaseConfiguration(); |
|
147 | + if (! empty($config)){ |
|
148 | 148 | try{ |
149 | 149 | $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
150 | 150 | $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
151 | 151 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
152 | 152 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
153 | 153 | return true; |
154 | - } |
|
155 | - catch (PDOException $e){ |
|
154 | + } |
|
155 | + catch (PDOException $e){ |
|
156 | 156 | $this->logger->fatal($e->getMessage()); |
157 | 157 | show_error('Cannot connect to Database.'); |
158 | 158 | return false; |
159 | - } |
|
160 | - } |
|
161 | - return false; |
|
159 | + } |
|
160 | + } |
|
161 | + return false; |
|
162 | 162 | } |
163 | 163 | |
164 | 164 | |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | * @return int |
168 | 168 | */ |
169 | 169 | public function numRows(){ |
170 | - return $this->numRows; |
|
170 | + return $this->numRows; |
|
171 | 171 | } |
172 | 172 | |
173 | 173 | /** |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | * @return mixed |
176 | 176 | */ |
177 | 177 | public function insertId(){ |
178 | - return $this->insertId; |
|
178 | + return $this->insertId; |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | |
@@ -186,13 +186,13 @@ discard block |
||
186 | 186 | * @return mixed the query SQL string or the record result |
187 | 187 | */ |
188 | 188 | public function get($returnSQLQueryOrResultType = false){ |
189 | - $this->getQueryBuilder()->limit(1); |
|
190 | - $query = $this->getAll(true); |
|
191 | - if ($returnSQLQueryOrResultType === true){ |
|
189 | + $this->getQueryBuilder()->limit(1); |
|
190 | + $query = $this->getAll(true); |
|
191 | + if ($returnSQLQueryOrResultType === true){ |
|
192 | 192 | return $query; |
193 | - } else { |
|
193 | + } else { |
|
194 | 194 | return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
195 | - } |
|
195 | + } |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | /** |
@@ -202,11 +202,11 @@ discard block |
||
202 | 202 | * @return mixed the query SQL string or the record result |
203 | 203 | */ |
204 | 204 | public function getAll($returnSQLQueryOrResultType = false){ |
205 | - $query = $this->getQueryBuilder()->getQuery(); |
|
206 | - if ($returnSQLQueryOrResultType === true){ |
|
207 | - return $query; |
|
208 | - } |
|
209 | - return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
205 | + $query = $this->getQueryBuilder()->getQuery(); |
|
206 | + if ($returnSQLQueryOrResultType === true){ |
|
207 | + return $query; |
|
208 | + } |
|
209 | + return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
|
210 | 210 | } |
211 | 211 | |
212 | 212 | /** |
@@ -216,19 +216,19 @@ discard block |
||
216 | 216 | * @return mixed the insert id of the new record or null |
217 | 217 | */ |
218 | 218 | public function insert($data = array(), $escape = true){ |
219 | - if (empty($data) && $this->getData()){ |
|
219 | + if (empty($data) && $this->getData()){ |
|
220 | 220 | //as when using $this->setData() may be the data already escaped |
221 | 221 | $escape = false; |
222 | 222 | $data = $this->getData(); |
223 | - } |
|
224 | - $query = $this->getQueryBuilder()->insert($data, $escape)->getQuery(); |
|
225 | - $result = $this->query($query); |
|
226 | - if ($result){ |
|
223 | + } |
|
224 | + $query = $this->getQueryBuilder()->insert($data, $escape)->getQuery(); |
|
225 | + $result = $this->query($query); |
|
226 | + if ($result){ |
|
227 | 227 | $this->insertId = $this->pdo->lastInsertId(); |
228 | - //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
228 | + //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
229 | 229 | return ! $this->insertId() ? true : $this->insertId(); |
230 | - } |
|
231 | - return false; |
|
230 | + } |
|
231 | + return false; |
|
232 | 232 | } |
233 | 233 | |
234 | 234 | /** |
@@ -238,13 +238,13 @@ discard block |
||
238 | 238 | * @return mixed the update status |
239 | 239 | */ |
240 | 240 | public function update($data = array(), $escape = true){ |
241 | - if (empty($data) && $this->getData()){ |
|
241 | + if (empty($data) && $this->getData()){ |
|
242 | 242 | //as when using $this->setData() may be the data already escaped |
243 | 243 | $escape = false; |
244 | 244 | $data = $this->getData(); |
245 | - } |
|
246 | - $query = $this->getQueryBuilder()->update($data, $escape)->getQuery(); |
|
247 | - return $this->query($query); |
|
245 | + } |
|
246 | + $query = $this->getQueryBuilder()->update($data, $escape)->getQuery(); |
|
247 | + return $this->query($query); |
|
248 | 248 | } |
249 | 249 | |
250 | 250 | /** |
@@ -252,8 +252,8 @@ discard block |
||
252 | 252 | * @return mixed the delete status |
253 | 253 | */ |
254 | 254 | public function delete(){ |
255 | - $query = $this->getQueryBuilder()->delete()->getQuery(); |
|
256 | - return $this->query($query); |
|
255 | + $query = $this->getQueryBuilder()->delete()->getQuery(); |
|
256 | + return $this->query($query); |
|
257 | 257 | } |
258 | 258 | |
259 | 259 | /** |
@@ -262,21 +262,21 @@ discard block |
||
262 | 262 | * @return object the current Database instance |
263 | 263 | */ |
264 | 264 | public function setCache($ttl = 0){ |
265 | - if ($ttl > 0){ |
|
265 | + if ($ttl > 0){ |
|
266 | 266 | $this->cacheTtl = $ttl; |
267 | 267 | $this->temporaryCacheTtl = $ttl; |
268 | - } |
|
269 | - return $this; |
|
268 | + } |
|
269 | + return $this; |
|
270 | 270 | } |
271 | 271 | |
272 | - /** |
|
273 | - * Enabled cache temporary for the current query not globally |
|
274 | - * @param integer $ttl the cache time to live in second |
|
275 | - * @return object the current Database instance |
|
276 | - */ |
|
277 | - public function cached($ttl = 0){ |
|
272 | + /** |
|
273 | + * Enabled cache temporary for the current query not globally |
|
274 | + * @param integer $ttl the cache time to live in second |
|
275 | + * @return object the current Database instance |
|
276 | + */ |
|
277 | + public function cached($ttl = 0){ |
|
278 | 278 | if ($ttl > 0){ |
279 | - $this->temporaryCacheTtl = $ttl; |
|
279 | + $this->temporaryCacheTtl = $ttl; |
|
280 | 280 | } |
281 | 281 | return $this; |
282 | 282 | } |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | * @return mixed the data after escaped or the same data if not |
289 | 289 | */ |
290 | 290 | public function escape($data, $escaped = true){ |
291 | - return $escaped ? |
|
291 | + return $escaped ? |
|
292 | 292 | $this->pdo->quote(trim($data)) |
293 | 293 | : $data; |
294 | 294 | } |
@@ -298,7 +298,7 @@ discard block |
||
298 | 298 | * @return int |
299 | 299 | */ |
300 | 300 | public function queryCount(){ |
301 | - return $this->queryCount; |
|
301 | + return $this->queryCount; |
|
302 | 302 | } |
303 | 303 | |
304 | 304 | /** |
@@ -306,7 +306,7 @@ discard block |
||
306 | 306 | * @return string |
307 | 307 | */ |
308 | 308 | public function getQuery(){ |
309 | - return $this->query; |
|
309 | + return $this->query; |
|
310 | 310 | } |
311 | 311 | |
312 | 312 | /** |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | * @return string |
315 | 315 | */ |
316 | 316 | public function getDatabaseName(){ |
317 | - return $this->databaseName; |
|
317 | + return $this->databaseName; |
|
318 | 318 | } |
319 | 319 | |
320 | 320 | /** |
@@ -322,17 +322,17 @@ discard block |
||
322 | 322 | * @return object |
323 | 323 | */ |
324 | 324 | public function getPdo(){ |
325 | - return $this->pdo; |
|
325 | + return $this->pdo; |
|
326 | 326 | } |
327 | 327 | |
328 | 328 | /** |
329 | 329 | * Set the PDO instance |
330 | 330 | * @param object $pdo the pdo object |
331 | - * @return object Database |
|
331 | + * @return object Database |
|
332 | 332 | */ |
333 | 333 | public function setPdo(PDO $pdo){ |
334 | - $this->pdo = $pdo; |
|
335 | - return $this; |
|
334 | + $this->pdo = $pdo; |
|
335 | + return $this; |
|
336 | 336 | } |
337 | 337 | |
338 | 338 | |
@@ -341,44 +341,44 @@ discard block |
||
341 | 341 | * @return Log |
342 | 342 | */ |
343 | 343 | public function getLogger(){ |
344 | - return $this->logger; |
|
344 | + return $this->logger; |
|
345 | 345 | } |
346 | 346 | |
347 | 347 | /** |
348 | 348 | * Set the log instance |
349 | 349 | * @param Log $logger the log object |
350 | - * @return object Database |
|
350 | + * @return object Database |
|
351 | 351 | */ |
352 | 352 | public function setLogger($logger){ |
353 | - $this->logger = $logger; |
|
354 | - return $this; |
|
353 | + $this->logger = $logger; |
|
354 | + return $this; |
|
355 | 355 | } |
356 | 356 | |
357 | - /** |
|
358 | - * Return the cache instance |
|
359 | - * @return CacheInterface |
|
360 | - */ |
|
357 | + /** |
|
358 | + * Return the cache instance |
|
359 | + * @return CacheInterface |
|
360 | + */ |
|
361 | 361 | public function getCacheInstance(){ |
362 | - return $this->cacheInstance; |
|
362 | + return $this->cacheInstance; |
|
363 | 363 | } |
364 | 364 | |
365 | 365 | /** |
366 | 366 | * Set the cache instance |
367 | 367 | * @param CacheInterface $cache the cache object |
368 | - * @return object Database |
|
368 | + * @return object Database |
|
369 | 369 | */ |
370 | 370 | public function setCacheInstance($cache){ |
371 | - $this->cacheInstance = $cache; |
|
372 | - return $this; |
|
371 | + $this->cacheInstance = $cache; |
|
372 | + return $this; |
|
373 | 373 | } |
374 | 374 | |
375 | 375 | |
376 | - /** |
|
377 | - * Return the DatabaseQueryBuilder instance |
|
378 | - * @return object DatabaseQueryBuilder |
|
379 | - */ |
|
376 | + /** |
|
377 | + * Return the DatabaseQueryBuilder instance |
|
378 | + * @return object DatabaseQueryBuilder |
|
379 | + */ |
|
380 | 380 | public function getQueryBuilder(){ |
381 | - return $this->queryBuilder; |
|
381 | + return $this->queryBuilder; |
|
382 | 382 | } |
383 | 383 | |
384 | 384 | /** |
@@ -386,8 +386,8 @@ discard block |
||
386 | 386 | * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
387 | 387 | */ |
388 | 388 | public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder){ |
389 | - $this->queryBuilder = $queryBuilder; |
|
390 | - return $this; |
|
389 | + $this->queryBuilder = $queryBuilder; |
|
390 | + return $this; |
|
391 | 391 | } |
392 | 392 | |
393 | 393 | /** |
@@ -395,7 +395,7 @@ discard block |
||
395 | 395 | * @return object DatabaseQueryRunner |
396 | 396 | */ |
397 | 397 | public function getQueryRunner(){ |
398 | - return $this->queryRunner; |
|
398 | + return $this->queryRunner; |
|
399 | 399 | } |
400 | 400 | |
401 | 401 | /** |
@@ -403,8 +403,8 @@ discard block |
||
403 | 403 | * @param object DatabaseQueryRunner $queryRunner the DatabaseQueryRunner object |
404 | 404 | */ |
405 | 405 | public function setQueryRunner(DatabaseQueryRunner $queryRunner){ |
406 | - $this->queryRunner = $queryRunner; |
|
407 | - return $this; |
|
406 | + $this->queryRunner = $queryRunner; |
|
407 | + return $this; |
|
408 | 408 | } |
409 | 409 | |
410 | 410 | /** |
@@ -412,7 +412,7 @@ discard block |
||
412 | 412 | * @return array |
413 | 413 | */ |
414 | 414 | public function getData(){ |
415 | - return $this->data; |
|
415 | + return $this->data; |
|
416 | 416 | } |
417 | 417 | |
418 | 418 | /** |
@@ -423,51 +423,51 @@ discard block |
||
423 | 423 | * @return object the current Database instance |
424 | 424 | */ |
425 | 425 | public function setData($key, $value = null, $escape = true){ |
426 | - if(is_array($key)){ |
|
427 | - foreach($key as $k => $v){ |
|
428 | - $this->setData($k, $v, $escape); |
|
429 | - } |
|
430 | - } else { |
|
426 | + if(is_array($key)){ |
|
427 | + foreach($key as $k => $v){ |
|
428 | + $this->setData($k, $v, $escape); |
|
429 | + } |
|
430 | + } else { |
|
431 | 431 | $this->data[$key] = $this->escape($value, $escape); |
432 | - } |
|
433 | - return $this; |
|
432 | + } |
|
433 | + return $this; |
|
434 | 434 | } |
435 | 435 | |
436 | - /** |
|
437 | - * Execute an SQL query |
|
438 | - * @param string $query the query SQL string |
|
439 | - * @param boolean $returnAsList indicate whether to return all record or just one row |
|
440 | - * @param boolean $returnAsArray return the result as array or not |
|
441 | - * @return mixed the query result |
|
442 | - */ |
|
436 | + /** |
|
437 | + * Execute an SQL query |
|
438 | + * @param string $query the query SQL string |
|
439 | + * @param boolean $returnAsList indicate whether to return all record or just one row |
|
440 | + * @param boolean $returnAsArray return the result as array or not |
|
441 | + * @return mixed the query result |
|
442 | + */ |
|
443 | 443 | public function query($query, $returnAsList = true, $returnAsArray = false){ |
444 | - $this->reset(); |
|
445 | - $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
446 | - //If is the SELECT query |
|
447 | - $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
444 | + $this->reset(); |
|
445 | + $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
|
446 | + //If is the SELECT query |
|
447 | + $isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
448 | 448 | |
449 | - //cache expire time |
|
450 | - $cacheExpire = $this->temporaryCacheTtl; |
|
449 | + //cache expire time |
|
450 | + $cacheExpire = $this->temporaryCacheTtl; |
|
451 | 451 | |
452 | - //return to the initial cache time |
|
453 | - $this->temporaryCacheTtl = $this->cacheTtl; |
|
452 | + //return to the initial cache time |
|
453 | + $this->temporaryCacheTtl = $this->cacheTtl; |
|
454 | 454 | |
455 | - //config for cache |
|
456 | - $cacheEnable = get_config('cache_enable'); |
|
455 | + //config for cache |
|
456 | + $cacheEnable = get_config('cache_enable'); |
|
457 | 457 | |
458 | - //the database cache content |
|
459 | - $cacheContent = null; |
|
458 | + //the database cache content |
|
459 | + $cacheContent = null; |
|
460 | 460 | |
461 | - //if can use cache feature for this query |
|
462 | - $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
461 | + //if can use cache feature for this query |
|
462 | + $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
|
463 | 463 | |
464 | - if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
465 | - $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
466 | - $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
|
467 | - } |
|
464 | + if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
465 | + $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
466 | + $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
|
467 | + } |
|
468 | 468 | |
469 | - if ( !$cacheContent){ |
|
470 | - //count the number of query execution to server |
|
469 | + if ( !$cacheContent){ |
|
470 | + //count the number of query execution to server |
|
471 | 471 | $this->queryCount++; |
472 | 472 | |
473 | 473 | $this->queryRunner->setQuery($query); |
@@ -484,35 +484,35 @@ discard block |
||
484 | 484 | $this->getCacheKeyForQuery($this->query, $returnAsList, $returnAsArray), |
485 | 485 | $this->result, |
486 | 486 | $cacheExpire |
487 | - ); |
|
487 | + ); |
|
488 | 488 | if (! $this->result){ |
489 | - $this->logger->info('No result where found for the query [' . $query . ']'); |
|
489 | + $this->logger->info('No result where found for the query [' . $query . ']'); |
|
490 | 490 | } |
491 | - } |
|
491 | + } |
|
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); |
|
492 | 497 | } |
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; |
|
498 | + return $this->result; |
|
499 | 499 | } |
500 | 500 | |
501 | 501 | |
502 | - /** |
|
503 | - * Return the database configuration |
|
504 | - * @return array |
|
505 | - */ |
|
506 | - public function getDatabaseConfiguration(){ |
|
507 | - return $this->config; |
|
508 | - } |
|
509 | - |
|
510 | - /** |
|
511 | - * Setting the database configuration using the configuration file and additional configuration from param |
|
512 | - * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
513 | - * @param boolean $useConfigFile whether to use database configuration file |
|
514 | - * @return object Database |
|
515 | - */ |
|
502 | + /** |
|
503 | + * Return the database configuration |
|
504 | + * @return array |
|
505 | + */ |
|
506 | + public function getDatabaseConfiguration(){ |
|
507 | + return $this->config; |
|
508 | + } |
|
509 | + |
|
510 | + /** |
|
511 | + * Setting the database configuration using the configuration file and additional configuration from param |
|
512 | + * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
513 | + * @param boolean $useConfigFile whether to use database configuration file |
|
514 | + * @return object Database |
|
515 | + */ |
|
516 | 516 | public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
517 | 517 | $db = array(); |
518 | 518 | if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
@@ -525,50 +525,50 @@ discard block |
||
525 | 525 | |
526 | 526 | //default configuration |
527 | 527 | $config = array( |
528 | - 'driver' => 'mysql', |
|
529 | - 'username' => 'root', |
|
530 | - 'password' => '', |
|
531 | - 'database' => '', |
|
532 | - 'hostname' => 'localhost', |
|
533 | - 'charset' => 'utf8', |
|
534 | - 'collation' => 'utf8_general_ci', |
|
535 | - 'prefix' => '', |
|
536 | - 'port' => '' |
|
528 | + 'driver' => 'mysql', |
|
529 | + 'username' => 'root', |
|
530 | + 'password' => '', |
|
531 | + 'database' => '', |
|
532 | + 'hostname' => 'localhost', |
|
533 | + 'charset' => 'utf8', |
|
534 | + 'collation' => 'utf8_general_ci', |
|
535 | + 'prefix' => '', |
|
536 | + 'port' => '' |
|
537 | 537 | ); |
538 | 538 | |
539 | - $config = array_merge($config, $db); |
|
540 | - //determine the port using the hostname like localhost:3307 |
|
541 | - //hostname will be "localhost", and port "3307" |
|
542 | - $p = explode(':', $config['hostname']); |
|
543 | - if (count($p) >= 2){ |
|
544 | - $config['hostname'] = $p[0]; |
|
545 | - $config['port'] = $p[1]; |
|
546 | - } |
|
539 | + $config = array_merge($config, $db); |
|
540 | + //determine the port using the hostname like localhost:3307 |
|
541 | + //hostname will be "localhost", and port "3307" |
|
542 | + $p = explode(':', $config['hostname']); |
|
543 | + if (count($p) >= 2){ |
|
544 | + $config['hostname'] = $p[0]; |
|
545 | + $config['port'] = $p[1]; |
|
546 | + } |
|
547 | 547 | |
548 | - $this->databaseName = $config['database']; |
|
549 | - $this->config = $config; |
|
550 | - $this->logger->info( |
|
551 | - 'The database configuration are listed below: ' |
|
552 | - . stringfy_vars(array_merge( |
|
553 | - $this->config, |
|
554 | - array('password' => string_hidden($this->config['password'])) |
|
555 | - )) |
|
556 | - ); |
|
548 | + $this->databaseName = $config['database']; |
|
549 | + $this->config = $config; |
|
550 | + $this->logger->info( |
|
551 | + 'The database configuration are listed below: ' |
|
552 | + . stringfy_vars(array_merge( |
|
553 | + $this->config, |
|
554 | + array('password' => string_hidden($this->config['password'])) |
|
555 | + )) |
|
556 | + ); |
|
557 | 557 | |
558 | - //Now connect to the database |
|
559 | - $this->connect(); |
|
558 | + //Now connect to the database |
|
559 | + $this->connect(); |
|
560 | 560 | |
561 | - //do update of QueryRunner and Builder |
|
562 | - $this->updateQueryBuilderAndRunnerProperties(); |
|
561 | + //do update of QueryRunner and Builder |
|
562 | + $this->updateQueryBuilderAndRunnerProperties(); |
|
563 | 563 | |
564 | - return $this; |
|
564 | + return $this; |
|
565 | 565 | } |
566 | 566 | |
567 | 567 | /** |
568 | 568 | * Close the connexion |
569 | 569 | */ |
570 | 570 | public function close(){ |
571 | - $this->pdo = null; |
|
571 | + $this->pdo = null; |
|
572 | 572 | } |
573 | 573 | |
574 | 574 | /** |
@@ -576,18 +576,18 @@ discard block |
||
576 | 576 | * @return void |
577 | 577 | */ |
578 | 578 | protected function updateQueryBuilderAndRunnerProperties(){ |
579 | - //update queryBuilder with some properties needed |
|
580 | - if(is_object($this->queryBuilder)){ |
|
579 | + //update queryBuilder with some properties needed |
|
580 | + if(is_object($this->queryBuilder)){ |
|
581 | 581 | $this->queryBuilder->setDriver($this->config['driver']); |
582 | 582 | $this->queryBuilder->setPrefix($this->config['prefix']); |
583 | 583 | $this->queryBuilder->setPdo($this->pdo); |
584 | - } |
|
584 | + } |
|
585 | 585 | |
586 | - //update queryRunner with some properties needed |
|
587 | - if(is_object($this->queryRunner)){ |
|
586 | + //update queryRunner with some properties needed |
|
587 | + if(is_object($this->queryRunner)){ |
|
588 | 588 | $this->queryRunner->setDriver($this->config['driver']); |
589 | 589 | $this->queryRunner->setPdo($this->pdo); |
590 | - } |
|
590 | + } |
|
591 | 591 | } |
592 | 592 | |
593 | 593 | |
@@ -596,24 +596,24 @@ discard block |
||
596 | 596 | * @return string the DSN string |
597 | 597 | */ |
598 | 598 | protected function getDsnFromDriver(){ |
599 | - $config = $this->getDatabaseConfiguration(); |
|
600 | - if (! empty($config)){ |
|
599 | + $config = $this->getDatabaseConfiguration(); |
|
600 | + if (! empty($config)){ |
|
601 | 601 | $driver = $config['driver']; |
602 | 602 | $driverDsnMap = array( |
603 | - 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
604 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
605 | - . 'dbname=' . $config['database'], |
|
606 | - 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
607 | - . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
608 | - . 'dbname=' . $config['database'], |
|
609 | - 'sqlite' => 'sqlite:' . $config['database'], |
|
610 | - 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
603 | + 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
|
604 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
605 | + . 'dbname=' . $config['database'], |
|
606 | + 'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' |
|
607 | + . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') |
|
608 | + . 'dbname=' . $config['database'], |
|
609 | + 'sqlite' => 'sqlite:' . $config['database'], |
|
610 | + 'oracle' => 'oci:dbname=' . $config['hostname'] |
|
611 | 611 | . (($config['port']) != '' ? ':' . $config['port'] : '') |
612 | 612 | . '/' . $config['database'] |
613 | - ); |
|
613 | + ); |
|
614 | 614 | return isset($driverDsnMap[$driver]) ? $driverDsnMap[$driver] : ''; |
615 | - } |
|
616 | - return null; |
|
615 | + } |
|
616 | + return null; |
|
617 | 617 | } |
618 | 618 | |
619 | 619 | /** |
@@ -625,11 +625,11 @@ discard block |
||
625 | 625 | protected function getCacheContentForQuery($query, $returnAsList, $returnAsArray){ |
626 | 626 | $cacheKey = $this->getCacheKeyForQuery($query, $returnAsList, $returnAsArray); |
627 | 627 | if (! is_object($this->cacheInstance)){ |
628 | - //can not call method with reference in argument |
|
629 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
630 | - //use temporary variable |
|
631 | - $instance = & get_instance()->cache; |
|
632 | - $this->cacheInstance = $instance; |
|
628 | + //can not call method with reference in argument |
|
629 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
630 | + //use temporary variable |
|
631 | + $instance = & get_instance()->cache; |
|
632 | + $this->cacheInstance = $instance; |
|
633 | 633 | } |
634 | 634 | return $this->cacheInstance->get($cacheKey); |
635 | 635 | } |
@@ -641,87 +641,87 @@ discard block |
||
641 | 641 | * @param mixed $result the query result to save |
642 | 642 | * @param int $expire the cache TTL |
643 | 643 | */ |
644 | - protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
644 | + protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
645 | 645 | $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
646 | 646 | if (! is_object($this->cacheInstance)){ |
647 | - //can not call method with reference in argument |
|
648 | - //like $this->setCacheInstance(& get_instance()->cache); |
|
649 | - //use temporary variable |
|
650 | - $instance = & get_instance()->cache; |
|
651 | - $this->cacheInstance = $instance; |
|
652 | - } |
|
647 | + //can not call method with reference in argument |
|
648 | + //like $this->setCacheInstance(& get_instance()->cache); |
|
649 | + //use temporary variable |
|
650 | + $instance = & get_instance()->cache; |
|
651 | + $this->cacheInstance = $instance; |
|
652 | + } |
|
653 | 653 | $this->cacheInstance->set($key, $result, $expire); |
654 | - } |
|
654 | + } |
|
655 | 655 | |
656 | 656 | |
657 | - /** |
|
658 | - * Return the cache key for the given query |
|
659 | - * @see Database::query |
|
660 | - * |
|
661 | - * @return string |
|
662 | - */ |
|
657 | + /** |
|
658 | + * Return the cache key for the given query |
|
659 | + * @see Database::query |
|
660 | + * |
|
661 | + * @return string |
|
662 | + */ |
|
663 | 663 | protected function getCacheKeyForQuery($query, $returnAsList, $returnAsArray){ |
664 | - return md5($query . $returnAsList . $returnAsArray); |
|
664 | + return md5($query . $returnAsList . $returnAsArray); |
|
665 | 665 | } |
666 | 666 | |
667 | - /** |
|
668 | - * Set the Log instance using argument or create new instance |
|
669 | - * @param object $logger the Log instance if not null |
|
670 | - */ |
|
667 | + /** |
|
668 | + * Set the Log instance using argument or create new instance |
|
669 | + * @param object $logger the Log instance if not null |
|
670 | + */ |
|
671 | 671 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
672 | - if ($logger !== null){ |
|
672 | + if ($logger !== null){ |
|
673 | 673 | $this->logger = $logger; |
674 | - } |
|
675 | - else{ |
|
676 | - $this->logger =& class_loader('Log', 'classes'); |
|
677 | - $this->logger->setLogger('Library::Database'); |
|
678 | - } |
|
674 | + } |
|
675 | + else{ |
|
676 | + $this->logger =& class_loader('Log', 'classes'); |
|
677 | + $this->logger->setLogger('Library::Database'); |
|
678 | + } |
|
679 | 679 | } |
680 | 680 | |
681 | - /** |
|
682 | - * Set the DatabaseQueryBuilder instance using argument or create new instance |
|
683 | - * @param object $queryBuilder the DatabaseQueryBuilder instance if not null |
|
684 | - */ |
|
685 | - protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null){ |
|
686 | - if ($queryBuilder !== null){ |
|
681 | + /** |
|
682 | + * Set the DatabaseQueryBuilder instance using argument or create new instance |
|
683 | + * @param object $queryBuilder the DatabaseQueryBuilder instance if not null |
|
684 | + */ |
|
685 | + protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null){ |
|
686 | + if ($queryBuilder !== null){ |
|
687 | 687 | $this->queryBuilder = $queryBuilder; |
688 | - } |
|
689 | - else{ |
|
690 | - $this->queryBuilder =& class_loader('DatabaseQueryBuilder', 'classes/database'); |
|
691 | - } |
|
692 | - } |
|
693 | - |
|
694 | - /** |
|
695 | - * Set the DatabaseQueryRunner instance using argument or create new instance |
|
696 | - * @param object $queryRunner the DatabaseQueryRunner instance if not null |
|
697 | - */ |
|
698 | - protected function setQueryRunnerFromParamOrCreateNewInstance(DatabaseQueryRunner $queryRunner = null){ |
|
688 | + } |
|
689 | + else{ |
|
690 | + $this->queryBuilder =& class_loader('DatabaseQueryBuilder', 'classes/database'); |
|
691 | + } |
|
692 | + } |
|
693 | + |
|
694 | + /** |
|
695 | + * Set the DatabaseQueryRunner instance using argument or create new instance |
|
696 | + * @param object $queryRunner the DatabaseQueryRunner instance if not null |
|
697 | + */ |
|
698 | + protected function setQueryRunnerFromParamOrCreateNewInstance(DatabaseQueryRunner $queryRunner = null){ |
|
699 | 699 | if ($queryRunner !== null){ |
700 | 700 | $this->queryRunner = $queryRunner; |
701 | 701 | } |
702 | 702 | else{ |
703 | - $this->queryRunner =& class_loader('DatabaseQueryRunner', 'classes/database'); |
|
703 | + $this->queryRunner =& class_loader('DatabaseQueryRunner', 'classes/database'); |
|
704 | + } |
|
704 | 705 | } |
705 | - } |
|
706 | 706 | |
707 | 707 | /** |
708 | 708 | * Reset the database class attributs to the initail values before each query. |
709 | 709 | */ |
710 | 710 | private function reset(){ |
711 | - //query builder reset |
|
712 | - $this->getQueryBuilder()->reset(); |
|
713 | - $this->numRows = 0; |
|
714 | - $this->insertId = null; |
|
715 | - $this->query = null; |
|
716 | - $this->result = array(); |
|
717 | - $this->data = array(); |
|
711 | + //query builder reset |
|
712 | + $this->getQueryBuilder()->reset(); |
|
713 | + $this->numRows = 0; |
|
714 | + $this->insertId = null; |
|
715 | + $this->query = null; |
|
716 | + $this->result = array(); |
|
717 | + $this->data = array(); |
|
718 | 718 | } |
719 | 719 | |
720 | 720 | /** |
721 | 721 | * The class destructor |
722 | 722 | */ |
723 | 723 | public function __destruct(){ |
724 | - $this->pdo = null; |
|
724 | + $this->pdo = null; |
|
725 | 725 | } |
726 | 726 | |
727 | 727 | } |
@@ -23,105 +23,105 @@ 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 | /** |
121 | 121 | * Construct new database |
122 | 122 | * @param array $overwriteConfig the config to overwrite with the config set in database.php |
123 | 123 | */ |
124 | - public function __construct($overwriteConfig = array()){ |
|
124 | + public function __construct($overwriteConfig = array()) { |
|
125 | 125 | //Set Log instance to use |
126 | 126 | $this->setLoggerFromParamOrCreateNewInstance(null); |
127 | 127 | |
@@ -142,17 +142,17 @@ discard block |
||
142 | 142 | * This is used to connect to database |
143 | 143 | * @return bool |
144 | 144 | */ |
145 | - public function connect(){ |
|
145 | + public function connect() { |
|
146 | 146 | $config = $this->getDatabaseConfiguration(); |
147 | - if (! empty($config)){ |
|
148 | - try{ |
|
147 | + if (!empty($config)) { |
|
148 | + try { |
|
149 | 149 | $this->pdo = new PDO($this->getDsnFromDriver(), $config['username'], $config['password']); |
150 | 150 | $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); |
151 | 151 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
152 | 152 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
153 | 153 | return true; |
154 | 154 | } |
155 | - catch (PDOException $e){ |
|
155 | + catch (PDOException $e) { |
|
156 | 156 | $this->logger->fatal($e->getMessage()); |
157 | 157 | show_error('Cannot connect to Database.'); |
158 | 158 | return false; |
@@ -166,7 +166,7 @@ discard block |
||
166 | 166 | * Return the number of rows returned by the current query |
167 | 167 | * @return int |
168 | 168 | */ |
169 | - public function numRows(){ |
|
169 | + public function numRows() { |
|
170 | 170 | return $this->numRows; |
171 | 171 | } |
172 | 172 | |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | * Return the last insert id value |
175 | 175 | * @return mixed |
176 | 176 | */ |
177 | - public function insertId(){ |
|
177 | + public function insertId() { |
|
178 | 178 | return $this->insertId; |
179 | 179 | } |
180 | 180 | |
@@ -185,10 +185,10 @@ discard block |
||
185 | 185 | * If is string will determine the result type "array" or "object" |
186 | 186 | * @return mixed the query SQL string or the record result |
187 | 187 | */ |
188 | - public function get($returnSQLQueryOrResultType = false){ |
|
188 | + public function get($returnSQLQueryOrResultType = false) { |
|
189 | 189 | $this->getQueryBuilder()->limit(1); |
190 | 190 | $query = $this->getAll(true); |
191 | - if ($returnSQLQueryOrResultType === true){ |
|
191 | + if ($returnSQLQueryOrResultType === true) { |
|
192 | 192 | return $query; |
193 | 193 | } else { |
194 | 194 | return $this->query($query, false, $returnSQLQueryOrResultType == 'array'); |
@@ -201,9 +201,9 @@ discard block |
||
201 | 201 | * If is string will determine the result type "array" or "object" |
202 | 202 | * @return mixed the query SQL string or the record result |
203 | 203 | */ |
204 | - public function getAll($returnSQLQueryOrResultType = false){ |
|
204 | + public function getAll($returnSQLQueryOrResultType = false) { |
|
205 | 205 | $query = $this->getQueryBuilder()->getQuery(); |
206 | - if ($returnSQLQueryOrResultType === true){ |
|
206 | + if ($returnSQLQueryOrResultType === true) { |
|
207 | 207 | return $query; |
208 | 208 | } |
209 | 209 | return $this->query($query, true, $returnSQLQueryOrResultType == 'array'); |
@@ -215,18 +215,18 @@ discard block |
||
215 | 215 | * @param boolean $escape whether to escape or not the values |
216 | 216 | * @return mixed the insert id of the new record or null |
217 | 217 | */ |
218 | - public function insert($data = array(), $escape = true){ |
|
219 | - if (empty($data) && $this->getData()){ |
|
218 | + public function insert($data = array(), $escape = true) { |
|
219 | + if (empty($data) && $this->getData()) { |
|
220 | 220 | //as when using $this->setData() may be the data already escaped |
221 | 221 | $escape = false; |
222 | 222 | $data = $this->getData(); |
223 | 223 | } |
224 | 224 | $query = $this->getQueryBuilder()->insert($data, $escape)->getQuery(); |
225 | 225 | $result = $this->query($query); |
226 | - if ($result){ |
|
226 | + if ($result) { |
|
227 | 227 | $this->insertId = $this->pdo->lastInsertId(); |
228 | 228 | //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
229 | - return ! $this->insertId() ? true : $this->insertId(); |
|
229 | + return !$this->insertId() ? true : $this->insertId(); |
|
230 | 230 | } |
231 | 231 | return false; |
232 | 232 | } |
@@ -237,8 +237,8 @@ discard block |
||
237 | 237 | * @param boolean $escape whether to escape or not the values |
238 | 238 | * @return mixed the update status |
239 | 239 | */ |
240 | - public function update($data = array(), $escape = true){ |
|
241 | - if (empty($data) && $this->getData()){ |
|
240 | + public function update($data = array(), $escape = true) { |
|
241 | + if (empty($data) && $this->getData()) { |
|
242 | 242 | //as when using $this->setData() may be the data already escaped |
243 | 243 | $escape = false; |
244 | 244 | $data = $this->getData(); |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | * Delete the record in database |
252 | 252 | * @return mixed the delete status |
253 | 253 | */ |
254 | - public function delete(){ |
|
254 | + public function delete() { |
|
255 | 255 | $query = $this->getQueryBuilder()->delete()->getQuery(); |
256 | 256 | return $this->query($query); |
257 | 257 | } |
@@ -261,8 +261,8 @@ discard block |
||
261 | 261 | * @param integer $ttl the cache time to live in second |
262 | 262 | * @return object the current Database instance |
263 | 263 | */ |
264 | - public function setCache($ttl = 0){ |
|
265 | - if ($ttl > 0){ |
|
264 | + public function setCache($ttl = 0) { |
|
265 | + if ($ttl > 0) { |
|
266 | 266 | $this->cacheTtl = $ttl; |
267 | 267 | $this->temporaryCacheTtl = $ttl; |
268 | 268 | } |
@@ -274,8 +274,8 @@ discard block |
||
274 | 274 | * @param integer $ttl the cache time to live in second |
275 | 275 | * @return object the current Database instance |
276 | 276 | */ |
277 | - public function cached($ttl = 0){ |
|
278 | - if ($ttl > 0){ |
|
277 | + public function cached($ttl = 0) { |
|
278 | + if ($ttl > 0) { |
|
279 | 279 | $this->temporaryCacheTtl = $ttl; |
280 | 280 | } |
281 | 281 | return $this; |
@@ -287,7 +287,7 @@ discard block |
||
287 | 287 | * @param boolean $escaped whether we can do escape of not |
288 | 288 | * @return mixed the data after escaped or the same data if not |
289 | 289 | */ |
290 | - public function escape($data, $escaped = true){ |
|
290 | + public function escape($data, $escaped = true) { |
|
291 | 291 | return $escaped ? |
292 | 292 | $this->pdo->quote(trim($data)) |
293 | 293 | : $data; |
@@ -297,7 +297,7 @@ discard block |
||
297 | 297 | * Return the number query executed count for the current request |
298 | 298 | * @return int |
299 | 299 | */ |
300 | - public function queryCount(){ |
|
300 | + public function queryCount() { |
|
301 | 301 | return $this->queryCount; |
302 | 302 | } |
303 | 303 | |
@@ -305,7 +305,7 @@ discard block |
||
305 | 305 | * Return the current query SQL string |
306 | 306 | * @return string |
307 | 307 | */ |
308 | - public function getQuery(){ |
|
308 | + public function getQuery() { |
|
309 | 309 | return $this->query; |
310 | 310 | } |
311 | 311 | |
@@ -313,7 +313,7 @@ discard block |
||
313 | 313 | * Return the application database name |
314 | 314 | * @return string |
315 | 315 | */ |
316 | - public function getDatabaseName(){ |
|
316 | + public function getDatabaseName() { |
|
317 | 317 | return $this->databaseName; |
318 | 318 | } |
319 | 319 | |
@@ -321,7 +321,7 @@ discard block |
||
321 | 321 | * Return the PDO instance |
322 | 322 | * @return object |
323 | 323 | */ |
324 | - public function getPdo(){ |
|
324 | + public function getPdo() { |
|
325 | 325 | return $this->pdo; |
326 | 326 | } |
327 | 327 | |
@@ -330,7 +330,7 @@ discard block |
||
330 | 330 | * @param object $pdo the pdo object |
331 | 331 | * @return object Database |
332 | 332 | */ |
333 | - public function setPdo(PDO $pdo){ |
|
333 | + public function setPdo(PDO $pdo) { |
|
334 | 334 | $this->pdo = $pdo; |
335 | 335 | return $this; |
336 | 336 | } |
@@ -340,7 +340,7 @@ discard block |
||
340 | 340 | * Return the Log instance |
341 | 341 | * @return Log |
342 | 342 | */ |
343 | - public function getLogger(){ |
|
343 | + public function getLogger() { |
|
344 | 344 | return $this->logger; |
345 | 345 | } |
346 | 346 | |
@@ -349,7 +349,7 @@ discard block |
||
349 | 349 | * @param Log $logger the log object |
350 | 350 | * @return object Database |
351 | 351 | */ |
352 | - public function setLogger($logger){ |
|
352 | + public function setLogger($logger) { |
|
353 | 353 | $this->logger = $logger; |
354 | 354 | return $this; |
355 | 355 | } |
@@ -358,7 +358,7 @@ discard block |
||
358 | 358 | * Return the cache instance |
359 | 359 | * @return CacheInterface |
360 | 360 | */ |
361 | - public function getCacheInstance(){ |
|
361 | + public function getCacheInstance() { |
|
362 | 362 | return $this->cacheInstance; |
363 | 363 | } |
364 | 364 | |
@@ -367,7 +367,7 @@ discard block |
||
367 | 367 | * @param CacheInterface $cache the cache object |
368 | 368 | * @return object Database |
369 | 369 | */ |
370 | - public function setCacheInstance($cache){ |
|
370 | + public function setCacheInstance($cache) { |
|
371 | 371 | $this->cacheInstance = $cache; |
372 | 372 | return $this; |
373 | 373 | } |
@@ -377,7 +377,7 @@ discard block |
||
377 | 377 | * Return the DatabaseQueryBuilder instance |
378 | 378 | * @return object DatabaseQueryBuilder |
379 | 379 | */ |
380 | - public function getQueryBuilder(){ |
|
380 | + public function getQueryBuilder() { |
|
381 | 381 | return $this->queryBuilder; |
382 | 382 | } |
383 | 383 | |
@@ -385,7 +385,7 @@ discard block |
||
385 | 385 | * Set the DatabaseQueryBuilder instance |
386 | 386 | * @param object DatabaseQueryBuilder $queryBuilder the DatabaseQueryBuilder object |
387 | 387 | */ |
388 | - public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder){ |
|
388 | + public function setQueryBuilder(DatabaseQueryBuilder $queryBuilder) { |
|
389 | 389 | $this->queryBuilder = $queryBuilder; |
390 | 390 | return $this; |
391 | 391 | } |
@@ -394,7 +394,7 @@ discard block |
||
394 | 394 | * Return the DatabaseQueryRunner instance |
395 | 395 | * @return object DatabaseQueryRunner |
396 | 396 | */ |
397 | - public function getQueryRunner(){ |
|
397 | + public function getQueryRunner() { |
|
398 | 398 | return $this->queryRunner; |
399 | 399 | } |
400 | 400 | |
@@ -402,7 +402,7 @@ discard block |
||
402 | 402 | * Set the DatabaseQueryRunner instance |
403 | 403 | * @param object DatabaseQueryRunner $queryRunner the DatabaseQueryRunner object |
404 | 404 | */ |
405 | - public function setQueryRunner(DatabaseQueryRunner $queryRunner){ |
|
405 | + public function setQueryRunner(DatabaseQueryRunner $queryRunner) { |
|
406 | 406 | $this->queryRunner = $queryRunner; |
407 | 407 | return $this; |
408 | 408 | } |
@@ -411,7 +411,7 @@ discard block |
||
411 | 411 | * Return the data to be used for insert, update, etc. |
412 | 412 | * @return array |
413 | 413 | */ |
414 | - public function getData(){ |
|
414 | + public function getData() { |
|
415 | 415 | return $this->data; |
416 | 416 | } |
417 | 417 | |
@@ -422,9 +422,9 @@ discard block |
||
422 | 422 | * @param boolean $escape whether to escape or not the $value |
423 | 423 | * @return object the current Database instance |
424 | 424 | */ |
425 | - public function setData($key, $value = null, $escape = true){ |
|
426 | - if(is_array($key)){ |
|
427 | - foreach($key as $k => $v){ |
|
425 | + public function setData($key, $value = null, $escape = true) { |
|
426 | + if (is_array($key)) { |
|
427 | + foreach ($key as $k => $v) { |
|
428 | 428 | $this->setData($k, $v, $escape); |
429 | 429 | } |
430 | 430 | } else { |
@@ -440,7 +440,7 @@ discard block |
||
440 | 440 | * @param boolean $returnAsArray return the result as array or not |
441 | 441 | * @return mixed the query result |
442 | 442 | */ |
443 | - public function query($query, $returnAsList = true, $returnAsArray = false){ |
|
443 | + public function query($query, $returnAsList = true, $returnAsArray = false) { |
|
444 | 444 | $this->reset(); |
445 | 445 | $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); |
446 | 446 | //If is the SELECT query |
@@ -461,12 +461,12 @@ discard block |
||
461 | 461 | //if can use cache feature for this query |
462 | 462 | $dbCacheStatus = $cacheEnable && $cacheExpire > 0; |
463 | 463 | |
464 | - if ($dbCacheStatus && $isSqlSELECTQuery){ |
|
464 | + if ($dbCacheStatus && $isSqlSELECTQuery) { |
|
465 | 465 | $this->logger->info('The cache is enabled for this query, try to get result from cache'); |
466 | 466 | $cacheContent = $this->getCacheContentForQuery($query, $returnAsList, $returnAsArray); |
467 | 467 | } |
468 | 468 | |
469 | - if ( !$cacheContent){ |
|
469 | + if (!$cacheContent) { |
|
470 | 470 | //count the number of query execution to server |
471 | 471 | $this->queryCount++; |
472 | 472 | |
@@ -475,23 +475,23 @@ discard block |
||
475 | 475 | $this->queryRunner->setReturnAsArray($returnAsArray); |
476 | 476 | |
477 | 477 | $queryResult = $this->queryRunner->execute(); |
478 | - if (is_object($queryResult)){ |
|
478 | + if (is_object($queryResult)) { |
|
479 | 479 | $this->result = $queryResult->getResult(); |
480 | 480 | $this->numRows = $queryResult->getNumRows(); |
481 | - if ($isSqlSELECTQuery && $dbCacheStatus){ |
|
481 | + if ($isSqlSELECTQuery && $dbCacheStatus) { |
|
482 | 482 | $this->setCacheContentForQuery( |
483 | 483 | $this->query, |
484 | 484 | $this->getCacheKeyForQuery($this->query, $returnAsList, $returnAsArray), |
485 | 485 | $this->result, |
486 | 486 | $cacheExpire |
487 | 487 | ); |
488 | - if (! $this->result){ |
|
488 | + if (!$this->result) { |
|
489 | 489 | $this->logger->info('No result where found for the query [' . $query . ']'); |
490 | 490 | } |
491 | 491 | } |
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 | } |
@@ -503,7 +503,7 @@ discard block |
||
503 | 503 | * Return the database configuration |
504 | 504 | * @return array |
505 | 505 | */ |
506 | - public function getDatabaseConfiguration(){ |
|
506 | + public function getDatabaseConfiguration() { |
|
507 | 507 | return $this->config; |
508 | 508 | } |
509 | 509 | |
@@ -513,9 +513,9 @@ discard block |
||
513 | 513 | * @param boolean $useConfigFile whether to use database configuration file |
514 | 514 | * @return object Database |
515 | 515 | */ |
516 | - public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
516 | + public function setDatabaseConfiguration(array $overwriteConfig = array(), $useConfigFile = true) { |
|
517 | 517 | $db = array(); |
518 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')){ |
|
518 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'database.php')) { |
|
519 | 519 | //here don't use require_once because somewhere user can create database instance directly |
520 | 520 | require CONFIG_PATH . 'database.php'; |
521 | 521 | } |
@@ -540,7 +540,7 @@ discard block |
||
540 | 540 | //determine the port using the hostname like localhost:3307 |
541 | 541 | //hostname will be "localhost", and port "3307" |
542 | 542 | $p = explode(':', $config['hostname']); |
543 | - if (count($p) >= 2){ |
|
543 | + if (count($p) >= 2) { |
|
544 | 544 | $config['hostname'] = $p[0]; |
545 | 545 | $config['port'] = $p[1]; |
546 | 546 | } |
@@ -567,7 +567,7 @@ discard block |
||
567 | 567 | /** |
568 | 568 | * Close the connexion |
569 | 569 | */ |
570 | - public function close(){ |
|
570 | + public function close() { |
|
571 | 571 | $this->pdo = null; |
572 | 572 | } |
573 | 573 | |
@@ -575,16 +575,16 @@ discard block |
||
575 | 575 | * Update the DatabaseQueryBuilder and DatabaseQueryRunner properties |
576 | 576 | * @return void |
577 | 577 | */ |
578 | - protected function updateQueryBuilderAndRunnerProperties(){ |
|
578 | + protected function updateQueryBuilderAndRunnerProperties() { |
|
579 | 579 | //update queryBuilder with some properties needed |
580 | - if(is_object($this->queryBuilder)){ |
|
580 | + if (is_object($this->queryBuilder)) { |
|
581 | 581 | $this->queryBuilder->setDriver($this->config['driver']); |
582 | 582 | $this->queryBuilder->setPrefix($this->config['prefix']); |
583 | 583 | $this->queryBuilder->setPdo($this->pdo); |
584 | 584 | } |
585 | 585 | |
586 | 586 | //update queryRunner with some properties needed |
587 | - if(is_object($this->queryRunner)){ |
|
587 | + if (is_object($this->queryRunner)) { |
|
588 | 588 | $this->queryRunner->setDriver($this->config['driver']); |
589 | 589 | $this->queryRunner->setPdo($this->pdo); |
590 | 590 | } |
@@ -595,9 +595,9 @@ discard block |
||
595 | 595 | * This method is used to get the PDO DSN string using the configured driver |
596 | 596 | * @return string the DSN string |
597 | 597 | */ |
598 | - protected function getDsnFromDriver(){ |
|
598 | + protected function getDsnFromDriver() { |
|
599 | 599 | $config = $this->getDatabaseConfiguration(); |
600 | - if (! empty($config)){ |
|
600 | + if (!empty($config)) { |
|
601 | 601 | $driver = $config['driver']; |
602 | 602 | $driverDsnMap = array( |
603 | 603 | 'mysql' => 'mysql:host=' . $config['hostname'] . ';' |
@@ -622,9 +622,9 @@ discard block |
||
622 | 622 | * |
623 | 623 | * @return mixed |
624 | 624 | */ |
625 | - protected function getCacheContentForQuery($query, $returnAsList, $returnAsArray){ |
|
625 | + protected function getCacheContentForQuery($query, $returnAsList, $returnAsArray) { |
|
626 | 626 | $cacheKey = $this->getCacheKeyForQuery($query, $returnAsList, $returnAsArray); |
627 | - if (! is_object($this->cacheInstance)){ |
|
627 | + if (!is_object($this->cacheInstance)) { |
|
628 | 628 | //can not call method with reference in argument |
629 | 629 | //like $this->setCacheInstance(& get_instance()->cache); |
630 | 630 | //use temporary variable |
@@ -641,9 +641,9 @@ discard block |
||
641 | 641 | * @param mixed $result the query result to save |
642 | 642 | * @param int $expire the cache TTL |
643 | 643 | */ |
644 | - protected function setCacheContentForQuery($query, $key, $result, $expire){ |
|
645 | - $this->logger->info('Save the result for query [' .$query. '] into cache for future use'); |
|
646 | - if (! is_object($this->cacheInstance)){ |
|
644 | + protected function setCacheContentForQuery($query, $key, $result, $expire) { |
|
645 | + $this->logger->info('Save the result for query [' . $query . '] into cache for future use'); |
|
646 | + if (!is_object($this->cacheInstance)) { |
|
647 | 647 | //can not call method with reference in argument |
648 | 648 | //like $this->setCacheInstance(& get_instance()->cache); |
649 | 649 | //use temporary variable |
@@ -660,7 +660,7 @@ discard block |
||
660 | 660 | * |
661 | 661 | * @return string |
662 | 662 | */ |
663 | - protected function getCacheKeyForQuery($query, $returnAsList, $returnAsArray){ |
|
663 | + protected function getCacheKeyForQuery($query, $returnAsList, $returnAsArray) { |
|
664 | 664 | return md5($query . $returnAsList . $returnAsArray); |
665 | 665 | } |
666 | 666 | |
@@ -668,12 +668,12 @@ discard block |
||
668 | 668 | * Set the Log instance using argument or create new instance |
669 | 669 | * @param object $logger the Log instance if not null |
670 | 670 | */ |
671 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
672 | - if ($logger !== null){ |
|
671 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null) { |
|
672 | + if ($logger !== null) { |
|
673 | 673 | $this->logger = $logger; |
674 | 674 | } |
675 | - else{ |
|
676 | - $this->logger =& class_loader('Log', 'classes'); |
|
675 | + else { |
|
676 | + $this->logger = & class_loader('Log', 'classes'); |
|
677 | 677 | $this->logger->setLogger('Library::Database'); |
678 | 678 | } |
679 | 679 | } |
@@ -682,12 +682,12 @@ discard block |
||
682 | 682 | * Set the DatabaseQueryBuilder instance using argument or create new instance |
683 | 683 | * @param object $queryBuilder the DatabaseQueryBuilder instance if not null |
684 | 684 | */ |
685 | - protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null){ |
|
686 | - if ($queryBuilder !== null){ |
|
685 | + protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null) { |
|
686 | + if ($queryBuilder !== null) { |
|
687 | 687 | $this->queryBuilder = $queryBuilder; |
688 | 688 | } |
689 | - else{ |
|
690 | - $this->queryBuilder =& class_loader('DatabaseQueryBuilder', 'classes/database'); |
|
689 | + else { |
|
690 | + $this->queryBuilder = & class_loader('DatabaseQueryBuilder', 'classes/database'); |
|
691 | 691 | } |
692 | 692 | } |
693 | 693 | |
@@ -695,19 +695,19 @@ discard block |
||
695 | 695 | * Set the DatabaseQueryRunner instance using argument or create new instance |
696 | 696 | * @param object $queryRunner the DatabaseQueryRunner instance if not null |
697 | 697 | */ |
698 | - protected function setQueryRunnerFromParamOrCreateNewInstance(DatabaseQueryRunner $queryRunner = null){ |
|
699 | - if ($queryRunner !== null){ |
|
698 | + protected function setQueryRunnerFromParamOrCreateNewInstance(DatabaseQueryRunner $queryRunner = null) { |
|
699 | + if ($queryRunner !== null) { |
|
700 | 700 | $this->queryRunner = $queryRunner; |
701 | 701 | } |
702 | - else{ |
|
703 | - $this->queryRunner =& class_loader('DatabaseQueryRunner', 'classes/database'); |
|
702 | + else { |
|
703 | + $this->queryRunner = & class_loader('DatabaseQueryRunner', 'classes/database'); |
|
704 | 704 | } |
705 | 705 | } |
706 | 706 | |
707 | 707 | /** |
708 | 708 | * Reset the database class attributs to the initail values before each query. |
709 | 709 | */ |
710 | - private function reset(){ |
|
710 | + private function reset() { |
|
711 | 711 | //query builder reset |
712 | 712 | $this->getQueryBuilder()->reset(); |
713 | 713 | $this->numRows = 0; |
@@ -720,7 +720,7 @@ discard block |
||
720 | 720 | /** |
721 | 721 | * The class destructor |
722 | 722 | */ |
723 | - public function __destruct(){ |
|
723 | + public function __destruct() { |
|
724 | 724 | $this->pdo = null; |
725 | 725 | } |
726 | 726 |
@@ -151,8 +151,7 @@ discard block |
||
151 | 151 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); |
152 | 152 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
153 | 153 | return true; |
154 | - } |
|
155 | - catch (PDOException $e){ |
|
154 | + } catch (PDOException $e){ |
|
156 | 155 | $this->logger->fatal($e->getMessage()); |
157 | 156 | show_error('Cannot connect to Database.'); |
158 | 157 | return false; |
@@ -671,8 +670,7 @@ discard block |
||
671 | 670 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
672 | 671 | if ($logger !== null){ |
673 | 672 | $this->logger = $logger; |
674 | - } |
|
675 | - else{ |
|
673 | + } else{ |
|
676 | 674 | $this->logger =& class_loader('Log', 'classes'); |
677 | 675 | $this->logger->setLogger('Library::Database'); |
678 | 676 | } |
@@ -685,8 +683,7 @@ discard block |
||
685 | 683 | protected function setQueryBuilderFromParamOrCreateNewInstance(DatabaseQueryBuilder $queryBuilder = null){ |
686 | 684 | if ($queryBuilder !== null){ |
687 | 685 | $this->queryBuilder = $queryBuilder; |
688 | - } |
|
689 | - else{ |
|
686 | + } else{ |
|
690 | 687 | $this->queryBuilder =& class_loader('DatabaseQueryBuilder', 'classes/database'); |
691 | 688 | } |
692 | 689 | } |
@@ -698,8 +695,7 @@ discard block |
||
698 | 695 | protected function setQueryRunnerFromParamOrCreateNewInstance(DatabaseQueryRunner $queryRunner = null){ |
699 | 696 | if ($queryRunner !== null){ |
700 | 697 | $this->queryRunner = $queryRunner; |
701 | - } |
|
702 | - else{ |
|
698 | + } else{ |
|
703 | 699 | $this->queryRunner =& class_loader('DatabaseQueryRunner', 'classes/database'); |
704 | 700 | } |
705 | 701 | } |
@@ -1,318 +1,318 @@ |
||
1 | 1 | <?php |
2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
3 | - /** |
|
4 | - * TNH Framework |
|
5 | - * |
|
6 | - * A simple PHP framework using HMVC architecture |
|
7 | - * |
|
8 | - * This content is released under the GNU GPL License (GPL) |
|
9 | - * |
|
10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
11 | - * |
|
12 | - * This program is free software; you can redistribute it and/or |
|
13 | - * modify it under the terms of the GNU General Public License |
|
14 | - * as published by the Free Software Foundation; either version 3 |
|
15 | - * of the License, or (at your option) any later version. |
|
16 | - * |
|
17 | - * This program is distributed in the hope that it will be useful, |
|
18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | - * GNU General Public License for more details. |
|
21 | - * |
|
22 | - * You should have received a copy of the GNU General Public License |
|
23 | - * along with this program; if not, write to the Free Software |
|
24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | - */ |
|
2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
3 | + /** |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | 26 | |
27 | - class FileCache implements CacheInterface{ |
|
27 | + class FileCache implements CacheInterface{ |
|
28 | 28 | |
29 | - /** |
|
30 | - * Whether to enable compression of the cache data file. |
|
31 | - * @var boolean |
|
32 | - */ |
|
33 | - private $compressCacheData = true; |
|
29 | + /** |
|
30 | + * Whether to enable compression of the cache data file. |
|
31 | + * @var boolean |
|
32 | + */ |
|
33 | + private $compressCacheData = true; |
|
34 | 34 | |
35 | - /** |
|
36 | - * The logger instance |
|
37 | - * @var Log |
|
38 | - */ |
|
39 | - private $logger; |
|
35 | + /** |
|
36 | + * The logger instance |
|
37 | + * @var Log |
|
38 | + */ |
|
39 | + private $logger; |
|
40 | 40 | |
41 | 41 | |
42 | - public function __construct(Log $logger = null){ |
|
43 | - if(! $this->isSupported()){ |
|
44 | - show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
|
45 | - } |
|
46 | - /** |
|
47 | - * instance of the Log class |
|
48 | - */ |
|
49 | - if(is_object($logger)){ |
|
50 | - $this->logger = $logger; |
|
51 | - } |
|
52 | - else{ |
|
53 | - $this->logger =& class_loader('Log', 'classes'); |
|
54 | - $this->logger->setLogger('Library::FileCache'); |
|
55 | - } |
|
42 | + public function __construct(Log $logger = null){ |
|
43 | + if(! $this->isSupported()){ |
|
44 | + show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
|
45 | + } |
|
46 | + /** |
|
47 | + * instance of the Log class |
|
48 | + */ |
|
49 | + if(is_object($logger)){ |
|
50 | + $this->logger = $logger; |
|
51 | + } |
|
52 | + else{ |
|
53 | + $this->logger =& class_loader('Log', 'classes'); |
|
54 | + $this->logger->setLogger('Library::FileCache'); |
|
55 | + } |
|
56 | 56 | |
57 | - //if Zlib extension is not loaded set compressCacheData to false |
|
58 | - if(! extension_loaded('zlib')){ |
|
59 | - $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
60 | - $this->compressCacheData = false; |
|
61 | - } |
|
62 | - } |
|
57 | + //if Zlib extension is not loaded set compressCacheData to false |
|
58 | + if(! extension_loaded('zlib')){ |
|
59 | + $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
60 | + $this->compressCacheData = false; |
|
61 | + } |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * This is used to get the cache data using the key |
|
66 | - * @param string $key the key to identify the cache data |
|
67 | - * @return mixed the cache data if exists else return false |
|
68 | - */ |
|
69 | - public function get($key){ |
|
70 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
71 | - $filePath = $this->getFilePath($key); |
|
72 | - if(! file_exists($filePath)){ |
|
73 | - $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
74 | - return false; |
|
75 | - } |
|
76 | - $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
77 | - $handle = fopen($filePath,'r'); |
|
78 | - if(! is_resource($handle)){ |
|
79 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
80 | - return false; |
|
81 | - } |
|
82 | - // Getting a shared lock |
|
83 | - flock($handle, LOCK_SH); |
|
84 | - $data = file_get_contents($filePath); |
|
85 | - fclose($handle); |
|
86 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
87 | - if (! $data) { |
|
88 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
89 | - // If unserializing somehow didn't work out, we'll delete the file |
|
90 | - unlink($filePath); |
|
91 | - return false; |
|
92 | - } |
|
93 | - if (time() > $data['expire']) { |
|
94 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
95 | - // Unlinking when the file was expired |
|
96 | - unlink($filePath); |
|
97 | - return false; |
|
98 | - } |
|
99 | - else{ |
|
100 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
101 | - return $data['data']; |
|
102 | - } |
|
103 | - } |
|
64 | + /** |
|
65 | + * This is used to get the cache data using the key |
|
66 | + * @param string $key the key to identify the cache data |
|
67 | + * @return mixed the cache data if exists else return false |
|
68 | + */ |
|
69 | + public function get($key){ |
|
70 | + $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
71 | + $filePath = $this->getFilePath($key); |
|
72 | + if(! file_exists($filePath)){ |
|
73 | + $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
74 | + return false; |
|
75 | + } |
|
76 | + $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
77 | + $handle = fopen($filePath,'r'); |
|
78 | + if(! is_resource($handle)){ |
|
79 | + $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
80 | + return false; |
|
81 | + } |
|
82 | + // Getting a shared lock |
|
83 | + flock($handle, LOCK_SH); |
|
84 | + $data = file_get_contents($filePath); |
|
85 | + fclose($handle); |
|
86 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
87 | + if (! $data) { |
|
88 | + $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
89 | + // If unserializing somehow didn't work out, we'll delete the file |
|
90 | + unlink($filePath); |
|
91 | + return false; |
|
92 | + } |
|
93 | + if (time() > $data['expire']) { |
|
94 | + $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
95 | + // Unlinking when the file was expired |
|
96 | + unlink($filePath); |
|
97 | + return false; |
|
98 | + } |
|
99 | + else{ |
|
100 | + $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
101 | + return $data['data']; |
|
102 | + } |
|
103 | + } |
|
104 | 104 | |
105 | 105 | |
106 | - /** |
|
107 | - * Save data to the cache |
|
108 | - * @param string $key the key to identify this cache data |
|
109 | - * @param mixed $data the cache data |
|
110 | - * @param integer $ttl the cache life time |
|
111 | - * @return boolean true if success otherwise will return false |
|
112 | - */ |
|
113 | - public function set($key, $data, $ttl = 0){ |
|
114 | - $expire = time() + $ttl; |
|
115 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
116 | - $filePath = $this->getFilePath($key); |
|
117 | - $handle = fopen($filePath,'w'); |
|
118 | - if(! is_resource($handle)){ |
|
119 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
120 | - return false; |
|
121 | - } |
|
122 | - flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
123 | - //Serializing along with the TTL |
|
124 | - $cacheData = serialize(array( |
|
125 | - 'mtime' => time(), |
|
126 | - 'expire' => $expire, |
|
127 | - 'data' => $data, |
|
128 | - 'ttl' => $ttl |
|
129 | - ) |
|
130 | - ); |
|
131 | - $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
132 | - if(! $result){ |
|
133 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
134 | - fclose($handle); |
|
135 | - return false; |
|
136 | - } |
|
137 | - else{ |
|
138 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
139 | - fclose($handle); |
|
140 | - chmod($filePath, 0640); |
|
141 | - return true; |
|
142 | - } |
|
143 | - } |
|
106 | + /** |
|
107 | + * Save data to the cache |
|
108 | + * @param string $key the key to identify this cache data |
|
109 | + * @param mixed $data the cache data |
|
110 | + * @param integer $ttl the cache life time |
|
111 | + * @return boolean true if success otherwise will return false |
|
112 | + */ |
|
113 | + public function set($key, $data, $ttl = 0){ |
|
114 | + $expire = time() + $ttl; |
|
115 | + $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
116 | + $filePath = $this->getFilePath($key); |
|
117 | + $handle = fopen($filePath,'w'); |
|
118 | + if(! is_resource($handle)){ |
|
119 | + $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
120 | + return false; |
|
121 | + } |
|
122 | + flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
123 | + //Serializing along with the TTL |
|
124 | + $cacheData = serialize(array( |
|
125 | + 'mtime' => time(), |
|
126 | + 'expire' => $expire, |
|
127 | + 'data' => $data, |
|
128 | + 'ttl' => $ttl |
|
129 | + ) |
|
130 | + ); |
|
131 | + $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
132 | + if(! $result){ |
|
133 | + $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
134 | + fclose($handle); |
|
135 | + return false; |
|
136 | + } |
|
137 | + else{ |
|
138 | + $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
139 | + fclose($handle); |
|
140 | + chmod($filePath, 0640); |
|
141 | + return true; |
|
142 | + } |
|
143 | + } |
|
144 | 144 | |
145 | 145 | |
146 | - /** |
|
147 | - * Delete the cache data for given key |
|
148 | - * @param string $key the key for cache to be deleted |
|
149 | - * @return boolean true if the cache is delete, false if can't delete |
|
150 | - * the cache or the cache with the given key not exist |
|
151 | - */ |
|
152 | - public function delete($key){ |
|
153 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
154 | - $filePath = $this->getFilePath($key); |
|
155 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
156 | - if(! file_exists($filePath)){ |
|
157 | - $this->logger->info('This cache file does not exists skipping'); |
|
158 | - return false; |
|
159 | - } |
|
160 | - else{ |
|
161 | - $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
162 | - unlink($filePath); |
|
163 | - return true; |
|
164 | - } |
|
165 | - } |
|
146 | + /** |
|
147 | + * Delete the cache data for given key |
|
148 | + * @param string $key the key for cache to be deleted |
|
149 | + * @return boolean true if the cache is delete, false if can't delete |
|
150 | + * the cache or the cache with the given key not exist |
|
151 | + */ |
|
152 | + public function delete($key){ |
|
153 | + $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
154 | + $filePath = $this->getFilePath($key); |
|
155 | + $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
156 | + if(! file_exists($filePath)){ |
|
157 | + $this->logger->info('This cache file does not exists skipping'); |
|
158 | + return false; |
|
159 | + } |
|
160 | + else{ |
|
161 | + $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
162 | + unlink($filePath); |
|
163 | + return true; |
|
164 | + } |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * Get the cache information for given key |
|
169 | - * @param string $key the key for cache to get the information for |
|
170 | - * @return boolean|array the cache information. The associative array and must contains the following information: |
|
171 | - * 'mtime' => creation time of the cache (Unix timestamp), |
|
172 | - * 'expire' => expiration time of the cache (Unix timestamp), |
|
173 | - * 'ttl' => the time to live of the cache in second |
|
174 | - */ |
|
175 | - public function getInfo($key){ |
|
176 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
177 | - $filePath = $this->getFilePath($key); |
|
178 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
179 | - if(! file_exists($filePath)){ |
|
180 | - $this->logger->info('This cache file does not exists skipping'); |
|
181 | - return false; |
|
182 | - } |
|
183 | - else{ |
|
184 | - $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
185 | - $data = file_get_contents($filePath); |
|
186 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
187 | - if(! $data){ |
|
188 | - $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
|
189 | - return false; |
|
190 | - } |
|
191 | - else{ |
|
192 | - $this->logger->info('This cache data is OK check for expire'); |
|
193 | - if(isset($data['expire']) && $data['expire'] > time()){ |
|
194 | - $this->logger->info('This cache not yet expired return cache informations'); |
|
195 | - $info = array( |
|
196 | - 'mtime' => $data['mtime'], |
|
197 | - 'expire' => $data['expire'], |
|
198 | - 'ttl' => $data['ttl'] |
|
199 | - ); |
|
200 | - return $info; |
|
201 | - } |
|
202 | - else{ |
|
203 | - $this->logger->info('This cache already expired return false'); |
|
204 | - return false; |
|
205 | - } |
|
206 | - } |
|
207 | - } |
|
208 | - } |
|
167 | + /** |
|
168 | + * Get the cache information for given key |
|
169 | + * @param string $key the key for cache to get the information for |
|
170 | + * @return boolean|array the cache information. The associative array and must contains the following information: |
|
171 | + * 'mtime' => creation time of the cache (Unix timestamp), |
|
172 | + * 'expire' => expiration time of the cache (Unix timestamp), |
|
173 | + * 'ttl' => the time to live of the cache in second |
|
174 | + */ |
|
175 | + public function getInfo($key){ |
|
176 | + $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
177 | + $filePath = $this->getFilePath($key); |
|
178 | + $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
179 | + if(! file_exists($filePath)){ |
|
180 | + $this->logger->info('This cache file does not exists skipping'); |
|
181 | + return false; |
|
182 | + } |
|
183 | + else{ |
|
184 | + $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
185 | + $data = file_get_contents($filePath); |
|
186 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
187 | + if(! $data){ |
|
188 | + $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
|
189 | + return false; |
|
190 | + } |
|
191 | + else{ |
|
192 | + $this->logger->info('This cache data is OK check for expire'); |
|
193 | + if(isset($data['expire']) && $data['expire'] > time()){ |
|
194 | + $this->logger->info('This cache not yet expired return cache informations'); |
|
195 | + $info = array( |
|
196 | + 'mtime' => $data['mtime'], |
|
197 | + 'expire' => $data['expire'], |
|
198 | + 'ttl' => $data['ttl'] |
|
199 | + ); |
|
200 | + return $info; |
|
201 | + } |
|
202 | + else{ |
|
203 | + $this->logger->info('This cache already expired return false'); |
|
204 | + return false; |
|
205 | + } |
|
206 | + } |
|
207 | + } |
|
208 | + } |
|
209 | 209 | |
210 | 210 | |
211 | - /** |
|
212 | - * Used to delete expired cache data |
|
213 | - */ |
|
214 | - public function deleteExpiredCache(){ |
|
215 | - $this->logger->debug('Deleting of expired cache files'); |
|
216 | - $list = glob(CACHE_PATH . '*.cache'); |
|
217 | - if(! $list){ |
|
218 | - $this->logger->info('No cache files were found skipping'); |
|
219 | - } |
|
220 | - else{ |
|
221 | - $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
|
222 | - foreach ($list as $file) { |
|
223 | - $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
224 | - $data = file_get_contents($file); |
|
225 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
226 | - if(! $data){ |
|
227 | - $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
228 | - } |
|
229 | - else if(time() > $data['expire']){ |
|
230 | - $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
231 | - unlink($file); |
|
232 | - } |
|
233 | - else{ |
|
234 | - $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
235 | - } |
|
236 | - } |
|
237 | - } |
|
238 | - } |
|
211 | + /** |
|
212 | + * Used to delete expired cache data |
|
213 | + */ |
|
214 | + public function deleteExpiredCache(){ |
|
215 | + $this->logger->debug('Deleting of expired cache files'); |
|
216 | + $list = glob(CACHE_PATH . '*.cache'); |
|
217 | + if(! $list){ |
|
218 | + $this->logger->info('No cache files were found skipping'); |
|
219 | + } |
|
220 | + else{ |
|
221 | + $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
|
222 | + foreach ($list as $file) { |
|
223 | + $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
224 | + $data = file_get_contents($file); |
|
225 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
226 | + if(! $data){ |
|
227 | + $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
228 | + } |
|
229 | + else if(time() > $data['expire']){ |
|
230 | + $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
231 | + unlink($file); |
|
232 | + } |
|
233 | + else{ |
|
234 | + $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
235 | + } |
|
236 | + } |
|
237 | + } |
|
238 | + } |
|
239 | 239 | |
240 | - /** |
|
241 | - * Remove all file from cache folder |
|
242 | - */ |
|
243 | - public function clean(){ |
|
244 | - $this->logger->debug('Deleting of all cache files'); |
|
245 | - $list = glob(CACHE_PATH . '*.cache'); |
|
246 | - if(! $list){ |
|
247 | - $this->logger->info('No cache files were found skipping'); |
|
248 | - } |
|
249 | - else{ |
|
250 | - $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
|
251 | - foreach ($list as $file) { |
|
252 | - $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
253 | - unlink($file); |
|
254 | - } |
|
255 | - } |
|
256 | - } |
|
240 | + /** |
|
241 | + * Remove all file from cache folder |
|
242 | + */ |
|
243 | + public function clean(){ |
|
244 | + $this->logger->debug('Deleting of all cache files'); |
|
245 | + $list = glob(CACHE_PATH . '*.cache'); |
|
246 | + if(! $list){ |
|
247 | + $this->logger->info('No cache files were found skipping'); |
|
248 | + } |
|
249 | + else{ |
|
250 | + $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
|
251 | + foreach ($list as $file) { |
|
252 | + $this->logger->debug('Processing the cache file [' . $file . ']'); |
|
253 | + unlink($file); |
|
254 | + } |
|
255 | + } |
|
256 | + } |
|
257 | 257 | |
258 | - /** |
|
259 | - * @return boolean |
|
260 | - */ |
|
261 | - public function isCompressCacheData(){ |
|
262 | - return $this->compressCacheData; |
|
263 | - } |
|
258 | + /** |
|
259 | + * @return boolean |
|
260 | + */ |
|
261 | + public function isCompressCacheData(){ |
|
262 | + return $this->compressCacheData; |
|
263 | + } |
|
264 | 264 | |
265 | - /** |
|
266 | - * @param boolean $compressCacheData |
|
267 | - * |
|
268 | - * @return object |
|
269 | - */ |
|
270 | - public function setCompressCacheData($status = true){ |
|
271 | - //if Zlib extension is not loaded set compressCacheData to false |
|
272 | - if($status === true && ! extension_loaded('zlib')){ |
|
265 | + /** |
|
266 | + * @param boolean $compressCacheData |
|
267 | + * |
|
268 | + * @return object |
|
269 | + */ |
|
270 | + public function setCompressCacheData($status = true){ |
|
271 | + //if Zlib extension is not loaded set compressCacheData to false |
|
272 | + if($status === true && ! extension_loaded('zlib')){ |
|
273 | 273 | |
274 | - $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
275 | - $this->compressCacheData = false; |
|
276 | - } |
|
277 | - else{ |
|
278 | - $this->compressCacheData = $status; |
|
279 | - } |
|
280 | - return $this; |
|
281 | - } |
|
274 | + $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
|
275 | + $this->compressCacheData = false; |
|
276 | + } |
|
277 | + else{ |
|
278 | + $this->compressCacheData = $status; |
|
279 | + } |
|
280 | + return $this; |
|
281 | + } |
|
282 | 282 | |
283 | - /** |
|
284 | - * Check whether the cache feature for the handle is supported |
|
285 | - * |
|
286 | - * @return bool |
|
287 | - */ |
|
288 | - public function isSupported(){ |
|
289 | - return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
|
290 | - } |
|
283 | + /** |
|
284 | + * Check whether the cache feature for the handle is supported |
|
285 | + * |
|
286 | + * @return bool |
|
287 | + */ |
|
288 | + public function isSupported(){ |
|
289 | + return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
|
290 | + } |
|
291 | 291 | |
292 | - /** |
|
293 | - * Return the Log instance |
|
294 | - * @return object |
|
295 | - */ |
|
296 | - public function getLogger(){ |
|
297 | - return $this->logger; |
|
298 | - } |
|
292 | + /** |
|
293 | + * Return the Log instance |
|
294 | + * @return object |
|
295 | + */ |
|
296 | + public function getLogger(){ |
|
297 | + return $this->logger; |
|
298 | + } |
|
299 | 299 | |
300 | - /** |
|
301 | - * Set the log instance |
|
302 | - * @param Log $logger the log object |
|
303 | - */ |
|
304 | - public function setLogger(Log $logger){ |
|
305 | - $this->logger = $logger; |
|
306 | - return $this; |
|
307 | - } |
|
300 | + /** |
|
301 | + * Set the log instance |
|
302 | + * @param Log $logger the log object |
|
303 | + */ |
|
304 | + public function setLogger(Log $logger){ |
|
305 | + $this->logger = $logger; |
|
306 | + return $this; |
|
307 | + } |
|
308 | 308 | |
309 | - /** |
|
310 | - * Get the cache file full path for the given key |
|
311 | - * |
|
312 | - * @param string $key the cache item key |
|
313 | - * @return string the full cache file path for this key |
|
314 | - */ |
|
315 | - private function getFilePath($key){ |
|
316 | - return CACHE_PATH . md5($key) . '.cache'; |
|
317 | - } |
|
318 | - } |
|
309 | + /** |
|
310 | + * Get the cache file full path for the given key |
|
311 | + * |
|
312 | + * @param string $key the cache item key |
|
313 | + * @return string the full cache file path for this key |
|
314 | + */ |
|
315 | + private function getFilePath($key){ |
|
316 | + return CACHE_PATH . md5($key) . '.cache'; |
|
317 | + } |
|
318 | + } |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | 25 | */ |
26 | 26 | |
27 | - class FileCache implements CacheInterface{ |
|
27 | + class FileCache implements CacheInterface { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * Whether to enable compression of the cache data file. |
@@ -39,23 +39,23 @@ discard block |
||
39 | 39 | private $logger; |
40 | 40 | |
41 | 41 | |
42 | - public function __construct(Log $logger = null){ |
|
43 | - if(! $this->isSupported()){ |
|
42 | + public function __construct(Log $logger = null) { |
|
43 | + if (!$this->isSupported()) { |
|
44 | 44 | show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
45 | 45 | } |
46 | 46 | /** |
47 | 47 | * instance of the Log class |
48 | 48 | */ |
49 | - if(is_object($logger)){ |
|
49 | + if (is_object($logger)) { |
|
50 | 50 | $this->logger = $logger; |
51 | 51 | } |
52 | - else{ |
|
53 | - $this->logger =& class_loader('Log', 'classes'); |
|
52 | + else { |
|
53 | + $this->logger = & class_loader('Log', 'classes'); |
|
54 | 54 | $this->logger->setLogger('Library::FileCache'); |
55 | 55 | } |
56 | 56 | |
57 | 57 | //if Zlib extension is not loaded set compressCacheData to false |
58 | - if(! extension_loaded('zlib')){ |
|
58 | + if (!extension_loaded('zlib')) { |
|
59 | 59 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
60 | 60 | $this->compressCacheData = false; |
61 | 61 | } |
@@ -66,17 +66,17 @@ discard block |
||
66 | 66 | * @param string $key the key to identify the cache data |
67 | 67 | * @return mixed the cache data if exists else return false |
68 | 68 | */ |
69 | - public function get($key){ |
|
70 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
69 | + public function get($key) { |
|
70 | + $this->logger->debug('Getting cache data for key [' . $key . ']'); |
|
71 | 71 | $filePath = $this->getFilePath($key); |
72 | - if(! file_exists($filePath)){ |
|
73 | - $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
72 | + if (!file_exists($filePath)) { |
|
73 | + $this->logger->info('No cache file found for the key [' . $key . '], return false'); |
|
74 | 74 | return false; |
75 | 75 | } |
76 | - $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
77 | - $handle = fopen($filePath,'r'); |
|
78 | - if(! is_resource($handle)){ |
|
79 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
76 | + $this->logger->info('The cache file [' . $filePath . '] for the key [' . $key . '] exists, check if the cache data is valid'); |
|
77 | + $handle = fopen($filePath, 'r'); |
|
78 | + if (!is_resource($handle)) { |
|
79 | + $this->logger->error('Can not open the file cache [' . $filePath . '] for the key [' . $key . '], return false'); |
|
80 | 80 | return false; |
81 | 81 | } |
82 | 82 | // Getting a shared lock |
@@ -84,20 +84,20 @@ discard block |
||
84 | 84 | $data = file_get_contents($filePath); |
85 | 85 | fclose($handle); |
86 | 86 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
87 | - if (! $data) { |
|
88 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
87 | + if (!$data) { |
|
88 | + $this->logger->error('Can not unserialize the cache data for the key [' . $key . '], return false'); |
|
89 | 89 | // If unserializing somehow didn't work out, we'll delete the file |
90 | 90 | unlink($filePath); |
91 | 91 | return false; |
92 | 92 | } |
93 | 93 | if (time() > $data['expire']) { |
94 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
94 | + $this->logger->info('The cache data for the key [' . $key . '] already expired delete the cache file [' . $filePath . ']'); |
|
95 | 95 | // Unlinking when the file was expired |
96 | 96 | unlink($filePath); |
97 | 97 | return false; |
98 | 98 | } |
99 | - else{ |
|
100 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
99 | + else { |
|
100 | + $this->logger->info('The cache not yet expire, now return the cache data for key [' . $key . '], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
101 | 101 | return $data['data']; |
102 | 102 | } |
103 | 103 | } |
@@ -110,13 +110,13 @@ discard block |
||
110 | 110 | * @param integer $ttl the cache life time |
111 | 111 | * @return boolean true if success otherwise will return false |
112 | 112 | */ |
113 | - public function set($key, $data, $ttl = 0){ |
|
113 | + public function set($key, $data, $ttl = 0) { |
|
114 | 114 | $expire = time() + $ttl; |
115 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
115 | + $this->logger->debug('Setting cache data for key [' . $key . '], time to live [' . $ttl . '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
116 | 116 | $filePath = $this->getFilePath($key); |
117 | - $handle = fopen($filePath,'w'); |
|
118 | - if(! is_resource($handle)){ |
|
119 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
117 | + $handle = fopen($filePath, 'w'); |
|
118 | + if (!is_resource($handle)) { |
|
119 | + $this->logger->error('Can not open the file cache [' . $filePath . '] for the key [' . $key . '], return false'); |
|
120 | 120 | return false; |
121 | 121 | } |
122 | 122 | flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
@@ -129,13 +129,13 @@ discard block |
||
129 | 129 | ) |
130 | 130 | ); |
131 | 131 | $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
132 | - if(! $result){ |
|
133 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
132 | + if (!$result) { |
|
133 | + $this->logger->error('Can not write cache data into file [' . $filePath . '] for the key [' . $key . '], return false'); |
|
134 | 134 | fclose($handle); |
135 | 135 | return false; |
136 | 136 | } |
137 | - else{ |
|
138 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
137 | + else { |
|
138 | + $this->logger->info('Cache data saved into file [' . $filePath . '] for the key [' . $key . ']'); |
|
139 | 139 | fclose($handle); |
140 | 140 | chmod($filePath, 0640); |
141 | 141 | return true; |
@@ -149,16 +149,16 @@ discard block |
||
149 | 149 | * @return boolean true if the cache is delete, false if can't delete |
150 | 150 | * the cache or the cache with the given key not exist |
151 | 151 | */ |
152 | - public function delete($key){ |
|
153 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
152 | + public function delete($key) { |
|
153 | + $this->logger->debug('Deleting of cache data for key [' . $key . ']'); |
|
154 | 154 | $filePath = $this->getFilePath($key); |
155 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
156 | - if(! file_exists($filePath)){ |
|
155 | + $this->logger->info('The file path for the key [' . $key . '] is [' . $filePath . ']'); |
|
156 | + if (!file_exists($filePath)) { |
|
157 | 157 | $this->logger->info('This cache file does not exists skipping'); |
158 | 158 | return false; |
159 | 159 | } |
160 | - else{ |
|
161 | - $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
160 | + else { |
|
161 | + $this->logger->info('Found cache file [' . $filePath . '] remove it'); |
|
162 | 162 | unlink($filePath); |
163 | 163 | return true; |
164 | 164 | } |
@@ -172,25 +172,25 @@ discard block |
||
172 | 172 | * 'expire' => expiration time of the cache (Unix timestamp), |
173 | 173 | * 'ttl' => the time to live of the cache in second |
174 | 174 | */ |
175 | - public function getInfo($key){ |
|
176 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
175 | + public function getInfo($key) { |
|
176 | + $this->logger->debug('Getting of cache info for key [' . $key . ']'); |
|
177 | 177 | $filePath = $this->getFilePath($key); |
178 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
179 | - if(! file_exists($filePath)){ |
|
178 | + $this->logger->info('The file path for the key [' . $key . '] is [' . $filePath . ']'); |
|
179 | + if (!file_exists($filePath)) { |
|
180 | 180 | $this->logger->info('This cache file does not exists skipping'); |
181 | 181 | return false; |
182 | 182 | } |
183 | - else{ |
|
184 | - $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
183 | + else { |
|
184 | + $this->logger->info('Found cache file [' . $filePath . '] check the validity'); |
|
185 | 185 | $data = file_get_contents($filePath); |
186 | 186 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
187 | - if(! $data){ |
|
187 | + if (!$data) { |
|
188 | 188 | $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
189 | 189 | return false; |
190 | 190 | } |
191 | - else{ |
|
191 | + else { |
|
192 | 192 | $this->logger->info('This cache data is OK check for expire'); |
193 | - if(isset($data['expire']) && $data['expire'] > time()){ |
|
193 | + if (isset($data['expire']) && $data['expire'] > time()) { |
|
194 | 194 | $this->logger->info('This cache not yet expired return cache informations'); |
195 | 195 | $info = array( |
196 | 196 | 'mtime' => $data['mtime'], |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | ); |
200 | 200 | return $info; |
201 | 201 | } |
202 | - else{ |
|
202 | + else { |
|
203 | 203 | $this->logger->info('This cache already expired return false'); |
204 | 204 | return false; |
205 | 205 | } |
@@ -211,26 +211,26 @@ discard block |
||
211 | 211 | /** |
212 | 212 | * Used to delete expired cache data |
213 | 213 | */ |
214 | - public function deleteExpiredCache(){ |
|
214 | + public function deleteExpiredCache() { |
|
215 | 215 | $this->logger->debug('Deleting of expired cache files'); |
216 | 216 | $list = glob(CACHE_PATH . '*.cache'); |
217 | - if(! $list){ |
|
217 | + if (!$list) { |
|
218 | 218 | $this->logger->info('No cache files were found skipping'); |
219 | 219 | } |
220 | - else{ |
|
220 | + else { |
|
221 | 221 | $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
222 | 222 | foreach ($list as $file) { |
223 | 223 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
224 | 224 | $data = file_get_contents($file); |
225 | 225 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
226 | - if(! $data){ |
|
226 | + if (!$data) { |
|
227 | 227 | $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
228 | 228 | } |
229 | - else if(time() > $data['expire']){ |
|
229 | + else if (time() > $data['expire']) { |
|
230 | 230 | $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
231 | 231 | unlink($file); |
232 | 232 | } |
233 | - else{ |
|
233 | + else { |
|
234 | 234 | $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
235 | 235 | } |
236 | 236 | } |
@@ -240,13 +240,13 @@ discard block |
||
240 | 240 | /** |
241 | 241 | * Remove all file from cache folder |
242 | 242 | */ |
243 | - public function clean(){ |
|
243 | + public function clean() { |
|
244 | 244 | $this->logger->debug('Deleting of all cache files'); |
245 | 245 | $list = glob(CACHE_PATH . '*.cache'); |
246 | - if(! $list){ |
|
246 | + if (!$list) { |
|
247 | 247 | $this->logger->info('No cache files were found skipping'); |
248 | 248 | } |
249 | - else{ |
|
249 | + else { |
|
250 | 250 | $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
251 | 251 | foreach ($list as $file) { |
252 | 252 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -258,7 +258,7 @@ discard block |
||
258 | 258 | /** |
259 | 259 | * @return boolean |
260 | 260 | */ |
261 | - public function isCompressCacheData(){ |
|
261 | + public function isCompressCacheData() { |
|
262 | 262 | return $this->compressCacheData; |
263 | 263 | } |
264 | 264 | |
@@ -267,14 +267,14 @@ discard block |
||
267 | 267 | * |
268 | 268 | * @return object |
269 | 269 | */ |
270 | - public function setCompressCacheData($status = true){ |
|
270 | + public function setCompressCacheData($status = true) { |
|
271 | 271 | //if Zlib extension is not loaded set compressCacheData to false |
272 | - if($status === true && ! extension_loaded('zlib')){ |
|
272 | + if ($status === true && !extension_loaded('zlib')) { |
|
273 | 273 | |
274 | 274 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
275 | 275 | $this->compressCacheData = false; |
276 | 276 | } |
277 | - else{ |
|
277 | + else { |
|
278 | 278 | $this->compressCacheData = $status; |
279 | 279 | } |
280 | 280 | return $this; |
@@ -285,7 +285,7 @@ discard block |
||
285 | 285 | * |
286 | 286 | * @return bool |
287 | 287 | */ |
288 | - public function isSupported(){ |
|
288 | + public function isSupported() { |
|
289 | 289 | return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
290 | 290 | } |
291 | 291 | |
@@ -293,7 +293,7 @@ discard block |
||
293 | 293 | * Return the Log instance |
294 | 294 | * @return object |
295 | 295 | */ |
296 | - public function getLogger(){ |
|
296 | + public function getLogger() { |
|
297 | 297 | return $this->logger; |
298 | 298 | } |
299 | 299 | |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | * Set the log instance |
302 | 302 | * @param Log $logger the log object |
303 | 303 | */ |
304 | - public function setLogger(Log $logger){ |
|
304 | + public function setLogger(Log $logger) { |
|
305 | 305 | $this->logger = $logger; |
306 | 306 | return $this; |
307 | 307 | } |
@@ -312,7 +312,7 @@ discard block |
||
312 | 312 | * @param string $key the cache item key |
313 | 313 | * @return string the full cache file path for this key |
314 | 314 | */ |
315 | - private function getFilePath($key){ |
|
315 | + private function getFilePath($key) { |
|
316 | 316 | return CACHE_PATH . md5($key) . '.cache'; |
317 | 317 | } |
318 | 318 | } |
@@ -1,393 +1,393 @@ discard block |
||
1 | 1 | <?php |
2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
3 | - /** |
|
4 | - * TNH Framework |
|
5 | - * |
|
6 | - * A simple PHP framework using HMVC architecture |
|
7 | - * |
|
8 | - * This content is released under the GNU GPL License (GPL) |
|
9 | - * |
|
10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
11 | - * |
|
12 | - * This program is free software; you can redistribute it and/or |
|
13 | - * modify it under the terms of the GNU General Public License |
|
14 | - * as published by the Free Software Foundation; either version 3 |
|
15 | - * of the License, or (at your option) any later version. |
|
16 | - * |
|
17 | - * This program is distributed in the hope that it will be useful, |
|
18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | - * GNU General Public License for more details. |
|
21 | - * |
|
22 | - * You should have received a copy of the GNU General Public License |
|
23 | - * along with this program; if not, write to the Free Software |
|
24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | - */ |
|
2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
3 | + /** |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | 26 | |
27 | - class Router { |
|
27 | + class Router { |
|
28 | 28 | |
29 | - /** |
|
30 | - * @var array $pattern: The list of URIs to validate against |
|
31 | - */ |
|
32 | - private $pattern = array(); |
|
29 | + /** |
|
30 | + * @var array $pattern: The list of URIs to validate against |
|
31 | + */ |
|
32 | + private $pattern = array(); |
|
33 | 33 | |
34 | - /** |
|
35 | - * @var array $callback: The list of callback to call |
|
36 | - */ |
|
37 | - private $callback = array(); |
|
34 | + /** |
|
35 | + * @var array $callback: The list of callback to call |
|
36 | + */ |
|
37 | + private $callback = array(); |
|
38 | 38 | |
39 | - /** |
|
40 | - * @var string $uriTrim: The char to remove from the URIs |
|
41 | - */ |
|
42 | - protected $uriTrim = '/\^$'; |
|
39 | + /** |
|
40 | + * @var string $uriTrim: The char to remove from the URIs |
|
41 | + */ |
|
42 | + protected $uriTrim = '/\^$'; |
|
43 | 43 | |
44 | - /** |
|
45 | - * The module name of the current request |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - protected $module = null; |
|
44 | + /** |
|
45 | + * The module name of the current request |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + protected $module = null; |
|
49 | 49 | |
50 | - /** |
|
51 | - * The controller name of the current request |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - protected $controller = null; |
|
50 | + /** |
|
51 | + * The controller name of the current request |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + protected $controller = null; |
|
55 | 55 | |
56 | - /** |
|
57 | - * The controller path |
|
58 | - * @var string |
|
59 | - */ |
|
60 | - protected $controllerPath = null; |
|
56 | + /** |
|
57 | + * The controller path |
|
58 | + * @var string |
|
59 | + */ |
|
60 | + protected $controllerPath = null; |
|
61 | 61 | |
62 | - /** |
|
63 | - * The method name. The default value is "index" |
|
64 | - * @var string |
|
65 | - */ |
|
66 | - protected $method = 'index'; |
|
62 | + /** |
|
63 | + * The method name. The default value is "index" |
|
64 | + * @var string |
|
65 | + */ |
|
66 | + protected $method = 'index'; |
|
67 | 67 | |
68 | - /** |
|
69 | - * List of argument to pass to the method |
|
70 | - * @var array |
|
71 | - */ |
|
72 | - protected $args = array(); |
|
68 | + /** |
|
69 | + * List of argument to pass to the method |
|
70 | + * @var array |
|
71 | + */ |
|
72 | + protected $args = array(); |
|
73 | 73 | |
74 | - /** |
|
75 | - * List of routes configurations |
|
76 | - * @var array |
|
77 | - */ |
|
78 | - protected $routes = array(); |
|
74 | + /** |
|
75 | + * List of routes configurations |
|
76 | + * @var array |
|
77 | + */ |
|
78 | + protected $routes = array(); |
|
79 | 79 | |
80 | - /** |
|
81 | - * The segments array for the current request |
|
82 | - * @var array |
|
83 | - */ |
|
84 | - protected $segments; |
|
80 | + /** |
|
81 | + * The segments array for the current request |
|
82 | + * @var array |
|
83 | + */ |
|
84 | + protected $segments; |
|
85 | 85 | |
86 | - /** |
|
87 | - * The logger instance |
|
88 | - * @var Log |
|
89 | - */ |
|
90 | - private $logger; |
|
86 | + /** |
|
87 | + * The logger instance |
|
88 | + * @var Log |
|
89 | + */ |
|
90 | + private $logger; |
|
91 | 91 | |
92 | - /** |
|
93 | - * Construct the new Router instance |
|
94 | - */ |
|
95 | - public function __construct(){ |
|
96 | - $this->setLoggerFromParamOrCreateNewInstance(null); |
|
92 | + /** |
|
93 | + * Construct the new Router instance |
|
94 | + */ |
|
95 | + public function __construct(){ |
|
96 | + $this->setLoggerFromParamOrCreateNewInstance(null); |
|
97 | 97 | |
98 | - //loading routes for module |
|
99 | - $moduleRouteList = array(); |
|
100 | - $modulesRoutes = Module::getModulesRoutes(); |
|
101 | - if($modulesRoutes && is_array($modulesRoutes)){ |
|
102 | - $moduleRouteList = $modulesRoutes; |
|
103 | - unset($modulesRoutes); |
|
104 | - } |
|
105 | - $this->setRouteConfiguration($moduleRouteList); |
|
106 | - $this->logger->info('The routes configuration are listed below: ' . stringfy_vars($this->routes)); |
|
98 | + //loading routes for module |
|
99 | + $moduleRouteList = array(); |
|
100 | + $modulesRoutes = Module::getModulesRoutes(); |
|
101 | + if($modulesRoutes && is_array($modulesRoutes)){ |
|
102 | + $moduleRouteList = $modulesRoutes; |
|
103 | + unset($modulesRoutes); |
|
104 | + } |
|
105 | + $this->setRouteConfiguration($moduleRouteList); |
|
106 | + $this->logger->info('The routes configuration are listed below: ' . stringfy_vars($this->routes)); |
|
107 | 107 | |
108 | - //Set route parameters |
|
109 | - $this->setRouteParams(); |
|
110 | - } |
|
108 | + //Set route parameters |
|
109 | + $this->setRouteParams(); |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * Add the URI and callback to the list of URIs to validate |
|
114 | - * |
|
115 | - * @param string $uri the request URI |
|
116 | - * @param object $callback the callback function |
|
117 | - */ |
|
118 | - public function add($uri, $callback) { |
|
119 | - $uri = trim($uri, $this->uriTrim); |
|
120 | - if(in_array($uri, $this->pattern)){ |
|
121 | - $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
|
122 | - } |
|
123 | - $this->pattern[] = $uri; |
|
124 | - $this->callback[] = $callback; |
|
125 | - } |
|
112 | + /** |
|
113 | + * Add the URI and callback to the list of URIs to validate |
|
114 | + * |
|
115 | + * @param string $uri the request URI |
|
116 | + * @param object $callback the callback function |
|
117 | + */ |
|
118 | + public function add($uri, $callback) { |
|
119 | + $uri = trim($uri, $this->uriTrim); |
|
120 | + if(in_array($uri, $this->pattern)){ |
|
121 | + $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
|
122 | + } |
|
123 | + $this->pattern[] = $uri; |
|
124 | + $this->callback[] = $callback; |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * Get the module name |
|
129 | - * @return string |
|
130 | - */ |
|
131 | - public function getModule(){ |
|
132 | - return $this->module; |
|
133 | - } |
|
127 | + /** |
|
128 | + * Get the module name |
|
129 | + * @return string |
|
130 | + */ |
|
131 | + public function getModule(){ |
|
132 | + return $this->module; |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * Get the controller name |
|
137 | - * @return string |
|
138 | - */ |
|
139 | - public function getController(){ |
|
140 | - return $this->controller; |
|
141 | - } |
|
135 | + /** |
|
136 | + * Get the controller name |
|
137 | + * @return string |
|
138 | + */ |
|
139 | + public function getController(){ |
|
140 | + return $this->controller; |
|
141 | + } |
|
142 | 142 | |
143 | - /** |
|
144 | - * Get the controller file path |
|
145 | - * @return string |
|
146 | - */ |
|
147 | - public function getControllerPath(){ |
|
148 | - return $this->controllerPath; |
|
149 | - } |
|
143 | + /** |
|
144 | + * Get the controller file path |
|
145 | + * @return string |
|
146 | + */ |
|
147 | + public function getControllerPath(){ |
|
148 | + return $this->controllerPath; |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * Get the controller method |
|
153 | - * @return string |
|
154 | - */ |
|
155 | - public function getMethod(){ |
|
156 | - return $this->method; |
|
157 | - } |
|
151 | + /** |
|
152 | + * Get the controller method |
|
153 | + * @return string |
|
154 | + */ |
|
155 | + public function getMethod(){ |
|
156 | + return $this->method; |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * Get the request arguments |
|
161 | - * @return array |
|
162 | - */ |
|
163 | - public function getArgs(){ |
|
164 | - return $this->args; |
|
165 | - } |
|
159 | + /** |
|
160 | + * Get the request arguments |
|
161 | + * @return array |
|
162 | + */ |
|
163 | + public function getArgs(){ |
|
164 | + return $this->args; |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * Get the URL segments array |
|
169 | - * @return array |
|
170 | - */ |
|
171 | - public function getSegments(){ |
|
172 | - return $this->segments; |
|
173 | - } |
|
167 | + /** |
|
168 | + * Get the URL segments array |
|
169 | + * @return array |
|
170 | + */ |
|
171 | + public function getSegments(){ |
|
172 | + return $this->segments; |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * Routing the request to the correspondant module/controller/method if exists |
|
177 | - * otherwise send 404 error. |
|
178 | - */ |
|
179 | - public function run() { |
|
180 | - $benchmark =& class_loader('Benchmark'); |
|
181 | - $benchmark->mark('ROUTING_PROCESS_START'); |
|
182 | - $this->logger->debug('Routing process start ...'); |
|
183 | - $segment = $this->segments; |
|
184 | - $baseUrl = get_config('base_url'); |
|
185 | - //check if the app is not in DOCUMENT_ROOT |
|
186 | - if(isset($segment[0]) && stripos($baseUrl, $segment[0]) != false){ |
|
187 | - array_shift($segment); |
|
188 | - $this->segments = $segment; |
|
189 | - } |
|
190 | - $this->logger->debug('Check if the request URI contains the front controller'); |
|
191 | - if(isset($segment[0]) && $segment[0] == SELF){ |
|
192 | - $this->logger->info('The request URI contains the front controller'); |
|
193 | - array_shift($segment); |
|
194 | - $this->segments = $segment; |
|
195 | - } |
|
196 | - else{ |
|
197 | - $this->logger->info('The request URI does not contain the front controller'); |
|
198 | - } |
|
199 | - $uri = implode('/', $segment); |
|
200 | - $this->logger->info('The final Request URI is [' . $uri . ']' ); |
|
201 | - //generic routes |
|
202 | - $pattern = array(':num', ':alpha', ':alnum', ':any'); |
|
203 | - $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
|
204 | - $this->logger->debug('Begin to loop in the predefined routes configuration to check if the current request match'); |
|
205 | - // Cycle through the URIs stored in the array |
|
206 | - foreach ($this->pattern as $index => $uriList) { |
|
207 | - $uriList = str_ireplace($pattern, $replace, $uriList); |
|
208 | - // Check for an existant matching URI |
|
209 | - if (preg_match("#^$uriList$#", $uri, $args)) { |
|
210 | - $this->logger->info('Route found for request URI [' . $uri . '] using the predefined configuration [' . $this->pattern[$index] . '] --> [' . $this->callback[$index] . ']'); |
|
211 | - array_shift($args); |
|
212 | - //check if this contains an module |
|
213 | - $moduleControllerMethod = explode('#', $this->callback[$index]); |
|
214 | - if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
215 | - $this->logger->info('The current request use the module [' .$moduleControllerMethod[0]. ']'); |
|
216 | - $this->module = $moduleControllerMethod[0]; |
|
217 | - $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
|
218 | - } |
|
219 | - else{ |
|
220 | - $this->logger->info('The current request does not use the module'); |
|
221 | - $moduleControllerMethod = explode('@', $this->callback[$index]); |
|
222 | - } |
|
223 | - if(is_array($moduleControllerMethod)){ |
|
224 | - if(isset($moduleControllerMethod[0])){ |
|
225 | - $this->controller = $moduleControllerMethod[0]; |
|
226 | - } |
|
227 | - if(isset($moduleControllerMethod[1])){ |
|
228 | - $this->method = $moduleControllerMethod[1]; |
|
229 | - } |
|
230 | - $this->args = $args; |
|
231 | - } |
|
232 | - // stop here |
|
233 | - break; |
|
234 | - } |
|
235 | - } |
|
236 | - //first if the controller is not set and the module is set use the module name as the controller |
|
237 | - if(! $this->getController() && $this->getModule()){ |
|
238 | - $this->logger->info('After loop in predefined routes configuration, the module name is set but the controller is not set, so we will use module as the controller'); |
|
239 | - $this->controller = $this->getModule(); |
|
240 | - } |
|
241 | - //if can not determine the module/controller/method via the defined routes configuration we will use |
|
242 | - //the URL like http://domain.com/module/controller/method/arg1/arg2 |
|
243 | - if(! $this->getController()){ |
|
244 | - $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
|
245 | - $nbSegment = count($segment); |
|
246 | - //if segment is null so means no need to perform |
|
247 | - if($nbSegment > 0){ |
|
248 | - //get the module list |
|
249 | - $modules = Module::getModuleList(); |
|
250 | - //first check if no module |
|
251 | - if(! $modules){ |
|
252 | - $this->logger->info('No module was loaded will skip the module checking'); |
|
253 | - //the application don't use module |
|
254 | - //controller |
|
255 | - if(isset($segment[0])){ |
|
256 | - $this->controller = $segment[0]; |
|
257 | - array_shift($segment); |
|
258 | - } |
|
259 | - //method |
|
260 | - if(isset($segment[0])){ |
|
261 | - $this->method = $segment[0]; |
|
262 | - array_shift($segment); |
|
263 | - } |
|
264 | - //args |
|
265 | - $this->args = $segment; |
|
266 | - } |
|
267 | - else{ |
|
268 | - $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
|
269 | - if(in_array($segment[0], $modules)){ |
|
270 | - $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
|
271 | - $this->module = $segment[0]; |
|
272 | - array_shift($segment); |
|
273 | - //check if the second arg is the controller from module |
|
274 | - if(isset($segment[0])){ |
|
275 | - $this->controller = $segment[0]; |
|
276 | - //check if the request use the same module name and controller |
|
277 | - $path = Module::findControllerFullPath(ucfirst($this->getController()), $this->getModule()); |
|
278 | - if(! $path){ |
|
279 | - $this->logger->info('The controller [' . $this->getController() . '] not found in the module, may be will use the module [' . $this->getModule() . '] as controller'); |
|
280 | - $this->controller = $this->getModule(); |
|
281 | - } |
|
282 | - else{ |
|
283 | - $this->controllerPath = $path; |
|
284 | - array_shift($segment); |
|
285 | - } |
|
286 | - } |
|
287 | - //check for method |
|
288 | - if(isset($segment[0])){ |
|
289 | - $this->method = $segment[0]; |
|
290 | - array_shift($segment); |
|
291 | - } |
|
292 | - //the remaining is for args |
|
293 | - $this->args = $segment; |
|
294 | - } |
|
295 | - else{ |
|
296 | - $this->logger->info('The current request information is not found in the module list'); |
|
297 | - //controller |
|
298 | - if(isset($segment[0])){ |
|
299 | - $this->controller = $segment[0]; |
|
300 | - array_shift($segment); |
|
301 | - } |
|
302 | - //method |
|
303 | - if(isset($segment[0])){ |
|
304 | - $this->method = $segment[0]; |
|
305 | - array_shift($segment); |
|
306 | - } |
|
307 | - //args |
|
308 | - $this->args = $segment; |
|
309 | - } |
|
310 | - } |
|
311 | - } |
|
312 | - } |
|
313 | - if(! $this->getController() && $this->getModule()){ |
|
314 | - $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
|
315 | - $this->controller = $this->getModule(); |
|
316 | - } |
|
317 | - //did we set the controller, so set the controller path |
|
318 | - if($this->getController() && ! $this->getControllerPath()){ |
|
319 | - $this->logger->debug('Setting the file path for the controller [' . $this->getController() . ']'); |
|
320 | - //if it is the module controller |
|
321 | - if($this->getModule()){ |
|
322 | - $this->controllerPath = Module::findControllerFullPath(ucfirst($this->getController()), $this->getModule()); |
|
323 | - } |
|
324 | - else{ |
|
325 | - $this->controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->getController()) . '.php'; |
|
326 | - } |
|
327 | - } |
|
328 | - $controller = ucfirst($this->getController()); |
|
329 | - $this->logger->info('The routing information are: module [' . $this->getModule() . '], controller [' . $controller . '], method [' . $this->getMethod() . '], args [' . stringfy_vars($this->args) . ']'); |
|
330 | - $classFilePath = $this->getControllerPath(); |
|
331 | - $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
|
332 | - $benchmark->mark('ROUTING_PROCESS_END'); |
|
333 | - $e404 = false; |
|
334 | - if(file_exists($classFilePath)){ |
|
335 | - require_once $classFilePath; |
|
336 | - if(! class_exists($controller, false)){ |
|
337 | - $e404 = true; |
|
338 | - $this->logger->info('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
339 | - } |
|
340 | - else{ |
|
341 | - $controllerInstance = new $controller(); |
|
342 | - $controllerMethod = $this->getMethod(); |
|
343 | - if(! method_exists($controllerInstance, $controllerMethod)){ |
|
344 | - $e404 = true; |
|
345 | - $this->logger->info('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
|
346 | - } |
|
347 | - else{ |
|
348 | - $this->logger->info('Routing data is set correctly now GO!'); |
|
349 | - call_user_func_array(array($controllerInstance, $controllerMethod), $this->getArgs()); |
|
350 | - $obj = & get_instance(); |
|
351 | - //render the final page to user |
|
352 | - $this->logger->info('Render the final output to the browser'); |
|
353 | - $obj->response->renderFinalPage(); |
|
354 | - } |
|
355 | - } |
|
356 | - } |
|
357 | - else{ |
|
358 | - $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
|
359 | - $e404 = true; |
|
360 | - } |
|
361 | - if($e404){ |
|
362 | - $response =& class_loader('Response', 'classes'); |
|
363 | - $response->send404(); |
|
364 | - } |
|
365 | - } |
|
175 | + /** |
|
176 | + * Routing the request to the correspondant module/controller/method if exists |
|
177 | + * otherwise send 404 error. |
|
178 | + */ |
|
179 | + public function run() { |
|
180 | + $benchmark =& class_loader('Benchmark'); |
|
181 | + $benchmark->mark('ROUTING_PROCESS_START'); |
|
182 | + $this->logger->debug('Routing process start ...'); |
|
183 | + $segment = $this->segments; |
|
184 | + $baseUrl = get_config('base_url'); |
|
185 | + //check if the app is not in DOCUMENT_ROOT |
|
186 | + if(isset($segment[0]) && stripos($baseUrl, $segment[0]) != false){ |
|
187 | + array_shift($segment); |
|
188 | + $this->segments = $segment; |
|
189 | + } |
|
190 | + $this->logger->debug('Check if the request URI contains the front controller'); |
|
191 | + if(isset($segment[0]) && $segment[0] == SELF){ |
|
192 | + $this->logger->info('The request URI contains the front controller'); |
|
193 | + array_shift($segment); |
|
194 | + $this->segments = $segment; |
|
195 | + } |
|
196 | + else{ |
|
197 | + $this->logger->info('The request URI does not contain the front controller'); |
|
198 | + } |
|
199 | + $uri = implode('/', $segment); |
|
200 | + $this->logger->info('The final Request URI is [' . $uri . ']' ); |
|
201 | + //generic routes |
|
202 | + $pattern = array(':num', ':alpha', ':alnum', ':any'); |
|
203 | + $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
|
204 | + $this->logger->debug('Begin to loop in the predefined routes configuration to check if the current request match'); |
|
205 | + // Cycle through the URIs stored in the array |
|
206 | + foreach ($this->pattern as $index => $uriList) { |
|
207 | + $uriList = str_ireplace($pattern, $replace, $uriList); |
|
208 | + // Check for an existant matching URI |
|
209 | + if (preg_match("#^$uriList$#", $uri, $args)) { |
|
210 | + $this->logger->info('Route found for request URI [' . $uri . '] using the predefined configuration [' . $this->pattern[$index] . '] --> [' . $this->callback[$index] . ']'); |
|
211 | + array_shift($args); |
|
212 | + //check if this contains an module |
|
213 | + $moduleControllerMethod = explode('#', $this->callback[$index]); |
|
214 | + if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
215 | + $this->logger->info('The current request use the module [' .$moduleControllerMethod[0]. ']'); |
|
216 | + $this->module = $moduleControllerMethod[0]; |
|
217 | + $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
|
218 | + } |
|
219 | + else{ |
|
220 | + $this->logger->info('The current request does not use the module'); |
|
221 | + $moduleControllerMethod = explode('@', $this->callback[$index]); |
|
222 | + } |
|
223 | + if(is_array($moduleControllerMethod)){ |
|
224 | + if(isset($moduleControllerMethod[0])){ |
|
225 | + $this->controller = $moduleControllerMethod[0]; |
|
226 | + } |
|
227 | + if(isset($moduleControllerMethod[1])){ |
|
228 | + $this->method = $moduleControllerMethod[1]; |
|
229 | + } |
|
230 | + $this->args = $args; |
|
231 | + } |
|
232 | + // stop here |
|
233 | + break; |
|
234 | + } |
|
235 | + } |
|
236 | + //first if the controller is not set and the module is set use the module name as the controller |
|
237 | + if(! $this->getController() && $this->getModule()){ |
|
238 | + $this->logger->info('After loop in predefined routes configuration, the module name is set but the controller is not set, so we will use module as the controller'); |
|
239 | + $this->controller = $this->getModule(); |
|
240 | + } |
|
241 | + //if can not determine the module/controller/method via the defined routes configuration we will use |
|
242 | + //the URL like http://domain.com/module/controller/method/arg1/arg2 |
|
243 | + if(! $this->getController()){ |
|
244 | + $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
|
245 | + $nbSegment = count($segment); |
|
246 | + //if segment is null so means no need to perform |
|
247 | + if($nbSegment > 0){ |
|
248 | + //get the module list |
|
249 | + $modules = Module::getModuleList(); |
|
250 | + //first check if no module |
|
251 | + if(! $modules){ |
|
252 | + $this->logger->info('No module was loaded will skip the module checking'); |
|
253 | + //the application don't use module |
|
254 | + //controller |
|
255 | + if(isset($segment[0])){ |
|
256 | + $this->controller = $segment[0]; |
|
257 | + array_shift($segment); |
|
258 | + } |
|
259 | + //method |
|
260 | + if(isset($segment[0])){ |
|
261 | + $this->method = $segment[0]; |
|
262 | + array_shift($segment); |
|
263 | + } |
|
264 | + //args |
|
265 | + $this->args = $segment; |
|
266 | + } |
|
267 | + else{ |
|
268 | + $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
|
269 | + if(in_array($segment[0], $modules)){ |
|
270 | + $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
|
271 | + $this->module = $segment[0]; |
|
272 | + array_shift($segment); |
|
273 | + //check if the second arg is the controller from module |
|
274 | + if(isset($segment[0])){ |
|
275 | + $this->controller = $segment[0]; |
|
276 | + //check if the request use the same module name and controller |
|
277 | + $path = Module::findControllerFullPath(ucfirst($this->getController()), $this->getModule()); |
|
278 | + if(! $path){ |
|
279 | + $this->logger->info('The controller [' . $this->getController() . '] not found in the module, may be will use the module [' . $this->getModule() . '] as controller'); |
|
280 | + $this->controller = $this->getModule(); |
|
281 | + } |
|
282 | + else{ |
|
283 | + $this->controllerPath = $path; |
|
284 | + array_shift($segment); |
|
285 | + } |
|
286 | + } |
|
287 | + //check for method |
|
288 | + if(isset($segment[0])){ |
|
289 | + $this->method = $segment[0]; |
|
290 | + array_shift($segment); |
|
291 | + } |
|
292 | + //the remaining is for args |
|
293 | + $this->args = $segment; |
|
294 | + } |
|
295 | + else{ |
|
296 | + $this->logger->info('The current request information is not found in the module list'); |
|
297 | + //controller |
|
298 | + if(isset($segment[0])){ |
|
299 | + $this->controller = $segment[0]; |
|
300 | + array_shift($segment); |
|
301 | + } |
|
302 | + //method |
|
303 | + if(isset($segment[0])){ |
|
304 | + $this->method = $segment[0]; |
|
305 | + array_shift($segment); |
|
306 | + } |
|
307 | + //args |
|
308 | + $this->args = $segment; |
|
309 | + } |
|
310 | + } |
|
311 | + } |
|
312 | + } |
|
313 | + if(! $this->getController() && $this->getModule()){ |
|
314 | + $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
|
315 | + $this->controller = $this->getModule(); |
|
316 | + } |
|
317 | + //did we set the controller, so set the controller path |
|
318 | + if($this->getController() && ! $this->getControllerPath()){ |
|
319 | + $this->logger->debug('Setting the file path for the controller [' . $this->getController() . ']'); |
|
320 | + //if it is the module controller |
|
321 | + if($this->getModule()){ |
|
322 | + $this->controllerPath = Module::findControllerFullPath(ucfirst($this->getController()), $this->getModule()); |
|
323 | + } |
|
324 | + else{ |
|
325 | + $this->controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->getController()) . '.php'; |
|
326 | + } |
|
327 | + } |
|
328 | + $controller = ucfirst($this->getController()); |
|
329 | + $this->logger->info('The routing information are: module [' . $this->getModule() . '], controller [' . $controller . '], method [' . $this->getMethod() . '], args [' . stringfy_vars($this->args) . ']'); |
|
330 | + $classFilePath = $this->getControllerPath(); |
|
331 | + $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
|
332 | + $benchmark->mark('ROUTING_PROCESS_END'); |
|
333 | + $e404 = false; |
|
334 | + if(file_exists($classFilePath)){ |
|
335 | + require_once $classFilePath; |
|
336 | + if(! class_exists($controller, false)){ |
|
337 | + $e404 = true; |
|
338 | + $this->logger->info('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
339 | + } |
|
340 | + else{ |
|
341 | + $controllerInstance = new $controller(); |
|
342 | + $controllerMethod = $this->getMethod(); |
|
343 | + if(! method_exists($controllerInstance, $controllerMethod)){ |
|
344 | + $e404 = true; |
|
345 | + $this->logger->info('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
|
346 | + } |
|
347 | + else{ |
|
348 | + $this->logger->info('Routing data is set correctly now GO!'); |
|
349 | + call_user_func_array(array($controllerInstance, $controllerMethod), $this->getArgs()); |
|
350 | + $obj = & get_instance(); |
|
351 | + //render the final page to user |
|
352 | + $this->logger->info('Render the final output to the browser'); |
|
353 | + $obj->response->renderFinalPage(); |
|
354 | + } |
|
355 | + } |
|
356 | + } |
|
357 | + else{ |
|
358 | + $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
|
359 | + $e404 = true; |
|
360 | + } |
|
361 | + if($e404){ |
|
362 | + $response =& class_loader('Response', 'classes'); |
|
363 | + $response->send404(); |
|
364 | + } |
|
365 | + } |
|
366 | 366 | |
367 | - /** |
|
367 | + /** |
|
368 | 368 | * Return the Log instance |
369 | 369 | * @return Log |
370 | 370 | */ |
371 | 371 | public function getLogger(){ |
372 | - return $this->logger; |
|
372 | + return $this->logger; |
|
373 | 373 | } |
374 | 374 | |
375 | 375 | /** |
376 | 376 | * Set the log instance |
377 | 377 | * @param Log $logger the log object |
378 | - * @return object |
|
378 | + * @return object |
|
379 | 379 | */ |
380 | 380 | public function setLogger($logger){ |
381 | - $this->logger = $logger; |
|
382 | - return $this; |
|
381 | + $this->logger = $logger; |
|
382 | + return $this; |
|
383 | 383 | } |
384 | 384 | |
385 | 385 | /** |
386 | - * Setting the route configuration using the configuration file and additional configuration from param |
|
387 | - * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
388 | - * @param boolean $useConfigFile whether to use route configuration file |
|
389 | - * @return object |
|
390 | - */ |
|
386 | + * Setting the route configuration using the configuration file and additional configuration from param |
|
387 | + * @param array $overwriteConfig the additional configuration to overwrite with the existing one |
|
388 | + * @param boolean $useConfigFile whether to use route configuration file |
|
389 | + * @return object |
|
390 | + */ |
|
391 | 391 | public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
392 | 392 | $route = array(); |
393 | 393 | if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')){ |
@@ -395,45 +395,45 @@ discard block |
||
395 | 395 | } |
396 | 396 | $route = array_merge($route, $overwriteConfig); |
397 | 397 | $this->routes = $route; |
398 | - return $this; |
|
398 | + return $this; |
|
399 | 399 | } |
400 | 400 | |
401 | 401 | /** |
402 | 402 | * Set the route paramaters using the configuration |
403 | 403 | */ |
404 | 404 | protected function setRouteParams(){ |
405 | - //adding route |
|
406 | - foreach($this->routes as $pattern => $callback){ |
|
407 | - $this->add($pattern, $callback); |
|
408 | - } |
|
405 | + //adding route |
|
406 | + foreach($this->routes as $pattern => $callback){ |
|
407 | + $this->add($pattern, $callback); |
|
408 | + } |
|
409 | 409 | |
410 | - //here use directly the variable $_SERVER |
|
411 | - $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; |
|
412 | - $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
|
413 | - //remove url suffix from the request URI |
|
414 | - $suffix = get_config('url_suffix'); |
|
415 | - if ($suffix) { |
|
416 | - $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
417 | - $uri = str_ireplace($suffix, '', $uri); |
|
418 | - } |
|
419 | - if (strpos($uri, '?') !== false){ |
|
420 | - $uri = substr($uri, 0, strpos($uri, '?')); |
|
421 | - } |
|
422 | - $uri = trim($uri, $this->uriTrim); |
|
423 | - $this->segments = explode('/', $uri); |
|
424 | - } |
|
410 | + //here use directly the variable $_SERVER |
|
411 | + $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; |
|
412 | + $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
|
413 | + //remove url suffix from the request URI |
|
414 | + $suffix = get_config('url_suffix'); |
|
415 | + if ($suffix) { |
|
416 | + $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
417 | + $uri = str_ireplace($suffix, '', $uri); |
|
418 | + } |
|
419 | + if (strpos($uri, '?') !== false){ |
|
420 | + $uri = substr($uri, 0, strpos($uri, '?')); |
|
421 | + } |
|
422 | + $uri = trim($uri, $this->uriTrim); |
|
423 | + $this->segments = explode('/', $uri); |
|
424 | + } |
|
425 | 425 | |
426 | - /** |
|
426 | + /** |
|
427 | 427 | * Set the Log instance using argument or create new instance |
428 | 428 | * @param object $logger the Log instance if not null |
429 | 429 | */ |
430 | 430 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
431 | - if ($logger !== null){ |
|
431 | + if ($logger !== null){ |
|
432 | 432 | $this->logger = $logger; |
433 | - } |
|
434 | - else{ |
|
435 | - $this->logger =& class_loader('Log', 'classes'); |
|
436 | - $this->logger->setLogger('Library::Router'); |
|
437 | - } |
|
433 | + } |
|
434 | + else{ |
|
435 | + $this->logger =& class_loader('Log', 'classes'); |
|
436 | + $this->logger->setLogger('Library::Router'); |
|
437 | + } |
|
438 | 438 | } |
439 | 439 | } |
@@ -92,13 +92,13 @@ discard block |
||
92 | 92 | /** |
93 | 93 | * Construct the new Router instance |
94 | 94 | */ |
95 | - public function __construct(){ |
|
95 | + public function __construct() { |
|
96 | 96 | $this->setLoggerFromParamOrCreateNewInstance(null); |
97 | 97 | |
98 | 98 | //loading routes for module |
99 | 99 | $moduleRouteList = array(); |
100 | 100 | $modulesRoutes = Module::getModulesRoutes(); |
101 | - if($modulesRoutes && is_array($modulesRoutes)){ |
|
101 | + if ($modulesRoutes && is_array($modulesRoutes)) { |
|
102 | 102 | $moduleRouteList = $modulesRoutes; |
103 | 103 | unset($modulesRoutes); |
104 | 104 | } |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | */ |
118 | 118 | public function add($uri, $callback) { |
119 | 119 | $uri = trim($uri, $this->uriTrim); |
120 | - if(in_array($uri, $this->pattern)){ |
|
120 | + if (in_array($uri, $this->pattern)) { |
|
121 | 121 | $this->logger->warning('The route [' . $uri . '] already added, may be adding again can have route conflict'); |
122 | 122 | } |
123 | 123 | $this->pattern[] = $uri; |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | * Get the module name |
129 | 129 | * @return string |
130 | 130 | */ |
131 | - public function getModule(){ |
|
131 | + public function getModule() { |
|
132 | 132 | return $this->module; |
133 | 133 | } |
134 | 134 | |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | * Get the controller name |
137 | 137 | * @return string |
138 | 138 | */ |
139 | - public function getController(){ |
|
139 | + public function getController() { |
|
140 | 140 | return $this->controller; |
141 | 141 | } |
142 | 142 | |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | * Get the controller file path |
145 | 145 | * @return string |
146 | 146 | */ |
147 | - public function getControllerPath(){ |
|
147 | + public function getControllerPath() { |
|
148 | 148 | return $this->controllerPath; |
149 | 149 | } |
150 | 150 | |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | * Get the controller method |
153 | 153 | * @return string |
154 | 154 | */ |
155 | - public function getMethod(){ |
|
155 | + public function getMethod() { |
|
156 | 156 | return $this->method; |
157 | 157 | } |
158 | 158 | |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | * Get the request arguments |
161 | 161 | * @return array |
162 | 162 | */ |
163 | - public function getArgs(){ |
|
163 | + public function getArgs() { |
|
164 | 164 | return $this->args; |
165 | 165 | } |
166 | 166 | |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | * Get the URL segments array |
169 | 169 | * @return array |
170 | 170 | */ |
171 | - public function getSegments(){ |
|
171 | + public function getSegments() { |
|
172 | 172 | return $this->segments; |
173 | 173 | } |
174 | 174 | |
@@ -177,27 +177,27 @@ discard block |
||
177 | 177 | * otherwise send 404 error. |
178 | 178 | */ |
179 | 179 | public function run() { |
180 | - $benchmark =& class_loader('Benchmark'); |
|
180 | + $benchmark = & class_loader('Benchmark'); |
|
181 | 181 | $benchmark->mark('ROUTING_PROCESS_START'); |
182 | 182 | $this->logger->debug('Routing process start ...'); |
183 | 183 | $segment = $this->segments; |
184 | 184 | $baseUrl = get_config('base_url'); |
185 | 185 | //check if the app is not in DOCUMENT_ROOT |
186 | - if(isset($segment[0]) && stripos($baseUrl, $segment[0]) != false){ |
|
186 | + if (isset($segment[0]) && stripos($baseUrl, $segment[0]) != false) { |
|
187 | 187 | array_shift($segment); |
188 | 188 | $this->segments = $segment; |
189 | 189 | } |
190 | 190 | $this->logger->debug('Check if the request URI contains the front controller'); |
191 | - if(isset($segment[0]) && $segment[0] == SELF){ |
|
191 | + if (isset($segment[0]) && $segment[0] == SELF) { |
|
192 | 192 | $this->logger->info('The request URI contains the front controller'); |
193 | 193 | array_shift($segment); |
194 | 194 | $this->segments = $segment; |
195 | 195 | } |
196 | - else{ |
|
196 | + else { |
|
197 | 197 | $this->logger->info('The request URI does not contain the front controller'); |
198 | 198 | } |
199 | 199 | $uri = implode('/', $segment); |
200 | - $this->logger->info('The final Request URI is [' . $uri . ']' ); |
|
200 | + $this->logger->info('The final Request URI is [' . $uri . ']'); |
|
201 | 201 | //generic routes |
202 | 202 | $pattern = array(':num', ':alpha', ':alnum', ':any'); |
203 | 203 | $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
@@ -211,20 +211,20 @@ discard block |
||
211 | 211 | array_shift($args); |
212 | 212 | //check if this contains an module |
213 | 213 | $moduleControllerMethod = explode('#', $this->callback[$index]); |
214 | - if(is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2){ |
|
215 | - $this->logger->info('The current request use the module [' .$moduleControllerMethod[0]. ']'); |
|
214 | + if (is_array($moduleControllerMethod) && count($moduleControllerMethod) >= 2) { |
|
215 | + $this->logger->info('The current request use the module [' . $moduleControllerMethod[0] . ']'); |
|
216 | 216 | $this->module = $moduleControllerMethod[0]; |
217 | 217 | $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
218 | 218 | } |
219 | - else{ |
|
219 | + else { |
|
220 | 220 | $this->logger->info('The current request does not use the module'); |
221 | 221 | $moduleControllerMethod = explode('@', $this->callback[$index]); |
222 | 222 | } |
223 | - if(is_array($moduleControllerMethod)){ |
|
224 | - if(isset($moduleControllerMethod[0])){ |
|
223 | + if (is_array($moduleControllerMethod)) { |
|
224 | + if (isset($moduleControllerMethod[0])) { |
|
225 | 225 | $this->controller = $moduleControllerMethod[0]; |
226 | 226 | } |
227 | - if(isset($moduleControllerMethod[1])){ |
|
227 | + if (isset($moduleControllerMethod[1])) { |
|
228 | 228 | $this->method = $moduleControllerMethod[1]; |
229 | 229 | } |
230 | 230 | $this->args = $args; |
@@ -234,73 +234,73 @@ discard block |
||
234 | 234 | } |
235 | 235 | } |
236 | 236 | //first if the controller is not set and the module is set use the module name as the controller |
237 | - if(! $this->getController() && $this->getModule()){ |
|
237 | + if (!$this->getController() && $this->getModule()) { |
|
238 | 238 | $this->logger->info('After loop in predefined routes configuration, the module name is set but the controller is not set, so we will use module as the controller'); |
239 | 239 | $this->controller = $this->getModule(); |
240 | 240 | } |
241 | 241 | //if can not determine the module/controller/method via the defined routes configuration we will use |
242 | 242 | //the URL like http://domain.com/module/controller/method/arg1/arg2 |
243 | - if(! $this->getController()){ |
|
243 | + if (!$this->getController()) { |
|
244 | 244 | $this->logger->info('Cannot determine the routing information using the predefined routes configuration, will use the request URI parameters'); |
245 | 245 | $nbSegment = count($segment); |
246 | 246 | //if segment is null so means no need to perform |
247 | - if($nbSegment > 0){ |
|
247 | + if ($nbSegment > 0) { |
|
248 | 248 | //get the module list |
249 | 249 | $modules = Module::getModuleList(); |
250 | 250 | //first check if no module |
251 | - if(! $modules){ |
|
251 | + if (!$modules) { |
|
252 | 252 | $this->logger->info('No module was loaded will skip the module checking'); |
253 | 253 | //the application don't use module |
254 | 254 | //controller |
255 | - if(isset($segment[0])){ |
|
255 | + if (isset($segment[0])) { |
|
256 | 256 | $this->controller = $segment[0]; |
257 | 257 | array_shift($segment); |
258 | 258 | } |
259 | 259 | //method |
260 | - if(isset($segment[0])){ |
|
260 | + if (isset($segment[0])) { |
|
261 | 261 | $this->method = $segment[0]; |
262 | 262 | array_shift($segment); |
263 | 263 | } |
264 | 264 | //args |
265 | 265 | $this->args = $segment; |
266 | 266 | } |
267 | - else{ |
|
267 | + else { |
|
268 | 268 | $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
269 | - if(in_array($segment[0], $modules)){ |
|
269 | + if (in_array($segment[0], $modules)) { |
|
270 | 270 | $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
271 | 271 | $this->module = $segment[0]; |
272 | 272 | array_shift($segment); |
273 | 273 | //check if the second arg is the controller from module |
274 | - if(isset($segment[0])){ |
|
274 | + if (isset($segment[0])) { |
|
275 | 275 | $this->controller = $segment[0]; |
276 | 276 | //check if the request use the same module name and controller |
277 | 277 | $path = Module::findControllerFullPath(ucfirst($this->getController()), $this->getModule()); |
278 | - if(! $path){ |
|
278 | + if (!$path) { |
|
279 | 279 | $this->logger->info('The controller [' . $this->getController() . '] not found in the module, may be will use the module [' . $this->getModule() . '] as controller'); |
280 | 280 | $this->controller = $this->getModule(); |
281 | 281 | } |
282 | - else{ |
|
282 | + else { |
|
283 | 283 | $this->controllerPath = $path; |
284 | 284 | array_shift($segment); |
285 | 285 | } |
286 | 286 | } |
287 | 287 | //check for method |
288 | - if(isset($segment[0])){ |
|
288 | + if (isset($segment[0])) { |
|
289 | 289 | $this->method = $segment[0]; |
290 | 290 | array_shift($segment); |
291 | 291 | } |
292 | 292 | //the remaining is for args |
293 | 293 | $this->args = $segment; |
294 | 294 | } |
295 | - else{ |
|
295 | + else { |
|
296 | 296 | $this->logger->info('The current request information is not found in the module list'); |
297 | 297 | //controller |
298 | - if(isset($segment[0])){ |
|
298 | + if (isset($segment[0])) { |
|
299 | 299 | $this->controller = $segment[0]; |
300 | 300 | array_shift($segment); |
301 | 301 | } |
302 | 302 | //method |
303 | - if(isset($segment[0])){ |
|
303 | + if (isset($segment[0])) { |
|
304 | 304 | $this->method = $segment[0]; |
305 | 305 | array_shift($segment); |
306 | 306 | } |
@@ -310,18 +310,18 @@ discard block |
||
310 | 310 | } |
311 | 311 | } |
312 | 312 | } |
313 | - if(! $this->getController() && $this->getModule()){ |
|
313 | + if (!$this->getController() && $this->getModule()) { |
|
314 | 314 | $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
315 | 315 | $this->controller = $this->getModule(); |
316 | 316 | } |
317 | 317 | //did we set the controller, so set the controller path |
318 | - if($this->getController() && ! $this->getControllerPath()){ |
|
318 | + if ($this->getController() && !$this->getControllerPath()) { |
|
319 | 319 | $this->logger->debug('Setting the file path for the controller [' . $this->getController() . ']'); |
320 | 320 | //if it is the module controller |
321 | - if($this->getModule()){ |
|
321 | + if ($this->getModule()) { |
|
322 | 322 | $this->controllerPath = Module::findControllerFullPath(ucfirst($this->getController()), $this->getModule()); |
323 | 323 | } |
324 | - else{ |
|
324 | + else { |
|
325 | 325 | $this->controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->getController()) . '.php'; |
326 | 326 | } |
327 | 327 | } |
@@ -331,20 +331,20 @@ discard block |
||
331 | 331 | $this->logger->debug('Loading controller [' . $controller . '], the file path is [' . $classFilePath . ']...'); |
332 | 332 | $benchmark->mark('ROUTING_PROCESS_END'); |
333 | 333 | $e404 = false; |
334 | - if(file_exists($classFilePath)){ |
|
334 | + if (file_exists($classFilePath)) { |
|
335 | 335 | require_once $classFilePath; |
336 | - if(! class_exists($controller, false)){ |
|
336 | + if (!class_exists($controller, false)) { |
|
337 | 337 | $e404 = true; |
338 | - $this->logger->info('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
|
338 | + $this->logger->info('The controller file [' . $classFilePath . '] exists but does not contain the class [' . $controller . ']'); |
|
339 | 339 | } |
340 | - else{ |
|
340 | + else { |
|
341 | 341 | $controllerInstance = new $controller(); |
342 | 342 | $controllerMethod = $this->getMethod(); |
343 | - if(! method_exists($controllerInstance, $controllerMethod)){ |
|
343 | + if (!method_exists($controllerInstance, $controllerMethod)) { |
|
344 | 344 | $e404 = true; |
345 | 345 | $this->logger->info('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
346 | 346 | } |
347 | - else{ |
|
347 | + else { |
|
348 | 348 | $this->logger->info('Routing data is set correctly now GO!'); |
349 | 349 | call_user_func_array(array($controllerInstance, $controllerMethod), $this->getArgs()); |
350 | 350 | $obj = & get_instance(); |
@@ -354,12 +354,12 @@ discard block |
||
354 | 354 | } |
355 | 355 | } |
356 | 356 | } |
357 | - else{ |
|
357 | + else { |
|
358 | 358 | $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
359 | 359 | $e404 = true; |
360 | 360 | } |
361 | - if($e404){ |
|
362 | - $response =& class_loader('Response', 'classes'); |
|
361 | + if ($e404) { |
|
362 | + $response = & class_loader('Response', 'classes'); |
|
363 | 363 | $response->send404(); |
364 | 364 | } |
365 | 365 | } |
@@ -368,7 +368,7 @@ discard block |
||
368 | 368 | * Return the Log instance |
369 | 369 | * @return Log |
370 | 370 | */ |
371 | - public function getLogger(){ |
|
371 | + public function getLogger() { |
|
372 | 372 | return $this->logger; |
373 | 373 | } |
374 | 374 | |
@@ -377,7 +377,7 @@ discard block |
||
377 | 377 | * @param Log $logger the log object |
378 | 378 | * @return object |
379 | 379 | */ |
380 | - public function setLogger($logger){ |
|
380 | + public function setLogger($logger) { |
|
381 | 381 | $this->logger = $logger; |
382 | 382 | return $this; |
383 | 383 | } |
@@ -388,9 +388,9 @@ discard block |
||
388 | 388 | * @param boolean $useConfigFile whether to use route configuration file |
389 | 389 | * @return object |
390 | 390 | */ |
391 | - public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true){ |
|
391 | + public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true) { |
|
392 | 392 | $route = array(); |
393 | - if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')){ |
|
393 | + if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')) { |
|
394 | 394 | require_once CONFIG_PATH . 'routes.php'; |
395 | 395 | } |
396 | 396 | $route = array_merge($route, $overwriteConfig); |
@@ -401,9 +401,9 @@ discard block |
||
401 | 401 | /** |
402 | 402 | * Set the route paramaters using the configuration |
403 | 403 | */ |
404 | - protected function setRouteParams(){ |
|
404 | + protected function setRouteParams() { |
|
405 | 405 | //adding route |
406 | - foreach($this->routes as $pattern => $callback){ |
|
406 | + foreach ($this->routes as $pattern => $callback) { |
|
407 | 407 | $this->add($pattern, $callback); |
408 | 408 | } |
409 | 409 | |
@@ -413,10 +413,10 @@ discard block |
||
413 | 413 | //remove url suffix from the request URI |
414 | 414 | $suffix = get_config('url_suffix'); |
415 | 415 | if ($suffix) { |
416 | - $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']' ); |
|
416 | + $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']'); |
|
417 | 417 | $uri = str_ireplace($suffix, '', $uri); |
418 | 418 | } |
419 | - if (strpos($uri, '?') !== false){ |
|
419 | + if (strpos($uri, '?') !== false) { |
|
420 | 420 | $uri = substr($uri, 0, strpos($uri, '?')); |
421 | 421 | } |
422 | 422 | $uri = trim($uri, $this->uriTrim); |
@@ -427,12 +427,12 @@ discard block |
||
427 | 427 | * Set the Log instance using argument or create new instance |
428 | 428 | * @param object $logger the Log instance if not null |
429 | 429 | */ |
430 | - protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
|
431 | - if ($logger !== null){ |
|
430 | + protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null) { |
|
431 | + if ($logger !== null) { |
|
432 | 432 | $this->logger = $logger; |
433 | 433 | } |
434 | - else{ |
|
435 | - $this->logger =& class_loader('Log', 'classes'); |
|
434 | + else { |
|
435 | + $this->logger = & class_loader('Log', 'classes'); |
|
436 | 436 | $this->logger->setLogger('Library::Router'); |
437 | 437 | } |
438 | 438 | } |
@@ -192,8 +192,7 @@ discard block |
||
192 | 192 | $this->logger->info('The request URI contains the front controller'); |
193 | 193 | array_shift($segment); |
194 | 194 | $this->segments = $segment; |
195 | - } |
|
196 | - else{ |
|
195 | + } else{ |
|
197 | 196 | $this->logger->info('The request URI does not contain the front controller'); |
198 | 197 | } |
199 | 198 | $uri = implode('/', $segment); |
@@ -215,8 +214,7 @@ discard block |
||
215 | 214 | $this->logger->info('The current request use the module [' .$moduleControllerMethod[0]. ']'); |
216 | 215 | $this->module = $moduleControllerMethod[0]; |
217 | 216 | $moduleControllerMethod = explode('@', $moduleControllerMethod[1]); |
218 | - } |
|
219 | - else{ |
|
217 | + } else{ |
|
220 | 218 | $this->logger->info('The current request does not use the module'); |
221 | 219 | $moduleControllerMethod = explode('@', $this->callback[$index]); |
222 | 220 | } |
@@ -263,8 +261,7 @@ discard block |
||
263 | 261 | } |
264 | 262 | //args |
265 | 263 | $this->args = $segment; |
266 | - } |
|
267 | - else{ |
|
264 | + } else{ |
|
268 | 265 | $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
269 | 266 | if(in_array($segment[0], $modules)){ |
270 | 267 | $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
@@ -278,8 +275,7 @@ discard block |
||
278 | 275 | if(! $path){ |
279 | 276 | $this->logger->info('The controller [' . $this->getController() . '] not found in the module, may be will use the module [' . $this->getModule() . '] as controller'); |
280 | 277 | $this->controller = $this->getModule(); |
281 | - } |
|
282 | - else{ |
|
278 | + } else{ |
|
283 | 279 | $this->controllerPath = $path; |
284 | 280 | array_shift($segment); |
285 | 281 | } |
@@ -291,8 +287,7 @@ discard block |
||
291 | 287 | } |
292 | 288 | //the remaining is for args |
293 | 289 | $this->args = $segment; |
294 | - } |
|
295 | - else{ |
|
290 | + } else{ |
|
296 | 291 | $this->logger->info('The current request information is not found in the module list'); |
297 | 292 | //controller |
298 | 293 | if(isset($segment[0])){ |
@@ -320,8 +315,7 @@ discard block |
||
320 | 315 | //if it is the module controller |
321 | 316 | if($this->getModule()){ |
322 | 317 | $this->controllerPath = Module::findControllerFullPath(ucfirst($this->getController()), $this->getModule()); |
323 | - } |
|
324 | - else{ |
|
318 | + } else{ |
|
325 | 319 | $this->controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->getController()) . '.php'; |
326 | 320 | } |
327 | 321 | } |
@@ -336,15 +330,13 @@ discard block |
||
336 | 330 | if(! class_exists($controller, false)){ |
337 | 331 | $e404 = true; |
338 | 332 | $this->logger->info('The controller file [' .$classFilePath. '] exists but does not contain the class [' . $controller . ']'); |
339 | - } |
|
340 | - else{ |
|
333 | + } else{ |
|
341 | 334 | $controllerInstance = new $controller(); |
342 | 335 | $controllerMethod = $this->getMethod(); |
343 | 336 | if(! method_exists($controllerInstance, $controllerMethod)){ |
344 | 337 | $e404 = true; |
345 | 338 | $this->logger->info('The controller [' . $controller . '] exist but does not contain the method [' . $controllerMethod . ']'); |
346 | - } |
|
347 | - else{ |
|
339 | + } else{ |
|
348 | 340 | $this->logger->info('Routing data is set correctly now GO!'); |
349 | 341 | call_user_func_array(array($controllerInstance, $controllerMethod), $this->getArgs()); |
350 | 342 | $obj = & get_instance(); |
@@ -353,8 +345,7 @@ discard block |
||
353 | 345 | $obj->response->renderFinalPage(); |
354 | 346 | } |
355 | 347 | } |
356 | - } |
|
357 | - else{ |
|
348 | + } else{ |
|
358 | 349 | $this->logger->info('The controller file path [' . $classFilePath . '] does not exist'); |
359 | 350 | $e404 = true; |
360 | 351 | } |
@@ -430,8 +421,7 @@ discard block |
||
430 | 421 | protected function setLoggerFromParamOrCreateNewInstance(Log $logger = null){ |
431 | 422 | if ($logger !== null){ |
432 | 423 | $this->logger = $logger; |
433 | - } |
|
434 | - else{ |
|
424 | + } else{ |
|
435 | 425 | $this->logger =& class_loader('Log', 'classes'); |
436 | 426 | $this->logger->setLogger('Library::Router'); |
437 | 427 | } |