1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Storeman\Config; |
4
|
|
|
|
5
|
|
|
use Storeman\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
|
|
|
public const VAULT_CONFIG_CLASS = VaultConfiguration::class; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The local base path of the archive. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $path = './'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Set of excluded paths. |
24
|
|
|
* |
25
|
|
|
* @var string[] |
26
|
|
|
*/ |
27
|
|
|
protected $exclude = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Identity to be visible in synchronization log. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $identity = 'unknown'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Identifier of the index builder to use. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $indexBuilder = 'standard'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* List of file checksums to be used for integrity checks and modification detection. |
45
|
|
|
* |
46
|
|
|
* @var string[] |
47
|
|
|
*/ |
48
|
|
|
protected $fileChecksums = ['sha256', 'sha1', 'md5']; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Array of vault configurations. |
52
|
|
|
* |
53
|
|
|
* @var VaultConfiguration[] |
54
|
|
|
*/ |
55
|
|
|
protected $vaults = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getPath(): string |
61
|
|
|
{ |
62
|
|
|
return $this->path; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $path |
67
|
|
|
* |
68
|
|
|
* @return Configuration |
69
|
|
|
*/ |
70
|
|
|
public function setPath(string $path): Configuration |
71
|
|
|
{ |
72
|
|
|
if (substr($path, -1) !== DIRECTORY_SEPARATOR) |
73
|
|
|
{ |
74
|
|
|
$path .= DIRECTORY_SEPARATOR; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->path = $path; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return \string[] |
84
|
|
|
*/ |
85
|
|
|
public function getExclude(): array |
86
|
|
|
{ |
87
|
|
|
return $this->exclude; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param \string[] $paths |
92
|
|
|
* |
93
|
|
|
* @return Configuration |
94
|
|
|
*/ |
95
|
|
|
public function setExclude(array $paths): Configuration |
96
|
|
|
{ |
97
|
|
|
$this->exclude = array_values($paths); |
|
|
|
|
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $path |
104
|
|
|
* |
105
|
|
|
* @return Configuration |
106
|
|
|
*/ |
107
|
|
|
public function addExclusion(string $path): Configuration |
108
|
|
|
{ |
109
|
|
|
$this->exclude[] = $path; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getIdentity(): string |
118
|
|
|
{ |
119
|
|
|
return $this->identity; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $identity |
124
|
|
|
* |
125
|
|
|
* @return Configuration |
126
|
|
|
*/ |
127
|
|
|
public function setIdentity(string $identity): Configuration |
128
|
|
|
{ |
129
|
|
|
$this->identity = $identity; |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function getIndexBuilder(): string |
138
|
|
|
{ |
139
|
|
|
return $this->indexBuilder; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $indexBuilder |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function setIndexBuilder(string $indexBuilder): Configuration |
147
|
|
|
{ |
148
|
|
|
$this->indexBuilder = $indexBuilder; |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return string[] |
155
|
|
|
*/ |
156
|
|
|
public function getFileChecksums(): array |
157
|
|
|
{ |
158
|
|
|
return $this->fileChecksums; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string[] $fileChecksums |
163
|
|
|
* @return Configuration |
164
|
|
|
*/ |
165
|
|
|
public function setFileChecksums(array $fileChecksums): Configuration |
166
|
|
|
{ |
167
|
|
|
$this->fileChecksums = array_values($fileChecksums); |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return VaultConfiguration[] |
174
|
|
|
*/ |
175
|
|
|
public function getVaults(): array |
176
|
|
|
{ |
177
|
|
|
return $this->vaults; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $title |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
|
|
public function hasVault(string $title): bool |
185
|
|
|
{ |
186
|
|
|
return $this->getVaultConfiguration($title) !== null; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $title |
191
|
|
|
* |
192
|
|
|
* @return VaultConfiguration |
193
|
|
|
*/ |
194
|
|
|
public function getVault(string $title): VaultConfiguration |
195
|
|
|
{ |
196
|
|
|
if ($vaultConfiguration = $this->getVaultConfiguration($title)) |
197
|
|
|
{ |
198
|
|
|
return $vaultConfiguration; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
throw new \InvalidArgumentException("Unknown vault configuration requested: {$title}"); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @internal Use VaultConfiguration constructor |
206
|
|
|
* @param VaultConfiguration $configuration |
207
|
|
|
* |
208
|
|
|
* @return Configuration |
209
|
|
|
* @throws Exception |
210
|
|
|
*/ |
211
|
|
|
public function addVault(VaultConfiguration $configuration): Configuration |
212
|
|
|
{ |
213
|
|
|
if (array_search($configuration, $this->vaults) === false) |
214
|
|
|
{ |
215
|
|
|
if ($this->hasVault($configuration->getTitle())) |
216
|
|
|
{ |
217
|
|
|
throw new Exception(sprintf('Trying to add vault with duplicate title %s.', $configuration->getTitle())); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$this->vaults[] = $configuration; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* {@inheritdoc} |
228
|
|
|
*/ |
229
|
|
|
public function exchangeArray(array $array) |
230
|
|
|
{ |
231
|
|
|
if ($diff = array_diff(array_keys($array), array_keys(get_object_vars($this)))) |
232
|
|
|
{ |
233
|
|
|
throw new \InvalidArgumentException("Invalid index(es): " . implode(',', $diff)); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
foreach ($array as $key => $value) |
237
|
|
|
{ |
238
|
|
|
if ($key === 'vaults') |
239
|
|
|
{ |
240
|
|
|
if (!is_array($value)) |
241
|
|
|
{ |
242
|
|
|
throw new \InvalidArgumentException(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$this->vaults = []; |
246
|
|
|
|
247
|
|
|
foreach ($value as $val) |
248
|
|
|
{ |
249
|
|
|
if (!is_array($val)) |
250
|
|
|
{ |
251
|
|
|
throw new \InvalidArgumentException(); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$className = static::VAULT_CONFIG_CLASS; |
255
|
|
|
|
256
|
|
|
/** @var VaultConfiguration $vaultConfig */ |
257
|
|
|
$vaultConfig = new $className($this); |
258
|
|
|
$vaultConfig->exchangeArray($val); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
else |
262
|
|
|
{ |
263
|
|
|
// using setter to prevent skipping validation |
264
|
|
|
call_user_func([$this, 'set' . ucfirst($key)], $value); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* {@inheritdoc} |
271
|
|
|
*/ |
272
|
|
|
public function getArrayCopy() |
273
|
|
|
{ |
274
|
|
|
$return = get_object_vars($this); |
275
|
|
|
$return['vaults'] = array_values(array_map(function(VaultConfiguration $vaultConfiguration) { |
276
|
|
|
|
277
|
|
|
return $vaultConfiguration->getArrayCopy(); |
278
|
|
|
|
279
|
|
|
}, $this->vaults)); |
280
|
|
|
|
281
|
|
|
return $return; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
protected function getVaultConfiguration(string $title): ?VaultConfiguration |
285
|
|
|
{ |
286
|
|
|
foreach ($this->vaults as $vaultConfiguration) |
287
|
|
|
{ |
288
|
|
|
if ($vaultConfiguration->getTitle() === $title) |
289
|
|
|
{ |
290
|
|
|
return $vaultConfiguration; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return null; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public static function loadValidatorMetadata(ClassMetadata $metadata) |
298
|
|
|
{ |
299
|
|
|
$metadata->addPropertyConstraint('path', new Assert\NotBlank()); |
300
|
|
|
$metadata->addPropertyConstraint('identity', new Assert\NotBlank()); |
301
|
|
|
$metadata->addPropertyConstraint('vaults', new Assert\Count(['min' => 1])); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
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..