This repository has been archived by the owner on Nov 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stream.php
277 lines (236 loc) · 5.16 KB
/
Stream.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
namespace Yuloh\Stream;
use Psr\Http\Message\StreamInterface;
class Stream implements StreamInterface
{
private $readable = [
'r',
'w+',
'r+',
'x+',
'c+',
'rb',
'w+b',
'r+b',
'x+b',
'c+b',
'rt',
'w+t',
'r+t',
'x+t',
'c+t',
'a+',
];
private $writable = [
'w',
'w+',
'rw',
'r+',
'x+',
'c+',
'wb',
'w+b',
'r+b',
'x+b',
'c+b',
'w+t',
'r+t',
'x+t',
'c+t',
'a',
'a+',
];
/**
* @var resource
*/
private $resource;
/**
* @param resource $resource
* @throws \InvalidArgumentException
*/
public function __construct($resource)
{
if (!is_resource($resource)) {
throw new \InvalidArgumentException(
sprintf('Expected a valid resource, got "%s".', gettype($resource))
);
}
$this->resource = $resource;
}
/**
* {@inheritdoc}
*/
public function __toString()
{
try {
$this->rewind();
return $this->getContents();
} catch (\RuntimeException $e) {
return '';
}
}
/**
* {@inheritdoc}
*/
public function close()
{
if ($this->resource) {
fclose($this->detach());
}
}
/**
* {@inheritdoc}
*/
public function detach()
{
$resource = $this->resource;
$this->resource = null;
return $resource;
}
/**
* {@inheritdoc}
*/
public function getSize()
{
if (!$this->resource) {
return null;
}
return fstat($this->resource)['size'];
}
/**
* {@inheritdoc}
*/
public function tell()
{
if (!$this->resource) {
throw new \RuntimeException('Cannot find the current pointer position, the stream is detached.');
}
if (($pos = ftell($this->resource)) !== false) {
return $pos;
}
throw new \RuntimeException('Unable to find current pointer position of stream.');
}
/**
* {@inheritdoc}
*/
public function eof()
{
if (!$this->resource) {
return true;
}
return feof($this->resource);
}
/**
* {@inheritdoc}
*/
public function isSeekable()
{
return $this->resource && $this->getMetadata('seekable');
}
/**
* {@inheritdoc}
*/
public function seek($offset, $whence = SEEK_SET)
{
$this->throwIfUnSeekable();
if (fseek($this->resource, $offset, $whence) === 0) {
return;
}
throw new \RuntimeException('Unable to seek stream.');
}
/**
* {@inheritdoc}
*/
public function rewind()
{
$this->seek(0);
}
/**
* {@inheritdoc}
*/
public function isWritable()
{
return $this->resource && in_array($this->getMetadata('mode'), $this->writable);
}
/**
* {@inheritdoc}
*/
public function write($string)
{
$this->throwIfUnwritable();
if ($bytesWritten = fwrite($this->resource, $string)) {
return $bytesWritten;
}
throw new \RuntimeException('Unable to write stream.');
}
/**
* {@inheritdoc}
*/
public function isReadable()
{
return $this->resource && in_array($this->getMetadata('mode'), $this->readable);
}
/**
* {@inheritdoc}
*/
public function read($length)
{
$this->throwIfUnreadable();
if ($contents = fread($this->resource, $length)) {
return $contents;
}
throw new \RuntimeException('Unable to read stream.');
}
/**
* {@inheritdoc}
*/
public function getContents()
{
$this->throwIfUnreadable();
if (($contents = stream_get_contents($this->resource)) !== false) {
return $contents;
}
throw new \RuntimeException('Unable to read stream.');
}
/**
* {@inheritdoc}
*/
public function getMetadata($key = null)
{
if (!$this->resource) {
return $key ? null : [];
}
$metadata = stream_get_meta_data($this->resource);
if (is_null($key)) {
return $metadata;
}
return array_key_exists($key, $metadata) ? $metadata[$key] : null;
}
/**
* @throws \RuntimeException
*/
private function throwIfUnwritable()
{
if (!$this->isWritable()) {
throw new \RuntimeException('The stream is not writable.');
}
}
/**
* @throws \RuntimeException
*/
private function throwIfUnreadable()
{
if (!$this->isReadable()) {
throw new \RuntimeException('The stream is not readable.');
}
}
/**
* @throws \RuntimeException
*/
private function throwIfUnSeekable()
{
if (!$this->isSeekable()) {
throw new \RuntimeException('The stream is not seekable.');
}
}
}