-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathFile.php
More file actions
109 lines (95 loc) · 3.46 KB
/
File.php
File metadata and controls
109 lines (95 loc) · 3.46 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace React\Filesystem\Uv;
use React\EventLoop\ExtUvLoop;
use React\Filesystem\Node\FileInterface;
use React\Filesystem\PollInterface;
use React\Promise\Promise;
use React\Promise\PromiseInterface;
use UV;
final class File implements FileInterface
{
use StatTrait;
private ExtUvLoop $loop;
private $uvLoop;
private PollInterface $poll;
private string $path;
private string $name;
public function __construct(PollInterface $poll, ExtUvLoop $loop, string $path, string $name)
{
$this->poll = $poll;
$this->loop = $loop;
$this->uvLoop = $loop->getUvLoop();
$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
{
$this->activate();
return new Promise(function (callable $resolve) use ($offset, $maxlen): void {
uv_fs_open($this->uvLoop, $this->path . DIRECTORY_SEPARATOR . $this->name, UV::O_RDONLY, 0, function ($fileDescriptor) use ($resolve, $offset, $maxlen): void {
uv_fs_fstat($this->uvLoop, $fileDescriptor, function ($fileDescriptor, array $stat) use ($resolve, $offset, $maxlen): void {
uv_fs_read($this->uvLoop, $fileDescriptor, $offset, $maxlen ?? (int)$stat['size'], function ($fileDescriptor, string $buffer) use ($resolve): void {
$resolve($buffer);
uv_fs_close($this->uvLoop, $fileDescriptor, function () {
$this->deactivate();
});
});
});
});
});
}
public function putContents(string $contents, int $flags = 0)
{
$this->activate();
return new Promise(function (callable $resolve) use ($contents, $flags): void {
uv_fs_open(
$this->uvLoop,
$this->path . DIRECTORY_SEPARATOR . $this->name,
(($flags & \FILE_APPEND) == \FILE_APPEND) ? UV::O_RDWR | UV::O_CREAT | UV::O_APPEND : UV::O_RDWR | UV::O_CREAT | UV::O_TRUNC,
0644,
function ($fileDescriptor) use ($resolve, $contents, $flags): void {
uv_fs_write($this->uvLoop, $fileDescriptor, $contents, 0, function ($fileDescriptor, int $bytesWritten) use ($resolve): void {
$resolve($bytesWritten);
uv_fs_close($this->uvLoop, $fileDescriptor, function () {
$this->deactivate();
});
});
}
);
});
}
public function unlink(): PromiseInterface
{
$this->activate();
return new Promise(function (callable $resolve): void {
uv_fs_unlink($this->uvLoop, $this->path . DIRECTORY_SEPARATOR . $this->name, function () use ($resolve): void {
$resolve(true);
$this->deactivate();
});
});
}
public function path(): string
{
return $this->path;
}
public function name(): string
{
return $this->name;
}
protected function uvLoop()
{
return $this->uvLoop;
}
protected function activate(): void
{
$this->poll->activate();
}
protected function deactivate(): void
{
$this->poll->deactivate();
}
}