-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathDirectory.php
More file actions
75 lines (62 loc) · 1.94 KB
/
Directory.php
File metadata and controls
75 lines (62 loc) · 1.94 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
<?php
namespace React\Filesystem\ChildProcess;
use React\EventLoop\Loop;
use React\Filesystem\AdapterInterface;
use React\Filesystem\Node;
use React\Promise\PromiseInterface;
use function React\Promise\all;
use function WyriHaximus\React\childProcessPromiseClosure;
/**
* @internal
*/
final class Directory implements Node\DirectoryInterface
{
use StatTrait;
private AdapterInterface $filesystem;
private string $path;
private string $name;
public function __construct(AdapterInterface $filesystem, string $path, string $name)
{
$this->filesystem = $filesystem;
$this->path = $path;
$this->name = $name;
}
public function stat(): PromiseInterface
{
return $this->internalStat($this->path . $this->name);
}
public function ls(): PromiseInterface
{
$path = $this->path . $this->name;
return childProcessPromiseClosure(Loop::get(), function () use ($path): array {
return scandir($path);
})->then( function (array $contents): PromiseInterface {
$promises = [];
foreach ($contents as $node) {
if (in_array($node, ['.', '..'])) {
continue;
}
$promises[] = $this->filesystem->detect($this->path . $this->name . DIRECTORY_SEPARATOR . $node);
}
return all($promises);
});
}
public function unlink(): PromiseInterface
{
$path = $this->path . $this->name;
return childProcessPromiseClosure(Loop::get(), function () use ($path): array {
if (count(scandir($path)) > 0) {
return ['unlinked' => false];
}
return ['unlinked' => rmdir($path)];
})->then(static fn (array $data) => (bool)$data['unlinked']);
}
public function path(): string
{
return $this->path;
}
public function name(): string
{
return $this->name;
}
}