PageRenderTime 102ms CodeModel.GetById 22ms RepoModel.GetById 3ms app.codeStats 0ms

/vendor/guzzlehttp/psr7/src/Stream.php

https://gitlab.com/ealexis.t/trends
PHP | 245 lines | 179 code | 41 blank | 25 comment | 19 complexity | c5bf50476cf7c5ea7f9c325cc66362b8 MD5 | raw file
  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. return fread($this->stream, $length);
  178. }
  179. public function write($string)
  180. {
  181. if (!$this->writable) {
  182. throw new \RuntimeException('Cannot write to a non-writable stream');
  183. }
  184. // We can't know the size after writing anything
  185. $this->size = null;
  186. $result = fwrite($this->stream, $string);
  187. if ($result === false) {
  188. throw new \RuntimeException('Unable to write to stream');
  189. }
  190. return $result;
  191. }
  192. public function getMetadata($key = null)
  193. {
  194. if (!isset($this->stream)) {
  195. return $key ? null : [];
  196. } elseif (!$key) {
  197. return $this->customMetadata + stream_get_meta_data($this->stream);
  198. } elseif (isset($this->customMetadata[$key])) {
  199. return $this->customMetadata[$key];
  200. }
  201. $meta = stream_get_meta_data($this->stream);
  202. return isset($meta[$key]) ? $meta[$key] : null;
  203. }
  204. }