1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup; |
3
|
|
|
|
4
|
|
|
use DirectoryIterator; |
5
|
|
|
use SplFileInfo; |
6
|
|
|
use phpbu\App\Util\Arr; |
7
|
|
|
use phpbu\App\Util\Str; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Collector |
11
|
|
|
* |
12
|
|
|
* @package phpbu |
13
|
|
|
* @subpackage Backup |
14
|
|
|
* @author Sebastian Feldmann <[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 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
class Collector |
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
|
7 |
|
* |
39
|
|
|
* @var \phpbu\App\Backup\File[] |
40
|
7 |
|
*/ |
41
|
7 |
|
protected $files; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor |
45
|
|
|
* |
46
|
|
|
* @param Target $target |
47
|
|
|
*/ |
48
|
7 |
|
public function __construct(Target $target) |
49
|
|
|
{ |
50
|
7 |
|
$this->target = $target; |
51
|
7 |
|
} |
52
|
|
|
|
53
|
7 |
|
/** |
54
|
7 |
|
* Get all created backups. |
55
|
7 |
|
* |
56
|
|
|
* @return \phpbu\App\Backup\File[] |
57
|
|
|
*/ |
58
|
|
|
public function getBackupFiles() |
59
|
|
|
{ |
60
|
|
|
if (null === $this->files) { |
61
|
|
|
// create regex to match only created backup files |
62
|
|
|
$this->fileRegex = Str::datePlaceholdersToRegex($this->target->getFilenameRaw()); |
63
|
|
|
$this->files = []; |
64
|
7 |
|
// collect all matching backup files |
65
|
|
|
$this->collect($this->target->getPathThatIsNotChanging(), 0); |
66
|
7 |
|
} |
67
|
|
|
return $this->files; |
68
|
7 |
|
} |
69
|
4 |
|
|
70
|
4 |
|
/** |
71
|
4 |
|
* Recursive backup collecting. |
72
|
|
|
* |
73
|
|
|
* @param string $path |
74
|
4 |
|
* @param int $depth |
75
|
4 |
|
*/ |
76
|
4 |
|
protected function collect($path, $depth) |
77
|
4 |
|
{ |
78
|
4 |
|
$dirIterator = new DirectoryIterator($path); |
79
|
|
|
// collect all matching sub directories and get all the backup files |
80
|
7 |
|
if ($depth < $this->target->countChangingPathElements()) { |
81
|
7 |
|
foreach ($dirIterator as $file) { |
82
|
1 |
|
if ($file->isDot()) { |
83
|
1 |
|
continue; |
84
|
|
|
} |
85
|
7 |
|
if ($this->isValidDirectory($file, $depth)) { |
86
|
7 |
|
$this->collect($file->getPathname(), $depth + 1); |
87
|
7 |
|
} |
88
|
|
|
} |
89
|
|
|
} else { |
90
|
7 |
|
/** @var \phpbu\App\Backup\File $file */ |
91
|
3 |
|
$this->collectFiles($dirIterator); |
92
|
|
|
} |
93
|
7 |
|
} |
94
|
7 |
|
|
95
|
7 |
|
/** |
96
|
7 |
|
* Collect backup files in directory. |
97
|
7 |
|
* |
98
|
|
|
* @param \DirectoryIterator $dirIterator |
99
|
7 |
|
*/ |
100
|
|
|
protected function collectFiles(DirectoryIterator $dirIterator) |
101
|
|
|
{ |
102
|
|
|
foreach ($dirIterator as $i => $file) { |
103
|
|
|
if ($file->isDir()) { |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
// skip currently created backup |
107
|
|
|
if ($file->getPathname() == $this->target->getPathname()) { |
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
if (preg_match('#' . $this->fileRegex . '#i', $file->getFilename())) { |
111
|
|
|
$index = date('YmdHis', $file->getMTime()) . '-' . $i . '-' . $file->getPathname(); |
112
|
|
|
$this->files[$index] = new File($file->getFileInfo()); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Check if the iterated file is part of a valid target path. |
119
|
|
|
* |
120
|
|
|
* @param \SplFileInfo $file |
121
|
|
|
* @param int $depth |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
protected function isValidDirectory(SplFileInfo $file, $depth) |
125
|
|
|
{ |
126
|
|
|
return $file->isDir() && $this->isMatchingDirectory($file->getBasename(), $depth); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Does a directory match the respective target path. |
131
|
|
|
* |
132
|
|
|
* @param string $dir |
133
|
|
|
* @param int $depth |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
protected function isMatchingDirectory($dir, $depth) |
137
|
|
|
{ |
138
|
|
|
$dirTarget = Arr::getValue($this->target->getChangingPathElements(), $depth); |
139
|
|
|
$dirRegex = Str::datePlaceholdersToRegex($dirTarget); |
140
|
|
|
return preg_match('#' . $dirRegex . '#i', $dir); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|