|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpbu\App; |
|
3
|
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Check; |
|
5
|
|
|
use phpbu\App\Backup\Cleaner; |
|
6
|
|
|
use phpbu\App\Backup\Crypter; |
|
7
|
|
|
use phpbu\App\Backup\Source; |
|
8
|
|
|
use phpbu\App\Backup\Sync; |
|
9
|
|
|
use phpbu\App\Backup\Target; |
|
10
|
|
|
use phpbu\App\Log\Logger; |
|
11
|
|
|
use phpbu\App\Runner\Backup\Task; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Factory |
|
15
|
|
|
* |
|
16
|
|
|
* @package phpbu |
|
17
|
|
|
* @subpackage App |
|
18
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
19
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
|
20
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
21
|
|
|
* @link https://phpbu.de/ |
|
22
|
|
|
* @since Class available since Release 1.0.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
class Factory |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Map of available sources, checks, syncs and cleanups. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private static $classMap = [ |
|
32
|
|
|
// type |
|
33
|
|
|
// alias => fqcn |
|
34
|
|
|
'adapter' => [ |
|
35
|
|
|
'array' => '\\phpbu\\App\\Adapter\\PHPArray', |
|
36
|
|
|
'dotenv' => '\\phpbu\\App\\Adapter\\Dotenv', |
|
37
|
|
|
'env' => '\\phpbu\\App\\Adapter\\Env', |
|
38
|
|
|
], |
|
39
|
|
|
'runner' => [ |
|
40
|
|
|
'check' => '\\phpbu\\App\\Runner\\Backup\\Check', |
|
41
|
|
|
'cleaner' => '\\phpbu\\App\\Runner\\Backup\\Cleaner', |
|
42
|
|
|
'crypter' => '\\phpbu\\App\\Runner\\Backup\\Crypter', |
|
43
|
|
|
'source' => '\\phpbu\\App\\Runner\\Backup\\Source', |
|
44
|
|
|
'sync' => '\\phpbu\\App\\Runner\\Backup\\Sync', |
|
45
|
|
|
], |
|
46
|
|
|
'logger' => [ |
|
47
|
|
|
'json' => '\\phpbu\\App\\Log\\Json', |
|
48
|
|
|
'mail' => '\\phpbu\\App\\Log\\Mail', |
|
49
|
|
|
'webhook' => '\\phpbu\\App\\Log\\Webhook', |
|
50
|
|
|
], |
|
51
|
|
|
'source' => [ |
|
52
|
|
|
'arangodump' => '\\phpbu\\App\\Backup\\Source\\Arangodump', |
|
53
|
|
|
'elasticdump' => '\\phpbu\\App\\Backup\\Source\\Elasticdump', |
|
54
|
|
|
'mongodump' => '\\phpbu\\App\\Backup\\Source\\Mongodump', |
|
55
|
|
|
'mysqldump' => '\\phpbu\\App\\Backup\\Source\\Mysqldump', |
|
56
|
|
|
'pgdump' => '\\phpbu\\App\\Backup\\Source\\Pgdump', |
|
57
|
|
|
'redis' => '\\phpbu\\App\\Backup\\Source\\Redis', |
|
58
|
|
|
'rsync' => '\\phpbu\\App\\Backup\\Source\\Rsync', |
|
59
|
|
|
'tar' => '\\phpbu\\App\\Backup\\Source\\Tar', |
|
60
|
|
|
'xtrabackup' => '\\phpbu\\App\\Backup\\Source\\XtraBackup', |
|
61
|
|
|
], |
|
62
|
|
|
'check' => [ |
|
63
|
|
|
'sizemin' => '\\phpbu\\App\\Backup\\Check\\SizeMin', |
|
64
|
|
|
'sizediffpreviouspercent' => '\\phpbu\\App\\Backup\\Check\\SizeDiffPreviousPercent', |
|
65
|
|
|
'sizediffavgpercent' => '\\phpbu\\App\\Backup\\Check\\SizeDiffAvgPercent', |
|
66
|
|
|
], |
|
67
|
|
|
'crypter' => [ |
|
68
|
|
|
'mcrypt' => '\\phpbu\\App\\Backup\\Crypter\\Mcrypt', |
|
69
|
|
|
'openssl' => '\\phpbu\\App\\Backup\\Crypter\\OpenSSL', |
|
70
|
|
|
], |
|
71
|
|
|
'sync' => [ |
|
72
|
|
|
'amazons3' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v3', |
|
73
|
|
|
'amazons3-v3' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v3', |
|
74
|
|
|
'amazons3-v2' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v2', |
|
75
|
|
|
'dropbox' => '\\phpbu\\App\\Backup\\Sync\\Dropbox', |
|
76
|
|
|
'ftp' => '\\phpbu\\App\\Backup\\Sync\\Ftp', |
|
77
|
|
|
'googledrive' => '\\phpbu\\App\\Backup\\Sync\\GoogleDrive', |
|
78
|
|
|
'rsync' => '\\phpbu\\App\\Backup\\Sync\\Rsync', |
|
79
|
|
|
'sftp' => '\\phpbu\\App\\Backup\\Sync\\Sftp', |
|
80
|
|
|
'softlayer' => '\\phpbu\\App\\Backup\\Sync\\SoftLayer', |
|
81
|
|
|
], |
|
82
|
|
|
'cleaner' => [ |
|
83
|
|
|
'capacity' => '\\phpbu\\App\\Backup\\Cleaner\\Capacity', |
|
84
|
|
|
'outdated' => '\\phpbu\\App\\Backup\\Cleaner\\Outdated', |
|
85
|
|
|
'stepwise' => '\\phpbu\\App\\Backup\\Cleaner\\Stepwise', |
|
86
|
|
|
'quantity' => '\\phpbu\\App\\Backup\\Cleaner\\Quantity', |
|
87
|
|
|
], |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Backup Factory. |
|
92
|
|
|
* Creates 'Source', 'Check', 'Crypter', 'Sync' and 'Cleaner' Objects. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $type |
|
95
|
|
|
* @param string $alias |
|
96
|
|
|
* @throws \phpbu\App\Exception |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
*/ |
|
99
|
24 |
View Code Duplication |
protected function create($type, $alias) |
|
100
|
|
|
{ |
|
101
|
24 |
|
self::checkType($type); |
|
102
|
24 |
|
$alias = strtolower($alias); |
|
103
|
|
|
|
|
104
|
24 |
|
if (!isset(self::$classMap[$type][$alias])) { |
|
105
|
1 |
|
throw new Exception(sprintf('unknown %s: %s', $type, $alias)); |
|
106
|
|
|
} |
|
107
|
23 |
|
$class = self::$classMap[$type][$alias]; |
|
108
|
23 |
|
return new $class(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Runner Factory. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $alias |
|
115
|
|
|
* @param bool $isSimulation |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
* @throws \phpbu\App\Exception |
|
118
|
|
|
*/ |
|
119
|
2 |
|
public function createRunner($alias, $isSimulation) |
|
120
|
|
|
{ |
|
121
|
2 |
|
$runner = $this->create('runner', $alias); |
|
122
|
2 |
|
if (!($runner instanceof Task)) { |
|
123
|
1 |
|
throw new Exception(sprintf('Runner \'%s\' has to implement the \'Task\' interface', $alias)); |
|
124
|
|
|
} |
|
125
|
1 |
|
$runner->setSimulation($isSimulation); |
|
126
|
1 |
|
return $runner; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Adapter Factory. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $alias |
|
133
|
|
|
* @param array $conf |
|
134
|
|
|
* @throws \phpbu\App\Exception |
|
135
|
|
|
* @return \phpbu\App\Adapter |
|
136
|
|
|
*/ |
|
137
|
6 |
|
public function createAdapter($alias, $conf = []) |
|
138
|
|
|
{ |
|
139
|
|
|
/** @var \phpbu\App\Adapter $adapter */ |
|
140
|
6 |
|
$adapter = $this->create('adapter', $alias); |
|
141
|
6 |
|
if (!($adapter instanceof Adapter)) { |
|
142
|
1 |
|
throw new Exception(sprintf('adapter \'%s\' has to implement the \'Adapter\' interfaces', $alias)); |
|
143
|
|
|
} |
|
144
|
5 |
|
$adapter->setup($conf); |
|
145
|
5 |
|
return $adapter; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Logger Factory. |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $alias |
|
152
|
|
|
* @param array $conf |
|
153
|
|
|
* @throws \phpbu\App\Exception |
|
154
|
|
|
* @return \phpbu\App\Log\Logger |
|
155
|
|
|
*/ |
|
156
|
3 |
|
public function createLogger($alias, $conf = []) |
|
157
|
|
|
{ |
|
158
|
|
|
/** @var \phpbu\App\Log\Logger $logger */ |
|
159
|
3 |
|
$logger = $this->create('logger', $alias); |
|
160
|
3 |
|
if (!($logger instanceof Logger)) { |
|
161
|
1 |
|
throw new Exception(sprintf('logger \'%s\' has to implement the \'Logger\' interfaces', $alias)); |
|
162
|
|
|
} |
|
163
|
2 |
|
if (!($logger instanceof Listener)) { |
|
164
|
1 |
|
throw new Exception(sprintf('logger \'%s\' has to implement the \'Listener\' interface', $alias)); |
|
165
|
|
|
} |
|
166
|
1 |
|
$logger->setup($conf); |
|
167
|
1 |
|
return $logger; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Create a backup target. |
|
172
|
|
|
* |
|
173
|
|
|
* @param \phpbu\App\Configuration\Backup\Target $conf |
|
174
|
|
|
* @return \phpbu\App\Backup\Target |
|
175
|
|
|
* @throws \phpbu\App\Exception |
|
176
|
|
|
*/ |
|
177
|
|
|
public function createTarget(Configuration\Backup\Target $conf) |
|
178
|
|
|
{ |
|
179
|
|
|
$target = new Target($conf->dirname, $conf->filename); |
|
180
|
|
|
$target->setupPath(); |
|
181
|
|
|
// add possible compressor |
|
182
|
|
|
if (!empty($conf->compression)) { |
|
183
|
|
|
$compression = Target\Compression\Factory::create($conf->compression); |
|
184
|
|
|
$target->setCompression($compression); |
|
185
|
|
|
} |
|
186
|
|
|
return $target; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Source Factory. |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $alias |
|
193
|
|
|
* @param array $conf |
|
194
|
|
|
* @throws \phpbu\App\Exception |
|
195
|
|
|
* @return \phpbu\App\Backup\Source |
|
196
|
|
|
*/ |
|
197
|
2 |
|
public function createSource($alias, $conf = []) |
|
198
|
|
|
{ |
|
199
|
|
|
/** @var \phpbu\App\Backup\Source $source */ |
|
200
|
2 |
|
$source = $this->create('source', $alias); |
|
201
|
2 |
|
if (!($source instanceof Source)) { |
|
202
|
1 |
|
throw new Exception(sprintf('source \'%s\' has to implement the \'Source\' interface', $alias)); |
|
203
|
|
|
} |
|
204
|
1 |
|
$source->setup($conf); |
|
205
|
1 |
|
return $source; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Check Factory. |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $alias |
|
212
|
|
|
* @throws \phpbu\App\Exception |
|
213
|
|
|
* @return \phpbu\App\Backup\Check |
|
214
|
|
|
*/ |
|
215
|
4 |
|
public function createCheck($alias) |
|
216
|
|
|
{ |
|
217
|
|
|
/** @var \phpbu\App\Backup\Check $check */ |
|
218
|
4 |
|
$check = $this->create('check', $alias); |
|
219
|
4 |
|
if (!($check instanceof Check)) { |
|
220
|
1 |
|
throw new Exception(sprintf('Check \'%s\' has to implement the \'Check\' interface', $alias)); |
|
221
|
|
|
} |
|
222
|
3 |
|
return $check; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Crypter Factory. |
|
227
|
|
|
* |
|
228
|
|
|
* @param string $alias |
|
229
|
|
|
* @param array $conf |
|
230
|
|
|
* @throws \phpbu\App\Exception |
|
231
|
|
|
* @return \phpbu\App\Backup\Crypter |
|
232
|
|
|
*/ |
|
233
|
2 |
|
public function createCrypter($alias, $conf = []) |
|
234
|
|
|
{ |
|
235
|
|
|
/** @var \phpbu\App\Backup\Crypter $crypter */ |
|
236
|
2 |
|
$crypter = $this->create('crypter', $alias); |
|
237
|
2 |
|
if (!($crypter instanceof Crypter)) { |
|
238
|
1 |
|
throw new Exception(sprintf('Crypter \'%s\' has to implement the \'Crypter\' interface', $alias)); |
|
239
|
|
|
} |
|
240
|
1 |
|
$crypter->setup($conf); |
|
241
|
1 |
|
return $crypter; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Sync Factory. |
|
246
|
|
|
* |
|
247
|
|
|
* @param string $alias |
|
248
|
|
|
* @param array $conf |
|
249
|
|
|
* @throws \phpbu\App\Exception |
|
250
|
|
|
* @return \phpbu\App\Backup\Sync |
|
251
|
|
|
*/ |
|
252
|
3 |
|
public function createSync($alias, $conf = []) |
|
253
|
|
|
{ |
|
254
|
|
|
/** @var \phpbu\App\Backup\Sync $sync */ |
|
255
|
3 |
|
$sync = $this->create('sync', $alias); |
|
256
|
2 |
|
if (!($sync instanceof Sync)) { |
|
257
|
1 |
|
throw new Exception(sprintf('sync \'%s\' has to implement the \'Sync\' interface', $alias)); |
|
258
|
|
|
} |
|
259
|
1 |
|
$sync->setup($conf); |
|
260
|
1 |
|
return $sync; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Cleaner Factory. |
|
265
|
|
|
* |
|
266
|
|
|
* @param string $alias |
|
267
|
|
|
* @param array $conf |
|
268
|
|
|
* @throws \phpbu\App\Exception |
|
269
|
|
|
* @return \phpbu\App\Backup\Cleaner |
|
270
|
|
|
*/ |
|
271
|
2 |
|
public function createCleaner($alias, $conf = []) |
|
272
|
|
|
{ |
|
273
|
|
|
/** @var \phpbu\App\Backup\Cleaner $cleaner */ |
|
274
|
2 |
|
$cleaner = $this->create('cleaner', $alias); |
|
275
|
2 |
|
if (!($cleaner instanceof Cleaner)) { |
|
276
|
1 |
|
throw new Exception(sprintf('cleaner \'%s\' has to implement the \'Cleaner\' interface', $alias)); |
|
277
|
|
|
} |
|
278
|
1 |
|
$cleaner->setup($conf); |
|
279
|
1 |
|
return $cleaner; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Extend the backup factory. |
|
284
|
|
|
* |
|
285
|
|
|
* @param string $type Type to create 'adapter', 'source', 'check', 'sync' or 'cleaner' |
|
286
|
|
|
* @param string $alias Name the class is registered at |
|
287
|
|
|
* @param string $fqcn Full Qualified Class Name |
|
288
|
|
|
* @param boolean $force Overwrite already registered class |
|
289
|
|
|
* @throws \phpbu\App\Exception |
|
290
|
|
|
*/ |
|
291
|
23 |
View Code Duplication |
public static function register($type, $alias, $fqcn, $force = false) |
|
292
|
|
|
{ |
|
293
|
23 |
|
self::checkType($type); |
|
294
|
22 |
|
$alias = strtolower($alias); |
|
295
|
|
|
|
|
296
|
22 |
|
if (!$force && isset(self::$classMap[$type][$alias])) { |
|
297
|
1 |
|
throw new Exception(sprintf('%s is already registered use force parameter to overwrite', $type)); |
|
298
|
|
|
} |
|
299
|
21 |
|
self::$classMap[$type][$alias] = $fqcn; |
|
300
|
21 |
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Throws an exception if type is invalid. |
|
304
|
|
|
* |
|
305
|
|
|
* @param string $type |
|
306
|
|
|
* @throws \phpbu\App\Exception |
|
307
|
|
|
*/ |
|
308
|
30 |
|
private static function checkType($type) |
|
309
|
|
|
{ |
|
310
|
30 |
|
if (!isset(self::$classMap[$type])) { |
|
311
|
1 |
|
throw new Exception( |
|
312
|
1 |
|
'invalid type, please use only \'' . implode('\', \'', array_keys(self::$classMap)) . '\'' |
|
313
|
|
|
); |
|
314
|
|
|
} |
|
315
|
29 |
|
} |
|
316
|
|
|
} |
|
317
|
|
|
|