1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.8.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Archive; |
16
|
|
|
|
17
|
|
|
use Phar; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ArchiveInterface |
21
|
|
|
* @package Quantum\Libraries\Archive |
22
|
|
|
*/ |
23
|
|
|
class PharArchive implements ArchiveInterface |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Phar|null |
28
|
|
|
*/ |
29
|
|
|
private static $instance = null; |
|
|
|
|
30
|
|
|
|
31
|
|
|
private $newPhar; |
32
|
|
|
private $archiveName; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Phar constructor |
36
|
|
|
*/ |
37
|
|
|
public function __construct(string $archiveName) |
38
|
|
|
{ |
39
|
|
|
// $this->newPhar = new Phar($archiveName); |
40
|
|
|
$this->archiveName = $archiveName; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// public function __destruct() |
44
|
|
|
// { |
45
|
|
|
// // $newPhar->unlinkArchive($this->filename); |
46
|
|
|
|
47
|
|
|
// $this->newPhar->unlinkArchive($this->archiveName); |
48
|
|
|
// } |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritDoc |
52
|
|
|
*/ |
53
|
|
|
public function removeArchive(): bool |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
|
|
// $newPhar = new Phar($this->archiveName); |
57
|
|
|
// $newPhar->decompress(); |
58
|
|
|
return Phar::unlinkArchive($this->archiveName); |
59
|
|
|
} catch (\Throwable $th) { |
60
|
|
|
dump($th); |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
public function decompress() |
69
|
|
|
{ |
70
|
|
|
try { |
71
|
|
|
$this->newPhar->decompress(); |
72
|
|
|
} catch (\Throwable $th) { |
73
|
|
|
dump($th); |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public function unset(string $fileName) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
try { |
84
|
|
|
// unset($this->newPhar[$fileName]); |
85
|
|
|
} catch (\Throwable $th) { |
|
|
|
|
86
|
|
|
dump($th); |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
|
|
public function addEmptyDir(string $newDirectory): bool |
94
|
|
|
{ |
95
|
|
|
try { |
96
|
|
|
$newPhar = new Phar($this->archiveName); |
97
|
|
|
$newPhar->addEmptyDir($newDirectory); |
98
|
|
|
// dd($this->newPhar[$newDirectory]->isDir()); |
99
|
|
|
return $newPhar[$newDirectory]->isDir(); |
100
|
|
|
} catch (\Throwable $th) { |
101
|
|
|
dump($th); |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritDoc |
108
|
|
|
*/ |
109
|
|
|
public function addFile(string $newFilePath, string $newFileName = ''): bool |
110
|
|
|
{ |
111
|
|
|
try { |
112
|
|
|
$newPhar = new Phar($this->archiveName); |
113
|
|
|
$newPhar->addFile($newFilePath, $newFileName); |
114
|
|
|
// $this->newPhar->__destruct(); |
115
|
|
|
return true; |
116
|
|
|
} catch (\Throwable $th) { |
117
|
|
|
// dump($th); |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritDoc |
124
|
|
|
*/ |
125
|
|
|
public function addFromString(string $newFileName, string $newFileContent): bool |
126
|
|
|
{ |
127
|
|
|
try { |
128
|
|
|
$newPhar = new Phar($this->archiveName); |
129
|
|
|
$newPhar->addFromString($newFileName, $newFileContent); |
130
|
|
|
// $this->newPhar->__destruct(); |
131
|
|
|
return true; |
132
|
|
|
} catch (\Throwable $th) { |
133
|
|
|
// dump($th); |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @inheritDoc |
140
|
|
|
*/ |
141
|
|
|
public function deleteUsingName(string $fileOrDirName): bool |
142
|
|
|
{ |
143
|
|
|
// if ($this->pharArchive->open($archiveName) === TRUE) { |
144
|
|
|
// $this->pharArchive->deleteName($fileOrDirName); |
145
|
|
|
// $this->newPhar->__destruct(); |
146
|
|
|
return true; |
147
|
|
|
// } else { |
148
|
|
|
// return false; |
149
|
|
|
// } |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @inheritDoc |
154
|
|
|
*/ |
155
|
|
|
public function extractTo(string $pathToExtract, $files = ''): bool |
156
|
|
|
{ |
157
|
|
|
|
158
|
|
|
try { |
159
|
|
|
$newPhar = new Phar($this->archiveName); |
160
|
|
|
$newPhar->extractTo($pathToExtract, $files); |
161
|
|
|
// $this->newPhar->__destruct(); |
162
|
|
|
return true; |
163
|
|
|
} catch (\Throwable $th) { |
164
|
|
|
// dump($th); |
165
|
|
|
|
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritDoc |
172
|
|
|
*/ |
173
|
|
|
public function count(): int |
174
|
|
|
{ |
175
|
|
|
$newPhar = new Phar($this->archiveName); |
176
|
|
|
$pharCount = $newPhar->count(); |
177
|
|
|
// $this->newPhar->__destruct(); |
178
|
|
|
return $pharCount; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @inheritDoc |
183
|
|
|
*/ |
184
|
|
|
public function renameUsingName(string $currentName, string $newName): bool |
185
|
|
|
{ |
186
|
|
|
// if ($this->pharArchive->open($archiveName) === TRUE) { |
187
|
|
|
// $this->pharArchive->renameName($currentName, $newName); |
188
|
|
|
// $this->newPhar->__destruct(); |
189
|
|
|
return true; |
190
|
|
|
// } else { |
191
|
|
|
// return false; |
192
|
|
|
// } |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @inheritDoc |
197
|
|
|
*/ |
198
|
|
|
public function addMultipleFiles(array $fileNames): bool |
199
|
|
|
{ |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
try { |
203
|
|
|
// dd($fileNames); |
204
|
|
|
$newPhar = new Phar($this->archiveName); |
205
|
|
|
foreach ($fileNames as $fileNmae => $filePath) { |
206
|
|
|
// dd($fileNmae , $filePath); |
207
|
|
|
// $this->pharArchive->addFile($filePath, $fileNmae); |
208
|
|
|
$newPhar->addFile($filePath, $fileNmae); |
209
|
|
|
} |
210
|
|
|
// $this->newPhar->__destruct(); |
211
|
|
|
return true; |
212
|
|
|
} catch (\Throwable $th) { |
213
|
|
|
// dump($th); |
214
|
|
|
return false; |
215
|
|
|
} |
216
|
|
|
// if ($this->pharArchive->open($archiveName) === TRUE) { |
217
|
|
|
// $this->newPhar->__destruct(); |
218
|
|
|
// return true; |
219
|
|
|
// } else { |
220
|
|
|
// return false; |
221
|
|
|
// } |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @inheritDoc |
226
|
|
|
*/ |
227
|
|
|
public function deleteMultipleFilesUsingName(array $fileNames): bool |
228
|
|
|
{ |
229
|
|
|
// if ($this->pharArchive->open($archiveName) === TRUE) { |
230
|
|
|
// foreach ($fileNames as $fileNmae => $filePath) { |
231
|
|
|
// $this->pharArchive->addFile($filePath, $fileNmae); |
232
|
|
|
// } |
233
|
|
|
// $this->newPhar->__destruct(); |
234
|
|
|
return true; |
235
|
|
|
// } else { |
236
|
|
|
// return false; |
237
|
|
|
// } |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|