-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathDirectory.php
More file actions
106 lines (90 loc) · 3.35 KB
/
Directory.php
File metadata and controls
106 lines (90 loc) · 3.35 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
<?php
namespace React\Filesystem\Eio;
use React\Filesystem\AdapterInterface;
use React\Filesystem\Node;
use React\Filesystem\PollInterface;
use React\Promise\Promise;
use React\Promise\PromiseInterface;
use function React\Promise\all;
final class Directory implements Node\DirectoryInterface
{
use StatTrait;
private PollInterface $poll;
private AdapterInterface $filesystem;
private string $path;
private string $name;
public function __construct(PollInterface $poll, AdapterInterface $filesystem, string $path, string $name)
{
$this->poll = $poll;
$this->filesystem = $filesystem;
$this->path = $path;
$this->name = $name;
}
public function stat(): PromiseInterface
{
return $this->internalStat($this->path . $this->name);
}
public function ls(): PromiseInterface
{
$this->activate();
return new Promise(function (callable $resolve, callable $reject): void {
\eio_readdir($this->path . $this->name . DIRECTORY_SEPARATOR, \EIO_READDIR_STAT_ORDER | \EIO_READDIR_DIRS_FIRST, \EIO_PRI_DEFAULT, function ($_, $contents, $resource) use ($resolve, $reject): void {
$this->deactivate();
$list = [];
if ($contents === -1) {
$reject(new \RuntimeException('Error reading from directory "' . $this->path . $this->name . DIRECTORY_SEPARATOR . '": ' . \eio_get_last_error($resource)));
return;
}
foreach ($contents['dents'] as $node) {
$fullPath = $this->path . $this->name . DIRECTORY_SEPARATOR . $node['name'];
switch ($node['type'] ?? null) {
case EIO_DT_DIR:
$list[] = $this->filesystem->directory($fullPath);
break;
case EIO_DT_REG :
$list[] = $this->filesystem->file($fullPath);
break;
default:
$list[] = $this->filesystem->detect($this->path . $this->name . DIRECTORY_SEPARATOR . $node['name']);
break;
}
}
$resolve(all($list));
});
});
}
public function unlink(): PromiseInterface
{
$this->activate();
return new Promise(function (callable $resolve): void {
\eio_readdir($this->path . $this->name . DIRECTORY_SEPARATOR, \EIO_READDIR_STAT_ORDER | \EIO_READDIR_DIRS_FIRST, \EIO_PRI_DEFAULT, function ($_, $contents) use ($resolve): void {
$this->deactivate();
if (count($contents['dents']) > 0) {
$resolve(false);
return;
}
$this->activate();
\eio_rmdir($this->path . $this->name, 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();
}
}