1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the m1\env library |
5
|
|
|
* |
6
|
|
|
* (c) m1 <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @package m1/env |
12
|
|
|
* @version 2.0.0 |
13
|
|
|
* @author Miles Croxford <[email protected]> |
14
|
|
|
* @copyright Copyright (c) Miles Croxford <[email protected]> |
15
|
|
|
* @license http://github.com/m1/env/blob/master/LICENSE.md |
16
|
|
|
* @link http://github.com/m1/env/blob/master/README.md Documentation |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace M1\Env\Parser; |
20
|
|
|
|
21
|
|
|
use M1\Env\Exception\ParseException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The value parser for Env |
25
|
|
|
* |
26
|
|
|
* @since 0.2.0 |
27
|
|
|
*/ |
28
|
|
|
class ValueParser extends AbstractParser |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The regex to get the content between double quote (") strings, ignoring escaped quotes. |
32
|
|
|
* Unescaped: "(?:[^"\\]*(?:\\.)?)*" |
33
|
|
|
* |
34
|
|
|
* @var string REGEX_QUOTE_DOUBLE_STRING |
35
|
|
|
*/ |
36
|
|
|
const REGEX_QUOTE_DOUBLE_STRING = '"(?:[^\"\\\\]*(?:\\\\.)?)*\"'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The regex to get the content between single quote (') strings, ignoring escaped quotes |
40
|
|
|
* Unescaped: '(?:[^'\\]*(?:\\.)?)*' |
41
|
|
|
* |
42
|
|
|
* @var string REGEX_QUOTE_SINGLE_STRING |
43
|
|
|
*/ |
44
|
|
|
const REGEX_QUOTE_SINGLE_STRING = "'(?:[^'\\\\]*(?:\\\\.)?)*'"; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The value types that Env supports |
48
|
|
|
* |
49
|
|
|
* @var array $value_types |
50
|
|
|
*/ |
51
|
|
|
private static $value_types = array( |
52
|
|
|
'string', |
53
|
|
|
'bool', |
54
|
|
|
'number', |
55
|
|
|
'null', |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The map to convert escaped characters into real characters |
60
|
|
|
* |
61
|
|
|
* @var array $character_map |
62
|
|
|
*/ |
63
|
|
|
private static $character_map = array( |
64
|
|
|
"\\n" => "\n", |
65
|
|
|
"\\\"" => "\"", |
66
|
|
|
'\\\'' => "'", |
67
|
|
|
'\\t' => "\t" |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The parser for variables |
72
|
|
|
* |
73
|
|
|
* @var \M1\Env\Parser\VariableParser $variable_parser |
74
|
|
|
*/ |
75
|
|
|
private $variable_parser; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
* |
80
|
|
|
* @param \M1\Env\Parser $parser The parent parser |
81
|
|
|
* @param array $context Variables context |
82
|
|
|
*/ |
83
|
66 |
|
public function __construct($parser, array $context = array()) |
84
|
|
|
{ |
85
|
66 |
|
parent::__construct($parser); |
86
|
|
|
|
87
|
66 |
|
$this->variable_parser = new VariableParser($parser, $context); |
88
|
66 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Parses a .env value |
92
|
|
|
* |
93
|
|
|
* @param string $value The value to parse |
94
|
|
|
* |
95
|
|
|
* @return string|null The parsed key, or null if the key is a comment |
96
|
|
|
*/ |
97
|
48 |
|
public function parse($value) |
98
|
|
|
{ |
99
|
48 |
|
$value = trim($value); |
100
|
|
|
|
101
|
48 |
|
if ($this->parser->string_helper->startsWith('#', $value)) { |
102
|
6 |
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
48 |
|
return $this->parseValue($value); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Parses a .env value |
110
|
|
|
* |
111
|
|
|
* @param string $value The value to parse |
112
|
|
|
* |
113
|
|
|
* @return string|null The parsed value, or null if the value is null |
114
|
|
|
* |
115
|
|
|
* @uses parseString |
116
|
|
|
* @uses parseNull |
117
|
|
|
* @uses parseBool |
118
|
|
|
* @uses parseNumber |
119
|
|
|
*/ |
120
|
48 |
|
private function parseValue($value) |
121
|
|
|
{ |
122
|
48 |
|
foreach (self::$value_types as $type) { |
123
|
48 |
|
$parsed_value = $value; |
124
|
|
|
|
125
|
48 |
|
if ($type !== 'string') { |
126
|
45 |
|
$parsed_value = $this->parser->string_helper->stripComments($value); |
127
|
45 |
|
} |
128
|
|
|
|
129
|
48 |
|
list($is_function, $parse_function) = $this->fetchFunctionNames($type); |
130
|
|
|
|
131
|
48 |
|
if ($this->parser->string_helper->$is_function($parsed_value)) { |
132
|
33 |
|
return $this->$parse_function($parsed_value); |
133
|
|
|
} |
134
|
45 |
|
} |
135
|
|
|
|
136
|
42 |
|
return (isset($parsed_value)) ? $this->parseUnquotedString($parsed_value) : $value; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Gets the functions for the value type |
141
|
|
|
* |
142
|
|
|
* @param string $type The value type |
143
|
|
|
* |
144
|
|
|
* @return string[] The is and parse function names |
145
|
|
|
*/ |
146
|
48 |
|
private function fetchFunctionNames($type) |
147
|
|
|
{ |
148
|
48 |
|
$type = ucfirst($type); |
149
|
|
|
|
150
|
|
|
return array( |
151
|
48 |
|
'is'.$type, |
152
|
|
|
'parse'.$type |
153
|
48 |
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Parses a .env string |
158
|
|
|
* |
159
|
|
|
* @param string $value The value to parse |
160
|
|
|
* |
161
|
|
|
* @return string The parsed string |
162
|
|
|
*/ |
163
|
24 |
|
private function parseString($value) |
164
|
|
|
{ |
165
|
24 |
|
$single = false; |
166
|
24 |
|
$regex = self::REGEX_QUOTE_DOUBLE_STRING; |
167
|
24 |
|
$symbol = '"'; |
168
|
|
|
|
169
|
24 |
|
if ($this->parser->string_helper->startsWith('\'', $value)) { |
170
|
15 |
|
$single = true; |
171
|
15 |
|
$regex = self::REGEX_QUOTE_SINGLE_STRING; |
172
|
15 |
|
$symbol = "'"; |
173
|
15 |
|
} |
174
|
|
|
|
175
|
24 |
|
$matches = $this->fetchStringMatches($value, $regex, $symbol); |
176
|
|
|
|
177
|
21 |
|
$value = $matches[0]; |
178
|
21 |
|
if ($value !== '') { |
179
|
21 |
|
$value = substr($value, 1, strlen($value) - 2); |
180
|
21 |
|
} |
181
|
21 |
|
$value = strtr($value, self::$character_map); |
182
|
|
|
|
183
|
21 |
|
return ($single) ? $value : $this->variable_parser->parse($value, true); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Gets the regex matches in the string |
188
|
|
|
* |
189
|
|
|
* @param string $regex The regex to use |
190
|
|
|
* @param string $value The value to parse |
191
|
|
|
* @param string $symbol The symbol we're parsing for |
192
|
|
|
* |
193
|
|
|
* @throws \M1\Env\Exception\ParseException If the string has a missing end quote |
194
|
|
|
* |
195
|
|
|
* @return string[] The matches based on the regex and the value |
196
|
|
|
*/ |
197
|
24 |
|
private function fetchStringMatches($value, $regex, $symbol) |
198
|
|
|
{ |
199
|
24 |
|
if (!preg_match('/'.$regex.'/', $value, $matches)) { |
200
|
3 |
|
throw new ParseException( |
201
|
3 |
|
sprintf('Missing end %s quote', $symbol), |
202
|
3 |
|
$value, |
203
|
3 |
|
$this->parser->line_num |
204
|
3 |
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
21 |
|
return $matches; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Parses a .env null value |
212
|
|
|
* |
213
|
|
|
* @param string $value The value to parse |
214
|
|
|
* |
215
|
|
|
* @return null Null value |
216
|
|
|
*/ |
217
|
12 |
|
private function parseNull($value) |
218
|
|
|
{ |
219
|
12 |
|
return (is_null($value) || $value === "null") ? null : false; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Parses a .env unquoted string |
224
|
|
|
* |
225
|
|
|
* @param string $value The value to parse |
226
|
|
|
* |
227
|
|
|
* @return string The parsed string |
228
|
|
|
*/ |
229
|
42 |
|
private function parseUnquotedString($value) |
230
|
|
|
{ |
231
|
42 |
|
if ($value === "") { |
232
|
6 |
|
return null; |
233
|
|
|
} |
234
|
|
|
|
235
|
39 |
|
return $this->variable_parser->parse($value); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Parses a .env bool |
240
|
|
|
* |
241
|
|
|
* @param string $value The value to parse |
242
|
|
|
* |
243
|
|
|
* @return bool The parsed bool |
244
|
|
|
*/ |
245
|
12 |
|
private function parseBool($value) |
246
|
|
|
{ |
247
|
12 |
|
$value = strtolower($value); |
248
|
|
|
|
249
|
12 |
|
return $value === "true" || $value === "yes"; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Parses a .env number |
254
|
|
|
* |
255
|
|
|
* @param string $value The value to parse |
256
|
|
|
* |
257
|
|
|
* @return int|float The parsed bool |
258
|
|
|
*/ |
259
|
9 |
|
private function parseNumber($value) |
260
|
|
|
{ |
261
|
9 |
|
if (strpos($value, '.') !== false) { |
262
|
9 |
|
return (float) $value; |
263
|
|
|
} |
264
|
|
|
|
265
|
9 |
|
return (int) $value; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|