Stream.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. if ($this->isSeekable()) {
  71. $this->seek(0);
  72. }
  73. return $this->getContents();
  74. } catch (\Exception $e) {
  75. return '';
  76. }
  77. }
  78. public function getContents()
  79. {
  80. if (!isset($this->stream)) {
  81. throw new \RuntimeException('Stream is detached');
  82. }
  83. $contents = stream_get_contents($this->stream);
  84. if ($contents === false) {
  85. throw new \RuntimeException('Unable to read stream contents');
  86. }
  87. return $contents;
  88. }
  89. public function close()
  90. {
  91. if (isset($this->stream)) {
  92. if (is_resource($this->stream)) {
  93. fclose($this->stream);
  94. }
  95. $this->detach();
  96. }
  97. }
  98. public function detach()
  99. {
  100. if (!isset($this->stream)) {
  101. return null;
  102. }
  103. $result = $this->stream;
  104. unset($this->stream);
  105. $this->size = $this->uri = null;
  106. $this->readable = $this->writable = $this->seekable = false;
  107. return $result;
  108. }
  109. public function getSize()
  110. {
  111. if ($this->size !== null) {
  112. return $this->size;
  113. }
  114. if (!isset($this->stream)) {
  115. return null;
  116. }
  117. // Clear the stat cache if the stream has a URI
  118. if ($this->uri) {
  119. clearstatcache(true, $this->uri);
  120. }
  121. $stats = fstat($this->stream);
  122. if (isset($stats['size'])) {
  123. $this->size = $stats['size'];
  124. return $this->size;
  125. }
  126. return null;
  127. }
  128. public function isReadable()
  129. {
  130. return $this->readable;
  131. }
  132. public function isWritable()
  133. {
  134. return $this->writable;
  135. }
  136. public function isSeekable()
  137. {
  138. return $this->seekable;
  139. }
  140. public function eof()
  141. {
  142. if (!isset($this->stream)) {
  143. throw new \RuntimeException('Stream is detached');
  144. }
  145. return feof($this->stream);
  146. }
  147. public function tell()
  148. {
  149. if (!isset($this->stream)) {
  150. throw new \RuntimeException('Stream is detached');
  151. }
  152. $result = ftell($this->stream);
  153. if ($result === false) {
  154. throw new \RuntimeException('Unable to determine stream position');
  155. }
  156. return $result;
  157. }
  158. public function rewind()
  159. {
  160. $this->seek(0);
  161. }
  162. public function seek($offset, $whence = SEEK_SET)
  163. {
  164. $whence = (int) $whence;
  165. if (!isset($this->stream)) {
  166. throw new \RuntimeException('Stream is detached');
  167. }
  168. if (!$this->seekable) {
  169. throw new \RuntimeException('Stream is not seekable');
  170. }
  171. if (fseek($this->stream, $offset, $whence) === -1) {
  172. throw new \RuntimeException('Unable to seek to stream position '
  173. . $offset . ' with whence ' . var_export($whence, true));
  174. }
  175. }
  176. public function read($length)
  177. {
  178. if (!isset($this->stream)) {
  179. throw new \RuntimeException('Stream is detached');
  180. }
  181. if (!$this->readable) {
  182. throw new \RuntimeException('Cannot read from non-readable stream');
  183. }
  184. if ($length < 0) {
  185. throw new \RuntimeException('Length parameter cannot be negative');
  186. }
  187. if (0 === $length) {
  188. return '';
  189. }
  190. $string = fread($this->stream, $length);
  191. if (false === $string) {
  192. throw new \RuntimeException('Unable to read from stream');
  193. }
  194. return $string;
  195. }
  196. public function write($string)
  197. {
  198. if (!isset($this->stream)) {
  199. throw new \RuntimeException('Stream is detached');
  200. }
  201. if (!$this->writable) {
  202. throw new \RuntimeException('Cannot write to a non-writable stream');
  203. }
  204. // We can't know the size after writing anything
  205. $this->size = null;
  206. $result = fwrite($this->stream, $string);
  207. if ($result === false) {
  208. throw new \RuntimeException('Unable to write to stream');
  209. }
  210. return $result;
  211. }
  212. public function getMetadata($key = null)
  213. {
  214. if (!isset($this->stream)) {
  215. return $key ? null : [];
  216. } elseif (!$key) {
  217. return $this->customMetadata + stream_get_meta_data($this->stream);
  218. } elseif (isset($this->customMetadata[$key])) {
  219. return $this->customMetadata[$key];
  220. }
  221. $meta = stream_get_meta_data($this->stream);
  222. return isset($meta[$key]) ? $meta[$key] : null;
  223. }
  224. }