-
Notifications
You must be signed in to change notification settings - Fork 6
/
php-class-splitter.php
43 lines (37 loc) · 1.1 KB
/
php-class-splitter.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
<?php
$file = $argv[1];
$dest = rtrim($argv[2], '/');
$tokens = token_get_all(file_get_contents($file));
$buffer = false;
while ($token = next($tokens)) {
if ($token[0] == T_CLASS) {
$buffer = true;
$name = null;
$code = '';
$braces = 1;
do {
$code .= is_string($token) ? $token : $token[1];
if (is_array($token)
&& $token[0] == T_STRING
&& empty($name)) {
$name = $token[1];
}
} while (!(is_string($token) && $token === '{') && !(is_array($token) && $token[1] == '{') && $token = next($tokens));
} elseif ($buffer) {
if (is_array($token)) {
$token = $token[1];
}
$code .= $token;
if ($token == '{') {
$braces++;
} elseif ($token == '}') {
$braces--;
if ($braces == 0) {
$buffer = false;
$file = $dest . '/' . $name . '.php';
$code = '<?php' . PHP_EOL . $code;
file_put_contents($file, $code);
}
}
}
}