forked from axute/paerius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paerius.php
178 lines (148 loc) · 5.81 KB
/
Paerius.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Paerius;
use FilesystemIterator;
use Phar;
use RecursiveCallbackFilterIterator;
use RecursiveDirectoryIterator;
use RecursiveIterator;
use RecursiveIteratorIterator;
use RuntimeException;
use SplFileInfo;
class Paerius {
public const DEFAULT_BASENAME = 'vendor.phar';
/** @var string */
protected $baseName;
public static $toDeleteBasenames = ['LICENSE', '.styleci.yml', '.travis.yml', '.gitattributes', 'phpunit.xml.dist', 'README.rst', 'CHANGELOG', 'CHANGES', 'README', 'VERSION', '.php_cs.dist', '.php_cs', '.editorconfig', 'php_cs.xml', 'phpunit.xml', 'phpcs.xml', '.codecov.yml', 'AUTHORS', 'Makefile', '.gitignore'];
public static $toDeleteDirectoryNames = ['.git', 'tests', 'Tests', 'test', 'doc', 'testing', 'test_old', '.svn', '.cvs', '.idea', '.DS_Store', '.hg'];
public static $toDeleteExtensions = ['md', 'gitignore', 'markdown', 'hprof', 'pyc'];
/** @var string */
protected $vendorPath;
/** @var string */
protected $workingPath;
/**
* Aerius constructor.
*
* @param string $workingPath
* @param null|string $baseName
* @throws \RuntimeException
*/
public function __construct(string $workingPath, ?string $baseName = self::DEFAULT_BASENAME) {
$workingPath = rtrim($workingPath, DIRECTORY_SEPARATOR);
$this->setWorkingPath($workingPath);
$this->setVendorPath($workingPath . DIRECTORY_SEPARATOR . 'vendor');
$this->setBaseName($baseName);
}
/**
* @param string|null $forceCompression gz*|bz*|none, null = automatic detection bz < gz < none
* @return bool
* @throws \UnexpectedValueException
* @throws \BadMethodCallException
*/
public function build(?string $forceCompression): bool {
$pharFilePath = $this->getWorkingPath() . DIRECTORY_SEPARATOR . $this->getBaseName();
if (is_file($pharFilePath)) {
unlink($pharFilePath);
}
$this->cleanupVendorDir();
$phar = new Phar($pharFilePath, FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, $this->baseName);
$phar->setMetadata(
[
'datetime' => date('c'),
]
);
$phar->setSignatureAlgorithm(Phar::SHA1);
$phar->buildFromDirectory($this->getVendorPath());
$supportedCompression = Phar::getSupportedCompression();
if (($forceCompression !== null && stripos($forceCompression, 'bz') === 0) || ($forceCompression === null && \in_array('BZIP2', $supportedCompression, true))) {
$phar->compressFiles(Phar::BZ2);
}
else if (($forceCompression !== null && stripos($forceCompression, 'gz') === 0) || ($forceCompression === null && \in_array('GZ', $supportedCompression, true))) {
$phar->compressFiles(Phar::GZ);
}
return $phar->setStub("<?php\n\\Phar::mapPhar();\nreturn require 'phar://{$this->baseName}/autoload.php';\n__HALT_COMPILER();");
}
/**
* remove all files from directory
*
* @return $this
*/
protected function cleanupVendorDir(): self {
$file_del = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(new RecursiveDirectoryIterator($this->vendorPath), [$this, 'filter'])
);
foreach ($file_del as $toDlete) {
/** @var \SplFileInfo $toDlete */
self::remove($toDlete->getPath() . DIRECTORY_SEPARATOR . $toDlete->getFilename());
}
return $this;
}
public function filter( SplFileInfo $current, $key, RecursiveIterator $iterator ): bool {
// Allow recursion
if ($iterator->hasChildren()) {
return true;
}
$entry = $current->getFilename();
if (\in_array($entry, ['.', '..'], true) === false) {
$path = $current->getPath();
$ext = strtolower(pathinfo($entry, PATHINFO_EXTENSION));
$dirnames = explode(DIRECTORY_SEPARATOR, trim(substr($path, \strlen($this->getVendorPath())), DIRECTORY_SEPARATOR));
if (\count(array_intersect($dirnames, self::$toDeleteDirectoryNames)) > 0) {
return true;
}
return \in_array($ext, self::$toDeleteExtensions, true) || \in_array($entry, self::$toDeleteBasenames, true);
}
return false;
}
public function getBaseName(): string {
return $this->baseName;
}
public function getVendorPath(): string {
return $this->vendorPath;
}
public function getWorkingPath(): string {
return $this->workingPath;
}
protected static function remove(string $src): void {
if (is_dir($src)) {
$dir = opendir($src);
while (false !== ($file = readdir($dir))) {
if (($file !== '.') && ($file !== '..')) {
self::remove($src . '/' . $file);
}
}
closedir($dir);
rmdir($src);
}
else if (is_file($src)) {
unlink($src);
}
}
protected function setBaseName(string $baseName): self {
$this->baseName = $baseName;
return $this;
}
/**
* @param string $vendorPath
* @return Paerius
* @throws \RuntimeException
*/
protected function setVendorPath(string $vendorPath): self {
if (!is_dir($vendorPath)) {
throw new RuntimeException("$vendorPath is not a directory!");
}
$this->vendorPath = $vendorPath;
return $this;
}
/**
* @param string $workingPath
* @return Paerius
* @throws \RuntimeException
*/
protected function setWorkingPath(string $workingPath): self {
if (!is_dir($workingPath)) {
throw new RuntimeException("{$workingPath} is not a directory");
}
$this->workingPath = $workingPath;
return $this;
}
}