Debian Codegraph @

vendor/guzzlehttp/psr7/src/CachingStream.php

raw
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 /** @var bool */
29 private $detached = false;
30
31 /**
32 * We will treat the buffer object as the body of the stream
33 *
34 * @param StreamInterface $stream Stream to cache. The cursor is assumed to be at the beginning of the stream.
35 * @param StreamInterface $target Optionally specify where data is cached
36 */
37 public function __construct(
38 StreamInterface $stream,
39 ?StreamInterface $target = null
40 ) {
41 $this->remoteStream = $stream;
42 $this->stream = $target ?: new Stream(Utils::tryFopen('php://temp', 'r+'));
43 }
44
45 public function getSize(): ?int
46 {
47 if ($this->detached) {
48 return null;
49 }
50
51 $remoteSize = $this->remoteStream->getSize();
52
53 if (null === $remoteSize) {
54 return null;
55 }
56
57 return max($this->stream->getSize(), $remoteSize);
58 }
59
60 public function rewind(): void
61 {
62 $this->seek(0);
63 }
64
65 public function seek($offset, $whence = SEEK_SET): void
66 {
67 if (!\is_int($offset)) {
68 \trigger_deprecation(
69 'guzzlehttp/psr7',
70 '2.11',
71 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $offset.',
72 \get_debug_type($offset)
73 );
74 }
75
76 if (!\is_int($whence)) {
77 \trigger_deprecation(
78 'guzzlehttp/psr7',
79 '2.11',
80 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $whence.',
81 \get_debug_type($whence)
82 );
83 }
84
85 if ($whence === SEEK_SET) {
86 $byte = $offset;
87 } elseif ($whence === SEEK_CUR) {
88 $byte = $offset + $this->tell();
89 } elseif ($whence === SEEK_END) {
90 $size = $this->remoteStream->getSize();
91 if ($size === null) {
92 $size = $this->cacheEntireStream();
93 }
94 $byte = $size + $offset;
95 } else {
96 throw new \InvalidArgumentException('Invalid whence');
97 }
98
99 $diff = $byte - $this->stream->getSize();
100
101 if ($diff > 0) {
102 // Read the remoteStream until we have read in at least the amount
103 // of bytes requested, or we reach the end of the file.
104 while ($diff > 0 && !$this->remoteStream->eof()) {
105 $previousSize = $this->stream->getSize();
106 $previousSkipReadBytes = $this->skipReadBytes;
107 $data = $this->read($diff);
108 $currentSize = $this->stream->getSize();
109
110 if ($data === '' && $currentSize === $previousSize && $this->skipReadBytes === $previousSkipReadBytes) {
111 break;
112 }
113
114 $diff = $byte - $currentSize;
115 }
116 } else {
117 // We can just do a normal seek since we've already seen this byte.
118 $this->stream->seek($byte);
119 }
120 }
121
122 public function read($length): string
123 {
124 if (!\is_int($length)) {
125 \trigger_deprecation(
126 'guzzlehttp/psr7',
127 '2.11',
128 'Passing %s to StreamInterface::read() is deprecated; guzzlehttp/psr7 3.0 requires int for $length.',
129 \get_debug_type($length)
130 );
131 }
132
133 // Perform a regular read on any previously read data from the buffer
134 $data = $this->stream->read($length);
135 $remaining = $length - strlen($data);
136
137 // More data was requested so read from the remote stream
138 if ($remaining) {
139 // If data was written to the buffer in a position that would have
140 // been filled from the remote stream, then we must skip bytes on
141 // the remote stream to emulate overwriting bytes from that
142 // position. This mimics the behavior of other PHP stream wrappers.
143 $remoteData = $this->remoteStream->read(
144 $remaining + $this->skipReadBytes
145 );
146
147 if ($this->skipReadBytes) {
148 $len = strlen($remoteData);
149 $remoteData = substr($remoteData, $this->skipReadBytes);
150 $this->skipReadBytes = max(0, $this->skipReadBytes - $len);
151 }
152
153 $data .= $remoteData;
154
155 // A short cache write would silently corrupt later replays, so fail loudly.
156 if ($this->stream->write($remoteData) !== strlen($remoteData)) {
157 throw new \RuntimeException('Unable to cache the entire read from the remote stream');
158 }
159 }
160
161 return $data;
162 }
163
164 public function write($string): int
165 {
166 if (!\is_string($string)) {
167 \trigger_deprecation(
168 'guzzlehttp/psr7',
169 '2.11',
170 'Passing %s to StreamInterface::write() is deprecated; guzzlehttp/psr7 3.0 requires string for $string.',
171 \get_debug_type($string)
172 );
173 }
174
175 // When appending to the end of the currently read stream, you'll want
176 // to skip bytes from being read from the remote stream to emulate
177 // other stream wrappers. Basically replacing bytes of data of a fixed
178 // length.
179 $overflow = (strlen($string) + $this->tell()) - $this->remoteStream->tell();
180 if ($overflow > 0) {
181 $this->skipReadBytes += $overflow;
182 }
183
184 return $this->stream->write($string);
185 }
186
187 public function eof(): bool
188 {
189 return $this->stream->eof() && $this->remoteStream->eof();
190 }
191
192 public function detach()
193 {
194 if ($this->detached) {
195 return null;
196 }
197
198 $position = $this->tell();
199
200 $this->cacheEntireStream();
201 $this->stream->seek($position);
202
203 $resource = $this->stream->detach();
204 $this->detached = true;
205
206 return $resource;
207 }
208
209 /**
210 * Close both the remote stream and buffer stream
211 */
212 public function close(): void
213 {
214 $this->remoteStream->close();
215 $this->stream->close();
216 $this->detached = true;
217 }
218
219 private function cacheEntireStream(): int
220 {
221 $target = new FnStream(['write' => 'strlen']);
222 Utils::copyToStream($this, $target);
223
224 return $this->tell();
225 }
226 }