-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathFile.php
More file actions
71 lines (59 loc) · 2.07 KB
/
File.php
File metadata and controls
71 lines (59 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace React\Filesystem\ChildProcess;
use React\EventLoop\Loop;
use React\Filesystem\Node\FileInterface;
use React\Promise\PromiseInterface;
use function WyriHaximus\React\childProcessPromiseClosure;
/**
* @internal
*/
final class File implements FileInterface
{
use StatTrait;
private string $path;
private string $name;
public function __construct(string $path, string $name)
{
$this->path = $path;
$this->name = $name;
}
public function stat(): PromiseInterface
{
return $this->internalStat($this->path . $this->name);
}
public function getContents(int $offset = 0 , ?int $maxlen = null): PromiseInterface
{
$path = $this->path . $this->name;
return childProcessPromiseClosure(Loop::get(), function () use ($path, $offset, $maxlen): array {
return ['contents' => file_get_contents($path, false, null, $offset, $maxlen ?? (int)stat($path)['size'])];
})->then(static fn (array $data): string => $data['contents']);
}
public function putContents(string $contents, int $flags = 0): PromiseInterface
{
// Making sure we only pass in one flag for security reasons
if (($flags & \FILE_APPEND) == \FILE_APPEND) {
$flags = \FILE_APPEND;
} else {
$flags = 0;
}
$path = $this->path . $this->name;
return childProcessPromiseClosure(Loop::get(), function () use ($path, $contents, $flags): array {
return ['size_written' => file_put_contents($path, $contents, $flags)];
})->then(static fn (array $data) => (int)$data['size_written']);
}
public function unlink(): PromiseInterface
{
$path = $this->path . $this->name;
return childProcessPromiseClosure(Loop::get(), function () use ($path): array {
return ['unlinked' => unlink($path)];
})->then(static fn (array $data) => (bool)$data['unlinked']);
}
public function path(): string
{
return $this->path;
}
public function name(): string
{
return $this->name;
}
}