Stream.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use Psr\Http\Message\StreamInterface;
  4. /**
  5. * PHP stream implementation.
  6. *
  7. * @var $stream
  8. */
  9. class Stream implements StreamInterface
  10. {
  11. private $stream;
  12. private $size;
  13. private $seekable;
  14. private $readable;
  15. private $writable;
  16. private $uri;
  17. private $customMetadata;
  18. /** @var array Hash of readable and writable stream types */
  19. private static $readWriteHash = [
  20. 'read' => [
  21. 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true,
  22. 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true,
  23. 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true,
  24. 'x+t' => true, 'c+t' => true, 'a+' => true
  25. ],
  26. 'write' => [
  27. 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true,
  28. 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true,
  29. 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true,
  30. 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true
  31. ]
  32. ];
  33. /**
  34. * This constructor accepts an associative array of options.
  35. *
  36. * - size: (int) If a read stream would otherwise have an indeterminate
  37. * size, but the size is known due to foreknowledge, then you can
  38. * provide that size, in bytes.
  39. * - metadata: (array) Any additional metadata to return when the metadata
  40. * of the stream is accessed.
  41. *
  42. * @param resource $stream Stream resource to wrap.
  43. * @param array $options Associative array of options.
  44. *
  45. * @throws \InvalidArgumentException if the stream is not a stream resource
  46. */
  47. public function __construct($stream, $options = [])
  48. {
  49. if (!is_resource($stream)) {
  50. throw new \InvalidArgumentException('Stream must be a resource');
  51. }
  52. if (isset($options['size'])) {
  53. $this->size = $options['size'];
  54. }
  55. $this->customMetadata = isset($options['metadata'])
  56. ? $options['metadata']
  57. : [];
  58. $this->stream = $stream;
  59. $meta = stream_get_meta_data($this->stream);
  60. $this->seekable = $meta['seekable'];
  61. $this->readable = isset(self::$readWriteHash['read'][$meta['mode']]);
  62. $this->writable = isset(self::$readWriteHash['write'][$meta['mode']]);
  63. $this->uri = $this->getMetadata('uri');
  64. }
  65. public function __get($name)
  66. {
  67. if ($name == 'stream') {
  68. throw new \RuntimeException('The stream is detached');
  69. }
  70. throw new \BadMethodCallException('No value for ' . $name);
  71. }
  72. /**
  73. * Closes the stream when the destructed
  74. */
  75. public function __destruct()
  76. {
  77. $this->close();
  78. }
  79. public function __toString()
  80. {
  81. try {
  82. $this->seek(0);
  83. return (string) stream_get_contents($this->stream);
  84. } catch (\Exception $e) {
  85. return '';
  86. }
  87. }
  88. public function getContents()
  89. {
  90. $contents = stream_get_contents($this->stream);
  91. if ($contents === false) {
  92. throw new \RuntimeException('Unable to read stream contents');
  93. }
  94. return $contents;
  95. }
  96. public function close()
  97. {
  98. if (isset($this->stream)) {
  99. if (is_resource($this->stream)) {
  100. fclose($this->stream);
  101. }
  102. $this->detach();
  103. }
  104. }
  105. public function detach()
  106. {
  107. if (!isset($this->stream)) {
  108. return null;
  109. }
  110. $result = $this->stream;
  111. unset($this->stream);
  112. $this->size = $this->uri = null;
  113. $this->readable = $this->writable = $this->seekable = false;
  114. return $result;
  115. }
  116. public function getSize()
  117. {
  118. if ($this->size !== null) {
  119. return $this->size;
  120. }
  121. if (!isset($this->stream)) {
  122. return null;
  123. }
  124. // Clear the stat cache if the stream has a URI
  125. if ($this->uri) {
  126. clearstatcache(true, $this->uri);
  127. }
  128. $stats = fstat($this->stream);
  129. if (isset($stats['size'])) {
  130. $this->size = $stats['size'];
  131. return $this->size;
  132. }
  133. return null;
  134. }
  135. public function isReadable()
  136. {
  137. return $this->readable;
  138. }
  139. public function isWritable()
  140. {
  141. return $this->writable;
  142. }
  143. public function isSeekable()
  144. {
  145. return $this->seekable;
  146. }
  147. public function eof()
  148. {
  149. return !$this->stream || feof($this->stream);
  150. }
  151. public function tell()
  152. {
  153. $result = ftell($this->stream);
  154. if ($result === false) {
  155. throw new \RuntimeException('Unable to determine stream position');
  156. }
  157. return $result;
  158. }
  159. public function rewind()
  160. {
  161. $this->seek(0);
  162. }
  163. public function seek($offset, $whence = SEEK_SET)
  164. {
  165. if (!$this->seekable) {
  166. throw new \RuntimeException('Stream is not seekable');
  167. } elseif (fseek($this->stream, $offset, $whence) === -1) {
  168. throw new \RuntimeException('Unable to seek to stream position '
  169. . $offset . ' with whence ' . var_export($whence, true));
  170. }
  171. }
  172. public function read($length)
  173. {
  174. if (!$this->readable) {
  175. throw new \RuntimeException('Cannot read from non-readable stream');
  176. }
  177. if ($length < 0) {
  178. throw new \RuntimeException('Length parameter cannot be negative');
  179. }
  180. if (0 === $length) {
  181. return '';
  182. }
  183. $string = fread($this->stream, $length);
  184. if (false === $string) {
  185. throw new \RuntimeException('Unable to read from stream');
  186. }
  187. return $string;
  188. }
  189. public function write($string)
  190. {
  191. if (!$this->writable) {
  192. throw new \RuntimeException('Cannot write to a non-writable stream');
  193. }
  194. // We can't know the size after writing anything
  195. $this->size = null;
  196. $result = fwrite($this->stream, $string);
  197. if ($result === false) {
  198. throw new \RuntimeException('Unable to write to stream');
  199. }
  200. return $result;
  201. }
  202. public function getMetadata($key = null)
  203. {
  204. if (!isset($this->stream)) {
  205. return $key ? null : [];
  206. } elseif (!$key) {
  207. return $this->customMetadata + stream_get_meta_data($this->stream);
  208. } elseif (isset($this->customMetadata[$key])) {
  209. return $this->customMetadata[$key];
  210. }
  211. $meta = stream_get_meta_data($this->stream);
  212. return isset($meta[$key]) ? $meta[$key] : null;
  213. }
  214. }