1 | <?php |
||
20 | class Tar extends Abstraction implements Executable |
||
21 | { |
||
22 | /** |
||
23 | * Path to compress |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | private $path; |
||
28 | |||
29 | /** |
||
30 | * Compression to use |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private $compression; |
||
35 | |||
36 | /** |
||
37 | * Compress program to use. |
||
38 | * --use-compress-program |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $compressProgram; |
||
43 | |||
44 | /** |
||
45 | * Path to dump file |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | private $tarPathname; |
||
50 | |||
51 | /** |
||
52 | * List of excluded path. |
||
53 | * --exclude='foo' |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | private $excludes = []; |
||
58 | |||
59 | /** |
||
60 | * Force local file resolution |
||
61 | * --force-local |
||
62 | * |
||
63 | * @var bool |
||
64 | */ |
||
65 | private $local = false; |
||
66 | |||
67 | /** |
||
68 | * Ignore failed reads |
||
69 | * --ignore-failed-read |
||
70 | * |
||
71 | * @var bool |
||
72 | */ |
||
73 | private $ignoreFailedRead; |
||
74 | |||
75 | /** |
||
76 | * Should the source directory be removed. |
||
77 | * |
||
78 | * @var boolean |
||
79 | */ |
||
80 | private $removeSourceDir = false; |
||
81 | |||
82 | /** |
||
83 | * List of available compressors |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | private static $availableCompressions = [ |
||
88 | 'bzip2' => 'j', |
||
89 | 'gzip' => 'z', |
||
90 | 'xz' => 'J' |
||
91 | ]; |
||
92 | |||
93 | /** |
||
94 | * Constructor. |
||
95 | * |
||
96 | * @param string $path |
||
97 | */ |
||
98 | 28 | public function __construct(string $path = '') |
|
102 | |||
103 | /** |
||
104 | * Return 'tar' compressor option e.g. 'j' for bzip2. |
||
105 | * |
||
106 | * @param string $compressor |
||
107 | * @return string |
||
108 | */ |
||
109 | 8 | protected function getCompressionOption(string $compressor) : string |
|
113 | |||
114 | /** |
||
115 | * Compress tar. |
||
116 | * |
||
117 | * @param string $compression |
||
118 | * @return \phpbu\App\Cli\Executable\Tar |
||
119 | */ |
||
120 | 18 | public function useCompression(string $compression) : Tar |
|
127 | |||
128 | /** |
||
129 | * Set compress program. |
||
130 | * |
||
131 | * @param string $program |
||
132 | * @return \phpbu\App\Cli\Executable\Tar |
||
133 | */ |
||
134 | 13 | public function useCompressProgram(string $program) : Tar |
|
139 | |||
140 | /** |
||
141 | * Add an path to exclude. |
||
142 | * |
||
143 | * @param string $path |
||
144 | * @return \phpbu\App\Cli\Executable\Tar |
||
145 | */ |
||
146 | 2 | public function addExclude(string $path) : Tar |
|
151 | |||
152 | /** |
||
153 | * Force local file resolution. |
||
154 | * |
||
155 | * @param bool $bool |
||
156 | * @return \phpbu\App\Cli\Executable\Tar |
||
157 | */ |
||
158 | 13 | public function forceLocal(bool $bool) |
|
163 | |||
164 | /** |
||
165 | * Ignore failed reads setter. |
||
166 | * |
||
167 | * @param bool $bool |
||
168 | * @return \phpbu\App\Cli\Executable\Tar |
||
169 | */ |
||
170 | 13 | public function ignoreFailedRead(bool $bool) : Tar |
|
175 | |||
176 | /** |
||
177 | * Does the tar handle the compression. |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | 3 | public function handlesCompression() : bool |
|
185 | |||
186 | /** |
||
187 | * Set folder to compress. |
||
188 | * |
||
189 | * @param string $path |
||
190 | * @return \phpbu\App\Cli\Executable\Tar |
||
191 | * @throws \phpbu\App\Exception |
||
192 | */ |
||
193 | 26 | public function archiveDirectory(string $path) : Tar |
|
199 | |||
200 | /** |
||
201 | * Set target filename. |
||
202 | * |
||
203 | * @param string $path |
||
204 | * @return \phpbu\App\Cli\Executable\Tar |
||
205 | */ |
||
206 | 24 | public function archiveTo(string $path) : Tar |
|
211 | |||
212 | /** |
||
213 | * Delete the source directory. |
||
214 | * |
||
215 | * @param boolean $bool |
||
216 | * @return \phpbu\App\Cli\Executable\Tar |
||
217 | */ |
||
218 | 16 | public function removeSourceDirectory(bool $bool) : Tar |
|
223 | |||
224 | /** |
||
225 | * Tar CommandLine generator. |
||
226 | * |
||
227 | * @return \SebastianFeldmann\Cli\CommandLine |
||
228 | */ |
||
229 | 26 | protected function createCommandLine() : CommandLine |
|
253 | |||
254 | /** |
||
255 | * Adds necessary exclude options to tat command. |
||
256 | * |
||
257 | * @param \SebastianFeldmann\Cli\Command\Executable $tar |
||
258 | */ |
||
259 | 24 | protected function setExcludeOptions(Cmd $tar) |
|
265 | |||
266 | /** |
||
267 | * Add a remove command if requested. |
||
268 | * |
||
269 | * @param \SebastianFeldmann\Cli\CommandLine $process |
||
270 | */ |
||
271 | 24 | protected function addRemoveCommand(CommandLine $process) |
|
277 | |||
278 | /** |
||
279 | * Return 'rm' command. |
||
280 | * |
||
281 | * @return \SebastianFeldmann\Cli\Command\Executable |
||
282 | */ |
||
283 | 5 | protected function getRmCommand() : Cmd |
|
289 | |||
290 | /** |
||
291 | * Check directory to compress. |
||
292 | * |
||
293 | * @param string $path |
||
294 | * @throws \phpbu\App\Exception |
||
295 | */ |
||
296 | 26 | private function validateDirectory(string $path) |
|
302 | |||
303 | /** |
||
304 | * Check if source and target values are set. |
||
305 | * |
||
306 | * @throws \phpbu\App\Exception |
||
307 | */ |
||
308 | 26 | private function validateSetup() |
|
317 | |||
318 | /** |
||
319 | * Return true if a given compression is valid false otherwise. |
||
320 | * |
||
321 | * @param string $compression |
||
322 | * @return bool |
||
323 | */ |
||
324 | 20 | public static function isCompressionValid(string $compression) : bool |
|
328 | } |
||
329 |