PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/api/vendor/guzzlehttp/streams/src/Stream.php

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