1 | <?php |
||
12 | class FlysystemDestination extends BaseDestination |
||
13 | { |
||
14 | /** |
||
15 | * @var FilesystemInterface |
||
16 | */ |
||
17 | protected $flysystem; |
||
18 | |||
19 | 4 | public function __construct(FilesystemInterface $flysystem) |
|
23 | |||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | public function delete($name) |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | 4 | public function push(BackupInterface $backup) |
|
44 | { |
||
45 | 4 | $backupDirectory = $backup->getName(); |
|
46 | |||
47 | 4 | if (!$this->flysystem->has($backupDirectory) && !$this->flysystem->createDir($backupDirectory)) { |
|
48 | throw new DestinationException(sprintf('Unable to create backup directory "%s" in flysystem destination.', $backupDirectory)); |
||
49 | } |
||
50 | |||
51 | 4 | $removedBackupFiles = $this->getFiles($backupDirectory); |
|
52 | |||
53 | /** |
||
54 | * @var FileInterface $backupFile |
||
55 | */ |
||
56 | 4 | foreach ($backup->getFiles() as $backupFile) { |
|
57 | |||
58 | 4 | if (isset($removedBackupFiles[$backupFile->getRelativePath()])) { |
|
59 | 2 | unset($removedBackupFiles[$backupFile->getRelativePath()]); |
|
60 | 2 | } |
|
61 | |||
62 | 4 | $path = $backupDirectory . '/' . $backupFile->getRelativePath(); |
|
63 | |||
64 | try { |
||
65 | |||
66 | 4 | if ($this->flysystem->has($path)) { |
|
67 | |||
68 | 2 | if ($backupFile->getModifiedAt() > new \DateTime('@' . $this->flysystem->getTimestamp($path))) { |
|
69 | $resource = fopen($backupFile->getPath(), 'r'); |
||
70 | $this->flysystem->updateStream($path, $resource); |
||
71 | fclose($resource); |
||
72 | } |
||
73 | |||
74 | 2 | } else { |
|
75 | 4 | $resource = fopen($backupFile->getPath(), 'r'); |
|
76 | 4 | $this->flysystem->putStream($path, $resource); |
|
77 | 4 | fclose($resource); |
|
78 | } |
||
79 | |||
80 | 4 | } catch (\Exception $e) { |
|
81 | throw new DestinationException(sprintf('Unable to backup file "%s" to flysystem destination.', $backupFile->getPath()), 0, $e); |
||
82 | } |
||
83 | 4 | } |
|
84 | |||
85 | /** |
||
86 | * @var FileInterface $removedBackupFile |
||
87 | */ |
||
88 | 4 | foreach ($removedBackupFiles as $removedBackupFile) { |
|
89 | |||
90 | 2 | $path = $backupDirectory . '/' . $removedBackupFile->getRelativePath(); |
|
91 | |||
92 | try { |
||
93 | 2 | $this->flysystem->delete($path); |
|
94 | 2 | } catch (\Exception $e) { |
|
95 | throw new DestinationException(sprintf('Unable to cleanup backup destination "%s" after backup process, file "%s" could not be removed.', $backupDirectory, $path), 0, $e); |
||
96 | } |
||
97 | 4 | } |
|
98 | |||
99 | 4 | $this->removeEmptyDirectories($backupDirectory); |
|
100 | |||
101 | 4 | if (!empty($this->backups)) { |
|
102 | $this->backups[] = $backup; |
||
103 | } |
||
104 | 4 | } |
|
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | 4 | protected function load() |
|
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 4 | protected function getFiles($path) |
|
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | 4 | protected function removeEmptyDirectories($backupName) |
|
169 | } |
||
170 |