1 | <?php |
||
29 | class ValueParser extends AbstractParser |
||
30 | { |
||
31 | /** |
||
32 | * The trait for checking types |
||
33 | */ |
||
34 | use ValueTypeCheckable; |
||
35 | |||
36 | /** |
||
37 | * The regex to get variables '$(VARIABLE)' in .env |
||
38 | * Unescaped: ${(.*?)} |
||
39 | * |
||
40 | * @var string REGEX_ENV_VARIABLE |
||
41 | */ |
||
42 | const REGEX_ENV_VARIABLE = '\\${(.*?)}'; |
||
43 | |||
44 | /** |
||
45 | * The regex to get the content between double quote (") strings, ignoring escaped quotes. |
||
46 | * Unescaped: "(?:[^"\\]*(?:\\.)?)*" |
||
47 | * |
||
48 | * @var string REGEX_QUOTE_DOUBLE_STRING |
||
49 | */ |
||
50 | const REGEX_QUOTE_DOUBLE_STRING = '"(?:[^\"\\\\]*(?:\\\\.)?)*\"'; |
||
51 | |||
52 | /** |
||
53 | * The regex to get the content between single quote (') strings, ignoring escaped quotes |
||
54 | * Unescaped: '(?:[^'\\]*(?:\\.)?)*' |
||
55 | * |
||
56 | * @var string REGEX_QUOTE_SINGLE_STRING |
||
57 | */ |
||
58 | const REGEX_QUOTE_SINGLE_STRING = "'(?:[^'\\\\]*(?:\\\\.)?)*'"; |
||
59 | |||
60 | /** |
||
61 | * The bool variants available in .env |
||
62 | * |
||
63 | * @var array $bool_variants |
||
64 | */ |
||
65 | private static $bool_variants = array( |
||
|
|||
66 | 'true', 'false', 'yes', 'no' |
||
67 | ); |
||
68 | |||
69 | /** |
||
70 | * The map to convert escaped characters into real characters |
||
71 | * |
||
72 | * @var array $character_map |
||
73 | */ |
||
74 | private static $character_map = array( |
||
75 | "\\n" => "\n", |
||
76 | "\\\"" => "\"", |
||
77 | '\\\'' => "'", |
||
78 | '\\t' => "\t" |
||
79 | ); |
||
80 | |||
81 | /** |
||
82 | * The line num of the current value |
||
83 | * |
||
84 | * @var int $line_num |
||
85 | */ |
||
86 | private $line_num; |
||
87 | |||
88 | /** |
||
89 | * The current parsed values/lines |
||
90 | * |
||
91 | * @var array $lines |
||
92 | */ |
||
93 | private $lines; |
||
94 | |||
95 | /** |
||
96 | * Parses a .env value |
||
97 | * |
||
98 | * @param string $value The value to parse |
||
99 | * @param array $lines The array of already parsed lines |
||
100 | * @param int $line_num The line num of the value |
||
101 | * |
||
102 | * @return string|null The parsed key, or null if the key is a comment |
||
103 | */ |
||
104 | 30 | public function parse($value, $lines, $line_num) |
|
117 | |||
118 | /** |
||
119 | * Parses a .env value |
||
120 | * |
||
121 | * @param string $value The value to parse |
||
122 | * |
||
123 | * @return string|null The parsed value, or null if the value is null |
||
124 | */ |
||
125 | 30 | private function parseValue($value) |
|
146 | |||
147 | /** |
||
148 | * Parses a .env variable |
||
149 | * |
||
150 | * @param string $value The value to parse |
||
151 | * @param bool $quoted_string Is the value in a quoted string |
||
152 | * |
||
153 | * @return string The parsed value |
||
154 | */ |
||
155 | 21 | private function parseVariables($value, $quoted_string = false) |
|
169 | |||
170 | /** |
||
171 | * Get variable matches inside a string |
||
172 | * |
||
173 | * @param string $value The value to parse |
||
174 | * |
||
175 | * @return array The variable matches |
||
176 | */ |
||
177 | 21 | private function fetchVariableMatches($value) |
|
187 | |||
188 | /** |
||
189 | * Parses a .env variable |
||
190 | * |
||
191 | * @param string $value The value to parse |
||
192 | * @param string $variable_name The variable name to get |
||
193 | * @param array $matches The matches of the variables |
||
194 | * @param bool $quoted_string Is the value in a quoted string |
||
195 | * |
||
196 | * @throws \M1\Env\Exception\ParseException If the variable can not be found |
||
197 | * |
||
198 | * @return string The parsed value |
||
199 | */ |
||
200 | 6 | private function fetchVariable($value, $variable_name, $matches, $quoted_string) |
|
212 | |||
213 | /** |
||
214 | * Checks to see if a variable exists |
||
215 | * |
||
216 | * @param string $value The value to throw an error with if doesn't exist |
||
217 | * @param string $variable The variable name to get |
||
218 | * @param array $lines The lines already parsed |
||
219 | * |
||
220 | * @throws \M1\Env\Exception\ParseException If the variable can not be found |
||
221 | */ |
||
222 | 6 | private function checkVariableExists($value, $variable, $lines) |
|
234 | |||
235 | /** |
||
236 | * Checks to see if a variable exists |
||
237 | * |
||
238 | * @param string $value The value to throw an error with if doesn't exist |
||
239 | * @param array $matches The matches of the variables |
||
240 | * @param bool $quoted_string Is the value in a quoted string |
||
241 | * |
||
242 | * @return string The parsed value |
||
243 | */ |
||
244 | 3 | private function doReplacements($value, $matches, $quoted_string) |
|
259 | |||
260 | /** |
||
261 | * Parses a .env string |
||
262 | * |
||
263 | * @param string $value The value to parse |
||
264 | * |
||
265 | * @return string The parsed string |
||
266 | */ |
||
267 | 12 | private function parseString($value) |
|
284 | |||
285 | /** |
||
286 | * Gets the regex matches in the string |
||
287 | * |
||
288 | * @param string $regex The regex to use |
||
289 | * @param string $value The value to parse |
||
290 | * @param string $symbol The symbol we're parsing for |
||
291 | * |
||
292 | * @throws \M1\Env\Exception\ParseException If the string has a missing end quote |
||
293 | * |
||
294 | * @return array The matches based on the regex and the value |
||
295 | */ |
||
296 | 12 | private function fetchStringMatches($value, $regex, $symbol) |
|
310 | /** |
||
311 | * Parses a .env null value |
||
312 | * |
||
313 | * @param string $value The value to parse |
||
314 | * |
||
315 | * @return null Null value |
||
316 | */ |
||
317 | 3 | private function parseNull($value) |
|
321 | |||
322 | /** |
||
323 | * Parses a .env unquoted string |
||
324 | * |
||
325 | * @param string $value The value to parse |
||
326 | * |
||
327 | * @return string The parsed string |
||
328 | */ |
||
329 | 24 | private function parseUnquotedString($value) |
|
337 | |||
338 | /** |
||
339 | * Parses a .env bool |
||
340 | * |
||
341 | * @param string $value The value to parse |
||
342 | * |
||
343 | * @return bool The parsed bool |
||
344 | */ |
||
345 | 6 | private function parseBool($value) |
|
351 | |||
352 | /** |
||
353 | * Parses a .env number |
||
354 | * |
||
355 | * @param string $value The value to parse |
||
356 | * |
||
357 | * @return int|float The parsed bool |
||
358 | */ |
||
359 | 6 | private function parseNumber($value) |
|
367 | |||
368 | /** |
||
369 | * Strips comments from a value |
||
370 | * |
||
371 | * @param string $value The value to strip comments from |
||
372 | * |
||
373 | * @return string value |
||
374 | */ |
||
375 | 27 | private function stripComments($value) |
|
379 | } |
||
380 |
This check marks private properties in classes that are never used. Those properties can be removed.