| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class ClassCollector { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
protected $namespace = ''; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
protected $classes; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
protected $startToken; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
protected $tokens; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
protected $alias; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function getClasses( $code ) { |
| 56 |
$this->namespace = ''; |
| 57 |
$this->classes = []; |
| 58 |
$this->startToken = null; |
| 59 |
$this->alias = null; |
| 60 |
$this->tokens = []; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
{} |
| 66 |
); |
| 67 |
$lines = []; |
| 68 |
$matches = null; |
| 69 |
preg_match_all( |
| 70 |
|
| 71 |
'#^\t*(?:namespace |(final )?(abstract )?(class|interface|trait|enum) |class_alias\()[^;{]+[;{]\s*\}?#m', |
| 72 |
$code, |
| 73 |
$matches |
| 74 |
); |
| 75 |
if ( isset( $matches[0][0] ) ) { |
| 76 |
foreach ( $matches[0] as $match ) { |
| 77 |
$match = trim( $match ); |
| 78 |
if ( str_ends_with( $match, '{' ) ) { |
| 79 |
|
| 80 |
$match .= '}'; |
| 81 |
} |
| 82 |
$lines[] = $match; |
| 83 |
} |
| 84 |
} |
| 85 |
$code = '<?php ' . implode( "\n", $lines ) . "\n"; |
| 86 |
|
| 87 |
foreach ( token_get_all( $code ) as $token ) { |
| 88 |
if ( $this->startToken === null ) { |
| 89 |
$this->tryBeginExpect( $token ); |
| 90 |
} else { |
| 91 |
$this->tryEndExpect( $token ); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return $this->classes; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
protected function tryBeginExpect( $token ) { |
| 104 |
if ( is_string( $token ) ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
switch ( $token[0] ) { |
| 110 |
case T_NAMESPACE: |
| 111 |
case T_CLASS: |
| 112 |
case T_ENUM: |
| 113 |
case T_INTERFACE: |
| 114 |
case T_TRAIT: |
| 115 |
case T_DOUBLE_COLON: |
| 116 |
case T_NEW: |
| 117 |
$this->startToken = $token; |
| 118 |
break; |
| 119 |
case T_STRING: |
| 120 |
if ( $token[1] === 'class_alias' ) { |
| 121 |
$this->startToken = $token; |
| 122 |
$this->alias = []; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
protected function tryEndExpect( $token ) { |
| 133 |
|
| 134 |
switch ( $this->startToken[0] ) { |
| 135 |
case T_DOUBLE_COLON: |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
$this->startToken = null; |
| 140 |
break; |
| 141 |
case T_NEW: |
| 142 |
|
| 143 |
if ( !is_array( $token ) || $token[0] !== T_WHITESPACE ) { |
| 144 |
$this->startToken = null; |
| 145 |
} |
| 146 |
break; |
| 147 |
case T_NAMESPACE: |
| 148 |
if ( $token === ';' || $token === '{' ) { |
| 149 |
$this->namespace = $this->implodeTokens() . '\\'; |
| 150 |
} else { |
| 151 |
$this->tokens[] = $token; |
| 152 |
} |
| 153 |
break; |
| 154 |
|
| 155 |
case T_STRING: |
| 156 |
if ( $this->alias !== null ) { |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
if ( $token === '(' ) { |
| 176 |
|
| 177 |
$this->alias = [ 'target' => false, 'name' => false ]; |
| 178 |
} elseif ( $token === ',' ) { |
| 179 |
|
| 180 |
if ( $this->alias['target'] === false ) { |
| 181 |
$this->alias['target'] = true; |
| 182 |
} |
| 183 |
} elseif ( is_array( $token ) && $token[0] === T_CONSTANT_ENCAPSED_STRING ) { |
| 184 |
if ( $this->alias['target'] === true ) { |
| 185 |
|
| 186 |
|
| 187 |
$this->alias['name'] = self::stripQuotes( $token[1] ); |
| 188 |
} |
| 189 |
} elseif ( $token === ')' ) { |
| 190 |
|
| 191 |
$this->classes[] = $this->alias['name']; |
| 192 |
$this->alias = null; |
| 193 |
$this->startToken = null; |
| 194 |
} elseif ( !is_array( $token ) || ( |
| 195 |
$token[0] !== T_STRING && |
| 196 |
$token[0] !== T_DOUBLE_COLON && |
| 197 |
$token[0] !== T_CLASS && |
| 198 |
$token[0] !== T_WHITESPACE |
| 199 |
) ) { |
| 200 |
|
| 201 |
$this->alias = null; |
| 202 |
$this->startToken = null; |
| 203 |
} |
| 204 |
} |
| 205 |
break; |
| 206 |
|
| 207 |
case T_CLASS: |
| 208 |
case T_ENUM: |
| 209 |
case T_INTERFACE: |
| 210 |
case T_TRAIT: |
| 211 |
$this->tokens[] = $token; |
| 212 |
if ( is_array( $token ) && $token[0] === T_STRING ) { |
| 213 |
$this->classes[] = $this->namespace . $this->implodeTokens(); |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
private static function stripQuotes( $str ) { |
| 228 |
return str_replace( '\\\\', '\\', substr( $str, 1, -1 ) ); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
protected function implodeTokens() { |
| 238 |
$content = []; |
| 239 |
foreach ( $this->tokens as $token ) { |
| 240 |
$content[] = is_string( $token ) ? $token : $token[1]; |
| 241 |
} |
| 242 |
|
| 243 |
$this->tokens = []; |
| 244 |
$this->startToken = null; |
| 245 |
|
| 246 |
return trim( implode( '', $content ), " \n\t" ); |
| 247 |
} |
| 248 |
} |