| Total Complexity | 71 |
| Total Lines | 542 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Response often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Response, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Response extends BaseClass { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The list of request header to send with response |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $headers = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The final page content to display to user |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $_pageRender = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The current request URL |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $_currentUrl = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The current request URL cache key |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private $_currentUrlCacheKey = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Whether we can compress the output using Gzip |
||
| 59 | * @var boolean |
||
| 60 | */ |
||
| 61 | private $_canCompressOutput = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Construct new instance |
||
| 65 | */ |
||
| 66 | public function __construct() { |
||
| 67 | parent::__construct(); |
||
| 68 | $globals = & class_loader('GlobalVar', 'classes'); |
||
| 69 | $currentUrl = ''; |
||
| 70 | if ($globals->server('REQUEST_URI')) { |
||
| 71 | $currentUrl = $globals->server('REQUEST_URI'); |
||
| 72 | } |
||
| 73 | if ($globals->server('QUERY_STRING')) { |
||
| 74 | $currentUrl .= '?' . $globals->server('QUERY_STRING'); |
||
| 75 | } |
||
| 76 | $this->_currentUrl = $currentUrl; |
||
| 77 | $this->_currentUrlCacheKey = md5($this->_currentUrl); |
||
| 78 | |||
| 79 | $this->_canCompressOutput = get_config('compress_output') |
||
| 80 | && $globals->server('HTTP_ACCEPT_ENCODING') !== null |
||
| 81 | && stripos($globals->server('HTTP_ACCEPT_ENCODING'), 'gzip') !== false |
||
| 82 | && extension_loaded('zlib') |
||
| 83 | && (bool) ini_get('zlib.output_compression') === false; |
||
| 84 | } |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Send the HTTP Response headers |
||
| 89 | * @param integer $httpCode the HTTP status code |
||
| 90 | * @param array $headers the additional headers to add to the existing headers list |
||
| 91 | */ |
||
| 92 | public function sendHeaders($httpCode = 200, array $headers = array()) { |
||
| 93 | set_http_status_header($httpCode); |
||
| 94 | $this->setHeaders($headers); |
||
| 95 | $this->setRequiredHeaders(); |
||
| 96 | //@codeCoverageIgnoreStart |
||
| 97 | //not available when running in CLI mode |
||
| 98 | if (!headers_sent()) { |
||
| 99 | foreach ($this->getHeaders() as $key => $value) { |
||
| 100 | header($key . ': ' . $value); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | //@codeCoverageIgnoreEnd |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the list of the headers |
||
| 108 | * @return array the headers list |
||
| 109 | */ |
||
| 110 | public function getHeaders() { |
||
| 111 | return $this->headers; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get the header value for the given name |
||
| 116 | * @param string $name the header name |
||
| 117 | * @return string|null the header value |
||
| 118 | */ |
||
| 119 | public function getHeader($name) { |
||
| 120 | if (array_key_exists($name, $this->headers)) { |
||
| 121 | return $this->headers[$name]; |
||
| 122 | } |
||
| 123 | return null; |
||
| 124 | } |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Set the header value for the specified name |
||
| 129 | * @param string $name the header name |
||
| 130 | * @param string $value the header value to be set |
||
| 131 | */ |
||
| 132 | public function setHeader($name, $value) { |
||
| 133 | $this->headers[$name] = $value; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Set the headers using array |
||
| 138 | * @param array $headers the list of the headers to set. |
||
| 139 | * Note: this will merge with the existing headers |
||
| 140 | */ |
||
| 141 | public function setHeaders(array $headers) { |
||
| 142 | $this->headers = array_merge($this->headers, $headers); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Redirect user to the specified page |
||
| 147 | * @param string $path the URL or URI to be redirect to |
||
| 148 | * @codeCoverageIgnore |
||
| 149 | */ |
||
| 150 | public function redirect($path = '') { |
||
| 151 | $url = Url::site_url($path); |
||
| 152 | if (!headers_sent()) { |
||
| 153 | header('Location: ' . $url); |
||
| 154 | exit; |
||
| 155 | } |
||
| 156 | echo '<script> |
||
| 157 | location.href = "'.$url . '"; |
||
| 158 | </script>'; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Render the view to display later or return the content |
||
| 163 | * @param string $view the view name or path |
||
| 164 | * @param array|object $data the variable data to use in the view |
||
| 165 | * @param boolean $return whether to return the view generated content or display it directly |
||
| 166 | * @return void|string if $return is true will return the view content otherwise |
||
| 167 | * will display the view content. |
||
| 168 | */ |
||
| 169 | public function render($view, $data = null, $return = false) { |
||
| 170 | //try to convert data to an array if is object or other thing |
||
| 171 | $data = (array) $data; |
||
| 172 | $view = str_ireplace('.php', '', $view); |
||
| 173 | $view = trim($view, '/\\'); |
||
| 174 | $viewFile = $view . '.php'; |
||
| 175 | $path = null; |
||
| 176 | |||
| 177 | //check in module first |
||
| 178 | $this->logger->debug('Checking the view [' . $view . '] from module list ...'); |
||
| 179 | $moduleInfo = $this->getModuleInfoForView($view); |
||
| 180 | $module = $moduleInfo['module']; |
||
| 181 | $view = $moduleInfo['view']; |
||
| 182 | |||
| 183 | $moduleViewPath = get_instance()->module->findViewFullPath($view, $module); |
||
| 184 | if ($moduleViewPath) { |
||
| 185 | $path = $moduleViewPath; |
||
| 186 | $this->logger->info('Found view [' . $view . '] in module [' . $module . '], the file path is [' . $moduleViewPath . '] we will used it'); |
||
| 187 | } else { |
||
| 188 | $this->logger->info('Cannot find view [' . $view . '] in module [' . $module . '] using the default location'); |
||
| 189 | } |
||
| 190 | if (!$path) { |
||
| 191 | $path = $this->getDefaultFilePathForView($viewFile); |
||
| 192 | } |
||
| 193 | $this->logger->info('The view file path to be loaded is [' . $path . ']'); |
||
| 194 | |||
| 195 | if ($return) { |
||
| 196 | return $this->loadView($path, $data, true); |
||
| 197 | } |
||
| 198 | $this->loadView($path, $data, false); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Send the final page output |
||
| 203 | */ |
||
| 204 | public function renderFinalPage() { |
||
| 205 | $content = $this->_pageRender; |
||
| 206 | if (!$content) { |
||
| 207 | $this->logger->warning('The final view content is empty.'); |
||
| 208 | return; |
||
| 209 | } |
||
| 210 | $obj = & get_instance(); |
||
| 211 | $cachePageStatus = get_instance()->config->get('cache_enable', false) && !empty($obj->view_cache_enable); |
||
| 212 | |||
| 213 | $content = $this->dispatchFinalViewEvent(); |
||
| 214 | |||
| 215 | //check whether need save the page into cache. |
||
| 216 | if ($cachePageStatus) { |
||
| 217 | $this->savePageContentIntoCache($content); |
||
| 218 | } |
||
| 219 | //update content |
||
| 220 | $this->_pageRender = $content; |
||
| 221 | |||
| 222 | $content = $this->replaceElapseTimeAndMemoryUsage($content); |
||
| 223 | |||
| 224 | //compress the output if is available |
||
| 225 | $type = null; |
||
| 226 | if ($this->_canCompressOutput) { |
||
| 227 | $type = 'ob_gzhandler'; |
||
| 228 | } |
||
| 229 | ob_start($type); |
||
| 230 | $this->sendHeaders(200); |
||
| 231 | echo $content; |
||
| 232 | ob_end_flush(); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Dispatch the FINAL_VIEW_READY event |
||
| 237 | * |
||
| 238 | * @return string|null the final view content after processing by each listener |
||
| 239 | * if they exists otherwise the same content will be returned |
||
| 240 | */ |
||
| 241 | protected function dispatchFinalViewEvent() { |
||
| 242 | //dispatch |
||
| 243 | $event = get_instance()->eventdispatcher->dispatch( |
||
| 244 | new EventInfo( |
||
| 245 | 'FINAL_VIEW_READY', |
||
| 246 | $this->_pageRender, |
||
| 247 | true |
||
| 248 | ) |
||
| 249 | ); |
||
| 250 | $content = null; |
||
| 251 | if (!empty($event->payload)) { |
||
| 252 | $content = $event->payload; |
||
| 253 | } |
||
| 254 | if (empty($content)) { |
||
| 255 | $this->logger->warning('The view content is empty after dispatch to event listeners.'); |
||
| 256 | } |
||
| 257 | return $content; |
||
| 258 | } |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Send the final page output to user if is cached |
||
| 263 | * @param object $cache the cache instance |
||
| 264 | * |
||
| 265 | * @return boolean whether the page content if available or not |
||
| 266 | */ |
||
| 267 | public function renderFinalPageFromCache(&$cache) { |
||
| 268 | //the current page cache key for identification |
||
| 269 | $pageCacheKey = $this->_currentUrlCacheKey; |
||
| 270 | |||
| 271 | $this->logger->debug('Checking if the page content for the URL [' . $this->_currentUrl . '] is cached ...'); |
||
| 272 | //get the cache information to prepare header to send to browser |
||
| 273 | $cacheInfo = $cache->getInfo($pageCacheKey); |
||
| 274 | if ($cacheInfo) { |
||
| 275 | $status = $this->sendCacheNotYetExpireInfoToBrowser($cacheInfo); |
||
| 276 | if ($status === false) { |
||
|
|
|||
| 277 | return $this->sendCachePageContentToBrowser($cache); |
||
| 278 | } |
||
| 279 | return true; |
||
| 280 | } |
||
| 281 | return false; |
||
| 282 | } |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * Get the final page to be rendered |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function getFinalPageRendered() { |
||
| 290 | return $this->_pageRender; |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Set the final page to be rendered |
||
| 295 | * @param string $finalPage the content of the final page |
||
| 296 | * |
||
| 297 | * @return object |
||
| 298 | */ |
||
| 299 | public function setFinalPageContent($finalPage) { |
||
| 300 | $this->_pageRender = $finalPage; |
||
| 301 | return $this; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Send the HTTP 404 error if can not found the |
||
| 306 | * routing information for the current request |
||
| 307 | */ |
||
| 308 | public function send404() { |
||
| 309 | $content = $this->_pageRender; |
||
| 310 | if (!$content) { |
||
| 311 | $this->logger->warning('The final view content is empty.'); |
||
| 312 | return; |
||
| 313 | } |
||
| 314 | $obj = & get_instance(); |
||
| 315 | $cachePageStatus = get_instance()->config->get('cache_enable', false) && !empty($obj->view_cache_enable); |
||
| 316 | //dispatch |
||
| 317 | get_instance()->eventdispatcher->dispatch(new EventInfo('PAGE_NOT_FOUND')); |
||
| 318 | //check whether need save the page into cache. |
||
| 319 | if ($cachePageStatus) { |
||
| 320 | $this->savePageContentIntoCache($content); |
||
| 321 | } |
||
| 322 | $content = $this->replaceElapseTimeAndMemoryUsage($content); |
||
| 323 | |||
| 324 | /**************************************** save the content into logs **************/ |
||
| 325 | $bwsr = & class_loader('Browser'); |
||
| 326 | $browser = $bwsr->getPlatform() . ', ' . $bwsr->getBrowser() . ' ' . $bwsr->getVersion(); |
||
| 327 | $obj->loader->functions('user_agent'); |
||
| 328 | $str = '[404 page not found] : '; |
||
| 329 | $str .= ' Unable to find the request page [' . $obj->request->requestUri() . ']. The visitor IP address [' . get_ip() . '], browser [' . $browser . ']'; |
||
| 330 | |||
| 331 | //Todo fix the issue the logger name change after load new class |
||
| 332 | $this->logger->error($str); |
||
| 333 | /**********************************************************************/ |
||
| 334 | //compress the output if is available |
||
| 335 | $type = null; |
||
| 336 | if ($this->_canCompressOutput) { |
||
| 337 | $type = 'ob_gzhandler'; |
||
| 338 | } |
||
| 339 | ob_start($type); |
||
| 340 | $this->sendHeaders(404); |
||
| 341 | echo $content; |
||
| 342 | ob_end_flush(); |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Display the error to user |
||
| 347 | */ |
||
| 348 | public function sendError() { |
||
| 349 | $content = $this->_pageRender; |
||
| 350 | if (!$content) { |
||
| 351 | $this->logger->warning('The final view content is empty.'); |
||
| 352 | return; |
||
| 353 | } |
||
| 354 | $content = $this->replaceElapseTimeAndMemoryUsage($content); |
||
| 355 | //compress the output if is available |
||
| 356 | $type = null; |
||
| 357 | if ($this->_canCompressOutput) { |
||
| 358 | $type = 'ob_gzhandler'; |
||
| 359 | } |
||
| 360 | ob_start($type); |
||
| 361 | $this->sendHeaders(503); |
||
| 362 | echo $content; |
||
| 363 | ob_end_flush(); |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Return the default full file path for view |
||
| 368 | * @param string $file the filename |
||
| 369 | * |
||
| 370 | * @return string|null the full file path |
||
| 371 | */ |
||
| 372 | protected function getDefaultFilePathForView($file){ |
||
| 373 | $searchDir = array(APPS_VIEWS_PATH, CORE_VIEWS_PATH); |
||
| 374 | $fullFilePath = null; |
||
| 375 | foreach ($searchDir as $dir) { |
||
| 376 | $filePath = $dir . $file; |
||
| 377 | if (file_exists($filePath)) { |
||
| 378 | $fullFilePath = $filePath; |
||
| 379 | //is already found not to continue |
||
| 380 | break; |
||
| 381 | } |
||
| 382 | } |
||
| 383 | return $fullFilePath; |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Send the cache not yet expire to browser |
||
| 388 | * @param array $cacheInfo the cache information |
||
| 389 | * @return boolean true if the information is sent otherwise false |
||
| 390 | */ |
||
| 391 | protected function sendCacheNotYetExpireInfoToBrowser($cacheInfo) { |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Send the page content from cache to browser |
||
| 414 | * @param object $cache the cache instance |
||
| 415 | * @return boolean the status of the operation |
||
| 416 | */ |
||
| 417 | protected function sendCachePageContentToBrowser(&$cache) { |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Save the content of page into cache |
||
| 448 | * @param string $content the page content to be saved |
||
| 449 | * @return void |
||
| 450 | */ |
||
| 451 | protected function savePageContentIntoCache($content) { |
||
| 452 | $obj = & get_instance(); |
||
| 453 | //current page URL |
||
| 454 | $url = $this->_currentUrl; |
||
| 455 | //Cache view Time to live in second |
||
| 456 | $viewCacheTtl = get_instance()->config->get('cache_ttl'); |
||
| 457 | if (!empty($obj->view_cache_ttl)) { |
||
| 458 | $viewCacheTtl = $obj->view_cache_ttl; |
||
| 459 | } |
||
| 460 | //the cache handler instance |
||
| 461 | $cacheInstance = $obj->cache; |
||
| 462 | //the current page cache key for identification |
||
| 463 | $cacheKey = $this->_currentUrlCacheKey; |
||
| 464 | $this->logger->debug('Save the page content for URL [' . $url . '] into the cache ...'); |
||
| 465 | $cacheInstance->set($cacheKey, $content, $viewCacheTtl); |
||
| 466 | |||
| 467 | //get the cache information to prepare header to send to browser |
||
| 468 | $cacheInfo = $cacheInstance->getInfo($cacheKey); |
||
| 469 | if ($cacheInfo) { |
||
| 470 | $lastModified = $cacheInfo['mtime']; |
||
| 471 | $expire = $cacheInfo['expire']; |
||
| 472 | $maxAge = $expire - time(); |
||
| 473 | $this->setHeader('Pragma', 'public'); |
||
| 474 | $this->setHeader('Cache-Control', 'max-age=' . $maxAge . ', public'); |
||
| 475 | $this->setHeader('Expires', gmdate('D, d M Y H:i:s', $expire) . ' GMT'); |
||
| 476 | $this->setHeader('Last-modified', gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Set the value of '{elapsed_time}' and '{memory_usage}' |
||
| 482 | * @param string $content the page content |
||
| 483 | * @return string the page content after replace |
||
| 484 | * '{elapsed_time}', '{memory_usage}' |
||
| 485 | */ |
||
| 486 | protected function replaceElapseTimeAndMemoryUsage($content) { |
||
| 487 | // Parse out the elapsed time and memory usage, |
||
| 488 | // then swap the pseudo-variables with the data |
||
| 489 | $elapsedTime = get_instance()->benchmark->elapsedTime('APP_EXECUTION_START', 'APP_EXECUTION_END'); |
||
| 490 | $memoryUsage = round(get_instance()->benchmark->memoryUsage( |
||
| 491 | 'APP_EXECUTION_START', |
||
| 492 | 'APP_EXECUTION_END') / 1024 / 1024, 6) . 'MB'; |
||
| 493 | return str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsedTime, $memoryUsage), $content); |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get the module information for the view to load |
||
| 498 | * @param string $view the view name like moduleName/viewName, viewName |
||
| 499 | * |
||
| 500 | * @return array the module information |
||
| 501 | * array( |
||
| 502 | * 'module'=> 'module_name' |
||
| 503 | * 'view' => 'view_name' |
||
| 504 | * 'viewFile' => 'view_file' |
||
| 505 | * ) |
||
| 506 | */ |
||
| 507 | protected function getModuleInfoForView($view) { |
||
| 526 | ); |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Render the view page |
||
| 531 | * @see Response::render |
||
| 532 | * @return void|string |
||
| 533 | */ |
||
| 534 | protected function loadView($path, array $data = array(), $return = false) { |
||
| 535 | $found = false; |
||
| 536 | if (file_exists($path)) { |
||
| 537 | //super instance |
||
| 538 | $obj = & get_instance(); |
||
| 539 | if ($obj instanceof Controller) { |
||
| 540 | foreach (get_object_vars($obj) as $key => $value) { |
||
| 541 | if (!property_exists($this, $key)) { |
||
| 542 | $this->{$key} = & $obj->{$key}; |
||
| 543 | } |
||
| 544 | } |
||
| 545 | } |
||
| 546 | ob_start(); |
||
| 547 | extract($data); |
||
| 548 | //need use require() instead of require_once because can load this view many time |
||
| 549 | require $path; |
||
| 550 | $content = ob_get_clean(); |
||
| 551 | if ($return) { |
||
| 552 | return $content; |
||
| 553 | } |
||
| 554 | $this->_pageRender .= $content; |
||
| 555 | $found = true; |
||
| 556 | } |
||
| 557 | if (!$found) { |
||
| 558 | show_error('Unable to find view [' . $path . ']'); |
||
| 559 | } |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Set the mandory headers, like security, etc. |
||
| 564 | */ |
||
| 565 | protected function setRequiredHeaders() { |
||
| 573 | } |
||
| 574 | } |
||
| 575 | } |
||
| 576 | } |
||
| 577 |