-
-
Notifications
You must be signed in to change notification settings - Fork 637
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #325 from vlucas/parser-backport
[2.6] Backport parser fixes from 3.3.0
- Loading branch information
Showing
5 changed files
with
96 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace Dotenv; | ||
|
||
use Dotenv\Exception\InvalidFileException; | ||
|
||
class Parser | ||
{ | ||
const INITIAL_STATE = 0; | ||
const UNQUOTED_STATE = 1; | ||
const QUOTED_STATE = 2; | ||
const ESCAPE_STATE = 3; | ||
const WHITESPACE_STATE = 4; | ||
const COMMENT_STATE = 5; | ||
|
||
/** | ||
* Parse the given variable name. | ||
* | ||
* @param string $name | ||
* | ||
* @return string | ||
*/ | ||
public static function parseName($name) | ||
{ | ||
return trim(str_replace(array('export ', '\'', '"'), '', $name)); | ||
} | ||
|
||
/** | ||
* Parse the given variable value. | ||
* | ||
* @param string $value | ||
* | ||
* @throws \Dotenv\Exception\InvalidFileException | ||
* | ||
* @return string | ||
*/ | ||
public static function parseValue($value) | ||
{ | ||
$data = array_reduce(str_split($value), function ($data, $char) use ($value) { | ||
switch ($data[1]) { | ||
case Parser::INITIAL_STATE: | ||
if ($char === '"') { | ||
return array($data[0], Parser::QUOTED_STATE); | ||
} else { | ||
return array($data[0].$char, Parser::UNQUOTED_STATE); | ||
} | ||
case Parser::UNQUOTED_STATE: | ||
if ($char === '#') { | ||
return array($data[0], Parser::COMMENT_STATE); | ||
} elseif (ctype_space($char)) { | ||
return array($data[0], Parser::WHITESPACE_STATE); | ||
} else { | ||
return array($data[0].$char, Parser::UNQUOTED_STATE); | ||
} | ||
case Parser::QUOTED_STATE: | ||
if ($char === '"') { | ||
return array($data[0], Parser::WHITESPACE_STATE); | ||
} elseif ($char === '\\') { | ||
return array($data[0], Parser::ESCAPE_STATE); | ||
} else { | ||
return array($data[0].$char, Parser::QUOTED_STATE); | ||
} | ||
case Parser::ESCAPE_STATE: | ||
if ($char === '"' || $char === '\\') { | ||
return array($data[0].$char, Parser::QUOTED_STATE); | ||
} else { | ||
return array($data[0].'\\'.$char, Parser::QUOTED_STATE); | ||
} | ||
case Parser::WHITESPACE_STATE: | ||
if ($char === '#') { | ||
return array($data[0], Parser::COMMENT_STATE); | ||
} elseif (!ctype_space($char)) { | ||
if ($data[0] !== '' && $data[0][0] === '#') { | ||
return array('', Parser::COMMENT_STATE); | ||
} else { | ||
throw new InvalidFileException('Dotenv values containing spaces must be surrounded by quotes.'); | ||
} | ||
} else { | ||
return array($data[0], Parser::WHITESPACE_STATE); | ||
} | ||
case Parser::COMMENT_STATE: | ||
return array($data[0], Parser::COMMENT_STATE); | ||
} | ||
}, array('', Parser::INITIAL_STATE)); | ||
|
||
return trim($data[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters