| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
use WCPOS\Vendor\Psr\Http\Message\StreamInterface; |
| 7 |
/** |
| 8 |
* Provides a read only stream that pumps data from a PHP callable. |
| 9 |
* |
| 10 |
* When invoking the provided callable, the PumpStream will pass the suggested |
| 11 |
* number of bytes to read to the callable. The callable can choose to ignore |
| 12 |
* this value and return fewer or more bytes than requested. Any extra data |
| 13 |
* returned by the callable is buffered internally until drained using the |
| 14 |
* read() function of the PumpStream. The callable MUST return a non-empty |
| 15 |
* string when data is available, or false or null when there is no more data |
| 16 |
* to read. |
| 17 |
* |
| 18 |
* Userland callables that declare no parameters are tolerated by PHP, but |
| 19 |
* length-aware callables remain the recommended formal shape. |
| 20 |
*/ |
| 21 |
final class PumpStream implements StreamInterface |
| 22 |
{ |
| 23 |
use NonSerializableStreamTrait; |
| 24 |
/** @var callable|null */ |
| 25 |
private $source; |
| 26 |
private ?int $size; |
| 27 |
private int $tellPos = 0; |
| 28 |
private array $metadata; |
| 29 |
private BufferStream $buffer; |
| 30 |
/** |
| 31 |
* @param (callable(): (string|false|null))|(callable(int): (string|false|null)) $source Source of the stream data. The callable receives |
| 32 |
* the suggested number of bytes to read, may ignore |
| 33 |
* that value, and may return fewer or more bytes. |
| 34 |
* Extra bytes are buffered. The callable MUST return |
| 35 |
* a non-empty string when producing data, or false|null |
| 36 |
* on error or EOF. Userland callables that declare no |
| 37 |
* parameters are tolerated by PHP, but length-aware |
| 38 |
* callables remain the recommended formal shape. |
| 39 |
* @param array{size?: int, metadata?: array} $options Stream options: |
| 40 |
* - metadata: Hash of metadata to use with stream. |
| 41 |
* - size: Size of the stream, if known. |
| 42 |
*/ |
| 43 |
public function __construct(callable $source, array $options = []) |
| 44 |
{ |
| 45 |
$this->source = $source; |
| 46 |
$this->size = Integers::assertOptionalNonNegativeSize($options['size'] ?? null, 'Stream size'); |
| 47 |
$this->metadata = $options['metadata'] ?? []; |
| 48 |
$this->buffer = new BufferStream(); |
| 49 |
} |
| 50 |
public function __unserialize(array $data) : void |
| 51 |
{ |
| 52 |
$this->source = null; |
| 53 |
$this->size = null; |
| 54 |
$this->tellPos = 0; |
| 55 |
$this->metadata = []; |
| 56 |
$this->buffer = new BufferStream(); |
| 57 |
throw new \LogicException(static::class . ' should never be unserialized'); |
| 58 |
} |
| 59 |
public function __toString() : string |
| 60 |
{ |
| 61 |
return Utils::copyToString($this); |
| 62 |
} |
| 63 |
public function close() : void |
| 64 |
{ |
| 65 |
$this->detach(); |
| 66 |
} |
| 67 |
public function detach() |
| 68 |
{ |
| 69 |
$this->tellPos = 0; |
| 70 |
$this->source = null; |
| 71 |
$this->buffer->close(); |
| 72 |
return null; |
| 73 |
} |
| 74 |
public function getSize() : ?int |
| 75 |
{ |
| 76 |
return $this->size; |
| 77 |
} |
| 78 |
public function tell() : int |
| 79 |
{ |
| 80 |
return $this->tellPos; |
| 81 |
} |
| 82 |
public function eof() : bool |
| 83 |
{ |
| 84 |
return $this->source === null; |
| 85 |
} |
| 86 |
public function isSeekable() : bool |
| 87 |
{ |
| 88 |
return \false; |
| 89 |
} |
| 90 |
public function rewind() : void |
| 91 |
{ |
| 92 |
$this->seek(0); |
| 93 |
} |
| 94 |
public function seek(int $offset, int $whence = \SEEK_SET) : void |
| 95 |
{ |
| 96 |
throw new \RuntimeException('Cannot seek a PumpStream'); |
| 97 |
} |
| 98 |
public function isWritable() : bool |
| 99 |
{ |
| 100 |
return \false; |
| 101 |
} |
| 102 |
public function write(string $string) : int |
| 103 |
{ |
| 104 |
throw new \RuntimeException('Cannot write to a PumpStream'); |
| 105 |
} |
| 106 |
public function isReadable() : bool |
| 107 |
{ |
| 108 |
return \true; |
| 109 |
} |
| 110 |
public function read(int $length) : string |
| 111 |
{ |
| 112 |
if ($length < 0) { |
| 113 |
throw new \RuntimeException('Length parameter cannot be negative'); |
| 114 |
} |
| 115 |
$bufferLength = $this->buffer->getSize() ?? 0; |
| 116 |
if ($length > $bufferLength) { |
| 117 |
$this->pump($length - $bufferLength); |
| 118 |
} |
| 119 |
$data = $this->buffer->read($length); |
| 120 |
$this->tellPos = Integers::add($this->tellPos, \strlen($data)); |
| 121 |
return $data; |
| 122 |
} |
| 123 |
public function getContents() : string |
| 124 |
{ |
| 125 |
return Utils::copyToString($this); |
| 126 |
} |
| 127 |
/** |
| 128 |
* @return mixed |
| 129 |
*/ |
| 130 |
public function getMetadata(?string $key = null) |
| 131 |
{ |
| 132 |
if ($key === null) { |
| 133 |
return $this->metadata; |
| 134 |
} |
| 135 |
return $this->metadata[$key] ?? null; |
| 136 |
} |
| 137 |
private function pump(int $length) : void |
| 138 |
{ |
| 139 |
if ($this->source !== null) { |
| 140 |
do { |
| 141 |
/** @var string|false|null $data */ |
| 142 |
$data = ($this->source)($length); |
| 143 |
if ($data === \false || $data === null) { |
| 144 |
$this->source = null; |
| 145 |
return; |
| 146 |
} |
| 147 |
if ($data === '') { |
| 148 |
throw new \RuntimeException('PumpStream source returned an empty string'); |
| 149 |
} |
| 150 |
$this->buffer->write($data); |
| 151 |
$length -= \strlen($data); |
| 152 |
} while ($length > 0); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|