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\Directory; |
13
|
|
|
|
14
|
|
|
use Slick\FsWatch\Directory; |
15
|
|
|
use Slick\FsWatch\FileTools; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Snapshot |
19
|
|
|
* |
20
|
|
|
* @package Slick\FsWatch\Directory |
21
|
|
|
*/ |
22
|
|
|
final class Snapshot |
23
|
|
|
{ |
24
|
|
|
/** @var array<string, mixed> */ |
25
|
|
|
protected array $sizeMap; |
26
|
|
|
private string $path; |
27
|
|
|
|
28
|
|
|
private FileTools $fileTools; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Creates a Snapshot |
32
|
|
|
* |
33
|
|
|
* @param Directory $directory |
34
|
|
|
*/ |
35
|
11 |
|
public function __construct(Directory $directory) |
36
|
|
|
{ |
37
|
11 |
|
$this->sizeMap = $directory->sizeMap(); |
38
|
11 |
|
$this->path = $directory->path(); |
39
|
11 |
|
$this->fileTools = new FileTools(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Directory content size hash |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
5 |
|
public function hash(): string |
48
|
|
|
{ |
49
|
5 |
|
return sha1(serialize($this->sizeMap)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Directory path |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
2 |
|
public function path(): string |
58
|
|
|
{ |
59
|
2 |
|
return $this->path; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return array<string, mixed> |
64
|
|
|
*/ |
65
|
3 |
|
public function __serialize(): array |
66
|
|
|
{ |
67
|
3 |
|
return [ |
68
|
3 |
|
"path" => $this->path, |
69
|
3 |
|
"size" => $this->sizeMap, |
70
|
3 |
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array<string, mixed> $data |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
3 |
|
public function __unserialize(array $data): void |
78
|
|
|
{ |
79
|
3 |
|
$this->path = $data['path']; |
80
|
3 |
|
$this->sizeMap = $data['size']; |
81
|
3 |
|
$this->fileTools = new FileTools(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Compares the current Snapshot with another Snapshot and determines the changes that occurred. |
86
|
|
|
* |
87
|
|
|
* @param Snapshot $secondSnapshot The second Snapshot to compare with. |
88
|
|
|
* @return Changes The changes that occurred between the two Snapshots. |
89
|
|
|
*/ |
90
|
3 |
|
public function compareTo(Snapshot $secondSnapshot): Changes |
91
|
|
|
{ |
92
|
3 |
|
$changes = new Changes(); |
93
|
3 |
|
$second = $this->fileTools->flattenSizeMap($secondSnapshot->sizeMap); |
94
|
3 |
|
$first = $this->fileTools->flattenSizeMap($this->sizeMap); |
95
|
|
|
|
96
|
3 |
|
$diff = array_diff_assoc($second, $first); |
97
|
|
|
|
98
|
3 |
|
foreach (array_keys($diff) as $key) { |
99
|
3 |
|
if (array_key_exists($key, $first)) { |
100
|
1 |
|
$changes->add($this->path.'/'.$key, FileChange::CHANGED); |
101
|
1 |
|
continue; |
102
|
|
|
} |
103
|
3 |
|
$changes->add($this->path.'/'.$key, FileChange::ADDED); |
104
|
|
|
} |
105
|
|
|
|
106
|
3 |
|
$diff = array_diff_assoc($first, $second); |
107
|
3 |
|
foreach (array_keys($diff) as $key) { |
108
|
1 |
|
if (!array_key_exists($key, $second)) { |
109
|
1 |
|
$changes->add($this->path.'/'.$key, FileChange::DELETED); |
110
|
|
|
} |
111
|
|
|
} |
112
|
3 |
|
return $changes; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|