Stream.php 6.6 KB

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