mirror of
https://github.com/danog/file.git
synced 2024-11-30 04:19:39 +01:00
108 lines
2.2 KiB
PHP
108 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Amp\File;
|
|
|
|
use Amp\Promise;
|
|
use Amp\Success;
|
|
use Amp\Failure;
|
|
|
|
class BlockingHandle implements Handle {
|
|
private $fh;
|
|
private $path;
|
|
private $mode;
|
|
|
|
/**
|
|
* @param resource $fh An open uv filesystem descriptor
|
|
*/
|
|
public function __construct($fh, $path, $mode) {
|
|
$this->fh = $fh;
|
|
$this->path = $path;
|
|
$this->mode = $mode;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function read($len) {
|
|
$data = \fread($this->fh, $len);
|
|
if ($data !== false) {
|
|
return new Success($data);
|
|
} else {
|
|
return new Failure(new \RuntimeException(
|
|
"Failed reading from file handle"
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function write($data) {
|
|
$len = \fwrite($this->fh, $data);
|
|
if ($len !== false) {
|
|
return new Success($data);
|
|
} else {
|
|
return new Failure(new \RuntimeException(
|
|
"Failed writing to file handle"
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function close() {
|
|
if (\fclose($this->fh)) {
|
|
return new Success;
|
|
} else {
|
|
return new Failure(new \RuntimeException(
|
|
"Failed closing file handle"
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function seek($position, $whence = \SEEK_SET) {
|
|
switch ($whence) {
|
|
case \SEEK_SET:
|
|
case \SEEK_CUR:
|
|
case \SEEK_END:
|
|
\fseek($this->fh, $position, $whence);
|
|
return;
|
|
default:
|
|
throw new FilesystemException(
|
|
"Invalid whence parameter; SEEK_SET, SEEK_CUR or SEEK_END expected"
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function tell() {
|
|
return \ftell($this->fh);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function eof() {
|
|
return \feof($this->fh);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function path() {
|
|
return $this->path;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function mode() {
|
|
return $this->mode;
|
|
}
|
|
} |