Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Extensions/Backups/BackupManager.php
7460 views
1
<?php
2
3
namespace Pterodactyl\Extensions\Backups;
4
5
use Closure;
6
use Aws\S3\S3Client;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Str;
9
use Webmozart\Assert\Assert;
10
use Illuminate\Foundation\Application;
11
use League\Flysystem\FilesystemAdapter;
12
use Pterodactyl\Extensions\Filesystem\S3Filesystem;
13
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
14
use Illuminate\Contracts\Config\Repository as ConfigRepository;
15
16
class BackupManager
17
{
18
protected ConfigRepository $config;
19
20
/**
21
* The array of resolved backup drivers.
22
*/
23
protected array $adapters = [];
24
25
/**
26
* The registered custom driver creators.
27
*/
28
protected array $customCreators;
29
30
/**
31
* BackupManager constructor.
32
*/
33
public function __construct(protected Application $app)
34
{
35
$this->config = $app->make(ConfigRepository::class);
36
}
37
38
/**
39
* Returns a backup adapter instance.
40
*/
41
public function adapter(?string $name = null): FilesystemAdapter
42
{
43
return $this->get($name ?: $this->getDefaultAdapter());
44
}
45
46
/**
47
* Set the given backup adapter instance.
48
*/
49
public function set(string $name, FilesystemAdapter $disk): self
50
{
51
$this->adapters[$name] = $disk;
52
53
return $this;
54
}
55
56
/**
57
* Gets a backup adapter.
58
*/
59
protected function get(string $name): FilesystemAdapter
60
{
61
return $this->adapters[$name] = $this->resolve($name);
62
}
63
64
/**
65
* Resolve the given backup disk.
66
*/
67
protected function resolve(string $name): FilesystemAdapter
68
{
69
$config = $this->getConfig($name);
70
71
if (empty($config['adapter'])) {
72
throw new \InvalidArgumentException("Backup disk [$name] does not have a configured adapter.");
73
}
74
75
$adapter = $config['adapter'];
76
77
if (isset($this->customCreators[$name])) {
78
return $this->callCustomCreator($config);
79
}
80
81
$adapterMethod = 'create' . Str::studly($adapter) . 'Adapter';
82
if (method_exists($this, $adapterMethod)) {
83
$instance = $this->{$adapterMethod}($config);
84
85
Assert::isInstanceOf($instance, FilesystemAdapter::class);
86
87
return $instance;
88
}
89
90
throw new \InvalidArgumentException("Adapter [$adapter] is not supported.");
91
}
92
93
/**
94
* Calls a custom creator for a given adapter type.
95
*/
96
protected function callCustomCreator(array $config): mixed
97
{
98
return $this->customCreators[$config['adapter']]($this->app, $config);
99
}
100
101
/**
102
* Creates a new Wings adapter.
103
*/
104
public function createWingsAdapter(array $config): FilesystemAdapter
105
{
106
return new InMemoryFilesystemAdapter();
107
}
108
109
/**
110
* Creates a new S3 adapter.
111
*/
112
public function createS3Adapter(array $config): FilesystemAdapter
113
{
114
$config['version'] = 'latest';
115
116
if (!empty($config['key']) && !empty($config['secret'])) {
117
$config['credentials'] = Arr::only($config, ['key', 'secret', 'token']);
118
}
119
120
$client = new S3Client($config);
121
122
return new S3Filesystem($client, $config['bucket'], $config['prefix'] ?? '', $config['options'] ?? []);
123
}
124
125
/**
126
* Returns the configuration associated with a given backup type.
127
*/
128
protected function getConfig(string $name): array
129
{
130
return $this->config->get("backups.disks.$name") ?: [];
131
}
132
133
/**
134
* Get the default backup driver name.
135
*/
136
public function getDefaultAdapter(): string
137
{
138
return $this->config->get('backups.default');
139
}
140
141
/**
142
* Set the default session driver name.
143
*/
144
public function setDefaultAdapter(string $name): void
145
{
146
$this->config->set('backups.default', $name);
147
}
148
149
/**
150
* Unset the given adapter instances.
151
*
152
* @param string|string[] $adapter
153
*/
154
public function forget(array|string $adapter): self
155
{
156
foreach ((array) $adapter as $adapterName) {
157
unset($this->adapters[$adapterName]);
158
}
159
160
return $this;
161
}
162
163
/**
164
* Register a custom adapter creator closure.
165
*/
166
public function extend(string $adapter, \Closure $callback): self
167
{
168
$this->customCreators[$adapter] = $callback;
169
170
return $this;
171
}
172
}
173
174