ResponseStream.php 8.5 KB

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