-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathFile.php
More file actions
174 lines (153 loc) · 5.95 KB
/
File.php
File metadata and controls
174 lines (153 loc) · 5.95 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace React\Filesystem\Uv;
use React\EventLoop\ExtUvLoop;
use React\EventLoop\Loop;
use React\Filesystem\Node\FileInterface;
use React\Filesystem\PollInterface;
use React\Promise\Promise;
use React\Promise\PromiseInterface;
use UV;
final class File implements FileInterface
{
// private const READ_CHUNK_FIZE = 65536;
private const READ_CHUNK_FIZE = 1;
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 $this->openFile(
$this->path . DIRECTORY_SEPARATOR . $this->name,
UV::O_RDONLY,
0,
)->then(
function ($fileDescriptor) use ($offset, $maxlen): PromiseInterface {
$buffer = '';
$bufferLength = 0;
$read = function (bool $finalAttempt, int $offset) use ($fileDescriptor, $maxlen, &$read, &$buffer, &$bufferLength): PromiseInterface {
return new Promise (function (callable $resolve) use ($fileDescriptor, $offset, $maxlen, $finalAttempt, &$read, &$buffer, &$bufferLength): void {
\uv_fs_read($this->uvLoop, $fileDescriptor, $offset, $maxlen ?? self::READ_CHUNK_FIZE, function ($fileDescriptor, string $contents) use ($resolve, $maxlen, $finalAttempt, &$read, &$buffer, &$bufferLength): void {
$contentLength = strlen($contents);
$buffer .= $contents;
$bufferLength += $contentLength;
if (
($maxlen === null && $finalAttempt) ||
($maxlen !== null && $bufferLength >= $maxlen)
) {
if ($maxlen !== null && $bufferLength > $maxlen) {
$buffer = substr($buffer, 0, $maxlen);
}
$resolve($this->closeOpenFile($fileDescriptor)->then(function () use ($buffer): string {
$this->deactivate();
return $buffer;
}));
} else if ($maxlen === null && !$finalAttempt && $contentLength === 0) {
$resolve($read(true, $bufferLength));
} else {
$resolve($read(false, $bufferLength));
}
});
});
};
return $read(false, $offset);
}
);
}
public function putContents(string $contents, int $flags = 0)
{
$this->activate();
return $this->openFile(
$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,
0644,
)->then(
function ($fileDescriptor) use ($contents): PromiseInterface {
return new Promise (function (callable $resolve) use ($contents, $fileDescriptor): void {
uv_fs_write($this->uvLoop, $fileDescriptor, $contents, 0, function ($fileDescriptor, int $bytesWritten) use ($resolve): void {
$resolve($this->closeOpenFile($fileDescriptor)->then(function () use ($bytesWritten): int {
$this->deactivate();
return $bytesWritten;
}));
});
}
);
});
}
private function openFile(string $path, int $flags, int $mode): PromiseInterface
{
$this->activate();
return new Promise(function (callable $resolve) use ($path, $flags, $mode): void {
uv_fs_open(
$this->uvLoop,
$this->path . DIRECTORY_SEPARATOR . $this->name,
$flags,
$mode,
function ($fileDescriptor) use ($resolve): void {
$this->deactivate();
$resolve($fileDescriptor);
}
);
});
}
private function closeOpenFile($fileDescriptor): PromiseInterface
{
$this->activate();
return new Promise(function (callable $resolve) use ($fileDescriptor) {
try {
uv_fs_close($this->uvLoop, $fileDescriptor, function () use ($resolve) {
$this->deactivate();
$resolve();
});
} catch (\Throwable $error) {
$this->deactivate();
throw $error;
}
});
}
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();
}
}