1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Collector; |
3
|
|
|
|
4
|
|
|
use DirectoryIterator; |
5
|
|
|
use phpbu\App\Backup\Collector; |
6
|
|
|
use phpbu\App\Backup\File\Local as FileLocal; |
7
|
|
|
use phpbu\App\Backup\Target; |
8
|
|
|
use phpbu\App\Util; |
9
|
|
|
use SplFileInfo; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Local collector class. |
13
|
|
|
* |
14
|
|
|
* @package phpbu |
15
|
|
|
* @subpackage Backup |
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
17
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
18
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
19
|
|
|
* @link http://phpbu.de/ |
20
|
|
|
* @since Class available since Release 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
class Local extends Abstraction implements Collector |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Constructor |
26
|
|
|
* |
27
|
|
|
* @param \phpbu\App\Backup\Target $target |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Target $target) |
30
|
|
|
{ |
31
|
|
|
$this->target = $target; |
32
|
|
|
$this->fileRegex = Util\Path::datePlaceholdersToRegex($this->target->getFilenameRaw()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Collect all created backups. |
37
|
|
|
*/ |
38
|
|
|
protected function collectBackups() |
39
|
|
|
{ |
40
|
|
|
$this->collect($this->target->getPath()->getPathThatIsNotChanging()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Recursive backup collecting. |
45
|
|
|
* |
46
|
|
|
* @param string $path |
47
|
|
|
*/ |
48
|
|
|
protected function collect(string $path) |
49
|
|
|
{ |
50
|
23 |
|
$dirIterator = new DirectoryIterator($path); |
51
|
|
|
// collect all matching sub directories and get all the backup files |
52
|
23 |
|
$depth = Util\Path::getPathDepth($path); |
53
|
23 |
|
if ($depth < $this->target->getPath()->getPathDepth()) { |
54
|
|
|
foreach ($dirIterator as $file) { |
55
|
|
|
if ($file->isDot()) { |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
if ($this->isValidDirectory($file, $depth)) { |
59
|
|
|
$this->collect($file->getPathname()); |
60
|
10 |
|
} |
61
|
|
|
} |
62
|
10 |
|
} else { |
63
|
|
|
$this->collectFiles($dirIterator); |
64
|
10 |
|
} |
65
|
10 |
|
} |
66
|
|
|
|
67
|
10 |
|
/** |
68
|
|
|
* Collect backup files in directory. |
69
|
10 |
|
* |
70
|
|
|
* @param \DirectoryIterator $dirIterator |
71
|
|
|
*/ |
72
|
|
|
protected function collectFiles(DirectoryIterator $dirIterator) |
73
|
|
|
{ |
74
|
|
|
foreach ($dirIterator as $i => $splFile) { |
75
|
|
|
if ($splFile->isDir()) { |
76
|
|
|
continue; |
77
|
10 |
|
} |
78
|
|
View Code Duplication |
if ($this->isFilenameMatch($splFile->getFilename())) { |
79
|
10 |
|
|
80
|
|
|
$file = new FileLocal($splFile->getFileInfo()); |
81
|
10 |
|
$index = $this->getFileIndex($file); |
82
|
10 |
|
$this->files[$index] = $file; |
83
|
6 |
|
} |
84
|
6 |
|
} |
85
|
6 |
|
} |
86
|
|
|
|
87
|
6 |
|
/** |
88
|
6 |
|
* Check if the iterated file is part of a valid target path. |
89
|
|
|
* |
90
|
|
|
* @param \SplFileInfo $file |
91
|
|
|
* @param int $depth |
92
|
10 |
|
* @return bool |
93
|
|
|
*/ |
94
|
10 |
|
protected function isValidDirectory(SplFileInfo $file, int $depth) |
95
|
|
|
{ |
96
|
|
|
return $file->isDir() && $this->isMatchingDirectory($file->getBasename(), $depth); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Does a directory match the respective target path. |
101
|
10 |
|
* |
102
|
|
|
* @param string $dir |
103
|
10 |
|
* @param int $depth |
104
|
10 |
|
* @return bool |
105
|
10 |
|
*/ |
106
|
|
|
protected function isMatchingDirectory(string $dir, int $depth) |
107
|
|
|
{ |
108
|
10 |
|
$dirTarget = $this->target->getPath()->getPathElementAtIndex($depth); |
109
|
3 |
|
$dirRegex = Util\Path::datePlaceholdersToRegex($dirTarget); |
110
|
|
|
return preg_match('#' . $dirRegex . '#i', $dir); |
111
|
10 |
|
} |
112
|
|
|
} |
113
|
|
|
|