Stream.php 7.2 KB

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