-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-packages.php
More file actions
76 lines (64 loc) · 2.75 KB
/
Copy pathgenerate-packages.php
File metadata and controls
76 lines (64 loc) · 2.75 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
<?php declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
function classLikeExists(string $fqcn): bool {
try {
return class_exists($fqcn, true) || interface_exists($fqcn, true) || trait_exists($fqcn, true) || enum_exists($fqcn, true);
} catch (\Throwable) {
return false;
}
}
$installed = require __DIR__ . '/vendor/composer/installed.php';
$classMap = [];
foreach ($installed['versions'] as $packageName => $info) {
$installPath = $info['install_path'] ?? null;
if (!$installPath || !is_dir($installPath)) {
continue;
}
$pkgComposer = $installPath . '/composer.json';
if (!file_exists($pkgComposer)) {
continue;
}
$pkgConfig = json_decode(file_get_contents($pkgComposer), true);
$psr4 = $pkgConfig['autoload']['psr-4'] ?? [];
foreach ($psr4 as $namespace => $dirs) {
foreach ((array) $dirs as $dir) {
$fullDir = realpath($installPath . '/' . $dir);
if (!$fullDir || !is_dir($fullDir)) {
continue;
}
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fullDir)) as $file) {
if ($file->getExtension() !== 'php') {
continue;
}
$tokens = PhpToken::tokenize(file_get_contents($file->getPathname()));
$currentNamespace = '';
for ($i = 0; $i < count($tokens); $i++) {
if ($tokens[$i]->is(T_NAMESPACE)) {
$currentNamespace = '';
$i++;
while (isset($tokens[$i]) && !$tokens[$i]->is(';')) {
if ($tokens[$i]->is([T_NAME_QUALIFIED, T_STRING])) {
$currentNamespace .= $tokens[$i]->text;
}
$i++;
}
} elseif ($tokens[$i]->is([T_CLASS, T_INTERFACE, T_TRAIT, T_ENUM])) {
$j = $i + 1;
while (isset($tokens[$j]) && $tokens[$j]->is(T_WHITESPACE)) {
$j++;
}
if (isset($tokens[$j]) && $tokens[$j]->is(T_STRING)) {
$fqcn = $currentNamespace . '\\' . $tokens[$j]->text;
if (classLikeExists($fqcn)) {
$classMap[$fqcn] = $packageName;
}
}
}
}
}
}
}
}
ksort($classMap);
file_put_contents(__DIR__ . '/data/classes.json', json_encode($classMap, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n");
echo "Generated data/classes.json with " . count($classMap) . " entries.\n";