forked from mantisbt/mantisbt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigParser.class.php
234 lines (203 loc) · 5.87 KB
/
ConfigParser.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
# MantisBT - A PHP based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* Configuration Parser class.
* @copyright Copyright 2016 MantisBT Team - [email protected]
* @link http://www.mantisbt.org
* @package MantisBT
* @subpackage classes
*/
/**
* Configuration Parser class
*
* Simple PHP code parser for scalar and array types
*
* @package MantisBT
* @subpackage classes
*
* @uses Tokenizer
*/
class ConfigParser
{
/**
* Define how extra tokens should be handled by parse() method
*/
const EXTRA_TOKENS_IGNORE = 0;
const EXTRA_TOKENS_ERROR = 1;
/**
* @var Tokenizer $tokens
*/
protected $tokens;
/**
* Parser constructor.
* @param string $p_code PHP code to parse
*/
public function __construct( $p_code ) {
$this->tokens = new Tokenizer( $p_code );
}
/**
* Parse the code for a variable assignment.
* Handles scalar types, and various array types (simple, associative,
* multi-dimensional)
* @param integer $p_extra_tokens Define how extra tokens should be handled
* - EXTRA_TOKENS_IGNORE silently ignore any
* extra code given after the first token
* - EXTRA_TOKENS_ERROR (default) throws an
* exception if extra code is found
* @return mixed variable
* @throws Exception when there are unexpected or extra tokens
*/
public function parse( $p_extra_tokens = self::EXTRA_TOKENS_ERROR ) {
switch( $this->tokens->type() ) {
case T_ARRAY:
$t_result = $this->process_array();
break;
case T_CONSTANT_ENCAPSED_STRING:
case T_STRING:
case T_LNUMBER:
case T_DNUMBER:
case '-':
case '+':
$t_result = $this->process_value();
break;
default:
throw new Exception( 'Unexpected token "' . $this->tokens->value() . '"' );
}
# Make sure we have processed all tokens
if( $p_extra_tokens == self::EXTRA_TOKENS_ERROR && !$this->tokens->is_empty() ) {
throw new Exception( 'Extra tokens found "' . $this->tokens->get_string() .'":' );
}
return $t_result;
}
/**
* Check if the passed string is a constant and returns its value if yes,
* or the string itself if not
* @param string $p_name String to check.
* @return mixed|string value of constant $p_name, or $p_name itself
*/
public static function constant_replace( $p_name ) {
$t_name = trim( $p_name );
if( is_string( $t_name ) && defined( $t_name ) ) {
# we have a constant
return constant( $t_name );
}
return $t_name;
}
/**
* Recursively process array declarations.
* @return array
* @throws Exception when there's an invalid token
*/
protected function process_array() {
$t_array = array();
$t_count = 0;
$this->tokens->ensure_matches( T_ARRAY );
$this->tokens->ensure_matches( '(' );
# Loop until we reach the end of the array
while( !$this->tokens->matches( ')' ) ) {
# A comma is required before each element except the first one
if ($t_count > 0) {
$this->tokens->ensure_matches(',');
}
switch( $this->tokens->type() ) {
# Nested array
case T_ARRAY:
$t_array[] = $this->process_array();
break;
# Value
case T_CONSTANT_ENCAPSED_STRING:
case T_STRING:
case T_LNUMBER:
case T_DNUMBER:
$t_str = $this->process_value();
if( $this->tokens->matches( T_DOUBLE_ARROW ) ) {
# key => value
$this->tokens->pop();
if( $this->tokens->matches( T_ARRAY ) ) {
$t_array[$t_str] = $this->process_array();
} else {
$t_array[$t_str] = $this->process_value();
}
} else {
# Simple value
$t_array[] = $t_str;
}
break;
case ')':
# Cover the trailing ',' case
break;
default:
throw new Exception("Invalid token '" . $this->tokens->value() . "'");
}
$t_count++;
}
$this->tokens->ensure_matches( ')' );
return $t_array;
}
/**
* Process a scalar value.
* Handles string literals including defined constants
* @return mixed
* @throws Exception when there's an unexpected value
*/
protected function process_value() {
# String literals
if( $this->tokens->matches( T_STRING ) ) {
$t_token = $this->tokens->pop();
$t_value = $t_token[1];
# PHP Standard string literals
switch (strtolower($t_value)) {
case 'null':
return null;
case 'true':
return true;
case 'false':
return false;
}
# Defined constants
$t_value = $this->constant_replace( $t_value );
if( $t_value !== $t_token[1] ) {
return $t_value;
}
throw new Exception("Unknown string literal '$t_value'");
}
# Strings
if( $this->tokens->matches( T_CONSTANT_ENCAPSED_STRING ) ) {
$t_value = $this->tokens->pop();
return (string)stripslashes( substr( $t_value[1], 1, -1 ) );
}
# Numbers
$t_negate = 1;
if( $this->tokens->matches( '-' ) ) {
$this->tokens->pop();
$t_negate = -1;
}
if( $this->tokens->matches( '+' ) ) {
$this->tokens->pop();
}
# Integers
if( $this->tokens->matches( T_LNUMBER ) ) {
$t_value = $this->tokens->pop();
return $t_negate * (int)$t_value[1];
}
# Floating point
if( $this->tokens->matches( T_DNUMBER ) ) {
$t_value = $this->tokens->pop();
return $t_negate * (float)$t_value[1];
}
# Anything else
throw new Exception( "Unexpected value" . $this->tokens->value() );
}
}