-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfix-double-namespace.php
More file actions
121 lines (103 loc) · 4.14 KB
/
fix-double-namespace.php
File metadata and controls
121 lines (103 loc) · 4.14 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
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
<?php
$directory = __DIR__ . '/inc/lib';
$pattern = '/Smartling\\\\Vendor\\\\Smartling\\\\Vendor\\\\/';
$replacement = 'Smartling\\Vendor\\';
$pass = 1;
$columns = 120;
do {
echo "Scanning directory: $directory, pass $pass\n";
echo str_repeat('-', $columns) . "\n";
$filesFound = 0;
$filesModified = 0;
$totalReplacements = 0;
foreach (new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST,
) as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$filesFound++;
$filePath = $file->getPathname();
$content = file_get_contents($filePath);
$count = preg_match_all($pattern, $content);
if ($count > 0) {
file_put_contents($filePath, preg_replace($pattern, $replacement, $content));
$filesModified++;
$totalReplacements += $count;
$relativePath = str_replace(__DIR__ . '/', '', $filePath);
echo "✓ Modified: $relativePath ($count replacements)\n";
}
}
}
echo str_repeat('-', $columns) . "\n";
echo "Summary:\n";
echo " Files found: $filesFound\n";
echo " Files modified: $filesModified\n";
echo " Total replacements: $totalReplacements\n";
echo "\nDone!\n";
++$pass;
} while ($filesModified > 0);
// Copy classmap files that are present in inc/third-party but absent from inc/lib.
// The namespacer skips namespace-less files (e.g. ValueWrapper.php), leaving them missing
// in inc/lib even though the generated autoloader still expects them.
$libDir = __DIR__ . '/inc/lib';
$thirdPartyDir = __DIR__ . '/inc/third-party';
$prefix = 'smartling-connector-';
echo "\n" . str_repeat('=', $columns) . "\n";
echo "Checking for classmap files missing from inc/lib...\n";
echo str_repeat('=', $columns) . "\n";
$filesCopied = 0;
$filesMissing = 0;
foreach (new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($libDir, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST,
) as $file) {
if (!$file->isFile() || $file->getFilename() !== 'composer.json') {
continue;
}
$packageDir = $file->getPath();
try {
$composer = json_decode(file_get_contents($file->getPathname()), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
echo "✗ Failed to parse: " . str_replace(__DIR__ . '/', '', $file->getPathname()) . " ({$e->getMessage()})\n";
continue;
}
$classmap = $composer['autoload']['classmap'] ?? [];
if (empty($classmap)) {
continue;
}
// Derive the original vendor/package path from the scoped name.
// e.g. "smartling-connector-symfony/cache" -> "symfony/cache"
$relativePackageDir = ltrim(str_replace($libDir, '', $packageDir), '/');
$parts = explode('/', $relativePackageDir, 2);
if (count($parts) !== 2 || !str_starts_with($parts[0], $prefix)) {
continue;
}
$originalVendor = substr($parts[0], strlen($prefix));
$originalPackageDir = $thirdPartyDir . '/' . $originalVendor . '/' . $parts[1];
foreach ($classmap as $classmapEntry) {
$libFile = $packageDir . '/' . ltrim($classmapEntry, '/');
if (file_exists($libFile)) {
continue;
}
$filesMissing++;
$sourceFile = $originalPackageDir . '/' . ltrim($classmapEntry, '/');
$relativeLibPath = str_replace(__DIR__ . '/', '', $libFile);
if (!file_exists($sourceFile)) {
echo "✗ Missing in both lib and third-party: $relativeLibPath\n";
continue;
}
$dir = dirname($libFile);
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
echo "✗ Failed to create directory: $dir\n";
continue;
}
copy($sourceFile, $libFile);
$filesCopied++;
echo "✓ Copied: $relativeLibPath\n";
}
}
echo str_repeat('-', $columns) . "\n";
echo "Summary:\n";
echo " Missing classmap files found: $filesMissing\n";
echo " Files copied: $filesCopied\n";
echo "\nDone!\n";