|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of expect package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Noritaka Horio <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace expect\config; |
|
12
|
|
|
|
|
13
|
|
|
use expect\Dictionary; |
|
14
|
|
|
use ReflectionClass; |
|
15
|
|
|
use RuntimeException; |
|
16
|
|
|
use Yosymfony\Toml\Toml; |
|
17
|
|
|
|
|
18
|
|
|
class ConfigurationLoader |
|
19
|
|
|
{ |
|
20
|
|
|
const REPORTER = '\expect\ResultReporter'; |
|
21
|
|
|
const PACKAGE_REGISTRAR = '\expect\PackageRegistrar'; |
|
22
|
|
|
|
|
23
|
|
|
public function loadFromFile($file) |
|
24
|
|
|
{ |
|
25
|
|
|
if (file_exists($file) === false) { |
|
26
|
|
|
throw new ConfigurationFileNotFoundException("$file not found"); |
|
27
|
|
|
} |
|
28
|
|
|
$values = Toml::parse($file); |
|
29
|
|
|
|
|
30
|
|
|
$defaultConfig = new DefaultConfiguration(); |
|
31
|
|
|
$runtimeConfig = $this->loadFromArray($values); |
|
32
|
|
|
|
|
33
|
|
|
return $defaultConfig->merge($runtimeConfig); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function loadFromArray(array $values) |
|
37
|
|
|
{ |
|
38
|
|
|
$config = Dictionary::fromArray($values); |
|
39
|
|
|
$loadedPackages = []; |
|
40
|
|
|
$loadedReporter = null; |
|
41
|
|
|
|
|
42
|
|
|
if ($config->containsKey('packages')) { |
|
43
|
|
|
$packages = $config->get('packages'); |
|
44
|
|
|
$loadedPackages = $this->createPackages($packages->toArray()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ($config->containsKey('reporter')) { |
|
48
|
|
|
$reporter = $config->get('reporter'); |
|
49
|
|
|
$loadedReporter = $this->createReporter($reporter); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return new RuntimeConfiguration($loadedPackages, $loadedReporter); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Create a few new package registrars |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $packages |
|
59
|
|
|
* |
|
60
|
|
|
* @return \expect\PackageRegistrar[] |
|
61
|
|
|
*/ |
|
62
|
|
|
private function createPackages(array $packages) |
|
63
|
|
|
{ |
|
64
|
|
|
$matcherPackages = []; |
|
65
|
|
|
|
|
66
|
|
|
foreach ($packages as $package) { |
|
67
|
|
|
$reflection = new ReflectionClass($package); |
|
68
|
|
|
|
|
69
|
|
|
if ($reflection->implementsInterface(self::PACKAGE_REGISTRAR) === false) { |
|
70
|
|
|
throw NotAvailableException::createForPackage(self::PACKAGE_REGISTRAR); |
|
71
|
|
|
} |
|
72
|
|
|
$matcherPackages[] = $reflection->newInstance(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $matcherPackages; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Create a new result reporter |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $reporter |
|
82
|
|
|
* |
|
83
|
|
|
* @return \expect\ResultReporter |
|
84
|
|
|
*/ |
|
85
|
|
|
private function createReporter($reporter) |
|
86
|
|
|
{ |
|
87
|
|
|
$reflection = new ReflectionClass($reporter); |
|
88
|
|
|
|
|
89
|
|
|
if ($reflection->implementsInterface(self::REPORTER) === false) { |
|
90
|
|
|
throw NotAvailableException::createForReporter(self::REPORTER); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $reflection->newInstance(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|