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.2.3 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / ecommerce / PaymentGateways / Paypal / vendor / guzzlehttp / psr7 / src / Stream.php
tutor / ecommerce / PaymentGateways / Paypal / vendor / guzzlehttp / psr7 / src Last commit date
Exception 1 year ago AppendStream.php 2 months ago BufferStream.php 2 months ago CachingStream.php 2 months ago DroppingStream.php 2 months ago FnStream.php 2 months ago Header.php 2 months ago HttpFactory.php 1 year ago InflateStream.php 1 year ago LazyOpenStream.php 1 year ago LimitStream.php 2 months ago Message.php 2 months ago MessageTrait.php 2 months ago MimeType.php 2 months ago MultipartStream.php 2 months ago NoSeekStream.php 2 months ago PumpStream.php 2 months ago Query.php 1 year ago Request.php 2 months ago Response.php 2 months ago Rfc3986.php 2 months ago Rfc7230.php 2 months ago ServerRequest.php 2 months ago Stream.php 2 months ago StreamDecoratorTrait.php 2 months ago StreamWrapper.php 2 months ago UploadedFile.php 2 months ago Uri.php 2 months ago UriComparator.php 2 months ago UriNormalizer.php 2 months ago UriResolver.php 2 months ago Utils.php 2 months ago
Stream.php
329 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace GuzzleHttp\Psr7;
6
7 use Psr\Http\Message\StreamInterface;
8
9 /**
10 * PHP stream implementation.
11 */
12 class Stream implements StreamInterface
13 {
14 /**
15 * @see https://www.php.net/manual/en/function.fopen.php
16 * @see https://www.php.net/manual/en/function.gzopen.php
17 */
18 private const READABLE_MODES = '/r|a\+|ab\+|w\+|wb\+|x\+|xb\+|c\+|cb\+/';
19 private const WRITABLE_MODES = '/a|w|r\+|rb\+|rw|x|c/';
20
21 /** @var resource */
22 private $stream;
23 /** @var int|null */
24 private $size;
25 /** @var bool */
26 private $seekable;
27 /** @var bool */
28 private $readable;
29 /** @var bool */
30 private $writable;
31 /** @var string|null */
32 private $uri;
33 /** @var mixed[] */
34 private $customMetadata;
35
36 /**
37 * This constructor accepts an associative array of options.
38 *
39 * - size: (int) If a read stream would otherwise have an indeterminate
40 * size, but the size is known due to foreknowledge, then you can
41 * provide that size, in bytes.
42 * - metadata: (array) Any additional metadata to return when the metadata
43 * of the stream is accessed.
44 *
45 * @param resource $stream Stream resource to wrap.
46 * @param array{size?: int, metadata?: array} $options Associative array of options.
47 *
48 * @throws \InvalidArgumentException if the stream is not a stream resource
49 */
50 public function __construct($stream, array $options = [])
51 {
52 if (!is_resource($stream)) {
53 throw new \InvalidArgumentException('Stream must be a resource');
54 }
55
56 if (isset($options['size'])) {
57 $this->size = $options['size'];
58 }
59
60 $this->customMetadata = $options['metadata'] ?? [];
61 $this->stream = $stream;
62 $meta = stream_get_meta_data($this->stream);
63 $this->seekable = $meta['seekable'];
64 $this->readable = (bool) preg_match(self::READABLE_MODES, $meta['mode']);
65 $this->writable = (bool) preg_match(self::WRITABLE_MODES, $meta['mode']);
66 $this->uri = $meta['uri'] ?? null;
67 }
68
69 /**
70 * Closes the stream when the destructed
71 */
72 public function __destruct()
73 {
74 $this->close();
75 }
76
77 public function __toString(): string
78 {
79 try {
80 if ($this->isSeekable()) {
81 $this->seek(0);
82 }
83
84 return $this->getContents();
85 } catch (\Throwable $e) {
86 if (\PHP_VERSION_ID >= 70400) {
87 throw $e;
88 }
89 trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR);
90
91 return '';
92 }
93 }
94
95 public function getContents(): string
96 {
97 if (!isset($this->stream)) {
98 throw new \RuntimeException('Stream is detached');
99 }
100
101 if (!$this->readable) {
102 throw new \RuntimeException('Cannot read from non-readable stream');
103 }
104
105 return Utils::tryGetContents($this->stream);
106 }
107
108 public function close(): void
109 {
110 if (isset($this->stream)) {
111 if (is_resource($this->stream)) {
112 fclose($this->stream);
113 }
114 $this->detach();
115 }
116 }
117
118 public function detach()
119 {
120 if (!isset($this->stream)) {
121 return null;
122 }
123
124 $result = $this->stream;
125 unset($this->stream);
126 $this->size = $this->uri = null;
127 $this->readable = $this->writable = $this->seekable = false;
128
129 return $result;
130 }
131
132 public function getSize(): ?int
133 {
134 if ($this->size !== null) {
135 return $this->size;
136 }
137
138 if (!isset($this->stream)) {
139 return null;
140 }
141
142 // Clear the stat cache if the stream has a URI
143 if ($this->uri) {
144 clearstatcache(true, $this->uri);
145 }
146
147 $stats = fstat($this->stream);
148 if (is_array($stats) && isset($stats['size'])) {
149 $this->size = $stats['size'];
150
151 return $this->size;
152 }
153
154 return null;
155 }
156
157 public function isReadable(): bool
158 {
159 return $this->readable;
160 }
161
162 public function isWritable(): bool
163 {
164 return $this->writable;
165 }
166
167 public function isSeekable(): bool
168 {
169 return $this->seekable;
170 }
171
172 public function eof(): bool
173 {
174 if (!isset($this->stream)) {
175 throw new \RuntimeException('Stream is detached');
176 }
177
178 return feof($this->stream);
179 }
180
181 public function tell(): int
182 {
183 if (!isset($this->stream)) {
184 throw new \RuntimeException('Stream is detached');
185 }
186
187 $result = ftell($this->stream);
188
189 if ($result === false) {
190 throw new \RuntimeException('Unable to determine stream position');
191 }
192
193 return $result;
194 }
195
196 public function rewind(): void
197 {
198 $this->seek(0);
199 }
200
201 public function seek($offset, $whence = SEEK_SET): void
202 {
203 if (!\is_int($offset)) {
204 \trigger_deprecation(
205 'guzzlehttp/psr7',
206 '2.11',
207 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $offset.',
208 \get_debug_type($offset)
209 );
210 }
211
212 if (!\is_int($whence)) {
213 \trigger_deprecation(
214 'guzzlehttp/psr7',
215 '2.11',
216 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $whence.',
217 \get_debug_type($whence)
218 );
219 }
220
221 $whence = (int) $whence;
222
223 if (!isset($this->stream)) {
224 throw new \RuntimeException('Stream is detached');
225 }
226 if (!$this->seekable) {
227 throw new \RuntimeException('Stream is not seekable');
228 }
229 if (fseek($this->stream, $offset, $whence) === -1) {
230 throw new \RuntimeException('Unable to seek to stream position '
231 .$offset.' with whence '.var_export($whence, true));
232 }
233 }
234
235 public function read($length): string
236 {
237 if (!\is_int($length)) {
238 \trigger_deprecation(
239 'guzzlehttp/psr7',
240 '2.11',
241 'Passing %s to StreamInterface::read() is deprecated; guzzlehttp/psr7 3.0 requires int for $length.',
242 \get_debug_type($length)
243 );
244 }
245
246 if (!isset($this->stream)) {
247 throw new \RuntimeException('Stream is detached');
248 }
249 if (!$this->readable) {
250 throw new \RuntimeException('Cannot read from non-readable stream');
251 }
252 if ($length < 0) {
253 throw new \RuntimeException('Length parameter cannot be negative');
254 }
255
256 if (0 === $length) {
257 return '';
258 }
259
260 try {
261 $string = fread($this->stream, $length);
262 } catch (\Exception $e) {
263 throw new \RuntimeException('Unable to read from stream', 0, $e);
264 }
265
266 if (false === $string) {
267 throw new \RuntimeException('Unable to read from stream');
268 }
269
270 return $string;
271 }
272
273 public function write($string): int
274 {
275 if (!\is_string($string)) {
276 \trigger_deprecation(
277 'guzzlehttp/psr7',
278 '2.11',
279 'Passing %s to StreamInterface::write() is deprecated; guzzlehttp/psr7 3.0 requires string for $string.',
280 \get_debug_type($string)
281 );
282 }
283
284 if (!isset($this->stream)) {
285 throw new \RuntimeException('Stream is detached');
286 }
287 if (!$this->writable) {
288 throw new \RuntimeException('Cannot write to a non-writable stream');
289 }
290
291 // We can't know the size after writing anything
292 $this->size = null;
293 $result = fwrite($this->stream, $string);
294
295 if ($result === false) {
296 throw new \RuntimeException('Unable to write to stream');
297 }
298
299 return $result;
300 }
301
302 /**
303 * @return mixed
304 */
305 public function getMetadata($key = null)
306 {
307 if ($key !== null && !\is_string($key)) {
308 \trigger_deprecation(
309 'guzzlehttp/psr7',
310 '2.11',
311 'Passing %s to StreamInterface::getMetadata() is deprecated; guzzlehttp/psr7 3.0 requires string|null for $key.',
312 \get_debug_type($key)
313 );
314 }
315
316 if (!isset($this->stream)) {
317 return $key ? null : [];
318 } elseif (!$key) {
319 return $this->customMetadata + stream_get_meta_data($this->stream);
320 } elseif (isset($this->customMetadata[$key])) {
321 return $this->customMetadata[$key];
322 }
323
324 $meta = stream_get_meta_data($this->stream);
325
326 return $meta[$key] ?? null;
327 }
328 }
329