1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
namespace Enjoys\Dotenv\Parser; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use Enjoys\Dotenv\Parser\Env\Comment; |
10
|
|
|
use Enjoys\Dotenv\Parser\Env\Key; |
11
|
|
|
use Enjoys\Dotenv\Parser\Env\Value; |
12
|
|
|
use Enjoys\Dotenv\Parser\Lines\CommentLine; |
13
|
|
|
use Enjoys\Dotenv\Parser\Lines\EmptyLine; |
14
|
|
|
use Enjoys\Dotenv\Parser\Lines\EnvLine; |
15
|
|
|
use Enjoys\Dotenv\Parser\Lines\LineInterface; |
16
|
|
|
use Enjoys\Dotenv\Parser\Lines\Multiline; |
17
|
|
|
|
18
|
|
|
final class Parser implements ParserInterface |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $content |
23
|
|
|
* @return array<string, string|null> |
24
|
|
|
*/ |
25
|
43 |
|
#[\Override] |
26
|
|
|
public function parseEnv(string $content): array |
27
|
|
|
{ |
28
|
43 |
|
$envArray = []; |
29
|
|
|
/** @var LineInterface $line */ |
30
|
43 |
|
foreach ($this->parseLines($content) as $line) { |
31
|
42 |
|
if ($line instanceof EnvLine) { |
32
|
42 |
|
$envArray[$line->getKey()->getValue()] = $line->getValue()?->getValue(); |
33
|
|
|
} |
34
|
|
|
} |
35
|
41 |
|
return $envArray; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $content |
40
|
|
|
* @return array<array-key, LineInterface> |
|
|
|
|
41
|
|
|
*/ |
42
|
2 |
|
#[\Override] |
43
|
|
|
public function parseStructure(string $content): array |
44
|
|
|
{ |
45
|
2 |
|
$structure = []; |
46
|
|
|
/** @var LineInterface $line */ |
47
|
2 |
|
foreach ($this->parseLines($content) as $line) { |
48
|
2 |
|
if ($line instanceof EnvLine) { |
49
|
2 |
|
$structure[$line->getKey()->getValue()] = $line; |
50
|
2 |
|
continue; |
51
|
|
|
} |
52
|
2 |
|
$structure[] = $line; |
53
|
|
|
} |
54
|
2 |
|
return $structure; |
55
|
|
|
} |
56
|
|
|
|
57
|
54 |
|
public function parseLines(string $content): \Generator |
58
|
|
|
{ |
59
|
54 |
|
$contentSplit = preg_split("/\R/u", $content); |
60
|
|
|
foreach ( |
61
|
54 |
|
Multiline::handle( |
62
|
54 |
|
array_map( |
63
|
54 |
|
'trim', |
64
|
54 |
|
$contentSplit === false ? [] : $contentSplit |
65
|
54 |
|
) |
66
|
54 |
|
) as $line |
67
|
|
|
) { |
68
|
54 |
|
if (empty($line)) { |
69
|
43 |
|
yield new EmptyLine(); |
70
|
43 |
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
53 |
|
if (str_starts_with($line, '#')) { |
74
|
9 |
|
yield new CommentLine($line); |
75
|
9 |
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
53 |
|
[$key, $value, $comment] = $this->parseEnvLine($line); |
79
|
52 |
|
yield new EnvLine( |
80
|
52 |
|
$key, |
81
|
52 |
|
$value, |
82
|
52 |
|
$comment |
83
|
52 |
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $rawLine |
89
|
|
|
* @return list{Key, ?Value, ?Comment} |
|
|
|
|
90
|
|
|
*/ |
91
|
53 |
|
private function parseEnvLine(string $rawLine): array |
92
|
|
|
{ |
93
|
|
|
/** |
94
|
|
|
* $explodedLine[0] - rawKey |
95
|
|
|
* $explodedLine[1] - rawValue |
96
|
|
|
* @var string[] $explodedLine |
97
|
|
|
*/ |
98
|
53 |
|
$explodedLine = array_map('trim', explode('=', $rawLine, 2)); |
99
|
|
|
|
100
|
53 |
|
return [ |
101
|
53 |
|
new Key($explodedLine[0]), |
102
|
53 |
|
...$this->parseValue($explodedLine[1] ??= null) |
103
|
53 |
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string|null $rawValue |
108
|
|
|
* @return list{?Value, ?Comment} |
109
|
|
|
*/ |
110
|
52 |
|
private function parseValue(?string $rawValue): array |
111
|
|
|
{ |
112
|
52 |
|
if ($rawValue === null) { |
113
|
10 |
|
return [ |
|
|
|
|
114
|
10 |
|
null, |
115
|
10 |
|
null |
116
|
10 |
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
46 |
|
preg_match( |
120
|
46 |
|
'/^(?<value>([\'"])(?:(?!\1|\\\\).|\\\\.)*?\2)(?<comment>.*)?/', |
121
|
46 |
|
$rawValue, |
122
|
46 |
|
$matches, |
123
|
46 |
|
PREG_UNMATCHED_AS_NULL |
124
|
46 |
|
); |
125
|
|
|
|
126
|
46 |
|
$matches['value'] ??= false; |
127
|
46 |
|
$matches['comment'] ??= null; |
128
|
|
|
|
129
|
46 |
|
if ($matches['value'] !== false) { |
130
|
22 |
|
return [ |
|
|
|
|
131
|
22 |
|
new Value($matches['value']), |
132
|
22 |
|
($matches['comment'] === '' || $matches['comment'] === null) ? null : new Comment($matches['comment']) |
133
|
22 |
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
42 |
|
$unquotedValue = array_map('trim', explode('#', $rawValue, 2)); |
137
|
42 |
|
$value = $unquotedValue[0]; |
138
|
42 |
|
$comment = $unquotedValue[1] ?? null; |
139
|
42 |
|
return [ |
|
|
|
|
140
|
42 |
|
new Value($value), |
141
|
42 |
|
$comment !== null ? new Comment($comment) : null |
142
|
42 |
|
]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|