1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Backup package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2015 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* This project is fork of "kbond/php-backup", for full credits info, please |
11
|
|
|
* view CREDITS file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
namespace RunOpenCode\Backup\Destination; |
14
|
|
|
|
15
|
|
|
use RunOpenCode\Backup\Contract\BackupInterface; |
16
|
|
|
use RunOpenCode\Backup\Contract\DestinationInterface; |
17
|
|
|
use RunOpenCode\Backup\Contract\FileInterface; |
18
|
|
|
use RunOpenCode\Backup\Exception\DestinationException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class BaseDestination |
22
|
|
|
* |
23
|
|
|
* BaseDestination provides prototype for concrete destination implementation. |
24
|
|
|
* |
25
|
|
|
* @package RunOpenCode\Backup\Destination |
26
|
|
|
*/ |
27
|
|
|
abstract class BaseDestination implements DestinationInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var BackupInterface[] |
31
|
|
|
*/ |
32
|
|
|
protected $backups; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function getIterator() |
38
|
|
|
{ |
39
|
|
|
if (is_null($this->backups)) { |
40
|
|
|
$this->load(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return new \ArrayIterator($this->backups); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function get($name) |
50
|
|
|
{ |
51
|
|
|
if (is_null($this->backups)) { |
52
|
|
|
$this->load(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->backups[$name]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function has($name) |
62
|
|
|
{ |
63
|
|
|
if (is_null($this->backups)) { |
64
|
|
|
$this->load(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return array_key_exists($name, $this->backups); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function all() |
74
|
|
|
{ |
75
|
|
|
if (is_null($this->backups)) { |
76
|
|
|
$this->load(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->backups; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function count() |
86
|
|
|
{ |
87
|
|
|
if (is_null($this->backups)) { |
88
|
|
|
$this->load(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return count($this->backups); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function delete($name) |
98
|
|
|
{ |
99
|
|
|
$this->doDelete($name); |
100
|
|
|
|
101
|
|
|
if (!is_null($this->backups)) { |
102
|
|
|
unset($this->backups[$name]); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function push(BackupInterface $backup) |
110
|
|
|
{ |
111
|
|
|
$backupDirectory = $this->getDirectoryForBackup($backup); |
112
|
|
|
$removedBackupFiles = $this->getFiles($backupDirectory); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var FileInterface $backupFile |
116
|
|
|
*/ |
117
|
|
|
foreach ($backup->getFiles() as $backupFile) { |
118
|
|
|
|
119
|
|
|
if (isset($removedBackupFiles[$backupFile->getRelativePath()])) { |
120
|
|
|
unset($removedBackupFiles[$backupFile->getRelativePath()]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->pushFile($backupDirectory, $backupFile); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
foreach ($removedBackupFiles as $removedBackupFile) { |
127
|
|
|
$this->removeFile($backupDirectory, $removedBackupFile); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$this->removeEmptyDirectories($backupDirectory); |
131
|
|
|
|
132
|
|
|
if (!empty($this->backups)) { |
133
|
|
|
$this->backups[] = $backup; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get backup directory where backups will be transferred. If directory does not exists, it will be created. |
139
|
|
|
* |
140
|
|
|
* @param BackupInterface $backup Backup for which directory is fetched/created. |
141
|
|
|
* @return string Path to created backup directory. |
142
|
|
|
* |
143
|
|
|
* @throws DestinationException |
144
|
|
|
*/ |
145
|
|
|
protected abstract function getDirectoryForBackup(BackupInterface $backup); |
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Load backups from destination. |
149
|
|
|
* |
150
|
|
|
* @return BackupInterface[] |
151
|
|
|
*/ |
152
|
|
|
protected abstract function load(); |
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Delete backup from destination. |
156
|
|
|
* |
157
|
|
|
* @param string $name Backup name to delete. |
158
|
|
|
* |
159
|
|
|
* @throws DestinationException |
160
|
|
|
*/ |
161
|
|
|
protected abstract function doDelete($name); |
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get all files in path. |
165
|
|
|
* |
166
|
|
|
* @param string $path Path to directory where files residue. |
167
|
|
|
* @return FileInterface[] List of files in given location |
168
|
|
|
*/ |
169
|
|
|
protected abstract function getFiles($path); |
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Push file to backup destination, or update it if exist and it is newer. |
173
|
|
|
* |
174
|
|
|
* @param string $backupDirectory Backup directory in backup destination where to push file. |
175
|
|
|
* @param FileInterface $backupFile File to push to destination. |
176
|
|
|
* |
177
|
|
|
* @throws DestinationException |
178
|
|
|
*/ |
179
|
|
|
protected abstract function pushFile($backupDirectory, FileInterface $backupFile); |
|
|
|
|
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Delete backup file from destination. |
183
|
|
|
* |
184
|
|
|
* @param string $backupDirectory Directory where backup file residue. |
185
|
|
|
* @param FileInterface $backupFile Backup file to remove. |
186
|
|
|
* |
187
|
|
|
* @throws DestinationException |
188
|
|
|
*/ |
189
|
|
|
protected abstract function removeFile($backupDirectory, FileInterface $backupFile); |
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Remove empty directories from backup destination. |
193
|
|
|
* |
194
|
|
|
* @param string $backupDirectory Backup directory to cleanup. |
195
|
|
|
* |
196
|
|
|
* @throws DestinationException |
197
|
|
|
*/ |
198
|
|
|
protected abstract function removeEmptyDirectories($backupDirectory); |
|
|
|
|
199
|
|
|
} |