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 | 21 | public function __construct(Target $target) |
|
30 | { |
||
31 | 21 | $this->target = $target; |
|
32 | 21 | } |
|
33 | |||
34 | /** |
||
35 | * Collect all created backups. |
||
36 | */ |
||
37 | 8 | protected function collectBackups() |
|
38 | { |
||
39 | 8 | $this->fileRegex = Util\Path::datePlaceholdersToRegex($this->target->getFilenameRaw()); |
|
40 | 8 | $this->collect($this->target->getPath()->getPathThatIsNotChanging()); |
|
41 | 8 | } |
|
42 | |||
43 | /** |
||
44 | * Recursive backup collecting. |
||
45 | * |
||
46 | * @param string $path |
||
47 | */ |
||
48 | 8 | protected function collect(string $path) |
|
49 | { |
||
50 | 8 | $dirIterator = new DirectoryIterator($path); |
|
51 | // collect all matching sub directories and get all the backup files |
||
52 | 8 | $depth = Util\Path::getPathDepth($path); |
|
53 | 8 | if ($depth < $this->target->getPath()->getPathDepth()) { |
|
54 | 4 | foreach ($dirIterator as $file) { |
|
55 | 4 | if ($file->isDot()) { |
|
56 | 4 | continue; |
|
57 | } |
||
58 | 4 | if ($this->isValidDirectory($file, $depth)) { |
|
59 | 4 | $this->collect($file->getPathname()); |
|
60 | } |
||
61 | } |
||
62 | } else { |
||
63 | 8 | $this->collectFiles($dirIterator); |
|
64 | } |
||
65 | 8 | } |
|
66 | |||
67 | /** |
||
68 | * Collect backup files in directory. |
||
69 | * |
||
70 | * @param \DirectoryIterator $dirIterator |
||
71 | */ |
||
72 | 8 | protected function collectFiles(DirectoryIterator $dirIterator) |
|
73 | { |
||
74 | 8 | foreach ($dirIterator as $i => $splFile) { |
|
75 | 8 | if ($splFile->isDir()) { |
|
76 | 8 | continue; |
|
77 | } |
||
78 | 8 | if ($this->isFilenameMatch($splFile->getFilename())) { |
|
79 | 8 | $file = new FileLocal($splFile->getFileInfo()); |
|
80 | 8 | $index = $this->getFileIndex($file); |
|
81 | 8 | $this->files[$index] = $file; |
|
82 | } |
||
83 | } |
||
84 | 8 | } |
|
85 | |||
86 | /** |
||
87 | * Check if the iterated file is part of a valid target path. |
||
88 | * |
||
89 | * @param \SplFileInfo $file |
||
90 | * @param int $depth |
||
91 | * @return bool |
||
92 | */ |
||
93 | 4 | protected function isValidDirectory(SplFileInfo $file, int $depth) |
|
94 | { |
||
95 | 4 | return $file->isDir() && $this->isMatchingDirectory($file->getBasename(), $depth); |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Does a directory match the respective target path. |
||
100 | * |
||
101 | * @param string $dir |
||
102 | * @param int $depth |
||
103 | * @return bool |
||
104 | */ |
||
105 | 4 | protected function isMatchingDirectory(string $dir, int $depth) |
|
106 | { |
||
107 | 4 | $dirTarget = $this->target->getPath()->getPathElementAtIndex($depth); |
|
108 | 4 | $dirRegex = Util\Path::datePlaceholdersToRegex($dirTarget); |
|
109 | 4 | return preg_match('#' . $dirRegex . '#i', $dir); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
110 | } |
||
111 | } |
||
112 |