123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
-
- namespace Illuminate\Http;
-
- use Exception;
- use Symfony\Component\HttpFoundation\HeaderBag;
- use Illuminate\Http\Exceptions\HttpResponseException;
-
- trait ResponseTrait
- {
- /**
- * The original content of the response.
- *
- * @var mixed
- */
- public $original;
-
- /**
- * The exception that triggered the error response (if applicable).
- *
- * @var \Exception|null
- */
- public $exception;
-
- /**
- * Get the status code for the response.
- *
- * @return int
- */
- public function status()
- {
- return $this->getStatusCode();
- }
-
- /**
- * Get the content of the response.
- *
- * @return string
- */
- public function content()
- {
- return $this->getContent();
- }
-
- /**
- * Get the original response content.
- *
- * @return mixed
- */
- public function getOriginalContent()
- {
- $original = $this->original;
-
- return $original instanceof self ? $original->{__FUNCTION__}() : $original;
- }
-
- /**
- * Set a header on the Response.
- *
- * @param string $key
- * @param array|string $values
- * @param bool $replace
- * @return $this
- */
- public function header($key, $values, $replace = true)
- {
- $this->headers->set($key, $values, $replace);
-
- return $this;
- }
-
- /**
- * Add an array of headers to the response.
- *
- * @param \Symfony\Component\HttpFoundation\HeaderBag|array $headers
- * @return $this
- */
- public function withHeaders($headers)
- {
- if ($headers instanceof HeaderBag) {
- $headers = $headers->all();
- }
-
- foreach ($headers as $key => $value) {
- $this->headers->set($key, $value);
- }
-
- return $this;
- }
-
- /**
- * Add a cookie to the response.
- *
- * @param \Symfony\Component\HttpFoundation\Cookie|mixed $cookie
- * @return $this
- */
- public function cookie($cookie)
- {
- return call_user_func_array([$this, 'withCookie'], func_get_args());
- }
-
- /**
- * Add a cookie to the response.
- *
- * @param \Symfony\Component\HttpFoundation\Cookie|mixed $cookie
- * @return $this
- */
- public function withCookie($cookie)
- {
- if (is_string($cookie) && function_exists('cookie')) {
- $cookie = call_user_func_array('cookie', func_get_args());
- }
-
- $this->headers->setCookie($cookie);
-
- return $this;
- }
-
- /**
- * Set the exception to attach to the response.
- *
- * @param \Exception $e
- * @return $this
- */
- public function withException(Exception $e)
- {
- $this->exception = $e;
-
- return $this;
- }
-
- /**
- * Throws the response in a HttpResponseException instance.
- *
- * @throws \Illuminate\Http\Exceptions\HttpResponseException
- */
- public function throwResponse()
- {
- throw new HttpResponseException($this);
- }
- }
|