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

173 lines
4.1 KiB
PHP
Raw Normal View History

2015-07-11 03:59:39 +02:00
<?php
namespace Amp\Fs;
2015-07-17 16:27:38 +02:00
use Amp\Reactor;
use Amp\Promise;
use Amp\Success;
use Amp\Failure;
2015-07-11 03:59:39 +02:00
class BlockingFilesystem implements Filesystem {
private $reactor;
public function __construct(Reactor $reactor = null) {
2015-07-17 16:27:38 +02:00
$this->reactor = $reactor ?: \Amp\reactor();
2015-07-11 03:59:39 +02:00
}
/**
* {@inheritdoc}
*/
2015-07-17 16:27:38 +02:00
public function open($path, $mode = self::READ) {
2015-07-11 03:59:39 +02:00
$openMode = 0;
if ($mode & self::READ && $mode & self::WRITE) {
$openMode = ($mode & self::CREATE) ? "c+" : "r+";
} elseif ($mode & self::READ) {
$openMode = "r";
} elseif ($mode & self::WRITE) {
$openMode = "c";
} else {
return new Failure(new \InvalidArgumentException(
"Invalid file open mode: Filesystem::READ or Filesystem::WRITE or both required"
));
}
if ($fh = @fopen($path, $openMode)) {
$descriptor = new BlockingDescriptor($fh, $path);
return new Success($descriptor);
} else {
return new Failure(new \RuntimeException(
"Failed opening file handle"
));
}
}
/**
* {@inheritdoc}
*/
2015-07-17 16:27:38 +02:00
public function stat($path) {
2015-07-11 03:59:39 +02:00
if ($stat = @stat($path)) {
$stat["isfile"] = (bool) is_file($path);
$stat["isdir"] = empty($stat["isfile"]);
clearstatcache(true, $path);
} else {
$stat = null;
}
return new Success($stat);
}
/**
* {@inheritdoc}
*/
2015-07-17 16:27:38 +02:00
public function lstat($path) {
2015-07-11 03:59:39 +02:00
if ($stat = @lstat($path)) {
$stat["isfile"] = (bool) is_file($path);
$stat["isdir"] = empty($stat["isfile"]);
clearstatcache(true, $path);
} else {
$stat = null;
}
return new Success($stat);
}
/**
* {@inheritdoc}
*/
2015-07-17 16:27:38 +02:00
public function symlink($target, $link) {
2015-07-11 03:59:39 +02:00
return new Success((bool) symlink($target, $link));
}
/**
* {@inheritdoc}
*/
2015-07-17 16:27:38 +02:00
public function rename($from, $to) {
2015-07-11 03:59:39 +02:00
return new Success((bool) rename($from, $to));
}
/**
* {@inheritdoc}
*/
2015-07-17 16:27:38 +02:00
public function unlink($path) {
2015-07-11 03:59:39 +02:00
return new Success((bool) unlink($path));
}
/**
* {@inheritdoc}
*/
2015-07-17 16:27:38 +02:00
public function mkdir($path, $mode = 0644) {
2015-07-11 03:59:39 +02:00
return new Success((bool) mkdir($path, $mode));
}
/**
* {@inheritdoc}
*/
2015-07-17 16:27:38 +02:00
public function rmdir($path) {
2015-07-11 03:59:39 +02:00
return new Success((bool) rmdir($path));
}
/**
* {@inheritdoc}
*/
2015-07-17 16:27:38 +02:00
public function scandir($path) {
2015-07-11 03:59:39 +02:00
if ($arr = scandir($path)) {
$arr = array_values(array_filter($arr, function($el) {
return !($el === "." || $el === "..");
}));
clearstatcache(true, $path);
return new Success($arr);
} else {
return new Failure(new \RuntimeException(
"Failed reading contents from {$path}"
));
}
}
/**
* {@inheritdoc}
*/
2015-07-17 16:27:38 +02:00
public function chmod($path, $mode) {
2015-07-11 03:59:39 +02:00
return new Success((bool) chmod($path, $mode));
}
/**
* {@inheritdoc}
*/
2015-07-17 16:27:38 +02:00
public function chown($path, $uid, $gid) {
2015-07-11 03:59:39 +02:00
if (!@chown($path, $uid)) {
return new Failure(new \RuntimeException(
error_get_last()["message"]
));
} elseif (!@chgrp($path, $gid)) {
return new Failure(new \RuntimeException(
error_get_last()["message"]
));
} else {
return new Success;
}
}
/**
* {@inheritdoc}
*/
2015-07-17 16:27:38 +02:00
public function get($path) {
2015-07-11 03:59:39 +02:00
$result = @file_get_contents($path);
return ($result === false)
? new Failure(new \RuntimeException(error_get_last()["message"]))
: new Success($result)
;
}
/**
* {@inheritdoc}
*/
2015-07-17 16:27:38 +02:00
public function put($path, $contents) {
2015-07-11 03:59:39 +02:00
$result = @file_put_contents($path, $contents);
return ($result === false)
? new Failure(new \RuntimeException(error_get_last()["message"]))
: new Success($result)
;
}
}