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) |
|
159 | |||
160 | /** |
||
161 | * Fetches the parameter expansion type |
||
162 | * |
||
163 | * @param string $variable_name The parameter expansion type to get from this |
||
164 | * |
||
165 | * @return string The parameter expansion type |
||
166 | */ |
||
167 | 9 | private function fetchParameterExpansionType($variable_name) |
|
175 | |||
176 | /** |
||
177 | * Fetches the parameter type symbol |
||
178 | * |
||
179 | * @param string $variable_name The variable |
||
180 | * @param string $type The type of parameter expansion |
||
181 | * |
||
182 | * @return array The symbol and if there is a empty check |
||
183 | */ |
||
184 | 9 | private function fetchParameterExpansionSymbol($variable_name, $type) |
|
197 | |||
198 | /** |
||
199 | * Splits the parameter expansion into variable and default |
||
200 | * |
||
201 | * @param string $variable_name The variable name to split |
||
202 | * @param string $parameter_symbol The parameter expansion symbol |
||
203 | * |
||
204 | * @throws \M1\Env\Exception\ParseException If the parameter expansion if not valid syntax |
||
205 | * |
||
206 | * @return array The split variable and default value |
||
207 | */ |
||
208 | 9 | private function splitVariableDefault($variable_name, $parameter_symbol) |
|
223 | |||
224 | /** |
||
225 | * Parses and sets the variable and default if needed |
||
226 | * |
||
227 | * @param string $variable_name The variable name to parse and set |
||
228 | * @param string $default The default value |
||
229 | * @param bool $check_empty Is there a check empty flag `:` |
||
230 | * @param string $parameter_type The type of parameter expansion |
||
231 | * |
||
232 | * @return string The parsed value |
||
233 | */ |
||
234 | 6 | private function parseVariableDefault($variable_name, $default, $check_empty, $parameter_type) |
|
247 | |||
248 | /** |
||
249 | * Parses and sets the variable and default if needed |
||
250 | * |
||
251 | * @param string $variable The variable to parse |
||
252 | * @param string $default The default value |
||
253 | * @param bool $exists Does the variable exist |
||
254 | * @param bool $empty Is there the variable empty if exists and the empty flag is set |
||
255 | * @param string $type The type of parameter expansion |
||
256 | * |
||
257 | * @return string The parsed value |
||
258 | */ |
||
259 | 6 | private function parseVariableParameter($variable, $default, $exists, $empty, $type) |
|
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 | * Checks to see if a variable exists |
||
306 | * |
||
307 | * @param string $variable The variable name to get |
||
308 | * @param bool $check_empty Is there a check empty flag `:` |
||
309 | * |
||
310 | * @return bool Is the variable empty if the check empty flag is set |
||
311 | */ |
||
312 | 6 | private function checkVariableEmpty($variable, $check_empty) |
|
316 | |||
317 | /** |
||
318 | * Do the variable replacements |
||
319 | * |
||
320 | * @param string $value The value to throw an error with if doesn't exist |
||
321 | * @param array $matches The matches of the variables |
||
322 | * @param bool $quoted_string Is the value in a quoted string |
||
323 | * |
||
324 | * @return string The parsed value |
||
325 | */ |
||
326 | 9 | public function doReplacements($value, $matches, $quoted_string) |
|
341 | } |
||
342 |