1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Backup\BackupDestination; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Contracts\Filesystem\Factory; |
8
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
9
|
|
|
use Spatie\Backup\Exceptions\InvalidBackupDestination; |
10
|
|
|
|
11
|
|
|
class BackupDestination |
12
|
|
|
{ |
13
|
|
|
/** @var \Illuminate\Contracts\Filesystem\Filesystem */ |
14
|
|
|
protected $disk; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $diskName; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $backupName; |
21
|
|
|
|
22
|
|
|
/** @var Exception */ |
23
|
|
|
public $connectionError; |
24
|
|
|
|
25
|
|
|
/** @var null|\Spatie\Backup\BackupDestination\BackupCollection */ |
26
|
|
|
protected $backupCollectionCache = null; |
27
|
|
|
|
28
|
|
|
public function __construct(Filesystem $disk = null, string $backupName, string $diskName) |
29
|
|
|
{ |
30
|
|
|
$this->disk = $disk; |
31
|
|
|
|
32
|
|
|
$this->diskName = $diskName; |
33
|
|
|
|
34
|
|
|
$this->backupName = preg_replace('/[^a-zA-Z0-9.]/', '-', $backupName); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function disk(): Filesystem |
38
|
|
|
{ |
39
|
|
|
return $this->disk; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function diskName(): string |
43
|
|
|
{ |
44
|
|
|
return $this->diskName; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function filesystemType(): string |
48
|
|
|
{ |
49
|
|
|
if (is_null($this->disk)) { |
50
|
|
|
return 'unknown'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$adapterClass = get_class($this->disk->getDriver()->getAdapter()); |
54
|
|
|
|
55
|
|
|
$filesystemType = last(explode('\\', $adapterClass)); |
56
|
|
|
|
57
|
|
|
return strtolower($filesystemType); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function create(string $diskName, string $backupName): self |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
|
|
$disk = app(Factory::class)->disk($diskName); |
64
|
|
|
|
65
|
|
|
return new static($disk, $backupName, $diskName); |
66
|
|
|
} catch (Exception $exception) { |
67
|
|
|
$backupDestination = new static(null, $backupName, $diskName); |
68
|
|
|
|
69
|
|
|
$backupDestination->connectionError = $exception; |
70
|
|
|
|
71
|
|
|
return $backupDestination; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function write(string $file) |
76
|
|
|
{ |
77
|
|
|
if (is_null($this->disk)) { |
78
|
|
|
throw InvalidBackupDestination::diskNotSet(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$destination = $this->backupName.'/'.pathinfo($file, PATHINFO_BASENAME); |
82
|
|
|
|
83
|
|
|
$handle = fopen($file, 'r+'); |
84
|
|
|
|
85
|
|
|
$options = $this->extraOptions(); |
86
|
|
|
|
87
|
|
|
$this->disk->getDriver()->writeStream($destination, $handle, $options); |
88
|
|
|
|
89
|
|
|
if (is_resource($handle)) { |
90
|
|
|
fclose($handle); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function backupName(): string |
95
|
|
|
{ |
96
|
|
|
return $this->backupName; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function backups(): BackupCollection |
100
|
|
|
{ |
101
|
|
|
if ($this->backupCollectionCache) { |
102
|
|
|
return $this->backupCollectionCache; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$files = is_null($this->disk) ? [] : $this->disk->allFiles($this->backupName); |
106
|
|
|
|
107
|
|
|
return $this->backupCollectionCache = BackupCollection::createFromFiles( |
|
|
|
|
108
|
|
|
$this->disk, |
109
|
|
|
$files |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function connectionError(): Exception |
114
|
|
|
{ |
115
|
|
|
return $this->connectionError; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function extraOptions(): ?array |
119
|
|
|
{ |
120
|
|
|
$extraConfig = config('filesystems.disks.'.$this->diskName().'.backup_extra_options') ?? []; |
121
|
|
|
|
122
|
|
|
if (! is_array($extraConfig) || (count($extraConfig) < 1)) { |
123
|
|
|
return []; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $extraConfig; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function isReachable(): bool |
130
|
|
|
{ |
131
|
|
|
if (is_null($this->disk)) { |
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
try { |
136
|
|
|
$this->disk->allFiles($this->backupName); |
137
|
|
|
|
138
|
|
|
return true; |
139
|
|
|
} catch (Exception $exception) { |
140
|
|
|
$this->connectionError = $exception; |
141
|
|
|
|
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function usedStorage(): int |
147
|
|
|
{ |
148
|
|
|
return $this->backups()->size(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function newestBackup(): ?Backup |
152
|
|
|
{ |
153
|
|
|
return $this->backups()->newest(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function oldestBackup(): ?Backup |
157
|
|
|
{ |
158
|
|
|
return $this->backups()->oldest(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function newestBackupIsOlderThan(Carbon $date): bool |
162
|
|
|
{ |
163
|
|
|
$newestBackup = $this->newestBackup(); |
164
|
|
|
|
165
|
|
|
if (is_null($newestBackup)) { |
166
|
|
|
return true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $newestBackup->date()->gt($date); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function fresh(): self |
173
|
|
|
{ |
174
|
|
|
$this->backupCollectionCache = null; |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..