Path: blob/1.0-develop/app/Extensions/Backups/BackupManager.php
7460 views
<?php12namespace Pterodactyl\Extensions\Backups;34use Closure;5use Aws\S3\S3Client;6use Illuminate\Support\Arr;7use Illuminate\Support\Str;8use Webmozart\Assert\Assert;9use Illuminate\Foundation\Application;10use League\Flysystem\FilesystemAdapter;11use Pterodactyl\Extensions\Filesystem\S3Filesystem;12use League\Flysystem\InMemory\InMemoryFilesystemAdapter;13use Illuminate\Contracts\Config\Repository as ConfigRepository;1415class BackupManager16{17protected ConfigRepository $config;1819/**20* The array of resolved backup drivers.21*/22protected array $adapters = [];2324/**25* The registered custom driver creators.26*/27protected array $customCreators;2829/**30* BackupManager constructor.31*/32public function __construct(protected Application $app)33{34$this->config = $app->make(ConfigRepository::class);35}3637/**38* Returns a backup adapter instance.39*/40public function adapter(?string $name = null): FilesystemAdapter41{42return $this->get($name ?: $this->getDefaultAdapter());43}4445/**46* Set the given backup adapter instance.47*/48public function set(string $name, FilesystemAdapter $disk): self49{50$this->adapters[$name] = $disk;5152return $this;53}5455/**56* Gets a backup adapter.57*/58protected function get(string $name): FilesystemAdapter59{60return $this->adapters[$name] = $this->resolve($name);61}6263/**64* Resolve the given backup disk.65*/66protected function resolve(string $name): FilesystemAdapter67{68$config = $this->getConfig($name);6970if (empty($config['adapter'])) {71throw new \InvalidArgumentException("Backup disk [$name] does not have a configured adapter.");72}7374$adapter = $config['adapter'];7576if (isset($this->customCreators[$name])) {77return $this->callCustomCreator($config);78}7980$adapterMethod = 'create' . Str::studly($adapter) . 'Adapter';81if (method_exists($this, $adapterMethod)) {82$instance = $this->{$adapterMethod}($config);8384Assert::isInstanceOf($instance, FilesystemAdapter::class);8586return $instance;87}8889throw new \InvalidArgumentException("Adapter [$adapter] is not supported.");90}9192/**93* Calls a custom creator for a given adapter type.94*/95protected function callCustomCreator(array $config): mixed96{97return $this->customCreators[$config['adapter']]($this->app, $config);98}99100/**101* Creates a new Wings adapter.102*/103public function createWingsAdapter(array $config): FilesystemAdapter104{105return new InMemoryFilesystemAdapter();106}107108/**109* Creates a new S3 adapter.110*/111public function createS3Adapter(array $config): FilesystemAdapter112{113$config['version'] = 'latest';114115if (!empty($config['key']) && !empty($config['secret'])) {116$config['credentials'] = Arr::only($config, ['key', 'secret', 'token']);117}118119$client = new S3Client($config);120121return new S3Filesystem($client, $config['bucket'], $config['prefix'] ?? '', $config['options'] ?? []);122}123124/**125* Returns the configuration associated with a given backup type.126*/127protected function getConfig(string $name): array128{129return $this->config->get("backups.disks.$name") ?: [];130}131132/**133* Get the default backup driver name.134*/135public function getDefaultAdapter(): string136{137return $this->config->get('backups.default');138}139140/**141* Set the default session driver name.142*/143public function setDefaultAdapter(string $name): void144{145$this->config->set('backups.default', $name);146}147148/**149* Unset the given adapter instances.150*151* @param string|string[] $adapter152*/153public function forget(array|string $adapter): self154{155foreach ((array) $adapter as $adapterName) {156unset($this->adapters[$adapterName]);157}158159return $this;160}161162/**163* Register a custom adapter creator closure.164*/165public function extend(string $adapter, \Closure $callback): self166{167$this->customCreators[$adapter] = $callback;168169return $this;170}171}172173174