1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2011-present Mediasift Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package Storyplayer/ConfigLib |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://datasift.github.io/storyplayer |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace DataSift\Storyplayer\ConfigLib; |
45
|
|
|
|
46
|
|
|
use DataSift\Storyplayer\DefinitionLib\TestEnvironment_Definition; |
47
|
|
|
use DataSift\Stone\ObjectLib\BaseObject; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* tracks a list of available configs of a given type |
51
|
|
|
* |
52
|
|
|
* @category Libraries |
53
|
|
|
* @package Storyplayer/ConfigLib |
54
|
|
|
* @author Stuart Herbert <[email protected]> |
55
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
56
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
57
|
|
|
* @link http://datasift.github.io/storyplayer |
58
|
|
|
*/ |
59
|
|
|
class ConfigList |
60
|
|
|
{ |
61
|
|
|
/** |
62
|
|
|
* what kind of config are we wrapping? |
63
|
|
|
* |
64
|
|
|
* this is the name of a class from ConfigLib |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
private $configType = null; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* where are we looking for these configs? |
72
|
|
|
* |
73
|
|
|
* @var array<string> |
74
|
|
|
*/ |
75
|
|
|
private $searchFolders = null; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* our list of tools to go and find our config files |
79
|
|
|
* |
80
|
|
|
* @var array<ConfigFinder> |
81
|
|
|
*/ |
82
|
|
|
private $configFinders = []; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* what configs (including hard-coded defaults) do we know about? |
86
|
|
|
* |
87
|
|
|
* @var array<SystemUnderTestConfig> |
88
|
|
|
*/ |
89
|
|
|
private $list = []; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* constructor |
93
|
|
|
*/ |
94
|
|
|
public function __construct($configType, $searchFolders, $configFinders) |
95
|
|
|
{ |
96
|
|
|
$this->setConfigType($configType); |
97
|
|
|
$this->setSearchFolders($searchFolders); |
98
|
|
|
$this->setConfigFinders($configFinders); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* returns the folders where we look for config files |
103
|
|
|
* |
104
|
|
|
* @return array<string> |
105
|
|
|
*/ |
106
|
|
|
public function getSearchFolders() |
107
|
|
|
{ |
108
|
|
|
return $this->searchFolders; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* tells us where to look for config files |
113
|
|
|
* |
114
|
|
|
* @param array<string> $searchFolders |
115
|
|
|
* the folders to search inside |
116
|
|
|
*/ |
117
|
|
|
public function setSearchFolders($searchFolders) |
118
|
|
|
{ |
119
|
|
|
if (!is_array($searchFolders)) { |
120
|
|
|
throw new E5xx_ArrayParameterExpected(__METHOD__, '$searchFolders', $searchFolders); |
121
|
|
|
} |
122
|
|
|
$this->searchFolders = $searchFolders; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* get the list of objects that know how to find our config files |
127
|
|
|
* |
128
|
|
|
* @return array<ConfigFinder> |
129
|
|
|
*/ |
130
|
|
|
public function getConfigFinders() |
131
|
|
|
{ |
132
|
|
|
return $this->configFinders; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* tell us how to find the config files |
137
|
|
|
* |
138
|
|
|
* @param array<ConfigFinder> $configFinders |
139
|
|
|
* a list of config finders to use |
140
|
|
|
*/ |
141
|
|
|
public function setConfigFinders($configFinders) |
142
|
|
|
{ |
143
|
|
|
if (!is_array($configFinders)) { |
144
|
|
|
throw new E5xx_ArrayParameterExpected(__METHOD__, '$configFinders', $configFinders); |
145
|
|
|
} |
146
|
|
|
$this->configFinders = $configFinders; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* find a list of config files in a folder, and load them |
151
|
|
|
* |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
public function findConfigs() |
155
|
|
|
{ |
156
|
|
|
// which config files do we need to load? |
157
|
|
|
$filenames = $this->findConfigFilenames(); |
158
|
|
|
|
159
|
|
|
// let's get them loaded |
160
|
|
|
foreach ($filenames as $filename) { |
161
|
|
|
// the file extension determines how we handle it |
162
|
|
|
$ext = pathinfo($filename, PATHINFO_EXTENSION); |
163
|
|
|
switch ($ext) { |
164
|
|
|
case "json": |
165
|
|
|
$config = $this->loadJsonConfig($filename); |
166
|
|
|
break; |
167
|
|
|
case "php": |
168
|
|
|
$config = $this->loadPhpConfig($filename); |
169
|
|
|
break; |
170
|
|
|
default: |
171
|
|
|
// should never happen, but just in case |
172
|
|
|
throw new E5xx_UnsupportedConfigFileExtension($filename); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// at this point, we have a WrappedConfig to add to our list |
176
|
|
|
$this->list[$config->getName()] = $config; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// we want our list of configs to be sorted, to make presentation |
180
|
|
|
// to end-users easier |
181
|
|
|
ksort($this->list); |
182
|
|
|
|
183
|
|
|
// all done |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* load a SPv2.0-style JSON configs |
188
|
|
|
* |
189
|
|
|
* @return object |
190
|
|
|
*/ |
191
|
|
|
protected function loadJsonConfig($filename) |
192
|
|
|
{ |
193
|
|
|
$config = $this->newWrappedConfigObject(); |
194
|
|
|
$config->loadConfigFromFile($filename); |
195
|
|
|
$config->validateConfig(); |
196
|
|
|
|
197
|
|
|
// all done |
198
|
|
|
return $config; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* load a SPv2.3-style PHP config |
203
|
|
|
* |
204
|
|
|
* @todo we need (at some point) to move the check for a return value |
205
|
|
|
* out to somewhere else |
206
|
|
|
* |
207
|
|
|
* @return object |
208
|
|
|
*/ |
209
|
|
|
protected function loadPhpConfig($filename) |
210
|
|
|
{ |
211
|
|
|
$config = require ($filename); |
212
|
|
|
|
213
|
|
|
// make sure we have a definition! |
214
|
|
|
if (!$config instanceof TestEnvironment_Definition) { |
215
|
|
|
throw new E4xx_TestEnvironmentFileMustReturnADefinition($filename); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
// all done |
219
|
|
|
return $config; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* build a list of the config files found by the ConfigFinders |
224
|
|
|
* |
225
|
|
|
* @return array<string> |
226
|
|
|
*/ |
227
|
|
|
protected function findConfigFilenames() |
228
|
|
|
{ |
229
|
|
|
// where are we looking? |
230
|
|
|
$searchFolders = $this->getSearchFolders(); |
231
|
|
|
|
232
|
|
|
// how are we looking? |
233
|
|
|
$configFinders = $this->getConfigFinders(); |
234
|
|
|
|
235
|
|
|
// our return value |
236
|
|
|
$filenames = []; |
237
|
|
|
|
238
|
|
|
foreach ($searchFolders as $searchFolder) { |
239
|
|
|
// do we have somewhere to look? |
240
|
|
|
if (null === $searchFolder || !is_dir($searchFolder)) { |
241
|
|
|
continue; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
// build our list |
245
|
|
|
foreach ($configFinders as $configFinder) { |
246
|
|
|
$filenames = array_merge($filenames, $configFinder->getListOfConfigFilesIn($searchFolder)); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// all done |
251
|
|
|
return $filenames; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* what type of content are we a list of? |
256
|
|
|
* |
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
|
|
public function getConfigType() |
260
|
|
|
{ |
261
|
|
|
return $this->configType; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* tells us what type of content we are going to be a list of |
266
|
|
|
* |
267
|
|
|
* @param string $configType |
268
|
|
|
* the name of a class from ConfigLib |
269
|
|
|
*/ |
270
|
|
|
public function setConfigType($configType) |
271
|
|
|
{ |
272
|
|
|
$this->configType = $configType; |
273
|
|
|
$classname = $this->getWrappedConfigClassname(); |
274
|
|
|
|
275
|
|
|
if (!class_exists($classname)) { |
276
|
|
|
throw new E4xx_NoSuchConfigClass($classname); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* returns a classname you can use for creating objects |
282
|
|
|
* |
283
|
|
|
* @return string |
284
|
|
|
*/ |
285
|
|
|
public function getWrappedConfigClassname() |
286
|
|
|
{ |
287
|
|
|
return $this->configType; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* returns a object of the correct type for the content we are listing |
292
|
|
|
* |
293
|
|
|
* @return object |
294
|
|
|
*/ |
295
|
|
|
public function newWrappedConfigObject() |
296
|
|
|
{ |
297
|
|
|
$classname = $this->getWrappedConfigClassname(); |
298
|
|
|
$obj = new $classname; |
299
|
|
|
|
300
|
|
|
return $obj; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* inject a config into our list |
305
|
|
|
* |
306
|
|
|
* useful for adding in hard-coded config options |
307
|
|
|
* |
308
|
|
|
* @param string $name |
309
|
|
|
* the name of the config to inject |
310
|
|
|
* @param object $config |
311
|
|
|
* the config to inject into our list |
312
|
|
|
*/ |
313
|
|
|
public function addEntry($name, $config) |
314
|
|
|
{ |
315
|
|
|
// make sure $config is the right type |
316
|
|
|
$classname = $this->getWrappedConfigClassname(); |
317
|
|
|
if (!$config instanceof $classname) { |
318
|
|
|
throw new E4xx_IncompatibleConfigClass($classname, get_class($config)); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
// if we get here, all is good |
322
|
|
|
$this->list[$name] = $config; |
323
|
|
|
|
324
|
|
|
// keep the configs sorted |
325
|
|
|
ksort($this->list); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* retrieve a single config entry |
330
|
|
|
* |
331
|
|
|
* @param string $name |
332
|
|
|
* the name of the config to retrieve |
333
|
|
|
* @return array|object |
334
|
|
|
*/ |
335
|
|
|
public function getEntry($name) |
336
|
|
|
{ |
337
|
|
|
if (!isset($this->list[$name])) { |
338
|
|
|
throw new E4xx_NoSuchConfigEntry($name); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return $this->list[$name]; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* do we have a config entry called $name? |
346
|
|
|
* |
347
|
|
|
* @param string $name |
348
|
|
|
* the name of the config to check for |
349
|
|
|
* @return boolean |
350
|
|
|
*/ |
351
|
|
|
public function hasEntry($name) |
352
|
|
|
{ |
353
|
|
|
if (!isset($this->list[$name])) { |
354
|
|
|
return false; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
return true; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* returns our list of all known configs |
362
|
|
|
* |
363
|
|
|
* @return array |
364
|
|
|
*/ |
365
|
|
|
public function getEntries() |
366
|
|
|
{ |
367
|
|
|
return $this->list; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* returns the names of all of the entries in our list |
372
|
|
|
* |
373
|
|
|
* @return array<string> |
|
|
|
|
374
|
|
|
*/ |
375
|
|
|
public function getEntryNames() |
376
|
|
|
{ |
377
|
|
|
return array_keys($this->list); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* add config entries from a hard-coded list |
382
|
|
|
* |
383
|
|
|
* @param HardCodedList $hardCodedDefaults |
384
|
|
|
* the entries to add |
385
|
|
|
*/ |
386
|
|
|
public function addHardCodedList(HardCodedList $hardCodedDefaults) |
387
|
|
|
{ |
388
|
|
|
$list = $hardCodedDefaults->getConfigs(); |
389
|
|
|
|
390
|
|
|
foreach ($list as $name => $config) |
391
|
|
|
{ |
392
|
|
|
$this->addEntry($name, $config); |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.