1 | <?php |
||
29 | class ValueParser extends AbstractParser |
||
30 | { |
||
31 | /** |
||
32 | * The trait for checking types |
||
33 | */ |
||
34 | use ValueCheckTrait; |
||
35 | |||
36 | /** |
||
37 | * The regex to get the content between double quote (") strings, ignoring escaped quotes. |
||
38 | * Unescaped: "(?:[^"\\]*(?:\\.)?)*" |
||
39 | * |
||
40 | * @var string REGEX_QUOTE_DOUBLE_STRING |
||
41 | */ |
||
42 | const REGEX_QUOTE_DOUBLE_STRING = '"(?:[^\"\\\\]*(?:\\\\.)?)*\"'; |
||
43 | |||
44 | /** |
||
45 | * The regex to get the content between single quote (') strings, ignoring escaped quotes |
||
46 | * Unescaped: '(?:[^'\\]*(?:\\.)?)*' |
||
47 | * |
||
48 | * @var string REGEX_QUOTE_SINGLE_STRING |
||
49 | */ |
||
50 | const REGEX_QUOTE_SINGLE_STRING = "'(?:[^'\\\\]*(?:\\\\.)?)*'"; |
||
51 | |||
52 | /** |
||
53 | * The map to convert escaped characters into real characters |
||
54 | * |
||
55 | * @var array $character_map |
||
56 | */ |
||
57 | private static $character_map = array( |
||
58 | "\\n" => "\n", |
||
59 | "\\\"" => "\"", |
||
60 | '\\\'' => "'", |
||
61 | '\\t' => "\t" |
||
62 | ); |
||
63 | |||
64 | /** |
||
65 | * The parser for variables |
||
66 | * |
||
67 | * @var \M1\Env\Parser\VariableParser $variable_parser |
||
68 | */ |
||
69 | private $variable_parser; |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | * |
||
74 | * @param \M1\Env\Parser $parser The parent parser |
||
75 | */ |
||
76 | 57 | public function __construct($parser) |
|
77 | { |
||
78 | 57 | parent::__construct($parser); |
|
79 | |||
80 | 57 | $this->variable_parser = new VariableParser($parser); |
|
81 | 57 | } |
|
82 | |||
83 | /** |
||
84 | * Parses a .env value |
||
85 | * |
||
86 | * @param string $value The value to parse |
||
87 | * |
||
88 | * @return string|null The parsed key, or null if the key is a comment |
||
89 | */ |
||
90 | 42 | public function parse($value) |
|
91 | { |
||
92 | 42 | $value = trim($value); |
|
93 | |||
94 | 42 | if ($this->startsWith('#', $value)) { |
|
95 | 3 | return null; |
|
96 | } |
||
97 | |||
98 | 42 | return $this->parseValue($value); |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Parses a .env value |
||
103 | * |
||
104 | * @param string $value The value to parse |
||
105 | * |
||
106 | * @return string|null The parsed value, or null if the value is null |
||
107 | */ |
||
108 | 42 | private function parseValue($value) |
|
109 | { |
||
110 | 42 | $types = array('string', 'bool', 'number', 'null'); |
|
111 | |||
112 | 42 | $parsed_value = null; |
|
113 | |||
114 | 42 | foreach ($types as $type) { |
|
115 | 42 | $parsed_value = $value; |
|
116 | |||
117 | 42 | if ($type !== 'string') { |
|
118 | 39 | $parsed_value = $this->stripComments($value); |
|
119 | 39 | } |
|
120 | |||
121 | 42 | $is_function = sprintf('is%s', ucfirst($type)); |
|
122 | 42 | $parse_function = sprintf('parse%s', ucfirst($type)); |
|
123 | |||
124 | 42 | if ($this->$is_function($parsed_value)) { |
|
125 | 30 | return $this->$parse_function($parsed_value); |
|
126 | } |
||
127 | 39 | } |
|
128 | |||
129 | 36 | return $this->parseUnquotedString($parsed_value); |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Parses a .env string |
||
134 | * |
||
135 | * @param string $value The value to parse |
||
136 | * |
||
137 | * @return string The parsed string |
||
138 | */ |
||
139 | 21 | private function parseString($value) |
|
140 | { |
||
141 | 21 | $regex = self::REGEX_QUOTE_DOUBLE_STRING; |
|
142 | 21 | $symbol = '"'; |
|
143 | |||
144 | 21 | if ($this->startsWith('\'', $value)) { |
|
145 | 12 | $regex = self::REGEX_QUOTE_SINGLE_STRING; |
|
146 | 12 | $symbol = "'"; |
|
147 | 12 | } |
|
148 | |||
149 | 21 | $matches = $this->fetchStringMatches($value, $regex, $symbol); |
|
150 | |||
151 | 18 | $value = trim($matches[0], $symbol); |
|
152 | 18 | $value = strtr($value, self::$character_map); |
|
153 | |||
154 | 18 | return $this->variable_parser->parse($value, true); |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * Gets the regex matches in the string |
||
159 | * |
||
160 | * @param string $regex The regex to use |
||
161 | * @param string $value The value to parse |
||
162 | * @param string $symbol The symbol we're parsing for |
||
163 | * |
||
164 | * @throws \M1\Env\Exception\ParseException If the string has a missing end quote |
||
165 | * |
||
166 | * @return string[] The matches based on the regex and the value |
||
167 | */ |
||
168 | 21 | private function fetchStringMatches($value, $regex, $symbol) |
|
169 | { |
||
170 | 21 | if (!preg_match('/'.$regex.'/', $value, $matches)) { |
|
171 | 3 | throw new ParseException( |
|
172 | 3 | sprintf('Missing end %s quote', $symbol), |
|
173 | 3 | $this->parser->origin_exception, |
|
174 | 3 | $this->parser->file, |
|
175 | 3 | $value, |
|
176 | 3 | $this->parser->line_num |
|
177 | 3 | ); |
|
178 | } |
||
179 | |||
180 | 18 | return $matches; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Parses a .env null value |
||
185 | * |
||
186 | * @param string $value The value to parse |
||
187 | * |
||
188 | * @return null Null value |
||
189 | */ |
||
190 | 12 | private function parseNull($value) |
|
191 | { |
||
192 | 12 | return (is_null($value) || $value === "null") ? null : false; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Parses a .env unquoted string |
||
197 | * |
||
198 | * @param string $value The value to parse |
||
199 | * |
||
200 | * @return string The parsed string |
||
201 | */ |
||
202 | 36 | private function parseUnquotedString($value) |
|
210 | |||
211 | /** |
||
212 | * Parses a .env bool |
||
213 | * |
||
214 | * @param string $value The value to parse |
||
215 | * |
||
216 | * @return bool The parsed bool |
||
217 | */ |
||
218 | 12 | private function parseBool($value) |
|
224 | |||
225 | /** |
||
226 | * Parses a .env number |
||
227 | * |
||
228 | * @param string $value The value to parse |
||
229 | * |
||
230 | * @return int|float The parsed bool |
||
231 | */ |
||
232 | 9 | private function parseNumber($value) |
|
240 | |||
241 | /** |
||
242 | * Strips comments from a value |
||
243 | * |
||
244 | * @param string $value The value to strip comments from |
||
245 | * |
||
246 | * @return string value |
||
247 | */ |
||
248 | 39 | private function stripComments($value) |
|
252 | } |
||
253 |