|
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\Arr; |
|
21
|
|
|
use Exen\Konfig\Utils; |
|
22
|
|
|
use Exen\Konfig\Exception\ParseException; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Konfig's Java-Properties 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 Properties extends AbstractFileParser |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* Parsed configuration file. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array $parsedFile |
|
41
|
|
|
* |
|
42
|
|
|
* @since 0.2.5 |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $parsedFile; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Loads a PROPERTIES file as an array. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $path File path |
|
50
|
|
|
* |
|
51
|
|
|
* @throws ParseException If there is an error parsing PROPERTIES file |
|
52
|
|
|
* |
|
53
|
|
|
* @return array The parsed data |
|
54
|
|
|
* |
|
55
|
|
|
* @since 0.2.4 |
|
56
|
|
|
*/ |
|
57
|
|
|
public function parse($path) |
|
58
|
6 |
|
{ |
|
59
|
|
|
$this->loadFile($path); |
|
60
|
6 |
|
|
|
61
|
|
|
$data = $this->parsedFile; |
|
62
|
6 |
|
|
|
63
|
|
|
unset($this->parsedFile); |
|
64
|
|
|
|
|
65
|
6 |
|
if (!$data || !is_array($data) || is_null($data)) { |
|
|
|
|
|
|
66
|
4 |
|
throw new ParseException( |
|
67
|
3 |
|
[ |
|
68
|
|
|
'message' => 'Error parsing PROPERTIES file', |
|
69
|
3 |
|
'file' => $this->file, |
|
70
|
2 |
|
] |
|
71
|
1 |
|
); |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $data; |
|
75
|
3 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
* |
|
80
|
|
|
* @return array Supported extensions |
|
81
|
|
|
* |
|
82
|
|
|
* @since 0.1.0 |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getSupportedFileExtensions() |
|
85
|
3 |
|
{ |
|
86
|
|
|
return ['properties']; |
|
87
|
3 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Parse Java-Properties |
|
91
|
|
|
* |
|
92
|
|
|
* @param string|null $string The string to parse |
|
93
|
|
|
* |
|
94
|
|
|
* @return array The parsed data |
|
95
|
|
|
* @since 0.2.6 |
|
96
|
|
|
* @codeCoverageIgnore |
|
97
|
|
|
*/ |
|
98
|
|
|
private function parseProperties($string = null) |
|
99
|
|
|
{ |
|
100
|
|
|
$result = []; |
|
101
|
|
|
$lines = preg_split('/\n\t|\n/', $string); |
|
102
|
|
|
$key = ''; |
|
103
|
|
|
|
|
104
|
|
|
static $isWaitingForOtherLine = false; |
|
105
|
|
|
|
|
106
|
|
|
foreach ($lines as $k => $line) { |
|
107
|
|
|
if (empty($line) || (!$isWaitingForOtherLine |
|
108
|
|
|
&& strpos($line, '#') === 0) |
|
109
|
|
|
) { |
|
110
|
|
|
continue; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (!strpos($line, '=') && !$isWaitingForOtherLine) { |
|
114
|
|
|
break; |
|
115
|
|
|
return false; |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if (!$isWaitingForOtherLine) { |
|
119
|
|
|
$key = substr($line, 0, strpos($line, '=')); |
|
120
|
|
|
$key = trim($key); |
|
121
|
|
|
$value = substr($line, strpos($line, '=') + 1, strlen($line)); |
|
122
|
|
|
} else { |
|
123
|
|
|
$value .= $line; |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Trim unnecessary white spaces from $value |
|
127
|
|
|
$value = trim($value); |
|
128
|
|
|
$value = Utils::trimWhitespace($value); |
|
129
|
|
|
|
|
130
|
|
|
// Remove unnecessary double/single qoutes from $value |
|
131
|
|
|
$value = Utils::removeQuotes($value); |
|
132
|
|
|
|
|
133
|
|
|
if (strpos($value, '\\') === strlen($value) - strlen('\\')) { |
|
134
|
|
|
$value = substr($value, 0, strlen($value) - 1); |
|
135
|
|
|
$isWaitingForOtherLine = true; |
|
136
|
|
|
} else { |
|
137
|
|
|
$isWaitingForOtherLine = false; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$result[$key] = empty($value) ? '' : $value; |
|
141
|
|
|
|
|
142
|
|
|
unset($lines[$k]); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
Utils::unescapeProperties($result); |
|
146
|
|
|
Utils::trimArrayElements($result); |
|
147
|
|
|
Utils::stripBackslashes($result); |
|
148
|
|
|
Utils::fixArrayValues($result); |
|
149
|
|
|
|
|
150
|
|
|
// Fix for dotted properties |
|
151
|
|
|
$data = []; |
|
152
|
|
|
|
|
153
|
|
|
foreach ($result as $k => $v) { |
|
|
|
|
|
|
154
|
|
|
Arr::set($data, $k, $v); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $data; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Loads in the given file and parses it. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string|bool|null $file File to load |
|
164
|
|
|
* |
|
165
|
|
|
* @return array The parsed file data |
|
166
|
|
|
* |
|
167
|
|
|
* @since 0.2.4 |
|
168
|
|
|
* @codeCoverageIgnore |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function loadFile($file = null) |
|
171
|
|
|
{ |
|
172
|
|
|
$this->file = is_file($file) ? $file : false; |
|
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
$contents = $this->parseVars(Utils::getContent($this->file)); |
|
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
if ($this->file && !is_null($file)) { |
|
177
|
|
|
$this->parsedFile = $this->parseProperties($contents); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Returns the formatted configuration file contents. |
|
183
|
|
|
* |
|
184
|
|
|
* @param array $contents configuration array |
|
185
|
|
|
* |
|
186
|
|
|
* @return string formatted configuration file contents |
|
187
|
|
|
* |
|
188
|
|
|
* @since 0.2.4 |
|
189
|
|
|
* @codeCoverageIgnore |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function exportFormat($contents = null) |
|
192
|
|
|
{ |
|
193
|
|
|
throw new Exception( |
|
194
|
|
|
'Saving configuration to `Properties` is not supported at this time' |
|
195
|
|
|
); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* __toString. |
|
200
|
|
|
* |
|
201
|
|
|
* @return string |
|
202
|
|
|
* @since 0.1.2 |
|
203
|
|
|
* @codeCoverageIgnore |
|
204
|
|
|
*/ |
|
205
|
|
|
public function __toString() |
|
206
|
|
|
{ |
|
207
|
|
|
return 'Exen\Konfig\FileParser\Properties' . PHP_EOL; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
// END OF ./src/FileParser/Properties.php FILE |
|
212
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.