1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Bernardo Secades |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in all |
16
|
|
|
* copies or substantial portions of the Software. |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace BernardoSecades\Packagist\SecurityChecker\Compiler; |
27
|
|
|
|
28
|
|
|
use Phar; |
29
|
|
|
use RuntimeException; |
30
|
|
|
use SplFileInfo; |
31
|
|
|
use Symfony\Component\Finder\Finder; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @author bernardosecades <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class Compiler |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Compiles composer into a single phar file |
40
|
|
|
* |
41
|
|
|
* @throws RuntimeException |
42
|
|
|
*/ |
43
|
|
|
public function compile() |
44
|
|
|
{ |
45
|
|
|
$pharFilePath = dirname(__FILE__).'/../../build/packagist-security-checker.phar'; |
46
|
|
|
|
47
|
|
|
if (file_exists($pharFilePath)) { |
48
|
|
|
unlink($pharFilePath); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$phar = new Phar($pharFilePath, 0, 'packagist-security-checker.phar'); |
52
|
|
|
$phar->setSignatureAlgorithm(\Phar::SHA1); |
53
|
|
|
|
54
|
|
|
$phar->startBuffering(); |
55
|
|
|
|
56
|
|
|
$this |
57
|
|
|
->addPHPFiles($phar) |
58
|
|
|
->addVendorFiles($phar) |
59
|
|
|
->addComposerVendorFiles($phar) |
60
|
|
|
->addBin($phar) |
61
|
|
|
->addStub($phar) |
62
|
|
|
->addLicense($phar); |
63
|
|
|
|
64
|
|
|
$phar->stopBuffering(); |
65
|
|
|
|
66
|
|
|
unset($phar); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Add a file into the phar package |
71
|
|
|
* |
72
|
|
|
* @param Phar $phar Phar object |
73
|
|
|
* @param SplFileInfo $file File to add |
74
|
|
|
* @param bool $strip strip |
75
|
|
|
* |
76
|
|
|
* @return Compiler self Object |
77
|
|
|
*/ |
78
|
|
|
protected function addFile( |
79
|
|
|
Phar $phar, |
80
|
|
|
SplFileInfo $file, |
81
|
|
|
$strip = true |
82
|
|
|
) { |
83
|
|
|
$path = strtr(str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath()), '\\', '/'); |
84
|
|
|
$content = file_get_contents($file); |
85
|
|
|
if ($strip) { |
86
|
|
|
$content = php_strip_whitespace($path); |
87
|
|
|
} elseif ('LICENSE' === basename($file)) { |
88
|
|
|
$content = "\n".$content."\n"; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$phar->addFromString($path, $content); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Add bin into Phar |
98
|
|
|
* |
99
|
|
|
* @param Phar $phar Phar |
100
|
|
|
* |
101
|
|
|
* @return Compiler self Object |
102
|
|
|
*/ |
103
|
|
|
protected function addBin(Phar $phar) |
104
|
|
|
{ |
105
|
|
|
$content = file_get_contents(__DIR__.'/../../bin/packagist-security-checker'); |
106
|
|
|
$content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content); |
107
|
|
|
$phar->addFromString('bin/packagist-security-checker', $content); |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function addStub(Phar $phar) |
113
|
|
|
{ |
114
|
|
|
$stub = <<<'EOF' |
115
|
|
|
#!/usr/bin/env php |
116
|
|
|
<?php |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* MIT License |
120
|
|
|
* |
121
|
|
|
* Copyright (c) 2016 Bernardo Secades |
122
|
|
|
* |
123
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
124
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
125
|
|
|
* in the Software without restriction, including without limitation the rights |
126
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
127
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
128
|
|
|
* furnished to do so, subject to the following conditions: |
129
|
|
|
* |
130
|
|
|
* The above copyright notice and this permission notice shall be included in all |
131
|
|
|
* copies or substantial portions of the Software. |
132
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
133
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
134
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
135
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
136
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
137
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
138
|
|
|
* SOFTWARE. |
139
|
|
|
*/ |
140
|
|
|
|
141
|
|
|
Phar::mapPhar('packagist-security-checker.phar'); |
142
|
|
|
|
143
|
|
|
require 'phar://packagist-security-checker.phar/bin/packagist-security-checker'; |
144
|
|
|
|
145
|
|
|
__HALT_COMPILER(); |
146
|
|
|
EOF; |
147
|
|
|
$phar->setStub($stub); |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Add php files |
154
|
|
|
* |
155
|
|
|
* @param Phar $phar Phar instance |
156
|
|
|
* |
157
|
|
|
* @return Compiler self Object |
158
|
|
|
*/ |
159
|
|
|
private function addPHPFiles(Phar $phar) |
160
|
|
|
{ |
161
|
|
|
$finder = new Finder(); |
162
|
|
|
$finder |
163
|
|
|
->files() |
164
|
|
|
->ignoreVCS(true) |
165
|
|
|
->name('*.php') |
166
|
|
|
->notName('Compiler.php') |
167
|
|
|
->in(realpath(__DIR__.'/../../src')); |
168
|
|
|
|
169
|
|
|
foreach ($finder as $file) { |
170
|
|
|
$this->addFile($phar, $file); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Add vendor files |
178
|
|
|
* |
179
|
|
|
* @param Phar $phar Phar instance |
180
|
|
|
* |
181
|
|
|
* @return Compiler self Object |
182
|
|
|
*/ |
183
|
|
|
private function addVendorFiles(Phar $phar) |
184
|
|
|
{ |
185
|
|
|
$vendorPath = __DIR__.'/../../vendor/'; |
186
|
|
|
|
187
|
|
|
$requiredDependencies = [ |
188
|
|
|
realpath($vendorPath.'symfony/'), |
189
|
|
|
realpath($vendorPath.'doctrine/'), |
190
|
|
|
realpath($vendorPath.'guzzlehttp'), |
191
|
|
|
realpath($vendorPath.'psr'), |
192
|
|
|
]; |
193
|
|
|
|
194
|
|
|
$finder = new Finder(); |
195
|
|
|
$finder |
196
|
|
|
->files() |
197
|
|
|
->ignoreVCS(true) |
198
|
|
|
->name('*.php') |
199
|
|
|
->exclude(['Tests', 'tests']) |
200
|
|
|
->in($requiredDependencies); |
201
|
|
|
|
202
|
|
|
foreach ($finder as $file) { |
203
|
|
|
$this->addFile($phar, $file); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Add composer vendor files |
211
|
|
|
* |
212
|
|
|
* @param Phar $phar Phar |
213
|
|
|
* |
214
|
|
|
* @return Compiler self Object |
215
|
|
|
*/ |
216
|
|
|
private function addComposerVendorFiles(Phar $phar) |
217
|
|
|
{ |
218
|
|
|
$vendorPath = __DIR__.'/../../vendor/'; |
219
|
|
|
|
220
|
|
|
$this |
221
|
|
|
->addFile($phar, new \SplFileInfo($vendorPath.'autoload.php')) |
222
|
|
|
->addFile($phar, new \SplFileInfo($vendorPath.'composer/autoload_namespaces.php')) |
223
|
|
|
->addFile($phar, new \SplFileInfo($vendorPath.'composer/autoload_psr4.php')) |
224
|
|
|
->addFile($phar, new \SplFileInfo($vendorPath.'composer/autoload_classmap.php')) |
225
|
|
|
->addFile($phar, new \SplFileInfo($vendorPath.'composer/autoload_real.php')) |
226
|
|
|
->addFile($phar, new \SplFileInfo($vendorPath.'composer/autoload_static.php')) |
227
|
|
|
->addFile($phar, new \SplFileInfo($vendorPath.'composer/ClassLoader.php')); |
228
|
|
|
|
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Add license |
234
|
|
|
* |
235
|
|
|
* @param Phar $phar Phar |
236
|
|
|
* |
237
|
|
|
* @return Compiler self Object |
238
|
|
|
*/ |
239
|
|
|
private function addLicense(Phar $phar) |
240
|
|
|
{ |
241
|
|
|
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../LICENSE'), false); |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|