Stream.php 6.9 KB

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