|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Archivr\IndexMerger; |
|
4
|
|
|
|
|
5
|
|
|
use Archivr\ConflictHandler\ConflictHandlerInterface; |
|
6
|
|
|
use Archivr\ConflictHandler\PanickingConflictHandler; |
|
7
|
|
|
use Archivr\Index; |
|
8
|
|
|
use Archivr\IndexObject; |
|
9
|
|
|
|
|
10
|
|
|
class StandardIndexMerger implements IndexMergerInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var ConflictHandlerInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $conflictHandler; |
|
16
|
|
|
|
|
17
|
|
|
public function setConflictHandler(ConflictHandlerInterface $conflictHandler = null): IndexMergerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
$this->conflictHandler = $conflictHandler; |
|
20
|
|
|
|
|
21
|
|
|
return $this; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function getConflictHandler(): ConflictHandlerInterface |
|
25
|
|
|
{ |
|
26
|
|
|
if ($this->conflictHandler === null) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->setConflictHandler(new PanickingConflictHandler()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return $this->conflictHandler; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function merge(Index $localIndex, Index $lastLocalIndex = null, Index $remoteIndex = null): Index |
|
35
|
|
|
{ |
|
36
|
|
|
$mergedIndex = new Index(); |
|
37
|
|
|
$conflictHandler = $this->getConflictHandler(); |
|
38
|
|
|
|
|
39
|
|
|
// build new index from local index |
|
40
|
|
|
foreach ($localIndex as $localObject) |
|
41
|
|
|
{ |
|
42
|
|
|
/** @var IndexObject $localObject */ |
|
43
|
|
|
|
|
44
|
|
|
$remoteObject = $remoteIndex ? $remoteIndex->getObjectByPath($localObject->getRelativePath()) : null; |
|
45
|
|
|
|
|
46
|
|
|
$localObjectModified = $lastLocalIndex ? ($localObject->getMtime() > $lastLocalIndex->getCreated()->getTimestamp()) : true; |
|
47
|
|
|
|
|
48
|
|
|
if ($remoteObject === null) |
|
49
|
|
|
{ |
|
50
|
|
|
if ($localObjectModified) |
|
51
|
|
|
{ |
|
52
|
|
|
$mergedIndex->addObject($localObject); |
|
53
|
|
|
} |
|
54
|
|
|
elseif ($remoteIndex === null) |
|
55
|
|
|
{ |
|
56
|
|
|
$mergedIndex->addObject($localObject); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
else |
|
60
|
|
|
{ |
|
61
|
|
|
$remoteObjectModified = $lastLocalIndex ? ($remoteObject->getMtime() > $lastLocalIndex->getCreated()->getTimestamp()) : false; |
|
62
|
|
|
|
|
63
|
|
|
if (!$localObjectModified) |
|
64
|
|
|
{ |
|
65
|
|
|
$mergedIndex->addObject($remoteObject); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
elseif (!$remoteObjectModified) |
|
69
|
|
|
{ |
|
70
|
|
|
$mergedIndex->addObject($localObject); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
else |
|
74
|
|
|
{ |
|
75
|
|
|
$conflictHandler->handleConflict($remoteObject, $localObject, $lastLocalIndex ? $lastLocalIndex->getObjectByPath($localObject->getRelativePath()) : null); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if ($remoteIndex !== null) |
|
81
|
|
|
{ |
|
82
|
|
|
// add remote index content |
|
83
|
|
|
foreach ($remoteIndex as $remoteObject) |
|
84
|
|
|
{ |
|
85
|
|
|
/** @var IndexObject $remoteObject */ |
|
86
|
|
|
|
|
87
|
|
|
$localObject = $localIndex->getObjectByPath($remoteObject->getRelativePath()); |
|
88
|
|
|
$lastLocalObject = $lastLocalIndex ? $lastLocalIndex->getObjectByPath($remoteObject->getRelativePath()) : null; |
|
89
|
|
|
|
|
90
|
|
|
if ($localObject === null) |
|
91
|
|
|
{ |
|
92
|
|
|
if ($lastLocalObject === null) |
|
93
|
|
|
{ |
|
94
|
|
|
$mergedIndex->addObject($remoteObject); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $mergedIndex; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|