1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RunOpenCode\Backup\Destination; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\FilesystemInterface; |
6
|
|
|
use RunOpenCode\Backup\Backup\Backup; |
7
|
|
|
use RunOpenCode\Backup\Backup\File; |
8
|
|
|
use RunOpenCode\Backup\Contract\BackupInterface; |
9
|
|
|
use RunOpenCode\Backup\Contract\FileInterface; |
10
|
|
|
use RunOpenCode\Backup\Exception\DestinationException; |
11
|
|
|
|
12
|
|
|
class FlysystemDestination extends BaseDestination |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var FilesystemInterface |
16
|
|
|
*/ |
17
|
|
|
protected $flysystem; |
18
|
|
|
|
19
|
|
|
public function __construct(FilesystemInterface $flysystem) |
20
|
|
|
{ |
21
|
|
|
$this->flysystem = $flysystem; |
22
|
|
|
} |
23
|
|
|
|
24
|
4 |
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
4 |
|
*/ |
27
|
4 |
|
protected function getDirectoryForBackup(BackupInterface $backup) |
28
|
|
|
{ |
29
|
|
|
if (!$this->flysystem->has($backup->getName())) { |
30
|
|
|
|
31
|
|
|
if (!$this->flysystem->createDir($backup->getName())) { |
32
|
4 |
|
throw new DestinationException(sprintf('Unable to create backup directory "%s" in flysystem destination.', $backup->getName())); |
33
|
|
|
} |
34
|
4 |
|
} |
35
|
4 |
|
|
36
|
4 |
|
return $backup->getName(); |
37
|
|
|
} |
38
|
4 |
|
|
39
|
|
|
/** |
40
|
4 |
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
protected function getFiles($path) |
43
|
|
|
{ |
44
|
4 |
|
$result = array(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
4 |
|
* @var \SplFileInfo $file |
48
|
4 |
|
*/ |
49
|
2 |
|
foreach ($this->flysystem->listContents($path, true) as $file) { |
50
|
4 |
|
|
51
|
|
|
if ($file['type'] == 'file') { |
52
|
2 |
|
$file = File::fromFlysystemMetadata($file, $path); |
53
|
|
|
$result[$file->getRelativePath()] = $file; |
54
|
|
|
} |
55
|
|
|
} |
56
|
4 |
|
|
57
|
|
|
return $result; |
58
|
|
|
} |
59
|
|
|
|
60
|
4 |
|
/** |
61
|
2 |
|
* {@inheritdoc} |
62
|
2 |
|
*/ |
63
|
|
|
protected function pushFile($backupDirectory, FileInterface $backupFile) |
64
|
4 |
|
{ |
65
|
|
|
$path = $backupDirectory . '/' . $backupFile->getRelativePath(); |
66
|
4 |
|
|
67
|
|
|
try { |
68
|
|
|
|
69
|
4 |
|
if ($this->flysystem->has($path)) { |
70
|
|
|
|
71
|
|
|
if ($backupFile->getModifiedAt() > new \DateTime('@' . $this->flysystem->getTimestamp($path))) { |
72
|
|
|
$resource = fopen($backupFile->getPath(), 'r'); |
73
|
4 |
|
$this->flysystem->updateStream($path, $resource); |
74
|
2 |
|
fclose($resource); |
75
|
4 |
|
} |
76
|
|
|
|
77
|
4 |
|
} else { |
78
|
|
|
$resource = fopen($backupFile->getPath(), 'r'); |
79
|
4 |
|
$this->flysystem->putStream($path, $resource); |
80
|
|
|
fclose($resource); |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
} catch (\Exception $e) { |
84
|
|
|
throw new DestinationException(sprintf('Unable to backup file "%s" to flysystem destination.', $backupFile->getPath()), 0, $e); |
85
|
|
|
} |
86
|
4 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
4 |
|
protected function load() |
92
|
|
|
{ |
93
|
4 |
|
$this->backups = array(); |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var \SplFileInfo $content |
97
|
4 |
|
*/ |
98
|
|
|
foreach ($this->flysystem->listContents() as $content) { |
99
|
|
|
|
100
|
|
|
if ($content['type'] == 'dir') { |
101
|
|
|
|
102
|
|
|
$backup = new Backup($content['basename'], $this->getFiles($content['path']), $content['timestamp'], $content['timestamp']); |
103
|
4 |
|
|
104
|
|
|
$this->backups[$backup->getName()] = $backup; |
105
|
4 |
|
} |
106
|
4 |
|
} |
107
|
4 |
|
} |
108
|
|
|
|
109
|
4 |
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
protected function doDelete($name) |
113
|
|
|
{ |
114
|
|
|
try { |
115
|
|
|
$this->flysystem->deleteDir($name); |
116
|
|
|
} catch (\Exception $e) { |
117
|
|
|
throw new DestinationException(sprintf('Unable to remove backup "%s" from flysystem destination.', $name), 0, $e); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
|
|
protected function removeFile($backupDirectory, FileInterface $backupFile) |
125
|
|
|
{ |
126
|
|
|
$path = $backupDirectory . '/' . $backupFile->getRelativePath(); |
127
|
|
|
|
128
|
|
|
try { |
129
|
|
|
$this->flysystem->delete($path); |
130
|
|
|
} catch (\Exception $e) { |
131
|
|
|
throw new DestinationException(sprintf('Unable to cleanup backup destination "%s" after backup process, file "%s" could not be removed.', $backupDirectory, $path), 0, $e); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
protected function removeEmptyDirectories($backupName) |
139
|
|
|
{ |
140
|
|
|
/** |
141
|
|
|
* @var \SplFileInfo $dir |
142
|
|
|
*/ |
143
|
2 |
|
foreach ($this->flysystem->listContents($backupName) as $dir) { |
144
|
|
|
|
145
|
2 |
|
if ($dir['type'] != 'dir') { |
146
|
|
|
continue; |
147
|
|
|
} |
148
|
|
|
|
149
|
2 |
|
if (count($this->flysystem->listContents($dir['path'])) > 0) { |
150
|
|
|
$this->removeEmptyDirectories($dir['path']); |
151
|
|
|
} else { |
152
|
|
|
$this->flysystem->deleteDir($dir['path']); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|