1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPSemVerChecker\Configuration; |
4
|
|
|
|
5
|
|
|
use Noodlehaus\Config; |
6
|
|
|
use PHPSemVerChecker\SemanticVersioning\Level; |
7
|
|
|
|
8
|
|
|
class Configuration |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $mapping = []; |
14
|
|
|
/** |
15
|
|
|
* @var \Noodlehaus\Config |
16
|
|
|
*/ |
17
|
|
|
protected $config; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string|array $path |
21
|
|
|
* @throws \Noodlehaus\Exception\EmptyDirectoryException |
22
|
|
|
*/ |
23
|
5 |
|
public function __construct($path) |
24
|
|
|
{ |
25
|
5 |
|
$this->config = new Config($path); |
26
|
5 |
|
$this->extractMapping($this->get('level.mapping', [])); |
27
|
5 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string|array $file |
31
|
|
|
* @return \PHPSemVerChecker\Configuration\Configuration |
32
|
|
|
* @throws \Noodlehaus\Exception\EmptyDirectoryException |
33
|
|
|
*/ |
34
|
|
|
public static function fromFile($file) |
35
|
|
|
{ |
36
|
|
|
return new Configuration($file); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $name |
41
|
|
|
* @return \PHPSemVerChecker\Configuration\Configuration |
42
|
|
|
* @throws \Noodlehaus\Exception\EmptyDirectoryException |
43
|
|
|
*/ |
44
|
|
|
public static function defaults($name) |
45
|
|
|
{ |
46
|
|
|
return self::fromFile(['?' . $name . '.yml.dist', '?' . $name . '.yml']); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param array $mapping |
51
|
|
|
*/ |
52
|
5 |
|
protected function extractMapping(array $mapping) |
53
|
|
|
{ |
54
|
5 |
|
foreach ($mapping as &$level) { |
55
|
4 |
|
$level = Level::fromString($level); |
56
|
|
|
} |
57
|
5 |
|
$this->mapping = $mapping; |
58
|
5 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
1 |
|
public function getLevelMapping() |
64
|
|
|
{ |
65
|
1 |
|
return $this->mapping; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @see \Noodlehaus\Config::get |
70
|
|
|
* @param string $key |
71
|
|
|
* @param mixed|null $default |
72
|
|
|
* @return array|mixed|null |
73
|
|
|
*/ |
74
|
5 |
|
public function get($key, $default = null) |
75
|
|
|
{ |
76
|
5 |
|
return $this->config->get($key, $default); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @see \Noodlehaus\Config::set |
81
|
|
|
* @param string $key |
82
|
|
|
* @param mixed $value |
83
|
|
|
*/ |
84
|
2 |
|
public function set($key, $value) |
85
|
|
|
{ |
86
|
2 |
|
$this->config->set($key, $value); |
87
|
2 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Merge this configuration with an associative array. |
91
|
|
|
* |
92
|
|
|
* Note that dot notation is used for keys. |
93
|
|
|
* |
94
|
|
|
* @param array $data |
95
|
|
|
*/ |
96
|
|
|
public function merge($data) |
97
|
|
|
{ |
98
|
|
|
foreach ($data as $key => $value) { |
99
|
|
|
$this->set($key, $value); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|