1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Compressor; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Target; |
5
|
|
|
use phpbu\App\Cli\Executable\Tar; |
6
|
|
|
use phpbu\App\Result; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Directory |
10
|
|
|
* |
11
|
|
|
* @package phpbu |
12
|
|
|
* @subpackage Backup |
13
|
|
|
* @author Sebastian Feldmann <[email protected]> |
14
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
15
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
16
|
|
|
* @link http://phpbu.de/ |
17
|
|
|
* @since Class available since Release 2.0.1 |
18
|
|
|
*/ |
19
|
|
|
class Directory extends Abstraction |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Validate path. |
23
|
|
|
* |
24
|
|
|
* @param string $path |
25
|
|
|
* @return boolean |
26
|
|
|
*/ |
27
|
|
|
public function isPathValid($path) |
28
|
5 |
|
{ |
29
|
|
|
return is_dir($path); |
30
|
5 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* If 'tar' can't compress with the requested compressor this will return false. |
34
|
|
|
* |
35
|
|
|
* @param \phpbu\App\Backup\Target $target |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
public function canCompress(Target $target) |
39
|
|
|
{ |
40
|
3 |
|
return Tar::isCompressorValid($target->getCompressor()->getCommand()); |
41
|
|
|
} |
42
|
3 |
|
|
43
|
3 |
|
/** |
44
|
1 |
|
* Compress the configured directory. |
45
|
|
|
* |
46
|
|
|
* @param \phpbu\App\Backup\Target $target |
47
|
|
|
* @param \phpbu\App\Result $result |
48
|
|
|
* @return string |
49
|
|
|
* @throws \phpbu\App\Exception |
50
|
|
|
*/ |
51
|
|
|
public function compress(Target $target, Result $result) |
52
|
3 |
|
{ |
53
|
3 |
|
$target->setMimeType('application/x-tar'); |
54
|
1 |
|
return parent::compress($target, $result); |
55
|
1 |
|
} |
56
|
|
|
|
57
|
1 |
|
/** |
58
|
1 |
|
* Returns the executable for this action. |
59
|
1 |
|
* |
60
|
1 |
|
* @param \phpbu\App\Backup\Target $target |
61
|
1 |
|
* @return \phpbu\App\Cli\Executable |
62
|
1 |
|
*/ |
63
|
1 |
|
public function getExecutable(Target $target) { |
64
|
3 |
|
if (null === $this->executable) { |
65
|
|
|
$this->executable = new Tar($this->pathToCommand); |
66
|
|
|
$this->executable->archiveDirectory($this->path); |
67
|
|
|
$this->executable->archiveTo($this->getArchiveFile($target)) |
68
|
|
|
->useCompression($target->getCompressor()->getCommand()) |
69
|
|
|
->removeSourceDirectory(true); |
70
|
|
|
} |
71
|
|
|
return $this->executable; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get final archive name. |
76
|
|
|
* |
77
|
|
|
* @param \phpbu\App\Backup\Target $target |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getArchiveFile(Target $target) |
81
|
|
|
{ |
82
|
|
|
return $this->canCompress($target) ? $target->getPathname() : $target->getPathnamePlain(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|