1
|
|
|
<?php |
2
|
|
|
namespace wapmorgan\UnifiedArchive\Drivers\Basic; |
3
|
|
|
|
4
|
|
|
use wapmorgan\UnifiedArchive\ArchiveEntry; |
5
|
|
|
use wapmorgan\UnifiedArchive\ArchiveInformation; |
6
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException; |
7
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\ArchiveModificationException; |
8
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\NonExistentArchiveFileException; |
9
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException; |
10
|
|
|
use wapmorgan\UnifiedArchive\Formats; |
11
|
|
|
|
12
|
|
|
abstract class BasicDriver |
13
|
|
|
{ |
14
|
|
|
const TYPE_EXTENSION = 1; |
15
|
|
|
const TYPE_UTILITIES = 2; |
16
|
|
|
const TYPE_PURE_PHP = 3; |
17
|
|
|
|
18
|
|
|
static $typeLabels = [ |
19
|
|
|
BasicDriver::TYPE_EXTENSION => 'php extension', |
20
|
|
|
BasicDriver::TYPE_UTILITIES => 'utilities + php bridge', |
21
|
|
|
BasicDriver::TYPE_PURE_PHP => 'pure php', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
const COMPRESSION_NONE = 0; |
25
|
|
|
const COMPRESSION_WEAK = 1; |
26
|
|
|
const COMPRESSION_AVERAGE = 2; |
27
|
|
|
const COMPRESSION_STRONG = 3; |
28
|
|
|
const COMPRESSION_MAXIMUM = 4; |
29
|
|
|
|
30
|
|
|
const TYPE = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $fileName; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $format; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
abstract public static function getDescription(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
abstract public static function isInstalled(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
abstract public static function getInstallationInstruction(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string[] |
59
|
|
|
*/ |
60
|
|
|
abstract public static function getFormats(); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $format |
64
|
|
|
* @return int[] |
65
|
|
|
*/ |
66
|
|
|
abstract public static function getFormatAbilities($format); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $ability |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function checkAbility($ability) |
73
|
|
|
{ |
74
|
|
|
return in_array($ability, static::getFormatAbilities($this->format), true); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $files |
79
|
|
|
* @param string $archiveFileName |
80
|
|
|
* @param string $archiveFormat |
81
|
|
|
* @param int $compressionLevel |
82
|
|
|
* @param string|null $password |
83
|
|
|
* @param callable|null $fileProgressCallable |
84
|
|
|
* @return int Number of archived files |
85
|
|
|
* @throws UnsupportedOperationException |
86
|
|
|
*/ |
87
|
|
|
public static function createArchive( |
88
|
|
|
array $files, |
89
|
|
|
$archiveFileName, |
90
|
|
|
$archiveFormat, |
91
|
|
|
$compressionLevel = self::COMPRESSION_AVERAGE, |
92
|
|
|
$password = null, |
93
|
|
|
$fileProgressCallable = null |
94
|
|
|
) { |
95
|
|
|
throw new UnsupportedOperationException(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $files |
100
|
|
|
* @param string $archiveFormat |
101
|
|
|
* @param int $compressionLevel |
102
|
|
|
* @param null $password |
|
|
|
|
103
|
|
|
* @param $fileProgressCallable |
104
|
|
|
* @return string Content of archive |
105
|
|
|
* @throws UnsupportedOperationException |
106
|
|
|
*/ |
107
|
|
|
public static function createArchiveInString( |
108
|
|
|
array $files, |
109
|
|
|
$archiveFormat, |
110
|
|
|
$compressionLevel = self::COMPRESSION_AVERAGE, |
111
|
|
|
$password = null, |
112
|
|
|
$fileProgressCallable = null |
113
|
|
|
) { |
114
|
|
|
$format_extension = Formats::getFormatExtension($archiveFormat); |
115
|
|
|
do { |
116
|
|
|
$temp_file = tempnam(sys_get_temp_dir(), 'temp_archive'); |
117
|
|
|
unlink($temp_file); |
118
|
|
|
$archive_file = $temp_file . '.' . $format_extension; |
119
|
|
|
} while (file_exists($archive_file)); |
120
|
|
|
$created = static::createArchive($files, $archive_file, $archiveFormat, $compressionLevel, $password, $fileProgressCallable); |
|
|
|
|
121
|
|
|
$string = file_get_contents($archive_file); |
122
|
|
|
unlink($archive_file); |
123
|
|
|
return $string; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* BasicDriver constructor. |
128
|
|
|
* @param string $format |
129
|
|
|
* @param string $archiveFileName |
130
|
|
|
* @param string|null $password Archive password for opening |
131
|
|
|
*/ |
132
|
|
|
public function __construct($archiveFileName, $format, $password = null) |
133
|
|
|
{ |
134
|
|
|
$this->fileName = $archiveFileName; |
135
|
|
|
$this->format = $format; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns summary about an archive. |
140
|
|
|
* Called after |
141
|
|
|
* - constructing |
142
|
|
|
* - addFiles() |
143
|
|
|
* - deleteFiles() |
144
|
|
|
* @return ArchiveInformation |
145
|
|
|
*/ |
146
|
|
|
abstract public function getArchiveInformation(); |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
abstract public function getFileNames(); |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $fileName |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
abstract public function isFileExists($fileName); |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $fileName |
161
|
|
|
* @return ArchiveEntry|false |
162
|
|
|
*/ |
163
|
|
|
abstract public function getFileData($fileName); |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $fileName |
167
|
|
|
* @return string|false |
168
|
|
|
* @throws NonExistentArchiveFileException |
169
|
|
|
*/ |
170
|
|
|
abstract public function getFileContent($fileName); |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $fileName |
174
|
|
|
* @return resource |
175
|
|
|
*/ |
176
|
|
|
abstract public function getFileStream($fileName); |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param $string |
180
|
|
|
* @return resource |
181
|
|
|
*/ |
182
|
|
|
public static function wrapStringInStream($string) |
183
|
|
|
{ |
184
|
|
|
$resource = fopen('php://temp', 'r+'); |
185
|
|
|
fwrite($resource, $string); |
186
|
|
|
rewind($resource); |
187
|
|
|
return $resource; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $outputFolder |
192
|
|
|
* @param array $files |
193
|
|
|
* @return int Number of extracted files |
194
|
|
|
* @throws ArchiveExtractionException |
195
|
|
|
*/ |
196
|
|
|
abstract public function extractFiles($outputFolder, array $files); |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param string $outputFolder |
200
|
|
|
* @return int Number of extracted files |
201
|
|
|
* @throws ArchiveExtractionException |
202
|
|
|
*/ |
203
|
|
|
abstract public function extractArchive($outputFolder); |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param array $files |
207
|
|
|
* @return false|int Number of deleted files |
208
|
|
|
* @throws UnsupportedOperationException |
209
|
|
|
* @throws ArchiveModificationException |
210
|
|
|
*/ |
211
|
|
|
public function deleteFiles(array $files) |
212
|
|
|
{ |
213
|
|
|
throw new UnsupportedOperationException(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param array $files |
218
|
|
|
* @return int Number of added files |
219
|
|
|
* @throws UnsupportedOperationException |
220
|
|
|
* @throws ArchiveModificationException |
221
|
|
|
*/ |
222
|
|
|
public function addFiles(array $files) |
223
|
|
|
{ |
224
|
|
|
throw new UnsupportedOperationException(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param string $inArchiveName |
229
|
|
|
* @param string $content |
230
|
|
|
* @return bool |
231
|
|
|
* @throws UnsupportedOperationException |
232
|
|
|
* @throws ArchiveModificationException |
233
|
|
|
*/ |
234
|
|
|
public function addFileFromString($inArchiveName, $content) |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
throw new UnsupportedOperationException(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return string|null |
241
|
|
|
*/ |
242
|
|
|
public function getComment() |
243
|
|
|
{ |
244
|
|
|
return null; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param string|null $comment |
249
|
|
|
* @return null |
250
|
|
|
* @throws UnsupportedOperationException |
251
|
|
|
*/ |
252
|
|
|
public function setComment($comment) |
|
|
|
|
253
|
|
|
{ |
254
|
|
|
throw new UnsupportedOperationException(); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|