| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Hoa
|
|
|
5 |
*
|
|
|
6 |
*
|
|
|
7 |
* @license
|
|
|
8 |
*
|
|
|
9 |
* New BSD License
|
|
|
10 |
*
|
|
|
11 |
* Copyright © 2007-2017, Hoa community. All rights reserved.
|
|
|
12 |
*
|
|
|
13 |
* Redistribution and use in source and binary forms, with or without
|
|
|
14 |
* modification, are permitted provided that the following conditions are met:
|
|
|
15 |
* * Redistributions of source code must retain the above copyright
|
|
|
16 |
* notice, this list of conditions and the following disclaimer.
|
|
|
17 |
* * Redistributions in binary form must reproduce the above copyright
|
|
|
18 |
* notice, this list of conditions and the following disclaimer in the
|
|
|
19 |
* documentation and/or other materials provided with the distribution.
|
|
|
20 |
* * Neither the name of the Hoa nor the names of its contributors may be
|
|
|
21 |
* used to endorse or promote products derived from this software without
|
|
|
22 |
* specific prior written permission.
|
|
|
23 |
*
|
|
|
24 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
25 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
26 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
27 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
|
|
|
28 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
29 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
30 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
31 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
32 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
33 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
34 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
35 |
*/
|
|
|
36 |
|
|
|
37 |
namespace Psy\Readline\Hoa;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Class \Hoa\File.
|
|
|
41 |
*
|
|
|
42 |
* File handler.
|
|
|
43 |
*/
|
|
|
44 |
abstract class File extends FileGeneric implements StreamBufferable, StreamLockable, StreamPointable
|
|
|
45 |
{
|
|
|
46 |
/**
|
|
|
47 |
* Open for reading only; place the file pointer at the beginning of the
|
|
|
48 |
* file.
|
|
|
49 |
*/
|
|
|
50 |
const MODE_READ = 'rb';
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Open for reading and writing; place the file pointer at the beginning of
|
|
|
54 |
* the file.
|
|
|
55 |
*/
|
|
|
56 |
const MODE_READ_WRITE = 'r+b';
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Open for writing only; place the file pointer at the beginning of the
|
|
|
60 |
* file and truncate the file to zero length. If the file does not exist,
|
|
|
61 |
* attempt to create it.
|
|
|
62 |
*/
|
|
|
63 |
const MODE_TRUNCATE_WRITE = 'wb';
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Open for reading and writing; place the file pointer at the beginning of
|
|
|
67 |
* the file and truncate the file to zero length. If the file does not
|
|
|
68 |
* exist, attempt to create it.
|
|
|
69 |
*/
|
|
|
70 |
const MODE_TRUNCATE_READ_WRITE = 'w+b';
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Open for writing only; place the file pointer at the end of the file. If
|
|
|
74 |
* the file does not exist, attempt to create it.
|
|
|
75 |
*/
|
|
|
76 |
const MODE_APPEND_WRITE = 'ab';
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Open for reading and writing; place the file pointer at the end of the
|
|
|
80 |
* file. If the file does not exist, attempt to create it.
|
|
|
81 |
*/
|
|
|
82 |
const MODE_APPEND_READ_WRITE = 'a+b';
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Create and open for writing only; place the file pointer at the beginning
|
|
|
86 |
* of the file. If the file already exits, the fopen() call with fail by
|
|
|
87 |
* returning false and generating an error of level E_WARNING. If the file
|
|
|
88 |
* does not exist, attempt to create it. This is equivalent to specifying
|
|
|
89 |
* O_EXCL | O_CREAT flags for the underlying open(2) system call.
|
|
|
90 |
*/
|
|
|
91 |
const MODE_CREATE_WRITE = 'xb';
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Create and open for reading and writing; place the file pointer at the
|
|
|
95 |
* beginning of the file. If the file already exists, the fopen() call with
|
|
|
96 |
* fail by returning false and generating an error of level E_WARNING. If
|
|
|
97 |
* the file does not exist, attempt to create it. This is equivalent to
|
|
|
98 |
* specifying O_EXCL | O_CREAT flags for the underlying open(2) system call.
|
|
|
99 |
*/
|
|
|
100 |
const MODE_CREATE_READ_WRITE = 'x+b';
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Open a file.
|
|
|
104 |
*/
|
|
|
105 |
public function __construct(
|
|
|
106 |
string $streamName,
|
|
|
107 |
string $mode,
|
|
|
108 |
string $context = null,
|
|
|
109 |
bool $wait = false
|
|
|
110 |
) {
|
|
|
111 |
$this->setMode($mode);
|
|
|
112 |
|
|
|
113 |
switch ($streamName) {
|
|
|
114 |
case '0':
|
|
|
115 |
$streamName = 'php://stdin';
|
|
|
116 |
|
|
|
117 |
break;
|
|
|
118 |
|
|
|
119 |
case '1':
|
|
|
120 |
$streamName = 'php://stdout';
|
|
|
121 |
|
|
|
122 |
break;
|
|
|
123 |
|
|
|
124 |
case '2':
|
|
|
125 |
$streamName = 'php://stderr';
|
|
|
126 |
|
|
|
127 |
break;
|
|
|
128 |
|
|
|
129 |
default:
|
|
|
130 |
if (true === \ctype_digit($streamName)) {
|
|
|
131 |
if (\PHP_VERSION_ID >= 50306) {
|
|
|
132 |
$streamName = 'php://fd/'.$streamName;
|
|
|
133 |
} else {
|
|
|
134 |
throw new FileException('You need PHP5.3.6 to use a file descriptor '.'other than 0, 1 or 2 (tried %d with PHP%s).', 0, [$streamName, \PHP_VERSION]);
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
parent::__construct($streamName, $context, $wait);
|
|
|
140 |
|
|
|
141 |
return;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* Open the stream and return the associated resource.
|
|
|
146 |
*/
|
|
|
147 |
protected function &_open(string $streamName, StreamContext $context = null)
|
|
|
148 |
{
|
|
|
149 |
if (\substr($streamName, 0, 4) === 'file' &&
|
|
|
150 |
false === \is_dir(\dirname($streamName))) {
|
|
|
151 |
throw new FileException('Directory %s does not exist. Could not open file %s.', 1, [\dirname($streamName), \basename($streamName)]);
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
if (null === $context) {
|
|
|
155 |
if (false === $out = @\fopen($streamName, $this->getMode(), true)) {
|
|
|
156 |
throw new FileException('Failed to open stream %s.', 2, $streamName);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
return $out;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
$out = @\fopen(
|
|
|
163 |
$streamName,
|
|
|
164 |
$this->getMode(),
|
|
|
165 |
true,
|
|
|
166 |
$context->getContext()
|
|
|
167 |
);
|
|
|
168 |
|
|
|
169 |
if (false === $out) {
|
|
|
170 |
throw new FileException('Failed to open stream %s.', 3, $streamName);
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
return $out;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Close the current stream.
|
|
|
178 |
*/
|
|
|
179 |
protected function _close(): bool
|
|
|
180 |
{
|
|
|
181 |
return @\fclose($this->getStream());
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
/**
|
|
|
185 |
* Start a new buffer.
|
|
|
186 |
* The callable acts like a light filter.
|
|
|
187 |
*/
|
|
|
188 |
public function newBuffer($callable = null, int $size = null): int
|
|
|
189 |
{
|
|
|
190 |
$this->setStreamBuffer($size);
|
|
|
191 |
|
|
|
192 |
// @TODO manage $callable as a filter?
|
|
|
193 |
|
|
|
194 |
return 1;
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
/**
|
|
|
198 |
* Flush the output to a stream.
|
|
|
199 |
*/
|
|
|
200 |
public function flush(): bool
|
|
|
201 |
{
|
|
|
202 |
return \fflush($this->getStream());
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
/**
|
|
|
206 |
* Delete buffer.
|
|
|
207 |
*/
|
|
|
208 |
public function deleteBuffer(): bool
|
|
|
209 |
{
|
|
|
210 |
return $this->disableStreamBuffer();
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* Get bufffer level.
|
|
|
215 |
*/
|
|
|
216 |
public function getBufferLevel(): int
|
|
|
217 |
{
|
|
|
218 |
return 1;
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* Get buffer size.
|
|
|
223 |
*/
|
|
|
224 |
public function getBufferSize(): int
|
|
|
225 |
{
|
|
|
226 |
return $this->getStreamBufferSize();
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* Portable advisory locking.
|
|
|
231 |
*/
|
|
|
232 |
public function lock(int $operation): bool
|
|
|
233 |
{
|
|
|
234 |
return \flock($this->getStream(), $operation);
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
/**
|
|
|
238 |
* Rewind the position of a stream pointer.
|
|
|
239 |
*/
|
|
|
240 |
public function rewind(): bool
|
|
|
241 |
{
|
|
|
242 |
return \rewind($this->getStream());
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
/**
|
|
|
246 |
* Seek on a stream pointer.
|
|
|
247 |
*/
|
|
|
248 |
public function seek(int $offset, int $whence = StreamPointable::SEEK_SET): int
|
|
|
249 |
{
|
|
|
250 |
return \fseek($this->getStream(), $offset, $whence);
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
/**
|
|
|
254 |
* Get the current position of the stream pointer.
|
|
|
255 |
*/
|
|
|
256 |
public function tell(): int
|
|
|
257 |
{
|
|
|
258 |
$stream = $this->getStream();
|
|
|
259 |
|
|
|
260 |
if (null === $stream) {
|
|
|
261 |
return 0;
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
return \ftell($stream);
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
/**
|
|
|
268 |
* Create a file.
|
|
|
269 |
*/
|
|
|
270 |
public static function create(string $name)
|
|
|
271 |
{
|
|
|
272 |
if (\file_exists($name)) {
|
|
|
273 |
return true;
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
return \touch($name);
|
|
|
277 |
}
|
|
|
278 |
}
|