99 * file that was distributed with this source code.
1010 *
1111 * @package m1/env
12- * @version 0.2 .0
12+ * @version 1.0 .0
1313 * @author Miles Croxford <[email protected] > 1414 * @copyright Copyright (c) Miles Croxford <[email protected] > 1515 * @license http://github.com/m1/env/blob/master/LICENSE.md
@@ -157,20 +157,11 @@ private function parseVariables($value, $quoted_string = false)
157157 $ matches = $ this ->fetchVariableMatches ($ value );
158158
159159 if (is_array ($ matches )) {
160- $ replacements = array ();
161-
162160 if ($ this ->isVariableClone ($ value , $ matches , $ quoted_string )) {
163161 return $ this ->fetchVariable ($ value , $ matches [1 ][0 ], $ matches , $ quoted_string );
164162 }
165163
166- for ($ i = 0 ; $ i <= (count ($ matches [0 ]) - 1 ); $ i ++) {
167- $ replacement = $ this ->fetchVariable ($ value , $ matches [1 ][$ i ], $ matches , $ quoted_string );
168- $ replacements [$ matches [0 ][$ i ]] = $ replacement ;
169- }
170-
171- if (!empty ($ replacements )) {
172- $ value = strtr ($ value , $ replacements );
173- }
164+ $ value = $ this ->doReplacements ($ value , $ matches , $ quoted_string );
174165 }
175166
176167 return $ value ;
@@ -208,46 +199,102 @@ private function fetchVariableMatches($value)
208199 */
209200 private function fetchVariable ($ value , $ variable_name , $ matches , $ quoted_string )
210201 {
202+ $ this ->checkVariableExists ($ value , $ variable_name , $ this ->lines );
211203
212- if (!isset ($ this ->lines [$ variable_name ])) {
204+ $ replacement = $ this ->lines [$ variable_name ];
205+
206+ if ($ this ->isBoolInString ($ replacement , $ quoted_string , count ($ matches [0 ]))) {
207+ $ replacement = ($ replacement ) ? 'true ' : 'false ' ;
208+ }
209+
210+ return $ replacement ;
211+ }
212+
213+ /**
214+ * Checks to see if a variable exists
215+ *
216+ * @param string $value The value to throw an error with if doesn't exist
217+ * @param string $variable The variable name to get
218+ * @param array $lines The lines already parsed
219+ *
220+ * @throws \M1\Env\Exception\ParseException If the variable can not be found
221+ */
222+ private function checkVariableExists ($ value , $ variable , $ lines )
223+ {
224+ if (!isset ($ lines [$ variable ])) {
213225 throw new ParseException (
214- sprintf ('Variable has not been defined: %s ' , $ variable_name ),
226+ sprintf ('Variable has not been defined: %s ' , $ variable ),
215227 $ this ->origin_exception ,
216228 $ this ->file ,
217229 $ value ,
218230 $ this ->line_num
219231 );
220232 }
233+ }
221234
222- $ replacement = $ this ->lines [$ variable_name ];
235+ /**
236+ * Checks to see if a variable exists
237+ *
238+ * @param string $value The value to throw an error with if doesn't exist
239+ * @param array $matches The matches of the variables
240+ * @param bool $quoted_string Is the value in a quoted string
241+ *
242+ * @return string The parsed value
243+ */
244+ private function doReplacements ($ value , $ matches , $ quoted_string )
245+ {
246+ $ replacements = array ();
223247
224- if ( is_bool ( $ replacement ) &&
225- ( $ quoted_string || count ( $ matches [0 ]) >= 2 )) {
226- $ replacement = ( $ replacement) ? ' true ' : ' false ' ;
248+ for ( $ i = 0 ; $ i <= ( count ( $ matches [ 0 ]) - 1 ); $ i ++) {
249+ $ replacement = $ this -> fetchVariable ( $ value , $ matches [1 ][ $ i ], $ matches , $ quoted_string );
250+ $ replacements [ $ matches [ 0 ][ $ i ]] = $ replacement ;
227251 }
228252
229- return $ replacement ;
253+ if (!empty ($ replacements )) {
254+ $ value = strtr ($ value , $ replacements );
255+ }
256+
257+ return $ value ;
230258 }
231259
232260 /**
233261 * Parses a .env string
234262 *
235263 * @param string $value The value to parse
236264 *
237- * @throws \M1\Env\Exception\ParseException If the string has a missing end quote
238- *
239265 * @return string The parsed string
240266 */
241267 private function parseString ($ value )
242268 {
269+ $ regex = self ::REGEX_QUOTE_DOUBLE_STRING ;
270+ $ symbol = '" ' ;
271+
243272 if ($ this ->startsWith ('\'' , $ value )) {
244273 $ regex = self ::REGEX_QUOTE_SINGLE_STRING ;
245274 $ symbol = "' " ;
246- } else {
247- $ regex = self ::REGEX_QUOTE_DOUBLE_STRING ;
248- $ symbol = '" ' ;
249275 }
250276
277+ $ matches = $ this ->fetchStringMatches ($ value , $ regex , $ symbol );
278+
279+ $ value = trim ($ matches [0 ], $ symbol );
280+ $ value = strtr ($ value , self ::$ character_map );
281+
282+ return $ this ->parseVariables ($ value , true );
283+ }
284+
285+ /**
286+ * Gets the regex matches in the string
287+ *
288+ * @param string $regex The regex to use
289+ * @param string $value The value to parse
290+ * @param string $symbol The symbol we're parsing for
291+ *
292+ * @throws \M1\Env\Exception\ParseException If the string has a missing end quote
293+ *
294+ * @return array The matches based on the regex and the value
295+ */
296+ private function fetchStringMatches ($ value , $ regex , $ symbol )
297+ {
251298 if (!preg_match ('/ ' .$ regex .'/ ' , $ value , $ matches )) {
252299 throw new ParseException (
253300 sprintf ('Missing end %s quote ' , $ symbol ),
@@ -258,12 +305,8 @@ private function parseString($value)
258305 );
259306 }
260307
261- $ value = trim ($ matches [0 ], $ symbol );
262- $ value = strtr ($ value , self ::$ character_map );
263-
264- return $ this ->parseVariables ($ value , true );
308+ return $ matches ;
265309 }
266-
267310 /**
268311 * Parses a .env null value
269312 *
@@ -301,19 +344,9 @@ private function parseUnquotedString($value)
301344 */
302345 private function parseBool ($ value )
303346 {
304- switch (strtolower ($ value )) {
305- case 'true ' :
306- case 'yes ' :
307- $ value = true ;
308- break ;
309- case 'false ' :
310- case 'no ' :
311- default :
312- $ value = false ;
313- break ;
314- }
347+ $ value = strtolower ($ value );
315348
316- return $ value ;
349+ return $ value === " true " || $ value === " yes " ;
317350 }
318351
319352 /**
@@ -343,4 +376,4 @@ private function stripComments($value)
343376 {
344377 return trim (explode ("# " , $ value , 2 )[0 ]);
345378 }
346- }
379+ }
0 commit comments