/vendor/guzzle/stream/Guzzle/Stream/Stream.php

https://bitbucket.org/nbravo777/repo_ratchet · PHP · 296 lines · 209 code · 55 blank · 32 comment · 17 complexity · 4e396b4eea35214354a4d9bb03bd005f MD5 · raw file

  1. <?php
  2. namespace Guzzle\Stream;
  3. use Guzzle\Common\Exception\InvalidArgumentException;
  4. /**
  5. * PHP stream implementation
  6. */
  7. class Stream implements StreamInterface
  8. {
  9. const STREAM_TYPE = 'stream_type';
  10. const WRAPPER_TYPE = 'wrapper_type';
  11. const IS_LOCAL = 'is_local';
  12. const IS_READABLE = 'is_readable';
  13. const IS_WRITABLE = 'is_writable';
  14. const SEEKABLE = 'seekable';
  15. /** @var resource Stream resource */
  16. protected $stream;
  17. /** @var int Size of the stream contents in bytes */
  18. protected $size;
  19. /** @var array Stream cached data */
  20. protected $cache = array();
  21. /** @var array Custom stream data */
  22. protected $customData = array();
  23. /** @var array Hash table of readable and writeable stream types for fast lookups */
  24. protected static $readWriteHash = array(
  25. 'read' => array(
  26. 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true,
  27. 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true, 'c+b' => true,
  28. 'rt' => true, 'w+t' => true, 'r+t' => true, 'x+t' => true, 'c+t' => true, 'a+' => true
  29. ),
  30. 'write' => array(
  31. 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true, 'c+' => true,
  32. 'wb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true, 'c+b' => true,
  33. 'w+t' => true, 'r+t' => true, 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true
  34. )
  35. );
  36. /**
  37. * @param resource $stream Stream resource to wrap
  38. * @param int $size Size of the stream in bytes. Only pass if the size cannot be obtained from the stream.
  39. *
  40. * @throws InvalidArgumentException if the stream is not a stream resource
  41. */
  42. public function __construct($stream, $size = null)
  43. {
  44. $this->setStream($stream, $size);
  45. }
  46. /**
  47. * Closes the stream when the helper is destructed
  48. */
  49. public function __destruct()
  50. {
  51. $this->close();
  52. }
  53. public function __toString()
  54. {
  55. if (!$this->isReadable() || (!$this->isSeekable() && $this->isConsumed())) {
  56. return '';
  57. }
  58. $originalPos = $this->ftell();
  59. $body = stream_get_contents($this->stream, -1, 0);
  60. $this->seek($originalPos);
  61. return $body;
  62. }
  63. public function close()
  64. {
  65. if (is_resource($this->stream)) {
  66. fclose($this->stream);
  67. }
  68. $this->cache[self::IS_READABLE] = false;
  69. $this->cache[self::IS_WRITABLE] = false;
  70. }
  71. /**
  72. * Calculate a hash of a Stream
  73. *
  74. * @param StreamInterface $stream Stream to calculate the hash for
  75. * @param string $algo Hash algorithm (e.g. md5, crc32, etc)
  76. * @param bool $rawOutput Whether or not to use raw output
  77. *
  78. * @return bool|string Returns false on failure or a hash string on success
  79. */
  80. public static function getHash(StreamInterface $stream, $algo, $rawOutput = false)
  81. {
  82. $pos = $stream->ftell();
  83. if (!$stream->seek(0)) {
  84. return false;
  85. }
  86. $ctx = hash_init($algo);
  87. while ($data = $stream->read(8192)) {
  88. hash_update($ctx, $data);
  89. }
  90. $out = hash_final($ctx, (bool) $rawOutput);
  91. $stream->seek($pos);
  92. return $out;
  93. }
  94. public function getMetaData($key = null)
  95. {
  96. $meta = stream_get_meta_data($this->stream);
  97. return !$key ? $meta : (array_key_exists($key, $meta) ? $meta[$key] : null);
  98. }
  99. public function getStream()
  100. {
  101. return $this->stream;
  102. }
  103. public function setStream($stream, $size = null)
  104. {
  105. if (!is_resource($stream)) {
  106. throw new InvalidArgumentException('Stream must be a resource');
  107. }
  108. $this->size = $size;
  109. $this->stream = $stream;
  110. $this->rebuildCache();
  111. return $this;
  112. }
  113. public function detachStream()
  114. {
  115. $this->stream = null;
  116. return $this;
  117. }
  118. public function getWrapper()
  119. {
  120. return $this->cache[self::WRAPPER_TYPE];
  121. }
  122. public function getWrapperData()
  123. {
  124. return $this->getMetaData('wrapper_data') ?: array();
  125. }
  126. public function getStreamType()
  127. {
  128. return $this->cache[self::STREAM_TYPE];
  129. }
  130. public function getUri()
  131. {
  132. return $this->cache['uri'];
  133. }
  134. public function getSize()
  135. {
  136. if ($this->size !== null) {
  137. return $this->size;
  138. }
  139. // If the stream is a file based stream and local, then use fstat
  140. if ($this->isLocal()) {
  141. clearstatcache(true, $this->getUri());
  142. $stats = fstat($this->stream);
  143. if (isset($stats['size'])) {
  144. return $stats['size'];
  145. }
  146. }
  147. // Only get the size based on the content if the the stream is readable and seekable
  148. if (!$this->cache[self::IS_READABLE] || !$this->cache[self::SEEKABLE]) {
  149. return false;
  150. } else {
  151. $pos = $this->ftell();
  152. $this->size = strlen((string) $this);
  153. $this->seek($pos);
  154. return $this->size;
  155. }
  156. }
  157. public function isReadable()
  158. {
  159. return $this->cache[self::IS_READABLE];
  160. }
  161. public function isWritable()
  162. {
  163. return $this->cache[self::IS_WRITABLE];
  164. }
  165. public function isConsumed()
  166. {
  167. return feof($this->stream);
  168. }
  169. public function feof()
  170. {
  171. return $this->isConsumed();
  172. }
  173. public function isLocal()
  174. {
  175. return $this->cache[self::IS_LOCAL];
  176. }
  177. public function isSeekable()
  178. {
  179. return $this->cache[self::SEEKABLE];
  180. }
  181. public function setSize($size)
  182. {
  183. $this->size = $size;
  184. return $this;
  185. }
  186. public function seek($offset, $whence = SEEK_SET)
  187. {
  188. return $this->cache[self::SEEKABLE] ? fseek($this->stream, $offset, $whence) === 0 : false;
  189. }
  190. public function read($length)
  191. {
  192. return $this->cache[self::IS_READABLE] ? fread($this->stream, $length) : false;
  193. }
  194. public function write($string)
  195. {
  196. if (!$this->cache[self::IS_WRITABLE]) {
  197. return 0;
  198. }
  199. $bytes = fwrite($this->stream, $string);
  200. // We can't know the size after writing if any bytes were written
  201. if ($bytes) {
  202. $this->size = null;
  203. }
  204. return $bytes;
  205. }
  206. public function ftell()
  207. {
  208. return ftell($this->stream);
  209. }
  210. public function rewind()
  211. {
  212. return $this->seek(0);
  213. }
  214. public function readLine($maxLength = null)
  215. {
  216. if (!$this->cache[self::IS_READABLE]) {
  217. return false;
  218. } else {
  219. return $maxLength ? fgets($this->getStream(), $maxLength) : fgets($this->getStream());
  220. }
  221. }
  222. public function setCustomData($key, $value)
  223. {
  224. $this->customData[$key] = $value;
  225. return $this;
  226. }
  227. public function getCustomData($key)
  228. {
  229. return isset($this->customData[$key]) ? $this->customData[$key] : null;
  230. }
  231. /**
  232. * Reprocess stream metadata
  233. */
  234. protected function rebuildCache()
  235. {
  236. $this->cache = stream_get_meta_data($this->stream);
  237. $this->cache[self::IS_LOCAL] = stream_is_local($this->stream);
  238. $this->cache[self::IS_READABLE] = isset(self::$readWriteHash['read'][$this->cache['mode']]);
  239. $this->cache[self::IS_WRITABLE] = isset(self::$readWriteHash['write'][$this->cache['mode']]);
  240. }
  241. }