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 1.1.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; |
20
|
|
|
|
21
|
|
|
use M1\Env\Exception\ParseException; |
22
|
|
|
use M1\Env\Helper\StringHelper; |
23
|
|
|
use M1\Env\Parser\ValueParser; |
24
|
|
|
use M1\Env\Parser\KeyParser; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The .env parser |
28
|
|
|
* |
29
|
|
|
* @since 0.1.0 |
30
|
|
|
*/ |
31
|
|
|
class Parser |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* The Env key parser |
35
|
|
|
* |
36
|
|
|
* @var \M1\Env\Parser\KeyParser $key_parser |
37
|
|
|
*/ |
38
|
|
|
private $key_parser; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The line num of the current value |
42
|
|
|
* |
43
|
|
|
* @var int $line_num |
44
|
|
|
*/ |
45
|
|
|
public $line_num; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The current parsed values/lines |
49
|
|
|
* |
50
|
|
|
* @var array $lines |
51
|
|
|
*/ |
52
|
|
|
public $lines = array(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The String helper class |
56
|
|
|
* |
57
|
|
|
* @var \M1\Env\Helper\StringHelper $string_helper |
58
|
|
|
*/ |
59
|
|
|
public $string_helper; |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The Env value parser |
64
|
|
|
* |
65
|
|
|
* @var \M1\Env\Parser\ValueParser $value_parser |
66
|
|
|
*/ |
67
|
|
|
public $value_parser; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The parser constructor |
71
|
|
|
* |
72
|
|
|
* @param string $content The |
73
|
|
|
*/ |
74
|
63 |
|
public function __construct($content) |
75
|
|
|
{ |
76
|
63 |
|
$this->key_parser = new KeyParser($this); |
77
|
63 |
|
$this->value_parser = new ValueParser($this); |
78
|
63 |
|
$this->string_helper = new StringHelper(); |
79
|
|
|
|
80
|
63 |
|
$this->doParse($content); |
81
|
42 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Parses the .env and returns the contents statically |
85
|
|
|
* |
86
|
|
|
* @param string $content The content to parse |
87
|
|
|
* |
88
|
|
|
* @return array The .env contents |
89
|
|
|
*/ |
90
|
63 |
|
public static function parse($content) |
91
|
|
|
{ |
92
|
63 |
|
$parser = new Parser($content); |
93
|
|
|
|
94
|
42 |
|
return $parser->getContent(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Opens the .env, parses it then returns the contents |
99
|
|
|
* |
100
|
|
|
* @param string $content The content to parse |
101
|
|
|
* |
102
|
|
|
* @return array The .env contents |
103
|
|
|
*/ |
104
|
63 |
|
private function doParse($content) |
105
|
|
|
{ |
106
|
63 |
|
$raw_lines = array_filter($this->makeLines($content), 'strlen'); |
107
|
|
|
|
108
|
63 |
|
if (empty($raw_lines)) { |
109
|
6 |
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
57 |
|
return $this->parseContent($raw_lines); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Splits the string into an array |
117
|
|
|
* |
118
|
|
|
* @param string $content The string content to split |
119
|
|
|
* |
120
|
|
|
* @return array The array of lines to parse |
121
|
|
|
*/ |
122
|
63 |
|
private function makeLines($content) |
123
|
|
|
{ |
124
|
63 |
|
return explode("\n", str_replace(array("\r\n", "\n\r", "\r"), "\n", $content)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Parses the .env line by line |
129
|
|
|
* |
130
|
|
|
* @param array $raw_lines The raw content of the file |
131
|
|
|
* |
132
|
|
|
* @throws \M1\Env\Exception\ParseException If the file does not have a key=value structure |
133
|
|
|
* |
134
|
|
|
* @return array The .env contents |
135
|
|
|
*/ |
136
|
57 |
|
private function parseContent(array $raw_lines) |
137
|
|
|
{ |
138
|
57 |
|
$this->lines = array(); |
139
|
57 |
|
$this->line_num = 0; |
140
|
|
|
|
141
|
57 |
|
foreach ($raw_lines as $raw_line) { |
142
|
57 |
|
$this->line_num++; |
143
|
|
|
|
144
|
57 |
|
if ($this->string_helper->startsWith('#', $raw_line) || !$raw_line) { |
145
|
9 |
|
continue; |
146
|
|
|
} |
147
|
|
|
|
148
|
57 |
|
$this->parseLine($raw_line); |
149
|
36 |
|
} |
150
|
|
|
|
151
|
36 |
|
return $this->lines; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Parses a line of the .env |
156
|
|
|
* |
157
|
|
|
* @param string $raw_line The raw content of the line |
158
|
|
|
* |
159
|
|
|
* @return array The parsed lines |
160
|
|
|
*/ |
161
|
57 |
|
private function parseLine($raw_line) |
162
|
|
|
{ |
163
|
57 |
|
$raw_line = $this->parseExport($raw_line); |
164
|
|
|
|
165
|
54 |
|
list($key, $value) = $this->parseKeyValue($raw_line); |
166
|
|
|
|
167
|
51 |
|
$key = $this->key_parser->parse($key); |
168
|
|
|
|
169
|
45 |
|
if (!is_string($key)) { |
170
|
3 |
|
return; |
171
|
|
|
} |
172
|
|
|
|
173
|
45 |
|
$this->lines[$key] = $this->value_parser->parse($value); |
174
|
36 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Parses a export line of the .env |
178
|
|
|
* |
179
|
|
|
* @param string $raw_line The raw content of the line |
180
|
|
|
* |
181
|
|
|
* @throws \M1\Env\Exception\ParseException If the file does not have a key=value structure |
182
|
|
|
* |
183
|
|
|
* @return string The parsed line |
184
|
|
|
*/ |
185
|
57 |
|
private function parseExport($raw_line) |
186
|
|
|
{ |
187
|
57 |
|
$line = trim($raw_line); |
188
|
|
|
|
189
|
57 |
|
if ($this->string_helper->startsWith("export", $line)) { |
190
|
6 |
|
$export_line = explode("export", $raw_line, 2); |
191
|
|
|
|
192
|
6 |
|
if (count($export_line) !== 2 || empty($export_line[1])) { |
193
|
3 |
|
throw new ParseException( |
194
|
3 |
|
'You must have a export key = value', |
195
|
3 |
|
$raw_line, |
196
|
3 |
|
$this->line_num |
197
|
3 |
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
3 |
|
$line = trim($export_line[1]); |
201
|
3 |
|
} |
202
|
|
|
|
203
|
54 |
|
return $line; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Gets the key = value items from the line |
208
|
|
|
* |
209
|
|
|
* @param string $raw_line The raw content of the line |
210
|
|
|
* |
211
|
|
|
* @throws \M1\Env\Exception\ParseException If the line does not have a key=value structure |
212
|
|
|
* |
213
|
|
|
* @return array The parsed lines |
214
|
|
|
*/ |
215
|
54 |
|
private function parseKeyValue($raw_line) |
216
|
|
|
{ |
217
|
54 |
|
$key_value = explode("=", $raw_line, 2); |
218
|
|
|
|
219
|
54 |
|
if (count($key_value) !== 2) { |
220
|
3 |
|
throw new ParseException( |
221
|
3 |
|
'You must have a key = value', |
222
|
3 |
|
$raw_line, |
223
|
3 |
|
$this->line_num |
224
|
3 |
|
); |
225
|
|
|
} |
226
|
|
|
|
227
|
51 |
|
return $key_value; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Returns the contents of the .env |
232
|
|
|
* |
233
|
|
|
* @return array The .env contents |
234
|
|
|
*/ |
235
|
42 |
|
public function getContent() |
236
|
|
|
{ |
237
|
42 |
|
return $this->lines; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|