Stream.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 (!$stream->feof()) {
  88. hash_update($ctx, $stream->read(8192));
  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. clearstatcache(true, $this->cache['uri']);
  141. $stats = fstat($this->stream);
  142. if (isset($stats['size'])) {
  143. $this->size = $stats['size'];
  144. return $this->size;
  145. } elseif ($this->cache[self::IS_READABLE] && $this->cache[self::SEEKABLE]) {
  146. // Only get the size based on the content if the the stream is readable and seekable
  147. $pos = $this->ftell();
  148. $this->size = strlen((string) $this);
  149. $this->seek($pos);
  150. return $this->size;
  151. }
  152. return false;
  153. }
  154. public function isReadable()
  155. {
  156. return $this->cache[self::IS_READABLE];
  157. }
  158. public function isRepeatable()
  159. {
  160. return $this->cache[self::IS_READABLE] && $this->cache[self::SEEKABLE];
  161. }
  162. public function isWritable()
  163. {
  164. return $this->cache[self::IS_WRITABLE];
  165. }
  166. public function isConsumed()
  167. {
  168. return feof($this->stream);
  169. }
  170. public function feof()
  171. {
  172. return $this->isConsumed();
  173. }
  174. public function isLocal()
  175. {
  176. return $this->cache[self::IS_LOCAL];
  177. }
  178. public function isSeekable()
  179. {
  180. return $this->cache[self::SEEKABLE];
  181. }
  182. public function setSize($size)
  183. {
  184. $this->size = $size;
  185. return $this;
  186. }
  187. public function seek($offset, $whence = SEEK_SET)
  188. {
  189. return $this->cache[self::SEEKABLE] ? fseek($this->stream, $offset, $whence) === 0 : false;
  190. }
  191. public function read($length)
  192. {
  193. return fread($this->stream, $length);
  194. }
  195. public function write($string)
  196. {
  197. // We can't know the size after writing anything
  198. $this->size = null;
  199. return fwrite($this->stream, $string);
  200. }
  201. public function ftell()
  202. {
  203. return ftell($this->stream);
  204. }
  205. public function rewind()
  206. {
  207. return $this->seek(0);
  208. }
  209. public function readLine($maxLength = null)
  210. {
  211. if (!$this->cache[self::IS_READABLE]) {
  212. return false;
  213. } else {
  214. return $maxLength ? fgets($this->getStream(), $maxLength) : fgets($this->getStream());
  215. }
  216. }
  217. public function setCustomData($key, $value)
  218. {
  219. $this->customData[$key] = $value;
  220. return $this;
  221. }
  222. public function getCustomData($key)
  223. {
  224. return isset($this->customData[$key]) ? $this->customData[$key] : null;
  225. }
  226. /**
  227. * Reprocess stream metadata
  228. */
  229. protected function rebuildCache()
  230. {
  231. $this->cache = stream_get_meta_data($this->stream);
  232. $this->cache[self::IS_LOCAL] = stream_is_local($this->stream);
  233. $this->cache[self::IS_READABLE] = isset(self::$readWriteHash['read'][$this->cache['mode']]);
  234. $this->cache[self::IS_WRITABLE] = isset(self::$readWriteHash['write'][$this->cache['mode']]);
  235. }
  236. }