2020-04-26 23:07:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Extensions\Backups;
|
|
|
|
|
2020-05-04 03:49:09 +00:00
|
|
|
use Aws\S3\S3Client;
|
2020-04-26 23:07:36 +00:00
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Webmozart\Assert\Assert;
|
2020-12-06 20:53:55 +00:00
|
|
|
use Illuminate\Foundation\Application;
|
2022-10-14 16:59:20 +00:00
|
|
|
use League\Flysystem\FilesystemAdapter;
|
|
|
|
use Pterodactyl\Extensions\Filesystem\S3Filesystem;
|
|
|
|
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
|
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
2020-04-26 23:07:36 +00:00
|
|
|
|
|
|
|
class BackupManager
|
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
protected ConfigRepository $config;
|
2020-04-26 23:07:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The array of resolved backup drivers.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected array $adapters = [];
|
2020-04-26 23:07:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The registered custom driver creators.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected array $customCreators;
|
2020-04-26 23:07:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* BackupManager constructor.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(protected Application $app)
|
2020-04-26 23:07:36 +00:00
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
$this->config = $app->make(ConfigRepository::class);
|
2020-04-26 23:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a backup adapter instance.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function adapter(string $name = null): FilesystemAdapter
|
2020-04-26 23:07:36 +00:00
|
|
|
{
|
|
|
|
return $this->get($name ?: $this->getDefaultAdapter());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the given backup adapter instance.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function set(string $name, FilesystemAdapter $disk): self
|
2020-04-26 23:07:36 +00:00
|
|
|
{
|
|
|
|
$this->adapters[$name] = $disk;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a backup adapter.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function get(string $name): FilesystemAdapter
|
2020-04-26 23:07:36 +00:00
|
|
|
{
|
|
|
|
return $this->adapters[$name] = $this->resolve($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve the given backup disk.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function resolve(string $name): FilesystemAdapter
|
2020-04-26 23:07:36 +00:00
|
|
|
{
|
|
|
|
$config = $this->getConfig($name);
|
|
|
|
|
|
|
|
if (empty($config['adapter'])) {
|
2022-11-29 17:53:59 +00:00
|
|
|
throw new \InvalidArgumentException("Backup disk [$name] does not have a configured adapter.");
|
2020-04-26 23:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$adapter = $config['adapter'];
|
|
|
|
|
|
|
|
if (isset($this->customCreators[$name])) {
|
|
|
|
return $this->callCustomCreator($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
$adapterMethod = 'create' . Str::studly($adapter) . 'Adapter';
|
|
|
|
if (method_exists($this, $adapterMethod)) {
|
|
|
|
$instance = $this->{$adapterMethod}($config);
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
Assert::isInstanceOf($instance, FilesystemAdapter::class);
|
2020-04-26 23:07:36 +00:00
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
2022-11-29 17:53:59 +00:00
|
|
|
throw new \InvalidArgumentException("Adapter [$adapter] is not supported.");
|
2020-04-26 23:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls a custom creator for a given adapter type.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function callCustomCreator(array $config): mixed
|
2020-04-26 23:07:36 +00:00
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
return $this->customCreators[$config['adapter']]($this->app, $config);
|
2020-04-26 23:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* Creates a new Wings adapter.
|
2020-04-26 23:07:36 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function createWingsAdapter(array $config): FilesystemAdapter
|
2020-04-26 23:07:36 +00:00
|
|
|
{
|
2022-10-26 16:26:56 +00:00
|
|
|
return new InMemoryFilesystemAdapter();
|
2020-04-26 23:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new S3 adapter.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function createS3Adapter(array $config): FilesystemAdapter
|
2020-04-26 23:07:36 +00:00
|
|
|
{
|
|
|
|
$config['version'] = 'latest';
|
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!empty($config['key']) && !empty($config['secret'])) {
|
2020-04-26 23:07:36 +00:00
|
|
|
$config['credentials'] = Arr::only($config, ['key', 'secret', 'token']);
|
|
|
|
}
|
|
|
|
|
2020-05-04 03:49:09 +00:00
|
|
|
$client = new S3Client($config);
|
2020-04-26 23:07:36 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
return new S3Filesystem($client, $config['bucket'], $config['prefix'] ?? '', $config['options'] ?? []);
|
2020-04-26 23:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the configuration associated with a given backup type.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function getConfig(string $name): array
|
2020-04-26 23:07:36 +00:00
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
return $this->config->get("backups.disks.$name") ?: [];
|
2020-04-26 23:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default backup driver name.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function getDefaultAdapter(): string
|
2020-04-26 23:07:36 +00:00
|
|
|
{
|
|
|
|
return $this->config->get('backups.default');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the default session driver name.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function setDefaultAdapter(string $name): void
|
2020-04-26 23:07:36 +00:00
|
|
|
{
|
|
|
|
$this->config->set('backups.default', $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unset the given adapter instances.
|
|
|
|
*
|
|
|
|
* @param string|string[] $adapter
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function forget(array|string $adapter): self
|
2020-04-26 23:07:36 +00:00
|
|
|
{
|
|
|
|
foreach ((array) $adapter as $adapterName) {
|
2022-10-14 16:59:20 +00:00
|
|
|
unset($this->adapters[$adapterName]);
|
2020-04-26 23:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a custom adapter creator closure.
|
|
|
|
*/
|
2022-11-29 17:53:59 +00:00
|
|
|
public function extend(string $adapter, \Closure $callback): self
|
2020-04-26 23:07:36 +00:00
|
|
|
{
|
|
|
|
$this->customCreators[$adapter] = $callback;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|