1 | <?php |
||
28 | class ValueParser extends AbstractParser |
||
29 | { |
||
30 | /** |
||
31 | * The regex to get the content between double quote (") strings, ignoring escaped quotes. |
||
32 | * Unescaped: "(?:[^"\\]*(?:\\.)?)*" |
||
33 | * |
||
34 | * @var string REGEX_QUOTE_DOUBLE_STRING |
||
35 | */ |
||
36 | const REGEX_QUOTE_DOUBLE_STRING = '"(?:[^\"\\\\]*(?:\\\\.)?)*\"'; |
||
37 | |||
38 | /** |
||
39 | * The regex to get the content between single quote (') strings, ignoring escaped quotes |
||
40 | * Unescaped: '(?:[^'\\]*(?:\\.)?)*' |
||
41 | * |
||
42 | * @var string REGEX_QUOTE_SINGLE_STRING |
||
43 | */ |
||
44 | const REGEX_QUOTE_SINGLE_STRING = "'(?:[^'\\\\]*(?:\\\\.)?)*'"; |
||
45 | |||
46 | /** |
||
47 | * The value types that Env supports |
||
48 | * |
||
49 | * @var array $value_types |
||
50 | */ |
||
51 | private static $value_types = array( |
||
52 | 'string', |
||
53 | 'bool', |
||
54 | 'number', |
||
55 | 'null', |
||
56 | ); |
||
57 | |||
58 | /** |
||
59 | * The map to convert escaped characters into real characters |
||
60 | * |
||
61 | * @var array $character_map |
||
62 | */ |
||
63 | private static $character_map = array( |
||
64 | "\\n" => "\n", |
||
65 | "\\\"" => "\"", |
||
66 | '\\\'' => "'", |
||
67 | '\\t' => "\t" |
||
68 | ); |
||
69 | |||
70 | /** |
||
71 | * The parser for variables |
||
72 | * |
||
73 | * @var \M1\Env\Parser\VariableParser $variable_parser |
||
74 | */ |
||
75 | private $variable_parser; |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | * |
||
80 | * @param \M1\Env\Parser $parser The parent parser |
||
81 | * @param array $context Variables context |
||
82 | */ |
||
83 | 66 | public function __construct($parser, array $context = array()) |
|
89 | |||
90 | /** |
||
91 | * Parses a .env value |
||
92 | * |
||
93 | * @param string $value The value to parse |
||
94 | * |
||
95 | * @return string|null The parsed key, or null if the key is a comment |
||
96 | */ |
||
97 | 48 | public function parse($value) |
|
107 | |||
108 | /** |
||
109 | * Parses a .env value |
||
110 | * |
||
111 | * @param string $value The value to parse |
||
112 | * |
||
113 | * @return string|null The parsed value, or null if the value is null |
||
114 | * |
||
115 | * @uses parseString |
||
116 | * @uses parseNull |
||
117 | * @uses parseBool |
||
118 | * @uses parseNumber |
||
119 | */ |
||
120 | 48 | private function parseValue($value) |
|
138 | |||
139 | /** |
||
140 | * Gets the functions for the value type |
||
141 | * |
||
142 | * @param string $type The value type |
||
143 | * |
||
144 | * @return string[] The is and parse function names |
||
145 | */ |
||
146 | 48 | private function fetchFunctionNames($type) |
|
155 | |||
156 | /** |
||
157 | * Parses a .env string |
||
158 | * |
||
159 | * @param string $value The value to parse |
||
160 | * |
||
161 | * @return string The parsed string |
||
162 | */ |
||
163 | 24 | private function parseString($value) |
|
185 | |||
186 | /** |
||
187 | * Gets the regex matches in the string |
||
188 | * |
||
189 | * @param string $regex The regex to use |
||
190 | * @param string $value The value to parse |
||
191 | * @param string $symbol The symbol we're parsing for |
||
192 | * |
||
193 | * @throws \M1\Env\Exception\ParseException If the string has a missing end quote |
||
194 | * |
||
195 | * @return string[] The matches based on the regex and the value |
||
196 | */ |
||
197 | 24 | private function fetchStringMatches($value, $regex, $symbol) |
|
209 | |||
210 | /** |
||
211 | * Parses a .env null value |
||
212 | * |
||
213 | * @param string $value The value to parse |
||
214 | * |
||
215 | * @return null Null value |
||
216 | */ |
||
217 | 12 | private function parseNull($value) |
|
221 | |||
222 | /** |
||
223 | * Parses a .env unquoted string |
||
224 | * |
||
225 | * @param string $value The value to parse |
||
226 | * |
||
227 | * @return string The parsed string |
||
228 | */ |
||
229 | 42 | private function parseUnquotedString($value) |
|
237 | |||
238 | /** |
||
239 | * Parses a .env bool |
||
240 | * |
||
241 | * @param string $value The value to parse |
||
242 | * |
||
243 | * @return bool The parsed bool |
||
244 | */ |
||
245 | 12 | private function parseBool($value) |
|
251 | |||
252 | /** |
||
253 | * Parses a .env number |
||
254 | * |
||
255 | * @param string $value The value to parse |
||
256 | * |
||
257 | * @return int|float The parsed bool |
||
258 | */ |
||
259 | 9 | private function parseNumber($value) |
|
267 | } |
||
268 |