|
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
|
19 |
|
public function __construct($buffer, array $objectInfo = array(), $mode = 'r+') |
|
89
|
|
|
{ |
|
90
|
19 |
|
$this->buffer = $buffer; |
|
91
|
19 |
|
$this->length = strlen($buffer); |
|
92
|
19 |
|
$this->position = 0; |
|
93
|
19 |
|
$this->objectInfo = $objectInfo; |
|
94
|
19 |
|
$this->mode = $mode; |
|
95
|
19 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns the complete contents of the buffer |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
6 |
|
public function getBuffer() |
|
103
|
|
|
{ |
|
104
|
6 |
|
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
|
11 |
|
public function isEof() |
|
113
|
|
|
{ |
|
114
|
11 |
|
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
|
11 |
|
public function read($count) |
|
124
|
|
|
{ |
|
125
|
11 |
|
if ($this->isEof()) { |
|
126
|
8 |
|
return null; |
|
127
|
|
|
} |
|
128
|
11 |
|
$buffer = substr($this->buffer, $this->position, $count); |
|
129
|
11 |
|
$this->position += $count; |
|
130
|
11 |
|
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
|
6 |
|
public function write($data) |
|
140
|
|
|
{ |
|
141
|
6 |
|
$dataLength = strlen($data); |
|
142
|
6 |
|
$start = substr($this->buffer, 0, $this->position); |
|
143
|
6 |
|
$end = substr($this->buffer, $this->position + $dataLength); |
|
144
|
6 |
|
$this->buffer = $start.$data.$end; |
|
145
|
6 |
|
$this->length = strlen($this->buffer); |
|
146
|
6 |
|
$this->position += $dataLength; |
|
147
|
6 |
|
return $dataLength; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Returns the current pointer position |
|
152
|
|
|
* |
|
153
|
|
|
* @return integer |
|
154
|
|
|
*/ |
|
155
|
6 |
|
public function getPosition() |
|
156
|
|
|
{ |
|
157
|
6 |
|
return $this->position; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Returns the current length |
|
162
|
|
|
* |
|
163
|
|
|
* @return integer |
|
164
|
|
|
*/ |
|
165
|
1 |
|
public function getLength() |
|
166
|
|
|
{ |
|
167
|
1 |
|
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
|
9 |
|
public function setPosition($position, $whence) |
|
178
|
|
|
{ |
|
179
|
|
|
switch ($whence) { |
|
180
|
9 |
|
case SEEK_SET: |
|
181
|
4 |
|
$this->position = $position; |
|
182
|
4 |
|
break; |
|
183
|
6 |
|
case SEEK_CUR: |
|
184
|
3 |
|
$this->position += $position; |
|
185
|
3 |
|
break; |
|
186
|
5 |
|
case SEEK_END: |
|
187
|
4 |
|
$this->position = $this->length + $position; |
|
188
|
4 |
|
break; |
|
189
|
|
|
default: |
|
190
|
1 |
|
$this->position = 0; |
|
191
|
1 |
|
return false; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
8 |
|
if ($this->position < 0) { |
|
195
|
1 |
|
$this->position = 0; |
|
196
|
1 |
|
return false; |
|
197
|
8 |
|
} else if ($this->position > $this->length) { |
|
198
|
1 |
|
$this->position = $this->length; |
|
199
|
1 |
|
return false; |
|
200
|
|
|
} else { |
|
201
|
7 |
|
return true; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Returns the stat information for the buffer |
|
207
|
|
|
* |
|
208
|
|
|
* @return array |
|
209
|
|
|
*/ |
|
210
|
8 |
|
public function getStat() |
|
211
|
|
|
{ |
|
212
|
|
|
$stat = array( |
|
213
|
8 |
|
'ino' => 0, |
|
214
|
8 |
|
'mode' => (array_key_exists('mode', $this->objectInfo)) ? $this->objectInfo['mode'] : 0, |
|
215
|
8 |
|
'nlink' => 0, |
|
216
|
8 |
|
'uid' => 0, |
|
217
|
8 |
|
'gid' => 0, |
|
218
|
8 |
|
'rdev' => 0, |
|
219
|
8 |
|
'size' => (array_key_exists('size', $this->objectInfo)) ? $this->objectInfo['size'] : 0, |
|
220
|
8 |
|
'atime' => 0, |
|
221
|
8 |
|
'mtime' => 0, |
|
222
|
8 |
|
'ctime' => 0, |
|
223
|
|
|
'blksize' => -1, |
|
224
|
|
|
'blocks' => -1, |
|
225
|
|
|
); |
|
226
|
8 |
|
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
|
8 |
|
public function close() |
|
244
|
|
|
{ |
|
245
|
8 |
|
$this->buffer = null; |
|
246
|
8 |
|
$this->length = null; |
|
247
|
8 |
|
$this->position = null; |
|
248
|
8 |
|
$this->objectInfo = null; |
|
|
|
|
|
|
249
|
8 |
|
} |
|
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..