1 | <?php |
||
30 | class LocalDestination extends BaseDestination |
||
31 | { |
||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $directory; |
||
36 | |||
37 | /** |
||
38 | * @var Filesystem |
||
39 | */ |
||
40 | private $filesystem; |
||
41 | |||
42 | 14 | public function __construct($directory, Filesystem $filesystem = null) |
|
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | public function delete($name) |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 14 | public function push(BackupInterface $backup) |
|
78 | { |
||
79 | 14 | $backupDirectory = $this->directory . DIRECTORY_SEPARATOR . $backup->getName(); |
|
80 | |||
81 | try { |
||
82 | 14 | $this->filesystem->mkdir($backupDirectory); |
|
83 | 7 | } catch (\Exception $e) { |
|
84 | throw new DestinationException(sprintf('Unable to create backup directory "%s" for backup "%s" in local destination.', $backupDirectory, $backup->getName())); |
||
85 | } |
||
86 | |||
87 | 14 | $removedBackupFiles = $this->getFiles($backupDirectory); |
|
88 | |||
89 | /** |
||
90 | * @var FileInterface $backupFile |
||
91 | */ |
||
92 | 14 | foreach ($backup->getFiles() as $backupFile) { |
|
93 | |||
94 | 14 | if (isset($removedBackupFiles[$backupFile->getRelativePath()])) { |
|
95 | 2 | unset($removedBackupFiles[$backupFile->getRelativePath()]); |
|
96 | 1 | } |
|
97 | |||
98 | try { |
||
99 | 14 | $this->filesystem->copy($backupFile->getPath(), sprintf('%s%s%s', $backupDirectory, DIRECTORY_SEPARATOR, $backupFile->getRelativePath())); |
|
100 | 7 | } catch (\Exception $e) { |
|
101 | 8 | throw new DestinationException(sprintf('Unable to backup file "%s" to destination "%s".', $backupFile->getPath(), $this->directory), 0, $e); |
|
102 | } |
||
103 | 7 | } |
|
104 | |||
105 | /** |
||
106 | * @var FileInterface $removedBackupFile |
||
107 | */ |
||
108 | 14 | foreach ($removedBackupFiles as $removedBackupFile) { |
|
109 | |||
110 | 2 | $path = $backupDirectory . DIRECTORY_SEPARATOR . $removedBackupFile->getRelativePath(); |
|
111 | |||
112 | try { |
||
113 | 2 | $this->filesystem->remove($path); |
|
114 | 1 | } catch (\Exception $e) { |
|
115 | 1 | throw new DestinationException(sprintf('Unable to cleanup backup destination "%s" after backup process, file "%s" could not be removed.', $backupDirectory, $path), 0, $e); |
|
116 | } |
||
117 | 7 | } |
|
118 | |||
119 | 14 | $this->removeEmptyDirectories($backupDirectory); |
|
120 | |||
121 | 14 | if (!empty($this->backups)) { |
|
122 | $this->backups[] = $backup; |
||
123 | 1 | } |
|
124 | 14 | } |
|
125 | |||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 14 | protected function getFiles($path) |
|
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | 10 | protected function load() |
|
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | 14 | protected function removeEmptyDirectories($backupDirectory) |
|
177 | } |
||
178 |