1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (C) 2017 by TEQneers GmbH & Co. KG |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
7
|
|
|
* in the Software without restriction, including without limitation the rights |
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
10
|
|
|
* furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
13
|
|
|
* all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Git Stream Wrapper for PHP |
26
|
|
|
* |
27
|
|
|
* @category TQ |
28
|
|
|
* @package TQ_VCS |
29
|
|
|
* @subpackage VCS |
30
|
|
|
* @copyright Copyright (C) 2018 by TEQneers GmbH & Co. KG |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
namespace TQ\Vcs\Buffer; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Encapsulates a file revision buffer to be used in the stream wrapper |
37
|
|
|
* |
38
|
|
|
* @author Stefan Gehrig <gehrigteqneers.de> |
39
|
|
|
* @category TQ |
40
|
|
|
* @package TQ_VCS |
41
|
|
|
* @subpackage VCS |
42
|
|
|
* @copyright Copyright (C) 2018 by TEQneers GmbH & Co. KG |
43
|
|
|
*/ |
44
|
|
|
class StringBuffer implements FileBufferInterface |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* The buffer contents |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $buffer; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The buffer length |
55
|
|
|
* |
56
|
|
|
* @var integer |
57
|
|
|
*/ |
58
|
|
|
protected $length; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The current pointer position |
62
|
|
|
* |
63
|
|
|
* @var integer |
64
|
|
|
*/ |
65
|
|
|
protected $position; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The object info if available |
69
|
|
|
* |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
protected $objectInfo; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The read/write mode |
76
|
|
|
* |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
protected $mode; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Creates a new file buffer with the given contents |
83
|
|
|
* |
84
|
|
|
* @param string $buffer The buffer content |
85
|
|
|
* @param array $objectInfo The object info |
86
|
|
|
* @param string $mode The file mode |
87
|
|
|
*/ |
88
|
|
|
public function __construct($buffer, array $objectInfo = array(), $mode = 'r+') |
89
|
|
|
{ |
90
|
|
|
$this->buffer = $buffer; |
91
|
|
|
$this->length = strlen($buffer); |
92
|
|
|
$this->position = 0; |
93
|
|
|
$this->objectInfo = $objectInfo; |
94
|
|
|
$this->mode = $mode; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the complete contents of the buffer |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getBuffer() |
103
|
|
|
{ |
104
|
|
|
return $this->buffer; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns true if the pointer is at the end of the buffer |
109
|
|
|
* |
110
|
|
|
* @return boolean |
111
|
|
|
*/ |
112
|
|
|
public function isEof() |
113
|
|
|
{ |
114
|
|
|
return ($this->position > $this->length); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Reads $count bytes from the buffer |
119
|
|
|
* |
120
|
|
|
* @param integer $count The number of bytes to read |
121
|
|
|
* @return string|null |
122
|
|
|
*/ |
123
|
|
|
public function read($count) |
124
|
|
|
{ |
125
|
|
|
if ($this->isEof()) { |
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
$buffer = substr($this->buffer, $this->position, $count); |
129
|
|
|
$this->position += $count; |
130
|
|
|
return $buffer; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Writes the given date into the buffer at the current pointer position |
135
|
|
|
* |
136
|
|
|
* @param string $data The data to write |
137
|
|
|
* @return integer The number of bytes written |
138
|
|
|
*/ |
139
|
|
|
public function write($data) |
140
|
|
|
{ |
141
|
|
|
$dataLength = strlen($data); |
142
|
|
|
$start = substr($this->buffer, 0, $this->position); |
143
|
|
|
$end = substr($this->buffer, $this->position + $dataLength); |
144
|
|
|
$this->buffer = $start.$data.$end; |
145
|
|
|
$this->length = strlen($this->buffer); |
146
|
|
|
$this->position += $dataLength; |
147
|
|
|
return $dataLength; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns the current pointer position |
152
|
|
|
* |
153
|
|
|
* @return integer |
154
|
|
|
*/ |
155
|
|
|
public function getPosition() |
156
|
|
|
{ |
157
|
|
|
return $this->position; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns the current length |
162
|
|
|
* |
163
|
|
|
* @return integer |
164
|
|
|
*/ |
165
|
|
|
public function getLength() |
166
|
|
|
{ |
167
|
|
|
return $this->length; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Sets the pointer position |
172
|
|
|
* |
173
|
|
|
* @param integer $position The position in bytes |
174
|
|
|
* @param integer $whence The reference from where to measure $position (SEEK_SET, SEEK_CUR or SEEK_END) |
175
|
|
|
* @return boolean True if the position could be set |
176
|
|
|
*/ |
177
|
|
|
public function setPosition($position, $whence) |
178
|
|
|
{ |
179
|
|
|
switch ($whence) { |
180
|
|
|
case SEEK_SET: |
181
|
|
|
$this->position = $position; |
182
|
|
|
break; |
183
|
|
|
case SEEK_CUR: |
184
|
|
|
$this->position += $position; |
185
|
|
|
break; |
186
|
|
|
case SEEK_END: |
187
|
|
|
$this->position = $this->length + $position; |
188
|
|
|
break; |
189
|
|
|
default: |
190
|
|
|
$this->position = 0; |
191
|
|
|
return false; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ($this->position < 0) { |
195
|
|
|
$this->position = 0; |
196
|
|
|
return false; |
197
|
|
|
} else if ($this->position > $this->length) { |
198
|
|
|
$this->position = $this->length; |
199
|
|
|
return false; |
200
|
|
|
} else { |
201
|
|
|
return true; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns the stat information for the buffer |
207
|
|
|
* |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
|
|
public function getStat() |
211
|
|
|
{ |
212
|
|
|
$stat = array( |
213
|
|
|
'ino' => 0, |
214
|
|
|
'mode' => (array_key_exists('mode', $this->objectInfo)) ? $this->objectInfo['mode'] : 0, |
215
|
|
|
'nlink' => 0, |
216
|
|
|
'uid' => 0, |
217
|
|
|
'gid' => 0, |
218
|
|
|
'rdev' => 0, |
219
|
|
|
'size' => (array_key_exists('size', $this->objectInfo)) ? $this->objectInfo['size'] : 0, |
220
|
|
|
'atime' => 0, |
221
|
|
|
'mtime' => 0, |
222
|
|
|
'ctime' => 0, |
223
|
|
|
'blksize' => -1, |
224
|
|
|
'blocks' => -1, |
225
|
|
|
); |
226
|
|
|
return array_merge($stat, array_values($stat)); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Flushes the buffer to the storage media |
231
|
|
|
* |
232
|
|
|
* @return boolean |
233
|
|
|
*/ |
234
|
|
|
public function flush() |
235
|
|
|
{ |
236
|
|
|
// no needed for the string buffer |
237
|
|
|
return true; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Closes the buffer |
242
|
|
|
*/ |
243
|
|
|
public function close() |
244
|
|
|
{ |
245
|
|
|
$this->buffer = null; |
246
|
|
|
$this->length = null; |
247
|
|
|
$this->position = null; |
248
|
|
|
$this->objectInfo = null; |
|
|
|
|
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..