Passed
Push — 1.0.0-dev ( 2b6704...9c9ab7 )
by nguereza
03:19
created
core/classes/Response.php 1 patch
Spacing   +77 added lines, -77 removed lines patch added patch discarded remove patch
@@ -24,7 +24,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 
Please login to merge, or discard this patch.