PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / psr7 / src / CachingStream.php

CachingStream.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at vendor_prefixed/guzzlehttp/psr7/src/CachingStream.php

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