Hoa central
File.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\File;
38 
39 use Hoa\Core;
40 use Hoa\Stream;
41 
50 abstract class File
51  extends Generic
52  implements Stream\IStream\Bufferable,
55 {
62  const MODE_READ = 'rb';
63 
70  const MODE_READ_WRITE = 'r+b';
71 
79  const MODE_TRUNCATE_WRITE = 'wb';
80 
88  const MODE_TRUNCATE_READ_WRITE = 'w+b';
89 
96  const MODE_APPEND_WRITE = 'ab';
97 
104  const MODE_APPEND_READ_WRITE = 'a+b';
105 
115  const MODE_CREATE_WRITE = 'xb';
116 
126  const MODE_CREATE_READ_WRITE = 'x+b';
127 
128 
129 
142  public function __construct(
143  $streamName,
144  $mode,
145  $context = null,
146  $wait = false
147  ) {
148  $this->setMode($mode);
149 
150  switch ($streamName) {
151 
152  case '0':
153  $streamName = 'php://stdin';
154 
155  break;
156 
157  case '1':
158  $streamName = 'php://stdout';
159 
160  break;
161 
162  case '2':
163  $streamName = 'php://stderr';
164 
165  break;
166 
167  default:
168  if (true === ctype_digit($streamName)) {
169  if (PHP_VERSION_ID >= 50306) {
170  $streamName = 'php://fd/' . $streamName;
171  } else {
172  throw new Exception(
173  'You need PHP5.3.6 to use a file descriptor ' .
174  'other than 0, 1 or 2 (tried %d with PHP%s).',
175  0,
176  [$streamName, PHP_VERSION]
177  );
178  }
179  }
180  }
181 
182  parent::__construct($streamName, $context, $wait);
183 
184  return;
185  }
186 
196  protected function &_open($streamName, Stream\Context $context = null)
197  {
198  if (substr($streamName, 0, 4) == 'file' &&
199  false === is_dir(dirname($streamName))) {
200  throw new Exception(
201  'Directory %s does not exist. Could not open file %s.',
202  1,
203  [dirname($streamName), basename($streamName)]
204  );
205  }
206 
207  if (null === $context) {
208  if (false === $out = @fopen($streamName, $this->getMode(), true)) {
209  throw new Exception(
210  'Failed to open stream %s.',
211  2,
212  $streamName
213  );
214  }
215 
216  return $out;
217  }
218 
219  $out = @fopen(
220  $streamName,
221  $this->getMode(),
222  true,
223  $context->getContext()
224  );
225 
226  if (false === $out) {
227  throw new Exception(
228  'Failed to open stream %s.',
229  3,
230  $streamName
231  );
232  }
233 
234  return $out;
235  }
236 
242  protected function _close()
243  {
244  return @fclose($this->getStream());
245  }
246 
255  public function newBuffer($callable = null, $size = null)
256  {
257  $this->setStreamBuffer($size);
258 
259  //@TODO manage $callable as a filter?
260 
261  return 1;
262  }
263 
269  public function flush()
270  {
271  return fflush($this->getStream());
272  }
273 
279  public function deleteBuffer()
280  {
281  return $this->disableStreamBuffer();
282  }
283 
289  public function getBufferLevel()
290  {
291  return 1;
292  }
293 
299  public function getBufferSize()
300  {
301  return $this->getStreamBufferSize();
302  }
303 
311  public function lock($operation)
312  {
313  return flock($this->getStream(), $operation);
314  }
315 
321  public function rewind()
322  {
323  return rewind($this->getStream());
324  }
325 
334  public function seek($offset, $whence = Stream\IStream\Pointable::SEEK_SET)
335  {
336  return fseek($this->getStream(), $offset, $whence);
337  }
338 
344  public function tell()
345  {
346  $stream = $this->getStream();
347 
348  if (null === $stream) {
349  return 0;
350  }
351 
352  return ftell($stream);
353  }
354 
362  public static function create($name, $dummy)
363  {
364  if (file_exists($name)) {
365  return true;
366  }
367 
368  return touch($name);
369  }
370 }
371 
375 Core\Consistency::flexEntity('Hoa\File\File');
deleteBuffer()
Definition: File.php:279
const MODE_READ
Definition: File.php:62
& _open($streamName, Stream\Context $context=null)
Definition: File.php:196
static create($name, $dummy)
Definition: File.php:362
lock($operation)
Definition: File.php:311
const MODE_CREATE_READ_WRITE
Definition: File.php:126
seek($offset, $whence=Stream\IStream\Pointable::SEEK_SET)
Definition: File.php:334
getBufferLevel()
Definition: File.php:289
const MODE_APPEND_WRITE
Definition: File.php:96
getBufferSize()
Definition: File.php:299
setMode($mode)
Definition: Generic.php:517
__construct($streamName, $mode, $context=null, $wait=false)
Definition: File.php:142
setStreamBuffer($buffer)
Definition: Stream.php:465
touch($time=-1, $atime=-1)
Definition: Generic.php:286
newBuffer($callable=null, $size=null)
Definition: File.php:255
const MODE_CREATE_WRITE
Definition: File.php:115
const MODE_TRUNCATE_READ_WRITE
Definition: File.php:88
const MODE_APPEND_READ_WRITE
Definition: File.php:104
const MODE_READ_WRITE
Definition: File.php:70
const MODE_TRUNCATE_WRITE
Definition: File.php:79