1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Konfig. |
5
|
|
|
* |
6
|
|
|
* Yet another simple configuration loader library. |
7
|
|
|
* |
8
|
|
|
* PHP version 5 |
9
|
|
|
* |
10
|
|
|
* @category Library |
11
|
|
|
* @package Konfig |
12
|
|
|
* @author Xeriab Nabil (aka KodeBurner) <[email protected]> |
13
|
|
|
* @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT |
14
|
|
|
* @link https://xeriab.github.io/projects/konfig |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Exen\Konfig\FileParser; |
18
|
|
|
|
19
|
|
|
use Exception; |
20
|
|
|
use Exen\Konfig\Exception\ParseException; |
21
|
|
|
use Exen\Konfig\Utils; |
22
|
|
|
use Nette\Neon\Neon as NeonParser; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Konfig's NEON parser class. |
26
|
|
|
* |
27
|
|
|
* @category FileParser |
28
|
|
|
* @package Konfig |
29
|
|
|
* @author Xeriab Nabil (aka KodeBurner) <[email protected]> |
30
|
|
|
* @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT |
31
|
|
|
* @link https://xeriab.github.io/projects/konfig |
32
|
|
|
* |
33
|
|
|
* @implements Exen\Konfig\FileParser\AbstractFileParser |
34
|
|
|
*/ |
35
|
|
|
class Neon extends AbstractFileParser |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Loads a NEON file as an array. |
39
|
|
|
* |
40
|
|
|
* @param string $path File path |
41
|
|
|
* |
42
|
|
|
* @throws ParseException If there is an error parsing NEON file |
43
|
|
|
* |
44
|
|
|
* @return array The parsed data |
45
|
|
|
* |
46
|
|
|
* @since 0.1.0 |
47
|
|
|
*/ |
48
|
6 |
|
public function parse($path) |
49
|
|
|
{ |
50
|
6 |
|
$data = null; |
51
|
|
|
|
52
|
|
|
try { |
53
|
6 |
|
$data = $this->loadFile($path); |
54
|
4 |
|
} catch (Exception $ex) { |
55
|
3 |
|
throw new ParseException( |
56
|
|
|
[ |
57
|
3 |
|
'message' => 'Error parsing NEON file', |
58
|
3 |
|
'file' => $path, |
59
|
3 |
|
'exception' => $ex, |
60
|
|
|
] |
61
|
1 |
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
return $data; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
* |
70
|
|
|
* @return array Supported extensions |
71
|
|
|
* |
72
|
|
|
* @since 0.1.0 |
73
|
|
|
*/ |
74
|
3 |
|
public function getSupportedFileExtensions() |
75
|
|
|
{ |
76
|
3 |
|
return ['neon']; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Loads in the given file and parses it. |
81
|
|
|
* |
82
|
|
|
* @param string $file File to load |
83
|
|
|
* |
84
|
|
|
* @return array The parsed file data |
85
|
|
|
* |
86
|
|
|
* @since 0.2.4 |
87
|
|
|
* @codeCoverageIgnore |
88
|
|
|
*/ |
89
|
|
|
protected function loadFile($file = null) |
90
|
|
|
{ |
91
|
|
|
$this->file = $file; |
92
|
|
|
$contents = $this->parseVars(Utils::getContent($file)); |
93
|
|
|
|
94
|
|
|
return NeonParser::decode($contents); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the formatted configuration file contents. |
99
|
|
|
* |
100
|
|
|
* @param array $contents configuration array |
101
|
|
|
* |
102
|
|
|
* @return string formatted configuration file contents |
103
|
|
|
* |
104
|
|
|
* @since 0.2.4 |
105
|
|
|
* @codeCoverageIgnore |
106
|
|
|
*/ |
107
|
|
|
protected function exportFormat($contents = null) |
108
|
|
|
{ |
109
|
|
|
$this->prepVars($contents); |
|
|
|
|
110
|
|
|
return NeonParser::encode($contents); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* __toString. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
* @since 0.1.2 |
118
|
|
|
* @codeCoverageIgnore |
119
|
|
|
*/ |
120
|
|
|
public function __toString() |
121
|
|
|
{ |
122
|
|
|
return 'Exen\Konfig\FileParser\Neon' . PHP_EOL; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// END OF ./src/FileParser/Neon.php FILE |
127
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.