|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace PTS\Psr7; |
|
5
|
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Psr\Http\Message\StreamInterface; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
use function clearstatcache; |
|
10
|
|
|
use function fclose; |
|
11
|
|
|
use function feof; |
|
12
|
|
|
use function fopen; |
|
13
|
|
|
use function fread; |
|
14
|
|
|
use function fseek; |
|
15
|
|
|
use function fstat; |
|
16
|
|
|
use function ftell; |
|
17
|
|
|
use function fwrite; |
|
18
|
|
|
use function is_resource; |
|
19
|
|
|
use function is_string; |
|
20
|
|
|
use function stream_get_contents; |
|
21
|
|
|
use function stream_get_meta_data; |
|
22
|
|
|
use const SEEK_SET; |
|
23
|
|
|
|
|
24
|
|
|
class Stream implements StreamInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var resource|null A resource reference */ |
|
27
|
|
|
protected $stream = null; |
|
28
|
|
|
protected bool $seekable = false; |
|
29
|
|
|
protected bool $readable = false; |
|
30
|
|
|
protected bool $writable = false; |
|
31
|
|
|
/** @var array|mixed|void|null */ |
|
32
|
|
|
protected $uri; |
|
33
|
|
|
/** @var int|null */ |
|
34
|
|
|
protected ?int $size = null; |
|
35
|
|
|
|
|
36
|
|
|
protected const READ_WRITE_HASH = [ |
|
37
|
|
|
'read' => [ |
|
38
|
|
|
'r' => true, |
|
39
|
|
|
'w+' => true, |
|
40
|
|
|
'r+' => true, |
|
41
|
|
|
'x+' => true, |
|
42
|
|
|
'c+' => true, |
|
43
|
|
|
'rb' => true, |
|
44
|
|
|
'w+b' => true, |
|
45
|
|
|
'r+b' => true, |
|
46
|
|
|
'x+b' => true, |
|
47
|
|
|
'c+b' => true, |
|
48
|
|
|
'rt' => true, |
|
49
|
|
|
'w+t' => true, |
|
50
|
|
|
'r+t' => true, |
|
51
|
|
|
'x+t' => true, |
|
52
|
|
|
'c+t' => true, |
|
53
|
|
|
'a+' => true, |
|
54
|
|
|
], |
|
55
|
|
|
'write' => [ |
|
56
|
|
|
'w' => true, |
|
57
|
|
|
'w+' => true, |
|
58
|
|
|
'rw' => true, |
|
59
|
|
|
'r+' => true, |
|
60
|
|
|
'x+' => true, |
|
61
|
|
|
'c+' => true, |
|
62
|
|
|
'wb' => true, |
|
63
|
|
|
'w+b' => true, |
|
64
|
|
|
'r+b' => true, |
|
65
|
|
|
'x+b' => true, |
|
66
|
|
|
'c+b' => true, |
|
67
|
|
|
'w+t' => true, |
|
68
|
|
|
'r+t' => true, |
|
69
|
|
|
'x+t' => true, |
|
70
|
|
|
'c+t' => true, |
|
71
|
|
|
'a' => true, |
|
72
|
|
|
'a+' => true, |
|
73
|
|
|
], |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Creates a new PSR-7 stream. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string|resource|StreamInterface $body |
|
80
|
|
|
* |
|
81
|
|
|
* @return StreamInterface |
|
82
|
|
|
* @throws InvalidArgumentException |
|
83
|
|
|
*/ |
|
84
|
54 |
|
public static function create($body = ''): StreamInterface |
|
85
|
|
|
{ |
|
86
|
54 |
|
if ($body instanceof StreamInterface) { |
|
87
|
2 |
|
return $body; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
52 |
|
if (is_string($body)) { |
|
91
|
32 |
|
$resource = fopen('php://temp', 'rw+'); |
|
92
|
32 |
|
fwrite($resource, $body); |
|
93
|
32 |
|
$body = $resource; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
52 |
|
if (is_resource($body)) { |
|
97
|
51 |
|
$new = new self(); |
|
98
|
51 |
|
$new->stream = $body; |
|
99
|
51 |
|
$meta = stream_get_meta_data($new->stream); |
|
100
|
51 |
|
$new->seekable = $meta['seekable'] && 0 === \fseek($new->stream, 0, \SEEK_CUR); |
|
101
|
51 |
|
$new->readable = isset(self::READ_WRITE_HASH['read'][$meta['mode']]); |
|
102
|
51 |
|
$new->writable = isset(self::READ_WRITE_HASH['write'][$meta['mode']]); |
|
103
|
51 |
|
$new->uri = $new->getMetadata('uri'); |
|
104
|
|
|
|
|
105
|
51 |
|
return $new; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
throw new InvalidArgumentException('First argument to Stream::create() must be a string, resource or StreamInterface.'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Closes the stream when the destructed. |
|
113
|
|
|
*/ |
|
114
|
51 |
|
public function __destruct() |
|
115
|
|
|
{ |
|
116
|
51 |
|
$this->close(); |
|
117
|
51 |
|
} |
|
118
|
|
|
|
|
119
|
23 |
|
public function __toString(): string |
|
120
|
|
|
{ |
|
121
|
23 |
|
if ($this->isSeekable()) { |
|
122
|
22 |
|
$this->seek(0); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
23 |
|
return $this->getContents(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
51 |
|
public function close(): void |
|
129
|
|
|
{ |
|
130
|
51 |
|
if (isset($this->stream) && is_resource($this->stream)) { |
|
131
|
49 |
|
fclose($this->stream); |
|
132
|
49 |
|
$this->detach(); |
|
133
|
|
|
} |
|
134
|
51 |
|
} |
|
135
|
|
|
|
|
136
|
51 |
|
public function detach() |
|
137
|
|
|
{ |
|
138
|
51 |
|
if ($this->stream === null) { |
|
139
|
1 |
|
return null; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
51 |
|
$result = $this->stream; |
|
143
|
51 |
|
$this->stream = null; |
|
144
|
|
|
|
|
145
|
51 |
|
$this->size = $this->uri = null; |
|
146
|
51 |
|
$this->readable = $this->writable = $this->seekable = false; |
|
147
|
|
|
|
|
148
|
51 |
|
return $result; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
7 |
|
public function getSize(): ?int |
|
152
|
|
|
{ |
|
153
|
7 |
|
if (null !== $this->size) { |
|
154
|
3 |
|
return $this->size; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
7 |
|
if (!isset($this->stream)) { |
|
158
|
2 |
|
return null; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
// Clear the stat cache if the stream has a URI |
|
162
|
5 |
|
if ($this->uri) { |
|
163
|
5 |
|
clearstatcache(true, $this->uri); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
5 |
|
$stats = fstat($this->stream); |
|
167
|
5 |
|
if (isset($stats['size'])) { |
|
168
|
5 |
|
$this->size = $stats['size']; |
|
169
|
5 |
|
return $this->size; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return null; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
2 |
|
public function tell(): int |
|
176
|
|
|
{ |
|
177
|
2 |
|
$position = $this->stream === null ? false : ftell($this->stream); |
|
178
|
2 |
|
if (false === $position) { |
|
179
|
1 |
|
throw new RuntimeException('Unable to determine stream position'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
1 |
|
return $position; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
6 |
|
public function eof(): bool |
|
186
|
|
|
{ |
|
187
|
6 |
|
return !$this->stream || feof($this->stream); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
29 |
|
public function isSeekable(): bool |
|
191
|
|
|
{ |
|
192
|
29 |
|
return $this->seekable; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
30 |
|
public function seek($offset, $whence = SEEK_SET): void |
|
196
|
|
|
{ |
|
197
|
30 |
|
if (!$this->seekable) { |
|
198
|
1 |
|
throw new RuntimeException('Stream is not seekable'); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
29 |
|
if (-1 === fseek($this->stream, $offset, $whence)) { |
|
202
|
1 |
|
throw new RuntimeException('Unable to seek to stream position ' . |
|
203
|
1 |
|
$offset . |
|
204
|
1 |
|
' with whence ' . |
|
205
|
1 |
|
\var_export($whence, true)); |
|
206
|
|
|
} |
|
207
|
28 |
|
} |
|
208
|
|
|
|
|
209
|
5 |
|
public function rewind(): void |
|
210
|
|
|
{ |
|
211
|
5 |
|
$this->seek(0); |
|
212
|
5 |
|
} |
|
213
|
|
|
|
|
214
|
3 |
|
public function isWritable(): bool |
|
215
|
|
|
{ |
|
216
|
3 |
|
return $this->writable; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
7 |
|
public function write($string): int |
|
220
|
|
|
{ |
|
221
|
7 |
|
if (!$this->writable) { |
|
222
|
2 |
|
throw new RuntimeException('Cannot write to a non-writable stream'); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
// We can't know the size after writing anything |
|
226
|
6 |
|
$this->size = null; |
|
227
|
|
|
|
|
228
|
6 |
|
if (false === $result = fwrite($this->stream, $string)) { |
|
229
|
|
|
throw new RuntimeException('Unable to write to stream'); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
6 |
|
return $result; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
3 |
|
public function isReadable(): bool |
|
236
|
|
|
{ |
|
237
|
3 |
|
return $this->readable; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
5 |
|
public function read($length): string |
|
241
|
|
|
{ |
|
242
|
5 |
|
if (!$this->readable) { |
|
243
|
1 |
|
throw new RuntimeException('Cannot read from non-readable stream'); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
4 |
|
$result = @fread($this->stream, $length); |
|
247
|
4 |
|
if (false === $result) { |
|
248
|
|
|
throw new RuntimeException('Unable to read from stream'); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
4 |
|
return $result; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
25 |
|
public function getContents(): string |
|
255
|
|
|
{ |
|
256
|
25 |
|
if (!isset($this->stream)) { |
|
257
|
1 |
|
throw new RuntimeException('Stream is detached'); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
24 |
|
if (false === $contents = @stream_get_contents($this->stream)) { |
|
261
|
|
|
throw new RuntimeException('Unable to read stream contents'); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
24 |
|
return $contents; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
51 |
|
/** |
|
268
|
|
|
* @param string|null $key |
|
269
|
51 |
|
* @return mixed |
|
270
|
1 |
|
*/ |
|
271
|
|
|
public function getMetadata($key = null) |
|
272
|
|
|
{ |
|
273
|
51 |
|
if (!isset($this->stream)) { |
|
274
|
|
|
return $key ? null : []; |
|
275
|
51 |
|
} |
|
276
|
1 |
|
|
|
277
|
|
|
$meta = stream_get_meta_data($this->stream); |
|
278
|
|
|
|
|
279
|
51 |
|
if (null === $key) { |
|
280
|
|
|
return $meta; |
|
281
|
1 |
|
} |
|
282
|
|
|
|
|
283
|
|
|
return $meta[$key] ?? null; |
|
284
|
|
|
} |
|
285
|
|
|
} |