1 | <?php |
||
29 | class VariableParser extends AbstractParser |
||
30 | { |
||
31 | /** |
||
32 | * The trait for checking types |
||
33 | */ |
||
34 | use ValueCheckTrait; |
||
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 symbol for the assign default value parameter expansion |
||
46 | * |
||
47 | * @var string SYMBOL_ASSIGN_DEFAULT_VALUE |
||
48 | */ |
||
49 | const SYMBOL_ASSIGN_DEFAULT_VALUE = '='; |
||
50 | |||
51 | /** |
||
52 | * The symbol for the default value parameter expansion |
||
53 | * |
||
54 | * @var string SYMBOL_DEFAULT_VALUE |
||
55 | */ |
||
56 | const SYMBOL_DEFAULT_VALUE = '-'; |
||
57 | |||
58 | /** |
||
59 | * Parses a .env variable |
||
60 | * |
||
61 | * @param string $value The value to parse |
||
62 | * @param bool $quoted_string Is the value in a quoted string |
||
63 | * |
||
64 | * @return string The parsed value |
||
65 | */ |
||
66 | 33 | public function parse($value, $quoted_string = false) |
|
80 | |||
81 | /** |
||
82 | * Get variable matches inside a string |
||
83 | * |
||
84 | * @param string $value The value to parse |
||
85 | * |
||
86 | * @return array The variable matches |
||
87 | */ |
||
88 | 33 | private function fetchVariableMatches($value) |
|
98 | |||
99 | /** |
||
100 | * Parses a .env variable |
||
101 | * |
||
102 | * @param string $value The value to parse |
||
103 | * @param string $variable_name The variable name to get |
||
104 | * @param array $matches The matches of the variables |
||
105 | * @param bool $quoted_string Is the value in a quoted string |
||
106 | * |
||
107 | * @return string The parsed value |
||
108 | */ |
||
109 | 18 | private function fetchVariable($value, $variable_name, $matches, $quoted_string) |
|
124 | |||
125 | /** |
||
126 | * Checks to see if the variable has a parameter expansion |
||
127 | * |
||
128 | * @param string $variable The variable to check |
||
129 | * |
||
130 | * @return bool Does the variable have a parameter expansion |
||
131 | */ |
||
132 | 18 | private function hasParameterExpansion($variable) |
|
142 | |||
143 | /** |
||
144 | * Fetches and sets the parameter expansion |
||
145 | * |
||
146 | * @param string $variable_name The parameter expansion inside this to fetch |
||
147 | * |
||
148 | * @return string The parsed value |
||
149 | */ |
||
150 | 9 | private function fetchParameterExpansion($variable_name) |
|
151 | { |
||
152 | 9 | $parameter_type = $this->fetchParameterExpansionType($variable_name); |
|
153 | |||
154 | 9 | list($parameter_symbol, $empty_flag) = $this->fetchParameterExpansionSymbol($variable_name, $parameter_type); |
|
155 | 9 | list($variable, $default) = $this->splitVariableDefault($variable_name, $parameter_symbol); |
|
156 | |||
157 | 6 | return $this->parseVariableParameter( |
|
158 | 6 | $variable, |
|
159 | 6 | $default, |
|
160 | 6 | $this->checkVariableExists($variable, $variable, true), |
|
161 | 6 | $empty_flag && empty($this->parser->lines[$variable]), |
|
162 | $parameter_type |
||
163 | 6 | ); |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * Fetches the parameter expansion type |
||
168 | * |
||
169 | * @param string $variable_name The parameter expansion type to get from this |
||
170 | * |
||
171 | * @return string The parameter expansion type |
||
172 | */ |
||
173 | 9 | private function fetchParameterExpansionType($variable_name) |
|
174 | { |
||
175 | 9 | if (strpos($variable_name, self::SYMBOL_ASSIGN_DEFAULT_VALUE) !== false) { |
|
176 | 9 | return 'assign_default_value'; |
|
177 | } |
||
178 | |||
179 | 6 | return 'default_value'; // self::DEFAULT_VALUE_SYMBOL |
|
180 | } |
||
181 | |||
182 | /** |
||
183 | * Fetches the parameter type symbol |
||
184 | * |
||
185 | * @param string $variable_name The variable |
||
186 | * @param string $type The type of parameter expansion |
||
187 | * |
||
188 | * @return array The symbol and if there is a empty check |
||
189 | */ |
||
190 | 9 | private function fetchParameterExpansionSymbol($variable_name, $type) |
|
191 | { |
||
192 | 9 | $symbol = (new \ReflectionClass($this))->getConstant('SYMBOL_'.strtoupper($type)); |
|
193 | 9 | $pos = strpos($variable_name, $symbol); |
|
194 | |||
195 | 9 | $check_empty = substr($variable_name, ($pos - 1), 1) === ":"; |
|
196 | |||
197 | 9 | if ($check_empty) { |
|
198 | 9 | $symbol = sprintf(":%s", $symbol); |
|
199 | 9 | } |
|
200 | |||
201 | 9 | return array($symbol, $check_empty); |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * Splits the parameter expansion into variable and default |
||
206 | * |
||
207 | * @param string $variable_name The variable name to split |
||
208 | * @param string $parameter_symbol The parameter expansion symbol |
||
209 | * |
||
210 | * @throws \M1\Env\Exception\ParseException If the parameter expansion if not valid syntax |
||
211 | * |
||
212 | * @return array The split variable and default value |
||
213 | */ |
||
214 | 9 | private function splitVariableDefault($variable_name, $parameter_symbol) |
|
215 | { |
||
216 | 9 | $variable_default = explode($parameter_symbol, $variable_name, 2); |
|
217 | |||
218 | 9 | if (count($variable_default) !== 2 || empty($variable_default[1])) { |
|
219 | 3 | throw new ParseException( |
|
220 | 3 | 'You must have valid parameter expansion syntax, eg. ${parameter:=word}', |
|
221 | 3 | $this->parser->origin_exception, |
|
222 | 3 | $this->parser->file, |
|
223 | 3 | $variable_name, |
|
224 | 3 | $this->parser->line_num |
|
225 | 3 | ); |
|
226 | } |
||
227 | |||
228 | 6 | return array(trim($variable_default[0]), trim($variable_default[1])); |
|
229 | } |
||
230 | |||
231 | |||
232 | /** |
||
233 | * Parses and sets the variable and default if needed |
||
234 | * |
||
235 | * @param string $variable The variable to parse |
||
236 | * @param string $default The default value |
||
237 | * @param bool $exists Does the variable exist |
||
238 | * @param bool $empty Is there the variable empty if exists and the empty flag is set |
||
239 | * @param string $type The type of parameter expansion |
||
240 | * |
||
241 | * @return string The parsed value |
||
242 | */ |
||
243 | 6 | private function parseVariableParameter($variable, $default, $exists, $empty, $type) |
|
244 | { |
||
245 | 6 | if ($exists && !$empty) { |
|
246 | 6 | return $this->parser->lines[$variable]; |
|
247 | } |
||
248 | |||
249 | 6 | return $this->assignVariableParameterDefault($variable, $default, $empty, $type); |
|
250 | |||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Parses and sets the variable parameter to default |
||
255 | * |
||
256 | * @param string $variable The variable to parse |
||
257 | * @param string $default The default value |
||
258 | * @param bool $empty Is there the variable empty if exists and the empty flag is set |
||
259 | * @param string $type The type of parameter expansion |
||
260 | * |
||
261 | * @return string The parsed default value |
||
262 | */ |
||
263 | 6 | private function assignVariableParameterDefault($variable, $default, $empty, $type) |
|
264 | { |
||
265 | 6 | $default = $this->parser->value_parser->parse($default); |
|
266 | |||
267 | 6 | if ($type === "assign_default_value" && $empty) { |
|
268 | 6 | $this->parser->lines[$variable] = $default; |
|
269 | 6 | } |
|
270 | |||
271 | 6 | return $default; |
|
272 | } |
||
273 | |||
274 | /** |
||
275 | * Checks to see if a variable exists |
||
276 | * |
||
277 | * @param string $value The value to throw an error with if doesn't exist |
||
278 | * @param string $variable The variable name to get |
||
279 | * @param bool $variable_check Are you checking to only see if a variable exists and not to throw an exception |
||
280 | * |
||
281 | * @throws \M1\Env\Exception\ParseException If the variable can not be found and `$variable_check` is false |
||
282 | * |
||
283 | * @return bool Does the variable exist |
||
284 | */ |
||
285 | 15 | private function checkVariableExists($value, $variable, $variable_check = false) |
|
303 | |||
304 | /** |
||
305 | * Do the variable replacements |
||
306 | * |
||
307 | * @param string $value The value to throw an error with if doesn't exist |
||
308 | * @param array $matches The matches of the variables |
||
309 | * @param bool $quoted_string Is the value in a quoted string |
||
310 | * |
||
311 | * @return string The parsed value |
||
312 | */ |
||
313 | 9 | public function doReplacements($value, $matches, $quoted_string) |
|
328 | } |
||
329 |