ResponseProxyTrait.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\HttpMessage\Server;
  12. use Hyperf\HttpMessage\Cookie\Cookie;
  13. use Psr\Http\Message\ResponseInterface;
  14. use Psr\Http\Message\StreamInterface;
  15. use RuntimeException;
  16. trait ResponseProxyTrait
  17. {
  18. protected ?ResponseInterface $response = null;
  19. public function setResponse(ResponseInterface $response)
  20. {
  21. $this->response = $response;
  22. }
  23. public function getResponse(): ResponseInterface
  24. {
  25. if (! $this->response instanceof ResponseInterface) {
  26. throw new RuntimeException('response is invalid.');
  27. }
  28. return $this->response;
  29. }
  30. public function getProtocolVersion(): string
  31. {
  32. return $this->getResponse()->getProtocolVersion();
  33. }
  34. public function withProtocolVersion($version): static
  35. {
  36. $this->setResponse($this->getResponse()->withProtocolVersion($version));
  37. return $this;
  38. }
  39. public function getHeaders(): array
  40. {
  41. return $this->getResponse()->getHeaders();
  42. }
  43. public function hasHeader($name): bool
  44. {
  45. return $this->getResponse()->hasHeader($name);
  46. }
  47. public function getHeader($name): array
  48. {
  49. return $this->getResponse()->getHeader($name);
  50. }
  51. public function getHeaderLine($name): string
  52. {
  53. return $this->getResponse()->getHeaderLine($name);
  54. }
  55. public function withHeader($name, $value): static
  56. {
  57. $this->setResponse($this->getResponse()->withHeader($name, $value));
  58. return $this;
  59. }
  60. public function withAddedHeader($name, $value): static
  61. {
  62. $this->setResponse($this->getResponse()->withAddedHeader($name, $value));
  63. return $this;
  64. }
  65. public function withoutHeader($name): static
  66. {
  67. $this->setResponse($this->getResponse()->withoutHeader($name));
  68. return $this;
  69. }
  70. public function getBody(): StreamInterface
  71. {
  72. return $this->getResponse()->getBody();
  73. }
  74. public function withBody(StreamInterface $body): static
  75. {
  76. $this->setResponse($this->getResponse()->withBody($body));
  77. return $this;
  78. }
  79. public function getStatusCode(): int
  80. {
  81. return $this->getResponse()->getStatusCode();
  82. }
  83. public function withStatus($code, $reasonPhrase = ''): static
  84. {
  85. $this->setResponse($this->getResponse()->withStatus($code, $reasonPhrase));
  86. return $this;
  87. }
  88. public function getReasonPhrase(): string
  89. {
  90. return $this->getResponse()->getReasonPhrase();
  91. }
  92. /**
  93. * Returns an instance with specified cookies.
  94. */
  95. public function withCookie(Cookie $cookie): static
  96. {
  97. $response = $this->getResponse();
  98. if (! method_exists($response, 'withCookie')) {
  99. throw new RuntimeException('Method withCookie is invalid.');
  100. }
  101. $this->setResponse($response->withCookie($cookie));
  102. return $this;
  103. }
  104. /**
  105. * Retrieves all cookies.
  106. */
  107. public function getCookies(): array
  108. {
  109. $response = $this->getResponse();
  110. if (! method_exists($response, 'getCookies')) {
  111. throw new RuntimeException('Method getCookies is invalid.');
  112. }
  113. return $response->getCookies();
  114. }
  115. /**
  116. * Returns an instance with specified trailer.
  117. * @param string $value
  118. */
  119. public function withTrailer(string $key, $value): static
  120. {
  121. $response = $this->getResponse();
  122. if (! method_exists($response, 'withTrailer')) {
  123. throw new RuntimeException('Method withTrailer is invalid.');
  124. }
  125. $this->setResponse($response->withTrailer($key, $value));
  126. return $this;
  127. }
  128. /**
  129. * Retrieves a specified trailer value, returns null if the value does not exists.
  130. */
  131. public function getTrailer(string $key)
  132. {
  133. $response = $this->getResponse();
  134. if (! method_exists($response, 'getTrailer')) {
  135. throw new RuntimeException('Method getTrailer is invalid.');
  136. }
  137. return $response->getTrailer($key);
  138. }
  139. /**
  140. * Retrieves all trailers values.
  141. */
  142. public function getTrailers(): array
  143. {
  144. $response = $this->getResponse();
  145. if (! method_exists($response, 'getTrailers')) {
  146. throw new RuntimeException('Method getTrailers is invalid.');
  147. }
  148. return $response->getTrailers();
  149. }
  150. }