Merge "StringStream::copyToStream() should adjust the offset"
[lhc/web/wiklou.git] / includes / Rest / StringStream.php
1 <?php
2
3 namespace MediaWiki\Rest;
4
5 /**
6 * A stream class which uses a string as the underlying storage. Surprisingly,
7 * Guzzle does not appear to have one of these. BufferStream does not do what
8 * we want.
9 *
10 * The normal use of this class should be to first write to the stream, then
11 * rewind, then read back the whole buffer with getContents().
12 *
13 * Seeking is supported, however seeking past the end of the string does not
14 * fill with null bytes as in a real file, it throws an exception instead.
15 */
16 class StringStream implements CopyableStreamInterface {
17 private $contents = '';
18 private $offset = 0;
19
20 /**
21 * Construct a StringStream with the given contents.
22 *
23 * The offset will start at 0, ready for reading. If appending to the
24 * given string is desired, you should first seek to the end.
25 *
26 * @param string $contents
27 */
28 public function __construct( $contents = '' ) {
29 $this->contents = $contents;
30 }
31
32 public function copyToStream( $stream ) {
33 if ( $this->offset !== 0 ) {
34 $block = substr( $this->contents, $this->offset );
35 } else {
36 $block = $this->contents;
37 }
38 $this->offset = strlen( $this->contents );
39 fwrite( $stream, $block );
40 }
41
42 public function __toString() {
43 return $this->contents;
44 }
45
46 public function close() {
47 }
48
49 public function detach() {
50 return null;
51 }
52
53 public function getSize() {
54 return strlen( $this->contents );
55 }
56
57 public function tell() {
58 return $this->offset;
59 }
60
61 public function eof() {
62 return $this->offset >= strlen( $this->contents );
63 }
64
65 public function isSeekable() {
66 return true;
67 }
68
69 public function seek( $offset, $whence = SEEK_SET ) {
70 switch ( $whence ) {
71 case SEEK_SET:
72 $this->offset = $offset;
73 break;
74
75 case SEEK_CUR:
76 $this->offset += $offset;
77 break;
78
79 case SEEK_END:
80 $this->offset = strlen( $this->contents ) + $offset;
81 break;
82
83 default:
84 throw new \InvalidArgumentException( "Invalid value for \$whence" );
85 }
86 if ( $this->offset > strlen( $this->contents ) ) {
87 throw new \InvalidArgumentException( "Cannot seek beyond the end of a StringStream" );
88 }
89 if ( $this->offset < 0 ) {
90 throw new \InvalidArgumentException( "Cannot seek before the start of a StringStream" );
91 }
92 }
93
94 public function rewind() {
95 $this->offset = 0;
96 }
97
98 public function isWritable() {
99 return true;
100 }
101
102 public function write( $string ) {
103 if ( $this->offset === strlen( $this->contents ) ) {
104 $this->contents .= $string;
105 } else {
106 $this->contents = substr_replace( $this->contents, $string,
107 $this->offset, strlen( $string ) );
108 }
109 $this->offset += strlen( $string );
110 return strlen( $string );
111 }
112
113 public function isReadable() {
114 return true;
115 }
116
117 public function read( $length ) {
118 if ( $this->offset === 0 && $length >= strlen( $this->contents ) ) {
119 $ret = $this->contents;
120 } else {
121 $ret = substr( $this->contents, $this->offset, $length );
122 }
123 $this->offset += strlen( $ret );
124 return $ret;
125 }
126
127 public function getContents() {
128 if ( $this->offset === 0 ) {
129 $ret = $this->contents;
130 } else {
131 $ret = substr( $this->contents, $this->offset );
132 }
133 $this->offset = strlen( $this->contents );
134 return $ret;
135 }
136
137 public function getMetadata( $key = null ) {
138 return null;
139 }
140 }