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

150 lines 4.6 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 amount of
11 * data requested 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 provided callable is buffered internally until drained using
14 * the read() function of the PumpStream. The provided callable MUST return
15 * false when there is no more data to read.
16 */
17 final class PumpStream implements \YoastSEO_Vendor\Psr\Http\Message\StreamInterface
18 {
19 /** @var callable(int): (string|false|null)|null */
20 private $source;
21 /** @var int|null */
22 private $size;
23 /** @var int */
24 private $tellPos = 0;
25 /** @var array */
26 private $metadata;
27 /** @var BufferStream */
28 private $buffer;
29 /**
30 * @param callable(int): (string|false|null) $source Source of the stream data. The callable MAY
31 * accept an integer argument used to control the
32 * amount of data to return. The callable MUST
33 * return a string when called, or false|null on error
34 * or EOF.
35 * @param array{size?: int, metadata?: array} $options Stream options:
36 * - metadata: Hash of metadata to use with stream.
37 * - size: Size of the stream, if known.
38 */
39 public function __construct(callable $source, array $options = [])
40 {
41 $this->source = $source;
42 $this->size = $options['size'] ?? null;
43 $this->metadata = $options['metadata'] ?? [];
44 $this->buffer = new \YoastSEO_Vendor\GuzzleHttp\Psr7\BufferStream();
45 }
46 public function __toString() : string
47 {
48 try {
49 return \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::copyToString($this);
50 } catch (\Throwable $e) {
51 if (\PHP_VERSION_ID >= 70400) {
52 throw $e;
53 }
54 \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR);
55 return '';
56 }
57 }
58 public function close() : void
59 {
60 $this->detach();
61 }
62 public function detach()
63 {
64 $this->tellPos = 0;
65 $this->source = null;
66 return null;
67 }
68 public function getSize() : ?int
69 {
70 return $this->size;
71 }
72 public function tell() : int
73 {
74 return $this->tellPos;
75 }
76 public function eof() : bool
77 {
78 return $this->source === null;
79 }
80 public function isSeekable() : bool
81 {
82 return \false;
83 }
84 public function rewind() : void
85 {
86 $this->seek(0);
87 }
88 public function seek($offset, $whence = \SEEK_SET) : void
89 {
90 throw new \RuntimeException('Cannot seek a PumpStream');
91 }
92 public function isWritable() : bool
93 {
94 return \false;
95 }
96 public function write($string) : int
97 {
98 throw new \RuntimeException('Cannot write to a PumpStream');
99 }
100 public function isReadable() : bool
101 {
102 return \true;
103 }
104 public function read($length) : string
105 {
106 $data = $this->buffer->read($length);
107 $readLen = \strlen($data);
108 $this->tellPos += $readLen;
109 $remaining = $length - $readLen;
110 if ($remaining) {
111 $this->pump($remaining);
112 $data .= $this->buffer->read($remaining);
113 $this->tellPos += \strlen($data) - $readLen;
114 }
115 return $data;
116 }
117 public function getContents() : string
118 {
119 $result = '';
120 while (!$this->eof()) {
121 $result .= $this->read(1000000);
122 }
123 return $result;
124 }
125 /**
126 * @return mixed
127 */
128 public function getMetadata($key = null)
129 {
130 if (!$key) {
131 return $this->metadata;
132 }
133 return $this->metadata[$key] ?? null;
134 }
135 private function pump(int $length) : void
136 {
137 if ($this->source !== null) {
138 do {
139 $data = ($this->source)($length);
140 if ($data === \false || $data === null) {
141 $this->source = null;
142 return;
143 }
144 $this->buffer->write($data);
145 $length -= \strlen($data);
146 } while ($length > 0);
147 }
148 }
149 }
150