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 stream 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 StreamBuffer implements FileBufferInterface |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* The file resource |
48
|
|
|
* |
49
|
|
|
* @var resource |
50
|
|
|
*/ |
51
|
|
|
protected $stream; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Creates a new file buffer with the given path |
55
|
|
|
* |
56
|
|
|
* @param string $path The path |
57
|
|
|
* @param string $mode The file mode |
58
|
|
|
* @throws StreamBufferException If the stream cannot be opened in the given mode |
59
|
|
|
*/ |
60
|
|
|
public function __construct($path, $mode = 'r+') |
61
|
|
|
{ |
62
|
|
|
$this->stream = @fopen($path, $mode); |
63
|
|
|
if ($this->stream === false) { |
64
|
|
|
throw new StreamBufferException(sprintf('Cannot access "%s" in mode "%s"', $path, $mode)); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Destructor closes file stream handle |
70
|
|
|
*/ |
71
|
|
|
public function __destruct() |
72
|
|
|
{ |
73
|
|
|
$this->close(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the complete contents of the buffer |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getBuffer() |
82
|
|
|
{ |
83
|
|
|
$currentPos = $this->getPosition(); |
84
|
|
|
$this->setPosition(0, SEEK_SET); |
85
|
|
|
$buffer = stream_get_contents($this->stream); |
86
|
|
|
$this->setPosition($currentPos, SEEK_SET); |
87
|
|
|
return $buffer; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns true if the pointer is at the end of the buffer |
92
|
|
|
* |
93
|
|
|
* @return boolean |
94
|
|
|
*/ |
95
|
|
|
public function isEof() |
96
|
|
|
{ |
97
|
|
|
return feof($this->stream); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Reads $count bytes from the buffer |
102
|
|
|
* |
103
|
|
|
* @param integer $count The number of bytes to read |
104
|
|
|
* @return string|null |
105
|
|
|
*/ |
106
|
|
|
public function read($count) |
107
|
|
|
{ |
108
|
|
|
return fread($this->stream, $count); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Writes the given date into the buffer at the current pointer position |
113
|
|
|
* |
114
|
|
|
* @param string $data The data to write |
115
|
|
|
* @return integer The number of bytes written |
116
|
|
|
*/ |
117
|
|
|
public function write($data) |
118
|
|
|
{ |
119
|
|
|
return fwrite($this->stream, $data); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns the current pointer position |
124
|
|
|
* |
125
|
|
|
* @return integer |
126
|
|
|
*/ |
127
|
|
|
public function getPosition() |
128
|
|
|
{ |
129
|
|
|
return ftell($this->stream); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sets the pointer position |
134
|
|
|
* |
135
|
|
|
* @param integer $position The position in bytes |
136
|
|
|
* @param integer $whence The reference from where to measure $position (SEEK_SET, SEEK_CUR or SEEK_END) |
137
|
|
|
* @return boolean True if the position could be set |
138
|
|
|
*/ |
139
|
|
|
public function setPosition($position, $whence) |
140
|
|
|
{ |
141
|
|
|
return (fseek($this->stream, $position, $whence) == 0); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns the stat information for the buffer |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
public function getStat() |
150
|
|
|
{ |
151
|
|
|
return fstat($this->stream); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Flushes the buffer to the storage media |
156
|
|
|
* |
157
|
|
|
* @return boolean |
158
|
|
|
*/ |
159
|
|
|
public function flush() |
160
|
|
|
{ |
161
|
|
|
return fflush($this->stream); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Closes the buffer |
166
|
|
|
*/ |
167
|
|
|
public function close() |
168
|
|
|
{ |
169
|
|
|
if ($this->stream !== null) { |
170
|
|
|
fclose($this->stream); |
171
|
|
|
$this->stream = null; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|