-
Notifications
You must be signed in to change notification settings - Fork 20
/
BasicDriver.php
272 lines (237 loc) · 6.8 KB
/
BasicDriver.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
namespace wapmorgan\UnifiedArchive\Drivers;
use wapmorgan\UnifiedArchive\ArchiveEntry;
use wapmorgan\UnifiedArchive\ArchiveInformation;
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException;
use wapmorgan\UnifiedArchive\Exceptions\ArchiveModificationException;
use wapmorgan\UnifiedArchive\Exceptions\NonExistentArchiveFileException;
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException;
use wapmorgan\UnifiedArchive\Formats;
abstract class BasicDriver
{
const TYPE_EXTENSION = 1;
const TYPE_UTILITIES = 2;
const TYPE_PURE_PHP = 3;
static $typeLabels = [
BasicDriver::TYPE_EXTENSION => 'php extension',
BasicDriver::TYPE_UTILITIES => 'utilities + php bridge',
BasicDriver::TYPE_PURE_PHP => 'pure php',
];
const COMPRESSION_NONE = 0;
const COMPRESSION_WEAK = 1;
const COMPRESSION_AVERAGE = 2;
const COMPRESSION_STRONG = 3;
const COMPRESSION_MAXIMUM = 4;
const OPEN = 1;
const OPEN_ENCRYPTED = 2;
const OPEN_VOLUMED = 4;
const GET_COMMENT = 64;
const EXTRACT_CONTENT = 128;
const STREAM_CONTENT = 256;
const APPEND = 4096;
const DELETE = 8192;
const SET_COMMENT = 16384;
const CREATE = 1048576;
const CREATE_ENCRYPTED = 2097152;
const CREATE_IN_STRING = 4194304;
const TYPE = null;
/**
* @var string
*/
protected $fileName;
/**
* @var string
*/
protected $format;
/**
* @return string
*/
abstract public static function getDescription();
/**
* @return bool
*/
abstract public static function isInstalled();
/**
* @return string
*/
abstract public static function getInstallationInstruction();
/**
* @return string[]
*/
abstract public static function getSupportedFormats();
/**
* @param string $format
* @return int[]
*/
abstract public static function checkFormatSupport($format);
/**
* @param $ability
* @return bool
*/
public function checkAbility($ability)
{
return in_array($ability, static::checkFormatSupport($this->format), true);
}
/**
* @param array $files
* @param string $archiveFileName
* @param string $archiveFormat
* @param int $compressionLevel
* @param null $password
* @param $fileProgressCallable
* @return int Number of archived files
* @throws UnsupportedOperationException
*/
public static function createArchive(
array $files,
$archiveFileName,
$archiveFormat,
$compressionLevel = self::COMPRESSION_AVERAGE,
$password = null,
$fileProgressCallable = null
) {
throw new UnsupportedOperationException();
}
/**
* @param array $files
* @param string $archiveFormat
* @param int $compressionLevel
* @param null $password
* @param $fileProgressCallable
* @return int Number of archived files
* @throws UnsupportedOperationException
*/
public static function createArchiveInString(
array $files,
$archiveFormat,
$compressionLevel = self::COMPRESSION_AVERAGE,
$password = null,
$fileProgressCallable = null
) {
$format_extension = Formats::getFormatExtension($archiveFormat);
do {
$temp_file = tempnam(sys_get_temp_dir(), 'temp_archive') . '.' . $format_extension;
} while (file_exists($temp_file));
$created = static::createArchive($files, $temp_file, $archiveFormat, $compressionLevel, $password, $fileProgressCallable);
$string = file_get_contents($temp_file);
unlink($temp_file);
return $string;
}
/**
* BasicDriver constructor.
* @param string $format
* @param string $archiveFileName
* @param string|null $password Archive password for opening
*/
public function __construct($archiveFileName, $format, $password = null)
{
$this->fileName = $archiveFileName;
$this->format = $format;
}
/**
* Returns summary about an archive.
* Called after
* - constructing
* - addFiles()
* - deleteFiles()
* @return ArchiveInformation
*/
abstract public function getArchiveInformation();
/**
* @return array
*/
abstract public function getFileNames();
/**
* @param string $fileName
* @return bool
*/
abstract public function isFileExists($fileName);
/**
* @param string $fileName
* @return ArchiveEntry|false
*/
abstract public function getFileData($fileName);
/**
* @param string $fileName
* @return string|false
* @throws NonExistentArchiveFileException
*/
abstract public function getFileContent($fileName);
/**
* @param string $fileName
* @return resource
*/
abstract public function getFileStream($fileName);
/**
* @param $string
* @return resource
*/
public static function wrapStringInStream($string)
{
$resource = fopen('php://temp', 'r+');
fwrite($resource, $string);
rewind($resource);
return $resource;
}
/**
* @param string $outputFolder
* @param array $files
* @return int Number of extracted files
* @throws ArchiveExtractionException
*/
abstract public function extractFiles($outputFolder, array $files);
/**
* @param string $outputFolder
* @return int Number of extracted files
* @throws ArchiveExtractionException
*/
abstract public function extractArchive($outputFolder);
/**
* @param array $files
* @return false|int Number of deleted files
* @throws UnsupportedOperationException
* @throws ArchiveModificationException
*/
public function deleteFiles(array $files)
{
throw new UnsupportedOperationException();
}
/**
* @param array $files
* @return int Number of added files
* @throws UnsupportedOperationException
* @throws ArchiveModificationException
*/
public function addFiles(array $files)
{
throw new UnsupportedOperationException();
}
/**
* @param string $inArchiveName
* @param string $content
* @return bool
* @throws UnsupportedOperationException
* @throws ArchiveModificationException
*/
public function addFileFromString($inArchiveName, $content)
{
throw new UnsupportedOperationException();
}
/**
* @return string|null
* @throws UnsupportedOperationException
*/
public function getComment()
{
throw new UnsupportedOperationException();
}
/**
* @param string|null $comment
* @return null
* @throws UnsupportedOperationException
*/
public function setComment($comment)
{
throw new UnsupportedOperationException();
}
}