GuzzleHttpPsr7PumpStream

Provides a read only stream that pumps data from a PHP callable.

Defined (1)

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

/lib/Azure/GuzzleHttp/Psr7/PumpStream.php  
  1. class PumpStream implements StreamInterface 
  2. /** @var callable */ 
  3. private $source; 
  4.  
  5. /** @var int */ 
  6. private $size; 
  7.  
  8. /** @var int */ 
  9. private $tellPos = 0; 
  10.  
  11. /** @var array */ 
  12. private $metadata; 
  13.  
  14. /** @var BufferStream */ 
  15. private $buffer; 
  16.  
  17. /** 
  18. * @param callable $source Source of the stream data. The callable MAY 
  19. * accept an integer argument used to control the 
  20. * amount of data to return. The callable MUST 
  21. * return a string when called, or false on error 
  22. * or EOF. 
  23. * @param array $options Stream options: 
  24. * - metadata: Hash of metadata to use with stream. 
  25. * - size: Size of the stream, if known. 
  26. */ 
  27. public function __construct(callable $source, array $options = []) 
  28. $this->source = $source; 
  29. $this->size = isset($options['size']) ? $options['size'] : null; 
  30. $this->metadata = isset($options['metadata']) ? $options['metadata'] : []; 
  31. $this->buffer = new BufferStream(); 
  32.  
  33. public function __toString() 
  34. try { 
  35. return copy_to_string($this); 
  36. } catch (\Exception $e) { 
  37. return ''; 
  38.  
  39. public function close() 
  40. $this->detach(); 
  41.  
  42. public function detach() 
  43. $this->tellPos = false; 
  44. $this->source = null; 
  45.  
  46. public function getSize() 
  47. return $this->size; 
  48.  
  49. public function tell() 
  50. return $this->tellPos; 
  51.  
  52. public function eof() 
  53. return !$this->source; 
  54.  
  55. public function isSeekable() 
  56. return false; 
  57.  
  58. public function rewind() 
  59. $this->seek(0); 
  60.  
  61. public function seek($offset, $whence = SEEK_SET) 
  62. throw new \RuntimeException('Cannot seek a PumpStream'); 
  63.  
  64. public function isWritable() 
  65. return false; 
  66.  
  67. public function write($string) 
  68. throw new \RuntimeException('Cannot write to a PumpStream'); 
  69.  
  70. public function isReadable() 
  71. return true; 
  72.  
  73. public function read($length) 
  74. $data = $this->buffer->read($length); 
  75. $readLen = strlen($data); 
  76. $this->tellPos += $readLen; 
  77. $remaining = $length - $readLen; 
  78.  
  79. if ($remaining) { 
  80. $this->pump($remaining); 
  81. $data .= $this->buffer->read($remaining); 
  82. $this->tellPos += strlen($data) - $readLen; 
  83.  
  84. return $data; 
  85.  
  86. public function getContents() 
  87. $result = ''; 
  88. while (!$this->eof()) { 
  89. $result .= $this->read(1000000); 
  90.  
  91. return $result; 
  92.  
  93. public function getMetadata($key = null) 
  94. if (!$key) { 
  95. return $this->metadata; 
  96.  
  97. return isset($this->metadata[$key]) ? $this->metadata[$key] : null; 
  98.  
  99. private function pump($length) 
  100. if ($this->source) { 
  101. do { 
  102. $data = call_user_func($this->source, $length); 
  103. if ($data === false || $data === null) { 
  104. $this->source = null; 
  105. return; 
  106. $this->buffer->write($data); 
  107. $length -= strlen($data); 
  108. } while ($length > 0);