forked from xp-framework/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreams.class.php
More file actions
executable file
·278 lines (246 loc) · 6.62 KB
/
Streams.class.php
File metadata and controls
executable file
·278 lines (246 loc) · 6.62 KB
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
278
<?php namespace io\streams;
use io\{FileNotFoundException, OperationNotSupportedException, IOException};
/**
* Wraps I/O streams into PHP streams
*
* @test io.unittest.StreamWrappingTest
* @see https://www.php.net/manual/de/ref.stream.php
*/
abstract class Streams {
protected static $streams= [];
protected $length= 0;
protected $id= null;
public $context= null;
static function __static() {
stream_wrapper_register('iostrr', get_class(new class() extends Streams {
static function __static() { }
public function stream_open($path, $mode, $options, $opened_path) {
parent::stream_open($path, $mode, $options, $opened_path);
$this->length= parent::$streams[$this->id]->available();
return true;
}
public function stream_write($data) {
throw new IOException('Cannot write to readable stream');
}
public function stream_read($count) {
return parent::$streams[$this->id]->read($count);
}
public function stream_flush() {
return true;
}
public function stream_eof() {
return 0 === parent::$streams[$this->id]->available();
}
}));
stream_wrapper_register('iostrw', get_class(new class() extends Streams {
static function __static() { }
public function stream_write($data) {
parent::$streams[$this->id]->write($data);
$written= strlen($data);
$this->length+= $written;
return $written;
}
public function stream_truncate($size) {
if (parent::$streams[$this->id] instanceof Truncation) {
parent::$streams[$this->id]->truncate($size);
return true;
}
throw new IOException('Cannot truncate underlying stream');
}
public function stream_read($count) {
throw new IOException('Cannot read from writeable stream');
}
public function stream_flush() {
return parent::$streams[$this->id]->flush();
}
public function stream_eof() {
return false;
}
}));
}
/**
* Open an input stream for reading
*
* @param io.streams.InputStream s
* @return resource
*/
public static function readableFd(InputStream $s) {
$hash= spl_object_hash($s);
self::$streams[$hash]= $s;
return fopen('iostrr://'.$hash, 'rb');
}
/**
* Open an input stream for reading and return URI
*
* @param io.streams.InputStream s
* @return string
*/
public static function readableUri(InputStream $s) {
$hash= spl_object_hash($s);
self::$streams[$hash]= $s;
return 'iostrr://'.$hash;
}
/**
* Open an output stream for writing
*
* @param io.streams.OutputStream s
* @return resource
*/
public static function writeableFd(OutputStream $s) {
$hash= spl_object_hash($s);
self::$streams[$hash]= $s;
return fopen('iostrw://'.$hash, 'wb');
}
/**
* Open an output stream for writing
*
* @param io.streams.OutputStream s
* @return resource
*/
public static function writeableUri(OutputStream $s) {
$hash= spl_object_hash($s);
self::$streams[$hash]= $s;
return 'iostrw://'.$hash;
}
/**
* Read an IOElements' contents completely into a buffer in a single call.
*
* @param io.streams.InputStream s
* @return string
* @throws io.IOException
*/
public static function readAll(InputStream $s) {
$r= '';
while ($s->available() > 0) $r.= $s->read();
return $r;
}
/**
* Read an IOElements' contents completely into a buffer in a single call.
*
* @param io.streams.InputStream $s
* @param int $offset
* @param int $whence default SEEK_SET (one of SEEK_[SET|CUR|END])
* @return void
* @throws io.IOException
*/
public static function seek($s, $offset, $whence= SEEK_SET) {
if ($s instanceof Seekable) {
$s->seek($offset, $whence);
} else {
throw new OperationNotSupportedException('Cannot seek instances of '.nameof($s));
}
}
/**
* Callback for fopen
*
* @param string path
* @param string mode
* @param int options
* @param string opened_path
* @throws io.FileNotFoundException in case the given file cannot be found
*/
public function stream_open($path, $mode, $options, $opened_path) {
sscanf(urldecode($path), "iostr%c://%[^$]", $m, $this->id);
if (!isset(self::$streams[$this->id])) {
throw new FileNotFoundException('Cannot open stream "'.$this->id.'" mode '.$mode);
}
return true;
}
/**
* Callback for fclose
*
* @return bool
*/
public function stream_close() {
if (!isset(self::$streams[$this->id])) return false;
self::$streams[$this->id]->close();
unset(self::$streams[$this->id]);
return true;
}
/**
* Callback for fseek
*
* @param int offset
* @param int whence
* @return bool
*/
public function stream_seek($offset, $whence) {
if (!self::$streams[$this->id] instanceof Seekable) {
throw new IOException('Underlying stream does not support seeking');
}
self::$streams[$this->id]->seek($offset, $whence);
return true;
}
/**
* Callback for ftell
*
* @return int position
*/
public function stream_tell() {
if (!self::$streams[$this->id] instanceof Seekable) {
throw new IOException('Underlying stream does not support seeking');
}
return self::$streams[$this->id]->tell();
}
/**
* Callback for fstat
*
* @return [:var] stat
*/
public function stream_stat() {
return ['size' => $this->length];
}
/**
* Callback for stat
*
* @see php://streamwrapper.url-stat
* @param string path
* @param int flags
* @return [:var] stat
*/
public function url_stat($path, $flags) {
sscanf(urldecode($path), "iostr%c://%[^$]", $m, $id);
if (!isset(self::$streams[$id])) {
return false;
} else if ('r' === $m) {
return ['size' => 0, 'mode' => 0100644];
} else if ('w' === $m) {
return ['size' => 0, 'mode' => 0100644];
}
}
/**
* Stream wrapper method stream_flush
*
* @return bool
*/
public abstract function stream_flush();
/**
* Callback for fwrite
*
* @param string data
* @return int length
*/
public abstract function stream_write($data);
/**
* Callback for fread
*
* @param int count
* @return string
*/
public abstract function stream_read($count);
/**
* Callback for feof
*
* @return bool eof
*/
public abstract function stream_eof();
/**
* Callback for casting
*
* @param int cast_as
* @return var
*/
public function stream_cast($cast_as) {
return false;
}
}