Parcourir la source

* Add method for getting headers
* Don't allow shutdown on a response twice

Nathaniel van Diepen il y a 7 ans
Parent
commit
fad0b66cba
1 fichiers modifiés avec 30 ajouts et 22 suppressions
  1. 30 22
      Http/response.class.php

+ 30 - 22
Http/response.class.php

@@ -61,16 +61,21 @@
 			}
 			return $this;
 		}
-		public function header(string $name, string $value){
-			if($this->open){
-				$this->fire('header', $name, $value);
-				array_push(
-					$this->headers,
-					[
-						$name,
-						$value
-					]
-				);
+		public function header(string $name, string $value = null){
+			if(is_null($value)){
+				$headers = array_filter($this->headers,function($header) use($name){
+					return $header[0] == $name;
+				});
+				return end($headers)[1];
+			}else{
+				if($this->open){
+					if($this->fire('header', $name, $value) !== false){
+						$this->headers[] = [
+							$name,
+							$value
+						];
+					}
+				}
 			}
 			return $this;
 		}
@@ -113,19 +118,22 @@
 			return $this;
 		}
 		public function shutdown(){
-			$this->shutdown = true;
-			$this->fire('beforeshutdown');
-			if($this->open){
-				$this->end();
-			}
-			$this->fire('shutdown');
-			http_response_code($this->code);
-			foreach($this->headers as $k => $header){
-				header("{$header[0]}: $header[1]");
+			if(!$this->shutdown){
+				$this->shutdown = true;
+				$this->fire('beforeshutdown');
+				if($this->open){
+					$this->end();
+				}
+				$this->fire('shutdown');
+				http_response_code($this->code);
+				foreach($this->headers as $k => $header){
+					header("{$header[0]}: $header[1]");
+				}
+				echo $this->body;
+				flush();
+				$this->fire('aftershutdown');
 			}
-			echo $this->body;
-			flush();
-			$this->fire('aftershutdown');
+			return $this;
 		}
 		public function open(){
 			if(!$this->shutdown){