@@ -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 | + } |
|