1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Cleaner; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Collector; |
5
|
|
|
use phpbu\App\Backup\Target; |
6
|
|
|
use phpbu\App\Result; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Cleanup Abstraction. |
10
|
|
|
* |
11
|
|
|
* Removes oldest backup till the given capacity isn't exceeded anymore. |
12
|
|
|
* |
13
|
|
|
* @package phpbu |
14
|
|
|
* @subpackage Backup |
15
|
|
|
* @author Sebastian Feldmann <[email protected]> |
16
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
17
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
18
|
|
|
* @link http://phpbu.de/ |
19
|
|
|
* @since Class available since Release 1.0.0 |
20
|
|
|
*/ |
21
|
|
|
abstract class Abstraction |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Cleanup your backup directory. |
25
|
|
|
* |
26
|
|
|
* @see \phpbu\App\Backup\Cleanup::cleanup() |
27
|
|
|
* @param \phpbu\App\Backup\Target $target |
28
|
|
|
* @param \phpbu\App\Backup\Collector $collector |
29
|
|
|
* @param \phpbu\App\Result $result |
30
|
|
|
* @throws \phpbu\App\Backup\Cleaner\Exception |
31
|
|
|
*/ |
32
|
|
|
public function cleanup(Target $target, Collector $collector, Result $result) |
33
|
|
|
{ |
34
|
|
|
foreach ($this->getFilesToDelete($target, $collector) as $file) { |
35
|
|
|
if (!$file->isWritable()) { |
36
|
|
|
throw new Exception(sprintf('can\'t delete file: %s', $file->getPathname())); |
37
|
|
|
} |
38
|
|
|
$result->debug(sprintf('delete %s', $file->getPathname())); |
39
|
|
|
$file->unlink(); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Simulate the cleanup execution. |
45
|
|
|
* |
46
|
|
|
* @param \phpbu\App\Backup\Target $target |
47
|
|
|
* @param \phpbu\App\Backup\Collector $collector |
48
|
|
|
* @param \phpbu\App\Result $result |
49
|
|
|
*/ |
50
|
|
|
public function simulate(Target $target, Collector $collector, Result $result) |
51
|
|
|
{ |
52
|
|
|
foreach ($this->getFilesToDelete($target, $collector) as $file) { |
53
|
|
|
$result->debug(sprintf('delete %s', $file->getPathname())); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return list of files to delete. |
59
|
|
|
* |
60
|
|
|
* @param \phpbu\App\Backup\Target $target |
61
|
|
|
* @param \phpbu\App\Backup\Collector $collector |
62
|
|
|
* @return \phpbu\App\Backup\File[] |
63
|
|
|
* @throws \phpbu\App\Exception |
64
|
|
|
*/ |
65
|
|
|
abstract protected function getFilesToDelete(Target $target, Collector $collector); |
66
|
|
|
} |
67
|
|
|
|