-
Notifications
You must be signed in to change notification settings - Fork 4
/
createclassap
executable file
·125 lines (110 loc) · 4.05 KB
/
createclassap
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
#!/usr/bin/php
<?php
/**
* ______ __
* __ /\\\\\\\\_ /\\\
* /\\\ /\\\//////\\\_ \/\\\
* /\\\// \/// \//\\\ ________ ___\/\\\ _______
* /\\\// /\\\ /\\\\\\\\\_ /\\\\\\\\\ /\\\\\\\\_
* /\\\//_ /\\\\/ /\\\/////\\\ /\\\////\\\ /\\\/////\\\
* \////\\\ __ /\\\/ \/\\\ \/\\\ \/\\\ \/\\\ /\\\\\\\\\\\
* \////\\\ __ \///_ \/\\\___\/\\\ \/\\\__\/\\\ \//\\\//////_
* \////\\\ /\\\ \/\\\\\\\\\\ \//\\\\\\\\\ \//\\\\\\\\\
* \/// \/// \/\\\////// \///////// \/////////
* \/\\\
* \///
*
* P(?:ointless|ortable|HP) Demo Engine/
*/
declare(strict_types = 1);
/**
* ClassMapBuilder
*
* Crawls the given directory and builds a simple classmap
*/
class ClassMapBuilder {
const EXCLUDE_PATTERNS = [
'.' => 1,
'..' => 1
];
const EXCLUDE_DIRS = [
'src/tests' => true
];
/** @var string[] $aFiles */
private array $aFiles = [];
/** @var string[] $aClassMap */
private array $aClassMap = [];
/**
* Main entry point
*
* @param string $sSourceDir
*/
public function execute(string $sSourceDir, string $sNameSpace, bool $bCheckGlobalFunctions = false) {
echo "Building class map from ", $sSourceDir, "\n";
$this->aFiles = [];
$this->traverse($sSourceDir);
$this->aClassMap = [];
foreach ($this->aFiles as $sSourcePath) {
$this->processFile($sSourcePath);
}
$sCode = "<?php\n\nnamespace " . $sNameSpace . ";\n\nconst CLASS_MAP = " .
str_replace(['array (', ')'], ['[', ']'], var_export($this->aClassMap, true)) .
";";
file_put_contents('src/classmap.php', $sCode);
if ($bCheckGlobalFunctions) {
echo "Checking for global functions without use-qualifier...\n";
foreach ($this->aFiles as $sFile) {
$sTest = shell_exec('php -dvld.active=1 -dvld.execute=0 ' . $sFile . ' 2>&1 | grep INIT_NS');
if (!empty($sTest)) {
echo $sFile, ":\n", $sTest, "\n";
}
}
}
}
/**
* Recurse over the directory structure
*
* @param string $sDirectory
*/
private function traverse(string $sDirectory) {
$oCurrentDir = dir($sDirectory);
$sDirectory = rtrim($sDirectory, '/');
$aSubdirs = [];
while (false !== ($sItem = $oCurrentDir->read())) {
if (isset(self::EXCLUDE_PATTERNS[$sItem])) {
continue;
}
$sPath = $sDirectory . '/' . $sItem;
if (isset(self::EXCLUDE_DIRS[$sDirectory]) || is_link($sPath)) {
continue;
}
if (is_dir($sPath)) {
$aSubdirs[] = $sPath;
} else {
if ('.php' === substr($sPath, -4, 4)) {
$this->aFiles[] = $sPath;
}
}
}
foreach ($aSubdirs as $sSubdir) {
$this->traverse($sSubdir);
}
}
/**
* Extract namespace, interface, class and trait definitions from a source file
*
* @param string $sFile
*/
private function processFile(string $sFile) {
$sContent = file_get_contents($sFile);
preg_match('/^namespace\s+([a-z0-9_\\\]+)/im', $sContent, $aMatches);
$sNamespace = $aMatches[1] . '\\';
preg_match_all('/^(class|interface|trait|abstract\s+class|final\s+class)\s+([a-z0-9_]+)/im', $sContent, $aMatches);
$aMatches = $aMatches[2];
$sFile = substr($sFile, 3);
foreach ($aMatches as $sName) {
$this->aClassMap[$sNamespace . $sName] = $sFile;
}
}
}
(new ClassMapBuilder)->execute('src/', 'ABadCafe\PDE', isset($_SERVER['argv'][1]));