forked from rapid7/hackazon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pack_vendor.php
91 lines (72 loc) · 2.6 KB
/
pack_vendor.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
<?php
/**
* Created by IntelliJ IDEA.
* User: Nikolay Chervyakov
* Date: 26.11.2014
* Time: 16:26
*
* The PHP has a problem with compressing more than 2042 files in one PHAR:
* @link[https://bugs.php.net/bug.php?id=53467]
* Here we avoid putting too much files into one phar
*/
$fileName = __DIR__ . '/vendor.phar';
$tmpFileName = $fileName . '.tmp';
$vendorDir = __DIR__ . DIRECTORY_SEPARATOR . 'vendor';
if (file_exists($tmpFileName)) {
unlink($tmpFileName);
}
$phars = [];
//$phar->buildFromIterator(
// new RecursiveIteratorIterator(
// new RecursiveDirectoryIterator($vendorDir)),
// $vendorDir);
foreach (new DirectoryIterator($vendorDir) as $dir) {
if (in_array($dir->getFilename(), ['..', '.', 'composer']) || !$dir->isDir()) {
continue;
}
foreach (new DirectoryIterator($dir->getRealPath()) as $subDir) {
if (in_array($subDir->getFilename(), ['..', '.']) || !$subDir->isDir()) {
continue;
}
$fName = $subDir->getRealPath() . '.phar';
$fNameTmp = $fName . '.tmp';
//var_dump([$fName, $fNameTmp, $subDir->getRealPath()]);exit;
$phar = new Phar($fNameTmp);
$phar->buildFromDirectory($subDir->getRealPath(), '#\\.(?!git)#');
if (Phar::canCompress(Phar::GZ)) {
$phar->compressFiles(Phar::GZ);
} else if (Phar::canCompress(Phar::BZ2)) {
$phar->compressFiles(Phar::BZ2);
}
if (file_exists($fName)) {
unlink($fName);
}
unset($phar);
rename($fNameTmp, $fName);
//delDirTree($subDir->getRealPath());
$phars[$dir->getFilename() . '/' . $subDir->getFilename()] =
str_replace(DIRECTORY_SEPARATOR, '/', str_replace($vendorDir, '', $fName));
}
}
$autoloadFiles = [
'composer/autoload_classmap.php',
'composer/autoload_files.php',
'composer/autoload_namespaces.php',
'composer/autoload_psr4.php',
];
foreach ($autoloadFiles as $file) {
$filePath = __DIR__.'/vendor/'.$file;
$content = file_get_contents($filePath);
$content = preg_replace('#(?<!\'phar://\' . )\\$vendorDir\\s+.\\s+\'(/[-\\w\\d_]+/[-\\w\\d_]+)#',
'\'phar://\' . $vendorDir . \'$1.phar', $content);
if ($content) {
file_put_contents($filePath, $content);
}
}
function delDirTree($dir) {
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file") && !is_link($dir)) ? delDirTree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}