ResponseTrait.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Illuminate\Http;
  3. use Exception;
  4. use Symfony\Component\HttpFoundation\HeaderBag;
  5. use Illuminate\Http\Exceptions\HttpResponseException;
  6. trait ResponseTrait
  7. {
  8. /**
  9. * The original content of the response.
  10. *
  11. * @var mixed
  12. */
  13. public $original;
  14. /**
  15. * The exception that triggered the error response (if applicable).
  16. *
  17. * @var \Exception|null
  18. */
  19. public $exception;
  20. /**
  21. * Get the status code for the response.
  22. *
  23. * @return int
  24. */
  25. public function status()
  26. {
  27. return $this->getStatusCode();
  28. }
  29. /**
  30. * Get the content of the response.
  31. *
  32. * @return string
  33. */
  34. public function content()
  35. {
  36. return $this->getContent();
  37. }
  38. /**
  39. * Get the original response content.
  40. *
  41. * @return mixed
  42. */
  43. public function getOriginalContent()
  44. {
  45. $original = $this->original;
  46. return $original instanceof self ? $original->{__FUNCTION__}() : $original;
  47. }
  48. /**
  49. * Set a header on the Response.
  50. *
  51. * @param string $key
  52. * @param array|string $values
  53. * @param bool $replace
  54. * @return $this
  55. */
  56. public function header($key, $values, $replace = true)
  57. {
  58. $this->headers->set($key, $values, $replace);
  59. return $this;
  60. }
  61. /**
  62. * Add an array of headers to the response.
  63. *
  64. * @param \Symfony\Component\HttpFoundation\HeaderBag|array $headers
  65. * @return $this
  66. */
  67. public function withHeaders($headers)
  68. {
  69. if ($headers instanceof HeaderBag) {
  70. $headers = $headers->all();
  71. }
  72. foreach ($headers as $key => $value) {
  73. $this->headers->set($key, $value);
  74. }
  75. return $this;
  76. }
  77. /**
  78. * Add a cookie to the response.
  79. *
  80. * @param \Symfony\Component\HttpFoundation\Cookie|mixed $cookie
  81. * @return $this
  82. */
  83. public function cookie($cookie)
  84. {
  85. return call_user_func_array([$this, 'withCookie'], func_get_args());
  86. }
  87. /**
  88. * Add a cookie to the response.
  89. *
  90. * @param \Symfony\Component\HttpFoundation\Cookie|mixed $cookie
  91. * @return $this
  92. */
  93. public function withCookie($cookie)
  94. {
  95. if (is_string($cookie) && function_exists('cookie')) {
  96. $cookie = call_user_func_array('cookie', func_get_args());
  97. }
  98. $this->headers->setCookie($cookie);
  99. return $this;
  100. }
  101. /**
  102. * Set the exception to attach to the response.
  103. *
  104. * @param \Exception $e
  105. * @return $this
  106. */
  107. public function withException(Exception $e)
  108. {
  109. $this->exception = $e;
  110. return $this;
  111. }
  112. /**
  113. * Throws the response in a HttpResponseException instance.
  114. *
  115. * @throws \Illuminate\Http\Exceptions\HttpResponseException
  116. */
  117. public function throwResponse()
  118. {
  119. throw new HttpResponseException($this);
  120. }
  121. }