@@ -1,343 +1,343 @@ |
||
1 | 1 | <?php |
2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
3 | - /** |
|
4 | - * TNH Framework |
|
5 | - * |
|
6 | - * A simple PHP framework using HMVC architecture |
|
7 | - * |
|
8 | - * This content is released under the GNU GPL License (GPL) |
|
9 | - * |
|
10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
11 | - * |
|
12 | - * This program is free software; you can redistribute it and/or |
|
13 | - * modify it under the terms of the GNU General Public License |
|
14 | - * as published by the Free Software Foundation; either version 3 |
|
15 | - * of the License, or (at your option) any later version. |
|
16 | - * |
|
17 | - * This program is distributed in the hope that it will be useful, |
|
18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | - * GNU General Public License for more details. |
|
21 | - * |
|
22 | - * You should have received a copy of the GNU General Public License |
|
23 | - * along with this program; if not, write to the Free Software |
|
24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | - */ |
|
2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
3 | + /** |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | 26 | |
27 | - class Html{ |
|
27 | + class Html{ |
|
28 | 28 | |
29 | - /** |
|
30 | - * Generate the html anchor link |
|
31 | - * @param string $link the href attribute value |
|
32 | - * @param string $anchor the displayed anchor |
|
33 | - * @param array $attributes the additional attributes to be added |
|
34 | - * @param boolean $return whether need return the generated html or just display it directly |
|
35 | - * |
|
36 | - * @return string|void the anchor link generated html if $return is true or display it if not |
|
37 | - */ |
|
38 | - public static function a($link = '', $anchor = null, array $attributes = array(), $return = true){ |
|
39 | - $link = Url::site_url($link); |
|
40 | - if(! $anchor){ |
|
41 | - $anchor = $link; |
|
42 | - } |
|
43 | - $str = null; |
|
44 | - $str .= '<a href = "'.$link.'"'; |
|
45 | - $str .= attributes_to_string($attributes); |
|
46 | - $str .= '>'; |
|
47 | - $str .= $anchor; |
|
48 | - $str .= '</a>'; |
|
29 | + /** |
|
30 | + * Generate the html anchor link |
|
31 | + * @param string $link the href attribute value |
|
32 | + * @param string $anchor the displayed anchor |
|
33 | + * @param array $attributes the additional attributes to be added |
|
34 | + * @param boolean $return whether need return the generated html or just display it directly |
|
35 | + * |
|
36 | + * @return string|void the anchor link generated html if $return is true or display it if not |
|
37 | + */ |
|
38 | + public static function a($link = '', $anchor = null, array $attributes = array(), $return = true){ |
|
39 | + $link = Url::site_url($link); |
|
40 | + if(! $anchor){ |
|
41 | + $anchor = $link; |
|
42 | + } |
|
43 | + $str = null; |
|
44 | + $str .= '<a href = "'.$link.'"'; |
|
45 | + $str .= attributes_to_string($attributes); |
|
46 | + $str .= '>'; |
|
47 | + $str .= $anchor; |
|
48 | + $str .= '</a>'; |
|
49 | 49 | |
50 | - if($return){ |
|
51 | - return $str; |
|
52 | - } |
|
53 | - echo $str; |
|
54 | - } |
|
50 | + if($return){ |
|
51 | + return $str; |
|
52 | + } |
|
53 | + echo $str; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Generate an mailto anchor link |
|
58 | - * @param string $link the email address |
|
59 | - * @param string $anchor the displayed value of the link |
|
60 | - * @param array $attributes the additional attributes to be added |
|
61 | - * @param boolean $return whether need return the generated html or just display it directly |
|
62 | - * |
|
63 | - * @return string|void the generated html for mailto link if $return is true or display it if not |
|
64 | - */ |
|
65 | - public static function mailto($link, $anchor = null, array $attributes = array(), $return = true){ |
|
66 | - if(! $anchor){ |
|
67 | - $anchor = $link; |
|
68 | - } |
|
69 | - $str = null; |
|
70 | - $str .= '<a href = "mailto:'.$link.'"'; |
|
71 | - $str .= attributes_to_string($attributes); |
|
72 | - $str .= '>'; |
|
73 | - $str .= $anchor; |
|
74 | - $str .= '</a>'; |
|
56 | + /** |
|
57 | + * Generate an mailto anchor link |
|
58 | + * @param string $link the email address |
|
59 | + * @param string $anchor the displayed value of the link |
|
60 | + * @param array $attributes the additional attributes to be added |
|
61 | + * @param boolean $return whether need return the generated html or just display it directly |
|
62 | + * |
|
63 | + * @return string|void the generated html for mailto link if $return is true or display it if not |
|
64 | + */ |
|
65 | + public static function mailto($link, $anchor = null, array $attributes = array(), $return = true){ |
|
66 | + if(! $anchor){ |
|
67 | + $anchor = $link; |
|
68 | + } |
|
69 | + $str = null; |
|
70 | + $str .= '<a href = "mailto:'.$link.'"'; |
|
71 | + $str .= attributes_to_string($attributes); |
|
72 | + $str .= '>'; |
|
73 | + $str .= $anchor; |
|
74 | + $str .= '</a>'; |
|
75 | 75 | |
76 | - if($return){ |
|
77 | - return $str; |
|
78 | - } |
|
79 | - echo $str; |
|
80 | - } |
|
76 | + if($return){ |
|
77 | + return $str; |
|
78 | + } |
|
79 | + echo $str; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Generate the html "br" tag |
|
84 | - * @param integer $nb the number of generated "<br />" tag |
|
85 | - * @param boolean $return whether need return the generated html or just display it directly |
|
86 | - * |
|
87 | - * @return string|void the generated "br" html if $return is true or display it if not |
|
88 | - */ |
|
89 | - public static function br($nb = 1, $return = true){ |
|
90 | - $nb = (int) $nb; |
|
91 | - $str = null; |
|
92 | - for ($i = 1; $i <= $nb; $i++) { |
|
93 | - $str .= '<br />'; |
|
94 | - } |
|
82 | + /** |
|
83 | + * Generate the html "br" tag |
|
84 | + * @param integer $nb the number of generated "<br />" tag |
|
85 | + * @param boolean $return whether need return the generated html or just display it directly |
|
86 | + * |
|
87 | + * @return string|void the generated "br" html if $return is true or display it if not |
|
88 | + */ |
|
89 | + public static function br($nb = 1, $return = true){ |
|
90 | + $nb = (int) $nb; |
|
91 | + $str = null; |
|
92 | + for ($i = 1; $i <= $nb; $i++) { |
|
93 | + $str .= '<br />'; |
|
94 | + } |
|
95 | 95 | |
96 | - if($return){ |
|
97 | - return $str; |
|
98 | - } |
|
99 | - echo $str; |
|
100 | - } |
|
96 | + if($return){ |
|
97 | + return $str; |
|
98 | + } |
|
99 | + echo $str; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Generate the html content for tag "hr" |
|
104 | - * @param integer $nb the number of generated "<hr />" tag |
|
105 | - * @param array $attributes the tag attributes |
|
106 | - * @param boolean $return whether need return the generated html or just display it directly |
|
107 | - * |
|
108 | - * @return string|void the generated "hr" html if $return is true or display it if not. |
|
109 | - */ |
|
110 | - public static function hr($nb = 1, array $attributes = array(), $return = true){ |
|
111 | - $nb = (int) $nb; |
|
112 | - $str = null; |
|
113 | - for ($i = 1; $i <= $nb; $i++) { |
|
114 | - $str .= '<hr' .attributes_to_string($attributes). ' />'; |
|
115 | - } |
|
116 | - if($return){ |
|
117 | - return $str; |
|
118 | - } |
|
119 | - echo $str; |
|
120 | - } |
|
102 | + /** |
|
103 | + * Generate the html content for tag "hr" |
|
104 | + * @param integer $nb the number of generated "<hr />" tag |
|
105 | + * @param array $attributes the tag attributes |
|
106 | + * @param boolean $return whether need return the generated html or just display it directly |
|
107 | + * |
|
108 | + * @return string|void the generated "hr" html if $return is true or display it if not. |
|
109 | + */ |
|
110 | + public static function hr($nb = 1, array $attributes = array(), $return = true){ |
|
111 | + $nb = (int) $nb; |
|
112 | + $str = null; |
|
113 | + for ($i = 1; $i <= $nb; $i++) { |
|
114 | + $str .= '<hr' .attributes_to_string($attributes). ' />'; |
|
115 | + } |
|
116 | + if($return){ |
|
117 | + return $str; |
|
118 | + } |
|
119 | + echo $str; |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * Generate the html content for tag like h1, h2, h3, h4, h5 and h6 |
|
124 | - * @param integer $type the tag type 1 mean h1, 2 h2, etc, |
|
125 | - * @param string $text the display text |
|
126 | - * @param integer $nb the number of generated "<h{1,2,3,4,5,6}>" |
|
127 | - * @param array $attributes the tag attributes |
|
128 | - * @param boolean $return whether need return the generated html or just display it directly |
|
129 | - * |
|
130 | - * @return string|void the generated header html if $return is true or display it if not. |
|
131 | - */ |
|
132 | - public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true){ |
|
133 | - $nb = (int) $nb; |
|
134 | - $type = (int) $type; |
|
135 | - $str = null; |
|
136 | - for ($i = 1; $i <= $nb; $i++) { |
|
137 | - $str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>'; |
|
138 | - } |
|
139 | - if($return){ |
|
140 | - return $str; |
|
141 | - } |
|
142 | - echo $str; |
|
143 | - } |
|
122 | + /** |
|
123 | + * Generate the html content for tag like h1, h2, h3, h4, h5 and h6 |
|
124 | + * @param integer $type the tag type 1 mean h1, 2 h2, etc, |
|
125 | + * @param string $text the display text |
|
126 | + * @param integer $nb the number of generated "<h{1,2,3,4,5,6}>" |
|
127 | + * @param array $attributes the tag attributes |
|
128 | + * @param boolean $return whether need return the generated html or just display it directly |
|
129 | + * |
|
130 | + * @return string|void the generated header html if $return is true or display it if not. |
|
131 | + */ |
|
132 | + public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true){ |
|
133 | + $nb = (int) $nb; |
|
134 | + $type = (int) $type; |
|
135 | + $str = null; |
|
136 | + for ($i = 1; $i <= $nb; $i++) { |
|
137 | + $str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>'; |
|
138 | + } |
|
139 | + if($return){ |
|
140 | + return $str; |
|
141 | + } |
|
142 | + echo $str; |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * Generate the html "ul" tag |
|
147 | - * @param array $data the data to use for each "li" tag |
|
148 | - * @param array $attributes the "ul" properties attribute use the array index below for each tag: |
|
149 | - * for ul "ul", for li "li". |
|
150 | - * @param boolean $return whether need return the generated html or just display it directly |
|
151 | - * |
|
152 | - * @return string|void the generated "ul" html if $return is true or display it if not. |
|
153 | - */ |
|
154 | - public static function ul($data = array(), $attributes = array(), $return = true){ |
|
155 | - if($return){ |
|
156 | - return self::buildUlOl($data, $attributes, true, 'ul'); |
|
157 | - } |
|
158 | - self::buildUlOl($data, $attributes, false, 'ul'); |
|
159 | - } |
|
145 | + /** |
|
146 | + * Generate the html "ul" tag |
|
147 | + * @param array $data the data to use for each "li" tag |
|
148 | + * @param array $attributes the "ul" properties attribute use the array index below for each tag: |
|
149 | + * for ul "ul", for li "li". |
|
150 | + * @param boolean $return whether need return the generated html or just display it directly |
|
151 | + * |
|
152 | + * @return string|void the generated "ul" html if $return is true or display it if not. |
|
153 | + */ |
|
154 | + public static function ul($data = array(), $attributes = array(), $return = true){ |
|
155 | + if($return){ |
|
156 | + return self::buildUlOl($data, $attributes, true, 'ul'); |
|
157 | + } |
|
158 | + self::buildUlOl($data, $attributes, false, 'ul'); |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * Generate the html "ol" tag |
|
163 | - * @param array $data the data to use for each "li" tag |
|
164 | - * @param array $attributes the "ol" properties attribute use the array index below for each tag: |
|
165 | - * for ol "ol", for li "li". |
|
166 | - * @param boolean $return whether need return the generated html or just display it directly |
|
167 | - * @return string|void the generated "ol" html if $return is true or display it if not. |
|
168 | - */ |
|
169 | - public static function ol($data = array(), $attributes = array(), $return = true){ |
|
170 | - if($return){ |
|
171 | - return self::buildUlOl($data, $attributes, true, 'ol'); |
|
172 | - } |
|
173 | - self::buildUlOl($data, $attributes, false, 'ol'); |
|
174 | - } |
|
161 | + /** |
|
162 | + * Generate the html "ol" tag |
|
163 | + * @param array $data the data to use for each "li" tag |
|
164 | + * @param array $attributes the "ol" properties attribute use the array index below for each tag: |
|
165 | + * for ol "ol", for li "li". |
|
166 | + * @param boolean $return whether need return the generated html or just display it directly |
|
167 | + * @return string|void the generated "ol" html if $return is true or display it if not. |
|
168 | + */ |
|
169 | + public static function ol($data = array(), $attributes = array(), $return = true){ |
|
170 | + if($return){ |
|
171 | + return self::buildUlOl($data, $attributes, true, 'ol'); |
|
172 | + } |
|
173 | + self::buildUlOl($data, $attributes, false, 'ol'); |
|
174 | + } |
|
175 | 175 | |
176 | 176 | |
177 | - /** |
|
178 | - * Generate the html "table" tag |
|
179 | - * @param array $headers the table headers to use between (<thead>) |
|
180 | - * @param array $body the table body values between (<tbody>) |
|
181 | - * @param array $attributes the table properties attribute use the array index below for each tag: |
|
182 | - * for table "table", for thead "thead", for thead tr "thead_tr", |
|
183 | - * for thead th "thead_th", for tbody "tbody", for tbody tr "tbody_tr", for tbody td "tbody_td", for tfoot "tfoot", |
|
184 | - * for tfoot tr "tfoot_tr", for tfoot th "tfoot_th". |
|
185 | - * @param boolean $use_footer whether need to generate table footer (<tfoot>) use the $headers values |
|
186 | - * @param boolean $return whether need return the generated html or just display it directly |
|
187 | - * @return string|void the generated "table" html if $return is true or display it if not. |
|
188 | - */ |
|
189 | - public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){ |
|
190 | - $headers = (array) $headers; |
|
191 | - $body = (array) $body; |
|
192 | - $str = null; |
|
193 | - $tableAttributes = ''; |
|
194 | - if(! empty($attributes['table'])){ |
|
195 | - $tableAttributes = ' ' . attributes_to_string($attributes['table']); |
|
196 | - } |
|
197 | - $str .= '<table' . $tableAttributes . '>'; |
|
198 | - $str .= self::buildTableHeader($headers, $attributes); |
|
199 | - $str .= self::buildTableBody($body, $attributes); |
|
177 | + /** |
|
178 | + * Generate the html "table" tag |
|
179 | + * @param array $headers the table headers to use between (<thead>) |
|
180 | + * @param array $body the table body values between (<tbody>) |
|
181 | + * @param array $attributes the table properties attribute use the array index below for each tag: |
|
182 | + * for table "table", for thead "thead", for thead tr "thead_tr", |
|
183 | + * for thead th "thead_th", for tbody "tbody", for tbody tr "tbody_tr", for tbody td "tbody_td", for tfoot "tfoot", |
|
184 | + * for tfoot tr "tfoot_tr", for tfoot th "tfoot_th". |
|
185 | + * @param boolean $use_footer whether need to generate table footer (<tfoot>) use the $headers values |
|
186 | + * @param boolean $return whether need return the generated html or just display it directly |
|
187 | + * @return string|void the generated "table" html if $return is true or display it if not. |
|
188 | + */ |
|
189 | + public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){ |
|
190 | + $headers = (array) $headers; |
|
191 | + $body = (array) $body; |
|
192 | + $str = null; |
|
193 | + $tableAttributes = ''; |
|
194 | + if(! empty($attributes['table'])){ |
|
195 | + $tableAttributes = ' ' . attributes_to_string($attributes['table']); |
|
196 | + } |
|
197 | + $str .= '<table' . $tableAttributes . '>'; |
|
198 | + $str .= self::buildTableHeader($headers, $attributes); |
|
199 | + $str .= self::buildTableBody($body, $attributes); |
|
200 | 200 | |
201 | - if($use_footer){ |
|
202 | - $str .= self::buildTableFooter($headers, $attributes); |
|
203 | - } |
|
204 | - $str .= '</table>'; |
|
205 | - if($return){ |
|
206 | - return $str; |
|
207 | - } |
|
208 | - echo $str; |
|
209 | - } |
|
201 | + if($use_footer){ |
|
202 | + $str .= self::buildTableFooter($headers, $attributes); |
|
203 | + } |
|
204 | + $str .= '</table>'; |
|
205 | + if($return){ |
|
206 | + return $str; |
|
207 | + } |
|
208 | + echo $str; |
|
209 | + } |
|
210 | 210 | |
211 | - /** |
|
212 | - * This method is used to build the header of the html table |
|
213 | - * @see Html::table |
|
214 | - * @return string|null |
|
215 | - */ |
|
216 | - protected static function buildTableHeader(array $headers, $attributes = array()){ |
|
217 | - $str = null; |
|
218 | - $theadAttributes = ''; |
|
219 | - if(! empty($attributes['thead'])){ |
|
220 | - $theadAttributes = ' ' . attributes_to_string($attributes['thead']); |
|
221 | - } |
|
222 | - $theadtrAttributes = ''; |
|
223 | - if(! empty($attributes['thead_tr'])){ |
|
224 | - $theadtrAttributes = ' ' . attributes_to_string($attributes['thead_tr']); |
|
225 | - } |
|
226 | - $thAttributes = ''; |
|
227 | - if(! empty($attributes['thead_th'])){ |
|
228 | - $thAttributes = ' ' . attributes_to_string($attributes['thead_th']); |
|
229 | - } |
|
230 | - $str .= '<thead' . $theadAttributes .'>'; |
|
231 | - $str .= '<tr' . $theadtrAttributes .'>'; |
|
232 | - foreach ($headers as $value) { |
|
233 | - $str .= '<th' . $thAttributes .'>' .$value. '</th>'; |
|
234 | - } |
|
235 | - $str .= '</tr>'; |
|
236 | - $str .= '</thead>'; |
|
237 | - return $str; |
|
238 | - } |
|
211 | + /** |
|
212 | + * This method is used to build the header of the html table |
|
213 | + * @see Html::table |
|
214 | + * @return string|null |
|
215 | + */ |
|
216 | + protected static function buildTableHeader(array $headers, $attributes = array()){ |
|
217 | + $str = null; |
|
218 | + $theadAttributes = ''; |
|
219 | + if(! empty($attributes['thead'])){ |
|
220 | + $theadAttributes = ' ' . attributes_to_string($attributes['thead']); |
|
221 | + } |
|
222 | + $theadtrAttributes = ''; |
|
223 | + if(! empty($attributes['thead_tr'])){ |
|
224 | + $theadtrAttributes = ' ' . attributes_to_string($attributes['thead_tr']); |
|
225 | + } |
|
226 | + $thAttributes = ''; |
|
227 | + if(! empty($attributes['thead_th'])){ |
|
228 | + $thAttributes = ' ' . attributes_to_string($attributes['thead_th']); |
|
229 | + } |
|
230 | + $str .= '<thead' . $theadAttributes .'>'; |
|
231 | + $str .= '<tr' . $theadtrAttributes .'>'; |
|
232 | + foreach ($headers as $value) { |
|
233 | + $str .= '<th' . $thAttributes .'>' .$value. '</th>'; |
|
234 | + } |
|
235 | + $str .= '</tr>'; |
|
236 | + $str .= '</thead>'; |
|
237 | + return $str; |
|
238 | + } |
|
239 | 239 | |
240 | - /** |
|
241 | - * This method is used to build the body of the html table |
|
242 | - * @see Html::table |
|
243 | - * @return string|null |
|
244 | - */ |
|
245 | - protected static function buildTableBody(array $body, $attributes = array()){ |
|
246 | - $str = null; |
|
247 | - $tbodyAttributes = ''; |
|
248 | - if(! empty($attributes['tbody'])){ |
|
249 | - $tbodyAttributes = ' ' . attributes_to_string($attributes['tbody']); |
|
250 | - } |
|
251 | - $tbodytrAttributes = ''; |
|
252 | - if(! empty($attributes['tbody_tr'])){ |
|
253 | - $tbodytrAttributes = ' ' . attributes_to_string($attributes['tbody_tr']); |
|
254 | - } |
|
255 | - $tbodytdAttributes = ''; |
|
256 | - if(! empty($attributes['tbody_td'])){ |
|
257 | - $tbodytdAttributes = ' ' . attributes_to_string($attributes['tbody_td']); |
|
258 | - } |
|
259 | - $str .= '<tbody' . $tbodyAttributes .'>'; |
|
260 | - $str .= self::buildTableBodyContent($body, $tbodytrAttributes, $tbodytdAttributes); |
|
261 | - $str .= '</tbody>'; |
|
262 | - return $str; |
|
263 | - } |
|
240 | + /** |
|
241 | + * This method is used to build the body of the html table |
|
242 | + * @see Html::table |
|
243 | + * @return string|null |
|
244 | + */ |
|
245 | + protected static function buildTableBody(array $body, $attributes = array()){ |
|
246 | + $str = null; |
|
247 | + $tbodyAttributes = ''; |
|
248 | + if(! empty($attributes['tbody'])){ |
|
249 | + $tbodyAttributes = ' ' . attributes_to_string($attributes['tbody']); |
|
250 | + } |
|
251 | + $tbodytrAttributes = ''; |
|
252 | + if(! empty($attributes['tbody_tr'])){ |
|
253 | + $tbodytrAttributes = ' ' . attributes_to_string($attributes['tbody_tr']); |
|
254 | + } |
|
255 | + $tbodytdAttributes = ''; |
|
256 | + if(! empty($attributes['tbody_td'])){ |
|
257 | + $tbodytdAttributes = ' ' . attributes_to_string($attributes['tbody_td']); |
|
258 | + } |
|
259 | + $str .= '<tbody' . $tbodyAttributes .'>'; |
|
260 | + $str .= self::buildTableBodyContent($body, $tbodytrAttributes, $tbodytdAttributes); |
|
261 | + $str .= '</tbody>'; |
|
262 | + return $str; |
|
263 | + } |
|
264 | 264 | |
265 | - /** |
|
266 | - * This method is used to build the body content of the html table |
|
267 | - * @param array $body the table body data |
|
268 | - * @param string $tbodytrAttributes the html attributes for each tr in tbody |
|
269 | - * @param string $tbodytdAttributes the html attributes for each td in tbody |
|
270 | - * @return string |
|
271 | - */ |
|
272 | - protected static function buildTableBodyContent(array $body, $tbodytrAttributes, $tbodytdAttributes){ |
|
273 | - $str = null; |
|
274 | - foreach ($body as $row) { |
|
275 | - if(is_array($row)){ |
|
276 | - $str .= '<tr' . $tbodytrAttributes .'>'; |
|
277 | - foreach ($row as $value) { |
|
278 | - $str .= '<td' . $tbodytdAttributes .'>' .$value. '</td>'; |
|
279 | - } |
|
280 | - $str .= '</tr>'; |
|
281 | - } |
|
282 | - } |
|
283 | - return $str; |
|
284 | - } |
|
265 | + /** |
|
266 | + * This method is used to build the body content of the html table |
|
267 | + * @param array $body the table body data |
|
268 | + * @param string $tbodytrAttributes the html attributes for each tr in tbody |
|
269 | + * @param string $tbodytdAttributes the html attributes for each td in tbody |
|
270 | + * @return string |
|
271 | + */ |
|
272 | + protected static function buildTableBodyContent(array $body, $tbodytrAttributes, $tbodytdAttributes){ |
|
273 | + $str = null; |
|
274 | + foreach ($body as $row) { |
|
275 | + if(is_array($row)){ |
|
276 | + $str .= '<tr' . $tbodytrAttributes .'>'; |
|
277 | + foreach ($row as $value) { |
|
278 | + $str .= '<td' . $tbodytdAttributes .'>' .$value. '</td>'; |
|
279 | + } |
|
280 | + $str .= '</tr>'; |
|
281 | + } |
|
282 | + } |
|
283 | + return $str; |
|
284 | + } |
|
285 | 285 | |
286 | - /** |
|
287 | - * This method is used to build the footer of the html table |
|
288 | - * @see Html::table |
|
289 | - * @return string|null |
|
290 | - */ |
|
291 | - protected static function buildTableFooter(array $footers, $attributes = array()){ |
|
292 | - $str = null; |
|
293 | - $tfootAttributes = ''; |
|
294 | - if(! empty($attributes['tfoot'])){ |
|
295 | - $tfootAttributes = ' ' . attributes_to_string($attributes['tfoot']); |
|
296 | - } |
|
297 | - $tfoottrAttributes = ''; |
|
298 | - if(! empty($attributes['tfoot_tr'])){ |
|
299 | - $tfoottrAttributes = ' ' . attributes_to_string($attributes['tfoot_tr']); |
|
300 | - } |
|
301 | - $thAttributes = ''; |
|
302 | - if(! empty($attributes['tfoot_th'])){ |
|
303 | - $thAttributes = ' ' . attributes_to_string($attributes['tfoot_th']); |
|
304 | - } |
|
305 | - $str .= '<tfoot' . $tfootAttributes .'>'; |
|
306 | - $str .= '<tr' . $tfoottrAttributes .'>'; |
|
307 | - foreach ($footers as $value) { |
|
308 | - $str .= '<th' . $thAttributes .'>' .$value. '</th>'; |
|
309 | - } |
|
310 | - $str .= '</tr>'; |
|
311 | - $str .= '</tfoot>'; |
|
312 | - return $str; |
|
313 | - } |
|
286 | + /** |
|
287 | + * This method is used to build the footer of the html table |
|
288 | + * @see Html::table |
|
289 | + * @return string|null |
|
290 | + */ |
|
291 | + protected static function buildTableFooter(array $footers, $attributes = array()){ |
|
292 | + $str = null; |
|
293 | + $tfootAttributes = ''; |
|
294 | + if(! empty($attributes['tfoot'])){ |
|
295 | + $tfootAttributes = ' ' . attributes_to_string($attributes['tfoot']); |
|
296 | + } |
|
297 | + $tfoottrAttributes = ''; |
|
298 | + if(! empty($attributes['tfoot_tr'])){ |
|
299 | + $tfoottrAttributes = ' ' . attributes_to_string($attributes['tfoot_tr']); |
|
300 | + } |
|
301 | + $thAttributes = ''; |
|
302 | + if(! empty($attributes['tfoot_th'])){ |
|
303 | + $thAttributes = ' ' . attributes_to_string($attributes['tfoot_th']); |
|
304 | + } |
|
305 | + $str .= '<tfoot' . $tfootAttributes .'>'; |
|
306 | + $str .= '<tr' . $tfoottrAttributes .'>'; |
|
307 | + foreach ($footers as $value) { |
|
308 | + $str .= '<th' . $thAttributes .'>' .$value. '</th>'; |
|
309 | + } |
|
310 | + $str .= '</tr>'; |
|
311 | + $str .= '</tfoot>'; |
|
312 | + return $str; |
|
313 | + } |
|
314 | 314 | |
315 | - /** |
|
316 | - * Return the HTML content for ol or ul tags |
|
317 | - * @see Html::ol |
|
318 | - * @see Html::ul |
|
319 | - * @param string $olul the type 'ol' or 'ul' |
|
320 | - * @return void|string |
|
321 | - */ |
|
322 | - protected static function buildUlOl($data = array(), $attributes = array(), $return = true, $olul = 'ul'){ |
|
323 | - $data = (array) $data; |
|
324 | - $str = null; |
|
325 | - $olulAttributes = ''; |
|
326 | - if(! empty($attributes[$olul])){ |
|
327 | - $olulAttributes = ' ' . attributes_to_string($attributes[$olul]); |
|
328 | - } |
|
329 | - $liAttributes = ''; |
|
330 | - if(! empty($attributes['li'])){ |
|
331 | - $liAttributes = ' ' . attributes_to_string($attributes['li']); |
|
332 | - } |
|
333 | - $str .= '<' . $olul . $olulAttributes . '>'; |
|
334 | - foreach ($data as $row) { |
|
335 | - $str .= '<li' . $liAttributes .'>' .$row. '</li>'; |
|
336 | - } |
|
337 | - $str .= '</' . $olul . '>'; |
|
338 | - if($return){ |
|
339 | - return $str; |
|
340 | - } |
|
341 | - echo $str; |
|
342 | - } |
|
343 | - } |
|
315 | + /** |
|
316 | + * Return the HTML content for ol or ul tags |
|
317 | + * @see Html::ol |
|
318 | + * @see Html::ul |
|
319 | + * @param string $olul the type 'ol' or 'ul' |
|
320 | + * @return void|string |
|
321 | + */ |
|
322 | + protected static function buildUlOl($data = array(), $attributes = array(), $return = true, $olul = 'ul'){ |
|
323 | + $data = (array) $data; |
|
324 | + $str = null; |
|
325 | + $olulAttributes = ''; |
|
326 | + if(! empty($attributes[$olul])){ |
|
327 | + $olulAttributes = ' ' . attributes_to_string($attributes[$olul]); |
|
328 | + } |
|
329 | + $liAttributes = ''; |
|
330 | + if(! empty($attributes['li'])){ |
|
331 | + $liAttributes = ' ' . attributes_to_string($attributes['li']); |
|
332 | + } |
|
333 | + $str .= '<' . $olul . $olulAttributes . '>'; |
|
334 | + foreach ($data as $row) { |
|
335 | + $str .= '<li' . $liAttributes .'>' .$row. '</li>'; |
|
336 | + } |
|
337 | + $str .= '</' . $olul . '>'; |
|
338 | + if($return){ |
|
339 | + return $str; |
|
340 | + } |
|
341 | + echo $str; |
|
342 | + } |
|
343 | + } |
@@ -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 Html{ |
|
27 | + class Html { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * Generate the html anchor link |
@@ -35,19 +35,19 @@ discard block |
||
35 | 35 | * |
36 | 36 | * @return string|void the anchor link generated html if $return is true or display it if not |
37 | 37 | */ |
38 | - public static function a($link = '', $anchor = null, array $attributes = array(), $return = true){ |
|
38 | + public static function a($link = '', $anchor = null, array $attributes = array(), $return = true) { |
|
39 | 39 | $link = Url::site_url($link); |
40 | - if(! $anchor){ |
|
40 | + if (!$anchor) { |
|
41 | 41 | $anchor = $link; |
42 | 42 | } |
43 | 43 | $str = null; |
44 | - $str .= '<a href = "'.$link.'"'; |
|
44 | + $str .= '<a href = "' . $link . '"'; |
|
45 | 45 | $str .= attributes_to_string($attributes); |
46 | 46 | $str .= '>'; |
47 | 47 | $str .= $anchor; |
48 | 48 | $str .= '</a>'; |
49 | 49 | |
50 | - if($return){ |
|
50 | + if ($return) { |
|
51 | 51 | return $str; |
52 | 52 | } |
53 | 53 | echo $str; |
@@ -62,18 +62,18 @@ discard block |
||
62 | 62 | * |
63 | 63 | * @return string|void the generated html for mailto link if $return is true or display it if not |
64 | 64 | */ |
65 | - public static function mailto($link, $anchor = null, array $attributes = array(), $return = true){ |
|
66 | - if(! $anchor){ |
|
65 | + public static function mailto($link, $anchor = null, array $attributes = array(), $return = true) { |
|
66 | + if (!$anchor) { |
|
67 | 67 | $anchor = $link; |
68 | 68 | } |
69 | 69 | $str = null; |
70 | - $str .= '<a href = "mailto:'.$link.'"'; |
|
70 | + $str .= '<a href = "mailto:' . $link . '"'; |
|
71 | 71 | $str .= attributes_to_string($attributes); |
72 | 72 | $str .= '>'; |
73 | 73 | $str .= $anchor; |
74 | 74 | $str .= '</a>'; |
75 | 75 | |
76 | - if($return){ |
|
76 | + if ($return) { |
|
77 | 77 | return $str; |
78 | 78 | } |
79 | 79 | echo $str; |
@@ -86,14 +86,14 @@ discard block |
||
86 | 86 | * |
87 | 87 | * @return string|void the generated "br" html if $return is true or display it if not |
88 | 88 | */ |
89 | - public static function br($nb = 1, $return = true){ |
|
89 | + public static function br($nb = 1, $return = true) { |
|
90 | 90 | $nb = (int) $nb; |
91 | 91 | $str = null; |
92 | 92 | for ($i = 1; $i <= $nb; $i++) { |
93 | 93 | $str .= '<br />'; |
94 | 94 | } |
95 | 95 | |
96 | - if($return){ |
|
96 | + if ($return) { |
|
97 | 97 | return $str; |
98 | 98 | } |
99 | 99 | echo $str; |
@@ -107,13 +107,13 @@ discard block |
||
107 | 107 | * |
108 | 108 | * @return string|void the generated "hr" html if $return is true or display it if not. |
109 | 109 | */ |
110 | - public static function hr($nb = 1, array $attributes = array(), $return = true){ |
|
110 | + public static function hr($nb = 1, array $attributes = array(), $return = true) { |
|
111 | 111 | $nb = (int) $nb; |
112 | 112 | $str = null; |
113 | 113 | for ($i = 1; $i <= $nb; $i++) { |
114 | - $str .= '<hr' .attributes_to_string($attributes). ' />'; |
|
114 | + $str .= '<hr' . attributes_to_string($attributes) . ' />'; |
|
115 | 115 | } |
116 | - if($return){ |
|
116 | + if ($return) { |
|
117 | 117 | return $str; |
118 | 118 | } |
119 | 119 | echo $str; |
@@ -129,14 +129,14 @@ discard block |
||
129 | 129 | * |
130 | 130 | * @return string|void the generated header html if $return is true or display it if not. |
131 | 131 | */ |
132 | - public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true){ |
|
132 | + public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true) { |
|
133 | 133 | $nb = (int) $nb; |
134 | 134 | $type = (int) $type; |
135 | 135 | $str = null; |
136 | 136 | for ($i = 1; $i <= $nb; $i++) { |
137 | - $str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>'; |
|
137 | + $str .= '<h' . $type . attributes_to_string($attributes) . '>' . $text . '</h' . $type . '>'; |
|
138 | 138 | } |
139 | - if($return){ |
|
139 | + if ($return) { |
|
140 | 140 | return $str; |
141 | 141 | } |
142 | 142 | echo $str; |
@@ -151,8 +151,8 @@ discard block |
||
151 | 151 | * |
152 | 152 | * @return string|void the generated "ul" html if $return is true or display it if not. |
153 | 153 | */ |
154 | - public static function ul($data = array(), $attributes = array(), $return = true){ |
|
155 | - if($return){ |
|
154 | + public static function ul($data = array(), $attributes = array(), $return = true) { |
|
155 | + if ($return) { |
|
156 | 156 | return self::buildUlOl($data, $attributes, true, 'ul'); |
157 | 157 | } |
158 | 158 | self::buildUlOl($data, $attributes, false, 'ul'); |
@@ -166,8 +166,8 @@ discard block |
||
166 | 166 | * @param boolean $return whether need return the generated html or just display it directly |
167 | 167 | * @return string|void the generated "ol" html if $return is true or display it if not. |
168 | 168 | */ |
169 | - public static function ol($data = array(), $attributes = array(), $return = true){ |
|
170 | - if($return){ |
|
169 | + public static function ol($data = array(), $attributes = array(), $return = true) { |
|
170 | + if ($return) { |
|
171 | 171 | return self::buildUlOl($data, $attributes, true, 'ol'); |
172 | 172 | } |
173 | 173 | self::buildUlOl($data, $attributes, false, 'ol'); |
@@ -186,23 +186,23 @@ discard block |
||
186 | 186 | * @param boolean $return whether need return the generated html or just display it directly |
187 | 187 | * @return string|void the generated "table" html if $return is true or display it if not. |
188 | 188 | */ |
189 | - public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){ |
|
189 | + public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true) { |
|
190 | 190 | $headers = (array) $headers; |
191 | 191 | $body = (array) $body; |
192 | 192 | $str = null; |
193 | 193 | $tableAttributes = ''; |
194 | - if(! empty($attributes['table'])){ |
|
194 | + if (!empty($attributes['table'])) { |
|
195 | 195 | $tableAttributes = ' ' . attributes_to_string($attributes['table']); |
196 | 196 | } |
197 | 197 | $str .= '<table' . $tableAttributes . '>'; |
198 | 198 | $str .= self::buildTableHeader($headers, $attributes); |
199 | 199 | $str .= self::buildTableBody($body, $attributes); |
200 | 200 | |
201 | - if($use_footer){ |
|
201 | + if ($use_footer) { |
|
202 | 202 | $str .= self::buildTableFooter($headers, $attributes); |
203 | 203 | } |
204 | 204 | $str .= '</table>'; |
205 | - if($return){ |
|
205 | + if ($return) { |
|
206 | 206 | return $str; |
207 | 207 | } |
208 | 208 | echo $str; |
@@ -213,24 +213,24 @@ discard block |
||
213 | 213 | * @see Html::table |
214 | 214 | * @return string|null |
215 | 215 | */ |
216 | - protected static function buildTableHeader(array $headers, $attributes = array()){ |
|
216 | + protected static function buildTableHeader(array $headers, $attributes = array()) { |
|
217 | 217 | $str = null; |
218 | 218 | $theadAttributes = ''; |
219 | - if(! empty($attributes['thead'])){ |
|
219 | + if (!empty($attributes['thead'])) { |
|
220 | 220 | $theadAttributes = ' ' . attributes_to_string($attributes['thead']); |
221 | 221 | } |
222 | 222 | $theadtrAttributes = ''; |
223 | - if(! empty($attributes['thead_tr'])){ |
|
223 | + if (!empty($attributes['thead_tr'])) { |
|
224 | 224 | $theadtrAttributes = ' ' . attributes_to_string($attributes['thead_tr']); |
225 | 225 | } |
226 | 226 | $thAttributes = ''; |
227 | - if(! empty($attributes['thead_th'])){ |
|
227 | + if (!empty($attributes['thead_th'])) { |
|
228 | 228 | $thAttributes = ' ' . attributes_to_string($attributes['thead_th']); |
229 | 229 | } |
230 | - $str .= '<thead' . $theadAttributes .'>'; |
|
231 | - $str .= '<tr' . $theadtrAttributes .'>'; |
|
230 | + $str .= '<thead' . $theadAttributes . '>'; |
|
231 | + $str .= '<tr' . $theadtrAttributes . '>'; |
|
232 | 232 | foreach ($headers as $value) { |
233 | - $str .= '<th' . $thAttributes .'>' .$value. '</th>'; |
|
233 | + $str .= '<th' . $thAttributes . '>' . $value . '</th>'; |
|
234 | 234 | } |
235 | 235 | $str .= '</tr>'; |
236 | 236 | $str .= '</thead>'; |
@@ -242,21 +242,21 @@ discard block |
||
242 | 242 | * @see Html::table |
243 | 243 | * @return string|null |
244 | 244 | */ |
245 | - protected static function buildTableBody(array $body, $attributes = array()){ |
|
245 | + protected static function buildTableBody(array $body, $attributes = array()) { |
|
246 | 246 | $str = null; |
247 | 247 | $tbodyAttributes = ''; |
248 | - if(! empty($attributes['tbody'])){ |
|
248 | + if (!empty($attributes['tbody'])) { |
|
249 | 249 | $tbodyAttributes = ' ' . attributes_to_string($attributes['tbody']); |
250 | 250 | } |
251 | 251 | $tbodytrAttributes = ''; |
252 | - if(! empty($attributes['tbody_tr'])){ |
|
252 | + if (!empty($attributes['tbody_tr'])) { |
|
253 | 253 | $tbodytrAttributes = ' ' . attributes_to_string($attributes['tbody_tr']); |
254 | 254 | } |
255 | 255 | $tbodytdAttributes = ''; |
256 | - if(! empty($attributes['tbody_td'])){ |
|
256 | + if (!empty($attributes['tbody_td'])) { |
|
257 | 257 | $tbodytdAttributes = ' ' . attributes_to_string($attributes['tbody_td']); |
258 | 258 | } |
259 | - $str .= '<tbody' . $tbodyAttributes .'>'; |
|
259 | + $str .= '<tbody' . $tbodyAttributes . '>'; |
|
260 | 260 | $str .= self::buildTableBodyContent($body, $tbodytrAttributes, $tbodytdAttributes); |
261 | 261 | $str .= '</tbody>'; |
262 | 262 | return $str; |
@@ -269,13 +269,13 @@ discard block |
||
269 | 269 | * @param string $tbodytdAttributes the html attributes for each td in tbody |
270 | 270 | * @return string |
271 | 271 | */ |
272 | - protected static function buildTableBodyContent(array $body, $tbodytrAttributes, $tbodytdAttributes){ |
|
272 | + protected static function buildTableBodyContent(array $body, $tbodytrAttributes, $tbodytdAttributes) { |
|
273 | 273 | $str = null; |
274 | 274 | foreach ($body as $row) { |
275 | - if(is_array($row)){ |
|
276 | - $str .= '<tr' . $tbodytrAttributes .'>'; |
|
275 | + if (is_array($row)) { |
|
276 | + $str .= '<tr' . $tbodytrAttributes . '>'; |
|
277 | 277 | foreach ($row as $value) { |
278 | - $str .= '<td' . $tbodytdAttributes .'>' .$value. '</td>'; |
|
278 | + $str .= '<td' . $tbodytdAttributes . '>' . $value . '</td>'; |
|
279 | 279 | } |
280 | 280 | $str .= '</tr>'; |
281 | 281 | } |
@@ -288,24 +288,24 @@ discard block |
||
288 | 288 | * @see Html::table |
289 | 289 | * @return string|null |
290 | 290 | */ |
291 | - protected static function buildTableFooter(array $footers, $attributes = array()){ |
|
291 | + protected static function buildTableFooter(array $footers, $attributes = array()) { |
|
292 | 292 | $str = null; |
293 | 293 | $tfootAttributes = ''; |
294 | - if(! empty($attributes['tfoot'])){ |
|
294 | + if (!empty($attributes['tfoot'])) { |
|
295 | 295 | $tfootAttributes = ' ' . attributes_to_string($attributes['tfoot']); |
296 | 296 | } |
297 | 297 | $tfoottrAttributes = ''; |
298 | - if(! empty($attributes['tfoot_tr'])){ |
|
298 | + if (!empty($attributes['tfoot_tr'])) { |
|
299 | 299 | $tfoottrAttributes = ' ' . attributes_to_string($attributes['tfoot_tr']); |
300 | 300 | } |
301 | 301 | $thAttributes = ''; |
302 | - if(! empty($attributes['tfoot_th'])){ |
|
302 | + if (!empty($attributes['tfoot_th'])) { |
|
303 | 303 | $thAttributes = ' ' . attributes_to_string($attributes['tfoot_th']); |
304 | 304 | } |
305 | - $str .= '<tfoot' . $tfootAttributes .'>'; |
|
306 | - $str .= '<tr' . $tfoottrAttributes .'>'; |
|
305 | + $str .= '<tfoot' . $tfootAttributes . '>'; |
|
306 | + $str .= '<tr' . $tfoottrAttributes . '>'; |
|
307 | 307 | foreach ($footers as $value) { |
308 | - $str .= '<th' . $thAttributes .'>' .$value. '</th>'; |
|
308 | + $str .= '<th' . $thAttributes . '>' . $value . '</th>'; |
|
309 | 309 | } |
310 | 310 | $str .= '</tr>'; |
311 | 311 | $str .= '</tfoot>'; |
@@ -319,23 +319,23 @@ discard block |
||
319 | 319 | * @param string $olul the type 'ol' or 'ul' |
320 | 320 | * @return void|string |
321 | 321 | */ |
322 | - protected static function buildUlOl($data = array(), $attributes = array(), $return = true, $olul = 'ul'){ |
|
322 | + protected static function buildUlOl($data = array(), $attributes = array(), $return = true, $olul = 'ul') { |
|
323 | 323 | $data = (array) $data; |
324 | 324 | $str = null; |
325 | 325 | $olulAttributes = ''; |
326 | - if(! empty($attributes[$olul])){ |
|
326 | + if (!empty($attributes[$olul])) { |
|
327 | 327 | $olulAttributes = ' ' . attributes_to_string($attributes[$olul]); |
328 | 328 | } |
329 | 329 | $liAttributes = ''; |
330 | - if(! empty($attributes['li'])){ |
|
330 | + if (!empty($attributes['li'])) { |
|
331 | 331 | $liAttributes = ' ' . attributes_to_string($attributes['li']); |
332 | 332 | } |
333 | 333 | $str .= '<' . $olul . $olulAttributes . '>'; |
334 | 334 | foreach ($data as $row) { |
335 | - $str .= '<li' . $liAttributes .'>' .$row. '</li>'; |
|
335 | + $str .= '<li' . $liAttributes . '>' . $row . '</li>'; |
|
336 | 336 | } |
337 | 337 | $str .= '</' . $olul . '>'; |
338 | - if($return){ |
|
338 | + if ($return) { |
|
339 | 339 | return $str; |
340 | 340 | } |
341 | 341 | echo $str; |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | * You should have received a copy of the GNU General Public License |
23 | 23 | * along with this program; if not, write to the Free Software |
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | - */ |
|
25 | + */ |
|
26 | 26 | |
27 | 27 | |
28 | 28 | /** |
@@ -120,10 +120,10 @@ discard block |
||
120 | 120 | protected $_temporary_return_type = NULL; |
121 | 121 | |
122 | 122 | |
123 | - /** |
|
123 | + /** |
|
124 | 124 | The database cache time |
125 | - */ |
|
126 | - protected $dbCacheTime = 0; |
|
125 | + */ |
|
126 | + protected $dbCacheTime = 0; |
|
127 | 127 | |
128 | 128 | /** |
129 | 129 | * Instance of the Loader class |
@@ -151,12 +151,12 @@ discard block |
||
151 | 151 | } |
152 | 152 | else{ |
153 | 153 | $obj = & get_instance(); |
154 | - if (isset($obj->database) && is_object($obj->database)){ |
|
154 | + if (isset($obj->database) && is_object($obj->database)){ |
|
155 | 155 | /** |
156 | - * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
157 | - * to prevent duplication |
|
158 | - */ |
|
159 | - $this->setDatabaseInstance(clone $obj->database); |
|
156 | + * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
|
157 | + * to prevent duplication |
|
158 | + */ |
|
159 | + $this->setDatabaseInstance(clone $obj->database); |
|
160 | 160 | } |
161 | 161 | } |
162 | 162 | |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | */ |
175 | 175 | public function get($primary_value) |
176 | 176 | { |
177 | - return $this->get_by($this->primary_key, $primary_value); |
|
177 | + return $this->get_by($this->primary_key, $primary_value); |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | /** |
@@ -189,12 +189,12 @@ discard block |
||
189 | 189 | { |
190 | 190 | $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
191 | 191 | } |
192 | - $this->_set_where($where); |
|
192 | + $this->_set_where($where); |
|
193 | 193 | |
194 | 194 | $this->trigger('before_get'); |
195 | - $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
|
195 | + $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
|
196 | 196 | $this->getQueryBuilder()->from($this->_table); |
197 | - $row = $this->_database->get($type); |
|
197 | + $row = $this->_database->get($type); |
|
198 | 198 | $this->_temporary_return_type = $this->return_type; |
199 | 199 | $row = $this->trigger('after_get', $row); |
200 | 200 | $this->_with = array(); |
@@ -231,9 +231,9 @@ discard block |
||
231 | 231 | { |
232 | 232 | $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
233 | 233 | } |
234 | - $type = $this->_temporary_return_type == 'array' ? 'array':false; |
|
234 | + $type = $this->_temporary_return_type == 'array' ? 'array':false; |
|
235 | 235 | $this->getQueryBuilder()->from($this->_table); |
236 | - $result = $this->_database->getAll($type); |
|
236 | + $result = $this->_database->getAll($type); |
|
237 | 237 | $this->_temporary_return_type = $this->return_type; |
238 | 238 | |
239 | 239 | foreach ($result as $key => &$row) |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | /** |
248 | 248 | * Insert a new row into the table. $data should be an associative array |
249 | 249 | * of data to be inserted. Returns newly created ID. |
250 | - * @see Database::insert |
|
250 | + * @see Database::insert |
|
251 | 251 | */ |
252 | 252 | public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
253 | 253 | { |
@@ -260,11 +260,11 @@ discard block |
||
260 | 260 | { |
261 | 261 | $data = $this->trigger('before_create', $data); |
262 | 262 | $this->getQueryBuilder()->from($this->_table); |
263 | - $this->_database->insert($data, $escape); |
|
263 | + $this->_database->insert($data, $escape); |
|
264 | 264 | $insert_id = $this->_database->insertId(); |
265 | 265 | $this->trigger('after_create', $insert_id); |
266 | - //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
267 | - return ! $insert_id ? true : $insert_id; |
|
266 | + //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
|
267 | + return ! $insert_id ? true : $insert_id; |
|
268 | 268 | } |
269 | 269 | else |
270 | 270 | { |
@@ -324,7 +324,7 @@ discard block |
||
324 | 324 | { |
325 | 325 | $this->getQueryBuilder()->in($this->primary_key, $primary_values) |
326 | 326 | ->from($this->_table); |
327 | - $result = $this->_database->update($data, $escape); |
|
327 | + $result = $this->_database->update($data, $escape); |
|
328 | 328 | $this->trigger('after_update', array($data, $result)); |
329 | 329 | return $result; |
330 | 330 | } |
@@ -356,7 +356,7 @@ discard block |
||
356 | 356 | { |
357 | 357 | $this->_set_where($args); |
358 | 358 | $this->getQueryBuilder()->from($this->_table); |
359 | - $result = $this->_database->update($data); |
|
359 | + $result = $this->_database->update($data); |
|
360 | 360 | $this->trigger('after_update', array($data, $result)); |
361 | 361 | return $result; |
362 | 362 | } |
@@ -370,7 +370,7 @@ discard block |
||
370 | 370 | { |
371 | 371 | $data = $this->trigger('before_update', $data); |
372 | 372 | $this->getQueryBuilder()->from($this->_table); |
373 | - $result = $this->_database->update($data, $escape); |
|
373 | + $result = $this->_database->update($data, $escape); |
|
374 | 374 | $this->trigger('after_update', array($data, $result)); |
375 | 375 | return $result; |
376 | 376 | } |
@@ -382,16 +382,16 @@ discard block |
||
382 | 382 | { |
383 | 383 | $this->trigger('before_delete', $id); |
384 | 384 | $this->getQueryBuilder()->where($this->primary_key, $id); |
385 | - $result = false; |
|
385 | + $result = false; |
|
386 | 386 | if ($this->soft_delete) |
387 | 387 | { |
388 | 388 | $this->getQueryBuilder()->from($this->_table); |
389 | - $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
389 | + $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
390 | 390 | } |
391 | 391 | else |
392 | 392 | { |
393 | 393 | $this->getQueryBuilder()->from($this->_table); |
394 | - $result = $this->_database->delete(); |
|
394 | + $result = $this->_database->delete(); |
|
395 | 395 | } |
396 | 396 | |
397 | 397 | $this->trigger('after_delete', $result); |
@@ -404,18 +404,18 @@ discard block |
||
404 | 404 | public function delete_by() |
405 | 405 | { |
406 | 406 | $where = func_get_args(); |
407 | - $where = $this->trigger('before_delete', $where); |
|
407 | + $where = $this->trigger('before_delete', $where); |
|
408 | 408 | $this->_set_where($where); |
409 | - $result = false; |
|
409 | + $result = false; |
|
410 | 410 | if ($this->soft_delete) |
411 | 411 | { |
412 | 412 | $this->getQueryBuilder()->from($this->_table); |
413 | - $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
413 | + $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
414 | 414 | } |
415 | 415 | else |
416 | 416 | { |
417 | 417 | $this->getQueryBuilder()->from($this->_table); |
418 | - $result = $this->_database->delete(); |
|
418 | + $result = $this->_database->delete(); |
|
419 | 419 | } |
420 | 420 | $this->trigger('after_delete', $result); |
421 | 421 | return $result; |
@@ -428,16 +428,16 @@ discard block |
||
428 | 428 | { |
429 | 429 | $primary_values = $this->trigger('before_delete', $primary_values); |
430 | 430 | $this->getQueryBuilder()->in($this->primary_key, $primary_values); |
431 | - $result = false; |
|
431 | + $result = false; |
|
432 | 432 | if ($this->soft_delete) |
433 | 433 | { |
434 | 434 | $this->getQueryBuilder()->from($this->_table); |
435 | - $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
435 | + $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
436 | 436 | } |
437 | 437 | else |
438 | 438 | { |
439 | 439 | $this->getQueryBuilder()->from($this->_table); |
440 | - $result = $this->_database->delete(); |
|
440 | + $result = $this->_database->delete(); |
|
441 | 441 | } |
442 | 442 | $this->trigger('after_delete', $result); |
443 | 443 | return $result; |
@@ -449,8 +449,8 @@ discard block |
||
449 | 449 | */ |
450 | 450 | public function truncate() |
451 | 451 | { |
452 | - $this->getQueryBuilder()->from($this->_table); |
|
453 | - $result = $this->_database->delete(); |
|
452 | + $this->getQueryBuilder()->from($this->_table); |
|
453 | + $result = $this->_database->delete(); |
|
454 | 454 | return $result; |
455 | 455 | } |
456 | 456 | |
@@ -468,14 +468,14 @@ discard block |
||
468 | 468 | return $this; |
469 | 469 | } |
470 | 470 | |
471 | - /** |
|
472 | - * Relationship |
|
473 | - */ |
|
471 | + /** |
|
472 | + * Relationship |
|
473 | + */ |
|
474 | 474 | public function relate($row) |
475 | 475 | { |
476 | - if (empty($row)) |
|
476 | + if (empty($row)) |
|
477 | 477 | { |
478 | - return $row; |
|
478 | + return $row; |
|
479 | 479 | } |
480 | 480 | |
481 | 481 | $row = $this->relateBelongsTo($row); |
@@ -508,9 +508,9 @@ discard block |
||
508 | 508 | $this->getQueryBuilder()->where($this->soft_delete_key, FALSE); |
509 | 509 | } |
510 | 510 | $this->getQueryBuilder() |
511 | - ->select(array($key, $value)) |
|
512 | - ->from($this->_table); |
|
513 | - $result = $this->_database->getAll(); |
|
511 | + ->select(array($key, $value)) |
|
512 | + ->from($this->_table); |
|
513 | + $result = $this->_database->getAll(); |
|
514 | 514 | $options = array(); |
515 | 515 | foreach ($result as $row) |
516 | 516 | { |
@@ -532,7 +532,7 @@ discard block |
||
532 | 532 | $where = func_get_args(); |
533 | 533 | $this->_set_where($where); |
534 | 534 | $this->getQueryBuilder()->from($this->_table); |
535 | - $this->_database->getAll(); |
|
535 | + $this->_database->getAll(); |
|
536 | 536 | return $this->_database->numRows(); |
537 | 537 | } |
538 | 538 | |
@@ -545,20 +545,20 @@ discard block |
||
545 | 545 | { |
546 | 546 | $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
547 | 547 | } |
548 | - $this->getQueryBuilder()->from($this->_table); |
|
549 | - $this->_database->getAll(); |
|
548 | + $this->getQueryBuilder()->from($this->_table); |
|
549 | + $this->_database->getAll(); |
|
550 | 550 | return $this->_database->numRows(); |
551 | 551 | } |
552 | 552 | |
553 | - /** |
|
554 | - * Enabled cache temporary |
|
555 | - */ |
|
556 | - public function cached($ttl = 0){ |
|
557 | - if ($ttl > 0){ |
|
558 | - $this->_database = $this->_database->cached($ttl); |
|
559 | - } |
|
560 | - return $this; |
|
561 | - } |
|
553 | + /** |
|
554 | + * Enabled cache temporary |
|
555 | + */ |
|
556 | + public function cached($ttl = 0){ |
|
557 | + if ($ttl > 0){ |
|
558 | + $this->_database = $this->_database->cached($ttl); |
|
559 | + } |
|
560 | + return $this; |
|
561 | + } |
|
562 | 562 | |
563 | 563 | /** |
564 | 564 | * Tell the class to skip the insert validation |
@@ -582,10 +582,10 @@ discard block |
||
582 | 582 | */ |
583 | 583 | public function get_next_id() |
584 | 584 | { |
585 | - $this->getQueryBuilder()->select('AUTO_INCREMENT') |
|
586 | - ->from('information_schema.TABLES') |
|
587 | - ->where('TABLE_NAME', $this->_table) |
|
588 | - ->where('TABLE_SCHEMA', $this->_database->getDatabaseName()); |
|
585 | + $this->getQueryBuilder()->select('AUTO_INCREMENT') |
|
586 | + ->from('information_schema.TABLES') |
|
587 | + ->where('TABLE_NAME', $this->_table) |
|
588 | + ->where('TABLE_SCHEMA', $this->_database->getDatabaseName()); |
|
589 | 589 | return (int) $this->_database->get()->AUTO_INCREMENT; |
590 | 590 | } |
591 | 591 | |
@@ -708,24 +708,24 @@ discard block |
||
708 | 708 | { |
709 | 709 | if (is_object($row)) |
710 | 710 | { |
711 | - if (isset($row->$attr)){ |
|
712 | - unset($row->$attr); |
|
713 | - } |
|
711 | + if (isset($row->$attr)){ |
|
712 | + unset($row->$attr); |
|
713 | + } |
|
714 | 714 | } |
715 | 715 | else |
716 | 716 | { |
717 | - if (isset($row[$attr])){ |
|
718 | - unset($row[$attr]); |
|
719 | - } |
|
717 | + if (isset($row[$attr])){ |
|
718 | + unset($row[$attr]); |
|
719 | + } |
|
720 | 720 | } |
721 | 721 | } |
722 | 722 | return $row; |
723 | 723 | } |
724 | 724 | |
725 | - /** |
|
726 | - * Return the database instance |
|
727 | - * @return Database the database instance |
|
728 | - */ |
|
725 | + /** |
|
726 | + * Return the database instance |
|
727 | + * @return Database the database instance |
|
728 | + */ |
|
729 | 729 | public function getDatabaseInstance(){ |
730 | 730 | return $this->_database; |
731 | 731 | } |
@@ -734,7 +734,7 @@ discard block |
||
734 | 734 | * set the Database instance for future use |
735 | 735 | * @param Database $db the database object |
736 | 736 | */ |
737 | - public function setDatabaseInstance($db){ |
|
737 | + public function setDatabaseInstance($db){ |
|
738 | 738 | $this->_database = $db; |
739 | 739 | if ($this->dbCacheTime > 0){ |
740 | 740 | $this->_database->setCache($this->dbCacheTime); |
@@ -753,14 +753,14 @@ discard block |
||
753 | 753 | /** |
754 | 754 | * Set the loader instance for future use |
755 | 755 | * @param Loader $loader the loader object |
756 | - * @return object |
|
756 | + * @return object |
|
757 | 757 | */ |
758 | - public function setLoader($loader){ |
|
758 | + public function setLoader($loader){ |
|
759 | 759 | $this->loaderInstance = $loader; |
760 | 760 | return $this; |
761 | 761 | } |
762 | 762 | |
763 | - /** |
|
763 | + /** |
|
764 | 764 | * Return the queryBuilder instance this is the shortcut to database queryBuilder |
765 | 765 | * @return object the DatabaseQueryBuilder instance |
766 | 766 | */ |
@@ -771,9 +771,9 @@ discard block |
||
771 | 771 | /** |
772 | 772 | * Set the DatabaseQueryBuilder instance for future use |
773 | 773 | * @param object $queryBuilder the DatabaseQueryBuilder object |
774 | - * @return object |
|
774 | + * @return object |
|
775 | 775 | */ |
776 | - public function setQueryBuilder($queryBuilder){ |
|
776 | + public function setQueryBuilder($queryBuilder){ |
|
777 | 777 | $this->_database->setQueryBuilder($queryBuilder); |
778 | 778 | return $this; |
779 | 779 | } |
@@ -790,9 +790,9 @@ discard block |
||
790 | 790 | /** |
791 | 791 | * Set the form validation instance for future use |
792 | 792 | * @param FormValidation $fv the form validation object |
793 | - * @return object |
|
793 | + * @return object |
|
794 | 794 | */ |
795 | - public function setFormValidation($fv){ |
|
795 | + public function setFormValidation($fv){ |
|
796 | 796 | $this->formValidationInstance = $fv; |
797 | 797 | return $this; |
798 | 798 | } |
@@ -833,12 +833,12 @@ discard block |
||
833 | 833 | * INTERNAL METHODS |
834 | 834 | * ------------------------------------------------------------ */ |
835 | 835 | |
836 | - /** |
|
837 | - * relate for the relation "belongs_to" |
|
838 | - * @return mixed |
|
839 | - */ |
|
840 | - protected function relateBelongsTo($row){ |
|
841 | - foreach ($this->belongs_to as $key => $value) |
|
836 | + /** |
|
837 | + * relate for the relation "belongs_to" |
|
838 | + * @return mixed |
|
839 | + */ |
|
840 | + protected function relateBelongsTo($row){ |
|
841 | + foreach ($this->belongs_to as $key => $value) |
|
842 | 842 | { |
843 | 843 | if (is_string($value)) |
844 | 844 | { |
@@ -869,15 +869,15 @@ discard block |
||
869 | 869 | } |
870 | 870 | } |
871 | 871 | } |
872 | - return $row; |
|
873 | - } |
|
874 | - |
|
875 | - /** |
|
876 | - * relate for the relation "has_many" |
|
877 | - * @return mixed |
|
878 | - */ |
|
879 | - protected function relateHasMany($row){ |
|
880 | - foreach ($this->has_many as $key => $value) |
|
872 | + return $row; |
|
873 | + } |
|
874 | + |
|
875 | + /** |
|
876 | + * relate for the relation "has_many" |
|
877 | + * @return mixed |
|
878 | + */ |
|
879 | + protected function relateHasMany($row){ |
|
880 | + foreach ($this->has_many as $key => $value) |
|
881 | 881 | { |
882 | 882 | if (is_string($value)) |
883 | 883 | { |
@@ -908,8 +908,8 @@ discard block |
||
908 | 908 | } |
909 | 909 | } |
910 | 910 | } |
911 | - return $row; |
|
912 | - } |
|
911 | + return $row; |
|
912 | + } |
|
913 | 913 | |
914 | 914 | /** |
915 | 915 | * Trigger an event and call its observers. Pass through the event name |
@@ -945,7 +945,7 @@ discard block |
||
945 | 945 | } |
946 | 946 | $fv = $this->formValidationInstance; |
947 | 947 | if (! is_object($fv)){ |
948 | - Loader::library('FormValidation'); |
|
948 | + Loader::library('FormValidation'); |
|
949 | 949 | $fv = $this->formvalidation; |
950 | 950 | $this->setFormValidation($fv); |
951 | 951 | } |
@@ -960,30 +960,30 @@ discard block |
||
960 | 960 | } |
961 | 961 | |
962 | 962 | |
963 | - /** |
|
964 | - * Set WHERE parameters, when is array |
|
965 | - * @param array $params |
|
966 | - */ |
|
967 | - protected function _set_where_array(array $params){ |
|
968 | - foreach ($params as $field => $filter) |
|
969 | - { |
|
970 | - if (is_array($filter)) |
|
971 | - { |
|
972 | - $this->getQueryBuilder()->in($field, $filter); |
|
973 | - } |
|
974 | - else |
|
975 | - { |
|
976 | - if (is_int($field)) |
|
977 | - { |
|
978 | - $this->getQueryBuilder()->where($filter); |
|
979 | - } |
|
980 | - else |
|
981 | - { |
|
982 | - $this->getQueryBuilder()->where($field, $filter); |
|
983 | - } |
|
984 | - } |
|
985 | - } |
|
986 | - } |
|
963 | + /** |
|
964 | + * Set WHERE parameters, when is array |
|
965 | + * @param array $params |
|
966 | + */ |
|
967 | + protected function _set_where_array(array $params){ |
|
968 | + foreach ($params as $field => $filter) |
|
969 | + { |
|
970 | + if (is_array($filter)) |
|
971 | + { |
|
972 | + $this->getQueryBuilder()->in($field, $filter); |
|
973 | + } |
|
974 | + else |
|
975 | + { |
|
976 | + if (is_int($field)) |
|
977 | + { |
|
978 | + $this->getQueryBuilder()->where($filter); |
|
979 | + } |
|
980 | + else |
|
981 | + { |
|
982 | + $this->getQueryBuilder()->where($field, $filter); |
|
983 | + } |
|
984 | + } |
|
985 | + } |
|
986 | + } |
|
987 | 987 | |
988 | 988 | |
989 | 989 | /** |
@@ -999,8 +999,8 @@ discard block |
||
999 | 999 | { |
1000 | 1000 | $this->getQueryBuilder()->where($params[0]); |
1001 | 1001 | } |
1002 | - else if (count($params) == 2) |
|
1003 | - { |
|
1002 | + else if (count($params) == 2) |
|
1003 | + { |
|
1004 | 1004 | if (is_array($params[1])) |
1005 | 1005 | { |
1006 | 1006 | $this->getQueryBuilder()->in($params[0], $params[1]); |
@@ -1009,11 +1009,11 @@ discard block |
||
1009 | 1009 | { |
1010 | 1010 | $this->getQueryBuilder()->where($params[0], $params[1]); |
1011 | 1011 | } |
1012 | - } |
|
1013 | - else if (count($params) == 3) |
|
1014 | - { |
|
1015 | - $this->getQueryBuilder()->where($params[0], $params[1], $params[2]); |
|
1016 | - } |
|
1012 | + } |
|
1013 | + else if (count($params) == 3) |
|
1014 | + { |
|
1015 | + $this->getQueryBuilder()->where($params[0], $params[1], $params[2]); |
|
1016 | + } |
|
1017 | 1017 | else |
1018 | 1018 | { |
1019 | 1019 | if (is_array($params[1])) |
@@ -1029,7 +1029,7 @@ discard block |
||
1029 | 1029 | |
1030 | 1030 | /** |
1031 | 1031 | Shortcut to controller |
1032 | - */ |
|
1032 | + */ |
|
1033 | 1033 | public function __get($key){ |
1034 | 1034 | return get_instance()->{$key}; |
1035 | 1035 | } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | * @copyright Copyright (c) 2012, Jamie Rumbelow <http://jamierumbelow.net> |
34 | 34 | */ |
35 | 35 | |
36 | - class Model{ |
|
36 | + class Model { |
|
37 | 37 | |
38 | 38 | /* -------------------------------------------------------------- |
39 | 39 | * VARIABLES |
@@ -145,13 +145,13 @@ discard block |
||
145 | 145 | * Initialise the model, tie into the CodeIgniter superobject and |
146 | 146 | * try our best to guess the table name. |
147 | 147 | */ |
148 | - public function __construct(Database $db = null){ |
|
149 | - if (is_object($db)){ |
|
148 | + public function __construct(Database $db = null) { |
|
149 | + if (is_object($db)) { |
|
150 | 150 | $this->setDatabaseInstance($db); |
151 | 151 | } |
152 | - else{ |
|
152 | + else { |
|
153 | 153 | $obj = & get_instance(); |
154 | - if (isset($obj->database) && is_object($obj->database)){ |
|
154 | + if (isset($obj->database) && is_object($obj->database)) { |
|
155 | 155 | /** |
156 | 156 | * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
157 | 157 | * to prevent duplication |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | |
188 | 188 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
189 | 189 | { |
190 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
190 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
191 | 191 | } |
192 | 192 | $this->_set_where($where); |
193 | 193 | |
@@ -229,9 +229,9 @@ discard block |
||
229 | 229 | $this->trigger('before_get'); |
230 | 230 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
231 | 231 | { |
232 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
232 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
233 | 233 | } |
234 | - $type = $this->_temporary_return_type == 'array' ? 'array':false; |
|
234 | + $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
|
235 | 235 | $this->getQueryBuilder()->from($this->_table); |
236 | 236 | $result = $this->_database->getAll($type); |
237 | 237 | $this->_temporary_return_type = $this->return_type; |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | $insert_id = $this->_database->insertId(); |
265 | 265 | $this->trigger('after_create', $insert_id); |
266 | 266 | //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
267 | - return ! $insert_id ? true : $insert_id; |
|
267 | + return !$insert_id ? true : $insert_id; |
|
268 | 268 | } |
269 | 269 | else |
270 | 270 | { |
@@ -341,13 +341,13 @@ discard block |
||
341 | 341 | { |
342 | 342 | $args = func_get_args(); |
343 | 343 | $data = array(); |
344 | - if (count($args) == 2){ |
|
345 | - if (is_array($args[1])){ |
|
344 | + if (count($args) == 2) { |
|
345 | + if (is_array($args[1])) { |
|
346 | 346 | $data = array_pop($args); |
347 | 347 | } |
348 | 348 | } |
349 | - else if (count($args) == 3){ |
|
350 | - if (is_array($args[2])){ |
|
349 | + else if (count($args) == 3) { |
|
350 | + if (is_array($args[2])) { |
|
351 | 351 | $data = array_pop($args); |
352 | 352 | } |
353 | 353 | } |
@@ -386,7 +386,7 @@ discard block |
||
386 | 386 | if ($this->soft_delete) |
387 | 387 | { |
388 | 388 | $this->getQueryBuilder()->from($this->_table); |
389 | - $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
389 | + $result = $this->_database->update(array($this->soft_delete_key => TRUE)); |
|
390 | 390 | } |
391 | 391 | else |
392 | 392 | { |
@@ -410,7 +410,7 @@ discard block |
||
410 | 410 | if ($this->soft_delete) |
411 | 411 | { |
412 | 412 | $this->getQueryBuilder()->from($this->_table); |
413 | - $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
413 | + $result = $this->_database->update(array($this->soft_delete_key => TRUE)); |
|
414 | 414 | } |
415 | 415 | else |
416 | 416 | { |
@@ -432,7 +432,7 @@ discard block |
||
432 | 432 | if ($this->soft_delete) |
433 | 433 | { |
434 | 434 | $this->getQueryBuilder()->from($this->_table); |
435 | - $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
|
435 | + $result = $this->_database->update(array($this->soft_delete_key => TRUE)); |
|
436 | 436 | } |
437 | 437 | else |
438 | 438 | { |
@@ -502,7 +502,7 @@ discard block |
||
502 | 502 | $key = $this->primary_key; |
503 | 503 | $value = $args[0]; |
504 | 504 | } |
505 | - $this->trigger('before_dropdown', array( $key, $value )); |
|
505 | + $this->trigger('before_dropdown', array($key, $value)); |
|
506 | 506 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
507 | 507 | { |
508 | 508 | $this->getQueryBuilder()->where($this->soft_delete_key, FALSE); |
@@ -527,7 +527,7 @@ discard block |
||
527 | 527 | { |
528 | 528 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
529 | 529 | { |
530 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
530 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
531 | 531 | } |
532 | 532 | $where = func_get_args(); |
533 | 533 | $this->_set_where($where); |
@@ -543,7 +543,7 @@ discard block |
||
543 | 543 | { |
544 | 544 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
545 | 545 | { |
546 | - $this->getQueryBuilder()->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
|
546 | + $this->getQueryBuilder()->where($this->soft_delete_key, (bool) $this->_temporary_only_deleted); |
|
547 | 547 | } |
548 | 548 | $this->getQueryBuilder()->from($this->_table); |
549 | 549 | $this->_database->getAll(); |
@@ -553,8 +553,8 @@ discard block |
||
553 | 553 | /** |
554 | 554 | * Enabled cache temporary |
555 | 555 | */ |
556 | - public function cached($ttl = 0){ |
|
557 | - if ($ttl > 0){ |
|
556 | + public function cached($ttl = 0) { |
|
557 | + if ($ttl > 0) { |
|
558 | 558 | $this->_database = $this->_database->cached($ttl); |
559 | 559 | } |
560 | 560 | return $this; |
@@ -708,13 +708,13 @@ discard block |
||
708 | 708 | { |
709 | 709 | if (is_object($row)) |
710 | 710 | { |
711 | - if (isset($row->$attr)){ |
|
711 | + if (isset($row->$attr)) { |
|
712 | 712 | unset($row->$attr); |
713 | 713 | } |
714 | 714 | } |
715 | 715 | else |
716 | 716 | { |
717 | - if (isset($row[$attr])){ |
|
717 | + if (isset($row[$attr])) { |
|
718 | 718 | unset($row[$attr]); |
719 | 719 | } |
720 | 720 | } |
@@ -726,7 +726,7 @@ discard block |
||
726 | 726 | * Return the database instance |
727 | 727 | * @return Database the database instance |
728 | 728 | */ |
729 | - public function getDatabaseInstance(){ |
|
729 | + public function getDatabaseInstance() { |
|
730 | 730 | return $this->_database; |
731 | 731 | } |
732 | 732 | |
@@ -734,9 +734,9 @@ discard block |
||
734 | 734 | * set the Database instance for future use |
735 | 735 | * @param Database $db the database object |
736 | 736 | */ |
737 | - public function setDatabaseInstance($db){ |
|
737 | + public function setDatabaseInstance($db) { |
|
738 | 738 | $this->_database = $db; |
739 | - if ($this->dbCacheTime > 0){ |
|
739 | + if ($this->dbCacheTime > 0) { |
|
740 | 740 | $this->_database->setCache($this->dbCacheTime); |
741 | 741 | } |
742 | 742 | return $this; |
@@ -746,7 +746,7 @@ discard block |
||
746 | 746 | * Return the loader instance |
747 | 747 | * @return Loader the loader instance |
748 | 748 | */ |
749 | - public function getLoader(){ |
|
749 | + public function getLoader() { |
|
750 | 750 | return $this->loaderInstance; |
751 | 751 | } |
752 | 752 | |
@@ -755,7 +755,7 @@ discard block |
||
755 | 755 | * @param Loader $loader the loader object |
756 | 756 | * @return object |
757 | 757 | */ |
758 | - public function setLoader($loader){ |
|
758 | + public function setLoader($loader) { |
|
759 | 759 | $this->loaderInstance = $loader; |
760 | 760 | return $this; |
761 | 761 | } |
@@ -764,7 +764,7 @@ discard block |
||
764 | 764 | * Return the queryBuilder instance this is the shortcut to database queryBuilder |
765 | 765 | * @return object the DatabaseQueryBuilder instance |
766 | 766 | */ |
767 | - public function getQueryBuilder(){ |
|
767 | + public function getQueryBuilder() { |
|
768 | 768 | return $this->_database->getQueryBuilder(); |
769 | 769 | } |
770 | 770 | |
@@ -773,7 +773,7 @@ discard block |
||
773 | 773 | * @param object $queryBuilder the DatabaseQueryBuilder object |
774 | 774 | * @return object |
775 | 775 | */ |
776 | - public function setQueryBuilder($queryBuilder){ |
|
776 | + public function setQueryBuilder($queryBuilder) { |
|
777 | 777 | $this->_database->setQueryBuilder($queryBuilder); |
778 | 778 | return $this; |
779 | 779 | } |
@@ -783,7 +783,7 @@ discard block |
||
783 | 783 | * Return the FormValidation instance |
784 | 784 | * @return FormValidation the form validation instance |
785 | 785 | */ |
786 | - public function getFormValidation(){ |
|
786 | + public function getFormValidation() { |
|
787 | 787 | return $this->formValidationInstance; |
788 | 788 | } |
789 | 789 | |
@@ -792,7 +792,7 @@ discard block |
||
792 | 792 | * @param FormValidation $fv the form validation object |
793 | 793 | * @return object |
794 | 794 | */ |
795 | - public function setFormValidation($fv){ |
|
795 | + public function setFormValidation($fv) { |
|
796 | 796 | $this->formValidationInstance = $fv; |
797 | 797 | return $this; |
798 | 798 | } |
@@ -806,7 +806,7 @@ discard block |
||
806 | 806 | */ |
807 | 807 | public function order_by($criteria, $order = 'ASC') |
808 | 808 | { |
809 | - if ( is_array($criteria) ) |
|
809 | + if (is_array($criteria)) |
|
810 | 810 | { |
811 | 811 | foreach ($criteria as $key => $value) |
812 | 812 | { |
@@ -837,13 +837,13 @@ discard block |
||
837 | 837 | * relate for the relation "belongs_to" |
838 | 838 | * @return mixed |
839 | 839 | */ |
840 | - protected function relateBelongsTo($row){ |
|
840 | + protected function relateBelongsTo($row) { |
|
841 | 841 | foreach ($this->belongs_to as $key => $value) |
842 | 842 | { |
843 | 843 | if (is_string($value)) |
844 | 844 | { |
845 | 845 | $relationship = $value; |
846 | - $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
|
846 | + $options = array('primary_key' => $value . '_id', 'model' => $value . '_model'); |
|
847 | 847 | } |
848 | 848 | else |
849 | 849 | { |
@@ -853,10 +853,10 @@ discard block |
||
853 | 853 | |
854 | 854 | if (in_array($relationship, $this->_with)) |
855 | 855 | { |
856 | - if (is_object($this->loaderInstance)){ |
|
856 | + if (is_object($this->loaderInstance)) { |
|
857 | 857 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
858 | 858 | } |
859 | - else{ |
|
859 | + else { |
|
860 | 860 | Loader::model($options['model'], $relationship . '_model'); |
861 | 861 | } |
862 | 862 | if (is_object($row)) |
@@ -876,13 +876,13 @@ discard block |
||
876 | 876 | * relate for the relation "has_many" |
877 | 877 | * @return mixed |
878 | 878 | */ |
879 | - protected function relateHasMany($row){ |
|
879 | + protected function relateHasMany($row) { |
|
880 | 880 | foreach ($this->has_many as $key => $value) |
881 | 881 | { |
882 | 882 | if (is_string($value)) |
883 | 883 | { |
884 | 884 | $relationship = $value; |
885 | - $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
|
885 | + $options = array('primary_key' => $this->_table . '_id', 'model' => $value . '_model'); |
|
886 | 886 | } |
887 | 887 | else |
888 | 888 | { |
@@ -892,10 +892,10 @@ discard block |
||
892 | 892 | |
893 | 893 | if (in_array($relationship, $this->_with)) |
894 | 894 | { |
895 | - if (is_object($this->loaderInstance)){ |
|
895 | + if (is_object($this->loaderInstance)) { |
|
896 | 896 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
897 | 897 | } |
898 | - else{ |
|
898 | + else { |
|
899 | 899 | Loader::model($options['model'], $relationship . '_model'); |
900 | 900 | } |
901 | 901 | if (is_object($row)) |
@@ -944,7 +944,7 @@ discard block |
||
944 | 944 | return $data; |
945 | 945 | } |
946 | 946 | $fv = $this->formValidationInstance; |
947 | - if (! is_object($fv)){ |
|
947 | + if (!is_object($fv)) { |
|
948 | 948 | Loader::library('FormValidation'); |
949 | 949 | $fv = $this->formvalidation; |
950 | 950 | $this->setFormValidation($fv); |
@@ -964,7 +964,7 @@ discard block |
||
964 | 964 | * Set WHERE parameters, when is array |
965 | 965 | * @param array $params |
966 | 966 | */ |
967 | - protected function _set_where_array(array $params){ |
|
967 | + protected function _set_where_array(array $params) { |
|
968 | 968 | foreach ($params as $field => $filter) |
969 | 969 | { |
970 | 970 | if (is_array($filter)) |
@@ -1030,7 +1030,7 @@ discard block |
||
1030 | 1030 | /** |
1031 | 1031 | Shortcut to controller |
1032 | 1032 | */ |
1033 | - public function __get($key){ |
|
1033 | + public function __get($key) { |
|
1034 | 1034 | return get_instance()->{$key}; |
1035 | 1035 | } |
1036 | 1036 |
@@ -148,8 +148,7 @@ discard block |
||
148 | 148 | public function __construct(Database $db = null){ |
149 | 149 | if (is_object($db)){ |
150 | 150 | $this->setDatabaseInstance($db); |
151 | - } |
|
152 | - else{ |
|
151 | + } else{ |
|
153 | 152 | $obj = & get_instance(); |
154 | 153 | if (isset($obj->database) && is_object($obj->database)){ |
155 | 154 | /** |
@@ -265,8 +264,7 @@ discard block |
||
265 | 264 | $this->trigger('after_create', $insert_id); |
266 | 265 | //if the table doesn't have the auto increment field or sequence, the value of 0 will be returned |
267 | 266 | return ! $insert_id ? true : $insert_id; |
268 | - } |
|
269 | - else |
|
267 | + } else |
|
270 | 268 | { |
271 | 269 | return FALSE; |
272 | 270 | } |
@@ -303,8 +301,7 @@ discard block |
||
303 | 301 | $result = $this->_database->update($data, $escape); |
304 | 302 | $this->trigger('after_update', array($data, $result)); |
305 | 303 | return $result; |
306 | - } |
|
307 | - else |
|
304 | + } else |
|
308 | 305 | { |
309 | 306 | return FALSE; |
310 | 307 | } |
@@ -327,8 +324,7 @@ discard block |
||
327 | 324 | $result = $this->_database->update($data, $escape); |
328 | 325 | $this->trigger('after_update', array($data, $result)); |
329 | 326 | return $result; |
330 | - } |
|
331 | - else |
|
327 | + } else |
|
332 | 328 | { |
333 | 329 | return FALSE; |
334 | 330 | } |
@@ -345,8 +341,7 @@ discard block |
||
345 | 341 | if (is_array($args[1])){ |
346 | 342 | $data = array_pop($args); |
347 | 343 | } |
348 | - } |
|
349 | - else if (count($args) == 3){ |
|
344 | + } else if (count($args) == 3){ |
|
350 | 345 | if (is_array($args[2])){ |
351 | 346 | $data = array_pop($args); |
352 | 347 | } |
@@ -387,8 +382,7 @@ discard block |
||
387 | 382 | { |
388 | 383 | $this->getQueryBuilder()->from($this->_table); |
389 | 384 | $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
390 | - } |
|
391 | - else |
|
385 | + } else |
|
392 | 386 | { |
393 | 387 | $this->getQueryBuilder()->from($this->_table); |
394 | 388 | $result = $this->_database->delete(); |
@@ -411,8 +405,7 @@ discard block |
||
411 | 405 | { |
412 | 406 | $this->getQueryBuilder()->from($this->_table); |
413 | 407 | $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
414 | - } |
|
415 | - else |
|
408 | + } else |
|
416 | 409 | { |
417 | 410 | $this->getQueryBuilder()->from($this->_table); |
418 | 411 | $result = $this->_database->delete(); |
@@ -433,8 +426,7 @@ discard block |
||
433 | 426 | { |
434 | 427 | $this->getQueryBuilder()->from($this->_table); |
435 | 428 | $result = $this->_database->update(array( $this->soft_delete_key => TRUE )); |
436 | - } |
|
437 | - else |
|
429 | + } else |
|
438 | 430 | { |
439 | 431 | $this->getQueryBuilder()->from($this->_table); |
440 | 432 | $result = $this->_database->delete(); |
@@ -496,8 +488,7 @@ discard block |
||
496 | 488 | if (count($args) == 2) |
497 | 489 | { |
498 | 490 | list($key, $value) = $args; |
499 | - } |
|
500 | - else |
|
491 | + } else |
|
501 | 492 | { |
502 | 493 | $key = $this->primary_key; |
503 | 494 | $value = $args[0]; |
@@ -649,8 +640,7 @@ discard block |
||
649 | 640 | if (is_object($row)) |
650 | 641 | { |
651 | 642 | $row->created_at = date('Y-m-d H:i:s'); |
652 | - } |
|
653 | - else |
|
643 | + } else |
|
654 | 644 | { |
655 | 645 | $row['created_at'] = date('Y-m-d H:i:s'); |
656 | 646 | } |
@@ -662,8 +652,7 @@ discard block |
||
662 | 652 | if (is_object($row)) |
663 | 653 | { |
664 | 654 | $row->updated_at = date('Y-m-d H:i:s'); |
665 | - } |
|
666 | - else |
|
655 | + } else |
|
667 | 656 | { |
668 | 657 | $row['updated_at'] = date('Y-m-d H:i:s'); |
669 | 658 | } |
@@ -690,8 +679,7 @@ discard block |
||
690 | 679 | if (is_array($row)) |
691 | 680 | { |
692 | 681 | $row[$column] = unserialize($row[$column]); |
693 | - } |
|
694 | - else |
|
682 | + } else |
|
695 | 683 | { |
696 | 684 | $row->$column = unserialize($row->$column); |
697 | 685 | } |
@@ -711,8 +699,7 @@ discard block |
||
711 | 699 | if (isset($row->$attr)){ |
712 | 700 | unset($row->$attr); |
713 | 701 | } |
714 | - } |
|
715 | - else |
|
702 | + } else |
|
716 | 703 | { |
717 | 704 | if (isset($row[$attr])){ |
718 | 705 | unset($row[$attr]); |
@@ -812,8 +799,7 @@ discard block |
||
812 | 799 | { |
813 | 800 | $this->getQueryBuilder()->orderBy($key, $value); |
814 | 801 | } |
815 | - } |
|
816 | - else |
|
802 | + } else |
|
817 | 803 | { |
818 | 804 | $this->getQueryBuilder()->orderBy($criteria, $order); |
819 | 805 | } |
@@ -844,8 +830,7 @@ discard block |
||
844 | 830 | { |
845 | 831 | $relationship = $value; |
846 | 832 | $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
847 | - } |
|
848 | - else |
|
833 | + } else |
|
849 | 834 | { |
850 | 835 | $relationship = $key; |
851 | 836 | $options = $value; |
@@ -855,15 +840,13 @@ discard block |
||
855 | 840 | { |
856 | 841 | if (is_object($this->loaderInstance)){ |
857 | 842 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
858 | - } |
|
859 | - else{ |
|
843 | + } else{ |
|
860 | 844 | Loader::model($options['model'], $relationship . '_model'); |
861 | 845 | } |
862 | 846 | if (is_object($row)) |
863 | 847 | { |
864 | 848 | $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
865 | - } |
|
866 | - else |
|
849 | + } else |
|
867 | 850 | { |
868 | 851 | $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
869 | 852 | } |
@@ -883,8 +866,7 @@ discard block |
||
883 | 866 | { |
884 | 867 | $relationship = $value; |
885 | 868 | $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
886 | - } |
|
887 | - else |
|
869 | + } else |
|
888 | 870 | { |
889 | 871 | $relationship = $key; |
890 | 872 | $options = $value; |
@@ -894,15 +876,13 @@ discard block |
||
894 | 876 | { |
895 | 877 | if (is_object($this->loaderInstance)){ |
896 | 878 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
897 | - } |
|
898 | - else{ |
|
879 | + } else{ |
|
899 | 880 | Loader::model($options['model'], $relationship . '_model'); |
900 | 881 | } |
901 | 882 | if (is_object($row)) |
902 | 883 | { |
903 | 884 | $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
904 | - } |
|
905 | - else |
|
885 | + } else |
|
906 | 886 | { |
907 | 887 | $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
908 | 888 | } |
@@ -970,14 +950,12 @@ discard block |
||
970 | 950 | if (is_array($filter)) |
971 | 951 | { |
972 | 952 | $this->getQueryBuilder()->in($field, $filter); |
973 | - } |
|
974 | - else |
|
953 | + } else |
|
975 | 954 | { |
976 | 955 | if (is_int($field)) |
977 | 956 | { |
978 | 957 | $this->getQueryBuilder()->where($filter); |
979 | - } |
|
980 | - else |
|
958 | + } else |
|
981 | 959 | { |
982 | 960 | $this->getQueryBuilder()->where($field, $filter); |
983 | 961 | } |
@@ -994,33 +972,27 @@ discard block |
||
994 | 972 | if (count($params) == 1 && is_array($params[0])) |
995 | 973 | { |
996 | 974 | $this->_set_where_array($params[0]); |
997 | - } |
|
998 | - else if (count($params) == 1) |
|
975 | + } else if (count($params) == 1) |
|
999 | 976 | { |
1000 | 977 | $this->getQueryBuilder()->where($params[0]); |
1001 | - } |
|
1002 | - else if (count($params) == 2) |
|
978 | + } else if (count($params) == 2) |
|
1003 | 979 | { |
1004 | 980 | if (is_array($params[1])) |
1005 | 981 | { |
1006 | 982 | $this->getQueryBuilder()->in($params[0], $params[1]); |
1007 | - } |
|
1008 | - else |
|
983 | + } else |
|
1009 | 984 | { |
1010 | 985 | $this->getQueryBuilder()->where($params[0], $params[1]); |
1011 | 986 | } |
1012 | - } |
|
1013 | - else if (count($params) == 3) |
|
987 | + } else if (count($params) == 3) |
|
1014 | 988 | { |
1015 | 989 | $this->getQueryBuilder()->where($params[0], $params[1], $params[2]); |
1016 | - } |
|
1017 | - else |
|
990 | + } else |
|
1018 | 991 | { |
1019 | 992 | if (is_array($params[1])) |
1020 | 993 | { |
1021 | 994 | $this->getQueryBuilder()->in($params[0], $params[1]); |
1022 | - } |
|
1023 | - else |
|
995 | + } else |
|
1024 | 996 | { |
1025 | 997 | $this->getQueryBuilder()->where($params[0], $params[1]); |
1026 | 998 | } |
@@ -1,541 +1,541 @@ |
||
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 Response{ |
|
27 | + class Response{ |
|
28 | 28 | |
29 | - /** |
|
30 | - * The list of request header to send with response |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - private static $headers = array(); |
|
29 | + /** |
|
30 | + * The list of request header to send with response |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + private static $headers = array(); |
|
34 | 34 | |
35 | - /** |
|
36 | - * The logger instance |
|
37 | - * @var object |
|
38 | - */ |
|
39 | - private static $logger; |
|
35 | + /** |
|
36 | + * The logger instance |
|
37 | + * @var object |
|
38 | + */ |
|
39 | + private static $logger; |
|
40 | 40 | |
41 | - /** |
|
42 | - * The final page content to display to user |
|
43 | - * @var string |
|
44 | - */ |
|
45 | - private $_pageRender = null; |
|
41 | + /** |
|
42 | + * The final page content to display to user |
|
43 | + * @var string |
|
44 | + */ |
|
45 | + private $_pageRender = null; |
|
46 | 46 | |
47 | - /** |
|
48 | - * The current request URL |
|
49 | - * @var string |
|
50 | - */ |
|
51 | - private $_currentUrl = null; |
|
47 | + /** |
|
48 | + * The current request URL |
|
49 | + * @var string |
|
50 | + */ |
|
51 | + private $_currentUrl = null; |
|
52 | 52 | |
53 | - /** |
|
54 | - * The current request URL cache key |
|
55 | - * @var string |
|
56 | - */ |
|
57 | - private $_currentUrlCacheKey = null; |
|
53 | + /** |
|
54 | + * The current request URL cache key |
|
55 | + * @var string |
|
56 | + */ |
|
57 | + private $_currentUrlCacheKey = null; |
|
58 | 58 | |
59 | - /** |
|
60 | - * Whether we can compress the output using Gzip |
|
61 | - * @var boolean |
|
62 | - */ |
|
63 | - private static $_canCompressOutput = false; |
|
59 | + /** |
|
60 | + * Whether we can compress the output using Gzip |
|
61 | + * @var boolean |
|
62 | + */ |
|
63 | + private static $_canCompressOutput = false; |
|
64 | 64 | |
65 | - /** |
|
66 | - * Construct new response instance |
|
67 | - */ |
|
68 | - public function __construct(){ |
|
69 | - $currentUrl = ''; |
|
70 | - if (! empty($_SERVER['REQUEST_URI'])){ |
|
71 | - $currentUrl = $_SERVER['REQUEST_URI']; |
|
72 | - } |
|
73 | - if (! empty($_SERVER['QUERY_STRING'])){ |
|
74 | - $currentUrl .= '?' . $_SERVER['QUERY_STRING']; |
|
75 | - } |
|
76 | - $this->_currentUrl = $currentUrl; |
|
65 | + /** |
|
66 | + * Construct new response instance |
|
67 | + */ |
|
68 | + public function __construct(){ |
|
69 | + $currentUrl = ''; |
|
70 | + if (! empty($_SERVER['REQUEST_URI'])){ |
|
71 | + $currentUrl = $_SERVER['REQUEST_URI']; |
|
72 | + } |
|
73 | + if (! empty($_SERVER['QUERY_STRING'])){ |
|
74 | + $currentUrl .= '?' . $_SERVER['QUERY_STRING']; |
|
75 | + } |
|
76 | + $this->_currentUrl = $currentUrl; |
|
77 | 77 | |
78 | - $this->_currentUrlCacheKey = md5($this->_currentUrl); |
|
78 | + $this->_currentUrlCacheKey = md5($this->_currentUrl); |
|
79 | 79 | |
80 | - self::$_canCompressOutput = get_config('compress_output') |
|
81 | - && isset($_SERVER['HTTP_ACCEPT_ENCODING']) |
|
82 | - && stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false |
|
83 | - && extension_loaded('zlib') |
|
84 | - && (bool) ini_get('zlib.output_compression') === false; |
|
85 | - } |
|
80 | + self::$_canCompressOutput = get_config('compress_output') |
|
81 | + && isset($_SERVER['HTTP_ACCEPT_ENCODING']) |
|
82 | + && stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false |
|
83 | + && extension_loaded('zlib') |
|
84 | + && (bool) ini_get('zlib.output_compression') === false; |
|
85 | + } |
|
86 | 86 | |
87 | 87 | |
88 | - /** |
|
89 | - * The signleton of the logger |
|
90 | - * @return Object the Log instance |
|
91 | - */ |
|
92 | - public static function getLogger(){ |
|
93 | - if(self::$logger == null){ |
|
94 | - $logger = array(); |
|
95 | - $logger[0] =& class_loader('Log', 'classes'); |
|
96 | - $logger[0]->setLogger('Library::Response'); |
|
97 | - self::$logger = $logger[0]; |
|
98 | - } |
|
99 | - return self::$logger; |
|
100 | - } |
|
88 | + /** |
|
89 | + * The signleton of the logger |
|
90 | + * @return Object the Log instance |
|
91 | + */ |
|
92 | + public static function getLogger(){ |
|
93 | + if(self::$logger == null){ |
|
94 | + $logger = array(); |
|
95 | + $logger[0] =& class_loader('Log', 'classes'); |
|
96 | + $logger[0]->setLogger('Library::Response'); |
|
97 | + self::$logger = $logger[0]; |
|
98 | + } |
|
99 | + return self::$logger; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Set the log instance for future use |
|
104 | - * @param object $logger the log object |
|
105 | - * @return object the log instance |
|
106 | - */ |
|
107 | - public static function setLogger($logger){ |
|
108 | - self::$logger = $logger; |
|
109 | - return self::$logger; |
|
110 | - } |
|
102 | + /** |
|
103 | + * Set the log instance for future use |
|
104 | + * @param object $logger the log object |
|
105 | + * @return object the log instance |
|
106 | + */ |
|
107 | + public static function setLogger($logger){ |
|
108 | + self::$logger = $logger; |
|
109 | + return self::$logger; |
|
110 | + } |
|
111 | 111 | |
112 | 112 | |
113 | - /** |
|
114 | - * Send the HTTP Response headers |
|
115 | - * @param integer $httpCode the HTTP status code |
|
116 | - * @param array $headers the additional headers to add to the existing headers list |
|
117 | - */ |
|
118 | - public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
119 | - set_http_status_header($httpCode); |
|
120 | - self::setHeaders($headers); |
|
121 | - if(! headers_sent()){ |
|
122 | - foreach(self::getHeaders() as $key => $value){ |
|
123 | - header($key .': '.$value); |
|
124 | - } |
|
125 | - } |
|
126 | - } |
|
113 | + /** |
|
114 | + * Send the HTTP Response headers |
|
115 | + * @param integer $httpCode the HTTP status code |
|
116 | + * @param array $headers the additional headers to add to the existing headers list |
|
117 | + */ |
|
118 | + public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
119 | + set_http_status_header($httpCode); |
|
120 | + self::setHeaders($headers); |
|
121 | + if(! headers_sent()){ |
|
122 | + foreach(self::getHeaders() as $key => $value){ |
|
123 | + header($key .': '.$value); |
|
124 | + } |
|
125 | + } |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * Get the list of the headers |
|
130 | - * @return array the headers list |
|
131 | - */ |
|
132 | - public static function getHeaders(){ |
|
133 | - return self::$headers; |
|
134 | - } |
|
128 | + /** |
|
129 | + * Get the list of the headers |
|
130 | + * @return array the headers list |
|
131 | + */ |
|
132 | + public static function getHeaders(){ |
|
133 | + return self::$headers; |
|
134 | + } |
|
135 | 135 | |
136 | - /** |
|
137 | - * Get the header value for the given name |
|
138 | - * @param string $name the header name |
|
139 | - * @return string|null the header value |
|
140 | - */ |
|
141 | - public static function getHeader($name){ |
|
142 | - if(array_key_exists($name, self::$headers)){ |
|
143 | - return self::$headers[$name]; |
|
144 | - } |
|
145 | - return null; |
|
146 | - } |
|
136 | + /** |
|
137 | + * Get the header value for the given name |
|
138 | + * @param string $name the header name |
|
139 | + * @return string|null the header value |
|
140 | + */ |
|
141 | + public static function getHeader($name){ |
|
142 | + if(array_key_exists($name, self::$headers)){ |
|
143 | + return self::$headers[$name]; |
|
144 | + } |
|
145 | + return null; |
|
146 | + } |
|
147 | 147 | |
148 | 148 | |
149 | - /** |
|
150 | - * Set the header value for the specified name |
|
151 | - * @param string $name the header name |
|
152 | - * @param string $value the header value to be set |
|
153 | - */ |
|
154 | - public static function setHeader($name, $value){ |
|
155 | - self::$headers[$name] = $value; |
|
156 | - } |
|
149 | + /** |
|
150 | + * Set the header value for the specified name |
|
151 | + * @param string $name the header name |
|
152 | + * @param string $value the header value to be set |
|
153 | + */ |
|
154 | + public static function setHeader($name, $value){ |
|
155 | + self::$headers[$name] = $value; |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * Set the headers using array |
|
160 | - * @param array $headers the list of the headers to set. |
|
161 | - * Note: this will merge with the existing headers |
|
162 | - */ |
|
163 | - public static function setHeaders(array $headers){ |
|
164 | - self::$headers = array_merge(self::getHeaders(), $headers); |
|
165 | - } |
|
158 | + /** |
|
159 | + * Set the headers using array |
|
160 | + * @param array $headers the list of the headers to set. |
|
161 | + * Note: this will merge with the existing headers |
|
162 | + */ |
|
163 | + public static function setHeaders(array $headers){ |
|
164 | + self::$headers = array_merge(self::getHeaders(), $headers); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * Redirect user to the specified page |
|
169 | - * @param string $path the URL or URI to be redirect to |
|
170 | - */ |
|
171 | - public static function redirect($path = ''){ |
|
172 | - $logger = self::getLogger(); |
|
173 | - $url = Url::site_url($path); |
|
174 | - $logger->info('Redirect to URL [' .$url. ']'); |
|
175 | - if(! headers_sent()){ |
|
176 | - header('Location: '.$url); |
|
177 | - exit; |
|
178 | - } |
|
179 | - echo '<script> |
|
167 | + /** |
|
168 | + * Redirect user to the specified page |
|
169 | + * @param string $path the URL or URI to be redirect to |
|
170 | + */ |
|
171 | + public static function redirect($path = ''){ |
|
172 | + $logger = self::getLogger(); |
|
173 | + $url = Url::site_url($path); |
|
174 | + $logger->info('Redirect to URL [' .$url. ']'); |
|
175 | + if(! headers_sent()){ |
|
176 | + header('Location: '.$url); |
|
177 | + exit; |
|
178 | + } |
|
179 | + echo '<script> |
|
180 | 180 | location.href = "'.$url.'"; |
181 | 181 | </script>'; |
182 | - } |
|
182 | + } |
|
183 | 183 | |
184 | - /** |
|
185 | - * Render the view to display later or return the content |
|
186 | - * @param string $view the view name or path |
|
187 | - * @param array|object $data the variable data to use in the view |
|
188 | - * @param boolean $return whether to return the view generated content or display it directly |
|
189 | - * @return void|string if $return is true will return the view content otherwise |
|
190 | - * will display the view content. |
|
191 | - */ |
|
192 | - public function render($view, $data = null, $return = false){ |
|
193 | - $logger = self::getLogger(); |
|
194 | - //convert data to an array |
|
195 | - $data = (array) $data; |
|
196 | - $view = str_ireplace('.php', '', $view); |
|
197 | - $view = trim($view, '/\\'); |
|
198 | - $viewFile = $view . '.php'; |
|
199 | - $path = APPS_VIEWS_PATH . $viewFile; |
|
184 | + /** |
|
185 | + * Render the view to display later or return the content |
|
186 | + * @param string $view the view name or path |
|
187 | + * @param array|object $data the variable data to use in the view |
|
188 | + * @param boolean $return whether to return the view generated content or display it directly |
|
189 | + * @return void|string if $return is true will return the view content otherwise |
|
190 | + * will display the view content. |
|
191 | + */ |
|
192 | + public function render($view, $data = null, $return = false){ |
|
193 | + $logger = self::getLogger(); |
|
194 | + //convert data to an array |
|
195 | + $data = (array) $data; |
|
196 | + $view = str_ireplace('.php', '', $view); |
|
197 | + $view = trim($view, '/\\'); |
|
198 | + $viewFile = $view . '.php'; |
|
199 | + $path = APPS_VIEWS_PATH . $viewFile; |
|
200 | 200 | |
201 | - //check in module first |
|
202 | - $logger->debug('Checking the view [' . $view . '] from module list ...'); |
|
203 | - $moduleInfo = $this->getModuleInfoForView($view); |
|
204 | - $module = $moduleInfo['module']; |
|
205 | - $view = $moduleInfo['view']; |
|
201 | + //check in module first |
|
202 | + $logger->debug('Checking the view [' . $view . '] from module list ...'); |
|
203 | + $moduleInfo = $this->getModuleInfoForView($view); |
|
204 | + $module = $moduleInfo['module']; |
|
205 | + $view = $moduleInfo['view']; |
|
206 | 206 | |
207 | - $moduleViewPath = Module::findViewFullPath($view, $module); |
|
208 | - if($moduleViewPath){ |
|
209 | - $path = $moduleViewPath; |
|
210 | - $logger->info('Found view [' . $view . '] in module [' .$module. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
211 | - } |
|
212 | - else{ |
|
213 | - $logger->info('Cannot find view [' . $view . '] in module [' .$module. '] using the default location'); |
|
214 | - } |
|
207 | + $moduleViewPath = Module::findViewFullPath($view, $module); |
|
208 | + if($moduleViewPath){ |
|
209 | + $path = $moduleViewPath; |
|
210 | + $logger->info('Found view [' . $view . '] in module [' .$module. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
211 | + } |
|
212 | + else{ |
|
213 | + $logger->info('Cannot find view [' . $view . '] in module [' .$module. '] using the default location'); |
|
214 | + } |
|
215 | 215 | |
216 | - $logger->info('The view file path to be loaded is [' . $path . ']'); |
|
216 | + $logger->info('The view file path to be loaded is [' . $path . ']'); |
|
217 | 217 | |
218 | - ///////// |
|
219 | - if($return){ |
|
220 | - return $this->loadView($path, $data, true); |
|
221 | - } |
|
222 | - $this->loadView($path, $data, false); |
|
223 | - } |
|
218 | + ///////// |
|
219 | + if($return){ |
|
220 | + return $this->loadView($path, $data, true); |
|
221 | + } |
|
222 | + $this->loadView($path, $data, false); |
|
223 | + } |
|
224 | 224 | |
225 | 225 | |
226 | - /** |
|
227 | - * Send the final page output to user |
|
228 | - */ |
|
229 | - public function renderFinalPage(){ |
|
230 | - $logger = self::getLogger(); |
|
231 | - $obj = & get_instance(); |
|
232 | - $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
|
233 | - $dispatcher = $obj->eventdispatcher; |
|
234 | - $content = $this->_pageRender; |
|
235 | - if(! $content){ |
|
236 | - $logger->warning('The final view content is empty.'); |
|
237 | - return; |
|
238 | - } |
|
239 | - //dispatch |
|
240 | - $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
|
241 | - $content = null; |
|
242 | - if(! empty($event->payload)){ |
|
243 | - $content = $event->payload; |
|
244 | - } |
|
245 | - if(empty($content)){ |
|
246 | - $logger->warning('The view content is empty after dispatch to event listeners.'); |
|
247 | - } |
|
248 | - //remove unsed space in the content |
|
249 | - $content = preg_replace('~>\s*\n\s*<~', '><', $content); |
|
250 | - //check whether need save the page into cache. |
|
251 | - if($cachePageStatus){ |
|
252 | - $this->savePageContentIntoCache($content); |
|
253 | - } |
|
254 | - $content = $this->replaceElapseTimeAndMemoryUsage($content); |
|
226 | + /** |
|
227 | + * Send the final page output to user |
|
228 | + */ |
|
229 | + public function renderFinalPage(){ |
|
230 | + $logger = self::getLogger(); |
|
231 | + $obj = & get_instance(); |
|
232 | + $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
|
233 | + $dispatcher = $obj->eventdispatcher; |
|
234 | + $content = $this->_pageRender; |
|
235 | + if(! $content){ |
|
236 | + $logger->warning('The final view content is empty.'); |
|
237 | + return; |
|
238 | + } |
|
239 | + //dispatch |
|
240 | + $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
|
241 | + $content = null; |
|
242 | + if(! empty($event->payload)){ |
|
243 | + $content = $event->payload; |
|
244 | + } |
|
245 | + if(empty($content)){ |
|
246 | + $logger->warning('The view content is empty after dispatch to event listeners.'); |
|
247 | + } |
|
248 | + //remove unsed space in the content |
|
249 | + $content = preg_replace('~>\s*\n\s*<~', '><', $content); |
|
250 | + //check whether need save the page into cache. |
|
251 | + if($cachePageStatus){ |
|
252 | + $this->savePageContentIntoCache($content); |
|
253 | + } |
|
254 | + $content = $this->replaceElapseTimeAndMemoryUsage($content); |
|
255 | 255 | |
256 | - //compress the output if is available |
|
257 | - $type = null; |
|
258 | - if (self::$_canCompressOutput){ |
|
259 | - $type = 'ob_gzhandler'; |
|
260 | - } |
|
261 | - ob_start($type); |
|
262 | - self::sendHeaders(200); |
|
263 | - echo $content; |
|
264 | - ob_end_flush(); |
|
265 | - } |
|
256 | + //compress the output if is available |
|
257 | + $type = null; |
|
258 | + if (self::$_canCompressOutput){ |
|
259 | + $type = 'ob_gzhandler'; |
|
260 | + } |
|
261 | + ob_start($type); |
|
262 | + self::sendHeaders(200); |
|
263 | + echo $content; |
|
264 | + ob_end_flush(); |
|
265 | + } |
|
266 | 266 | |
267 | 267 | |
268 | - /** |
|
269 | - * Send the final page output to user if is cached |
|
270 | - * @param object $cache the cache instance |
|
271 | - * |
|
272 | - * @return boolean whether the page content if available or not |
|
273 | - */ |
|
274 | - public function renderFinalPageFromCache(&$cache){ |
|
275 | - $logger = self::getLogger(); |
|
276 | - //the current page cache key for identification |
|
277 | - $pageCacheKey = $this->_currentUrlCacheKey; |
|
268 | + /** |
|
269 | + * Send the final page output to user if is cached |
|
270 | + * @param object $cache the cache instance |
|
271 | + * |
|
272 | + * @return boolean whether the page content if available or not |
|
273 | + */ |
|
274 | + public function renderFinalPageFromCache(&$cache){ |
|
275 | + $logger = self::getLogger(); |
|
276 | + //the current page cache key for identification |
|
277 | + $pageCacheKey = $this->_currentUrlCacheKey; |
|
278 | 278 | |
279 | - $logger->debug('Checking if the page content for the URL [' . $this->_currentUrl . '] is cached ...'); |
|
280 | - //get the cache information to prepare header to send to browser |
|
281 | - $cacheInfo = $cache->getInfo($pageCacheKey); |
|
282 | - if($cacheInfo){ |
|
283 | - $status = $this->sendCacheNotYetExpireInfo($cacheInfo); |
|
284 | - if($status === false){ |
|
285 | - return $this->sendCachePageContentToBrowser($cache); |
|
286 | - } |
|
287 | - return true; |
|
288 | - } |
|
289 | - return false; |
|
290 | - } |
|
279 | + $logger->debug('Checking if the page content for the URL [' . $this->_currentUrl . '] is cached ...'); |
|
280 | + //get the cache information to prepare header to send to browser |
|
281 | + $cacheInfo = $cache->getInfo($pageCacheKey); |
|
282 | + if($cacheInfo){ |
|
283 | + $status = $this->sendCacheNotYetExpireInfo($cacheInfo); |
|
284 | + if($status === false){ |
|
285 | + return $this->sendCachePageContentToBrowser($cache); |
|
286 | + } |
|
287 | + return true; |
|
288 | + } |
|
289 | + return false; |
|
290 | + } |
|
291 | 291 | |
292 | 292 | |
293 | - /** |
|
294 | - * Get the final page to be rendered |
|
295 | - * @return string |
|
296 | - */ |
|
297 | - public function getFinalPageRendered(){ |
|
298 | - return $this->_pageRender; |
|
299 | - } |
|
293 | + /** |
|
294 | + * Get the final page to be rendered |
|
295 | + * @return string |
|
296 | + */ |
|
297 | + public function getFinalPageRendered(){ |
|
298 | + return $this->_pageRender; |
|
299 | + } |
|
300 | 300 | |
301 | - /** |
|
302 | - * Send the HTTP 404 error if can not found the |
|
303 | - * routing information for the current request |
|
304 | - */ |
|
305 | - public static function send404(){ |
|
306 | - /********* for logs **************/ |
|
307 | - //can't use $obj = & get_instance() here because the global super object will be available until |
|
308 | - //the main controller is loaded even for Loader::library('xxxx'); |
|
309 | - $logger = self::getLogger(); |
|
310 | - $request =& class_loader('Request', 'classes'); |
|
311 | - $userAgent =& class_loader('Browser'); |
|
312 | - $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
301 | + /** |
|
302 | + * Send the HTTP 404 error if can not found the |
|
303 | + * routing information for the current request |
|
304 | + */ |
|
305 | + public static function send404(){ |
|
306 | + /********* for logs **************/ |
|
307 | + //can't use $obj = & get_instance() here because the global super object will be available until |
|
308 | + //the main controller is loaded even for Loader::library('xxxx'); |
|
309 | + $logger = self::getLogger(); |
|
310 | + $request =& class_loader('Request', 'classes'); |
|
311 | + $userAgent =& class_loader('Browser'); |
|
312 | + $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
313 | 313 | |
314 | - //here can't use Loader::functions just include the helper manually |
|
315 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
314 | + //here can't use Loader::functions just include the helper manually |
|
315 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
316 | 316 | |
317 | - $str = '[404 page not found] : '; |
|
318 | - $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
319 | - $logger->error($str); |
|
320 | - /***********************************/ |
|
321 | - $path = CORE_VIEWS_PATH . '404.php'; |
|
322 | - if(file_exists($path)){ |
|
323 | - //compress the output if is available |
|
324 | - $type = null; |
|
325 | - if (self::$_canCompressOutput){ |
|
326 | - $type = 'ob_gzhandler'; |
|
327 | - } |
|
328 | - ob_start($type); |
|
329 | - require_once $path; |
|
330 | - $output = ob_get_clean(); |
|
331 | - self::sendHeaders(404); |
|
332 | - echo $output; |
|
333 | - } |
|
334 | - else{ |
|
335 | - show_error('The 404 view [' .$path. '] does not exist'); |
|
336 | - } |
|
337 | - } |
|
317 | + $str = '[404 page not found] : '; |
|
318 | + $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
319 | + $logger->error($str); |
|
320 | + /***********************************/ |
|
321 | + $path = CORE_VIEWS_PATH . '404.php'; |
|
322 | + if(file_exists($path)){ |
|
323 | + //compress the output if is available |
|
324 | + $type = null; |
|
325 | + if (self::$_canCompressOutput){ |
|
326 | + $type = 'ob_gzhandler'; |
|
327 | + } |
|
328 | + ob_start($type); |
|
329 | + require_once $path; |
|
330 | + $output = ob_get_clean(); |
|
331 | + self::sendHeaders(404); |
|
332 | + echo $output; |
|
333 | + } |
|
334 | + else{ |
|
335 | + show_error('The 404 view [' .$path. '] does not exist'); |
|
336 | + } |
|
337 | + } |
|
338 | 338 | |
339 | - /** |
|
340 | - * Display the error to user |
|
341 | - * @param array $data the error information |
|
342 | - */ |
|
343 | - public static function sendError(array $data = array()){ |
|
344 | - $path = CORE_VIEWS_PATH . 'errors.php'; |
|
345 | - if(file_exists($path)){ |
|
346 | - //compress the output if is available |
|
347 | - $type = null; |
|
348 | - if (self::$_canCompressOutput){ |
|
349 | - $type = 'ob_gzhandler'; |
|
350 | - } |
|
351 | - ob_start($type); |
|
352 | - extract($data); |
|
353 | - require_once $path; |
|
354 | - $output = ob_get_clean(); |
|
355 | - self::sendHeaders(503); |
|
356 | - echo $output; |
|
357 | - } |
|
358 | - else{ |
|
359 | - //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
360 | - set_http_status_header(503); |
|
361 | - echo 'The error view [' . $path . '] does not exist'; |
|
362 | - } |
|
363 | - } |
|
339 | + /** |
|
340 | + * Display the error to user |
|
341 | + * @param array $data the error information |
|
342 | + */ |
|
343 | + public static function sendError(array $data = array()){ |
|
344 | + $path = CORE_VIEWS_PATH . 'errors.php'; |
|
345 | + if(file_exists($path)){ |
|
346 | + //compress the output if is available |
|
347 | + $type = null; |
|
348 | + if (self::$_canCompressOutput){ |
|
349 | + $type = 'ob_gzhandler'; |
|
350 | + } |
|
351 | + ob_start($type); |
|
352 | + extract($data); |
|
353 | + require_once $path; |
|
354 | + $output = ob_get_clean(); |
|
355 | + self::sendHeaders(503); |
|
356 | + echo $output; |
|
357 | + } |
|
358 | + else{ |
|
359 | + //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
360 | + set_http_status_header(503); |
|
361 | + echo 'The error view [' . $path . '] does not exist'; |
|
362 | + } |
|
363 | + } |
|
364 | 364 | |
365 | - /** |
|
366 | - * Send the cache not yet expire to browser |
|
367 | - * @param array $cacheInfo the cache information |
|
368 | - * @return boolean true if the information is sent otherwise false |
|
369 | - */ |
|
370 | - protected function sendCacheNotYetExpireInfo($cacheInfo){ |
|
371 | - if(! empty($cacheInfo)){ |
|
372 | - $logger = self::getLogger(); |
|
373 | - $lastModified = $cacheInfo['mtime']; |
|
374 | - $expire = $cacheInfo['expire']; |
|
375 | - $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
376 | - self::setHeader('Pragma', 'public'); |
|
377 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
378 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
379 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
380 | - if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
381 | - $logger->info('The cache page content is not yet expire for the URL [' . $this->_currentUrl . '] send 304 header to browser'); |
|
382 | - self::sendHeaders(304); |
|
383 | - return true; |
|
384 | - } |
|
385 | - } |
|
386 | - return false; |
|
387 | - } |
|
365 | + /** |
|
366 | + * Send the cache not yet expire to browser |
|
367 | + * @param array $cacheInfo the cache information |
|
368 | + * @return boolean true if the information is sent otherwise false |
|
369 | + */ |
|
370 | + protected function sendCacheNotYetExpireInfo($cacheInfo){ |
|
371 | + if(! empty($cacheInfo)){ |
|
372 | + $logger = self::getLogger(); |
|
373 | + $lastModified = $cacheInfo['mtime']; |
|
374 | + $expire = $cacheInfo['expire']; |
|
375 | + $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
376 | + self::setHeader('Pragma', 'public'); |
|
377 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
378 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
379 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
380 | + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
381 | + $logger->info('The cache page content is not yet expire for the URL [' . $this->_currentUrl . '] send 304 header to browser'); |
|
382 | + self::sendHeaders(304); |
|
383 | + return true; |
|
384 | + } |
|
385 | + } |
|
386 | + return false; |
|
387 | + } |
|
388 | 388 | |
389 | - /** |
|
390 | - * Set the value of '{elapsed_time}' and '{memory_usage}' |
|
391 | - * @param string $content the page content |
|
392 | - * @return string the page content after replace |
|
393 | - * '{elapsed_time}', '{memory_usage}' |
|
394 | - */ |
|
395 | - protected function replaceElapseTimeAndMemoryUsage($content){ |
|
396 | - //load benchmark class |
|
397 | - $benchmark = & class_loader('Benchmark'); |
|
389 | + /** |
|
390 | + * Set the value of '{elapsed_time}' and '{memory_usage}' |
|
391 | + * @param string $content the page content |
|
392 | + * @return string the page content after replace |
|
393 | + * '{elapsed_time}', '{memory_usage}' |
|
394 | + */ |
|
395 | + protected function replaceElapseTimeAndMemoryUsage($content){ |
|
396 | + //load benchmark class |
|
397 | + $benchmark = & class_loader('Benchmark'); |
|
398 | 398 | |
399 | - // Parse out the elapsed time and memory usage, |
|
400 | - // then swap the pseudo-variables with the data |
|
401 | - $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
402 | - $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
403 | - return str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
404 | - } |
|
399 | + // Parse out the elapsed time and memory usage, |
|
400 | + // then swap the pseudo-variables with the data |
|
401 | + $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
402 | + $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
403 | + return str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
404 | + } |
|
405 | 405 | |
406 | - /** |
|
407 | - * Send the page content from cache to browser |
|
408 | - * @param object $cache the cache instance |
|
409 | - * @return boolean the status of the operation |
|
410 | - */ |
|
411 | - protected function sendCachePageContentToBrowser(&$cache){ |
|
412 | - $logger = self::getLogger(); |
|
413 | - $logger->info('The cache page content is expired or the browser doesn\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $this->_currentUrl . '] send cache headers to tell the browser'); |
|
414 | - self::sendHeaders(200); |
|
415 | - //current page cache key |
|
416 | - $pageCacheKey = $this->_currentUrlCacheKey; |
|
417 | - //get the cache content |
|
418 | - $content = $cache->get($pageCacheKey); |
|
419 | - if($content){ |
|
420 | - $logger->info('The page content for the URL [' . $this->_currentUrl . '] already cached just display it'); |
|
421 | - $content = $this->replaceElapseTimeAndMemoryUsage($content); |
|
422 | - ///display the final output |
|
423 | - //compress the output if is available |
|
424 | - $type = null; |
|
425 | - if (self::$_canCompressOutput){ |
|
426 | - $type = 'ob_gzhandler'; |
|
427 | - } |
|
428 | - ob_start($type); |
|
429 | - echo $content; |
|
430 | - ob_end_flush(); |
|
431 | - return true; |
|
432 | - } |
|
433 | - $logger->info('The page cache content for the URL [' . $this->_currentUrl . '] is not valid may be already expired'); |
|
434 | - $cache->delete($pageCacheKey); |
|
435 | - return false; |
|
436 | - } |
|
406 | + /** |
|
407 | + * Send the page content from cache to browser |
|
408 | + * @param object $cache the cache instance |
|
409 | + * @return boolean the status of the operation |
|
410 | + */ |
|
411 | + protected function sendCachePageContentToBrowser(&$cache){ |
|
412 | + $logger = self::getLogger(); |
|
413 | + $logger->info('The cache page content is expired or the browser doesn\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $this->_currentUrl . '] send cache headers to tell the browser'); |
|
414 | + self::sendHeaders(200); |
|
415 | + //current page cache key |
|
416 | + $pageCacheKey = $this->_currentUrlCacheKey; |
|
417 | + //get the cache content |
|
418 | + $content = $cache->get($pageCacheKey); |
|
419 | + if($content){ |
|
420 | + $logger->info('The page content for the URL [' . $this->_currentUrl . '] already cached just display it'); |
|
421 | + $content = $this->replaceElapseTimeAndMemoryUsage($content); |
|
422 | + ///display the final output |
|
423 | + //compress the output if is available |
|
424 | + $type = null; |
|
425 | + if (self::$_canCompressOutput){ |
|
426 | + $type = 'ob_gzhandler'; |
|
427 | + } |
|
428 | + ob_start($type); |
|
429 | + echo $content; |
|
430 | + ob_end_flush(); |
|
431 | + return true; |
|
432 | + } |
|
433 | + $logger->info('The page cache content for the URL [' . $this->_currentUrl . '] is not valid may be already expired'); |
|
434 | + $cache->delete($pageCacheKey); |
|
435 | + return false; |
|
436 | + } |
|
437 | 437 | |
438 | - /** |
|
439 | - * Save the content of page into cache |
|
440 | - * @param string $content the page content to be saved |
|
441 | - * @return void |
|
442 | - */ |
|
443 | - protected function savePageContentIntoCache($content){ |
|
444 | - $obj = & get_instance(); |
|
445 | - $logger = self::getLogger(); |
|
438 | + /** |
|
439 | + * Save the content of page into cache |
|
440 | + * @param string $content the page content to be saved |
|
441 | + * @return void |
|
442 | + */ |
|
443 | + protected function savePageContentIntoCache($content){ |
|
444 | + $obj = & get_instance(); |
|
445 | + $logger = self::getLogger(); |
|
446 | 446 | |
447 | - //current page URL |
|
448 | - $url = $this->_currentUrl; |
|
449 | - //Cache view Time to live in second |
|
450 | - $viewCacheTtl = get_config('cache_ttl'); |
|
451 | - if (!empty($obj->view_cache_ttl)){ |
|
452 | - $viewCacheTtl = $obj->view_cache_ttl; |
|
453 | - } |
|
454 | - //the cache handler instance |
|
455 | - $cacheInstance = $obj->cache; |
|
456 | - //the current page cache key for identification |
|
457 | - $cacheKey = $this->_currentUrlCacheKey; |
|
458 | - $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
459 | - $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
447 | + //current page URL |
|
448 | + $url = $this->_currentUrl; |
|
449 | + //Cache view Time to live in second |
|
450 | + $viewCacheTtl = get_config('cache_ttl'); |
|
451 | + if (!empty($obj->view_cache_ttl)){ |
|
452 | + $viewCacheTtl = $obj->view_cache_ttl; |
|
453 | + } |
|
454 | + //the cache handler instance |
|
455 | + $cacheInstance = $obj->cache; |
|
456 | + //the current page cache key for identification |
|
457 | + $cacheKey = $this->_currentUrlCacheKey; |
|
458 | + $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
459 | + $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
460 | 460 | |
461 | - //get the cache information to prepare header to send to browser |
|
462 | - $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
463 | - if($cacheInfo){ |
|
464 | - $lastModified = $cacheInfo['mtime']; |
|
465 | - $expire = $cacheInfo['expire']; |
|
466 | - $maxAge = $expire - time(); |
|
467 | - self::setHeader('Pragma', 'public'); |
|
468 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
469 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
470 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
471 | - } |
|
472 | - } |
|
461 | + //get the cache information to prepare header to send to browser |
|
462 | + $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
463 | + if($cacheInfo){ |
|
464 | + $lastModified = $cacheInfo['mtime']; |
|
465 | + $expire = $cacheInfo['expire']; |
|
466 | + $maxAge = $expire - time(); |
|
467 | + self::setHeader('Pragma', 'public'); |
|
468 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
469 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
470 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
471 | + } |
|
472 | + } |
|
473 | 473 | |
474 | 474 | |
475 | - /** |
|
476 | - * Get the module information for the view to load |
|
477 | - * @param string $view the view name like moduleName/viewName, viewName |
|
478 | - * |
|
479 | - * @return array the module information |
|
480 | - * array( |
|
481 | - * 'module'=> 'module_name' |
|
482 | - * 'view' => 'view_name' |
|
483 | - * 'viewFile' => 'view_file' |
|
484 | - * ) |
|
485 | - */ |
|
486 | - protected function getModuleInfoForView($view){ |
|
487 | - $module = null; |
|
488 | - $viewFile = null; |
|
489 | - $obj = & get_instance(); |
|
490 | - //check if the request class contains module name |
|
491 | - if(strpos($view, '/') !== false){ |
|
492 | - $viewPath = explode('/', $view); |
|
493 | - if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
494 | - $module = $viewPath[0]; |
|
495 | - array_shift($viewPath); |
|
496 | - $view = implode('/', $viewPath); |
|
497 | - $viewFile = $view . '.php'; |
|
498 | - } |
|
499 | - } |
|
500 | - if(! $module && !empty($obj->moduleName)){ |
|
501 | - $module = $obj->moduleName; |
|
502 | - } |
|
503 | - return array( |
|
504 | - 'view' => $view, |
|
505 | - 'module' => $module, |
|
506 | - 'viewFile' => $viewFile |
|
507 | - ); |
|
508 | - } |
|
475 | + /** |
|
476 | + * Get the module information for the view to load |
|
477 | + * @param string $view the view name like moduleName/viewName, viewName |
|
478 | + * |
|
479 | + * @return array the module information |
|
480 | + * array( |
|
481 | + * 'module'=> 'module_name' |
|
482 | + * 'view' => 'view_name' |
|
483 | + * 'viewFile' => 'view_file' |
|
484 | + * ) |
|
485 | + */ |
|
486 | + protected function getModuleInfoForView($view){ |
|
487 | + $module = null; |
|
488 | + $viewFile = null; |
|
489 | + $obj = & get_instance(); |
|
490 | + //check if the request class contains module name |
|
491 | + if(strpos($view, '/') !== false){ |
|
492 | + $viewPath = explode('/', $view); |
|
493 | + if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
494 | + $module = $viewPath[0]; |
|
495 | + array_shift($viewPath); |
|
496 | + $view = implode('/', $viewPath); |
|
497 | + $viewFile = $view . '.php'; |
|
498 | + } |
|
499 | + } |
|
500 | + if(! $module && !empty($obj->moduleName)){ |
|
501 | + $module = $obj->moduleName; |
|
502 | + } |
|
503 | + return array( |
|
504 | + 'view' => $view, |
|
505 | + 'module' => $module, |
|
506 | + 'viewFile' => $viewFile |
|
507 | + ); |
|
508 | + } |
|
509 | 509 | |
510 | - /** |
|
511 | - * Render the view page |
|
512 | - * @see Response::render |
|
513 | - * @return void|string |
|
514 | - */ |
|
515 | - protected function loadView($path, array $data = array(), $return = false){ |
|
516 | - $found = false; |
|
517 | - if(file_exists($path)){ |
|
518 | - //super instance |
|
519 | - $obj = & get_instance(); |
|
520 | - foreach(get_object_vars($obj) as $key => $value){ |
|
521 | - if(! isset($this->{$key})){ |
|
522 | - $this->{$key} = & $obj->{$key}; |
|
523 | - } |
|
524 | - } |
|
525 | - ob_start(); |
|
526 | - extract($data); |
|
527 | - //need use require() instead of require_once because can load this view many time |
|
528 | - require $path; |
|
529 | - $content = ob_get_clean(); |
|
530 | - if($return){ |
|
531 | - return $content; |
|
532 | - } |
|
533 | - $this->_pageRender .= $content; |
|
534 | - $found = true; |
|
535 | - } |
|
536 | - if(! $found){ |
|
537 | - show_error('Unable to find view [' .$view . ']'); |
|
538 | - } |
|
539 | - } |
|
510 | + /** |
|
511 | + * Render the view page |
|
512 | + * @see Response::render |
|
513 | + * @return void|string |
|
514 | + */ |
|
515 | + protected function loadView($path, array $data = array(), $return = false){ |
|
516 | + $found = false; |
|
517 | + if(file_exists($path)){ |
|
518 | + //super instance |
|
519 | + $obj = & get_instance(); |
|
520 | + foreach(get_object_vars($obj) as $key => $value){ |
|
521 | + if(! isset($this->{$key})){ |
|
522 | + $this->{$key} = & $obj->{$key}; |
|
523 | + } |
|
524 | + } |
|
525 | + ob_start(); |
|
526 | + extract($data); |
|
527 | + //need use require() instead of require_once because can load this view many time |
|
528 | + require $path; |
|
529 | + $content = ob_get_clean(); |
|
530 | + if($return){ |
|
531 | + return $content; |
|
532 | + } |
|
533 | + $this->_pageRender .= $content; |
|
534 | + $found = true; |
|
535 | + } |
|
536 | + if(! $found){ |
|
537 | + show_error('Unable to find view [' .$view . ']'); |
|
538 | + } |
|
539 | + } |
|
540 | 540 | |
541 | - } |
|
541 | + } |
@@ -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 Response{ |
|
27 | + class Response { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * The list of request header to send with response |
@@ -65,15 +65,15 @@ discard block |
||
65 | 65 | /** |
66 | 66 | * Construct new response instance |
67 | 67 | */ |
68 | - public function __construct(){ |
|
68 | + public function __construct() { |
|
69 | 69 | $currentUrl = ''; |
70 | - if (! empty($_SERVER['REQUEST_URI'])){ |
|
70 | + if (!empty($_SERVER['REQUEST_URI'])) { |
|
71 | 71 | $currentUrl = $_SERVER['REQUEST_URI']; |
72 | 72 | } |
73 | - if (! empty($_SERVER['QUERY_STRING'])){ |
|
73 | + if (!empty($_SERVER['QUERY_STRING'])) { |
|
74 | 74 | $currentUrl .= '?' . $_SERVER['QUERY_STRING']; |
75 | 75 | } |
76 | - $this->_currentUrl = $currentUrl; |
|
76 | + $this->_currentUrl = $currentUrl; |
|
77 | 77 | |
78 | 78 | $this->_currentUrlCacheKey = md5($this->_currentUrl); |
79 | 79 | |
@@ -89,10 +89,10 @@ discard block |
||
89 | 89 | * The signleton of the logger |
90 | 90 | * @return Object the Log instance |
91 | 91 | */ |
92 | - public static function getLogger(){ |
|
93 | - if(self::$logger == null){ |
|
92 | + public static function getLogger() { |
|
93 | + if (self::$logger == null) { |
|
94 | 94 | $logger = array(); |
95 | - $logger[0] =& class_loader('Log', 'classes'); |
|
95 | + $logger[0] = & class_loader('Log', 'classes'); |
|
96 | 96 | $logger[0]->setLogger('Library::Response'); |
97 | 97 | self::$logger = $logger[0]; |
98 | 98 | } |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | * @param object $logger the log object |
105 | 105 | * @return object the log instance |
106 | 106 | */ |
107 | - public static function setLogger($logger){ |
|
107 | + public static function setLogger($logger) { |
|
108 | 108 | self::$logger = $logger; |
109 | 109 | return self::$logger; |
110 | 110 | } |
@@ -115,12 +115,12 @@ discard block |
||
115 | 115 | * @param integer $httpCode the HTTP status code |
116 | 116 | * @param array $headers the additional headers to add to the existing headers list |
117 | 117 | */ |
118 | - public static function sendHeaders($httpCode = 200, array $headers = array()){ |
|
118 | + public static function sendHeaders($httpCode = 200, array $headers = array()) { |
|
119 | 119 | set_http_status_header($httpCode); |
120 | 120 | self::setHeaders($headers); |
121 | - if(! headers_sent()){ |
|
122 | - foreach(self::getHeaders() as $key => $value){ |
|
123 | - header($key .': '.$value); |
|
121 | + if (!headers_sent()) { |
|
122 | + foreach (self::getHeaders() as $key => $value) { |
|
123 | + header($key . ': ' . $value); |
|
124 | 124 | } |
125 | 125 | } |
126 | 126 | } |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | * Get the list of the headers |
130 | 130 | * @return array the headers list |
131 | 131 | */ |
132 | - public static function getHeaders(){ |
|
132 | + public static function getHeaders() { |
|
133 | 133 | return self::$headers; |
134 | 134 | } |
135 | 135 | |
@@ -138,8 +138,8 @@ discard block |
||
138 | 138 | * @param string $name the header name |
139 | 139 | * @return string|null the header value |
140 | 140 | */ |
141 | - public static function getHeader($name){ |
|
142 | - if(array_key_exists($name, self::$headers)){ |
|
141 | + public static function getHeader($name) { |
|
142 | + if (array_key_exists($name, self::$headers)) { |
|
143 | 143 | return self::$headers[$name]; |
144 | 144 | } |
145 | 145 | return null; |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | * @param string $name the header name |
152 | 152 | * @param string $value the header value to be set |
153 | 153 | */ |
154 | - public static function setHeader($name, $value){ |
|
154 | + public static function setHeader($name, $value) { |
|
155 | 155 | self::$headers[$name] = $value; |
156 | 156 | } |
157 | 157 | |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | * @param array $headers the list of the headers to set. |
161 | 161 | * Note: this will merge with the existing headers |
162 | 162 | */ |
163 | - public static function setHeaders(array $headers){ |
|
163 | + public static function setHeaders(array $headers) { |
|
164 | 164 | self::$headers = array_merge(self::getHeaders(), $headers); |
165 | 165 | } |
166 | 166 | |
@@ -168,16 +168,16 @@ discard block |
||
168 | 168 | * Redirect user to the specified page |
169 | 169 | * @param string $path the URL or URI to be redirect to |
170 | 170 | */ |
171 | - public static function redirect($path = ''){ |
|
171 | + public static function redirect($path = '') { |
|
172 | 172 | $logger = self::getLogger(); |
173 | 173 | $url = Url::site_url($path); |
174 | - $logger->info('Redirect to URL [' .$url. ']'); |
|
175 | - if(! headers_sent()){ |
|
176 | - header('Location: '.$url); |
|
174 | + $logger->info('Redirect to URL [' . $url . ']'); |
|
175 | + if (!headers_sent()) { |
|
176 | + header('Location: ' . $url); |
|
177 | 177 | exit; |
178 | 178 | } |
179 | 179 | echo '<script> |
180 | - location.href = "'.$url.'"; |
|
180 | + location.href = "'.$url . '"; |
|
181 | 181 | </script>'; |
182 | 182 | } |
183 | 183 | |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | * @return void|string if $return is true will return the view content otherwise |
190 | 190 | * will display the view content. |
191 | 191 | */ |
192 | - public function render($view, $data = null, $return = false){ |
|
192 | + public function render($view, $data = null, $return = false) { |
|
193 | 193 | $logger = self::getLogger(); |
194 | 194 | //convert data to an array |
195 | 195 | $data = (array) $data; |
@@ -201,22 +201,22 @@ discard block |
||
201 | 201 | //check in module first |
202 | 202 | $logger->debug('Checking the view [' . $view . '] from module list ...'); |
203 | 203 | $moduleInfo = $this->getModuleInfoForView($view); |
204 | - $module = $moduleInfo['module']; |
|
205 | - $view = $moduleInfo['view']; |
|
204 | + $module = $moduleInfo['module']; |
|
205 | + $view = $moduleInfo['view']; |
|
206 | 206 | |
207 | 207 | $moduleViewPath = Module::findViewFullPath($view, $module); |
208 | - if($moduleViewPath){ |
|
208 | + if ($moduleViewPath) { |
|
209 | 209 | $path = $moduleViewPath; |
210 | - $logger->info('Found view [' . $view . '] in module [' .$module. '], the file path is [' .$moduleViewPath. '] we will used it'); |
|
210 | + $logger->info('Found view [' . $view . '] in module [' . $module . '], the file path is [' . $moduleViewPath . '] we will used it'); |
|
211 | 211 | } |
212 | - else{ |
|
213 | - $logger->info('Cannot find view [' . $view . '] in module [' .$module. '] using the default location'); |
|
212 | + else { |
|
213 | + $logger->info('Cannot find view [' . $view . '] in module [' . $module . '] using the default location'); |
|
214 | 214 | } |
215 | 215 | |
216 | 216 | $logger->info('The view file path to be loaded is [' . $path . ']'); |
217 | 217 | |
218 | 218 | ///////// |
219 | - if($return){ |
|
219 | + if ($return) { |
|
220 | 220 | return $this->loadView($path, $data, true); |
221 | 221 | } |
222 | 222 | $this->loadView($path, $data, false); |
@@ -226,36 +226,36 @@ discard block |
||
226 | 226 | /** |
227 | 227 | * Send the final page output to user |
228 | 228 | */ |
229 | - public function renderFinalPage(){ |
|
229 | + public function renderFinalPage() { |
|
230 | 230 | $logger = self::getLogger(); |
231 | 231 | $obj = & get_instance(); |
232 | 232 | $cachePageStatus = get_config('cache_enable', false) && !empty($obj->view_cache_enable); |
233 | 233 | $dispatcher = $obj->eventdispatcher; |
234 | 234 | $content = $this->_pageRender; |
235 | - if(! $content){ |
|
235 | + if (!$content) { |
|
236 | 236 | $logger->warning('The final view content is empty.'); |
237 | 237 | return; |
238 | 238 | } |
239 | 239 | //dispatch |
240 | 240 | $event = $dispatcher->dispatch(new EventInfo('FINAL_VIEW_READY', $content, true)); |
241 | 241 | $content = null; |
242 | - if(! empty($event->payload)){ |
|
242 | + if (!empty($event->payload)) { |
|
243 | 243 | $content = $event->payload; |
244 | 244 | } |
245 | - if(empty($content)){ |
|
245 | + if (empty($content)) { |
|
246 | 246 | $logger->warning('The view content is empty after dispatch to event listeners.'); |
247 | 247 | } |
248 | 248 | //remove unsed space in the content |
249 | 249 | $content = preg_replace('~>\s*\n\s*<~', '><', $content); |
250 | 250 | //check whether need save the page into cache. |
251 | - if($cachePageStatus){ |
|
251 | + if ($cachePageStatus) { |
|
252 | 252 | $this->savePageContentIntoCache($content); |
253 | 253 | } |
254 | 254 | $content = $this->replaceElapseTimeAndMemoryUsage($content); |
255 | 255 | |
256 | 256 | //compress the output if is available |
257 | 257 | $type = null; |
258 | - if (self::$_canCompressOutput){ |
|
258 | + if (self::$_canCompressOutput) { |
|
259 | 259 | $type = 'ob_gzhandler'; |
260 | 260 | } |
261 | 261 | ob_start($type); |
@@ -271,7 +271,7 @@ discard block |
||
271 | 271 | * |
272 | 272 | * @return boolean whether the page content if available or not |
273 | 273 | */ |
274 | - public function renderFinalPageFromCache(&$cache){ |
|
274 | + public function renderFinalPageFromCache(&$cache) { |
|
275 | 275 | $logger = self::getLogger(); |
276 | 276 | //the current page cache key for identification |
277 | 277 | $pageCacheKey = $this->_currentUrlCacheKey; |
@@ -279,9 +279,9 @@ discard block |
||
279 | 279 | $logger->debug('Checking if the page content for the URL [' . $this->_currentUrl . '] is cached ...'); |
280 | 280 | //get the cache information to prepare header to send to browser |
281 | 281 | $cacheInfo = $cache->getInfo($pageCacheKey); |
282 | - if($cacheInfo){ |
|
282 | + if ($cacheInfo) { |
|
283 | 283 | $status = $this->sendCacheNotYetExpireInfo($cacheInfo); |
284 | - if($status === false){ |
|
284 | + if ($status === false) { |
|
285 | 285 | return $this->sendCachePageContentToBrowser($cache); |
286 | 286 | } |
287 | 287 | return true; |
@@ -294,7 +294,7 @@ discard block |
||
294 | 294 | * Get the final page to be rendered |
295 | 295 | * @return string |
296 | 296 | */ |
297 | - public function getFinalPageRendered(){ |
|
297 | + public function getFinalPageRendered() { |
|
298 | 298 | return $this->_pageRender; |
299 | 299 | } |
300 | 300 | |
@@ -302,14 +302,14 @@ discard block |
||
302 | 302 | * Send the HTTP 404 error if can not found the |
303 | 303 | * routing information for the current request |
304 | 304 | */ |
305 | - public static function send404(){ |
|
305 | + public static function send404() { |
|
306 | 306 | /********* for logs **************/ |
307 | 307 | //can't use $obj = & get_instance() here because the global super object will be available until |
308 | 308 | //the main controller is loaded even for Loader::library('xxxx'); |
309 | 309 | $logger = self::getLogger(); |
310 | - $request =& class_loader('Request', 'classes'); |
|
311 | - $userAgent =& class_loader('Browser'); |
|
312 | - $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
310 | + $request = & class_loader('Request', 'classes'); |
|
311 | + $userAgent = & class_loader('Browser'); |
|
312 | + $browser = $userAgent->getPlatform() . ', ' . $userAgent->getBrowser() . ' ' . $userAgent->getVersion(); |
|
313 | 313 | |
314 | 314 | //here can't use Loader::functions just include the helper manually |
315 | 315 | require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
@@ -319,10 +319,10 @@ discard block |
||
319 | 319 | $logger->error($str); |
320 | 320 | /***********************************/ |
321 | 321 | $path = CORE_VIEWS_PATH . '404.php'; |
322 | - if(file_exists($path)){ |
|
322 | + if (file_exists($path)) { |
|
323 | 323 | //compress the output if is available |
324 | 324 | $type = null; |
325 | - if (self::$_canCompressOutput){ |
|
325 | + if (self::$_canCompressOutput) { |
|
326 | 326 | $type = 'ob_gzhandler'; |
327 | 327 | } |
328 | 328 | ob_start($type); |
@@ -331,8 +331,8 @@ discard block |
||
331 | 331 | self::sendHeaders(404); |
332 | 332 | echo $output; |
333 | 333 | } |
334 | - else{ |
|
335 | - show_error('The 404 view [' .$path. '] does not exist'); |
|
334 | + else { |
|
335 | + show_error('The 404 view [' . $path . '] does not exist'); |
|
336 | 336 | } |
337 | 337 | } |
338 | 338 | |
@@ -340,12 +340,12 @@ discard block |
||
340 | 340 | * Display the error to user |
341 | 341 | * @param array $data the error information |
342 | 342 | */ |
343 | - public static function sendError(array $data = array()){ |
|
343 | + public static function sendError(array $data = array()) { |
|
344 | 344 | $path = CORE_VIEWS_PATH . 'errors.php'; |
345 | - if(file_exists($path)){ |
|
345 | + if (file_exists($path)) { |
|
346 | 346 | //compress the output if is available |
347 | 347 | $type = null; |
348 | - if (self::$_canCompressOutput){ |
|
348 | + if (self::$_canCompressOutput) { |
|
349 | 349 | $type = 'ob_gzhandler'; |
350 | 350 | } |
351 | 351 | ob_start($type); |
@@ -355,7 +355,7 @@ discard block |
||
355 | 355 | self::sendHeaders(503); |
356 | 356 | echo $output; |
357 | 357 | } |
358 | - else{ |
|
358 | + else { |
|
359 | 359 | //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
360 | 360 | set_http_status_header(503); |
361 | 361 | echo 'The error view [' . $path . '] does not exist'; |
@@ -367,17 +367,17 @@ discard block |
||
367 | 367 | * @param array $cacheInfo the cache information |
368 | 368 | * @return boolean true if the information is sent otherwise false |
369 | 369 | */ |
370 | - protected function sendCacheNotYetExpireInfo($cacheInfo){ |
|
371 | - if(! empty($cacheInfo)){ |
|
370 | + protected function sendCacheNotYetExpireInfo($cacheInfo) { |
|
371 | + if (!empty($cacheInfo)) { |
|
372 | 372 | $logger = self::getLogger(); |
373 | 373 | $lastModified = $cacheInfo['mtime']; |
374 | 374 | $expire = $cacheInfo['expire']; |
375 | 375 | $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
376 | 376 | self::setHeader('Pragma', 'public'); |
377 | 377 | self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
378 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
379 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
380 | - if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
378 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire) . ' GMT'); |
|
379 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
|
380 | + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { |
|
381 | 381 | $logger->info('The cache page content is not yet expire for the URL [' . $this->_currentUrl . '] send 304 header to browser'); |
382 | 382 | self::sendHeaders(304); |
383 | 383 | return true; |
@@ -392,7 +392,7 @@ discard block |
||
392 | 392 | * @return string the page content after replace |
393 | 393 | * '{elapsed_time}', '{memory_usage}' |
394 | 394 | */ |
395 | - protected function replaceElapseTimeAndMemoryUsage($content){ |
|
395 | + protected function replaceElapseTimeAndMemoryUsage($content) { |
|
396 | 396 | //load benchmark class |
397 | 397 | $benchmark = & class_loader('Benchmark'); |
398 | 398 | |
@@ -408,7 +408,7 @@ discard block |
||
408 | 408 | * @param object $cache the cache instance |
409 | 409 | * @return boolean the status of the operation |
410 | 410 | */ |
411 | - protected function sendCachePageContentToBrowser(&$cache){ |
|
411 | + protected function sendCachePageContentToBrowser(&$cache) { |
|
412 | 412 | $logger = self::getLogger(); |
413 | 413 | $logger->info('The cache page content is expired or the browser doesn\'t send the HTTP_IF_MODIFIED_SINCE header for the URL [' . $this->_currentUrl . '] send cache headers to tell the browser'); |
414 | 414 | self::sendHeaders(200); |
@@ -416,13 +416,13 @@ discard block |
||
416 | 416 | $pageCacheKey = $this->_currentUrlCacheKey; |
417 | 417 | //get the cache content |
418 | 418 | $content = $cache->get($pageCacheKey); |
419 | - if($content){ |
|
419 | + if ($content) { |
|
420 | 420 | $logger->info('The page content for the URL [' . $this->_currentUrl . '] already cached just display it'); |
421 | 421 | $content = $this->replaceElapseTimeAndMemoryUsage($content); |
422 | 422 | ///display the final output |
423 | 423 | //compress the output if is available |
424 | 424 | $type = null; |
425 | - if (self::$_canCompressOutput){ |
|
425 | + if (self::$_canCompressOutput) { |
|
426 | 426 | $type = 'ob_gzhandler'; |
427 | 427 | } |
428 | 428 | ob_start($type); |
@@ -440,7 +440,7 @@ discard block |
||
440 | 440 | * @param string $content the page content to be saved |
441 | 441 | * @return void |
442 | 442 | */ |
443 | - protected function savePageContentIntoCache($content){ |
|
443 | + protected function savePageContentIntoCache($content) { |
|
444 | 444 | $obj = & get_instance(); |
445 | 445 | $logger = self::getLogger(); |
446 | 446 | |
@@ -448,7 +448,7 @@ discard block |
||
448 | 448 | $url = $this->_currentUrl; |
449 | 449 | //Cache view Time to live in second |
450 | 450 | $viewCacheTtl = get_config('cache_ttl'); |
451 | - if (!empty($obj->view_cache_ttl)){ |
|
451 | + if (!empty($obj->view_cache_ttl)) { |
|
452 | 452 | $viewCacheTtl = $obj->view_cache_ttl; |
453 | 453 | } |
454 | 454 | //the cache handler instance |
@@ -460,14 +460,14 @@ discard block |
||
460 | 460 | |
461 | 461 | //get the cache information to prepare header to send to browser |
462 | 462 | $cacheInfo = $cacheInstance->getInfo($cacheKey); |
463 | - if($cacheInfo){ |
|
463 | + if ($cacheInfo) { |
|
464 | 464 | $lastModified = $cacheInfo['mtime']; |
465 | 465 | $expire = $cacheInfo['expire']; |
466 | 466 | $maxAge = $expire - time(); |
467 | 467 | self::setHeader('Pragma', 'public'); |
468 | 468 | self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
469 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
470 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
469 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire) . ' GMT'); |
|
470 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
|
471 | 471 | } |
472 | 472 | } |
473 | 473 | |
@@ -483,21 +483,21 @@ discard block |
||
483 | 483 | * 'viewFile' => 'view_file' |
484 | 484 | * ) |
485 | 485 | */ |
486 | - protected function getModuleInfoForView($view){ |
|
486 | + protected function getModuleInfoForView($view) { |
|
487 | 487 | $module = null; |
488 | 488 | $viewFile = null; |
489 | 489 | $obj = & get_instance(); |
490 | 490 | //check if the request class contains module name |
491 | - if(strpos($view, '/') !== false){ |
|
491 | + if (strpos($view, '/') !== false) { |
|
492 | 492 | $viewPath = explode('/', $view); |
493 | - if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
493 | + if (isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())) { |
|
494 | 494 | $module = $viewPath[0]; |
495 | 495 | array_shift($viewPath); |
496 | 496 | $view = implode('/', $viewPath); |
497 | 497 | $viewFile = $view . '.php'; |
498 | 498 | } |
499 | 499 | } |
500 | - if(! $module && !empty($obj->moduleName)){ |
|
500 | + if (!$module && !empty($obj->moduleName)) { |
|
501 | 501 | $module = $obj->moduleName; |
502 | 502 | } |
503 | 503 | return array( |
@@ -512,13 +512,13 @@ discard block |
||
512 | 512 | * @see Response::render |
513 | 513 | * @return void|string |
514 | 514 | */ |
515 | - protected function loadView($path, array $data = array(), $return = false){ |
|
515 | + protected function loadView($path, array $data = array(), $return = false) { |
|
516 | 516 | $found = false; |
517 | - if(file_exists($path)){ |
|
517 | + if (file_exists($path)) { |
|
518 | 518 | //super instance |
519 | 519 | $obj = & get_instance(); |
520 | - foreach(get_object_vars($obj) as $key => $value){ |
|
521 | - if(! isset($this->{$key})){ |
|
520 | + foreach (get_object_vars($obj) as $key => $value) { |
|
521 | + if (!isset($this->{$key})) { |
|
522 | 522 | $this->{$key} = & $obj->{$key}; |
523 | 523 | } |
524 | 524 | } |
@@ -527,14 +527,14 @@ discard block |
||
527 | 527 | //need use require() instead of require_once because can load this view many time |
528 | 528 | require $path; |
529 | 529 | $content = ob_get_clean(); |
530 | - if($return){ |
|
530 | + if ($return) { |
|
531 | 531 | return $content; |
532 | 532 | } |
533 | 533 | $this->_pageRender .= $content; |
534 | 534 | $found = true; |
535 | 535 | } |
536 | - if(! $found){ |
|
537 | - show_error('Unable to find view [' .$view . ']'); |
|
536 | + if (!$found) { |
|
537 | + show_error('Unable to find view [' . $view . ']'); |
|
538 | 538 | } |
539 | 539 | } |
540 | 540 |
@@ -1,293 +1,293 @@ |
||
1 | 1 | <?php |
2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
3 | - /** |
|
4 | - * TNH Framework |
|
5 | - * |
|
6 | - * A simple PHP framework using HMVC architecture |
|
7 | - * |
|
8 | - * This content is released under the GNU GPL License (GPL) |
|
9 | - * |
|
10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
11 | - * |
|
12 | - * This program is free software; you can redistribute it and/or |
|
13 | - * modify it under the terms of the GNU General Public License |
|
14 | - * as published by the Free Software Foundation; either version 3 |
|
15 | - * of the License, or (at your option) any later version. |
|
16 | - * |
|
17 | - * This program is distributed in the hope that it will be useful, |
|
18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | - * GNU General Public License for more details. |
|
21 | - * |
|
22 | - * You should have received a copy of the GNU General Public License |
|
23 | - * along with this program; if not, write to the Free Software |
|
24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | - */ |
|
2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
3 | + /** |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | 26 | |
27 | - class Log{ |
|
27 | + class Log{ |
|
28 | 28 | |
29 | - /** |
|
30 | - * The defined constante for Log level |
|
31 | - */ |
|
32 | - const NONE = 99999999; |
|
33 | - const FATAL = 500; |
|
34 | - const ERROR = 400; |
|
35 | - const WARNING = 300; |
|
36 | - const INFO = 200; |
|
37 | - const DEBUG = 100; |
|
38 | - const ALL = -99999999; |
|
29 | + /** |
|
30 | + * The defined constante for Log level |
|
31 | + */ |
|
32 | + const NONE = 99999999; |
|
33 | + const FATAL = 500; |
|
34 | + const ERROR = 400; |
|
35 | + const WARNING = 300; |
|
36 | + const INFO = 200; |
|
37 | + const DEBUG = 100; |
|
38 | + const ALL = -99999999; |
|
39 | 39 | |
40 | - /** |
|
41 | - * The logger name |
|
42 | - * @var string |
|
43 | - */ |
|
44 | - private $logger = 'ROOT_LOGGER'; |
|
40 | + /** |
|
41 | + * The logger name |
|
42 | + * @var string |
|
43 | + */ |
|
44 | + private $logger = 'ROOT_LOGGER'; |
|
45 | 45 | |
46 | - /** |
|
47 | - * List of valid log level to be checked for the configuration |
|
48 | - * @var array |
|
49 | - */ |
|
50 | - private static $validConfigLevel = array('off', 'none', 'fatal', 'error', 'warning', 'warn', 'info', 'debug', 'all'); |
|
46 | + /** |
|
47 | + * List of valid log level to be checked for the configuration |
|
48 | + * @var array |
|
49 | + */ |
|
50 | + private static $validConfigLevel = array('off', 'none', 'fatal', 'error', 'warning', 'warn', 'info', 'debug', 'all'); |
|
51 | 51 | |
52 | - /** |
|
53 | - * Create new Log instance |
|
54 | - */ |
|
55 | - public function __construct(){ |
|
56 | - } |
|
52 | + /** |
|
53 | + * Create new Log instance |
|
54 | + */ |
|
55 | + public function __construct(){ |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Set the logger to identify each message in the log |
|
60 | - * @param string $logger the logger name |
|
61 | - */ |
|
62 | - public function setLogger($logger){ |
|
63 | - $this->logger = $logger; |
|
64 | - } |
|
58 | + /** |
|
59 | + * Set the logger to identify each message in the log |
|
60 | + * @param string $logger the logger name |
|
61 | + */ |
|
62 | + public function setLogger($logger){ |
|
63 | + $this->logger = $logger; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Save the fatal message in the log |
|
68 | - * @see Log::writeLog for more detail |
|
69 | - * @param string $message the log message to save |
|
70 | - */ |
|
71 | - public function fatal($message){ |
|
72 | - $this->writeLog($message, self::FATAL); |
|
73 | - } |
|
66 | + /** |
|
67 | + * Save the fatal message in the log |
|
68 | + * @see Log::writeLog for more detail |
|
69 | + * @param string $message the log message to save |
|
70 | + */ |
|
71 | + public function fatal($message){ |
|
72 | + $this->writeLog($message, self::FATAL); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Save the error message in the log |
|
77 | - * @see Log::writeLog for more detail |
|
78 | - * @param string $message the log message to save |
|
79 | - */ |
|
80 | - public function error($message){ |
|
81 | - $this->writeLog($message, self::ERROR); |
|
82 | - } |
|
75 | + /** |
|
76 | + * Save the error message in the log |
|
77 | + * @see Log::writeLog for more detail |
|
78 | + * @param string $message the log message to save |
|
79 | + */ |
|
80 | + public function error($message){ |
|
81 | + $this->writeLog($message, self::ERROR); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Save the warning message in the log |
|
86 | - * @see Log::writeLog for more detail |
|
87 | - * @param string $message the log message to save |
|
88 | - */ |
|
89 | - public function warning($message){ |
|
90 | - $this->writeLog($message, self::WARNING); |
|
91 | - } |
|
84 | + /** |
|
85 | + * Save the warning message in the log |
|
86 | + * @see Log::writeLog for more detail |
|
87 | + * @param string $message the log message to save |
|
88 | + */ |
|
89 | + public function warning($message){ |
|
90 | + $this->writeLog($message, self::WARNING); |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Save the info message in the log |
|
95 | - * @see Log::writeLog for more detail |
|
96 | - * @param string $message the log message to save |
|
97 | - */ |
|
98 | - public function info($message){ |
|
99 | - $this->writeLog($message, self::INFO); |
|
100 | - } |
|
93 | + /** |
|
94 | + * Save the info message in the log |
|
95 | + * @see Log::writeLog for more detail |
|
96 | + * @param string $message the log message to save |
|
97 | + */ |
|
98 | + public function info($message){ |
|
99 | + $this->writeLog($message, self::INFO); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Save the debug message in the log |
|
104 | - * @see Log::writeLog for more detail |
|
105 | - * @param string $message the log message to save |
|
106 | - */ |
|
107 | - public function debug($message){ |
|
108 | - $this->writeLog($message, self::DEBUG); |
|
109 | - } |
|
102 | + /** |
|
103 | + * Save the debug message in the log |
|
104 | + * @see Log::writeLog for more detail |
|
105 | + * @param string $message the log message to save |
|
106 | + */ |
|
107 | + public function debug($message){ |
|
108 | + $this->writeLog($message, self::DEBUG); |
|
109 | + } |
|
110 | 110 | |
111 | 111 | |
112 | - /** |
|
113 | - * Save the log message |
|
114 | - * @param string $message the log message to be saved |
|
115 | - * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
|
116 | - * to allow check the log level threshold. |
|
117 | - */ |
|
118 | - public function writeLog($message, $level = self::INFO){ |
|
119 | - $configLogLevel = get_config('log_level'); |
|
120 | - if(! $configLogLevel){ |
|
121 | - //so means no need log just stop here |
|
122 | - return; |
|
123 | - } |
|
124 | - //check config log level |
|
125 | - if(! self::isValidConfigLevel($configLogLevel)){ |
|
126 | - //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
127 | - show_error('Invalid config log level [' . $configLogLevel . '], the value must be one of the following: ' . implode(', ', array_map('strtoupper', self::$validConfigLevel)), $title = 'Log Config Error', $logging = false); |
|
128 | - } |
|
112 | + /** |
|
113 | + * Save the log message |
|
114 | + * @param string $message the log message to be saved |
|
115 | + * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
|
116 | + * to allow check the log level threshold. |
|
117 | + */ |
|
118 | + public function writeLog($message, $level = self::INFO){ |
|
119 | + $configLogLevel = get_config('log_level'); |
|
120 | + if(! $configLogLevel){ |
|
121 | + //so means no need log just stop here |
|
122 | + return; |
|
123 | + } |
|
124 | + //check config log level |
|
125 | + if(! self::isValidConfigLevel($configLogLevel)){ |
|
126 | + //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
127 | + show_error('Invalid config log level [' . $configLogLevel . '], the value must be one of the following: ' . implode(', ', array_map('strtoupper', self::$validConfigLevel)), $title = 'Log Config Error', $logging = false); |
|
128 | + } |
|
129 | 129 | |
130 | - //check if config log_logger_name and current log can save log data |
|
131 | - if(! $this->canSaveLogDataForLogger()){ |
|
132 | - return; |
|
133 | - } |
|
130 | + //check if config log_logger_name and current log can save log data |
|
131 | + if(! $this->canSaveLogDataForLogger()){ |
|
132 | + return; |
|
133 | + } |
|
134 | 134 | |
135 | - //if $level is not an integer |
|
136 | - if(! is_numeric($level)){ |
|
137 | - $level = self::getLevelValue($level); |
|
138 | - } |
|
135 | + //if $level is not an integer |
|
136 | + if(! is_numeric($level)){ |
|
137 | + $level = self::getLevelValue($level); |
|
138 | + } |
|
139 | 139 | |
140 | - //check if can logging regarding the log level config |
|
141 | - $configLevel = self::getLevelValue($configLogLevel); |
|
142 | - if($configLevel > $level){ |
|
143 | - //can't log |
|
144 | - return; |
|
145 | - } |
|
146 | - //check log file and directory |
|
147 | - $path = $this->checkAndSetLogFileDirectory(); |
|
148 | - //save the log data |
|
149 | - $this->saveLogData($path, $level, $message); |
|
150 | - } |
|
140 | + //check if can logging regarding the log level config |
|
141 | + $configLevel = self::getLevelValue($configLogLevel); |
|
142 | + if($configLevel > $level){ |
|
143 | + //can't log |
|
144 | + return; |
|
145 | + } |
|
146 | + //check log file and directory |
|
147 | + $path = $this->checkAndSetLogFileDirectory(); |
|
148 | + //save the log data |
|
149 | + $this->saveLogData($path, $level, $message); |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * Save the log data into file |
|
154 | - * @param string $path the path of the log file |
|
155 | - * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
|
156 | - * @param string $message the log message to save |
|
157 | - * @return void |
|
158 | - */ |
|
159 | - protected function saveLogData($path, $level, $message){ |
|
160 | - //may be at this time helper user_agent not yet included |
|
161 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
152 | + /** |
|
153 | + * Save the log data into file |
|
154 | + * @param string $path the path of the log file |
|
155 | + * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
|
156 | + * @param string $message the log message to save |
|
157 | + * @return void |
|
158 | + */ |
|
159 | + protected function saveLogData($path, $level, $message){ |
|
160 | + //may be at this time helper user_agent not yet included |
|
161 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
162 | 162 | |
163 | - ///////////////////// date ////////////// |
|
164 | - $timestampWithMicro = microtime(true); |
|
165 | - $microtime = sprintf('%06d', ($timestampWithMicro - floor($timestampWithMicro)) * 1000000); |
|
166 | - $dateTime = new DateTime(date('Y-m-d H:i:s.' . $microtime, $timestampWithMicro)); |
|
167 | - $logDate = $dateTime->format('Y-m-d H:i:s.u'); |
|
168 | - //ip |
|
169 | - $ip = get_ip(); |
|
163 | + ///////////////////// date ////////////// |
|
164 | + $timestampWithMicro = microtime(true); |
|
165 | + $microtime = sprintf('%06d', ($timestampWithMicro - floor($timestampWithMicro)) * 1000000); |
|
166 | + $dateTime = new DateTime(date('Y-m-d H:i:s.' . $microtime, $timestampWithMicro)); |
|
167 | + $logDate = $dateTime->format('Y-m-d H:i:s.u'); |
|
168 | + //ip |
|
169 | + $ip = get_ip(); |
|
170 | 170 | |
171 | - //if $level is not an integer |
|
172 | - if(! is_numeric($level)){ |
|
173 | - $level = self::getLevelValue($level); |
|
174 | - } |
|
171 | + //if $level is not an integer |
|
172 | + if(! is_numeric($level)){ |
|
173 | + $level = self::getLevelValue($level); |
|
174 | + } |
|
175 | 175 | |
176 | - //level name |
|
177 | - $levelName = self::getLevelName($level); |
|
176 | + //level name |
|
177 | + $levelName = self::getLevelName($level); |
|
178 | 178 | |
179 | - //debug info |
|
180 | - $dtrace = debug_backtrace(); |
|
181 | - $fileInfo = $dtrace[0]; |
|
182 | - if ($dtrace[0]['file'] == __FILE__){ |
|
183 | - $fileInfo = $dtrace[1]; |
|
184 | - } |
|
179 | + //debug info |
|
180 | + $dtrace = debug_backtrace(); |
|
181 | + $fileInfo = $dtrace[0]; |
|
182 | + if ($dtrace[0]['file'] == __FILE__){ |
|
183 | + $fileInfo = $dtrace[1]; |
|
184 | + } |
|
185 | 185 | |
186 | - $str = $logDate . ' [' . str_pad($levelName, 7 /*warning len*/) . '] ' . ' [' . str_pad($ip, 15) . '] ' . $this->logger . ' : ' . $message . ' ' . '[' . $fileInfo['file'] . '::' . $fileInfo['line'] . ']' . "\n"; |
|
187 | - $fp = fopen($path, 'a+'); |
|
188 | - if(is_resource($fp)){ |
|
189 | - flock($fp, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
190 | - fwrite($fp, $str); |
|
191 | - fclose($fp); |
|
192 | - } |
|
193 | - } |
|
186 | + $str = $logDate . ' [' . str_pad($levelName, 7 /*warning len*/) . '] ' . ' [' . str_pad($ip, 15) . '] ' . $this->logger . ' : ' . $message . ' ' . '[' . $fileInfo['file'] . '::' . $fileInfo['line'] . ']' . "\n"; |
|
187 | + $fp = fopen($path, 'a+'); |
|
188 | + if(is_resource($fp)){ |
|
189 | + flock($fp, LOCK_EX); // exclusive lock, will get released when the file is closed |
|
190 | + fwrite($fp, $str); |
|
191 | + fclose($fp); |
|
192 | + } |
|
193 | + } |
|
194 | 194 | |
195 | - /** |
|
196 | - * Check if the current logger can save log data regarding the configuration |
|
197 | - * of logger filter |
|
198 | - * @return boolean |
|
199 | - */ |
|
200 | - protected function canSaveLogDataForLogger(){ |
|
201 | - if(! empty($this->logger)){ |
|
202 | - $configLoggersName = get_config('log_logger_name', array()); |
|
203 | - if (!empty($configLoggersName)) { |
|
204 | - //for best comparaison put all string to lowercase |
|
205 | - $configLoggersName = array_map('strtolower', $configLoggersName); |
|
206 | - if(! in_array(strtolower($this->logger), $configLoggersName)){ |
|
207 | - return false; |
|
208 | - } |
|
209 | - } |
|
210 | - } |
|
211 | - return true; |
|
212 | - } |
|
195 | + /** |
|
196 | + * Check if the current logger can save log data regarding the configuration |
|
197 | + * of logger filter |
|
198 | + * @return boolean |
|
199 | + */ |
|
200 | + protected function canSaveLogDataForLogger(){ |
|
201 | + if(! empty($this->logger)){ |
|
202 | + $configLoggersName = get_config('log_logger_name', array()); |
|
203 | + if (!empty($configLoggersName)) { |
|
204 | + //for best comparaison put all string to lowercase |
|
205 | + $configLoggersName = array_map('strtolower', $configLoggersName); |
|
206 | + if(! in_array(strtolower($this->logger), $configLoggersName)){ |
|
207 | + return false; |
|
208 | + } |
|
209 | + } |
|
210 | + } |
|
211 | + return true; |
|
212 | + } |
|
213 | 213 | |
214 | - /** |
|
215 | - * Check the file and directory |
|
216 | - * @return string the log file path |
|
217 | - */ |
|
218 | - protected function checkAndSetLogFileDirectory(){ |
|
219 | - $logSavePath = get_config('log_save_path'); |
|
220 | - if(! $logSavePath){ |
|
221 | - $logSavePath = LOGS_PATH; |
|
222 | - } |
|
214 | + /** |
|
215 | + * Check the file and directory |
|
216 | + * @return string the log file path |
|
217 | + */ |
|
218 | + protected function checkAndSetLogFileDirectory(){ |
|
219 | + $logSavePath = get_config('log_save_path'); |
|
220 | + if(! $logSavePath){ |
|
221 | + $logSavePath = LOGS_PATH; |
|
222 | + } |
|
223 | 223 | |
224 | - if(! is_dir($logSavePath) || !is_writable($logSavePath)){ |
|
225 | - //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
226 | - show_error('Error : the log dir does not exists or is not writable', $title = 'Log directory error', $logging = false); |
|
227 | - } |
|
224 | + if(! is_dir($logSavePath) || !is_writable($logSavePath)){ |
|
225 | + //NOTE: here need put the show_error() "logging" to false to prevent loop |
|
226 | + show_error('Error : the log dir does not exists or is not writable', $title = 'Log directory error', $logging = false); |
|
227 | + } |
|
228 | 228 | |
229 | - $path = $logSavePath . 'logs-' . date('Y-m-d') . '.log'; |
|
230 | - if(! file_exists($path)){ |
|
231 | - touch($path); |
|
232 | - } |
|
233 | - return $path; |
|
234 | - } |
|
229 | + $path = $logSavePath . 'logs-' . date('Y-m-d') . '.log'; |
|
230 | + if(! file_exists($path)){ |
|
231 | + touch($path); |
|
232 | + } |
|
233 | + return $path; |
|
234 | + } |
|
235 | 235 | |
236 | - /** |
|
237 | - * Check if the given log level is valid |
|
238 | - * |
|
239 | - * @param string $level the log level |
|
240 | - * |
|
241 | - * @return boolean true if the given log level is valid, false if not |
|
242 | - */ |
|
243 | - protected static function isValidConfigLevel($level){ |
|
244 | - $level = strtolower($level); |
|
245 | - return in_array($level, self::$validConfigLevel); |
|
246 | - } |
|
236 | + /** |
|
237 | + * Check if the given log level is valid |
|
238 | + * |
|
239 | + * @param string $level the log level |
|
240 | + * |
|
241 | + * @return boolean true if the given log level is valid, false if not |
|
242 | + */ |
|
243 | + protected static function isValidConfigLevel($level){ |
|
244 | + $level = strtolower($level); |
|
245 | + return in_array($level, self::$validConfigLevel); |
|
246 | + } |
|
247 | 247 | |
248 | - /** |
|
249 | - * Get the log level number for the given level string |
|
250 | - * @param string $level the log level in string format |
|
251 | - * |
|
252 | - * @return int the log level in integer format using the predefined constants |
|
253 | - */ |
|
254 | - protected static function getLevelValue($level){ |
|
255 | - $level = strtolower($level); |
|
256 | - $levelMaps = array( |
|
257 | - 'fatal' => self::FATAL, |
|
258 | - 'error' => self::ERROR, |
|
259 | - 'warning' => self::WARNING, |
|
260 | - 'warn' => self::WARNING, |
|
261 | - 'info' => self::INFO, |
|
262 | - 'debug' => self::DEBUG, |
|
263 | - 'all' => self::ALL |
|
264 | - ); |
|
265 | - //the default value is NONE, so means no need test for NONE |
|
266 | - $value = self::NONE; |
|
267 | - if(isset($levelMaps[$level])){ |
|
268 | - $value = $levelMaps[$level]; |
|
269 | - } |
|
270 | - return $value; |
|
271 | - } |
|
248 | + /** |
|
249 | + * Get the log level number for the given level string |
|
250 | + * @param string $level the log level in string format |
|
251 | + * |
|
252 | + * @return int the log level in integer format using the predefined constants |
|
253 | + */ |
|
254 | + protected static function getLevelValue($level){ |
|
255 | + $level = strtolower($level); |
|
256 | + $levelMaps = array( |
|
257 | + 'fatal' => self::FATAL, |
|
258 | + 'error' => self::ERROR, |
|
259 | + 'warning' => self::WARNING, |
|
260 | + 'warn' => self::WARNING, |
|
261 | + 'info' => self::INFO, |
|
262 | + 'debug' => self::DEBUG, |
|
263 | + 'all' => self::ALL |
|
264 | + ); |
|
265 | + //the default value is NONE, so means no need test for NONE |
|
266 | + $value = self::NONE; |
|
267 | + if(isset($levelMaps[$level])){ |
|
268 | + $value = $levelMaps[$level]; |
|
269 | + } |
|
270 | + return $value; |
|
271 | + } |
|
272 | 272 | |
273 | - /** |
|
274 | - * Get the log level string for the given log level integer |
|
275 | - * @param integer $level the log level in integer format |
|
276 | - * @return string the log level in string format |
|
277 | - */ |
|
278 | - protected static function getLevelName($level){ |
|
279 | - $levelMaps = array( |
|
280 | - self::FATAL => 'FATAL', |
|
281 | - self::ERROR => 'ERROR', |
|
282 | - self::WARNING => 'WARNING', |
|
283 | - self::INFO => 'INFO', |
|
284 | - self::DEBUG => 'DEBUG' |
|
285 | - ); |
|
286 | - $value = ''; |
|
287 | - if(isset($levelMaps[$level])){ |
|
288 | - $value = $levelMaps[$level]; |
|
289 | - } |
|
290 | - return $value; |
|
291 | - } |
|
273 | + /** |
|
274 | + * Get the log level string for the given log level integer |
|
275 | + * @param integer $level the log level in integer format |
|
276 | + * @return string the log level in string format |
|
277 | + */ |
|
278 | + protected static function getLevelName($level){ |
|
279 | + $levelMaps = array( |
|
280 | + self::FATAL => 'FATAL', |
|
281 | + self::ERROR => 'ERROR', |
|
282 | + self::WARNING => 'WARNING', |
|
283 | + self::INFO => 'INFO', |
|
284 | + self::DEBUG => 'DEBUG' |
|
285 | + ); |
|
286 | + $value = ''; |
|
287 | + if(isset($levelMaps[$level])){ |
|
288 | + $value = $levelMaps[$level]; |
|
289 | + } |
|
290 | + return $value; |
|
291 | + } |
|
292 | 292 | |
293 | - } |
|
293 | + } |
@@ -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 Log{ |
|
27 | + class Log { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * The defined constante for Log level |
@@ -52,14 +52,14 @@ discard block |
||
52 | 52 | /** |
53 | 53 | * Create new Log instance |
54 | 54 | */ |
55 | - public function __construct(){ |
|
55 | + public function __construct() { |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
59 | 59 | * Set the logger to identify each message in the log |
60 | 60 | * @param string $logger the logger name |
61 | 61 | */ |
62 | - public function setLogger($logger){ |
|
62 | + public function setLogger($logger) { |
|
63 | 63 | $this->logger = $logger; |
64 | 64 | } |
65 | 65 | |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | * @see Log::writeLog for more detail |
69 | 69 | * @param string $message the log message to save |
70 | 70 | */ |
71 | - public function fatal($message){ |
|
71 | + public function fatal($message) { |
|
72 | 72 | $this->writeLog($message, self::FATAL); |
73 | 73 | } |
74 | 74 | |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | * @see Log::writeLog for more detail |
78 | 78 | * @param string $message the log message to save |
79 | 79 | */ |
80 | - public function error($message){ |
|
80 | + public function error($message) { |
|
81 | 81 | $this->writeLog($message, self::ERROR); |
82 | 82 | } |
83 | 83 | |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | * @see Log::writeLog for more detail |
87 | 87 | * @param string $message the log message to save |
88 | 88 | */ |
89 | - public function warning($message){ |
|
89 | + public function warning($message) { |
|
90 | 90 | $this->writeLog($message, self::WARNING); |
91 | 91 | } |
92 | 92 | |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | * @see Log::writeLog for more detail |
96 | 96 | * @param string $message the log message to save |
97 | 97 | */ |
98 | - public function info($message){ |
|
98 | + public function info($message) { |
|
99 | 99 | $this->writeLog($message, self::INFO); |
100 | 100 | } |
101 | 101 | |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | * @see Log::writeLog for more detail |
105 | 105 | * @param string $message the log message to save |
106 | 106 | */ |
107 | - public function debug($message){ |
|
107 | + public function debug($message) { |
|
108 | 108 | $this->writeLog($message, self::DEBUG); |
109 | 109 | } |
110 | 110 | |
@@ -115,31 +115,31 @@ discard block |
||
115 | 115 | * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
116 | 116 | * to allow check the log level threshold. |
117 | 117 | */ |
118 | - public function writeLog($message, $level = self::INFO){ |
|
118 | + public function writeLog($message, $level = self::INFO) { |
|
119 | 119 | $configLogLevel = get_config('log_level'); |
120 | - if(! $configLogLevel){ |
|
120 | + if (!$configLogLevel) { |
|
121 | 121 | //so means no need log just stop here |
122 | 122 | return; |
123 | 123 | } |
124 | 124 | //check config log level |
125 | - if(! self::isValidConfigLevel($configLogLevel)){ |
|
125 | + if (!self::isValidConfigLevel($configLogLevel)) { |
|
126 | 126 | //NOTE: here need put the show_error() "logging" to false to prevent loop |
127 | 127 | show_error('Invalid config log level [' . $configLogLevel . '], the value must be one of the following: ' . implode(', ', array_map('strtoupper', self::$validConfigLevel)), $title = 'Log Config Error', $logging = false); |
128 | 128 | } |
129 | 129 | |
130 | 130 | //check if config log_logger_name and current log can save log data |
131 | - if(! $this->canSaveLogDataForLogger()){ |
|
131 | + if (!$this->canSaveLogDataForLogger()) { |
|
132 | 132 | return; |
133 | 133 | } |
134 | 134 | |
135 | 135 | //if $level is not an integer |
136 | - if(! is_numeric($level)){ |
|
136 | + if (!is_numeric($level)) { |
|
137 | 137 | $level = self::getLevelValue($level); |
138 | 138 | } |
139 | 139 | |
140 | 140 | //check if can logging regarding the log level config |
141 | 141 | $configLevel = self::getLevelValue($configLogLevel); |
142 | - if($configLevel > $level){ |
|
142 | + if ($configLevel > $level) { |
|
143 | 143 | //can't log |
144 | 144 | return; |
145 | 145 | } |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | * @param string $message the log message to save |
157 | 157 | * @return void |
158 | 158 | */ |
159 | - protected function saveLogData($path, $level, $message){ |
|
159 | + protected function saveLogData($path, $level, $message) { |
|
160 | 160 | //may be at this time helper user_agent not yet included |
161 | 161 | require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
162 | 162 | |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | $ip = get_ip(); |
170 | 170 | |
171 | 171 | //if $level is not an integer |
172 | - if(! is_numeric($level)){ |
|
172 | + if (!is_numeric($level)) { |
|
173 | 173 | $level = self::getLevelValue($level); |
174 | 174 | } |
175 | 175 | |
@@ -179,13 +179,13 @@ discard block |
||
179 | 179 | //debug info |
180 | 180 | $dtrace = debug_backtrace(); |
181 | 181 | $fileInfo = $dtrace[0]; |
182 | - if ($dtrace[0]['file'] == __FILE__){ |
|
182 | + if ($dtrace[0]['file'] == __FILE__) { |
|
183 | 183 | $fileInfo = $dtrace[1]; |
184 | 184 | } |
185 | 185 | |
186 | 186 | $str = $logDate . ' [' . str_pad($levelName, 7 /*warning len*/) . '] ' . ' [' . str_pad($ip, 15) . '] ' . $this->logger . ' : ' . $message . ' ' . '[' . $fileInfo['file'] . '::' . $fileInfo['line'] . ']' . "\n"; |
187 | 187 | $fp = fopen($path, 'a+'); |
188 | - if(is_resource($fp)){ |
|
188 | + if (is_resource($fp)) { |
|
189 | 189 | flock($fp, LOCK_EX); // exclusive lock, will get released when the file is closed |
190 | 190 | fwrite($fp, $str); |
191 | 191 | fclose($fp); |
@@ -197,13 +197,13 @@ discard block |
||
197 | 197 | * of logger filter |
198 | 198 | * @return boolean |
199 | 199 | */ |
200 | - protected function canSaveLogDataForLogger(){ |
|
201 | - if(! empty($this->logger)){ |
|
200 | + protected function canSaveLogDataForLogger() { |
|
201 | + if (!empty($this->logger)) { |
|
202 | 202 | $configLoggersName = get_config('log_logger_name', array()); |
203 | 203 | if (!empty($configLoggersName)) { |
204 | 204 | //for best comparaison put all string to lowercase |
205 | 205 | $configLoggersName = array_map('strtolower', $configLoggersName); |
206 | - if(! in_array(strtolower($this->logger), $configLoggersName)){ |
|
206 | + if (!in_array(strtolower($this->logger), $configLoggersName)) { |
|
207 | 207 | return false; |
208 | 208 | } |
209 | 209 | } |
@@ -215,19 +215,19 @@ discard block |
||
215 | 215 | * Check the file and directory |
216 | 216 | * @return string the log file path |
217 | 217 | */ |
218 | - protected function checkAndSetLogFileDirectory(){ |
|
218 | + protected function checkAndSetLogFileDirectory() { |
|
219 | 219 | $logSavePath = get_config('log_save_path'); |
220 | - if(! $logSavePath){ |
|
220 | + if (!$logSavePath) { |
|
221 | 221 | $logSavePath = LOGS_PATH; |
222 | 222 | } |
223 | 223 | |
224 | - if(! is_dir($logSavePath) || !is_writable($logSavePath)){ |
|
224 | + if (!is_dir($logSavePath) || !is_writable($logSavePath)) { |
|
225 | 225 | //NOTE: here need put the show_error() "logging" to false to prevent loop |
226 | 226 | show_error('Error : the log dir does not exists or is not writable', $title = 'Log directory error', $logging = false); |
227 | 227 | } |
228 | 228 | |
229 | 229 | $path = $logSavePath . 'logs-' . date('Y-m-d') . '.log'; |
230 | - if(! file_exists($path)){ |
|
230 | + if (!file_exists($path)) { |
|
231 | 231 | touch($path); |
232 | 232 | } |
233 | 233 | return $path; |
@@ -240,7 +240,7 @@ discard block |
||
240 | 240 | * |
241 | 241 | * @return boolean true if the given log level is valid, false if not |
242 | 242 | */ |
243 | - protected static function isValidConfigLevel($level){ |
|
243 | + protected static function isValidConfigLevel($level) { |
|
244 | 244 | $level = strtolower($level); |
245 | 245 | return in_array($level, self::$validConfigLevel); |
246 | 246 | } |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | * |
252 | 252 | * @return int the log level in integer format using the predefined constants |
253 | 253 | */ |
254 | - protected static function getLevelValue($level){ |
|
254 | + protected static function getLevelValue($level) { |
|
255 | 255 | $level = strtolower($level); |
256 | 256 | $levelMaps = array( |
257 | 257 | 'fatal' => self::FATAL, |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | ); |
265 | 265 | //the default value is NONE, so means no need test for NONE |
266 | 266 | $value = self::NONE; |
267 | - if(isset($levelMaps[$level])){ |
|
267 | + if (isset($levelMaps[$level])) { |
|
268 | 268 | $value = $levelMaps[$level]; |
269 | 269 | } |
270 | 270 | return $value; |
@@ -275,7 +275,7 @@ discard block |
||
275 | 275 | * @param integer $level the log level in integer format |
276 | 276 | * @return string the log level in string format |
277 | 277 | */ |
278 | - protected static function getLevelName($level){ |
|
278 | + protected static function getLevelName($level) { |
|
279 | 279 | $levelMaps = array( |
280 | 280 | self::FATAL => 'FATAL', |
281 | 281 | self::ERROR => 'ERROR', |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | self::DEBUG => 'DEBUG' |
285 | 285 | ); |
286 | 286 | $value = ''; |
287 | - if(isset($levelMaps[$level])){ |
|
287 | + if (isset($levelMaps[$level])) { |
|
288 | 288 | $value = $levelMaps[$level]; |
289 | 289 | } |
290 | 290 | return $value; |
@@ -1,197 +1,197 @@ |
||
1 | 1 | <?php |
2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
3 | - /** |
|
4 | - * TNH Framework |
|
5 | - * |
|
6 | - * A simple PHP framework using HMVC architecture |
|
7 | - * |
|
8 | - * This content is released under the GNU GPL License (GPL) |
|
9 | - * |
|
10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
11 | - * |
|
12 | - * This program is free software; you can redistribute it and/or |
|
13 | - * modify it under the terms of the GNU General Public License |
|
14 | - * as published by the Free Software Foundation; either version 3 |
|
15 | - * of the License, or (at your option) any later version. |
|
16 | - * |
|
17 | - * This program is distributed in the hope that it will be useful, |
|
18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | - * GNU General Public License for more details. |
|
21 | - * |
|
22 | - * You should have received a copy of the GNU General Public License |
|
23 | - * along with this program; if not, write to the Free Software |
|
24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | - */ |
|
2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
3 | + /** |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | 26 | |
27 | - /** |
|
28 | - * @file bootstrap.php |
|
29 | - * |
|
30 | - * Contains the loading process: loading of constants, common functions, libraries |
|
31 | - * configurations, etc |
|
32 | - * verification of the environment and the routing of the request. |
|
33 | - * |
|
34 | - * @package core |
|
35 | - * @author Tony NGUEREZA |
|
36 | - * @copyright Copyright (c) 2017 |
|
37 | - * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
38 | - * @link http://www.iacademy.cf |
|
39 | - * @version 1.0.0 |
|
40 | - * @filesource |
|
41 | - */ |
|
27 | + /** |
|
28 | + * @file bootstrap.php |
|
29 | + * |
|
30 | + * Contains the loading process: loading of constants, common functions, libraries |
|
31 | + * configurations, etc |
|
32 | + * verification of the environment and the routing of the request. |
|
33 | + * |
|
34 | + * @package core |
|
35 | + * @author Tony NGUEREZA |
|
36 | + * @copyright Copyright (c) 2017 |
|
37 | + * @license https://opensource.org/licenses/gpl-3.0.html GNU GPL License (GPL) |
|
38 | + * @link http://www.iacademy.cf |
|
39 | + * @version 1.0.0 |
|
40 | + * @filesource |
|
41 | + */ |
|
42 | 42 | |
43 | - //if the application is running in CLI mode $_SESSION global variable is not available |
|
44 | - if(IS_CLI){ |
|
45 | - $_SESSION = array(); |
|
46 | - } |
|
43 | + //if the application is running in CLI mode $_SESSION global variable is not available |
|
44 | + if(IS_CLI){ |
|
45 | + $_SESSION = array(); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * inclusion of global constants of the environment that contain : name of the framework, |
|
50 | - * version, release date, version of PHP required, etc. |
|
51 | - */ |
|
52 | - require_once CORE_PATH . 'constants.php'; |
|
48 | + /** |
|
49 | + * inclusion of global constants of the environment that contain : name of the framework, |
|
50 | + * version, release date, version of PHP required, etc. |
|
51 | + */ |
|
52 | + require_once CORE_PATH . 'constants.php'; |
|
53 | 53 | |
54 | - /** |
|
55 | - * include file containing commons functions used in the framework such: show_error, |
|
56 | - * php_exception_handler, php_error_handler, get_instance, etc. |
|
57 | - */ |
|
58 | - require_once CORE_PATH . 'common.php'; |
|
54 | + /** |
|
55 | + * include file containing commons functions used in the framework such: show_error, |
|
56 | + * php_exception_handler, php_error_handler, get_instance, etc. |
|
57 | + */ |
|
58 | + require_once CORE_PATH . 'common.php'; |
|
59 | 59 | |
60 | - /** |
|
61 | - * The Benchmark class |
|
62 | - */ |
|
63 | - $BENCHMARK =& class_loader('Benchmark'); |
|
60 | + /** |
|
61 | + * The Benchmark class |
|
62 | + */ |
|
63 | + $BENCHMARK =& class_loader('Benchmark'); |
|
64 | 64 | |
65 | - $BENCHMARK->mark('APP_EXECUTION_START'); |
|
65 | + $BENCHMARK->mark('APP_EXECUTION_START'); |
|
66 | 66 | |
67 | - /** |
|
68 | - * instance of the Log class |
|
69 | - */ |
|
67 | + /** |
|
68 | + * instance of the Log class |
|
69 | + */ |
|
70 | 70 | $LOGGER =& class_loader('Log', 'classes'); |
71 | 71 | |
72 | 72 | $LOGGER->setLogger('ApplicationBootstrap'); |
73 | 73 | |
74 | 74 | $LOGGER->debug('Checking PHP version ...'); |
75 | 75 | |
76 | - /** |
|
77 | - * Verification of the PHP environment: minimum and maximum version |
|
78 | - */ |
|
79 | - if (version_compare(phpversion(), TNH_REQUIRED_PHP_MIN_VERSION, '<')){ |
|
80 | - show_error('Your PHP Version [' . phpversion() . '] is less than [' . TNH_REQUIRED_PHP_MIN_VERSION . '], please install a new version or update your PHP to the latest.', 'PHP Error environment'); |
|
81 | - } |
|
82 | - else if(version_compare(phpversion(), TNH_REQUIRED_PHP_MAX_VERSION, '>')){ |
|
83 | - show_error('Your PHP Version [' . phpversion() . '] is greather than [' . TNH_REQUIRED_PHP_MAX_VERSION . '] please install a PHP version that is compatible.', 'PHP Error environment'); |
|
84 | - } |
|
85 | - $LOGGER->info('PHP version [' . phpversion() . '] is OK [REQUIRED MINIMUM: ' . TNH_REQUIRED_PHP_MIN_VERSION . ', REQUIRED MAXIMUM: ' . TNH_REQUIRED_PHP_MAX_VERSION . '], application can work without any issue'); |
|
76 | + /** |
|
77 | + * Verification of the PHP environment: minimum and maximum version |
|
78 | + */ |
|
79 | + if (version_compare(phpversion(), TNH_REQUIRED_PHP_MIN_VERSION, '<')){ |
|
80 | + show_error('Your PHP Version [' . phpversion() . '] is less than [' . TNH_REQUIRED_PHP_MIN_VERSION . '], please install a new version or update your PHP to the latest.', 'PHP Error environment'); |
|
81 | + } |
|
82 | + else if(version_compare(phpversion(), TNH_REQUIRED_PHP_MAX_VERSION, '>')){ |
|
83 | + show_error('Your PHP Version [' . phpversion() . '] is greather than [' . TNH_REQUIRED_PHP_MAX_VERSION . '] please install a PHP version that is compatible.', 'PHP Error environment'); |
|
84 | + } |
|
85 | + $LOGGER->info('PHP version [' . phpversion() . '] is OK [REQUIRED MINIMUM: ' . TNH_REQUIRED_PHP_MIN_VERSION . ', REQUIRED MAXIMUM: ' . TNH_REQUIRED_PHP_MAX_VERSION . '], application can work without any issue'); |
|
86 | 86 | |
87 | - /** |
|
88 | - * Setting of the PHP error message handling function |
|
89 | - */ |
|
90 | - set_error_handler('php_error_handler'); |
|
87 | + /** |
|
88 | + * Setting of the PHP error message handling function |
|
89 | + */ |
|
90 | + set_error_handler('php_error_handler'); |
|
91 | 91 | |
92 | - /** |
|
93 | - * Setting of the PHP error exception handling function |
|
94 | - */ |
|
95 | - set_exception_handler('php_exception_handler'); |
|
92 | + /** |
|
93 | + * Setting of the PHP error exception handling function |
|
94 | + */ |
|
95 | + set_exception_handler('php_exception_handler'); |
|
96 | 96 | |
97 | - /** |
|
98 | - * Setting of the PHP shutdown handling function |
|
99 | - */ |
|
100 | - register_shutdown_function('php_shudown_handler'); |
|
97 | + /** |
|
98 | + * Setting of the PHP shutdown handling function |
|
99 | + */ |
|
100 | + register_shutdown_function('php_shudown_handler'); |
|
101 | 101 | |
102 | - //if user have some composer packages |
|
103 | - $LOGGER->debug('Check for composer autoload'); |
|
104 | - if(file_exists(VENDOR_PATH . 'autoload.php')){ |
|
105 | - $LOGGER->info('The composer autoload file exists include it'); |
|
106 | - require_once VENDOR_PATH . 'autoload.php'; |
|
107 | - } |
|
108 | - else{ |
|
109 | - $LOGGER->info('The composer autoload file does not exist skipping'); |
|
110 | - } |
|
102 | + //if user have some composer packages |
|
103 | + $LOGGER->debug('Check for composer autoload'); |
|
104 | + if(file_exists(VENDOR_PATH . 'autoload.php')){ |
|
105 | + $LOGGER->info('The composer autoload file exists include it'); |
|
106 | + require_once VENDOR_PATH . 'autoload.php'; |
|
107 | + } |
|
108 | + else{ |
|
109 | + $LOGGER->info('The composer autoload file does not exist skipping'); |
|
110 | + } |
|
111 | 111 | |
112 | - $LOGGER->debug('Begin to load the required resources'); |
|
112 | + $LOGGER->debug('Begin to load the required resources'); |
|
113 | 113 | |
114 | - /** |
|
115 | - * Load the EventInfo class file |
|
116 | - */ |
|
117 | - require_once CORE_CLASSES_PATH . 'EventInfo.php'; |
|
114 | + /** |
|
115 | + * Load the EventInfo class file |
|
116 | + */ |
|
117 | + require_once CORE_CLASSES_PATH . 'EventInfo.php'; |
|
118 | 118 | |
119 | 119 | |
120 | - $BENCHMARK->mark('CONFIG_INIT_START'); |
|
121 | - /** |
|
122 | - * Load configurations and using the |
|
123 | - * static method "init()" to initialize the Config class . |
|
124 | - */ |
|
125 | - $CONFIG =& class_loader('Config', 'classes'); |
|
126 | - $CONFIG->init(); |
|
127 | - $BENCHMARK->mark('CONFIG_INIT_END'); |
|
120 | + $BENCHMARK->mark('CONFIG_INIT_START'); |
|
121 | + /** |
|
122 | + * Load configurations and using the |
|
123 | + * static method "init()" to initialize the Config class . |
|
124 | + */ |
|
125 | + $CONFIG =& class_loader('Config', 'classes'); |
|
126 | + $CONFIG->init(); |
|
127 | + $BENCHMARK->mark('CONFIG_INIT_END'); |
|
128 | 128 | |
129 | - $BENCHMARK->mark('MODULE_INIT_START'); |
|
130 | - /** |
|
131 | - * Load modules and using the |
|
132 | - * static method "init()" to initialize the Module class. |
|
133 | - */ |
|
134 | - $MODULE =& class_loader('Module', 'classes'); |
|
135 | - $MODULE->init(); |
|
136 | - $BENCHMARK->mark('MODULE_INIT_END'); |
|
129 | + $BENCHMARK->mark('MODULE_INIT_START'); |
|
130 | + /** |
|
131 | + * Load modules and using the |
|
132 | + * static method "init()" to initialize the Module class. |
|
133 | + */ |
|
134 | + $MODULE =& class_loader('Module', 'classes'); |
|
135 | + $MODULE->init(); |
|
136 | + $BENCHMARK->mark('MODULE_INIT_END'); |
|
137 | 137 | |
138 | - $LOGGER->debug('Loading Base Controller ...'); |
|
139 | - /** |
|
140 | - * Include of the file containing the Base Controller class |
|
141 | - */ |
|
142 | - require_once CORE_CLASSES_PATH . 'Controller.php'; |
|
143 | - $LOGGER->info('Base Controller loaded successfully'); |
|
138 | + $LOGGER->debug('Loading Base Controller ...'); |
|
139 | + /** |
|
140 | + * Include of the file containing the Base Controller class |
|
141 | + */ |
|
142 | + require_once CORE_CLASSES_PATH . 'Controller.php'; |
|
143 | + $LOGGER->info('Base Controller loaded successfully'); |
|
144 | 144 | |
145 | - /** |
|
146 | - * Register controllers autoload function |
|
147 | - */ |
|
148 | - spl_autoload_register('autoload_controller'); |
|
145 | + /** |
|
146 | + * Register controllers autoload function |
|
147 | + */ |
|
148 | + spl_autoload_register('autoload_controller'); |
|
149 | 149 | |
150 | - /** |
|
151 | - * Loading Security class |
|
152 | - */ |
|
153 | - $SECURITY =& class_loader('Security', 'classes'); |
|
154 | - $SECURITY->checkWhiteListIpAccess(); |
|
150 | + /** |
|
151 | + * Loading Security class |
|
152 | + */ |
|
153 | + $SECURITY =& class_loader('Security', 'classes'); |
|
154 | + $SECURITY->checkWhiteListIpAccess(); |
|
155 | 155 | |
156 | - /** |
|
157 | - * Loading Url class |
|
158 | - */ |
|
159 | - $URL =& class_loader('Url', 'classes'); |
|
156 | + /** |
|
157 | + * Loading Url class |
|
158 | + */ |
|
159 | + $URL =& class_loader('Url', 'classes'); |
|
160 | 160 | |
161 | - if(get_config('cache_enable', false)){ |
|
162 | - /** |
|
163 | - * Load Cache interface file |
|
164 | - */ |
|
165 | - require_once CORE_CLASSES_CACHE_PATH . 'CacheInterface.php'; |
|
166 | - $cacheHandler = get_config('cache_handler'); |
|
167 | - if(! $cacheHandler){ |
|
168 | - show_error('The cache feature is enabled in the configuration but the cache handler class is not set.'); |
|
169 | - } |
|
170 | - $CACHE = null; |
|
171 | - //first check if the cache handler is the system driver |
|
172 | - if(file_exists(CORE_CLASSES_CACHE_PATH . $cacheHandler . '.php')){ |
|
173 | - $CACHE =& class_loader($cacheHandler, 'classes/cache'); |
|
174 | - } |
|
175 | - else{ |
|
176 | - //it's not a system driver use user library |
|
177 | - $CACHE =& class_loader($cacheHandler); |
|
178 | - } |
|
179 | - //check if the page already cached |
|
180 | - if(! empty($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'get'){ |
|
181 | - $RESPONSE = & class_loader('Response', 'classes'); |
|
182 | - if ($RESPONSE->renderFinalPageFromCache($CACHE)){ |
|
183 | - return; |
|
184 | - } |
|
185 | - } |
|
186 | - } |
|
161 | + if(get_config('cache_enable', false)){ |
|
162 | + /** |
|
163 | + * Load Cache interface file |
|
164 | + */ |
|
165 | + require_once CORE_CLASSES_CACHE_PATH . 'CacheInterface.php'; |
|
166 | + $cacheHandler = get_config('cache_handler'); |
|
167 | + if(! $cacheHandler){ |
|
168 | + show_error('The cache feature is enabled in the configuration but the cache handler class is not set.'); |
|
169 | + } |
|
170 | + $CACHE = null; |
|
171 | + //first check if the cache handler is the system driver |
|
172 | + if(file_exists(CORE_CLASSES_CACHE_PATH . $cacheHandler . '.php')){ |
|
173 | + $CACHE =& class_loader($cacheHandler, 'classes/cache'); |
|
174 | + } |
|
175 | + else{ |
|
176 | + //it's not a system driver use user library |
|
177 | + $CACHE =& class_loader($cacheHandler); |
|
178 | + } |
|
179 | + //check if the page already cached |
|
180 | + if(! empty($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'get'){ |
|
181 | + $RESPONSE = & class_loader('Response', 'classes'); |
|
182 | + if ($RESPONSE->renderFinalPageFromCache($CACHE)){ |
|
183 | + return; |
|
184 | + } |
|
185 | + } |
|
186 | + } |
|
187 | 187 | |
188 | - //load model class |
|
189 | - require_once CORE_CLASSES_MODEL_PATH . 'Model.php'; |
|
188 | + //load model class |
|
189 | + require_once CORE_CLASSES_MODEL_PATH . 'Model.php'; |
|
190 | 190 | |
191 | - $LOGGER->info('Everything is OK load Router library and dispatch the request to the corresponding controller'); |
|
192 | - /** |
|
193 | - * Routing |
|
194 | - * instantiation of the "Router" class and request processing. |
|
195 | - */ |
|
196 | - $ROUTER = & class_loader('Router', 'classes'); |
|
197 | - $ROUTER->processRequest(); |
|
191 | + $LOGGER->info('Everything is OK load Router library and dispatch the request to the corresponding controller'); |
|
192 | + /** |
|
193 | + * Routing |
|
194 | + * instantiation of the "Router" class and request processing. |
|
195 | + */ |
|
196 | + $ROUTER = & class_loader('Router', 'classes'); |
|
197 | + $ROUTER->processRequest(); |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | */ |
42 | 42 | |
43 | 43 | //if the application is running in CLI mode $_SESSION global variable is not available |
44 | - if(IS_CLI){ |
|
44 | + if (IS_CLI) { |
|
45 | 45 | $_SESSION = array(); |
46 | 46 | } |
47 | 47 | |
@@ -60,14 +60,14 @@ discard block |
||
60 | 60 | /** |
61 | 61 | * The Benchmark class |
62 | 62 | */ |
63 | - $BENCHMARK =& class_loader('Benchmark'); |
|
63 | + $BENCHMARK = & class_loader('Benchmark'); |
|
64 | 64 | |
65 | 65 | $BENCHMARK->mark('APP_EXECUTION_START'); |
66 | 66 | |
67 | 67 | /** |
68 | 68 | * instance of the Log class |
69 | 69 | */ |
70 | - $LOGGER =& class_loader('Log', 'classes'); |
|
70 | + $LOGGER = & class_loader('Log', 'classes'); |
|
71 | 71 | |
72 | 72 | $LOGGER->setLogger('ApplicationBootstrap'); |
73 | 73 | |
@@ -76,10 +76,10 @@ discard block |
||
76 | 76 | /** |
77 | 77 | * Verification of the PHP environment: minimum and maximum version |
78 | 78 | */ |
79 | - if (version_compare(phpversion(), TNH_REQUIRED_PHP_MIN_VERSION, '<')){ |
|
79 | + if (version_compare(phpversion(), TNH_REQUIRED_PHP_MIN_VERSION, '<')) { |
|
80 | 80 | show_error('Your PHP Version [' . phpversion() . '] is less than [' . TNH_REQUIRED_PHP_MIN_VERSION . '], please install a new version or update your PHP to the latest.', 'PHP Error environment'); |
81 | 81 | } |
82 | - else if(version_compare(phpversion(), TNH_REQUIRED_PHP_MAX_VERSION, '>')){ |
|
82 | + else if (version_compare(phpversion(), TNH_REQUIRED_PHP_MAX_VERSION, '>')) { |
|
83 | 83 | show_error('Your PHP Version [' . phpversion() . '] is greather than [' . TNH_REQUIRED_PHP_MAX_VERSION . '] please install a PHP version that is compatible.', 'PHP Error environment'); |
84 | 84 | } |
85 | 85 | $LOGGER->info('PHP version [' . phpversion() . '] is OK [REQUIRED MINIMUM: ' . TNH_REQUIRED_PHP_MIN_VERSION . ', REQUIRED MAXIMUM: ' . TNH_REQUIRED_PHP_MAX_VERSION . '], application can work without any issue'); |
@@ -101,11 +101,11 @@ discard block |
||
101 | 101 | |
102 | 102 | //if user have some composer packages |
103 | 103 | $LOGGER->debug('Check for composer autoload'); |
104 | - if(file_exists(VENDOR_PATH . 'autoload.php')){ |
|
104 | + if (file_exists(VENDOR_PATH . 'autoload.php')) { |
|
105 | 105 | $LOGGER->info('The composer autoload file exists include it'); |
106 | 106 | require_once VENDOR_PATH . 'autoload.php'; |
107 | 107 | } |
108 | - else{ |
|
108 | + else { |
|
109 | 109 | $LOGGER->info('The composer autoload file does not exist skipping'); |
110 | 110 | } |
111 | 111 | |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | * Load configurations and using the |
123 | 123 | * static method "init()" to initialize the Config class . |
124 | 124 | */ |
125 | - $CONFIG =& class_loader('Config', 'classes'); |
|
125 | + $CONFIG = & class_loader('Config', 'classes'); |
|
126 | 126 | $CONFIG->init(); |
127 | 127 | $BENCHMARK->mark('CONFIG_INIT_END'); |
128 | 128 | |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | * Load modules and using the |
132 | 132 | * static method "init()" to initialize the Module class. |
133 | 133 | */ |
134 | - $MODULE =& class_loader('Module', 'classes'); |
|
134 | + $MODULE = & class_loader('Module', 'classes'); |
|
135 | 135 | $MODULE->init(); |
136 | 136 | $BENCHMARK->mark('MODULE_INIT_END'); |
137 | 137 | |
@@ -150,36 +150,36 @@ discard block |
||
150 | 150 | /** |
151 | 151 | * Loading Security class |
152 | 152 | */ |
153 | - $SECURITY =& class_loader('Security', 'classes'); |
|
153 | + $SECURITY = & class_loader('Security', 'classes'); |
|
154 | 154 | $SECURITY->checkWhiteListIpAccess(); |
155 | 155 | |
156 | 156 | /** |
157 | 157 | * Loading Url class |
158 | 158 | */ |
159 | - $URL =& class_loader('Url', 'classes'); |
|
159 | + $URL = & class_loader('Url', 'classes'); |
|
160 | 160 | |
161 | - if(get_config('cache_enable', false)){ |
|
161 | + if (get_config('cache_enable', false)) { |
|
162 | 162 | /** |
163 | 163 | * Load Cache interface file |
164 | 164 | */ |
165 | 165 | require_once CORE_CLASSES_CACHE_PATH . 'CacheInterface.php'; |
166 | 166 | $cacheHandler = get_config('cache_handler'); |
167 | - if(! $cacheHandler){ |
|
167 | + if (!$cacheHandler) { |
|
168 | 168 | show_error('The cache feature is enabled in the configuration but the cache handler class is not set.'); |
169 | 169 | } |
170 | 170 | $CACHE = null; |
171 | 171 | //first check if the cache handler is the system driver |
172 | - if(file_exists(CORE_CLASSES_CACHE_PATH . $cacheHandler . '.php')){ |
|
173 | - $CACHE =& class_loader($cacheHandler, 'classes/cache'); |
|
172 | + if (file_exists(CORE_CLASSES_CACHE_PATH . $cacheHandler . '.php')) { |
|
173 | + $CACHE = & class_loader($cacheHandler, 'classes/cache'); |
|
174 | 174 | } |
175 | - else{ |
|
175 | + else { |
|
176 | 176 | //it's not a system driver use user library |
177 | - $CACHE =& class_loader($cacheHandler); |
|
177 | + $CACHE = & class_loader($cacheHandler); |
|
178 | 178 | } |
179 | 179 | //check if the page already cached |
180 | - if(! empty($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'get'){ |
|
180 | + if (!empty($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'get') { |
|
181 | 181 | $RESPONSE = & class_loader('Response', 'classes'); |
182 | - if ($RESPONSE->renderFinalPageFromCache($CACHE)){ |
|
182 | + if ($RESPONSE->renderFinalPageFromCache($CACHE)) { |
|
183 | 183 | return; |
184 | 184 | } |
185 | 185 | } |