-
Notifications
You must be signed in to change notification settings - Fork 32
/
lib-vendor-organizer.php
104 lines (89 loc) · 2.62 KB
/
lib-vendor-organizer.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
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
#!/usr/bin/env php
<?php
$sourceDirectory = $argv[1];
$sourceDirectory = rtrim($sourceDirectory, '/') . '/';
if (!str_starts_with('/', $sourceDirectory)) {
$sourceDirectory = getcwd() . '/' . $sourceDirectory;
}
$stripNamespacePrefix = $argv[2] ?? '';
if ($stripNamespacePrefix) {
printf("Namespace Prefix to strip from destination dir is %s%s", $stripNamespacePrefix, PHP_EOL);
}
if (!file_exists($sourceDirectory) || !is_dir($sourceDirectory)) {
print("Directory not found");
exit(1);
}
$organizationList = [];
foreach(scandir($sourceDirectory) as $file) {
if (!is_dir($sourceDirectory . $file) || $file === '.' || $file === '..') {
continue;
}
$organizationList[] = $sourceDirectory . $file . '/';
}
$projectList = [];
foreach($organizationList as $organizationDir) {
foreach(scandir($organizationDir) as $file) {
if (!is_dir($organizationDir . $file) || $file === '.' || $file === '..') {
continue;
}
$projectList[] = $organizationDir . $file . '/';
}
}
$knownDestination = [];
foreach ($projectList as $projectDir) {
if (!file_exists($projectDir . 'composer.json')) {
continue;
}
$projectInfo = json_decode(file_get_contents($projectDir . 'composer.json'), true);
if (!isset($projectInfo['autoload']['psr-4'])) {
printf("No supported autoload configuration in %s" . PHP_EOL, $projectDir);
exit(2);
}
foreach ($projectInfo['autoload']['psr-4'] as $namespace => $codeDir) {
if ($stripNamespacePrefix !== '' && strpos($namespace, $stripNamespacePrefix) === 0) {
$namespace = str_replace($stripNamespacePrefix, '', $namespace);
}
$destination = rtrim($sourceDirectory, '/') . str_replace('\\', '/', $namespace);
if (!in_array($destination, $knownDestination)) {
if (file_exists($destination)) {
rmdir_recursive($destination);
}
mkdir($destination, 0777, true);
$knownDestination[] = $destination;
}
if (!rename_or_move($projectDir . $codeDir, $destination)) {
printf("Failed to move %s to %s" . PHP_EOL, $projectDir . $codeDir, $destination);
exit(3);
}
}
}
foreach($organizationList as $organizationDir) {
rmdir_recursive($organizationDir);
}
function rmdir_recursive($dir) {
foreach(scandir($dir) as $file) {
if ('.' === $file || '..' === $file) {
continue;
}
if (is_dir("$dir/$file")) {
rmdir_recursive("$dir/$file");
} else {
unlink("$dir/$file");
}
}
rmdir($dir);
}
function rename_or_move(string $orig, string $dest): bool {
if (@rename($orig, $dest)) {
return true;
}
foreach (scandir($orig) as $file) {
if ('.' === $file || '..' === $file) {
continue;
}
if (!rename_or_move("$orig/$file", "$dest/$file")) {
return false;
}
}
return true;
}