PluginProbe
Media Cloud Sync / 1.0.1
Media Cloud Sync v1.0.1
1.4.2 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 All 36 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Psr7 / Stream.php

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

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