11<?php
22
3- /**
3+ /*
44 * This file is part of the Symfony package.
5- * (c) Fabien Potencier <fabien@symfony.com>.
5+ *
6+ * (c) Fabien Potencier <fabien@symfony.com>
67 *
78 * For the full copyright and license information, please view the LICENSE
89 * 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
1710 */
11+
1812namespace MockStream ;
1913
2014/**
2115 * Mock stream class to be used with stream_wrapper_register.
22- *
2316 * stream_wrapper_register('mock', 'MockStream\MockStream')
2417 */
25- class MockStream {
26- private $ str_overloaded ;
18+ class MockStream
19+ {
20+ private $ strOverloaded ;
2721 private $ content ;
2822 private $ position ;
2923 private $ atime ;
@@ -37,13 +31,16 @@ class MockStream {
3731 * @param string $path Specifies the URL that was passed to the original function
3832 * @param string $mode The mode used to open the file, as detailed for fopen()
3933 * @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
4137 *
4238 * @return bool
4339 */
44- public function stream_open ($ path , $ mode , $ options , &$ opened_path ) {
40+ public function stream_open ($ path , $ mode , $ options , &$ opened_path )
41+ {
4542 // 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 ));
4744 $ this ->path = $ path ;
4845 $ this ->content = '' ;
4946 $ this ->position = 0 ;
@@ -60,8 +57,9 @@ public function stream_open($path, $mode, $options, &$opened_path) {
6057 *
6158 * @return string The data
6259 */
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 );
6563 $ this ->position += $ this ->strlen ($ ret );
6664 $ this ->atime = time ();
6765
@@ -75,10 +73,11 @@ public function stream_read($count) {
7573 *
7674 * @return int Number of bytes that were successfully stored, or 0 if none could be stored
7775 */
78- public function stream_write ($ data ) {
76+ public function stream_write ($ data )
77+ {
7978 $ left = $ this ->substr ($ this ->content , 0 , $ this ->position );
8079 $ right = $ this ->substr ($ this ->content , $ this ->position + $ this ->strlen ($ data ));
81- $ this ->content = $ left. $ data. $ right ;
80+ $ this ->content = $ left . $ data . $ right ;
8281 $ this ->position += $ this ->strlen ($ data );
8382 $ this ->mtime = time ();
8483 $ this ->ctime = time ();
@@ -91,50 +90,54 @@ public function stream_write($data) {
9190 *
9291 * @return int The current position of the stream
9392 */
94- public function stream_tell () {
93+ public function stream_tell ()
94+ {
9595 return $ this ->position ;
9696 }
9797
9898 /**
9999 * Tests for end-of-file on a file pointer.
100100 *
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
102103 */
103- public function stream_eof () {
104+ public function stream_eof ()
105+ {
104106 return $ this ->position >= $ this ->strlen ($ this ->content );
105107 }
106108
107109 /**
108110 * Seeks to specific location in a stream.
109111 *
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
112114 *
113115 * @return bool Return true if the position was updated, false otherwise
114116 */
115- public function stream_seek ($ offset , $ whence ) {
117+ public function stream_seek ($ offset , $ whence )
118+ {
116119 switch ($ whence ) {
117120 case SEEK_SET :
118121 if ($ offset < $ this ->strlen ($ this ->content ) && 0 <= $ offset ) {
119- $ this ->position = $ offset ;
122+ $ this ->position = $ offset ;
120123
121- return true ;
124+ return true ;
122125 }
123126 break ;
124127
125128 case SEEK_CUR :
126129 if (0 <= $ offset ) {
127- $ this ->position += $ offset ;
130+ $ this ->position += $ offset ;
128131
129- return true ;
132+ return true ;
130133 }
131134 break ;
132135
133136 case SEEK_END :
134137 if (0 <= $ this ->strlen ($ this ->content ) + $ offset ) {
135- $ this ->position = $ this ->strlen ($ this ->content ) + $ offset ;
138+ $ this ->position = $ this ->strlen ($ this ->content ) + $ offset ;
136139
137- return true ;
140+ return true ;
138141 }
139142 break ;
140143 }
@@ -145,13 +148,14 @@ public function stream_seek($offset, $whence) {
145148 /**
146149 * Change stream options, only touch is supported.
147150 *
148- * @param string $path The file path or URL to set metadata
151+ * @param string $path The file path or URL to set metadata
149152 * @param array $option
150- * @param array $value Additional arguments for the option
153+ * @param array $value Additional arguments for the option
151154 *
152155 * @return bool Return true on success or fale on failure or if option is not implemented
153156 */
154- public function stream_metadata ($ path , $ option , $ value ) {
157+ public function stream_metadata ($ path , $ option , $ value )
158+ {
155159 if (STREAM_META_TOUCH === $ option ) {
156160 $ now = array_key_exists (0 , $ value ) ? $ value [0 ] : time ();
157161 $ this ->atime = array_key_exists (1 , $ value ) ? $ value [1 ] : $ now ;
@@ -169,7 +173,8 @@ public function stream_metadata($path, $option, $value) {
169173 *
170174 * @return array Stream stats
171175 */
172- public function stream_stat () {
176+ public function stream_stat ()
177+ {
173178 return array (
174179 'dev ' => 0 ,
175180 'ino ' => 0 ,
@@ -195,7 +200,8 @@ public function stream_stat() {
195200 *
196201 * @return array File stats
197202 */
198- public function url_stat ($ path , $ flags ) {
203+ public function url_stat ($ path , $ flags )
204+ {
199205 return $ this ->stream_stat ();
200206 }
201207
@@ -206,21 +212,23 @@ public function url_stat($path, $flags) {
206212 *
207213 * @return int The number of bytes in the string on success, and 0 if the string is empty
208214 */
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 );
211218 }
212219
213220 /**
214221 * Returns the portion of string specified by the start and length parameters even when substr is overloaded to mb_substr.
215222 *
216223 * @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
219226 *
220227 * @return string
221228 */
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 );
224232 }
225233
226234}
0 commit comments