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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
<?php
/**
* Class WP_Test_Stream.
*
* An in-memory streamWrapper implementation for testing streams. Writes to a
* stream URL like "protocol://bucket/foo" will be stored in the static
* variable WP_Test_Stream::$data['bucket']['/foo'].
*
* Creating a directory at "protocol://bucket/foo" will store the string
* 'DIRECTORY' to the static variable WP_Test_Stream::$data['bucket']['/foo/']
* (note the trailing slash).
*
* This class can be used to test that code works with basic read/write streams.
*
* This class does not register itself as a stream handler: test fixtures
* should make the appropriate call to stream_wrapper_register().
*/
class WP_Test_Stream {
const FILE_MODE = 0100666;
const DIRECTORY_MODE = 040777;
/**
* In-memory storage for files and directories simulated by this wrapper.
*/
public static $data = array();
public $position;
public $file;
public $bucket;
public $data_ref;
/**
* The current context.
*
* @link https://www.php.net/manual/en/class.streamwrapper.php
*
* @var resource|null
*/
public $context;
/**
* Initializes internal state for reading the given URL.
*
* @param string $url A URL of the form "protocol://bucket/path".
*/
private function open( $url ) {
$components = array_merge(
array(
'host' => '',
'path' => '',
),
parse_url( $url )
);
$this->bucket = $components['host'];
$this->file = $components['path'] ? $components['path'] : '/';
if ( empty( $this->bucket ) ) {
throw new Exception( 'Cannot use an empty bucket name' );
}
if ( ! isset( WP_Test_Stream::$data[ $this->bucket ] ) ) {
WP_Test_Stream::$data[ $this->bucket ] = array();
}
$this->data_ref =& WP_Test_Stream::$data[ $this->bucket ][ $this->file ];
$this->position = 0;
}
/**
* Opens a URL.
*
* @see streamWrapper::stream_open
*/
public function stream_open( $path, $mode, $options, &$opened_path ) {
$this->open( $path );
return true;
}
/**
* Reads from a stream.
*
* @see streamWrapper::stream_read
*/
public function stream_read( $count ) {
if ( ! isset( $this->data_ref ) ) {
return '';
}
$ret = substr( $this->data_ref, $this->position, $count );
$this->position += strlen( $ret );
return $ret;
}
/**
* Writes to a stream.
*
* @see streamWrapper::stream_write
*/
public function stream_write( $data ) {
if ( ! isset( $this->data_ref ) ) {
$this->data_ref = '';
}
$left = substr( $this->data_ref, 0, $this->position );
$right = substr( $this->data_ref, $this->position + strlen( $data ) );
WP_Test_Stream::$data[ $this->bucket ][ $this->file ] = $left . $data . $right;
$this->position += strlen( $data );
return strlen( $data );
}
/**
* Seeks to specific location in a stream.
*
* @see streamWrapper::stream_seek
*
* @param int $offset The stream offset to seek to.
* @param int $whence Optional. Seek position.
* @return bool Returns true when position is updated, else false.
*/
public function stream_seek( $offset, $whence = SEEK_SET ) {
if ( empty( $this->data_ref ) ) {
return false;
}
$new_offset = $this->position;
switch ( $whence ) {
case SEEK_CUR:
$new_offset += $offset;
break;
case SEEK_END:
$new_offset = strlen( $this->data_ref ) + $offset;
break;
case SEEK_SET:
$new_offset = $offset;
break;
default:
return false;
}
if ( $new_offset < 0 ) {
return false;
}
// Save the new position.
$this->position = $new_offset;
return true;
}
/**
* Retrieves the current position of a stream.
*
* @see streamWrapper::stream_tell
*/
public function stream_tell() {
return $this->position;
}
/**
* Tests for end-of-file.
*
* @see streamWrapper::stream_eof
*/
public function stream_eof() {
if ( ! isset( $this->data_ref ) ) {
return true;
}
return $this->position >= strlen( $this->data_ref );
}
/**
* Change stream metadata.
*
* @see streamWrapper::stream_metadata
*/
public function stream_metadata( $path, $option, $value ) {
$this->open( $path );
if ( STREAM_META_TOUCH === $option ) {
if ( ! isset( $this->data_ref ) ) {
$this->data_ref = '';
}
return true;
}
return false;
}
/**
* Creates a directory.
*
* @see streamWrapper::mkdir
*
* @param string $path Directory which should be created.
* @param int $mode The value passed to mkdir().
* @param int $options A bitwise mask of values, such as STREAM_MKDIR_RECURSIVE.
* @return bool True on success, false on failure.
*/
public function mkdir( $path, $mode, $options ) {
$this->open( $path );
$plainfile = rtrim( $this->file, '/' );
// Check if a file or directory with the same name already exists.
if ( isset( WP_Test_Stream::$data[ $this->bucket ][ $plainfile ] )
|| isset( WP_Test_Stream::$data[ $this->bucket ][ $plainfile . '/' ] )
) {
return false;
}
$dir_ref = & $this->get_directory_ref();
$dir_ref = 'DIRECTORY';
return true;
}
/**
* Creates a file metadata object, with defaults.
*
* @param array $stats Partial file metadata.
* @return array Complete file metadata.
*/
private function make_stat( $stats ) {
$defaults = array(
'dev' => 0,
'ino' => 0,
'mode' => 0,
'nlink' => 0,
'uid' => 0,
'gid' => 0,
'rdev' => 0,
'size' => 0,
'atime' => 0,
'mtime' => 0,
'ctime' => 0,
'blksize' => 0,
'blocks' => 0,
);
return array_merge( $defaults, $stats );
}
/**
* Retrieves information about a file.
*
* @see streamWrapper::stream_stat
*/
public function stream_stat() {
$dir_ref = & $this->get_directory_ref();
if ( substr( $this->file, -1 ) === '/' || isset( $dir_ref ) ) {
return $this->make_stat(
array(
'mode' => WP_Test_Stream::DIRECTORY_MODE,
)
);
}
if ( ! isset( $this->data_ref ) ) {
return false;
}
return $this->make_stat(
array(
'size' => strlen( $this->data_ref ),
'mode' => WP_Test_Stream::FILE_MODE,
)
);
}
/**
* Retrieves information about a file.
*
* @see streamWrapper::url_stat
*/
public function url_stat( $path, $flags ) {
$this->open( $path );
return $this->stream_stat();
}
/**
* Deletes a file.
*
* @see streamWrapper::unlink
*/
public function unlink( $path ) {
if ( ! isset( $this->data_ref ) ) {
return false;
}
unset( WP_Test_Stream::$data[ $this->bucket ][ $this->file ] );
return true;
}
/**
* Interprets this stream's path as a directory, and returns the entry.
*
* @return A reference to the data entry for the directory.
*/
private function &get_directory_ref() {
return WP_Test_Stream::$data[ $this->bucket ][ rtrim( $this->file, '/' ) . '/' ];
}
}
|