PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Psr7 / PumpStream.php

PumpStream.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/GuzzleHttp/Psr7/PumpStream.php

150 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 declare (strict_types=1);
4 namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7;
5
6 use Dudlewebs\WPMCS\s3\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 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 BufferStream();
45 }
46 public function __toString() : string
47 {
48 try {
49 return 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