c512072522255f48224844349ce7a69ae849927d
[yaffs-website] / vendor / drush / drush / lib / Drush / Make / Parser / ParserIni.php
1 <?php
2
3 /**
4  * @file
5  * Parser for INI format.
6  */
7
8 namespace Drush\Make\Parser;
9
10 class ParserIni implements ParserInterface {
11
12   /**
13    * Regex for parsing INI format.
14    */
15   private static $iniRegex = '
16     @^\s*                           # Start at the beginning of a line, ignoring leading whitespace
17     ((?:
18       [^=;\[\]]|                    # Key names cannot contain equal signs, semi-colons or square brackets,
19       \[[^\[\]]*\]                  # unless they are balanced and not nested
20     )+?)
21     \s*=\s*                         # Key/value pairs are separated by equal signs (ignoring white-space)
22     (?:
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
27     @msx';
28
29   /**
30    * {@inheritdoc}
31    */
32   public static function supportedFile($filename) {
33     $info = pathinfo($filename);
34     return isset($info['extension']) && $info['extension'] === 'make';
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function parse($data) {
41     if (preg_match_all(self::$iniRegex, $data, $matches, PREG_SET_ORDER)) {
42       $info = array();
43       foreach ($matches as $match) {
44         // Fetch the key and value string.
45         $i = 0;
46         foreach (array('key', 'value1', 'value2', 'value3') as $var) {
47           $$var = isset($match[++$i]) ? $match[$i] : '';
48         }
49         $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;
50
51         // Parse array syntax.
52         $keys = preg_split('/\]?\[/', rtrim($key, ']'));
53         $last = array_pop($keys);
54         $parent = &$info;
55
56         // Create nested arrays.
57         foreach ($keys as $key) {
58           if ($key == '') {
59             $key = count($parent);
60           }
61           if (isset($merge_item) && isset($parent[$key]) && !is_array($parent[$key])) {
62             $parent[$key] = array($merge_item => $parent[$key]);
63           }
64           if (!isset($parent[$key]) || !is_array($parent[$key])) {
65             $parent[$key] = array();
66           }
67           $parent = &$parent[$key];
68         }
69
70         // Handle PHP constants.
71         if (defined($value)) {
72           $value = constant($value);
73         }
74
75         // Insert actual value.
76         if ($last == '') {
77           $last = count($parent);
78         }
79         if (isset($merge_item) && isset($parent[$last]) && is_array($parent[$last])) {
80           $parent[$last][$merge_item] = $value;
81         }
82         else {
83           $parent[$last] = $value;
84         }
85       }
86       return $info;
87     }
88   }
89
90 }