Response.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Base HTTP response object
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\HTTP\PhpEnvironment;
  9. class Response extends \Zend\Http\PhpEnvironment\Response implements \Magento\Framework\App\Response\HttpInterface
  10. {
  11. /**
  12. * Flag; is this response a redirect?
  13. * @var boolean
  14. */
  15. protected $isRedirect = false;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function getHeader($name)
  20. {
  21. $header = false;
  22. $headers = $this->getHeaders();
  23. if ($headers->has($name)) {
  24. $header = $headers->get($name);
  25. }
  26. return $header;
  27. }
  28. /**
  29. * Send the response, including all headers, rendering exceptions if so
  30. * requested.
  31. *
  32. * @return void
  33. */
  34. public function sendResponse()
  35. {
  36. $this->send();
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function appendBody($value)
  42. {
  43. $body = $this->getContent();
  44. $this->setContent($body . $value);
  45. return $this;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function setBody($value)
  51. {
  52. $this->setContent($value);
  53. return $this;
  54. }
  55. /**
  56. * Clear body
  57. * @return $this
  58. */
  59. public function clearBody()
  60. {
  61. $this->setContent('');
  62. return $this;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function setHeader($name, $value, $replace = false)
  68. {
  69. $value = (string)$value;
  70. if ($replace) {
  71. $this->clearHeader($name);
  72. }
  73. $this->getHeaders()->addHeaderLine($name, $value);
  74. return $this;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function clearHeader($name)
  80. {
  81. $headers = $this->getHeaders();
  82. if ($headers->has($name)) {
  83. $header = $headers->get($name);
  84. $headers->removeHeader($header);
  85. }
  86. return $this;
  87. }
  88. /**
  89. * Remove all headers
  90. *
  91. * @return $this
  92. */
  93. public function clearHeaders()
  94. {
  95. $headers = $this->getHeaders();
  96. $headers->clearHeaders();
  97. return $this;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function setRedirect($url, $code = 302)
  103. {
  104. $this->setHeader('Location', $url, true)
  105. ->setHttpResponseCode($code);
  106. return $this;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function setHttpResponseCode($code)
  112. {
  113. if (!is_numeric($code) || (100 > $code) || (599 < $code)) {
  114. throw new \InvalidArgumentException('Invalid HTTP response code');
  115. }
  116. $this->isRedirect = (300 <= $code && 307 >= $code) ? true : false;
  117. $this->setStatusCode($code);
  118. return $this;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function setStatusHeader($httpCode, $version = null, $phrase = null)
  124. {
  125. $version = $version === null ? $this->detectVersion() : $version;
  126. $phrase = $phrase === null ? $this->getReasonPhrase() : $phrase;
  127. $this->setVersion($version);
  128. $this->setHttpResponseCode($httpCode);
  129. $this->setReasonPhrase($phrase);
  130. return $this;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function getHttpResponseCode()
  136. {
  137. return $this->getStatusCode();
  138. }
  139. /**
  140. * Is this a redirect?
  141. *
  142. * @return boolean
  143. */
  144. public function isRedirect()
  145. {
  146. return $this->isRedirect;
  147. }
  148. /**
  149. * @return string[]
  150. */
  151. public function __sleep()
  152. {
  153. return ['content', 'isRedirect', 'statusCode'];
  154. }
  155. }