| 1 |
<?php |
| 2 |
|
| 3 |
namespace YoastSEO_Vendor\GuzzleHttp\Psr7; |
| 4 |
|
| 5 |
use YoastSEO_Vendor\Psr\Http\Message\StreamInterface; |
| 6 |
/** |
| 7 |
* Provides a buffer stream that can be written to to fill a buffer, and read |
| 8 |
* from to remove bytes from the buffer. |
| 9 |
* |
| 10 |
* This stream returns a "hwm" metadata value that tells upstream consumers |
| 11 |
* what the configured high water mark of the stream is, or the maximum |
| 12 |
* preferred size of the buffer. |
| 13 |
* |
| 14 |
* @final |
| 15 |
*/ |
| 16 |
class BufferStream implements \YoastSEO_Vendor\Psr\Http\Message\StreamInterface |
| 17 |
{ |
| 18 |
private $hwm; |
| 19 |
private $buffer = ''; |
| 20 |
/** |
| 21 |
* @param int $hwm High water mark, representing the preferred maximum |
| 22 |
* buffer size. If the size of the buffer exceeds the high |
| 23 |
* water mark, then calls to write will continue to succeed |
| 24 |
* but will return false to inform writers to slow down |
| 25 |
* until the buffer has been drained by reading from it. |
| 26 |
*/ |
| 27 |
public function __construct($hwm = 16384) |
| 28 |
{ |
| 29 |
$this->hwm = $hwm; |
| 30 |
} |
| 31 |
public function __toString() |
| 32 |
{ |
| 33 |
return $this->getContents(); |
| 34 |
} |
| 35 |
public function getContents() |
| 36 |
{ |
| 37 |
$buffer = $this->buffer; |
| 38 |
$this->buffer = ''; |
| 39 |
return $buffer; |
| 40 |
} |
| 41 |
public function close() |
| 42 |
{ |
| 43 |
$this->buffer = ''; |
| 44 |
} |
| 45 |
public function detach() |
| 46 |
{ |
| 47 |
$this->close(); |
| 48 |
return null; |
| 49 |
} |
| 50 |
public function getSize() |
| 51 |
{ |
| 52 |
return \strlen($this->buffer); |
| 53 |
} |
| 54 |
public function isReadable() |
| 55 |
{ |
| 56 |
return \true; |
| 57 |
} |
| 58 |
public function isWritable() |
| 59 |
{ |
| 60 |
return \true; |
| 61 |
} |
| 62 |
public function isSeekable() |
| 63 |
{ |
| 64 |
return \false; |
| 65 |
} |
| 66 |
public function rewind() |
| 67 |
{ |
| 68 |
$this->seek(0); |
| 69 |
} |
| 70 |
public function seek($offset, $whence = \SEEK_SET) |
| 71 |
{ |
| 72 |
throw new \RuntimeException('Cannot seek a BufferStream'); |
| 73 |
} |
| 74 |
public function eof() |
| 75 |
{ |
| 76 |
return \strlen($this->buffer) === 0; |
| 77 |
} |
| 78 |
public function tell() |
| 79 |
{ |
| 80 |
throw new \RuntimeException('Cannot determine the position of a BufferStream'); |
| 81 |
} |
| 82 |
/** |
| 83 |
* Reads data from the buffer. |
| 84 |
*/ |
| 85 |
public function read($length) |
| 86 |
{ |
| 87 |
$currentLength = \strlen($this->buffer); |
| 88 |
if ($length >= $currentLength) { |
| 89 |
// No need to slice the buffer because we don't have enough data. |
| 90 |
$result = $this->buffer; |
| 91 |
$this->buffer = ''; |
| 92 |
} else { |
| 93 |
// Slice up the result to provide a subset of the buffer. |
| 94 |
$result = \substr($this->buffer, 0, $length); |
| 95 |
$this->buffer = \substr($this->buffer, $length); |
| 96 |
} |
| 97 |
return $result; |
| 98 |
} |
| 99 |
/** |
| 100 |
* Writes data to the buffer. |
| 101 |
*/ |
| 102 |
public function write($string) |
| 103 |
{ |
| 104 |
$this->buffer .= $string; |
| 105 |
// TODO: What should happen here? |
| 106 |
if (\strlen($this->buffer) >= $this->hwm) { |
| 107 |
return \false; |
| 108 |
} |
| 109 |
return \strlen($string); |
| 110 |
} |
| 111 |
public function getMetadata($key = null) |
| 112 |
{ |
| 113 |
if ($key == 'hwm') { |
| 114 |
return $this->hwm; |
| 115 |
} |
| 116 |
return $key ? null : []; |
| 117 |
} |
| 118 |
} |
| 119 |
|