|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_menu; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_paths\PathsException; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class MoreEntries |
|
11
|
|
|
* @package kalanis\kw_menu |
|
12
|
|
|
* Process the menu against the file tree |
|
13
|
|
|
* Load more already unloaded entries and remove non-existing ones |
|
14
|
|
|
*/ |
|
15
|
|
|
class MoreEntries |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var string[] */ |
|
18
|
|
|
protected array $groupKey = []; |
|
19
|
|
|
protected MetaProcessor $meta; |
|
20
|
|
|
protected Interfaces\IEntriesSource $dataSource; |
|
21
|
|
|
|
|
22
|
2 |
|
public function __construct(MetaProcessor $metaSource, Interfaces\IEntriesSource $dataSource) |
|
23
|
|
|
{ |
|
24
|
2 |
|
$this->meta = $metaSource; |
|
25
|
2 |
|
$this->dataSource = $dataSource; |
|
26
|
2 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string[] $groupKey directory to scan |
|
30
|
|
|
* @return $this |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function setGroupKey(array $groupKey): self |
|
33
|
|
|
{ |
|
34
|
1 |
|
$this->groupKey = $groupKey; |
|
35
|
1 |
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string[] $metaKey file/id with meta data |
|
40
|
|
|
* @throws MenuException |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function setMeta(array $metaKey): self |
|
44
|
|
|
{ |
|
45
|
2 |
|
$this->meta->setKey($metaKey); |
|
46
|
2 |
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @throws MenuException |
|
51
|
|
|
* @throws PathsException |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function load(): self |
|
55
|
|
|
{ |
|
56
|
2 |
|
if ($this->meta->exists()) { |
|
57
|
1 |
|
$this->meta->load(); |
|
58
|
1 |
|
$this->fillMissing(); |
|
59
|
|
|
} else { |
|
60
|
1 |
|
$this->createNew(); |
|
61
|
|
|
} |
|
62
|
2 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @throws MenuException |
|
67
|
|
|
* @throws PathsException |
|
68
|
|
|
*/ |
|
69
|
1 |
|
protected function createNew(): void |
|
70
|
|
|
{ |
|
71
|
1 |
|
foreach ($this->dataSource->getFiles($this->groupKey) as $file) { |
|
72
|
1 |
|
$this->meta->addEntry($file); |
|
73
|
|
|
} |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @throws MenuException |
|
78
|
|
|
* @throws PathsException |
|
79
|
|
|
*/ |
|
80
|
1 |
|
protected function fillMissing(): void |
|
81
|
|
|
{ |
|
82
|
1 |
|
$toRemoval = array_map([$this, 'entryId'], $this->meta->getWorking()); |
|
83
|
1 |
|
$toRemoval = (array) array_combine($toRemoval, array_fill(0, count($toRemoval), true)); |
|
84
|
|
|
|
|
85
|
1 |
|
foreach ($this->dataSource->getFiles($this->groupKey) as $file) { |
|
86
|
1 |
|
$alreadyKnown = false; |
|
87
|
1 |
|
foreach ($this->meta->getWorking() as $item) { # stay |
|
88
|
1 |
|
if ((!$alreadyKnown) && ($item->getId() == $file)) { |
|
89
|
1 |
|
$alreadyKnown = true; |
|
90
|
1 |
|
$toRemoval[$item->getId()] = false; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
1 |
|
if (!$alreadyKnown) { |
|
94
|
1 |
|
$this->meta->addEntry($file); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
1 |
|
foreach ($this->meta->getWorking() as $item) { |
|
98
|
1 |
|
if (!empty($toRemoval[$item->getId()])) { |
|
99
|
1 |
|
$this->meta->removeEntry($item->getId()); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
1 |
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
public function entryId(Menu\Entry $item): string |
|
105
|
|
|
{ |
|
106
|
1 |
|
return $item->getId(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
2 |
|
public function getMeta(): MetaProcessor |
|
110
|
|
|
{ |
|
111
|
2 |
|
return $this->meta; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|