| [01a620f] | 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Psr7;
|
|---|
| 3 |
|
|---|
| 4 | use Psr\Http\Message\StreamInterface;
|
|---|
| 5 |
|
|---|
| 6 | /**
|
|---|
| 7 | * Stream decorator that can cache previously read bytes from a sequentially
|
|---|
| 8 | * read stream.
|
|---|
| 9 | */
|
|---|
| 10 | class CachingStream implements StreamInterface
|
|---|
| 11 | {
|
|---|
| 12 | use StreamDecoratorTrait;
|
|---|
| 13 |
|
|---|
| 14 | /** @var StreamInterface Stream being wrapped */
|
|---|
| 15 | private $remoteStream;
|
|---|
| 16 |
|
|---|
| 17 | /** @var int Number of bytes to skip reading due to a write on the buffer */
|
|---|
| 18 | private $skipReadBytes = 0;
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * We will treat the buffer object as the body of the stream
|
|---|
| 22 | *
|
|---|
| 23 | * @param StreamInterface $stream Stream to cache
|
|---|
| 24 | * @param StreamInterface $target Optionally specify where data is cached
|
|---|
| 25 | */
|
|---|
| 26 | public function __construct(
|
|---|
| 27 | StreamInterface $stream,
|
|---|
| 28 | StreamInterface $target = null
|
|---|
| 29 | ) {
|
|---|
| 30 | $this->remoteStream = $stream;
|
|---|
| 31 | $this->stream = $target ?: new Stream(fopen('php://temp', 'r+'));
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | public function getSize()
|
|---|
| 35 | {
|
|---|
| 36 | return max($this->stream->getSize(), $this->remoteStream->getSize());
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | public function rewind()
|
|---|
| 40 | {
|
|---|
| 41 | $this->seek(0);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | public function seek($offset, $whence = SEEK_SET)
|
|---|
| 45 | {
|
|---|
| 46 | if ($whence == SEEK_SET) {
|
|---|
| 47 | $byte = $offset;
|
|---|
| 48 | } elseif ($whence == SEEK_CUR) {
|
|---|
| 49 | $byte = $offset + $this->tell();
|
|---|
| 50 | } elseif ($whence == SEEK_END) {
|
|---|
| 51 | $size = $this->remoteStream->getSize();
|
|---|
| 52 | if ($size === null) {
|
|---|
| 53 | $size = $this->cacheEntireStream();
|
|---|
| 54 | }
|
|---|
| 55 | // Because 0 is the first byte, we seek to size - 1.
|
|---|
| 56 | $byte = $size - 1 - $offset;
|
|---|
| 57 | } else {
|
|---|
| 58 | throw new \InvalidArgumentException('Invalid whence');
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | $diff = $byte - $this->stream->getSize();
|
|---|
| 62 |
|
|---|
| 63 | if ($diff > 0) {
|
|---|
| 64 | // If the seek byte is greater the number of read bytes, then read
|
|---|
| 65 | // the difference of bytes to cache the bytes and inherently seek.
|
|---|
| 66 | $this->read($diff);
|
|---|
| 67 | } else {
|
|---|
| 68 | // We can just do a normal seek since we've already seen this byte.
|
|---|
| 69 | $this->stream->seek($byte);
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | public function read($length)
|
|---|
| 74 | {
|
|---|
| 75 | // Perform a regular read on any previously read data from the buffer
|
|---|
| 76 | $data = $this->stream->read($length);
|
|---|
| 77 | $remaining = $length - strlen($data);
|
|---|
| 78 |
|
|---|
| 79 | // More data was requested so read from the remote stream
|
|---|
| 80 | if ($remaining) {
|
|---|
| 81 | // If data was written to the buffer in a position that would have
|
|---|
| 82 | // been filled from the remote stream, then we must skip bytes on
|
|---|
| 83 | // the remote stream to emulate overwriting bytes from that
|
|---|
| 84 | // position. This mimics the behavior of other PHP stream wrappers.
|
|---|
| 85 | $remoteData = $this->remoteStream->read(
|
|---|
| 86 | $remaining + $this->skipReadBytes
|
|---|
| 87 | );
|
|---|
| 88 |
|
|---|
| 89 | if ($this->skipReadBytes) {
|
|---|
| 90 | $len = strlen($remoteData);
|
|---|
| 91 | $remoteData = substr($remoteData, $this->skipReadBytes);
|
|---|
| 92 | $this->skipReadBytes = max(0, $this->skipReadBytes - $len);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | $data .= $remoteData;
|
|---|
| 96 | $this->stream->write($remoteData);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | return $data;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | public function write($string)
|
|---|
| 103 | {
|
|---|
| 104 | // When appending to the end of the currently read stream, you'll want
|
|---|
| 105 | // to skip bytes from being read from the remote stream to emulate
|
|---|
| 106 | // other stream wrappers. Basically replacing bytes of data of a fixed
|
|---|
| 107 | // length.
|
|---|
| 108 | $overflow = (strlen($string) + $this->tell()) - $this->remoteStream->tell();
|
|---|
| 109 | if ($overflow > 0) {
|
|---|
| 110 | $this->skipReadBytes += $overflow;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | return $this->stream->write($string);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | public function eof()
|
|---|
| 117 | {
|
|---|
| 118 | return $this->stream->eof() && $this->remoteStream->eof();
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /**
|
|---|
| 122 | * Close both the remote stream and buffer stream
|
|---|
| 123 | */
|
|---|
| 124 | public function close()
|
|---|
| 125 | {
|
|---|
| 126 | $this->remoteStream->close() && $this->stream->close();
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | private function cacheEntireStream()
|
|---|
| 130 | {
|
|---|
| 131 | $target = new FnStream(['write' => 'strlen']);
|
|---|
| 132 | copy_to_stream($this, $target);
|
|---|
| 133 |
|
|---|
| 134 | return $this->tell();
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|