|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Backup\BackupDestination; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Filesystem\Factory; |
|
6
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
|
|
9
|
|
|
class BackupDestination |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var \Illuminate\Contracts\Filesystem\Filesystem */ |
|
12
|
|
|
protected $disk; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
protected $backupDirectory; |
|
16
|
|
|
|
|
17
|
|
|
/** @var Exception */ |
|
18
|
|
|
protected $connectionError; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* BackupDestination constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param \Illuminate\Contracts\Filesystem\Filesystem $disk |
|
24
|
|
|
* @param string $backupName |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(Filesystem $disk, $backupName) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->disk = $disk; |
|
29
|
|
|
|
|
30
|
|
|
$this->backupName = preg_replace('/[^a-zA-Z0-9.]/', '-', $backupName); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getFilesystemType() |
|
37
|
|
|
{ |
|
38
|
|
|
$adapterClass = get_class($this->disk->getDriver()->getAdapter()); |
|
39
|
|
|
|
|
40
|
|
|
$filesystemType = last(explode('\\', $adapterClass)); |
|
41
|
|
|
|
|
42
|
|
|
return strtolower($filesystemType); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $filesystemName |
|
47
|
|
|
* @param string $backupName |
|
48
|
|
|
* |
|
49
|
|
|
* @return \Spatie\Backup\BackupDestination\BackupDestination |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function create($filesystemName, $backupName) |
|
52
|
|
|
{ |
|
53
|
|
|
$disk = app(Factory::class)->disk($filesystemName); |
|
54
|
|
|
|
|
55
|
|
|
return new static($disk, $backupName); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $file |
|
60
|
|
|
*/ |
|
61
|
|
|
public function write($file) |
|
62
|
|
|
{ |
|
63
|
|
|
$destination = $this->backupName.'/'.pathinfo($file, PATHINFO_BASENAME); |
|
64
|
|
|
|
|
65
|
|
|
$handle = fopen($file, 'r+'); |
|
66
|
|
|
|
|
67
|
|
|
$this->disk->getDriver()->writeStream($destination, $handle); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getBackupName() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->backupName; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return \Spatie\Backup\BackupDestination\BackupCollection |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getBackups() |
|
82
|
|
|
{ |
|
83
|
|
|
$files = $this->isReachable() ? $this->disk->allFiles($this->backupName) : []; |
|
84
|
|
|
|
|
85
|
|
|
return BackupCollection::createFromFiles( |
|
86
|
|
|
$this->disk, |
|
87
|
|
|
$files |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return \Exception |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getConnectionError() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->connectionError; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
|
|
public function isReachable() |
|
103
|
|
|
{ |
|
104
|
|
|
try { |
|
105
|
|
|
$this->disk->allFiles($this->backupName); |
|
106
|
|
|
|
|
107
|
|
|
return true; |
|
108
|
|
|
} catch (Exception $exception) { |
|
109
|
|
|
$this->connectionError = $exception; |
|
110
|
|
|
|
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Return the used storage in bytes. |
|
117
|
|
|
* |
|
118
|
|
|
* @return int |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getUsedStorage() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->getBackups()->size(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return \Spatie\Backup\BackupDestination\Backup|null |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getNewestBackup() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->getBackups()->newest(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param \Carbon\Carbon $date |
|
135
|
|
|
* |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
|
|
public function isNewestBackupOlderThan($date) |
|
139
|
|
|
{ |
|
140
|
|
|
$newestBackup = $this->getNewestBackup(); |
|
141
|
|
|
|
|
142
|
|
|
if (is_null($newestBackup)) { |
|
143
|
|
|
return true; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $newestBackup->date()->gt($date); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: