1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Archivr; |
4
|
|
|
|
5
|
|
|
use Archivr\Exception\Exception; |
6
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata; |
7
|
|
|
use Zend\Stdlib\ArraySerializableInterface; |
8
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
9
|
|
|
|
10
|
|
|
class Configuration implements ArraySerializableInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The local base path of the archive. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $path; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Set of excluded paths. |
21
|
|
|
* |
22
|
|
|
* @var string[] |
23
|
|
|
*/ |
24
|
|
|
protected $exclude = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Identity to be visible in synchronization log. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $identity; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Map of vault configurations by identifier. |
35
|
|
|
* |
36
|
|
|
* @var VaultConfiguration[] |
37
|
|
|
*/ |
38
|
|
|
protected $vaults = []; |
39
|
|
|
|
40
|
|
|
public function __construct(string $localPath = './') |
41
|
|
|
{ |
42
|
|
|
$this->setPath($localPath); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function getPath(): string |
49
|
|
|
{ |
50
|
|
|
return $this->path; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $path |
55
|
|
|
* |
56
|
|
|
* @return Configuration |
57
|
|
|
*/ |
58
|
|
|
public function setPath(string $path): Configuration |
59
|
|
|
{ |
60
|
|
|
if (substr($path, -1) !== DIRECTORY_SEPARATOR) |
61
|
|
|
{ |
62
|
|
|
$path .= DIRECTORY_SEPARATOR; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->path = $path; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return \string[] |
72
|
|
|
*/ |
73
|
|
|
public function getExclude(): array |
74
|
|
|
{ |
75
|
|
|
return $this->exclude; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param \string[] $paths |
80
|
|
|
* |
81
|
|
|
* @return Configuration |
82
|
|
|
*/ |
83
|
|
|
public function setExclude(array $paths): Configuration |
84
|
|
|
{ |
85
|
|
|
$this->exclude = array_values($paths); |
|
|
|
|
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $path |
92
|
|
|
* |
93
|
|
|
* @return Configuration |
94
|
|
|
*/ |
95
|
|
|
public function addExclusion(string $path): Configuration |
96
|
|
|
{ |
97
|
|
|
$this->exclude[] = $path; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getIdentity() |
106
|
|
|
{ |
107
|
|
|
return $this->identity; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $identity |
112
|
|
|
* |
113
|
|
|
* @return Configuration |
114
|
|
|
*/ |
115
|
|
|
public function setIdentity(string $identity): Configuration |
116
|
|
|
{ |
117
|
|
|
$this->identity = $identity; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return VaultConfiguration[] |
124
|
|
|
*/ |
125
|
|
|
public function getVaults(): array |
126
|
|
|
{ |
127
|
|
|
return $this->vaults; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $title |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function hasVault(string $title): bool |
135
|
|
|
{ |
136
|
|
|
return isset($this->vaults[$title]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $title |
141
|
|
|
* |
142
|
|
|
* @return VaultConfiguration |
143
|
|
|
*/ |
144
|
|
|
public function getVaultByTitle(string $title) |
145
|
|
|
{ |
146
|
|
|
if (!isset($this->vaults[$title])) |
147
|
|
|
{ |
148
|
|
|
throw new \InvalidArgumentException("Unknown vault configuration requested: {$title}"); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->vaults[$title]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param VaultConfiguration $configuration |
156
|
|
|
* |
157
|
|
|
* @return Configuration |
158
|
|
|
* @throws Exception |
159
|
|
|
*/ |
160
|
|
|
public function addVault(VaultConfiguration $configuration): Configuration |
161
|
|
|
{ |
162
|
|
|
if (isset($this->vaults[$configuration->getTitle()])) |
163
|
|
|
{ |
164
|
|
|
throw new Exception(sprintf('Trying to add vault with duplicate title %s.', $configuration->getTitle())); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$this->vaults[$configuration->getTitle()] = $configuration; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public function exchangeArray(array $array) |
176
|
|
|
{ |
177
|
|
View Code Duplication |
if ($diff = array_diff(array_keys($array), array_keys(get_object_vars($this)))) |
|
|
|
|
178
|
|
|
{ |
179
|
|
|
throw new \InvalidArgumentException("Invalid index(es): " . implode(',', $diff)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
foreach ($array as $key => $value) |
183
|
|
|
{ |
184
|
|
|
if ($key === 'vaults') |
185
|
|
|
{ |
186
|
|
|
if (!is_array($value)) |
187
|
|
|
{ |
188
|
|
|
throw new \InvalidArgumentException(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->vaults = []; |
192
|
|
|
|
193
|
|
|
foreach ($value as $val) |
194
|
|
|
{ |
195
|
|
|
if (!is_array($val)) |
196
|
|
|
{ |
197
|
|
|
throw new \InvalidArgumentException(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$vaultConfig = new VaultConfiguration(); |
201
|
|
|
$vaultConfig->exchangeArray($val); |
202
|
|
|
|
203
|
|
|
$this->addVault($vaultConfig); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
else |
207
|
|
|
{ |
208
|
|
|
// using setter to prevent skipping validation |
209
|
|
|
call_user_func([$this, 'set' . ucfirst($key)], $value); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* {@inheritdoc} |
216
|
|
|
*/ |
217
|
|
|
public function getArrayCopy() |
218
|
|
|
{ |
219
|
|
|
$return = get_object_vars($this); |
220
|
|
|
$return['vaults'] = array_values(array_map(function(VaultConfiguration $vaultConfiguration) { |
221
|
|
|
|
222
|
|
|
return $vaultConfiguration->getArrayCopy(); |
223
|
|
|
|
224
|
|
|
}, $this->vaults)); |
225
|
|
|
|
226
|
|
|
return $return; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public static function loadValidatorMetadata(ClassMetadata $metadata) |
230
|
|
|
{ |
231
|
|
|
$metadata->addPropertyConstraint('path', new Assert\NotBlank()); |
232
|
|
|
$metadata->addPropertyConstraint('identity', new Assert\NotBlank()); |
233
|
|
|
$metadata->addPropertyConstraint('vaults', new Assert\Count(['min' => 1])); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..