|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of fswatch |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Slick\FsWatch; |
|
13
|
|
|
|
|
14
|
|
|
use Slick\FsWatch\Directory\Snapshot; |
|
15
|
|
|
use Slick\FsWatch\Exception\DirectoryNotAccecible; |
|
16
|
|
|
use Slick\FsWatch\Exception\DirectoryNotFound; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Directory |
|
20
|
|
|
* |
|
21
|
|
|
* @package Slick\FsWatch |
|
22
|
|
|
*/ |
|
23
|
|
|
class Directory |
|
24
|
|
|
{ |
|
25
|
|
|
private FileTools $fileTools; |
|
26
|
|
|
/** |
|
27
|
|
|
* Creates a Directory |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $path |
|
30
|
|
|
* @throws DirectoryNotFound|DirectoryNotAccecible When directory is not accessible |
|
31
|
|
|
*/ |
|
32
|
12 |
|
public function __construct(private string $path) |
|
33
|
|
|
{ |
|
34
|
12 |
|
$this->fileTools = new FileTools(); |
|
35
|
12 |
|
$this->path = $this->fileTools->normalizePath($this->path); |
|
36
|
12 |
|
if (!is_dir($this->path)) { |
|
37
|
1 |
|
throw new DirectoryNotFound("Directory $this->path does not exist."); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
11 |
|
if (!is_readable($this->path)) { |
|
41
|
1 |
|
throw new DirectoryNotAccecible("Directory $this->path is not readable."); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Returns the directory path. |
|
47
|
|
|
* |
|
48
|
|
|
* @return string The path. |
|
49
|
|
|
*/ |
|
50
|
8 |
|
public function path(): string |
|
51
|
|
|
{ |
|
52
|
8 |
|
return rtrim($this->path, '/'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Creates a size map of all directories and files within the specified directory. |
|
57
|
|
|
* |
|
58
|
|
|
* @return array<string, mixed> Array containing the size information of directories and files |
|
59
|
|
|
*/ |
|
60
|
7 |
|
public function sizeMap(): array |
|
61
|
|
|
{ |
|
62
|
7 |
|
return $this->map($this->path); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Recursively maps a directory and its content sizes. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $path The path to the directory. |
|
69
|
|
|
* @return array<string, mixed> The mapped directory with its contents. |
|
70
|
|
|
*/ |
|
71
|
7 |
|
private function map(string $path): array |
|
72
|
|
|
{ |
|
73
|
7 |
|
$result = []; |
|
74
|
7 |
|
$path = $this->fileTools->normalizePath($path); |
|
75
|
7 |
|
$handle = opendir($path); |
|
76
|
7 |
|
while ($handle && false !== ($file = readdir($handle))) { |
|
77
|
7 |
|
if ($file === '.' || $file === '..') { |
|
78
|
7 |
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
7 |
|
if (is_dir($path . $file) === true) { |
|
82
|
7 |
|
$result[$file] = $this->map($path . $file); |
|
83
|
7 |
|
continue; |
|
84
|
|
|
} |
|
85
|
7 |
|
$result[$file] = $this->fileTools->calculateSize($path . $file); |
|
86
|
|
|
} |
|
87
|
7 |
|
unset($handle); |
|
88
|
7 |
|
return $result; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
5 |
|
public function snapshot(): Snapshot |
|
92
|
|
|
{ |
|
93
|
5 |
|
return new Snapshot($this); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
public function hasChanged(Snapshot $snapshot): bool |
|
97
|
|
|
{ |
|
98
|
2 |
|
return $this->snapshot()->hash() !== $snapshot->hash(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|