1<?php 2 3namespace Illuminate\Http; 4 5use Illuminate\Http\Exceptions\HttpResponseException; 6use Symfony\Component\HttpFoundation\HeaderBag; 7use Throwable; 8 9trait ResponseTrait 10{ 11 /** 12 * The original content of the response. 13 * 14 * @var mixed 15 */ 16 public $original; 17 18 /** 19 * The exception that triggered the error response (if applicable). 20 * 21 * @var \Throwable|null 22 */ 23 public $exception; 24 25 /** 26 * Get the status code for the response. 27 * 28 * @return int 29 */ 30 public function status() 31 { 32 return $this->getStatusCode(); 33 } 34 35 /** 36 * Get the content of the response. 37 * 38 * @return string 39 */ 40 public function content() 41 { 42 return $this->getContent(); 43 } 44 45 /** 46 * Get the original response content. 47 * 48 * @return mixed 49 */ 50 public function getOriginalContent() 51 { 52 $original = $this->original; 53 54 return $original instanceof self ? $original->{__FUNCTION__}() : $original; 55 } 56 57 /** 58 * Set a header on the Response. 59 * 60 * @param string $key 61 * @param array|string $values 62 * @param bool $replace 63 * @return $this 64 */ 65 public function header($key, $values, $replace = true) 66 { 67 $this->headers->set($key, $values, $replace); 68 69 return $this; 70 } 71 72 /** 73 * Add an array of headers to the response. 74 * 75 * @param \Symfony\Component\HttpFoundation\HeaderBag|array $headers 76 * @return $this 77 */ 78 public function withHeaders($headers) 79 { 80 if ($headers instanceof HeaderBag) { 81 $headers = $headers->all(); 82 } 83 84 foreach ($headers as $key => $value) { 85 $this->headers->set($key, $value); 86 } 87 88 return $this; 89 } 90 91 /** 92 * Add a cookie to the response. 93 * 94 * @param \Symfony\Component\HttpFoundation\Cookie|mixed $cookie 95 * @return $this 96 */ 97 public function cookie($cookie) 98 { 99 return $this->withCookie(...func_get_args()); 100 } 101 102 /** 103 * Add a cookie to the response. 104 * 105 * @param \Symfony\Component\HttpFoundation\Cookie|mixed $cookie 106 * @return $this 107 */ 108 public function withCookie($cookie) 109 { 110 if (is_string($cookie) && function_exists('cookie')) { 111 $cookie = cookie(...func_get_args()); 112 } 113 114 $this->headers->setCookie($cookie); 115 116 return $this; 117 } 118 119 /** 120 * Expire a cookie when sending the response. 121 * 122 * @param \Symfony\Component\HttpFoundation\Cookie|mixed $cookie 123 * @param string|null $path 124 * @param string|null $domain 125 * @return $this 126 */ 127 public function withoutCookie($cookie, $path = null, $domain = null) 128 { 129 if (is_string($cookie) && function_exists('cookie')) { 130 $cookie = cookie($cookie, null, -2628000, $path, $domain); 131 } 132 133 $this->headers->setCookie($cookie); 134 135 return $this; 136 } 137 138 /** 139 * Get the callback of the response. 140 * 141 * @return string|null 142 */ 143 public function getCallback() 144 { 145 return $this->callback ?? null; 146 } 147 148 /** 149 * Set the exception to attach to the response. 150 * 151 * @param \Throwable $e 152 * @return $this 153 */ 154 public function withException(Throwable $e) 155 { 156 $this->exception = $e; 157 158 return $this; 159 } 160 161 /** 162 * Throws the response in a HttpResponseException instance. 163 * 164 * @return void 165 * 166 * @throws \Illuminate\Http\Exceptions\HttpResponseException 167 */ 168 public function throwResponse() 169 { 170 throw new HttpResponseException($this); 171 } 172} 173