SwooleFileStream.php 6.5 KB

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