1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Drush Cerbere command line tools. |
5
|
|
|
* Copyright (C) 2015 - Sebastien Malot <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License along |
18
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
19
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Cerbere\Parser; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Ini |
26
|
|
|
* @package Cerbere\Parser |
27
|
|
|
*/ |
28
|
|
|
abstract class Ini implements ParserInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Parses data in Drupal's .info format. |
32
|
|
|
* |
33
|
|
|
* Data should be in an .ini-like format to specify values. White-space |
34
|
|
|
* generally doesn't matter, except inside values: |
35
|
|
|
* |
36
|
|
|
* @code |
37
|
|
|
* key = value |
38
|
|
|
* key = "value" |
39
|
|
|
* key = 'value' |
40
|
|
|
* key = "multi-line |
41
|
|
|
* value" |
42
|
|
|
* key = 'multi-line |
43
|
|
|
* value' |
44
|
|
|
* key |
45
|
|
|
* = |
46
|
|
|
* 'value' |
47
|
|
|
* @endcode |
48
|
|
|
* |
49
|
|
|
* Arrays are created using a HTTP GET alike syntax: |
50
|
|
|
* @code |
51
|
|
|
* key[] = "numeric array" |
52
|
|
|
* key[index] = "associative array" |
53
|
|
|
* key[index][] = "nested numeric array" |
54
|
|
|
* key[index][index] = "nested associative array" |
55
|
|
|
* @endcode |
56
|
|
|
* |
57
|
|
|
* PHP constants are substituted in, but only when used as the entire value. |
58
|
|
|
* Comments should start with a semi-colon at the beginning of a line. |
59
|
|
|
* |
60
|
|
|
* @param string $data |
61
|
|
|
* A string to parse. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
* The info array. |
65
|
|
|
* |
66
|
|
|
* @see drupal_parse_info_file() |
67
|
|
|
*/ |
68
|
|
|
protected function parseContent($data) |
69
|
|
|
{ |
70
|
|
|
$info = array(); |
71
|
|
|
|
72
|
|
|
if (preg_match_all( |
73
|
|
|
' |
74
|
|
|
@^\s* # Start at the beginning of a line, ignoring leading whitespace |
75
|
|
|
((?: |
76
|
|
|
[^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets, |
77
|
|
|
\[[^\[\]]*\] # unless they are balanced and not nested |
78
|
|
|
)+?) |
79
|
|
|
\s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space) |
80
|
|
|
(?: |
81
|
|
|
("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes |
82
|
|
|
(\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes |
83
|
|
|
([^\r\n]*?) # Non-quoted string |
84
|
|
|
)\s*$ # Stop at the next end of a line, ignoring trailing whitespace |
85
|
|
|
@msx', |
86
|
|
|
$data, |
87
|
|
|
$matches, |
88
|
|
|
PREG_SET_ORDER |
89
|
|
|
)) { |
90
|
|
|
foreach ($matches as $match) { |
91
|
|
|
// Fetch the key and value string. |
92
|
|
|
$i = 0; |
93
|
|
|
$key = $value1 = $value2 = $value3 = ''; |
94
|
|
|
foreach (array('key', 'value1', 'value2', 'value3') as $var) { |
95
|
|
|
$$var = isset($match[++$i]) ? $match[$i] : ''; |
96
|
|
|
} |
97
|
|
|
$value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3; |
98
|
|
|
|
99
|
|
|
// Parse array syntax. |
100
|
|
|
$keys = preg_split('/\]?\[/', rtrim($key, ']')); |
101
|
|
|
$last = array_pop($keys); |
102
|
|
|
$parent = &$info; |
103
|
|
|
|
104
|
|
|
// Create nested arrays. |
105
|
|
|
foreach ($keys as $key) { |
106
|
|
|
if ($key == '') { |
107
|
|
|
$key = count($parent); |
108
|
|
|
} |
109
|
|
|
if (!isset($parent[$key]) || !is_array($parent[$key])) { |
110
|
|
|
$parent[$key] = array(); |
111
|
|
|
} |
112
|
|
|
$parent = &$parent[$key]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Handle PHP constants. |
116
|
|
|
if (preg_match('/^\w+$/i', $value) && defined($value)) { |
117
|
|
|
$value = constant($value); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Insert actual value. |
121
|
|
|
if ($last == '') { |
122
|
|
|
$last = count($parent); |
123
|
|
|
} |
124
|
|
|
$parent[$last] = $value; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $info; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $filename |
133
|
|
|
*/ |
134
|
|
|
public function processFile($filename) |
135
|
|
|
{ |
136
|
|
|
$content = file_get_contents($filename); |
137
|
|
|
|
138
|
|
|
$this->processContent($content); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|