|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Storeman; |
|
4
|
|
|
|
|
5
|
|
|
use League\Container\Exception\NotFoundException; |
|
6
|
|
|
use Psr\Container\ContainerInterface; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use Storeman\Config\Configuration; |
|
9
|
|
|
use Storeman\Config\ConfigurationException; |
|
10
|
|
|
|
|
11
|
|
|
final class VaultContainer implements ContainerInterface, \Countable, \IteratorAggregate |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Vault[] |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $vaults = []; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(Configuration $configuration, Storeman $storeman, LoggerInterface $logger) |
|
19
|
|
|
{ |
|
20
|
|
|
foreach ($configuration->getVaults() as $vaultConfiguration) |
|
21
|
|
|
{ |
|
22
|
|
|
$vault = new Vault($storeman, $vaultConfiguration); |
|
23
|
|
|
$vault->setLogger($logger); |
|
24
|
|
|
|
|
25
|
|
|
if (array_key_exists($vault->getHash(), $this->vaults)) |
|
26
|
|
|
{ |
|
27
|
|
|
$existingVault = $this->vaults[$vault->getHash()]; |
|
28
|
|
|
|
|
29
|
|
|
assert($existingVault instanceof Vault); |
|
30
|
|
|
|
|
31
|
|
|
throw new ConfigurationException("Duplicate vault hash for vaults '{$vault->getIdentifier()}' and '{$existingVault->getIdentifier()}'"); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$this->vaults[$vault->getHash()] = $vault; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// ensure consistent iterator order |
|
38
|
|
|
ksort($this->vaults); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns a vault by its title. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $title |
|
45
|
|
|
* @return Vault |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getVaultByTitle(string $title): ?Vault |
|
48
|
|
|
{ |
|
49
|
|
|
foreach ($this as $vault) |
|
50
|
|
|
{ |
|
51
|
|
|
/** @var Vault $vault */ |
|
52
|
|
|
|
|
53
|
|
|
if ($vault->getVaultConfiguration()->getTitle() === $title) |
|
54
|
|
|
{ |
|
55
|
|
|
return $vault; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns a subset of the configured vaults identified by the given set of titles. |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $titles |
|
66
|
|
|
* @return Vault[] |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getVaultsByTitles(array $titles): array |
|
69
|
|
|
{ |
|
70
|
|
|
return array_filter(array_map(function(string $title) { |
|
71
|
|
|
|
|
72
|
|
|
return $this->getVaultByTitle($title); |
|
73
|
|
|
|
|
74
|
|
|
}, $titles)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns all those vaults that have a given revision. |
|
79
|
|
|
* |
|
80
|
|
|
* @param int $revision |
|
81
|
|
|
* @return Vault[] |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getVaultsHavingRevision(int $revision): array |
|
84
|
|
|
{ |
|
85
|
|
|
return array_filter($this->vaults, function(Vault $vault) use ($revision) { |
|
86
|
|
|
|
|
87
|
|
|
return $vault->getVaultLayout()->getSynchronizations()->getSynchronization($revision) !== null; |
|
88
|
|
|
}); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns the vault with the highest priority. |
|
93
|
|
|
* |
|
94
|
|
|
* @param Vault[] $vaults Vaults to consider. Defaults to all configured vaults. |
|
95
|
|
|
* @return Vault |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getPrioritizedVault(array $vaults = null): ?Vault |
|
98
|
|
|
{ |
|
99
|
|
|
/** @var Vault[] $vaults */ |
|
100
|
|
|
$vaults = ($vaults === null) ? $this : $vaults; |
|
101
|
|
|
|
|
102
|
|
|
/** @var Vault $return */ |
|
103
|
|
|
$return = null; |
|
104
|
|
|
|
|
105
|
|
|
foreach ($vaults as $vault) |
|
106
|
|
|
{ |
|
107
|
|
|
if ($return === null || $return->getVaultConfiguration()->getPriority() < $vault->getVaultConfiguration()->getPriority()) |
|
108
|
|
|
{ |
|
109
|
|
|
$return = $vault; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $return; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
|
|
public function has($id) |
|
120
|
|
|
{ |
|
121
|
|
|
return array_key_exists($id, $this->vaults); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
|
|
public function get($id) |
|
128
|
|
|
{ |
|
129
|
|
|
if (array_key_exists($id, $this->vaults)) |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->vaults[$id]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
throw new NotFoundException(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritdoc} |
|
139
|
|
|
*/ |
|
140
|
|
|
public function count() |
|
141
|
|
|
{ |
|
142
|
|
|
return count($this->vaults); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* {@inheritdoc} |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getIterator() |
|
149
|
|
|
{ |
|
150
|
|
|
return new \ArrayIterator(array_values($this->vaults)); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|