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 string |
28
|
|
|
*/ |
29
|
|
|
private $archiveName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Phar constructor |
33
|
|
|
*/ |
34
|
|
|
public function __construct(string $archiveName) |
35
|
|
|
{ |
36
|
|
|
$this->archiveName = $archiveName; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritDoc |
41
|
|
|
*/ |
42
|
|
|
public function removeArchive(): bool |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
return Phar::unlinkArchive($this->archiveName); |
46
|
|
|
} catch (\Throwable $th) { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritDoc |
53
|
|
|
*/ |
54
|
|
|
public function offsetExists(string $fileOrDirName): bool |
55
|
|
|
{ |
56
|
|
|
$newPhar = new Phar($this->archiveName); |
57
|
|
|
return $newPhar->offsetExists($fileOrDirName); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
|
|
public function addEmptyDir(string $newDirectory): bool |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
|
|
$newPhar = new Phar($this->archiveName); |
67
|
|
|
$newPhar->addEmptyDir($newDirectory); |
68
|
|
|
return true; |
69
|
|
|
} catch (\Throwable $th) { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
|
|
public function addFile(string $newFilePath, string $newFileName = ''): bool |
78
|
|
|
{ |
79
|
|
|
try { |
80
|
|
|
$newPhar = new Phar($this->archiveName); |
81
|
|
|
$newPhar->addFile($newFilePath, $newFileName); |
82
|
|
|
return true; |
83
|
|
|
} catch (\Throwable $th) { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritDoc |
90
|
|
|
*/ |
91
|
|
|
public function addFromString(string $newFileName, string $newFileContent): bool |
92
|
|
|
{ |
93
|
|
|
try { |
94
|
|
|
$newPhar = new Phar($this->archiveName); |
95
|
|
|
$newPhar->addFromString($newFileName, $newFileContent); |
96
|
|
|
return true; |
97
|
|
|
} catch (\Throwable $th) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritDoc |
104
|
|
|
*/ |
105
|
|
|
public function deleteUsingName(string $fileName): bool |
106
|
|
|
{ |
107
|
|
|
try { |
108
|
|
|
$newPhar = new Phar($this->archiveName); |
109
|
|
|
$newPhar->delete($fileName); |
110
|
|
|
return true; |
111
|
|
|
} catch (\Throwable $th) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritDoc |
118
|
|
|
*/ |
119
|
|
|
public function extractTo(string $pathToExtract, $files = ''): bool |
120
|
|
|
{ |
121
|
|
|
|
122
|
|
|
try { |
123
|
|
|
$newPhar = new Phar($this->archiveName); |
124
|
|
|
$newPhar->extractTo($pathToExtract, $files); |
125
|
|
|
return true; |
126
|
|
|
} catch (\Throwable $th) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritDoc |
133
|
|
|
*/ |
134
|
|
|
public function count(): int |
135
|
|
|
{ |
136
|
|
|
$newPhar = new Phar($this->archiveName); |
137
|
|
|
$pharCount = $newPhar->count(); |
138
|
|
|
return $pharCount; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritDoc |
143
|
|
|
*/ |
144
|
|
|
public function addMultipleFiles(array $fileNames): bool |
145
|
|
|
{ |
146
|
|
|
try { |
147
|
|
|
$newPhar = new Phar($this->archiveName); |
148
|
|
|
foreach ($fileNames as $fileName => $filePath) { |
149
|
|
|
$newPhar->addFile($filePath, $fileName); |
150
|
|
|
} |
151
|
|
|
return true; |
152
|
|
|
} catch (\Throwable $th) { |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @inheritDoc |
159
|
|
|
*/ |
160
|
|
|
public function deleteMultipleFilesUsingName(array $fileNames): bool |
161
|
|
|
{ |
162
|
|
|
try { |
163
|
|
|
$newPhar = new Phar($this->archiveName); |
164
|
|
|
foreach ($fileNames as $key => $fileName) { |
165
|
|
|
$newPhar->delete($fileName); |
166
|
|
|
} |
167
|
|
|
return true; |
168
|
|
|
} catch (\Throwable $th) { |
169
|
|
|
return false; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|