acquia /
df
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Acquia\DF; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Contains methods for parsing and dumping data in the legacy info file format. |
||
| 7 | */ |
||
| 8 | class IniEncoder { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Serializes an array to legacy make format. |
||
| 12 | * |
||
| 13 | * @param array $input |
||
| 14 | * The data to serialize. |
||
| 15 | * |
||
| 16 | * @return string |
||
| 17 | * The serialized data. |
||
| 18 | */ |
||
| 19 | public function encode(array $input) { |
||
| 20 | return implode("\n", $this->doEncode($input)); |
||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Recursively serializes data to legacy make format. |
||
| 25 | * |
||
| 26 | * @param array $input |
||
| 27 | * The data to serialize. |
||
| 28 | * @param array $keys |
||
| 29 | * The current key path. |
||
| 30 | * |
||
| 31 | * @return string[] |
||
| 32 | * The serialized data as a flat array of lines. |
||
| 33 | */ |
||
| 34 | protected function doEncode(array $input, array $keys = []) { |
||
| 35 | $output = []; |
||
| 36 | |||
| 37 | foreach ($input as $key => $value) { |
||
| 38 | $keys[] = $key; |
||
| 39 | |||
| 40 | if (is_array($value)) { |
||
| 41 | if ($this->isAssociative($value)) { |
||
| 42 | $output = array_merge($output, $this->doEncode($value, $keys)); |
||
| 43 | } |
||
| 44 | else { |
||
| 45 | foreach ($value as $j) { |
||
| 46 | $output[] = $this->keysToString($keys) . '[] = ' . $j; |
||
| 47 | } |
||
| 48 | } |
||
| 49 | } |
||
| 50 | else { |
||
| 51 | $output[] = $this->keysToString($keys) . ' = ' . $value; |
||
| 52 | } |
||
| 53 | |||
| 54 | array_pop($keys); |
||
| 55 | } |
||
| 56 | |||
| 57 | return $output; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Transforms an key path to a string. |
||
| 62 | * |
||
| 63 | * @param string[] $keys |
||
| 64 | * The key path. |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | * The flattened key path. |
||
| 68 | */ |
||
| 69 | protected function keysToString(array $keys) { |
||
| 70 | $head = array_shift($keys); |
||
| 71 | if ($keys) { |
||
| 72 | return $head . '[' . implode('][', $keys) . ']'; |
||
| 73 | } |
||
| 74 | else { |
||
| 75 | return $head; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Tests if an array is associative. |
||
| 81 | * |
||
| 82 | * @param array $input |
||
| 83 | * The array to test. |
||
| 84 | * |
||
| 85 | * @return bool |
||
| 86 | * Whether or not the array has non-numeric keys. |
||
| 87 | */ |
||
| 88 | protected function isAssociative(array $input) { |
||
| 89 | $keys = implode('', array_keys($input)); |
||
| 90 | return !is_numeric($keys); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Parses data in Drupal's .info format. |
||
| 95 | * |
||
| 96 | * @see https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_parse_info_format/7.x |
||
| 97 | * |
||
| 98 | * @param string $data |
||
| 99 | * A string to parse. |
||
| 100 | * |
||
| 101 | * @return array |
||
| 102 | * The parsed data. |
||
| 103 | */ |
||
| 104 | public function parse($data) { |
||
| 105 | $info = array(); |
||
| 106 | |||
| 107 | if (preg_match_all(' |
||
| 108 | @^\s* # Start at the beginning of a line, ignoring leading whitespace |
||
| 109 | ((?: |
||
| 110 | [^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets, |
||
| 111 | \[[^\[\]]*\] # unless they are balanced and not nested |
||
| 112 | )+?) |
||
| 113 | \s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space) |
||
| 114 | (?: |
||
| 115 | ("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes |
||
| 116 | (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes |
||
| 117 | ([^\r\n]*?) # Non-quoted string |
||
| 118 | )\s*$ # Stop at the next end of a line, ignoring trailing whitespace |
||
| 119 | @msx', $data, $matches, PREG_SET_ORDER)) { |
||
| 120 | foreach ($matches as $match) { |
||
| 121 | // Fetch the key and value string. |
||
| 122 | $i = 0; |
||
| 123 | foreach (['key', 'value1', 'value2', 'value3'] as $var) { |
||
| 124 | $$var = isset($match[++$i]) ? $match[$i] : ''; |
||
| 125 | } |
||
| 126 | $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
| 127 | |||
| 128 | // Parse array syntax. |
||
| 129 | $keys = preg_split('/\]?\[/', rtrim($key, ']')); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 130 | $last = array_pop($keys); |
||
| 131 | $parent = &$info; |
||
| 132 | |||
| 133 | // Create nested arrays. |
||
| 134 | foreach ($keys as $key) { |
||
| 135 | if ($key == '') { |
||
| 136 | $key = count($parent); |
||
| 137 | } |
||
| 138 | if (!isset($parent[$key]) || !is_array($parent[$key])) { |
||
| 139 | $parent[$key] = []; |
||
| 140 | } |
||
| 141 | $parent = &$parent[$key]; |
||
| 142 | } |
||
| 143 | |||
| 144 | // Handle PHP constants. |
||
| 145 | if (preg_match('/^\w+$/i', $value) && defined($value)) { |
||
| 146 | $value = constant($value); |
||
| 147 | } |
||
| 148 | |||
| 149 | // Insert actual value. |
||
| 150 | if ($last == '') { |
||
| 151 | $last = count($parent); |
||
| 152 | } |
||
| 153 | $parent[$last] = $value; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | return $info; |
||
| 158 | } |
||
| 159 | |||
| 160 | } |