1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Collector; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\File; |
5
|
|
|
use phpbu\App\Util; |
6
|
|
|
use phpbu\App\Backup\Target; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Abstraction class. |
10
|
|
|
* |
11
|
|
|
* @package phpbu |
12
|
|
|
* @subpackage Backup |
13
|
|
|
* @author Sebastian Feldmann <[email protected]> |
14
|
|
|
* @author Vitaly Baev <[email protected]> |
15
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
16
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
17
|
|
|
* @link http://phpbu.de/ |
18
|
|
|
* @since Class available since Release 5.1.0 |
19
|
|
|
*/ |
20
|
|
|
abstract class Abstraction |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Backup target |
24
|
|
|
* |
25
|
|
|
* @var \phpbu\App\Backup\Target |
26
|
|
|
*/ |
27
|
|
|
protected $target; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Target filename regex |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $fileRegex; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Collection cache |
38
|
|
|
* |
39
|
|
|
* @var \phpbu\App\Backup\File\Local[] |
40
|
|
|
*/ |
41
|
|
|
protected $files; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Indicates if current execution is a simulation. |
45
|
|
|
* |
46
|
|
|
* @var bool |
47
|
|
|
*/ |
48
|
|
|
protected $isSimulation = false; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Index count for collected backups. |
52
|
|
|
* |
53
|
|
|
* @var int |
54
|
|
|
*/ |
55
|
|
|
protected $index = 0; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Indicate that this is a simulation and make sure the collector includes a fake target. |
59
|
|
|
* |
60
|
|
|
* @param bool $isSimulation |
61
|
|
|
*/ |
62
|
|
|
public function setSimulation(bool $isSimulation) |
63
|
|
|
{ |
64
|
|
|
$this->isSimulation = $isSimulation; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get all created backups. |
69
|
|
|
* |
70
|
|
|
* @return \phpbu\App\Backup\File\Local[] |
71
|
|
|
*/ |
72
|
|
|
public function getBackupFiles() : array |
73
|
|
|
{ |
74
|
|
|
if (null === $this->files) { |
75
|
|
|
$this->files = []; |
76
|
|
|
$this->collectBackups(); |
77
|
|
|
// if current run is a simulation |
78
|
|
|
// add a fake target to the collected files |
79
|
|
|
if ($this->isSimulation) { |
80
|
|
|
$file = new File\Simulation( |
81
|
|
|
time(), |
82
|
|
|
20000000, |
83
|
|
|
$this->target->getPath()->getPath(), |
84
|
|
|
$this->target->getFilename() |
85
|
|
|
); |
86
|
|
|
$this->files[$this->getFileIndex($file)] = $file; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
return $this->files; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Collect all created backups. |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
abstract protected function collectBackups(); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return an array index for a given file for key sorting the list later. |
101
|
|
|
* |
102
|
|
|
* @param \phpbu\App\Backup\File $file |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function getFileIndex(File $file) : string |
106
|
|
|
{ |
107
|
|
|
return $file->getMTime() . '-' . $file->getFilename() . '-' . $this->index++; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns true if filename matches the target regex |
112
|
|
|
* |
113
|
|
|
* @param string $filename |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
protected function isFilenameMatch(string $filename) : bool |
117
|
|
|
{ |
118
|
|
|
return preg_match('#' . $this->fileRegex . '#i', $filename); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|