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