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; |
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 content to parse |
73
|
|
|
* @param array $context Variables context |
74
|
|
|
*/ |
75
|
66 |
|
public function __construct($content, array $context = array()) |
76
|
|
|
{ |
77
|
66 |
|
$this->key_parser = new KeyParser($this); |
78
|
66 |
|
$this->value_parser = new ValueParser($this, $context); |
79
|
66 |
|
$this->string_helper = new StringHelper(); |
80
|
|
|
|
81
|
66 |
|
$this->doParse($content); |
82
|
45 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Parses the .env and returns the contents statically |
86
|
|
|
* |
87
|
|
|
* @param string $content The content to parse |
88
|
|
|
* @param array $context Variables context |
89
|
|
|
* |
90
|
|
|
* @return array The .env contents |
91
|
|
|
*/ |
92
|
66 |
|
public static function parse($content, array $context = array()) |
93
|
|
|
{ |
94
|
66 |
|
$parser = new Parser($content, $context); |
95
|
|
|
|
96
|
45 |
|
return $parser->getContent(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Opens the .env, parses it then returns the contents |
101
|
|
|
* |
102
|
|
|
* @param string $content The content to parse |
103
|
|
|
* |
104
|
|
|
* @return array The .env contents |
105
|
|
|
*/ |
106
|
66 |
|
protected function doParse($content) |
107
|
|
|
{ |
108
|
66 |
|
$raw_lines = array_filter($this->makeLines($content), 'strlen'); |
109
|
|
|
|
110
|
66 |
|
if (empty($raw_lines)) { |
111
|
6 |
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
60 |
|
return $this->parseContent($raw_lines); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Splits the string into an array |
119
|
|
|
* |
120
|
|
|
* @param string $content The string content to split |
121
|
|
|
* |
122
|
|
|
* @return array The array of lines to parse |
123
|
|
|
*/ |
124
|
66 |
|
private function makeLines($content) |
125
|
|
|
{ |
126
|
66 |
|
return explode("\n", str_replace(array("\r\n", "\n\r", "\r"), "\n", $content)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Parses the .env line by line |
131
|
|
|
* |
132
|
|
|
* @param array $raw_lines The raw content of the file |
133
|
|
|
* |
134
|
|
|
* @throws \M1\Env\Exception\ParseException If the file does not have a key=value structure |
135
|
|
|
* |
136
|
|
|
* @return array The .env contents |
137
|
|
|
*/ |
138
|
60 |
|
private function parseContent(array $raw_lines) |
139
|
|
|
{ |
140
|
60 |
|
$this->lines = array(); |
141
|
60 |
|
$this->line_num = 0; |
142
|
|
|
|
143
|
60 |
|
foreach ($raw_lines as $raw_line) { |
144
|
60 |
|
$this->line_num++; |
145
|
|
|
|
146
|
60 |
|
$line = trim($raw_line); |
147
|
|
|
|
148
|
60 |
|
if ($this->string_helper->startsWith('#', $line) || !$line) { |
149
|
9 |
|
continue; |
150
|
|
|
} |
151
|
|
|
|
152
|
60 |
|
$this->parseLine($raw_line); |
153
|
39 |
|
} |
154
|
|
|
|
155
|
39 |
|
return $this->lines; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Parses a line of the .env |
160
|
|
|
* |
161
|
|
|
* @param string $raw_line The raw content of the line |
162
|
|
|
* |
163
|
|
|
* @return array The parsed lines |
164
|
|
|
*/ |
165
|
60 |
|
private function parseLine($raw_line) |
166
|
|
|
{ |
167
|
60 |
|
$raw_line = $this->parseExport($raw_line); |
168
|
|
|
|
169
|
57 |
|
list($key, $value) = $this->parseKeyValue($raw_line); |
170
|
|
|
|
171
|
54 |
|
$key = $this->key_parser->parse($key); |
172
|
|
|
|
173
|
48 |
|
if (!is_string($key)) { |
174
|
|
|
return; |
175
|
|
|
} |
176
|
|
|
|
177
|
48 |
|
$this->lines[$key] = $this->value_parser->parse($value); |
178
|
39 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Parses a export line of the .env |
182
|
|
|
* |
183
|
|
|
* @param string $raw_line The raw content of the line |
184
|
|
|
* |
185
|
|
|
* @throws \M1\Env\Exception\ParseException If the file does not have a key=value structure |
186
|
|
|
* |
187
|
|
|
* @return string The parsed line |
188
|
|
|
*/ |
189
|
60 |
|
private function parseExport($raw_line) |
190
|
|
|
{ |
191
|
60 |
|
$line = trim($raw_line); |
192
|
|
|
|
193
|
60 |
|
if ($this->string_helper->startsWith("export", $line)) { |
194
|
6 |
|
$export_line = explode("export", $raw_line, 2); |
195
|
|
|
|
196
|
6 |
|
if (count($export_line) !== 2 || empty($export_line[1])) { |
197
|
3 |
|
throw new ParseException( |
198
|
3 |
|
'You must have a export key = value', |
199
|
3 |
|
$raw_line, |
200
|
3 |
|
$this->line_num |
201
|
3 |
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
3 |
|
$line = trim($export_line[1]); |
205
|
3 |
|
} |
206
|
|
|
|
207
|
57 |
|
return $line; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Gets the key = value items from the line |
212
|
|
|
* |
213
|
|
|
* @param string $raw_line The raw content of the line |
214
|
|
|
* |
215
|
|
|
* @throws \M1\Env\Exception\ParseException If the line does not have a key=value structure |
216
|
|
|
* |
217
|
|
|
* @return array The parsed lines |
218
|
|
|
*/ |
219
|
57 |
|
private function parseKeyValue($raw_line) |
220
|
|
|
{ |
221
|
57 |
|
$key_value = explode("=", $raw_line, 2); |
222
|
|
|
|
223
|
57 |
|
if (count($key_value) !== 2) { |
224
|
3 |
|
throw new ParseException( |
225
|
3 |
|
'You must have a key = value', |
226
|
3 |
|
$raw_line, |
227
|
3 |
|
$this->line_num |
228
|
3 |
|
); |
229
|
|
|
} |
230
|
|
|
|
231
|
54 |
|
return $key_value; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Returns the contents of the .env |
236
|
|
|
* |
237
|
|
|
* @return array The .env contents |
238
|
|
|
*/ |
239
|
45 |
|
public function getContent($keyName = null) |
240
|
|
|
{ |
241
|
45 |
|
if (!is_null($keyName)) { |
242
|
|
|
return (array_key_exists($keyName, $this->lines)) ? $this->lines[$keyName] : null; |
243
|
|
|
} |
244
|
45 |
|
return $this->lines; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|