REST API initial commit
[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 fwrite( $stream, $block );
39 }
40
41 public function __toString() {
42 return $this->contents;
43 }
44
45 public function close() {
46 }
47
48 public function detach() {
49 return null;
50 }
51
52 public function getSize() {
53 return strlen( $this->contents );
54 }
55
56 public function tell() {
57 return $this->offset;
58 }
59
60 public function eof() {
61 return $this->offset >= strlen( $this->contents );
62 }
63
64 public function isSeekable() {
65 return true;
66 }
67
68 public function seek( $offset, $whence = SEEK_SET ) {
69 switch ( $whence ) {
70 case SEEK_SET:
71 $this->offset = $offset;
72 break;
73
74 case SEEK_CUR:
75 $this->offset += $offset;
76 break;
77
78 case SEEK_END:
79 $this->offset = strlen( $this->contents ) + $offset;
80 break;
81
82 default:
83 throw new \InvalidArgumentException( "Invalid value for \$whence" );
84 }
85 if ( $this->offset > strlen( $this->contents ) ) {
86 throw new \InvalidArgumentException( "Cannot seek beyond the end of a StringStream" );
87 }
88 if ( $this->offset < 0 ) {
89 throw new \InvalidArgumentException( "Cannot seek before the start of a StringStream" );
90 }
91 }
92
93 public function rewind() {
94 $this->offset = 0;
95 }
96
97 public function isWritable() {
98 return true;
99 }
100
101 public function write( $string ) {
102 if ( $this->offset === strlen( $this->contents ) ) {
103 $this->contents .= $string;
104 } else {
105 $this->contents = substr_replace( $this->contents, $string,
106 $this->offset, strlen( $string ) );
107 }
108 $this->offset += strlen( $string );
109 return strlen( $string );
110 }
111
112 public function isReadable() {
113 return true;
114 }
115
116 public function read( $length ) {
117 if ( $this->offset === 0 && $length >= strlen( $this->contents ) ) {
118 $ret = $this->contents;
119 } else {
120 $ret = substr( $this->contents, $this->offset, $length );
121 }
122 $this->offset += strlen( $ret );
123 return $ret;
124 }
125
126 public function getContents() {
127 if ( $this->offset === 0 ) {
128 $ret = $this->contents;
129 } else {
130 $ret = substr( $this->contents, $this->offset );
131 }
132 $this->offset = strlen( $this->contents );
133 return $ret;
134 }
135
136 public function getMetadata( $key = null ) {
137 return null;
138 }
139 }