-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathFile.php
More file actions
158 lines (139 loc) · 5.59 KB
/
File.php
File metadata and controls
158 lines (139 loc) · 5.59 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
<?php
namespace React\Filesystem\Eio;
use React\Filesystem\Node\FileInterface;
use React\Filesystem\PollInterface;
use React\Promise\Promise;
use React\Promise\PromiseInterface;
use function React\Promise\resolve;
final class File implements FileInterface
{
private const READ_CHUNK_FIZE = 65536;
// private const READ_CHUNK_FIZE = 1;
use StatTrait;
private PollInterface $poll;
private string $path;
private string $name;
public function __construct(PollInterface $poll, string $path, string $name)
{
$this->poll = $poll;
$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,
\EIO_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 {
\eio_read($fileDescriptor, $maxlen ?? self::READ_CHUNK_FIZE, $offset, \PHP_INT_MAX, 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) ? \EIO_O_RDWR | \EIO_O_APPEND : \EIO_O_RDWR | \EIO_O_CREAT,
0644
)->then(
function ($fileDescriptor) use ($contents, $flags): PromiseInterface {
return new Promise (function (callable $resolve) use ($contents, $fileDescriptor): void {
\eio_write($fileDescriptor, $contents, strlen($contents), 0, \EIO_PRI_DEFAULT, function ($fileDescriptor, int $bytesWritten) use ($resolve): void {
$resolve($this->closeOpenFile($fileDescriptor)->then(function () use ($bytesWritten): int {
return $bytesWritten;
}));
}, $fileDescriptor);
});
}
);
}
private function openFile(string $path, int $flags, int $mode): PromiseInterface
{
return new Promise(function (callable $resolve, callable $reject) use ($path, $flags, $mode): void {
\eio_open(
$path,
$flags,
$mode,
\EIO_PRI_DEFAULT,
function ($_, $fileDescriptor) use ($resolve): void {
$resolve($fileDescriptor);
}
);
});
}
private function closeOpenFile($fileDescriptor): PromiseInterface
{
return new Promise(function (callable $resolve) use ($fileDescriptor) {
try {
\eio_close($fileDescriptor, \EIO_PRI_DEFAULT, function () use ($resolve): void {
$this->deactivate();
$resolve();
});
} catch (\Throwable $error) {
$this->deactivate();
throw $error;
}
});
}
public function unlink(): PromiseInterface
{
$this->activate();
return new Promise(function (callable $resolve): void {
\eio_unlink($this->path . DIRECTORY_SEPARATOR . $this->name, \EIO_PRI_DEFAULT, function () use ($resolve): void {
$this->deactivate();
$resolve(true);
});
});
}
public function path(): string
{
return $this->path;
}
public function name(): string
{
return $this->name;
}
protected function activate(): void
{
$this->poll->activate();
}
protected function deactivate(): void
{
$this->poll->deactivate();
}
}