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

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

236 lines 7.2 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 * PHP stream implementation.
9 */
10 class Stream implements StreamInterface
11 {
12 /**
13 * @see https://www.php.net/manual/en/function.fopen.php
14 * @see https://www.php.net/manual/en/function.gzopen.php
15 */
16 private const READABLE_MODES = '/r|a\\+|ab\\+|w\\+|wb\\+|x\\+|xb\\+|c\\+|cb\\+/';
17 private const WRITABLE_MODES = '/a|w|r\\+|rb\\+|rw|x|c/';
18 /** @var resource */
19 private $stream;
20 /** @var int|null */
21 private $size;
22 /** @var bool */
23 private $seekable;
24 /** @var bool */
25 private $readable;
26 /** @var bool */
27 private $writable;
28 /** @var string|null */
29 private $uri;
30 /** @var mixed[] */
31 private $customMetadata;
32 /**
33 * This constructor accepts an associative array of options.
34 *
35 * - size: (int) If a read stream would otherwise have an indeterminate
36 * size, but the size is known due to foreknowledge, then you can
37 * provide that size, in bytes.
38 * - metadata: (array) Any additional metadata to return when the metadata
39 * of the stream is accessed.
40 *
41 * @param resource $stream Stream resource to wrap.
42 * @param array{size?: int, metadata?: array} $options Associative array of options.
43 *
44 * @throws \InvalidArgumentException if the stream is not a stream resource
45 */
46 public function __construct($stream, array $options = [])
47 {
48 if (!\is_resource($stream)) {
49 throw new \InvalidArgumentException('Stream must be a resource');
50 }
51 if (isset($options['size'])) {
52 $this->size = $options['size'];
53 }
54 $this->customMetadata = $options['metadata'] ?? [];
55 $this->stream = $stream;
56 $meta = \stream_get_meta_data($this->stream);
57 $this->seekable = $meta['seekable'];
58 $this->readable = (bool) \preg_match(self::READABLE_MODES, $meta['mode']);
59 $this->writable = (bool) \preg_match(self::WRITABLE_MODES, $meta['mode']);
60 $this->uri = $this->getMetadata('uri');
61 }
62 /**
63 * Closes the stream when the destructed
64 */
65 public function __destruct()
66 {
67 $this->close();
68 }
69 public function __toString() : string
70 {
71 try {
72 if ($this->isSeekable()) {
73 $this->seek(0);
74 }
75 return $this->getContents();
76 } catch (\Throwable $e) {
77 if (\PHP_VERSION_ID >= 70400) {
78 throw $e;
79 }
80 \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR);
81 return '';
82 }
83 }
84 public function getContents() : string
85 {
86 if (!isset($this->stream)) {
87 throw new \RuntimeException('Stream is detached');
88 }
89 if (!$this->readable) {
90 throw new \RuntimeException('Cannot read from non-readable stream');
91 }
92 return Utils::tryGetContents($this->stream);
93 }
94 public function close() : void
95 {
96 if (isset($this->stream)) {
97 if (\is_resource($this->stream)) {
98 \fclose($this->stream);
99 }
100 $this->detach();
101 }
102 }
103 public function detach()
104 {
105 if (!isset($this->stream)) {
106 return null;
107 }
108 $result = $this->stream;
109 unset($this->stream);
110 $this->size = $this->uri = null;
111 $this->readable = $this->writable = $this->seekable = \false;
112 return $result;
113 }
114 public function getSize() : ?int
115 {
116 if ($this->size !== null) {
117 return $this->size;
118 }
119 if (!isset($this->stream)) {
120 return null;
121 }
122 // Clear the stat cache if the stream has a URI
123 if ($this->uri) {
124 \clearstatcache(\true, $this->uri);
125 }
126 $stats = \fstat($this->stream);
127 if (\is_array($stats) && isset($stats['size'])) {
128 $this->size = $stats['size'];
129 return $this->size;
130 }
131 return null;
132 }
133 public function isReadable() : bool
134 {
135 return $this->readable;
136 }
137 public function isWritable() : bool
138 {
139 return $this->writable;
140 }
141 public function isSeekable() : bool
142 {
143 return $this->seekable;
144 }
145 public function eof() : bool
146 {
147 if (!isset($this->stream)) {
148 throw new \RuntimeException('Stream is detached');
149 }
150 return \feof($this->stream);
151 }
152 public function tell() : int
153 {
154 if (!isset($this->stream)) {
155 throw new \RuntimeException('Stream is detached');
156 }
157 $result = \ftell($this->stream);
158 if ($result === \false) {
159 throw new \RuntimeException('Unable to determine stream position');
160 }
161 return $result;
162 }
163 public function rewind() : void
164 {
165 $this->seek(0);
166 }
167 public function seek($offset, $whence = \SEEK_SET) : void
168 {
169 $whence = (int) $whence;
170 if (!isset($this->stream)) {
171 throw new \RuntimeException('Stream is detached');
172 }
173 if (!$this->seekable) {
174 throw new \RuntimeException('Stream is not seekable');
175 }
176 if (\fseek($this->stream, $offset, $whence) === -1) {
177 throw new \RuntimeException('Unable to seek to stream position ' . $offset . ' with whence ' . \var_export($whence, \true));
178 }
179 }
180 public function read($length) : string
181 {
182 if (!isset($this->stream)) {
183 throw new \RuntimeException('Stream is detached');
184 }
185 if (!$this->readable) {
186 throw new \RuntimeException('Cannot read from non-readable stream');
187 }
188 if ($length < 0) {
189 throw new \RuntimeException('Length parameter cannot be negative');
190 }
191 if (0 === $length) {
192 return '';
193 }
194 try {
195 $string = \fread($this->stream, $length);
196 } catch (\Exception $e) {
197 throw new \RuntimeException('Unable to read from stream', 0, $e);
198 }
199 if (\false === $string) {
200 throw new \RuntimeException('Unable to read from stream');
201 }
202 return $string;
203 }
204 public function write($string) : int
205 {
206 if (!isset($this->stream)) {
207 throw new \RuntimeException('Stream is detached');
208 }
209 if (!$this->writable) {
210 throw new \RuntimeException('Cannot write to a non-writable stream');
211 }
212 // We can't know the size after writing anything
213 $this->size = null;
214 $result = \fwrite($this->stream, $string);
215 if ($result === \false) {
216 throw new \RuntimeException('Unable to write to stream');
217 }
218 return $result;
219 }
220 /**
221 * @return mixed
222 */
223 public function getMetadata($key = null)
224 {
225 if (!isset($this->stream)) {
226 return $key ? null : [];
227 } elseif (!$key) {
228 return $this->customMetadata + \stream_get_meta_data($this->stream);
229 } elseif (isset($this->customMetadata[$key])) {
230 return $this->customMetadata[$key];
231 }
232 $meta = \stream_get_meta_data($this->stream);
233 return $meta[$key] ?? null;
234 }
235 }
236