| 1 |
<?php |
| 2 |
|
| 3 |
namespace YoastSEO_Vendor\GuzzleHttp\Psr7; |
| 4 |
|
| 5 |
use YoastSEO_Vendor\Psr\Http\Message\StreamInterface; |
| 6 |
/** |
| 7 |
* Provides a read only stream that pumps data from a PHP callable. |
| 8 |
* |
| 9 |
* When invoking the provided callable, the PumpStream will pass the amount of |
| 10 |
* data requested to read to the callable. The callable can choose to ignore |
| 11 |
* this value and return fewer or more bytes than requested. Any extra data |
| 12 |
* returned by the provided callable is buffered internally until drained using |
| 13 |
* the read() function of the PumpStream. The provided callable MUST return |
| 14 |
* false when there is no more data to read. |
| 15 |
* |
| 16 |
* @final |
| 17 |
*/ |
| 18 |
class PumpStream implements \YoastSEO_Vendor\Psr\Http\Message\StreamInterface |
| 19 |
{ |
| 20 |
/** @var callable */ |
| 21 |
private $source; |
| 22 |
/** @var int */ |
| 23 |
private $size; |
| 24 |
/** @var int */ |
| 25 |
private $tellPos = 0; |
| 26 |
/** @var array */ |
| 27 |
private $metadata; |
| 28 |
/** @var BufferStream */ |
| 29 |
private $buffer; |
| 30 |
/** |
| 31 |
* @param callable $source Source of the stream data. The callable MAY |
| 32 |
* accept an integer argument used to control the |
| 33 |
* amount of data to return. The callable MUST |
| 34 |
* return a string when called, or false on error |
| 35 |
* or EOF. |
| 36 |
* @param array $options Stream options: |
| 37 |
* - metadata: Hash of metadata to use with stream. |
| 38 |
* - size: Size of the stream, if known. |
| 39 |
*/ |
| 40 |
public function __construct(callable $source, array $options = []) |
| 41 |
{ |
| 42 |
$this->source = $source; |
| 43 |
$this->size = isset($options['size']) ? $options['size'] : null; |
| 44 |
$this->metadata = isset($options['metadata']) ? $options['metadata'] : []; |
| 45 |
$this->buffer = new \YoastSEO_Vendor\GuzzleHttp\Psr7\BufferStream(); |
| 46 |
} |
| 47 |
public function __toString() |
| 48 |
{ |
| 49 |
try { |
| 50 |
return \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::copyToString($this); |
| 51 |
} catch (\Exception $e) { |
| 52 |
return ''; |
| 53 |
} |
| 54 |
} |
| 55 |
public function close() |
| 56 |
{ |
| 57 |
$this->detach(); |
| 58 |
} |
| 59 |
public function detach() |
| 60 |
{ |
| 61 |
$this->tellPos = \false; |
| 62 |
$this->source = null; |
| 63 |
return null; |
| 64 |
} |
| 65 |
public function getSize() |
| 66 |
{ |
| 67 |
return $this->size; |
| 68 |
} |
| 69 |
public function tell() |
| 70 |
{ |
| 71 |
return $this->tellPos; |
| 72 |
} |
| 73 |
public function eof() |
| 74 |
{ |
| 75 |
return !$this->source; |
| 76 |
} |
| 77 |
public function isSeekable() |
| 78 |
{ |
| 79 |
return \false; |
| 80 |
} |
| 81 |
public function rewind() |
| 82 |
{ |
| 83 |
$this->seek(0); |
| 84 |
} |
| 85 |
public function seek($offset, $whence = \SEEK_SET) |
| 86 |
{ |
| 87 |
throw new \RuntimeException('Cannot seek a PumpStream'); |
| 88 |
} |
| 89 |
public function isWritable() |
| 90 |
{ |
| 91 |
return \false; |
| 92 |
} |
| 93 |
public function write($string) |
| 94 |
{ |
| 95 |
throw new \RuntimeException('Cannot write to a PumpStream'); |
| 96 |
} |
| 97 |
public function isReadable() |
| 98 |
{ |
| 99 |
return \true; |
| 100 |
} |
| 101 |
public function read($length) |
| 102 |
{ |
| 103 |
$data = $this->buffer->read($length); |
| 104 |
$readLen = \strlen($data); |
| 105 |
$this->tellPos += $readLen; |
| 106 |
$remaining = $length - $readLen; |
| 107 |
if ($remaining) { |
| 108 |
$this->pump($remaining); |
| 109 |
$data .= $this->buffer->read($remaining); |
| 110 |
$this->tellPos += \strlen($data) - $readLen; |
| 111 |
} |
| 112 |
return $data; |
| 113 |
} |
| 114 |
public function getContents() |
| 115 |
{ |
| 116 |
$result = ''; |
| 117 |
while (!$this->eof()) { |
| 118 |
$result .= $this->read(1000000); |
| 119 |
} |
| 120 |
return $result; |
| 121 |
} |
| 122 |
public function getMetadata($key = null) |
| 123 |
{ |
| 124 |
if (!$key) { |
| 125 |
return $this->metadata; |
| 126 |
} |
| 127 |
return isset($this->metadata[$key]) ? $this->metadata[$key] : null; |
| 128 |
} |
| 129 |
private function pump($length) |
| 130 |
{ |
| 131 |
if ($this->source) { |
| 132 |
do { |
| 133 |
$data = \call_user_func($this->source, $length); |
| 134 |
if ($data === \false || $data === null) { |
| 135 |
$this->source = null; |
| 136 |
return; |
| 137 |
} |
| 138 |
$this->buffer->write($data); |
| 139 |
$length -= \strlen($data); |
| 140 |
} while ($length > 0); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|