1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Noodlehaus\Parser; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Symfony\Component\Yaml\Yaml as YamlParser; |
7
|
|
|
use Noodlehaus\Exception\ParseException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* YAML parser |
11
|
|
|
* |
12
|
|
|
* @package Config |
13
|
|
|
* @author Jesus A. Domingo <[email protected]> |
14
|
|
|
* @author Hassan Khan <[email protected]> |
15
|
|
|
* @author Filip Š <[email protected]> |
16
|
|
|
* @link https://github.com/noodlehaus/config |
17
|
|
|
* @license MIT |
18
|
|
|
*/ |
19
|
|
|
class Yaml implements ParserInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritDoc} |
23
|
|
|
* Loads a YAML/YML file as an array |
24
|
|
|
* |
25
|
|
|
* @throws ParseException If there is an error parsing the YAML file |
26
|
|
|
*/ |
27
|
6 |
|
public function parseFile($filename) |
28
|
|
|
{ |
29
|
|
|
try { |
30
|
6 |
|
$data = YamlParser::parseFile($filename, YamlParser::PARSE_CONSTANT); |
31
|
5 |
|
} catch (Exception $exception) { |
32
|
3 |
|
throw new ParseException( |
33
|
|
|
[ |
34
|
3 |
|
'message' => 'Error parsing YAML file', |
35
|
3 |
|
'exception' => $exception, |
36
|
|
|
] |
37
|
2 |
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
$parsed = $this->parse($data); |
41
|
|
|
|
42
|
3 |
|
return $parsed === null ? [] : $parsed; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
* Loads a YAML/YML string as an array |
48
|
|
|
* |
49
|
|
|
* @throws ParseException If If there is an error parsing the YAML string |
50
|
|
|
*/ |
51
|
6 |
|
public function parseString($config) |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
6 |
|
$data = YamlParser::parse($config, YamlParser::PARSE_CONSTANT); |
55
|
5 |
|
} catch (Exception $exception) { |
56
|
3 |
|
throw new ParseException( |
57
|
|
|
[ |
58
|
3 |
|
'message' => 'Error parsing YAML string', |
59
|
3 |
|
'exception' => $exception, |
60
|
|
|
] |
61
|
2 |
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
$parsed = $this->parse($data); |
65
|
|
|
|
66
|
3 |
|
return $parsed === null ? [] : $parsed; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Completes parsing of YAML/YML data |
71
|
|
|
* |
72
|
|
|
* @param array $data |
73
|
|
|
* |
74
|
|
|
* @return array|null |
75
|
|
|
*/ |
76
|
6 |
|
protected function parse($data = null) |
77
|
|
|
{ |
78
|
6 |
|
return $data; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritDoc} |
83
|
|
|
*/ |
84
|
3 |
|
public static function getSupportedExtensions() |
85
|
|
|
{ |
86
|
3 |
|
return ['yaml', 'yml']; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|