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 / PumpStream.php

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

171 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 * Provides a read only stream that pumps data from a PHP callable.
9 *
10 * When invoking the provided callable, the PumpStream will pass the suggested
11 * number of bytes to read to the callable. The callable can choose to ignore
12 * this value and return fewer or more bytes than requested. Any extra data
13 * returned by the callable is buffered internally until drained using the
14 * read() function of the PumpStream. The callable MUST return false or null
15 * when there is no more data to read.
16 *
17 * Userland callables that declare no parameters are tolerated by PHP, but
18 * length-aware callables remain the recommended formal shape.
19 */
20 final class PumpStream implements \YoastSEO_Vendor\Psr\Http\Message\StreamInterface
21 {
22 /** @var callable|null */
23 private $source;
24 /** @var int|null */
25 private $size;
26 /** @var int */
27 private $tellPos = 0;
28 /** @var array */
29 private $metadata;
30 /** @var BufferStream */
31 private $buffer;
32 /**
33 * @param (callable(): (string|false|null))|(callable(int): (string|false|null)) $source Source of the stream data. The callable receives
34 * the suggested number of bytes to read, may ignore
35 * that value, and may return fewer or more bytes.
36 * Extra bytes are buffered. The callable MUST return
37 * a string when called, or false|null on error or EOF.
38 * Userland callables that declare no parameters are
39 * tolerated by PHP, but length-aware callables remain
40 * the recommended formal shape.
41 * @param array{size?: int, metadata?: array} $options Stream options:
42 * - metadata: Hash of metadata to use with stream.
43 * - size: Size of the stream, if known.
44 */
45 public function __construct(callable $source, array $options = [])
46 {
47 $this->source = $source;
48 $this->size = $options['size'] ?? null;
49 $this->metadata = $options['metadata'] ?? [];
50 $this->buffer = new \YoastSEO_Vendor\GuzzleHttp\Psr7\BufferStream();
51 }
52 public function __toString() : string
53 {
54 try {
55 return \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::copyToString($this);
56 } catch (\Throwable $e) {
57 if (\PHP_VERSION_ID >= 70400) {
58 throw $e;
59 }
60 \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR);
61 return '';
62 }
63 }
64 public function close() : void
65 {
66 $this->detach();
67 }
68 public function detach()
69 {
70 $this->tellPos = 0;
71 $this->source = null;
72 return null;
73 }
74 public function getSize() : ?int
75 {
76 return $this->size;
77 }
78 public function tell() : int
79 {
80 return $this->tellPos;
81 }
82 public function eof() : bool
83 {
84 return $this->source === null;
85 }
86 public function isSeekable() : bool
87 {
88 return \false;
89 }
90 public function rewind() : void
91 {
92 $this->seek(0);
93 }
94 public function seek($offset, $whence = \SEEK_SET) : void
95 {
96 if (!\is_int($offset)) {
97 \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));
98 }
99 if (!\is_int($whence)) {
100 \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));
101 }
102 throw new \RuntimeException('Cannot seek a PumpStream');
103 }
104 public function isWritable() : bool
105 {
106 return \false;
107 }
108 public function write($string) : int
109 {
110 if (!\is_string($string)) {
111 \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));
112 }
113 throw new \RuntimeException('Cannot write to a PumpStream');
114 }
115 public function isReadable() : bool
116 {
117 return \true;
118 }
119 public function read($length) : string
120 {
121 if (!\is_int($length)) {
122 \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));
123 }
124 $data = $this->buffer->read($length);
125 $readLen = \strlen($data);
126 $this->tellPos += $readLen;
127 $remaining = $length - $readLen;
128 if ($remaining) {
129 $this->pump($remaining);
130 $data .= $this->buffer->read($remaining);
131 $this->tellPos += \strlen($data) - $readLen;
132 }
133 return $data;
134 }
135 public function getContents() : string
136 {
137 $result = '';
138 while (!$this->eof()) {
139 $result .= $this->read(1000000);
140 }
141 return $result;
142 }
143 /**
144 * @return mixed
145 */
146 public function getMetadata($key = null)
147 {
148 if ($key !== null && !\is_string($key)) {
149 \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::getMetadata() is deprecated; guzzlehttp/psr7 3.0 requires string|null for $key.', \get_debug_type($key));
150 }
151 if (!$key) {
152 return $this->metadata;
153 }
154 return $this->metadata[$key] ?? null;
155 }
156 private function pump(int $length) : void
157 {
158 if ($this->source !== null) {
159 do {
160 $data = ($this->source)($length);
161 if ($data === \false || $data === null) {
162 $this->source = null;
163 return;
164 }
165 $this->buffer->write($data);
166 $length -= \strlen($data);
167 } while ($length > 0);
168 }
169 }
170 }
171