1 | <?php |
||
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() |
||
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() |
||
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() |
||
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() |
||
369 | |||
370 | /** |
||
371 | * returns the names of all of the entries in our list |
||
372 | * |
||
373 | * @return array<string> |
||
|
|||
374 | */ |
||
375 | public function getEntryNames() |
||
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); |
||
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.