1
1
<?php
2
2
3
- /**
3
+ /*
4
4
* This file is part of the Symfony package.
5
- * (c) Fabien Potencier <fabien@symfony.com>.
5
+ *
6
+ * (c) Fabien Potencier <fabien@symfony.com>
6
7
*
7
8
* For the full copyright and license information, please view the LICENSE
8
9
* file that was distributed with this source code.
9
- *
10
- * This class is based on VariableStream from the PHP Manual, which is licenced
11
- * under Creative Commons Attribution 3.0 Licence copyright (c) the PHP
12
- * Documentation Group
13
- *
14
- * @url http://php.net/manual/en/stream.streamwrapper.example-1.php
15
- * @url http://php.net/license/
16
- * @url http://creativecommons.org/licenses/by/3.0/legalcode
17
10
*/
11
+
18
12
namespace MockStream ;
19
13
20
14
/**
21
15
* Mock stream class to be used with stream_wrapper_register.
22
- *
23
- * stream_wrapper_register('mock', 'MockStream\MockStream')
16
+ * stream_wrapper_register('mock', 'MockStream\MockStream').
24
17
*/
25
- class MockStream {
26
- private $ str_overloaded ;
18
+ class MockStream
19
+ {
20
+ private $ strOverloaded ;
27
21
private $ content ;
28
22
private $ position ;
29
23
private $ atime ;
@@ -37,13 +31,16 @@ class MockStream {
37
31
* @param string $path Specifies the URL that was passed to the original function
38
32
* @param string $mode The mode used to open the file, as detailed for fopen()
39
33
* @param int $options Holds additional flags set by the streams API
40
- * @param string $opened_path If the path is opened successfully, and STREAM_USE_PATH is set in options, opened_path should be set to the full path of the file/resource that was actually opened
34
+ * @param string $opened_path If the path is opened successfully, and STREAM_USE_PATH is set in options,
35
+ * opened_path should be set to the full path of the file/resource that was actually
36
+ * opened
41
37
*
42
38
* @return bool
43
39
*/
44
- public function stream_open ($ path , $ mode , $ options , &$ opened_path ) {
40
+ public function stream_open ($ path , $ mode , $ options , &$ opened_path )
41
+ {
45
42
// Is mbstring.func_overload applied to string functions (bit 2 set)
46
- $ this ->str_overloaded = (bool ) (ini_get ('mbstring.func_overload ' ) & (1 << 2 ));
43
+ $ this ->strOverloaded = (bool ) (ini_get ('mbstring.func_overload ' ) & (1 << 2 ));
47
44
$ this ->path = $ path ;
48
45
$ this ->content = '' ;
49
46
$ this ->position = 0 ;
@@ -60,8 +57,9 @@ public function stream_open($path, $mode, $options, &$opened_path) {
60
57
*
61
58
* @return string The data
62
59
*/
63
- public function stream_read ($ count ) {
64
- $ ret = $ this ->substr ($ this ->varname , $ this ->position , $ count );
60
+ public function stream_read ($ count )
61
+ {
62
+ $ ret = $ this ->substr ($ this ->content , $ this ->position , $ count );
65
63
$ this ->position += $ this ->strlen ($ ret );
66
64
$ this ->atime = time ();
67
65
@@ -75,7 +73,8 @@ public function stream_read($count) {
75
73
*
76
74
* @return int Number of bytes that were successfully stored, or 0 if none could be stored
77
75
*/
78
- public function stream_write ($ data ) {
76
+ public function stream_write ($ data )
77
+ {
79
78
$ left = $ this ->substr ($ this ->content , 0 , $ this ->position );
80
79
$ right = $ this ->substr ($ this ->content , $ this ->position + $ this ->strlen ($ data ));
81
80
$ this ->content = $ left .$ data .$ right ;
@@ -91,50 +90,54 @@ public function stream_write($data) {
91
90
*
92
91
* @return int The current position of the stream
93
92
*/
94
- public function stream_tell () {
93
+ public function stream_tell ()
94
+ {
95
95
return $ this ->position ;
96
96
}
97
97
98
98
/**
99
99
* Tests for end-of-file on a file pointer.
100
100
*
101
- * @return bool Return true if the read/write position is at the end of the stream and if no more data is available to be read, or false otherwise
101
+ * @return bool Return true if the read/write position is at the end of the stream and if no more data is available
102
+ * to be read, or false otherwise
102
103
*/
103
- public function stream_eof () {
104
+ public function stream_eof ()
105
+ {
104
106
return $ this ->position >= $ this ->strlen ($ this ->content );
105
107
}
106
108
107
109
/**
108
110
* Seeks to specific location in a stream.
109
111
*
110
- * @param string $offset The stream offset to seek to
111
- * @param int $whence Set position based on value
112
+ * @param int $offset The stream offset to seek to
113
+ * @param int $whence Set position based on value
112
114
*
113
115
* @return bool Return true if the position was updated, false otherwise
114
116
*/
115
- public function stream_seek ($ offset , $ whence ) {
117
+ public function stream_seek ($ offset , $ whence )
118
+ {
116
119
switch ($ whence ) {
117
120
case SEEK_SET :
118
121
if ($ offset < $ this ->strlen ($ this ->content ) && 0 <= $ offset ) {
119
- $ this ->position = $ offset ;
122
+ $ this ->position = $ offset ;
120
123
121
- return true ;
124
+ return true ;
122
125
}
123
126
break ;
124
127
125
128
case SEEK_CUR :
126
129
if (0 <= $ offset ) {
127
- $ this ->position += $ offset ;
130
+ $ this ->position += $ offset ;
128
131
129
- return true ;
132
+ return true ;
130
133
}
131
134
break ;
132
135
133
136
case SEEK_END :
134
137
if (0 <= $ this ->strlen ($ this ->content ) + $ offset ) {
135
- $ this ->position = $ this ->strlen ($ this ->content ) + $ offset ;
138
+ $ this ->position = $ this ->strlen ($ this ->content ) + $ offset ;
136
139
137
- return true ;
140
+ return true ;
138
141
}
139
142
break ;
140
143
}
@@ -151,7 +154,8 @@ public function stream_seek($offset, $whence) {
151
154
*
152
155
* @return bool Return true on success or fale on failure or if option is not implemented
153
156
*/
154
- public function stream_metadata ($ path , $ option , $ value ) {
157
+ public function stream_metadata ($ path , $ option , $ value )
158
+ {
155
159
if (STREAM_META_TOUCH === $ option ) {
156
160
$ now = array_key_exists (0 , $ value ) ? $ value [0 ] : time ();
157
161
$ this ->atime = array_key_exists (1 , $ value ) ? $ value [1 ] : $ now ;
@@ -169,7 +173,8 @@ public function stream_metadata($path, $option, $value) {
169
173
*
170
174
* @return array Stream stats
171
175
*/
172
- public function stream_stat () {
176
+ public function stream_stat ()
177
+ {
173
178
return array (
174
179
'dev ' => 0 ,
175
180
'ino ' => 0 ,
@@ -195,7 +200,8 @@ public function stream_stat() {
195
200
*
196
201
* @return array File stats
197
202
*/
198
- public function url_stat ($ path , $ flags ) {
203
+ public function url_stat ($ path , $ flags )
204
+ {
199
205
return $ this ->stream_stat ();
200
206
}
201
207
@@ -206,21 +212,23 @@ public function url_stat($path, $flags) {
206
212
*
207
213
* @return int The number of bytes in the string on success, and 0 if the string is empty
208
214
*/
209
- protected function strlen ($ string ) {
210
- return function_exists ('mb_strlen ' ) && $ this ->str_overloaded ? mb_strlen ($ string , '8bit ' ) : strlen ($ string );
215
+ protected function strlen ($ string )
216
+ {
217
+ return function_exists ('mb_strlen ' ) && $ this ->strOverloaded ? mb_strlen ($ string , '8bit ' ) : strlen ($ string );
211
218
}
212
219
213
220
/**
214
221
* Returns the portion of string specified by the start and length parameters even when substr is overloaded to mb_substr.
215
222
*
216
223
* @param string $string The input string which must be one character or longer
217
- * @param start $int Starting position in bytes
218
- * @param length $ int Length in bytes which if omitted or NULL is passed, extracts all bytes to the end of the string
224
+ * @param int $start Starting position in bytes
225
+ * @param int $length Length in bytes which if omitted or NULL is passed, extracts all bytes to the end of the string
219
226
*
220
227
* @return string
221
228
*/
222
- protected function substr ($ string , $ start , $ length = null ) {
223
- return function_exists ('mb_substr ' ) && $ this ->str_overloaded ? mb_substr ($ string , $ start , $ length , '8bit ' ) : substr ($ string , $ start , $ length );
229
+ protected function substr ($ string , $ start , $ length = null )
230
+ {
231
+ return function_exists ('mb_substr ' ) && $ this ->strOverloaded ? mb_substr ($ string , $ start , $ length , '8bit ' ) : substr ($ string , $ start , $ length );
224
232
}
225
233
226
234
}
0 commit comments