PHPBuilder.php
2.89 KB
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
<?php
class PHPBuilder {
static function build () {
return new PHPBuilder() ;
}
function __construct() {
$this->lines = array() ;
}
function l() {
foreach ( func_get_args() as $lines ) {
if ( !$lines ) continue ;
if ( is_string( $lines ) ) $lines = preg_split( '/\r\n|\r|\n/', $lines ) ;
if ( !$lines ) continue ;
if ( $lines instanceof PHPBuilder ) $lines = $lines->lines ;
else $lines = array_map( 'ltrim', $lines ) ;
if ( !$lines ) continue ;
$this->lines = array_merge( $this->lines, $lines ) ;
}
return $this ;
}
function b() {
$args = func_get_args() ;
$entry = array_shift( $args ) ;
$block = new PHPBuilder() ;
call_user_func_array( array( $block, 'l' ), $args ) ;
$this->lines[] = array( $entry, $block->lines ) ;
return $this ;
}
function replace( $replacements, &$array = NULL ) {
if ( $array === NULL ) {
unset( $array ) ;
$array =& $this->lines ;
}
$i = 0 ;
while ( $i < count( $array ) ) {
/* Recurse into blocks */
if ( is_array( $array[$i] ) ) {
$this->replace( $replacements, $array[$i][1] ) ;
if ( count( $array[$i][1] ) == 0 ) {
$nextelse = isset( $array[$i+1] ) && is_array( $array[$i+1] ) && preg_match( '/^\s*else\s*$/i', $array[$i+1][0] ) ;
$delete = preg_match( '/^\s*else\s*$/i', $array[$i][0] ) ;
$delete = $delete || ( preg_match( '/^\s*if\s*\(/i', $array[$i][0] ) && !$nextelse ) ;
if ( $delete ) {
// Is this always safe? Not if the expression has side-effects.
// print "/* REMOVING EMPTY BLOCK: " . $array[$i][0] . "*/\n" ;
array_splice( $array, $i, 1 ) ;
continue ;
}
}
}
/* Handle replacing lines with NULL to remove, or string, array of strings or PHPBuilder to replace */
else {
if ( array_key_exists( $array[$i], $replacements ) ) {
$rep = $replacements[$array[$i]] ;
if ( $rep === NULL ) {
array_splice( $array, $i, 1 ) ;
continue ;
}
if ( is_string( $rep ) ) {
$array[$i] = $rep ;
$i++ ;
continue ;
}
if ( $rep instanceof PHPBuilder ) $rep = $rep->lines ;
if ( is_array( $rep ) ) {
array_splice( $array, $i, 1, $rep ) ; $i += count( $rep ) + 1 ;
continue ;
}
throw 'Unknown type passed to PHPBuilder#replace' ;
}
}
$i++ ;
}
return $this ;
}
function render( $array = NULL, $indent = "" ) {
if ( $array === NULL ) $array = $this->lines ;
$out = array() ;
foreach( $array as $line ) {
if ( is_array( $line ) ) {
list( $entry, $block ) = $line ;
$str = $this->render( $block, $indent . "\t" ) ;
if ( strlen( $str ) < 40 ) {
$out[] = $indent . $entry . ' { ' . ltrim( $str ) . ' }' ;
}
else {
$out[] = $indent . $entry . ' {' ;
$out[] = $str ;
$out[] = $indent . '}' ;
}
}
else {
$out[] = $indent . $line ;
}
}
return implode( PHP_EOL, $out ) ;
}
}