GuzzleHttpPsr7Stream

PHP stream implementation.

Defined (1)

The class is defined in the following location(s).

/lib/Azure/GuzzleHttp/Psr7/Stream.php  
  1. class Stream implements StreamInterface 
  2. private $stream; 
  3. private $size; 
  4. private $seekable; 
  5. private $readable; 
  6. private $writable; 
  7. private $uri; 
  8. private $customMetadata; 
  9.  
  10. /** @var array Hash of readable and writable stream types */ 
  11. private static $readWriteHash = [ 
  12. 'read' => [ 
  13. 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true,  
  14. 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true,  
  15. 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true,  
  16. 'x+t' => true, 'c+t' => true, 'a+' => true 
  17. ],  
  18. 'write' => [ 
  19. 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true,  
  20. 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true,  
  21. 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true,  
  22. 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true 
  23. ]; 
  24.  
  25. /** 
  26. * This constructor accepts an associative array of options. 
  27. * - size: (int) If a read stream would otherwise have an indeterminate 
  28. * size, but the size is known due to foreknowledge, then you can 
  29. * provide that size, in bytes. 
  30. * - metadata: (array) Any additional metadata to return when the metadata 
  31. * of the stream is accessed. 
  32. * @param resource $stream Stream resource to wrap. 
  33. * @param array $options Associative array of options. 
  34. * @throws \InvalidArgumentException if the stream is not a stream resource 
  35. */ 
  36. public function __construct($stream, $options = []) 
  37. if (!is_resource($stream)) { 
  38. throw new \InvalidArgumentException('Stream must be a resource'); 
  39.  
  40. if (isset($options['size'])) { 
  41. $this->size = $options['size']; 
  42.  
  43. $this->customMetadata = isset($options['metadata']) 
  44. ? $options['metadata'] 
  45. : []; 
  46.  
  47. $this->stream = $stream; 
  48. $meta = stream_get_meta_data($this->stream); 
  49. $this->seekable = $meta['seekable']; 
  50. $this->readable = isset(self::$readWriteHash['read'][$meta['mode']]); 
  51. $this->writable = isset(self::$readWriteHash['write'][$meta['mode']]); 
  52. $this->uri = $this->getMetadata('uri'); 
  53.  
  54. public function __get($name) 
  55. if ($name == 'stream') { 
  56. throw new \RuntimeException('The stream is detached'); 
  57.  
  58. throw new \BadMethodCallException('No value for ' . $name); 
  59.  
  60. /** 
  61. * Closes the stream when the destructed 
  62. */ 
  63. public function __destruct() 
  64. $this->close(); 
  65.  
  66. public function __toString() 
  67. try { 
  68. $this->seek(0); 
  69. return (string) stream_get_contents($this->stream); 
  70. } catch (\Exception $e) { 
  71. return ''; 
  72.  
  73. public function getContents() 
  74. $contents = stream_get_contents($this->stream); 
  75.  
  76. if ($contents === false) { 
  77. throw new \RuntimeException('Unable to read stream contents'); 
  78.  
  79. return $contents; 
  80.  
  81. public function close() 
  82. if (isset($this->stream)) { 
  83. if (is_resource($this->stream)) { 
  84. fclose($this->stream); 
  85. $this->detach(); 
  86.  
  87. public function detach() 
  88. if (!isset($this->stream)) { 
  89. return null; 
  90.  
  91. $result = $this->stream; 
  92. unset($this->stream); 
  93. $this->size = $this->uri = null; 
  94. $this->readable = $this->writable = $this->seekable = false; 
  95.  
  96. return $result; 
  97.  
  98. public function getSize() 
  99. if ($this->size !== null) { 
  100. return $this->size; 
  101.  
  102. if (!isset($this->stream)) { 
  103. return null; 
  104.  
  105. // Clear the stat cache if the stream has a URI 
  106. if ($this->uri) { 
  107. clearstatcache(true, $this->uri); 
  108.  
  109. $stats = fstat($this->stream); 
  110. if (isset($stats['size'])) { 
  111. $this->size = $stats['size']; 
  112. return $this->size; 
  113.  
  114. return null; 
  115.  
  116. public function isReadable() 
  117. return $this->readable; 
  118.  
  119. public function isWritable() 
  120. return $this->writable; 
  121.  
  122. public function isSeekable() 
  123. return $this->seekable; 
  124.  
  125. public function eof() 
  126. return !$this->stream || feof($this->stream); 
  127.  
  128. public function tell() 
  129. $result = ftell($this->stream); 
  130.  
  131. if ($result === false) { 
  132. throw new \RuntimeException('Unable to determine stream position'); 
  133.  
  134. return $result; 
  135.  
  136. public function rewind() 
  137. $this->seek(0); 
  138.  
  139. public function seek($offset, $whence = SEEK_SET) 
  140. if (!$this->seekable) { 
  141. throw new \RuntimeException('Stream is not seekable'); 
  142. } elseif (fseek($this->stream, $offset, $whence) === -1) { 
  143. throw new \RuntimeException('Unable to seek to stream position ' 
  144. . $offset . ' with whence ' . var_export($whence, true)); 
  145.  
  146. public function read($length) 
  147. if (!$this->readable) { 
  148. throw new \RuntimeException('Cannot read from non-readable stream'); 
  149.  
  150. return fread($this->stream, $length); 
  151.  
  152. public function write($string) 
  153. if (!$this->writable) { 
  154. throw new \RuntimeException('Cannot write to a non-writable stream'); 
  155.  
  156. // We can't know the size after writing anything 
  157. $this->size = null; 
  158. $result = fwrite($this->stream, $string); 
  159.  
  160. if ($result === false) { 
  161. throw new \RuntimeException('Unable to write to stream'); 
  162.  
  163. return $result; 
  164.  
  165. public function getMetadata($key = null) 
  166. if (!isset($this->stream)) { 
  167. return $key ? null : []; 
  168. } elseif (!$key) { 
  169. return $this->customMetadata + stream_get_meta_data($this->stream); 
  170. } elseif (isset($this->customMetadata[$key])) { 
  171. return $this->customMetadata[$key]; 
  172.  
  173. $meta = stream_get_meta_data($this->stream); 
  174.  
  175. return isset($meta[$key]) ? $meta[$key] : null;