Directory.php
1.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
<?php
declare(strict_types=1);
namespace ACP\Storage;
use ACP\Exception\FailedToCreateDirectoryException;
use DirectoryIterator;
use SplFileInfo;
final class Directory
{
private $info;
public function __construct(string $path)
{
$this->info = new SplFileInfo($path);
}
public function exists(): bool
{
return $this->info->getRealPath() !== false;
}
public function is_readable(): bool
{
return $this->info->isReadable();
}
public function get_info(): SplFileInfo
{
return $this->info;
}
/**
* @throws FailedToCreateDirectoryException
*/
public function create(): void
{
if ($this->exists()) {
return;
}
// Recursively try to set permissions to 0755 unless the system has wider permissions
$result = @mkdir($this->info->getPathname(), (fileperms(ABSPATH) & 0777 | 0755), true);
if ( ! $result) {
throw new FailedToCreateDirectoryException($this->info->getPathname());
}
}
public function has_path(string $path): bool
{
return false !== strpos($this->get_path(), $path);
}
/**
* Proxy method to get the (real) path from the directory
*/
public function get_path(): string
{
return $this->info->getRealPath() ?: '';
}
public function get_iterator(): DirectoryIterator
{
return new DirectoryIterator($this->get_path());
}
}