1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Target; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Exception; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Compression |
8
|
|
|
* |
9
|
|
|
* @package phpbu |
10
|
|
|
* @subpackage Backup |
11
|
|
|
* @author Sebastian Feldmann <[email protected]> |
12
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
13
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
14
|
|
|
* @link http://phpbu.de/ |
15
|
|
|
* @since Class available since Release 1.0.0 |
16
|
|
|
*/ |
17
|
|
|
class Compression |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Path to command binary |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $path; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Command name |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $cmd; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Suffix for compressed files |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $suffix; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* MIME type for compressed files |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $mimeType; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* List of available compressors |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected static $availableCompressors = [ |
53
|
|
|
'gzip' => [ |
54
|
|
|
'pipeable' => true, |
55
|
|
|
'suffix' => 'gz', |
56
|
|
|
'mime' => 'application/x-gzip' |
57
|
|
|
], |
58
|
|
|
'bzip2' => [ |
59
|
|
|
'pipeable' => true, |
60
|
|
|
'suffix' => 'bz2', |
61
|
|
|
'mime' => 'application/x-bzip2' |
62
|
|
|
], |
63
|
|
|
'zip' => [ |
64
|
|
|
'pipeable' => false, |
65
|
|
|
'suffix' => 'zip', |
66
|
|
|
'mime' => 'application/zip' |
67
|
|
|
] |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Constructor. |
72
|
|
|
* |
73
|
|
|
* @param string $cmd |
74
|
|
|
* @param string $pathToCmd without trailing slash |
75
|
|
|
*/ |
76
|
|
|
public function __construct($cmd, $pathToCmd = null) |
77
|
|
|
{ |
78
|
|
|
$this->path = $pathToCmd; |
79
|
|
|
$this->cmd = $cmd; |
80
|
|
|
$this->suffix = self::$availableCompressors[$cmd]['suffix']; |
81
|
|
|
$this->mimeType = self::$availableCompressors[$cmd]['mime']; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Return the cli command. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getCommand() |
90
|
|
|
{ |
91
|
|
|
return $this->cmd; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Path getter. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getPath() |
100
|
|
|
{ |
101
|
|
|
return $this->path; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns the compressor suffix e.g. 'bzip2' |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getSuffix() |
110
|
|
|
{ |
111
|
|
|
return $this->suffix; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Is the compression app pipeable. |
116
|
|
|
* |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
public function isPipeable() |
120
|
|
|
{ |
121
|
|
|
return self::$availableCompressors[$this->cmd]['pipeable']; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns the compressor mime type. |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function getMimeType() |
130
|
|
|
{ |
131
|
|
|
return $this->mimeType; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Factory method. |
136
|
|
|
* |
137
|
|
|
* @param string $name |
138
|
|
|
* @return \phpbu\App\Backup\Target\Compression |
139
|
|
|
* @throws \phpbu\App\Exception |
140
|
|
|
*/ |
141
|
|
|
public static function create($name) |
142
|
|
|
{ |
143
|
|
|
$path = null; |
144
|
|
|
// check if a path is given for the compressor |
145
|
|
|
if (basename($name) !== $name) { |
146
|
|
|
$path = dirname($name); |
147
|
|
|
$name = basename($name); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (!isset(self::$availableCompressors[$name])) { |
151
|
|
|
throw new Exception('invalid compressor:' . $name); |
152
|
|
|
} |
153
|
|
|
return new static($name, $path); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|