PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.4
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 18.4, at vendor_prefixed/guzzlehttp/psr7/src/CachingStream.php

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