|
| 1 | +namespace Nuxed\Environment\_Private; |
| 2 | + |
| 3 | +use namespace HH\Lib\{C, Regex, Str, Vec}; |
| 4 | +use namespace Nuxed\Environment\Exception; |
| 5 | + |
| 6 | +final abstract class Parser { |
| 7 | + /** |
| 8 | + * Parse the given environment variable entry into a name and value. |
| 9 | + */ |
| 10 | + public static function parse(string $entry): (string, ?string) { |
| 11 | + $name = $entry; |
| 12 | + $value = null; |
| 13 | + if (Str\contains($entry, '=')) { |
| 14 | + list($name, $value) = Vec\map( |
| 15 | + Str\split($entry, '=', 2), |
| 16 | + ($str) ==> Str\trim($str), |
| 17 | + ); |
| 18 | + } |
| 19 | + |
| 20 | + return tuple(self::parseName($name), self::parseValue($value)); |
| 21 | + } |
| 22 | + |
| 23 | + public static function parseName(string $name): string { |
| 24 | + if ($name === '') { |
| 25 | + throw new Exception\InvalidArgumentException( |
| 26 | + 'Failed to parse environment variable name : name cannot be empty.', |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + $name = Str\trim( |
| 31 | + Str\replace_every($name, dict['export ' => '', '\'' => '', '"' => '']), |
| 32 | + ); |
| 33 | + if (!Regex\matches($name, re"~\A[a-zA-Z0-9_.]+\z~")) { |
| 34 | + throw new Exception\InvalidArgumentException( |
| 35 | + Str\format( |
| 36 | + 'Failed to parse environment variable name : an invalid name ( %s ).', |
| 37 | + $name, |
| 38 | + ), |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + return $name; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Strips quotes and comments from the environment variable value. |
| 47 | + */ |
| 48 | + public static function parseValue(?string $value): ?string { |
| 49 | + if ($value === null || Str\trim($value) === '') { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + return C\reduce( |
| 54 | + Str\chunk($value), |
| 55 | + ((string, State) $data, string $char): (string, State) ==> { |
| 56 | + switch ($data[1]) { |
| 57 | + case State::INITIAL: |
| 58 | + if ($char === '"' || $char === '\'') { |
| 59 | + return tuple($data[0], State::QUOTED); |
| 60 | + } else if ($char === '#') { |
| 61 | + return tuple($data[0], State::COMMENT); |
| 62 | + } else { |
| 63 | + return tuple($data[0].$char, State::UNQUOTED); |
| 64 | + } |
| 65 | + case State::UNQUOTED: |
| 66 | + if ($char === '#') { |
| 67 | + return tuple($data[0], State::COMMENT); |
| 68 | + } else if (\ctype_space($char)) { |
| 69 | + return tuple($data[0], State::WHITESPACE); |
| 70 | + } |
| 71 | + |
| 72 | + return tuple($data[0].$char, State::UNQUOTED); |
| 73 | + case State::QUOTED: |
| 74 | + if ($char === $value[0]) { |
| 75 | + return tuple($data[0], State::WHITESPACE); |
| 76 | + } else if ($char === '\\') { |
| 77 | + return tuple($data[0], State::ESCAPE); |
| 78 | + } |
| 79 | + |
| 80 | + return tuple($data[0].$char, State::QUOTED); |
| 81 | + case State::ESCAPE: |
| 82 | + if ($char === $value[0] || $char === '\\') { |
| 83 | + return tuple($data[0].$char, State::QUOTED); |
| 84 | + } |
| 85 | + |
| 86 | + throw new Exception\InvalidArgumentException( |
| 87 | + Str\format( |
| 88 | + 'Failed to parse environment entry : an unexpected escape sequence ( %s ).', |
| 89 | + $value, |
| 90 | + ), |
| 91 | + ); |
| 92 | + case State::WHITESPACE: |
| 93 | + if ($char === '#') { |
| 94 | + return tuple($data[0], State::COMMENT); |
| 95 | + } else if (!\ctype_space($char)) { |
| 96 | + throw new Exception\InvalidArgumentException( |
| 97 | + Str\format( |
| 98 | + 'Failed to parse environment entry : unexpected whitespace ( %s ).', |
| 99 | + $value, |
| 100 | + ), |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + return tuple($data[0], State::WHITESPACE); |
| 105 | + case State::COMMENT: |
| 106 | + return tuple($data[0], State::COMMENT); |
| 107 | + } |
| 108 | + }, |
| 109 | + tuple('', State::INITIAL), |
| 110 | + )[0]; |
| 111 | + } |
| 112 | +} |
0 commit comments