1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Enzyme\Axiom\Console; |
4
|
|
|
|
5
|
|
|
use Enzyme\Parrot\File; |
6
|
|
|
use Enzyme\Freckle\Dot; |
7
|
|
|
use Symfony\Component\Yaml\Parser; |
8
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
9
|
|
|
|
10
|
|
|
class Config |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Reference to the Symfony YAML parser. |
14
|
|
|
* |
15
|
|
|
* @var \Symfony\Component\Yaml\Parser |
16
|
|
|
*/ |
17
|
|
|
protected $parser; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Reference to a parrot file wrapper. |
21
|
|
|
* |
22
|
|
|
* @var \Enzyme\Parrot\File |
23
|
|
|
*/ |
24
|
|
|
protected $file_dispatch; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The parsed YAML configuration. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $config; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The dot array accessor. |
35
|
|
|
* |
36
|
|
|
* @var \Enzyme\Freckle\Dot |
37
|
|
|
*/ |
38
|
|
|
protected $dot; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new configuration manager. |
42
|
|
|
* |
43
|
|
|
* @param \Symfony\Component\Yaml\Parser $parser |
44
|
|
|
* @param \Enzyme\Parrot\File $file_dispatch |
45
|
|
|
* @param \Enzyme\Freckle\Dot $dot |
46
|
|
|
*/ |
47
|
|
|
public function __construct(Parser $parser, File $file_dispatch, Dot $dot) |
48
|
|
|
{ |
49
|
|
|
$this->parser = $parser; |
50
|
|
|
$this->file_dispatch = $file_dispatch; |
51
|
|
|
$this->dot = $dot; |
52
|
|
|
$this->config = []; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Parse the given YAML file. |
57
|
|
|
* |
58
|
|
|
* @param string $file The path to the file. |
59
|
|
|
* |
60
|
|
|
* @throws Exception If the YAML file cannot be parsed. |
61
|
|
|
*/ |
62
|
|
|
public function parse($file) |
63
|
|
|
{ |
64
|
|
|
if (false === $this->file_dispatch->exists($file)) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$results = $this->parser->parse( |
70
|
|
|
$this->file_dispatch->getContents($file) |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
if (true === is_array($results)) { |
74
|
|
|
$this->config = $results; |
75
|
|
|
} |
76
|
|
|
} catch (ParseException $e) { |
77
|
|
|
throw new Exception( |
78
|
|
|
"The yaml configuration file [$file] is invalid." |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the value associated with the given key, if the key does not |
85
|
|
|
* exist, return null. |
86
|
|
|
* |
87
|
|
|
* @param string $key |
88
|
|
|
* |
89
|
|
|
* @return null|string |
90
|
|
|
*/ |
91
|
|
|
public function get($key) |
92
|
|
|
{ |
93
|
|
|
if (count($this->config) < 1) { |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->dot->get($this->config, $key); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|