SwooleStream.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\Stream;
  12. use BadMethodCallException;
  13. use Psr\Http\Message\StreamInterface;
  14. use RuntimeException;
  15. use Stringable;
  16. use Throwable;
  17. class SwooleStream implements StreamInterface, Stringable
  18. {
  19. protected int $size;
  20. protected bool $writable;
  21. /**
  22. * SwooleStream constructor.
  23. */
  24. public function __construct(protected string $contents = '')
  25. {
  26. $this->size = strlen($this->contents);
  27. $this->writable = true;
  28. }
  29. /**
  30. * Reads all data from the stream into a string, from the beginning to end.
  31. * This method MUST attempt to seek to the beginning of the stream before
  32. * reading data and read the stream until the end is reached.
  33. * Warning: This could attempt to load a large amount of data into memory.
  34. * This method MUST NOT raise an exception in order to conform with PHP's
  35. * string casting operations.
  36. *
  37. * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring
  38. */
  39. public function __toString(): string
  40. {
  41. try {
  42. return $this->getContents();
  43. } catch (Throwable) {
  44. return '';
  45. }
  46. }
  47. /**
  48. * Closes the stream and any underlying resources.
  49. */
  50. public function close(): void
  51. {
  52. $this->detach();
  53. }
  54. /**
  55. * Separates any underlying resources from the stream.
  56. * After the stream has been detached, the stream is in an unusable state.
  57. *
  58. * @return null|resource Underlying PHP stream, if any
  59. */
  60. public function detach()
  61. {
  62. $this->contents = '';
  63. $this->size = 0;
  64. $this->writable = false;
  65. return null;
  66. }
  67. /**
  68. * Get the size of the stream if known.
  69. *
  70. * @return null|int returns the size in bytes if known, or null if unknown
  71. */
  72. public function getSize(): ?int
  73. {
  74. if (! $this->size) {
  75. $this->size = strlen($this->getContents());
  76. }
  77. return $this->size;
  78. }
  79. /**
  80. * Returns the current position of the file read/write pointer.
  81. *
  82. * @return int Position of the file pointer
  83. * @throws RuntimeException on error
  84. */
  85. public function tell(): int
  86. {
  87. throw new RuntimeException('Cannot determine the position of a SwooleStream');
  88. }
  89. /**
  90. * Returns true if the stream is at the end of the stream.
  91. */
  92. public function eof(): bool
  93. {
  94. return $this->getSize() === 0;
  95. }
  96. /**
  97. * Returns whether or not the stream is seekable.
  98. */
  99. public function isSeekable(): bool
  100. {
  101. return false;
  102. }
  103. /**
  104. * Seek to a position in the stream.
  105. *
  106. * @see http://www.php.net/manual/en/function.fseek.php
  107. * @param int $offset Stream offset
  108. * @param int $whence Specifies how the cursor position will be calculated
  109. * based on the seek offset. Valid values are identical to the built-in
  110. * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to
  111. * offset bytes SEEK_CUR: Set position to current location plus offset
  112. * SEEK_END: Set position to end-of-stream plus offset.
  113. * @throws RuntimeException on failure
  114. */
  115. public function seek($offset, $whence = SEEK_SET): void
  116. {
  117. throw new RuntimeException('Cannot seek a SwooleStream');
  118. }
  119. /**
  120. * Seek to the beginning of the stream.
  121. * If the stream is not seekable, this method will raise an exception;
  122. * otherwise, it will perform a seek(0).
  123. *
  124. * @throws RuntimeException on failure
  125. * @see http://www.php.net/manual/en/function.fseek.php
  126. * @see seek()
  127. */
  128. public function rewind(): void
  129. {
  130. $this->seek(0);
  131. }
  132. /**
  133. * Returns whether or not the stream is writable.
  134. */
  135. public function isWritable(): bool
  136. {
  137. return $this->writable;
  138. }
  139. /**
  140. * Write data to the stream.
  141. *
  142. * @param string $string the string that is to be written
  143. * @return int returns the number of bytes written to the stream
  144. * @throws RuntimeException on failure
  145. */
  146. public function write($string): int
  147. {
  148. if (! $this->writable) {
  149. throw new RuntimeException('Cannot write to a non-writable stream');
  150. }
  151. $size = strlen($string);
  152. $this->contents .= $string;
  153. $this->size += $size;
  154. return $size;
  155. }
  156. /**
  157. * Returns whether or not the stream is readable.
  158. */
  159. public function isReadable(): bool
  160. {
  161. return true;
  162. }
  163. /**
  164. * Read data from the stream.
  165. *
  166. * @param int $length Read up to $length bytes from the object and return
  167. * them. Fewer than $length bytes may be returned if underlying stream
  168. * call returns fewer bytes.
  169. * @return string returns the data read from the stream, or an empty string
  170. * if no bytes are available
  171. * @throws RuntimeException if an error occurs
  172. */
  173. public function read($length): string
  174. {
  175. if ($length >= $this->getSize()) {
  176. $result = $this->contents;
  177. $this->contents = '';
  178. $this->size = 0;
  179. } else {
  180. $result = substr($this->contents, 0, $length);
  181. $this->contents = substr($this->contents, $length);
  182. $this->size = $this->getSize() - $length;
  183. }
  184. return $result;
  185. }
  186. /**
  187. * Returns the remaining contents in a string.
  188. *
  189. * @throws RuntimeException if unable to read or an error occurs while
  190. * reading
  191. */
  192. public function getContents(): string
  193. {
  194. return $this->contents;
  195. }
  196. /**
  197. * Get stream metadata as an associative array or retrieve a specific key.
  198. * The keys returned are identical to the keys returned from PHP's
  199. * stream_get_meta_data() function.
  200. *
  201. * @see http://php.net/manual/en/function.stream-get-meta-data.php
  202. * @param string $key specific metadata to retrieve
  203. * @return null|array|mixed Returns an associative array if no key is
  204. * provided. Returns a specific key value if a key is provided and the
  205. * value is found, or null if the key is not found.
  206. */
  207. public function getMetadata($key = null)
  208. {
  209. throw new BadMethodCallException('Not implemented');
  210. }
  211. }