|
1
|
|
|
<?php |
|
2
|
|
|
namespace Filesystem; |
|
3
|
|
|
|
|
4
|
|
|
use Filesystem\Builder; |
|
5
|
|
|
use Filesystem\Subprocess; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Archive class. |
|
9
|
|
|
* |
|
10
|
|
|
* @package Filesystem |
|
11
|
|
|
*/ |
|
12
|
|
|
class Archive |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Archive filename. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $archive; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Files to archive. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private $files = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Archive command. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
public $command = ''; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Strict error checking. |
|
38
|
|
|
* |
|
39
|
|
|
* @var boolean |
|
40
|
|
|
*/ |
|
41
|
|
|
public $strict = false; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $archive archive filename |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct($archive) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->archive = $archive; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Convert object of class to string. |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __toString() |
|
60
|
|
|
{ |
|
61
|
|
|
return (string) $this->command; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Add files to archive file. |
|
66
|
|
|
* |
|
67
|
|
|
* @param mixed $files files to archive |
|
68
|
|
|
* @return object |
|
69
|
|
|
*/ |
|
70
|
|
|
final public function add_files($files) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->files = is_array($files) ? $files : [$files]; |
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns files to be added to archive file. |
|
78
|
|
|
* |
|
79
|
|
|
* @return mixed |
|
80
|
|
|
*/ |
|
81
|
|
|
final public function get_files() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->files; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns archive file info (zip). |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $options supported zip command options. |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
final public function zipinfo(array $options = []) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->command = (new Builder()) |
|
95
|
|
|
->setPrefix('zipinfo') |
|
96
|
|
|
->setOptions($options) |
|
97
|
|
|
->setArguments([$this->archive]); |
|
98
|
|
|
|
|
99
|
|
|
$process = (new Subprocess())->run($this->command); |
|
100
|
|
|
$stdout = $process['stdout']; |
|
101
|
|
|
$stderr = $process['stderr']; |
|
102
|
|
|
$retval = $process['error_code'][0]; |
|
103
|
|
|
|
|
104
|
|
|
if ($retval != 0) |
|
105
|
|
|
{ |
|
106
|
|
|
throw new \RuntimeException(empty($stderr) ? $stdout : $stderr); |
|
107
|
|
|
} |
|
108
|
|
|
return $stdout; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Create archive file (zip). |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $options supported zip command options |
|
115
|
|
|
* @param bool $strict strict error checking |
|
116
|
|
|
* @return object |
|
117
|
|
|
*/ |
|
118
|
|
|
final public function zip(array $options = [], $strict = false) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->command = (new Builder()) |
|
121
|
|
|
->setPrefix('zip') |
|
122
|
|
|
->setOptions($options) |
|
123
|
|
|
->setArguments([$this->archive, $this->get_files()]); |
|
124
|
|
|
$this->strict = $strict; |
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Extract archive file (zip). |
|
130
|
|
|
* |
|
131
|
|
|
* @param array $options supported zip command options |
|
132
|
|
|
* @param array $arguments supported zip command arguments |
|
133
|
|
|
* @param bool $strict strict error checking |
|
134
|
|
|
* @return object |
|
135
|
|
|
*/ |
|
136
|
|
|
final public function unzip(array $options = [], array $arguments = [], $strict = true) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->command = (new Builder()) |
|
139
|
|
|
->setPrefix('unzip') |
|
140
|
|
|
->setOptions($options) |
|
141
|
|
|
->setArguments(array_merge([$this->archive], $arguments)); |
|
142
|
|
|
$this->strict = $strict; |
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Create archive file (tar). |
|
148
|
|
|
* |
|
149
|
|
|
* @param array $options supported tar command options |
|
150
|
|
|
* @param array $arguments supported tar command arguments |
|
151
|
|
|
* @param bool $strict strict error checking |
|
152
|
|
|
* @return object |
|
153
|
|
|
*/ |
|
154
|
|
|
final public function tar(array $options = [], array $arguments = [], $strict = true) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->command = (new Builder()) |
|
157
|
|
|
->setPrefix('tar') |
|
158
|
|
|
->setOptions($options) |
|
159
|
|
|
->setArguments(array_merge([$this->archive], $arguments, $this->get_files())); |
|
160
|
|
|
$this->strict = $strict; |
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Compresses archive file (xz). |
|
166
|
|
|
* |
|
167
|
|
|
* @param array $options supported tar command options. |
|
168
|
|
|
* @param bool $strict strict error checking |
|
169
|
|
|
* @return object |
|
170
|
|
|
*/ |
|
171
|
|
|
final public function xz(array $options = [], $strict = true) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->command = (new Builder()) |
|
174
|
|
|
->setPrefix('xz') |
|
175
|
|
|
->setOptions($options) |
|
176
|
|
|
->setArguments($this->get_files()); |
|
177
|
|
|
$this->strict = $strict; |
|
178
|
|
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Executes archive command.. |
|
183
|
|
|
* |
|
184
|
|
|
* @return mixed exit status code or false (no files) |
|
185
|
|
|
*/ |
|
186
|
|
|
final public function run() |
|
187
|
|
|
{ |
|
188
|
|
|
$process = (new Subprocess())->run($this->command); |
|
189
|
|
|
$stdout = $process['stdout']; |
|
190
|
|
|
$stderr = $process['stderr']; |
|
191
|
|
|
$retval = $process['error_code'][0]; |
|
192
|
|
|
|
|
193
|
|
|
if (($this->strict === false || $this->strict) && $retval == 0) |
|
194
|
|
|
{ |
|
195
|
|
|
return true; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
if ($this->strict === false && $retval != 0) |
|
199
|
|
|
{ |
|
200
|
|
|
return $retval; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
if ($this->strict && $retval != 0) |
|
204
|
|
|
{ |
|
205
|
|
|
throw new \RuntimeException(empty($stderr) ? $stdout : $stderr); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|