5 * Parser for INI format.
8 namespace Drush\Make\Parser;
10 class ParserIni implements ParserInterface {
13 * Regex for parsing INI format.
15 private static $iniRegex = '
16 @^\s* # Start at the beginning of a line, ignoring leading whitespace
18 [^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets,
19 \[[^\[\]]*\] # unless they are balanced and not nested
21 \s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space)
23 ("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes
24 (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
25 ([^\r\n]*?) # Non-quoted string
26 )\s*$ # Stop at the next end of a line, ignoring trailing whitespace
32 public static function supportedFile($filename) {
33 $info = pathinfo($filename);
34 return isset($info['extension']) && $info['extension'] === 'make';
40 public static function parse($data) {
41 if (preg_match_all(self::$iniRegex, $data, $matches, PREG_SET_ORDER)) {
43 foreach ($matches as $match) {
44 // Fetch the key and value string.
46 foreach (array('key', 'value1', 'value2', 'value3') as $var) {
47 $$var = isset($match[++$i]) ? $match[$i] : '';
49 $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;
51 // Parse array syntax.
52 $keys = preg_split('/\]?\[/', rtrim($key, ']'));
53 $last = array_pop($keys);
56 // Create nested arrays.
57 foreach ($keys as $key) {
59 $key = count($parent);
61 if (isset($merge_item) && isset($parent[$key]) && !is_array($parent[$key])) {
62 $parent[$key] = array($merge_item => $parent[$key]);
64 if (!isset($parent[$key]) || !is_array($parent[$key])) {
65 $parent[$key] = array();
67 $parent = &$parent[$key];
70 // Handle PHP constants.
71 if (defined($value)) {
72 $value = constant($value);
75 // Insert actual value.
77 $last = count($parent);
79 if (isset($merge_item) && isset($parent[$last]) && is_array($parent[$last])) {
80 $parent[$last][$merge_item] = $value;
83 $parent[$last] = $value;