Index: trunk/tools/switch-master/ConfEditor.php |
— | — | @@ -245,6 +245,84 @@ |
246 | 246 | } |
247 | 247 | return $out; |
248 | 248 | } |
| 249 | + |
| 250 | + /** |
| 251 | + * Get the variables defined in the text |
| 252 | + * @return array( varname => value ) |
| 253 | + */ |
| 254 | + function getVars() { |
| 255 | + $vars = array(); |
| 256 | + $this->parse(); |
| 257 | + foreach( $this->pathInfo as $path => $data ) { |
| 258 | + if ( $path[0] != '$' ) |
| 259 | + continue; |
| 260 | + $trimmedPath = substr( $path, 1 ); |
| 261 | + $name = $data['name']; |
| 262 | + if ( $name[0] == '@' ) |
| 263 | + continue; |
| 264 | + if ( $name[0] == '$' ) |
| 265 | + $name = substr( $name, 1 ); |
| 266 | + $parentPath = substr( $trimmedPath, 0, |
| 267 | + strlen( $trimmedPath ) - strlen( $name ) ); |
| 268 | + if( substr( $parentPath, -1 ) == '/' ) |
| 269 | + $parentPath = substr( $parentPath, 0, -1 ); |
| 270 | + |
| 271 | + $value = substr( $this->text, $data['valueStartByte'], |
| 272 | + $data['valueEndByte'] - $data['valueStartByte'] |
| 273 | + ); |
| 274 | + $this->setVar( $vars, $parentPath, $name, |
| 275 | + $this->parseScalar( $value ) ); |
| 276 | + } |
| 277 | + return $vars; |
| 278 | + } |
| 279 | + |
| 280 | + /** |
| 281 | + * Set a value in an array, unless it's set already. For instance, |
| 282 | + * setVar( $arr, 'foo/bar', 'baz', 3 ); will set |
| 283 | + * $arr['foo']['bar']['baz'] = 3; |
| 284 | + * @param $array array |
| 285 | + * @param $path string slash-delimited path |
| 286 | + * @param $key mixed Key |
| 287 | + * @param $value mixed Value |
| 288 | + */ |
| 289 | + function setVar( &$array, $path, $key, $value ) { |
| 290 | + echo "$path [$key] = $value\n"; |
| 291 | + $pathArr = explode( '/', $path ); |
| 292 | + $target =& $array; |
| 293 | + if ( $path !== '' ) { |
| 294 | + foreach ( $pathArr as $p ) { |
| 295 | + if( !isset( $target[$p] ) ) |
| 296 | + $target[$p] = array(); |
| 297 | + $target =& $target[$p]; |
| 298 | + } |
| 299 | + } |
| 300 | + if ( !isset( $target[$key] ) ) |
| 301 | + $target[$key] = $value; |
| 302 | + } |
| 303 | + |
| 304 | + /** |
| 305 | + * Parse a scalar value in PHP |
| 306 | + * @return mixed Parsed value |
| 307 | + */ |
| 308 | + function parseScalar( $str ) { |
| 309 | + if ( @$str[0] == '\'' ) |
| 310 | + // Single-quoted string |
| 311 | + return strtr( substr( $str, 1, -1 ), |
| 312 | + array( '\\\'' => '\'', '\\\\' => '\\' ) ); |
| 313 | + if ( @$str[0] == '"' ) |
| 314 | + // Double-quoted string |
| 315 | + return strtr( stripcslashes( substr( $str, 1, -1 ) ), |
| 316 | + array( '\'' => '\\\'' ) ); |
| 317 | + if ( substr( $str, 0, 4 ) == 'true' ) |
| 318 | + return true; |
| 319 | + if ( substr( $str, 0, 5 ) == 'false' ) |
| 320 | + return false; |
| 321 | + if ( substr( $str, 0, 4 ) == 'null' ) |
| 322 | + return null; |
| 323 | + // Must be some kind of numeric value, so let PHP's weak typing |
| 324 | + // be useful for a change |
| 325 | + return $str; |
| 326 | + } |
249 | 327 | |
250 | 328 | /** |
251 | 329 | * Replace the byte offset region of the source with $newText. |
— | — | @@ -560,7 +638,7 @@ |
561 | 639 | $this->error( "expected a string or number for the array key" ); |
562 | 640 | } |
563 | 641 | if ( $token->type == T_CONSTANT_ENCAPSED_STRING ) { |
564 | | - $text = stripslashes( substr( $token->text, 1, -1 ) ); |
| 642 | + $text = $thsi->parseScalar( $token->text ); |
565 | 643 | } else { |
566 | 644 | $text = $token->text; |
567 | 645 | } |