1
|
|
|
<?php |
2
|
|
|
namespace wapmorgan\UnifiedArchive\Drivers; |
3
|
|
|
|
4
|
|
|
use Alchemy\Zippy\Archive\Member; |
|
|
|
|
5
|
|
|
use Alchemy\Zippy\Exception\NoAdapterOnPlatformException; |
|
|
|
|
6
|
|
|
use Alchemy\Zippy\Zippy; |
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use wapmorgan\UnifiedArchive\ArchiveEntry; |
9
|
|
|
use wapmorgan\UnifiedArchive\ArchiveInformation; |
10
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\ArchiveCreationException; |
11
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException; |
12
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\ArchiveModificationException; |
13
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException; |
14
|
|
|
use wapmorgan\UnifiedArchive\Formats; |
15
|
|
|
use wapmorgan\UnifiedArchive\Drivers\BasicDriver; |
16
|
|
|
|
17
|
|
|
class AlchemyZippy extends BasicDriver |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Zippy |
21
|
|
|
*/ |
22
|
|
|
protected static $zippy; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $fileName; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Alchemy\Zippy\Archive\ArchiveInterface |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
protected $archive; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $files; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $format; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Member[] |
46
|
|
|
*/ |
47
|
|
|
protected $members; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return mixed|void |
51
|
|
|
*/ |
52
|
1 |
|
public static function getSupportedFormats() |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
1 |
|
Formats::ZIP, |
56
|
|
|
Formats::TAR, |
57
|
|
|
Formats::TAR_GZIP, |
58
|
|
|
Formats::TAR_BZIP, |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
protected static function init() |
63
|
|
|
{ |
64
|
4 |
|
if (!class_exists('\Alchemy\Zippy\Zippy')) |
65
|
4 |
|
static::$zippy = false; |
|
|
|
|
66
|
|
|
else if (static::$zippy === null) |
67
|
|
|
static::$zippy = Zippy::load(); |
68
|
4 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $format |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
4 |
|
public static function checkFormatSupport($format) |
75
|
|
|
{ |
76
|
4 |
|
static::init(); |
77
|
|
|
|
78
|
4 |
|
if (static::$zippy === false) |
|
|
|
|
79
|
4 |
|
return false; |
80
|
|
|
|
81
|
|
|
switch ($format) { |
82
|
|
|
case Formats::TAR_BZIP: |
83
|
|
|
case Formats::TAR: |
84
|
|
|
case Formats::TAR_GZIP: |
85
|
|
|
case Formats::ZIP: |
86
|
|
|
return static::checkAdapterFor($format); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
|
|
public static function getDescription() |
94
|
|
|
{ |
95
|
|
|
return 'php-library and console programs'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritDoc |
100
|
|
|
*/ |
101
|
|
|
public static function getInstallationInstruction() |
102
|
|
|
{ |
103
|
|
|
self::init(); |
104
|
|
|
return static::$zippy === false |
|
|
|
|
105
|
|
|
? 'install library `alchemy/zippy` and console programs (tar, zip)' |
106
|
|
|
: null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $format |
111
|
|
|
* @param $adapter |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
protected static function checkAdapterFor($format, &$adapter = null) |
115
|
|
|
{ |
116
|
|
|
try { |
117
|
|
|
$adapter = static::$zippy->getAdapterFor($format); |
118
|
|
|
return true; |
119
|
|
|
} catch (NoAdapterOnPlatformException $e) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $format |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public static function canCreateArchive($format) |
129
|
|
|
{ |
130
|
|
|
return true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $format |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public static function canAddFiles($format) |
138
|
|
|
{ |
139
|
|
|
return true; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $format |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public static function canDeleteFiles($format) |
147
|
|
|
{ |
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param array $files |
153
|
|
|
* @param string $archiveFileName |
154
|
|
|
* @param int $compressionLevel |
155
|
|
|
* @param null $password |
|
|
|
|
156
|
|
|
* @return int |
157
|
|
|
* @throws ArchiveCreationException |
158
|
|
|
* @throws UnsupportedOperationException |
159
|
|
|
*/ |
160
|
|
|
public static function createArchive(array $files, $archiveFileName, $compressionLevel = self::COMPRESSION_AVERAGE, $password = null) |
161
|
|
|
{ |
162
|
|
|
if ($password !== null) { |
|
|
|
|
163
|
|
|
throw new UnsupportedOperationException('AlchemyZippy could not encrypt an archive'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
try { |
167
|
|
|
$archive = static::$zippy->create($archiveFileName, $files); |
|
|
|
|
168
|
|
|
} catch (Exception $e) { |
169
|
|
|
throw new ArchiveCreationException('Could not create archive: '.$e->getMessage(), $e->getCode(), $e); |
170
|
|
|
} |
171
|
|
|
return count($files); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @inheritDoc |
176
|
|
|
*/ |
177
|
|
|
public function __construct($archiveFileName, $format, $password = null) |
178
|
|
|
{ |
179
|
|
|
$this->fileName = $archiveFileName; |
180
|
|
|
$this->format = $format; |
181
|
|
|
$this->archive = static::$zippy->open($archiveFileName); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @inheritDoc |
186
|
|
|
*/ |
187
|
|
|
public function getArchiveInformation() |
188
|
|
|
{ |
189
|
|
|
$this->files = []; |
190
|
|
|
$information = new ArchiveInformation(); |
191
|
|
|
|
192
|
|
|
foreach ($this->archive->getMembers() as $member) { |
193
|
|
|
if ($member->isDir()) |
194
|
|
|
continue; |
195
|
|
|
|
196
|
|
|
$this->files[] = $information->files[] = str_replace('\\', '/', $member->getLocation()); |
197
|
|
|
$this->members[str_replace('\\', '/', $member->getLocation())] = $member; |
198
|
|
|
$information->compressedFilesSize += (int)$member->getSize(); |
199
|
|
|
$information->uncompressedFilesSize += (int)$member->getSize(); |
200
|
|
|
} |
201
|
|
|
return $information; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @inheritDoc |
206
|
|
|
*/ |
207
|
|
|
public function getFileNames() |
208
|
|
|
{ |
209
|
|
|
return $this->files; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @inheritDoc |
214
|
|
|
*/ |
215
|
|
|
public function isFileExists($fileName) |
216
|
|
|
{ |
217
|
|
|
return in_array($fileName, $this->files); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
protected function getMember($fileName) |
221
|
|
|
{ |
222
|
|
|
return $this->members[$fileName]; |
223
|
|
|
|
224
|
|
|
// foreach ($this->archive->getMembers() as $member) { |
225
|
|
|
// if ($member->isDir()) |
226
|
|
|
// continue; |
227
|
|
|
// if ($member->getLocation() === $fileName) |
228
|
|
|
// return $member; |
229
|
|
|
// } |
230
|
|
|
// return null; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @inheritDoc |
235
|
|
|
*/ |
236
|
|
|
public function getFileData($fileName) |
237
|
|
|
{ |
238
|
|
|
$member = $this->getMember($fileName); |
239
|
|
|
return new ArchiveEntry($member->getLocation(), $member->getSize(), $member->getSize(), strtotime($member->getLastModifiedDate()), true); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @inheritDoc |
244
|
|
|
*/ |
245
|
|
|
public function getFileContent($fileName) |
246
|
|
|
{ |
247
|
|
|
$member = $this->getMember($fileName); |
248
|
|
|
return (string)$member; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @inheritDoc |
253
|
|
|
*/ |
254
|
|
|
public function getFileStream($fileName) |
255
|
|
|
{ |
256
|
|
|
$member = $this->getMember($fileName); |
257
|
|
|
return self::wrapStringInStream((string)$member); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @inheritDoc |
262
|
|
|
*/ |
263
|
|
|
public function extractFiles($outputFolder, array $files) |
264
|
|
|
{ |
265
|
|
|
try { |
266
|
|
|
foreach ($files as $file) { |
267
|
|
|
$member = $this->getMember($file); |
268
|
|
|
$member->extract($outputFolder); |
269
|
|
|
} |
270
|
|
|
} catch (Exception $e) { |
271
|
|
|
throw new ArchiveExtractionException('Could not extract archive: '.$e->getMessage(), $e->getCode(), $e); |
272
|
|
|
} |
273
|
|
|
return count($files); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @inheritDoc |
278
|
|
|
*/ |
279
|
|
|
public function extractArchive($outputFolder) |
280
|
|
|
{ |
281
|
|
|
try { |
282
|
|
|
$this->archive->extract($outputFolder); |
283
|
|
|
} catch (Exception $e) { |
284
|
|
|
throw new ArchiveExtractionException('Could not extract archive: '.$e->getMessage(), $e->getCode(), $e); |
285
|
|
|
} |
286
|
|
|
return count($this->files); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @inheritDoc |
291
|
|
|
*/ |
292
|
|
|
public function deleteFiles(array $files) |
293
|
|
|
{ |
294
|
|
|
$members = []; |
295
|
|
|
try { |
296
|
|
|
foreach ($files as $file) { |
297
|
|
|
$members[] = $this->getMember($file); |
298
|
|
|
} |
299
|
|
|
$this->archive->removeMembers($members); |
300
|
|
|
} catch (Exception $e) { |
301
|
|
|
throw new ArchiveModificationException('Could not remove files from archive: '.$e->getMessage(), $e->getCode(), $e); |
302
|
|
|
} |
303
|
|
|
return count($files); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @inheritDoc |
308
|
|
|
*/ |
309
|
|
|
public function addFiles(array $files) |
310
|
|
|
{ |
311
|
|
|
$added = 0; |
312
|
|
|
try { |
313
|
|
|
foreach ($files as $localName => $filename) { |
314
|
|
|
if (is_null($filename)) { |
315
|
|
|
// $this->archive->addEmptyDir($localName); |
316
|
|
|
} else { |
317
|
|
|
$this->archive->addMembers([$filename => $localName]); |
318
|
|
|
$added++; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
} catch (Exception $e) { |
322
|
|
|
throw new ArchiveModificationException('Could not add file "'.$filename.'": '.$e->getMessage(), $e->getCode()); |
323
|
|
|
} |
324
|
|
|
$this->getArchiveInformation(); |
325
|
|
|
return $added; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @param string $inArchiveName |
330
|
|
|
* @param string $content |
331
|
|
|
* @return bool |
332
|
|
|
*/ |
333
|
|
|
public function addFileFromString($inArchiveName, $content) |
334
|
|
|
{ |
335
|
|
|
$fp = fopen('php://memory', 'w'); |
336
|
|
|
fwrite($fp, $content); |
337
|
|
|
rewind($fp); |
338
|
|
|
$this->archive->addMembers([$inArchiveName => $fp]); |
339
|
|
|
fclose($fp); |
340
|
|
|
return true; |
341
|
|
|
} |
342
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths