@@ -1,536 +1,536 @@ |
||
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 | - } |
|
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 | 248 | |
249 | - //check whether need save the page into cache. |
|
250 | - if($cachePageStatus){ |
|
251 | - $this->savePageContentIntoCache($content); |
|
252 | - } |
|
249 | + //check whether need save the page into cache. |
|
250 | + if($cachePageStatus){ |
|
251 | + $this->savePageContentIntoCache($content); |
|
252 | + } |
|
253 | 253 | |
254 | - // Parse out the elapsed time and memory usage, |
|
255 | - // then swap the pseudo-variables with the data |
|
256 | - $elapsedTime = $obj->benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
257 | - $memoryUsage = round($obj->benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
258 | - $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
254 | + // Parse out the elapsed time and memory usage, |
|
255 | + // then swap the pseudo-variables with the data |
|
256 | + $elapsedTime = $obj->benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
257 | + $memoryUsage = round($obj->benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
258 | + $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
259 | 259 | |
260 | - //compress the output if is available |
|
261 | - $type = null; |
|
262 | - if (self::$_canCompressOutput){ |
|
263 | - $type = 'ob_gzhandler'; |
|
264 | - } |
|
265 | - ob_start($type); |
|
266 | - self::sendHeaders(200); |
|
267 | - echo $content; |
|
268 | - ob_end_flush(); |
|
269 | - } |
|
260 | + //compress the output if is available |
|
261 | + $type = null; |
|
262 | + if (self::$_canCompressOutput){ |
|
263 | + $type = 'ob_gzhandler'; |
|
264 | + } |
|
265 | + ob_start($type); |
|
266 | + self::sendHeaders(200); |
|
267 | + echo $content; |
|
268 | + ob_end_flush(); |
|
269 | + } |
|
270 | 270 | |
271 | 271 | |
272 | - /** |
|
273 | - * Send the final page output to user if is cached |
|
274 | - * @param object $cache the cache instance |
|
275 | - * |
|
276 | - * @return boolean whether the page content if available or not |
|
277 | - */ |
|
278 | - public function renderFinalPageFromCache(&$cache){ |
|
279 | - $logger = self::getLogger(); |
|
280 | - //the current page cache key for identification |
|
281 | - $pageCacheKey = $this->_currentUrlCacheKey; |
|
272 | + /** |
|
273 | + * Send the final page output to user if is cached |
|
274 | + * @param object $cache the cache instance |
|
275 | + * |
|
276 | + * @return boolean whether the page content if available or not |
|
277 | + */ |
|
278 | + public function renderFinalPageFromCache(&$cache){ |
|
279 | + $logger = self::getLogger(); |
|
280 | + //the current page cache key for identification |
|
281 | + $pageCacheKey = $this->_currentUrlCacheKey; |
|
282 | 282 | |
283 | - $logger->debug('Checking if the page content for the URL [' . $this->_currentUrl . '] is cached ...'); |
|
284 | - //get the cache information to prepare header to send to browser |
|
285 | - $cacheInfo = $cache->getInfo($pageCacheKey); |
|
286 | - if($cacheInfo){ |
|
287 | - $status = $this->sendCacheNotYetExpireInfo($cacheInfo); |
|
288 | - if($status === false){ |
|
289 | - return $this->sendPageContentToBrowser($cache); |
|
290 | - } |
|
291 | - } |
|
292 | - return false; |
|
293 | - } |
|
283 | + $logger->debug('Checking if the page content for the URL [' . $this->_currentUrl . '] is cached ...'); |
|
284 | + //get the cache information to prepare header to send to browser |
|
285 | + $cacheInfo = $cache->getInfo($pageCacheKey); |
|
286 | + if($cacheInfo){ |
|
287 | + $status = $this->sendCacheNotYetExpireInfo($cacheInfo); |
|
288 | + if($status === false){ |
|
289 | + return $this->sendPageContentToBrowser($cache); |
|
290 | + } |
|
291 | + } |
|
292 | + return false; |
|
293 | + } |
|
294 | 294 | |
295 | 295 | |
296 | - /** |
|
297 | - * Get the final page to be rendered |
|
298 | - * @return string |
|
299 | - */ |
|
300 | - public function getFinalPageRendered(){ |
|
301 | - return $this->_pageRender; |
|
302 | - } |
|
296 | + /** |
|
297 | + * Get the final page to be rendered |
|
298 | + * @return string |
|
299 | + */ |
|
300 | + public function getFinalPageRendered(){ |
|
301 | + return $this->_pageRender; |
|
302 | + } |
|
303 | 303 | |
304 | - /** |
|
305 | - * Send the HTTP 404 error if can not found the |
|
306 | - * routing information for the current request |
|
307 | - */ |
|
308 | - public static function send404(){ |
|
309 | - /********* for logs **************/ |
|
310 | - //can't use $obj = & get_instance() here because the global super object will be available until |
|
311 | - //the main controller is loaded even for Loader::library('xxxx'); |
|
312 | - $logger = self::getLogger(); |
|
313 | - $request =& class_loader('Request', 'classes'); |
|
314 | - $userAgent =& class_loader('Browser'); |
|
315 | - $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
304 | + /** |
|
305 | + * Send the HTTP 404 error if can not found the |
|
306 | + * routing information for the current request |
|
307 | + */ |
|
308 | + public static function send404(){ |
|
309 | + /********* for logs **************/ |
|
310 | + //can't use $obj = & get_instance() here because the global super object will be available until |
|
311 | + //the main controller is loaded even for Loader::library('xxxx'); |
|
312 | + $logger = self::getLogger(); |
|
313 | + $request =& class_loader('Request', 'classes'); |
|
314 | + $userAgent =& class_loader('Browser'); |
|
315 | + $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
316 | 316 | |
317 | - //here can't use Loader::functions just include the helper manually |
|
318 | - require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
317 | + //here can't use Loader::functions just include the helper manually |
|
318 | + require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
|
319 | 319 | |
320 | - $str = '[404 page not found] : '; |
|
321 | - $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
322 | - $logger->error($str); |
|
323 | - /***********************************/ |
|
324 | - $path = CORE_VIEWS_PATH . '404.php'; |
|
325 | - if(file_exists($path)){ |
|
326 | - //compress the output if is available |
|
327 | - $type = null; |
|
328 | - if (self::$_canCompressOutput){ |
|
329 | - $type = 'ob_gzhandler'; |
|
330 | - } |
|
331 | - ob_start($type); |
|
332 | - require_once $path; |
|
333 | - $output = ob_get_clean(); |
|
334 | - self::sendHeaders(404); |
|
335 | - echo $output; |
|
336 | - } |
|
337 | - else{ |
|
338 | - show_error('The 404 view [' .$path. '] does not exist'); |
|
339 | - } |
|
340 | - } |
|
320 | + $str = '[404 page not found] : '; |
|
321 | + $str .= ' Unable to find the request page [' . $request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
|
322 | + $logger->error($str); |
|
323 | + /***********************************/ |
|
324 | + $path = CORE_VIEWS_PATH . '404.php'; |
|
325 | + if(file_exists($path)){ |
|
326 | + //compress the output if is available |
|
327 | + $type = null; |
|
328 | + if (self::$_canCompressOutput){ |
|
329 | + $type = 'ob_gzhandler'; |
|
330 | + } |
|
331 | + ob_start($type); |
|
332 | + require_once $path; |
|
333 | + $output = ob_get_clean(); |
|
334 | + self::sendHeaders(404); |
|
335 | + echo $output; |
|
336 | + } |
|
337 | + else{ |
|
338 | + show_error('The 404 view [' .$path. '] does not exist'); |
|
339 | + } |
|
340 | + } |
|
341 | 341 | |
342 | - /** |
|
343 | - * Display the error to user |
|
344 | - * @param array $data the error information |
|
345 | - */ |
|
346 | - public static function sendError(array $data = array()){ |
|
347 | - $path = CORE_VIEWS_PATH . 'errors.php'; |
|
348 | - if(file_exists($path)){ |
|
349 | - //compress the output if is available |
|
350 | - $type = null; |
|
351 | - if (self::$_canCompressOutput){ |
|
352 | - $type = 'ob_gzhandler'; |
|
353 | - } |
|
354 | - ob_start($type); |
|
355 | - extract($data); |
|
356 | - require_once $path; |
|
357 | - $output = ob_get_clean(); |
|
358 | - self::sendHeaders(503); |
|
359 | - echo $output; |
|
360 | - } |
|
361 | - else{ |
|
362 | - //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
363 | - set_http_status_header(503); |
|
364 | - echo 'The error view [' . $path . '] does not exist'; |
|
365 | - } |
|
366 | - } |
|
342 | + /** |
|
343 | + * Display the error to user |
|
344 | + * @param array $data the error information |
|
345 | + */ |
|
346 | + public static function sendError(array $data = array()){ |
|
347 | + $path = CORE_VIEWS_PATH . 'errors.php'; |
|
348 | + if(file_exists($path)){ |
|
349 | + //compress the output if is available |
|
350 | + $type = null; |
|
351 | + if (self::$_canCompressOutput){ |
|
352 | + $type = 'ob_gzhandler'; |
|
353 | + } |
|
354 | + ob_start($type); |
|
355 | + extract($data); |
|
356 | + require_once $path; |
|
357 | + $output = ob_get_clean(); |
|
358 | + self::sendHeaders(503); |
|
359 | + echo $output; |
|
360 | + } |
|
361 | + else{ |
|
362 | + //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
|
363 | + set_http_status_header(503); |
|
364 | + echo 'The error view [' . $path . '] does not exist'; |
|
365 | + } |
|
366 | + } |
|
367 | 367 | |
368 | - /** |
|
369 | - * Send the cache not yet expire to browser |
|
370 | - * @param array $cacheInfo the cache information |
|
371 | - * @return boolean true if the information is sent otherwise false |
|
372 | - */ |
|
373 | - protected function sendCacheNotYetExpireInfo($cacheInfo){ |
|
374 | - if(! empty($cacheInfo)){ |
|
375 | - $logger = self::getLogger(); |
|
376 | - $lastModified = $cacheInfo['mtime']; |
|
377 | - $expire = $cacheInfo['expire']; |
|
378 | - $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
379 | - self::setHeader('Pragma', 'public'); |
|
380 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
381 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
382 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
383 | - if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
384 | - $logger->info('The cache page content is not yet expire for the URL [' . $this->_currentUrl . '] send 304 header to browser'); |
|
385 | - self::sendHeaders(304); |
|
386 | - return true; |
|
387 | - } |
|
388 | - } |
|
389 | - return false; |
|
390 | - } |
|
368 | + /** |
|
369 | + * Send the cache not yet expire to browser |
|
370 | + * @param array $cacheInfo the cache information |
|
371 | + * @return boolean true if the information is sent otherwise false |
|
372 | + */ |
|
373 | + protected function sendCacheNotYetExpireInfo($cacheInfo){ |
|
374 | + if(! empty($cacheInfo)){ |
|
375 | + $logger = self::getLogger(); |
|
376 | + $lastModified = $cacheInfo['mtime']; |
|
377 | + $expire = $cacheInfo['expire']; |
|
378 | + $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
|
379 | + self::setHeader('Pragma', 'public'); |
|
380 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
381 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
382 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
383 | + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
384 | + $logger->info('The cache page content is not yet expire for the URL [' . $this->_currentUrl . '] send 304 header to browser'); |
|
385 | + self::sendHeaders(304); |
|
386 | + return true; |
|
387 | + } |
|
388 | + } |
|
389 | + return false; |
|
390 | + } |
|
391 | 391 | |
392 | - /** |
|
393 | - * Send the page content from cache to browser |
|
394 | - * @param object $cache the cache instance |
|
395 | - * @return boolean the status of the operation |
|
396 | - */ |
|
397 | - protected function sendPageContentToBrowser(&$cache){ |
|
398 | - $logger = self::getLogger(); |
|
399 | - $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'); |
|
400 | - self::sendHeaders(200); |
|
401 | - //current page cache key |
|
402 | - $pageCacheKey = $this->_currentUrlCacheKey; |
|
403 | - //get the cache content |
|
404 | - $content = $cache->get($pageCacheKey); |
|
405 | - if($content){ |
|
406 | - $logger->info('The page content for the URL [' . $this->_currentUrl . '] already cached just display it'); |
|
407 | - //load benchmark class |
|
408 | - $benchmark = & class_loader('Benchmark'); |
|
392 | + /** |
|
393 | + * Send the page content from cache to browser |
|
394 | + * @param object $cache the cache instance |
|
395 | + * @return boolean the status of the operation |
|
396 | + */ |
|
397 | + protected function sendPageContentToBrowser(&$cache){ |
|
398 | + $logger = self::getLogger(); |
|
399 | + $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'); |
|
400 | + self::sendHeaders(200); |
|
401 | + //current page cache key |
|
402 | + $pageCacheKey = $this->_currentUrlCacheKey; |
|
403 | + //get the cache content |
|
404 | + $content = $cache->get($pageCacheKey); |
|
405 | + if($content){ |
|
406 | + $logger->info('The page content for the URL [' . $this->_currentUrl . '] already cached just display it'); |
|
407 | + //load benchmark class |
|
408 | + $benchmark = & class_loader('Benchmark'); |
|
409 | 409 | |
410 | - // Parse out the elapsed time and memory usage, |
|
411 | - // then swap the pseudo-variables with the data |
|
412 | - $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
413 | - $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
414 | - $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
410 | + // Parse out the elapsed time and memory usage, |
|
411 | + // then swap the pseudo-variables with the data |
|
412 | + $elapsedTime = $benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
|
413 | + $memoryUsage = round($benchmark->memoryUsage('APP_EXECUTION_START', 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
|
414 | + $content = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
|
415 | 415 | |
416 | - ///display the final output |
|
417 | - //compress the output if is available |
|
418 | - $type = null; |
|
419 | - if (self::$_canCompressOutput){ |
|
420 | - $type = 'ob_gzhandler'; |
|
421 | - } |
|
422 | - ob_start($type); |
|
423 | - echo $content; |
|
424 | - ob_end_flush(); |
|
425 | - return true; |
|
426 | - } |
|
427 | - $logger->info('The page cache content for the URL [' . $this->_currentUrl . '] is not valid may be already expired'); |
|
428 | - $cache->delete($pageCacheKey); |
|
429 | - return false; |
|
430 | - } |
|
416 | + ///display the final output |
|
417 | + //compress the output if is available |
|
418 | + $type = null; |
|
419 | + if (self::$_canCompressOutput){ |
|
420 | + $type = 'ob_gzhandler'; |
|
421 | + } |
|
422 | + ob_start($type); |
|
423 | + echo $content; |
|
424 | + ob_end_flush(); |
|
425 | + return true; |
|
426 | + } |
|
427 | + $logger->info('The page cache content for the URL [' . $this->_currentUrl . '] is not valid may be already expired'); |
|
428 | + $cache->delete($pageCacheKey); |
|
429 | + return false; |
|
430 | + } |
|
431 | 431 | |
432 | - /** |
|
433 | - * Save the content of page into cache |
|
434 | - * @param string $content the page content to be saved |
|
435 | - * @return void |
|
436 | - */ |
|
437 | - protected function savePageContentIntoCache($content){ |
|
438 | - $obj = & get_instance(); |
|
439 | - $logger = self::getLogger(); |
|
432 | + /** |
|
433 | + * Save the content of page into cache |
|
434 | + * @param string $content the page content to be saved |
|
435 | + * @return void |
|
436 | + */ |
|
437 | + protected function savePageContentIntoCache($content){ |
|
438 | + $obj = & get_instance(); |
|
439 | + $logger = self::getLogger(); |
|
440 | 440 | |
441 | - //current page URL |
|
442 | - $url = $this->_currentUrl; |
|
443 | - //Cache view Time to live in second |
|
444 | - $viewCacheTtl = get_config('cache_ttl'); |
|
445 | - if (!empty($obj->view_cache_ttl)){ |
|
446 | - $viewCacheTtl = $obj->view_cache_ttl; |
|
447 | - } |
|
448 | - //the cache handler instance |
|
449 | - $cacheInstance = $obj->cache; |
|
450 | - //the current page cache key for identification |
|
451 | - $cacheKey = $this->_currentUrlCacheKey; |
|
441 | + //current page URL |
|
442 | + $url = $this->_currentUrl; |
|
443 | + //Cache view Time to live in second |
|
444 | + $viewCacheTtl = get_config('cache_ttl'); |
|
445 | + if (!empty($obj->view_cache_ttl)){ |
|
446 | + $viewCacheTtl = $obj->view_cache_ttl; |
|
447 | + } |
|
448 | + //the cache handler instance |
|
449 | + $cacheInstance = $obj->cache; |
|
450 | + //the current page cache key for identification |
|
451 | + $cacheKey = $this->_currentUrlCacheKey; |
|
452 | 452 | |
453 | - $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
454 | - $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
453 | + $logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
|
454 | + $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
|
455 | 455 | |
456 | - //get the cache information to prepare header to send to browser |
|
457 | - $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
458 | - if($cacheInfo){ |
|
459 | - $lastModified = $cacheInfo['mtime']; |
|
460 | - $expire = $cacheInfo['expire']; |
|
461 | - $maxAge = $expire - time(); |
|
462 | - self::setHeader('Pragma', 'public'); |
|
463 | - self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
464 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
465 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
466 | - } |
|
467 | - } |
|
456 | + //get the cache information to prepare header to send to browser |
|
457 | + $cacheInfo = $cacheInstance->getInfo($cacheKey); |
|
458 | + if($cacheInfo){ |
|
459 | + $lastModified = $cacheInfo['mtime']; |
|
460 | + $expire = $cacheInfo['expire']; |
|
461 | + $maxAge = $expire - time(); |
|
462 | + self::setHeader('Pragma', 'public'); |
|
463 | + self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
|
464 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
465 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
466 | + } |
|
467 | + } |
|
468 | 468 | |
469 | 469 | |
470 | - /** |
|
471 | - * Get the module information for the view to load |
|
472 | - * @param string $view the view name like moduleName/viewName, viewName |
|
473 | - * |
|
474 | - * @return array the module information |
|
475 | - * array( |
|
476 | - * 'module'=> 'module_name' |
|
477 | - * 'view' => 'view_name' |
|
478 | - * 'viewFile' => 'view_file' |
|
479 | - * ) |
|
480 | - */ |
|
481 | - protected function getModuleInfoForView($view){ |
|
482 | - $module = null; |
|
483 | - $viewFile = null; |
|
484 | - $obj = & get_instance(); |
|
485 | - //check if the request class contains module name |
|
486 | - if(strpos($view, '/') !== false){ |
|
487 | - $viewPath = explode('/', $view); |
|
488 | - if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
489 | - $module = $viewPath[0]; |
|
490 | - array_shift($viewPath); |
|
491 | - $view = implode('/', $viewPath); |
|
492 | - $viewFile = $view . '.php'; |
|
493 | - } |
|
494 | - } |
|
495 | - if(! $module && !empty($obj->moduleName)){ |
|
496 | - $module = $obj->moduleName; |
|
497 | - } |
|
498 | - return array( |
|
499 | - 'view' => $view, |
|
500 | - 'module' => $module, |
|
501 | - 'viewFile' => $viewFile |
|
502 | - ); |
|
503 | - } |
|
470 | + /** |
|
471 | + * Get the module information for the view to load |
|
472 | + * @param string $view the view name like moduleName/viewName, viewName |
|
473 | + * |
|
474 | + * @return array the module information |
|
475 | + * array( |
|
476 | + * 'module'=> 'module_name' |
|
477 | + * 'view' => 'view_name' |
|
478 | + * 'viewFile' => 'view_file' |
|
479 | + * ) |
|
480 | + */ |
|
481 | + protected function getModuleInfoForView($view){ |
|
482 | + $module = null; |
|
483 | + $viewFile = null; |
|
484 | + $obj = & get_instance(); |
|
485 | + //check if the request class contains module name |
|
486 | + if(strpos($view, '/') !== false){ |
|
487 | + $viewPath = explode('/', $view); |
|
488 | + if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
489 | + $module = $viewPath[0]; |
|
490 | + array_shift($viewPath); |
|
491 | + $view = implode('/', $viewPath); |
|
492 | + $viewFile = $view . '.php'; |
|
493 | + } |
|
494 | + } |
|
495 | + if(! $module && !empty($obj->moduleName)){ |
|
496 | + $module = $obj->moduleName; |
|
497 | + } |
|
498 | + return array( |
|
499 | + 'view' => $view, |
|
500 | + 'module' => $module, |
|
501 | + 'viewFile' => $viewFile |
|
502 | + ); |
|
503 | + } |
|
504 | 504 | |
505 | - /** |
|
506 | - * Render the view page |
|
507 | - * @see Response::render |
|
508 | - * @return void|string |
|
509 | - */ |
|
510 | - protected function loadView($path, array $data = array(), $return = false){ |
|
511 | - $found = false; |
|
512 | - if(file_exists($path)){ |
|
513 | - //super instance |
|
514 | - $obj = & get_instance(); |
|
515 | - foreach(get_object_vars($obj) as $key => $value){ |
|
516 | - if(! isset($this->{$key})){ |
|
517 | - $this->{$key} = & $obj->{$key}; |
|
518 | - } |
|
519 | - } |
|
520 | - ob_start(); |
|
521 | - extract($data); |
|
522 | - //need use require() instead of require_once because can load this view many time |
|
523 | - require $path; |
|
524 | - $content = ob_get_clean(); |
|
525 | - if($return){ |
|
526 | - return $content; |
|
527 | - } |
|
528 | - $this->_pageRender .= $content; |
|
529 | - $found = true; |
|
530 | - } |
|
531 | - if(! $found){ |
|
532 | - show_error('Unable to find view [' .$view . ']'); |
|
533 | - } |
|
534 | - } |
|
505 | + /** |
|
506 | + * Render the view page |
|
507 | + * @see Response::render |
|
508 | + * @return void|string |
|
509 | + */ |
|
510 | + protected function loadView($path, array $data = array(), $return = false){ |
|
511 | + $found = false; |
|
512 | + if(file_exists($path)){ |
|
513 | + //super instance |
|
514 | + $obj = & get_instance(); |
|
515 | + foreach(get_object_vars($obj) as $key => $value){ |
|
516 | + if(! isset($this->{$key})){ |
|
517 | + $this->{$key} = & $obj->{$key}; |
|
518 | + } |
|
519 | + } |
|
520 | + ob_start(); |
|
521 | + extract($data); |
|
522 | + //need use require() instead of require_once because can load this view many time |
|
523 | + require $path; |
|
524 | + $content = ob_get_clean(); |
|
525 | + if($return){ |
|
526 | + return $content; |
|
527 | + } |
|
528 | + $this->_pageRender .= $content; |
|
529 | + $found = true; |
|
530 | + } |
|
531 | + if(! $found){ |
|
532 | + show_error('Unable to find view [' .$view . ']'); |
|
533 | + } |
|
534 | + } |
|
535 | 535 | |
536 | - } |
|
536 | + } |
@@ -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,28 +226,28 @@ 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 | |
249 | 249 | //check whether need save the page into cache. |
250 | - if($cachePageStatus){ |
|
250 | + if ($cachePageStatus) { |
|
251 | 251 | $this->savePageContentIntoCache($content); |
252 | 252 | } |
253 | 253 | |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | |
260 | 260 | //compress the output if is available |
261 | 261 | $type = null; |
262 | - if (self::$_canCompressOutput){ |
|
262 | + if (self::$_canCompressOutput) { |
|
263 | 263 | $type = 'ob_gzhandler'; |
264 | 264 | } |
265 | 265 | ob_start($type); |
@@ -275,7 +275,7 @@ discard block |
||
275 | 275 | * |
276 | 276 | * @return boolean whether the page content if available or not |
277 | 277 | */ |
278 | - public function renderFinalPageFromCache(&$cache){ |
|
278 | + public function renderFinalPageFromCache(&$cache) { |
|
279 | 279 | $logger = self::getLogger(); |
280 | 280 | //the current page cache key for identification |
281 | 281 | $pageCacheKey = $this->_currentUrlCacheKey; |
@@ -283,9 +283,9 @@ discard block |
||
283 | 283 | $logger->debug('Checking if the page content for the URL [' . $this->_currentUrl . '] is cached ...'); |
284 | 284 | //get the cache information to prepare header to send to browser |
285 | 285 | $cacheInfo = $cache->getInfo($pageCacheKey); |
286 | - if($cacheInfo){ |
|
286 | + if ($cacheInfo) { |
|
287 | 287 | $status = $this->sendCacheNotYetExpireInfo($cacheInfo); |
288 | - if($status === false){ |
|
288 | + if ($status === false) { |
|
289 | 289 | return $this->sendPageContentToBrowser($cache); |
290 | 290 | } |
291 | 291 | } |
@@ -297,7 +297,7 @@ discard block |
||
297 | 297 | * Get the final page to be rendered |
298 | 298 | * @return string |
299 | 299 | */ |
300 | - public function getFinalPageRendered(){ |
|
300 | + public function getFinalPageRendered() { |
|
301 | 301 | return $this->_pageRender; |
302 | 302 | } |
303 | 303 | |
@@ -305,14 +305,14 @@ discard block |
||
305 | 305 | * Send the HTTP 404 error if can not found the |
306 | 306 | * routing information for the current request |
307 | 307 | */ |
308 | - public static function send404(){ |
|
308 | + public static function send404() { |
|
309 | 309 | /********* for logs **************/ |
310 | 310 | //can't use $obj = & get_instance() here because the global super object will be available until |
311 | 311 | //the main controller is loaded even for Loader::library('xxxx'); |
312 | 312 | $logger = self::getLogger(); |
313 | - $request =& class_loader('Request', 'classes'); |
|
314 | - $userAgent =& class_loader('Browser'); |
|
315 | - $browser = $userAgent->getPlatform().', '.$userAgent->getBrowser().' '.$userAgent->getVersion(); |
|
313 | + $request = & class_loader('Request', 'classes'); |
|
314 | + $userAgent = & class_loader('Browser'); |
|
315 | + $browser = $userAgent->getPlatform() . ', ' . $userAgent->getBrowser() . ' ' . $userAgent->getVersion(); |
|
316 | 316 | |
317 | 317 | //here can't use Loader::functions just include the helper manually |
318 | 318 | require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
@@ -322,10 +322,10 @@ discard block |
||
322 | 322 | $logger->error($str); |
323 | 323 | /***********************************/ |
324 | 324 | $path = CORE_VIEWS_PATH . '404.php'; |
325 | - if(file_exists($path)){ |
|
325 | + if (file_exists($path)) { |
|
326 | 326 | //compress the output if is available |
327 | 327 | $type = null; |
328 | - if (self::$_canCompressOutput){ |
|
328 | + if (self::$_canCompressOutput) { |
|
329 | 329 | $type = 'ob_gzhandler'; |
330 | 330 | } |
331 | 331 | ob_start($type); |
@@ -334,8 +334,8 @@ discard block |
||
334 | 334 | self::sendHeaders(404); |
335 | 335 | echo $output; |
336 | 336 | } |
337 | - else{ |
|
338 | - show_error('The 404 view [' .$path. '] does not exist'); |
|
337 | + else { |
|
338 | + show_error('The 404 view [' . $path . '] does not exist'); |
|
339 | 339 | } |
340 | 340 | } |
341 | 341 | |
@@ -343,12 +343,12 @@ discard block |
||
343 | 343 | * Display the error to user |
344 | 344 | * @param array $data the error information |
345 | 345 | */ |
346 | - public static function sendError(array $data = array()){ |
|
346 | + public static function sendError(array $data = array()) { |
|
347 | 347 | $path = CORE_VIEWS_PATH . 'errors.php'; |
348 | - if(file_exists($path)){ |
|
348 | + if (file_exists($path)) { |
|
349 | 349 | //compress the output if is available |
350 | 350 | $type = null; |
351 | - if (self::$_canCompressOutput){ |
|
351 | + if (self::$_canCompressOutput) { |
|
352 | 352 | $type = 'ob_gzhandler'; |
353 | 353 | } |
354 | 354 | ob_start($type); |
@@ -358,7 +358,7 @@ discard block |
||
358 | 358 | self::sendHeaders(503); |
359 | 359 | echo $output; |
360 | 360 | } |
361 | - else{ |
|
361 | + else { |
|
362 | 362 | //can't use show_error() at this time because some dependencies not yet loaded and to prevent loop |
363 | 363 | set_http_status_header(503); |
364 | 364 | echo 'The error view [' . $path . '] does not exist'; |
@@ -370,17 +370,17 @@ discard block |
||
370 | 370 | * @param array $cacheInfo the cache information |
371 | 371 | * @return boolean true if the information is sent otherwise false |
372 | 372 | */ |
373 | - protected function sendCacheNotYetExpireInfo($cacheInfo){ |
|
374 | - if(! empty($cacheInfo)){ |
|
373 | + protected function sendCacheNotYetExpireInfo($cacheInfo) { |
|
374 | + if (!empty($cacheInfo)) { |
|
375 | 375 | $logger = self::getLogger(); |
376 | 376 | $lastModified = $cacheInfo['mtime']; |
377 | 377 | $expire = $cacheInfo['expire']; |
378 | 378 | $maxAge = $expire - $_SERVER['REQUEST_TIME']; |
379 | 379 | self::setHeader('Pragma', 'public'); |
380 | 380 | self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
381 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
382 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
383 | - if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ |
|
381 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire) . ' GMT'); |
|
382 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
|
383 | + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $lastModified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { |
|
384 | 384 | $logger->info('The cache page content is not yet expire for the URL [' . $this->_currentUrl . '] send 304 header to browser'); |
385 | 385 | self::sendHeaders(304); |
386 | 386 | return true; |
@@ -394,7 +394,7 @@ discard block |
||
394 | 394 | * @param object $cache the cache instance |
395 | 395 | * @return boolean the status of the operation |
396 | 396 | */ |
397 | - protected function sendPageContentToBrowser(&$cache){ |
|
397 | + protected function sendPageContentToBrowser(&$cache) { |
|
398 | 398 | $logger = self::getLogger(); |
399 | 399 | $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'); |
400 | 400 | self::sendHeaders(200); |
@@ -402,7 +402,7 @@ discard block |
||
402 | 402 | $pageCacheKey = $this->_currentUrlCacheKey; |
403 | 403 | //get the cache content |
404 | 404 | $content = $cache->get($pageCacheKey); |
405 | - if($content){ |
|
405 | + if ($content) { |
|
406 | 406 | $logger->info('The page content for the URL [' . $this->_currentUrl . '] already cached just display it'); |
407 | 407 | //load benchmark class |
408 | 408 | $benchmark = & class_loader('Benchmark'); |
@@ -416,7 +416,7 @@ discard block |
||
416 | 416 | ///display the final output |
417 | 417 | //compress the output if is available |
418 | 418 | $type = null; |
419 | - if (self::$_canCompressOutput){ |
|
419 | + if (self::$_canCompressOutput) { |
|
420 | 420 | $type = 'ob_gzhandler'; |
421 | 421 | } |
422 | 422 | ob_start($type); |
@@ -434,7 +434,7 @@ discard block |
||
434 | 434 | * @param string $content the page content to be saved |
435 | 435 | * @return void |
436 | 436 | */ |
437 | - protected function savePageContentIntoCache($content){ |
|
437 | + protected function savePageContentIntoCache($content) { |
|
438 | 438 | $obj = & get_instance(); |
439 | 439 | $logger = self::getLogger(); |
440 | 440 | |
@@ -442,7 +442,7 @@ discard block |
||
442 | 442 | $url = $this->_currentUrl; |
443 | 443 | //Cache view Time to live in second |
444 | 444 | $viewCacheTtl = get_config('cache_ttl'); |
445 | - if (!empty($obj->view_cache_ttl)){ |
|
445 | + if (!empty($obj->view_cache_ttl)) { |
|
446 | 446 | $viewCacheTtl = $obj->view_cache_ttl; |
447 | 447 | } |
448 | 448 | //the cache handler instance |
@@ -455,14 +455,14 @@ discard block |
||
455 | 455 | |
456 | 456 | //get the cache information to prepare header to send to browser |
457 | 457 | $cacheInfo = $cacheInstance->getInfo($cacheKey); |
458 | - if($cacheInfo){ |
|
458 | + if ($cacheInfo) { |
|
459 | 459 | $lastModified = $cacheInfo['mtime']; |
460 | 460 | $expire = $cacheInfo['expire']; |
461 | 461 | $maxAge = $expire - time(); |
462 | 462 | self::setHeader('Pragma', 'public'); |
463 | 463 | self::setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
464 | - self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire).' GMT'); |
|
465 | - self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified).' GMT'); |
|
464 | + self::setHeader('Expires', gmdate('D, d M Y H:i:s', $expire) . ' GMT'); |
|
465 | + self::setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
|
466 | 466 | } |
467 | 467 | } |
468 | 468 | |
@@ -478,21 +478,21 @@ discard block |
||
478 | 478 | * 'viewFile' => 'view_file' |
479 | 479 | * ) |
480 | 480 | */ |
481 | - protected function getModuleInfoForView($view){ |
|
481 | + protected function getModuleInfoForView($view) { |
|
482 | 482 | $module = null; |
483 | 483 | $viewFile = null; |
484 | 484 | $obj = & get_instance(); |
485 | 485 | //check if the request class contains module name |
486 | - if(strpos($view, '/') !== false){ |
|
486 | + if (strpos($view, '/') !== false) { |
|
487 | 487 | $viewPath = explode('/', $view); |
488 | - if(isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())){ |
|
488 | + if (isset($viewPath[0]) && in_array($viewPath[0], Module::getModuleList())) { |
|
489 | 489 | $module = $viewPath[0]; |
490 | 490 | array_shift($viewPath); |
491 | 491 | $view = implode('/', $viewPath); |
492 | 492 | $viewFile = $view . '.php'; |
493 | 493 | } |
494 | 494 | } |
495 | - if(! $module && !empty($obj->moduleName)){ |
|
495 | + if (!$module && !empty($obj->moduleName)) { |
|
496 | 496 | $module = $obj->moduleName; |
497 | 497 | } |
498 | 498 | return array( |
@@ -507,13 +507,13 @@ discard block |
||
507 | 507 | * @see Response::render |
508 | 508 | * @return void|string |
509 | 509 | */ |
510 | - protected function loadView($path, array $data = array(), $return = false){ |
|
510 | + protected function loadView($path, array $data = array(), $return = false) { |
|
511 | 511 | $found = false; |
512 | - if(file_exists($path)){ |
|
512 | + if (file_exists($path)) { |
|
513 | 513 | //super instance |
514 | 514 | $obj = & get_instance(); |
515 | - foreach(get_object_vars($obj) as $key => $value){ |
|
516 | - if(! isset($this->{$key})){ |
|
515 | + foreach (get_object_vars($obj) as $key => $value) { |
|
516 | + if (!isset($this->{$key})) { |
|
517 | 517 | $this->{$key} = & $obj->{$key}; |
518 | 518 | } |
519 | 519 | } |
@@ -522,14 +522,14 @@ discard block |
||
522 | 522 | //need use require() instead of require_once because can load this view many time |
523 | 523 | require $path; |
524 | 524 | $content = ob_get_clean(); |
525 | - if($return){ |
|
525 | + if ($return) { |
|
526 | 526 | return $content; |
527 | 527 | } |
528 | 528 | $this->_pageRender .= $content; |
529 | 529 | $found = true; |
530 | 530 | } |
531 | - if(! $found){ |
|
532 | - show_error('Unable to find view [' .$view . ']'); |
|
531 | + if (!$found) { |
|
532 | + show_error('Unable to find view [' . $view . ']'); |
|
533 | 533 | } |
534 | 534 | } |
535 | 535 |