-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathParser.php
More file actions
68 lines (58 loc) · 2.43 KB
/
Parser.php
File metadata and controls
68 lines (58 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace Snowair\Dotenv;
class Parser extends \josegonzalez\Dotenv\Parser
{
public function parse($contents)
{
$lines = preg_split('/\r\n|\r|\n/', $contents);
$environment = array();
foreach ($lines as $line) {
$line = trim($line);
if (empty($line)) {
continue;
}
if ($line[0] == '#') {
continue;
}
if (!preg_match('/(?:export )?(?<!\.)([a-zA-Z0-9_.]*)(\s?)=(\s?)(.*)/', $line, $matches)) {
continue;
}
$key = $matches[1];
$value = $matches[4];
if (preg_match('/^[0-9]/', $key) == 1) {
continue;
}
if (strlen($value) === 0) {
$value = '';
} elseif (strpbrk($value[0], '"\'') !== false) {
$quote = $value[0];
$regexPattern = sprintf('/^
%1$s # match a quote at the start of the value
( # capturing sub-pattern used
(?: # we do not need to capture this
[^%1$s\\\\] # any character other than a quote or backslash
|\\\\\\\\ # or two backslashes together
|\\\\%1$s # or an escaped quote e.g \"
)* # as many characters that match the previous rules
) # end of the capturing sub-pattern
%1$s # and the closing quote
.*$ # and discard any string after the closing quote
/mx', $quote);
$value = preg_replace($regexPattern, '$1', $value);
$value = str_replace("\\$quote", $quote, $value);
$value = str_replace('\\\\', '\\', $value);
$value = $this->processQuotedValue($value, $environment);
if (!empty($value) && strpbrk($value[0], '"\'') !== false) {
$quote = $value[0];
$value = preg_replace($regexPattern, '$1', $value);
$value = str_replace("\\$quote", $quote, $value);
$value = str_replace('\\\\', '\\', $value);
}
} else {
$value = $this->processUnquotedValue($value);
}
$environment[$key] = $value;
}
return $environment;
}
}