|
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\DestinationInterface; |
|
10
|
|
|
use RunOpenCode\Backup\Exception\DestinationException; |
|
11
|
|
|
|
|
12
|
|
|
class FlysystemDestination implements DestinationInterface |
|
13
|
|
|
{ |
|
14
|
|
|
protected $flysystem; |
|
15
|
|
|
|
|
16
|
|
|
protected $backups; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(FilesystemInterface $flysystem) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->flysystem = $flysystem; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
public function push(BackupInterface $backup) |
|
27
|
|
|
{ |
|
28
|
|
|
throw new DestinationException('Flysystem is not implemented yet.'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
public function get($name) |
|
35
|
|
|
{ |
|
36
|
|
|
if (is_null($this->backups)) { |
|
37
|
|
|
$this->load(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $this->backups[$name]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function has($name) |
|
47
|
|
|
{ |
|
48
|
|
|
if (is_null($this->backups)) { |
|
49
|
|
|
$this->load(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return array_key_exists($name, $this->backups); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function delete($name) |
|
59
|
|
|
{ |
|
60
|
|
|
try { |
|
61
|
|
|
$this->flysystem->deleteDir($name); |
|
62
|
|
|
} catch (\Exception $e) { |
|
63
|
|
|
throw new DestinationException(sprintf('Unable to remove backup "%s" from flysystem destination.', $name), 0, $e); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (!is_null($this->backups)) { |
|
67
|
|
|
unset($this->backups[$name]); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function all() |
|
75
|
|
|
{ |
|
76
|
|
|
if (is_null($this->backups)) { |
|
77
|
|
|
$this->load(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this->backups; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getIterator() |
|
87
|
|
|
{ |
|
88
|
|
|
if (is_null($this->backups)) { |
|
89
|
|
|
$this->load(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return new \ArrayIterator($this->backups); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Load backups from destination. |
|
97
|
|
|
* |
|
98
|
|
|
* @return BackupInterface[] |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function load() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->backups = array(); |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @var \SplFileInfo $content |
|
106
|
|
|
*/ |
|
107
|
|
|
foreach ($this->flysystem->listContents() as $content) { |
|
108
|
|
|
|
|
109
|
|
|
if ($content->isDir()) { |
|
110
|
|
|
|
|
111
|
|
|
$backup = new Backup($content->getBasename(), array(), $content->getCTime(), $content->getMTime()); |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @var \SplFileInfo $backupFile |
|
115
|
|
|
*/ |
|
116
|
|
|
foreach ($this->flysystem->listContents($content->getBasename(), true) as $backupFile) { |
|
117
|
|
|
|
|
118
|
|
|
if ($backupFile->isFile()) { |
|
119
|
|
|
|
|
120
|
|
|
$backup->addFile(File::fromSplFileInfo($backupFile)); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$this->backups[$backup->getName()] = $backup; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
|
|
public function count() |
|
133
|
|
|
{ |
|
134
|
|
|
return count($this->getIterator()); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|