1
0
mirror of https://github.com/danog/file.git synced 2024-11-26 20:04:51 +01:00
file/lib/BlockingHandle.php

145 lines
3.4 KiB
PHP
Raw Normal View History

2016-08-24 07:01:41 +02:00
<?php declare(strict_types = 1);
2015-08-13 01:02:41 +02:00
namespace Amp\File;
use Amp\{ Success, Failure };
use Interop\Async\Awaitable;
2015-08-13 01:02:41 +02:00
class BlockingHandle implements Handle {
private $fh;
private $path;
private $mode;
/**
* @param resource $fh An open uv filesystem descriptor
2016-08-30 21:05:14 +02:00
* @param string $path
* @param string $mode
2015-08-13 01:02:41 +02:00
*/
public function __construct($fh, string $path, string $mode) {
2015-08-13 01:02:41 +02:00
$this->fh = $fh;
$this->path = $path;
$this->mode = $mode;
}
2016-08-30 21:05:14 +02:00
public function __destruct() {
if ($this->fh !== null) {
\fclose($this->fh);
}
}
2015-08-13 01:02:41 +02:00
/**
* {@inheritdoc}
*/
public function read(int $length): Awaitable {
2016-08-30 21:05:14 +02:00
if ($this->fh === null) {
throw new \Error("The file has been closed");
}
$data = \fread($this->fh, $length);
2015-08-13 01:02:41 +02:00
if ($data !== false) {
return new Success($data);
} else {
return new Failure(new FilesystemException(
2015-08-13 01:02:41 +02:00
"Failed reading from file handle"
));
}
}
/**
* {@inheritdoc}
*/
public function write(string $data): Awaitable {
2016-08-30 21:05:14 +02:00
if ($this->fh === null) {
throw new \Error("The file has been closed");
}
2015-08-13 01:02:41 +02:00
$len = \fwrite($this->fh, $data);
if ($len !== false) {
2016-08-30 21:05:14 +02:00
return new Success($len);
2015-08-13 01:02:41 +02:00
} else {
return new Failure(new FilesystemException(
2015-08-13 01:02:41 +02:00
"Failed writing to file handle"
));
}
}
/**
* {@inheritdoc}
*/
public function close(): Awaitable {
2016-08-30 21:05:14 +02:00
if ($this->fh === null) {
throw new \Error("The file has already been closed");
}
$fh = $this->fh;
$this->fh = null;
if (\fclose($fh)) {
2015-08-13 01:02:41 +02:00
return new Success;
} else {
return new Failure(new FilesystemException(
2015-08-13 01:02:41 +02:00
"Failed closing file handle"
));
}
}
/**
* {@inheritdoc}
*/
2016-08-30 21:05:14 +02:00
public function seek(int $position, int $whence = \SEEK_SET): Awaitable {
if ($this->fh === null) {
throw new \Error("The file has been closed");
}
2015-08-13 01:02:41 +02:00
switch ($whence) {
case \SEEK_SET:
case \SEEK_CUR:
case \SEEK_END:
2016-08-30 21:05:14 +02:00
if (@\fseek($this->fh, $position, $whence) === -1) {
return new Failure(new FilesystemException("Could not seek in file"));
}
return new Success($this->tell());
2015-08-13 01:02:41 +02:00
default:
2016-08-30 21:05:14 +02:00
throw new \Error(
2015-08-13 01:02:41 +02:00
"Invalid whence parameter; SEEK_SET, SEEK_CUR or SEEK_END expected"
);
}
}
/**
* {@inheritdoc}
*/
public function tell(): int {
2016-08-30 21:05:14 +02:00
if ($this->fh === null) {
throw new \Error("The file has been closed");
}
2015-08-13 01:02:41 +02:00
return \ftell($this->fh);
}
/**
* {@inheritdoc}
*/
public function eof(): bool {
2016-08-30 21:05:14 +02:00
if ($this->fh === null) {
throw new \Error("The file has been closed");
}
2015-08-13 01:02:41 +02:00
return \feof($this->fh);
}
/**
* {@inheritdoc}
*/
public function path(): string {
2015-08-13 01:02:41 +02:00
return $this->path;
}
/**
* {@inheritdoc}
*/
public function mode(): string {
2015-08-13 01:02:41 +02:00
return $this->mode;
}
}