Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding default implementation of TemporaryDirectory #1846

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Adapters/TemporaryDirectoryAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Spatie\Backup\Adapters;

use Spatie\Backup\Contracts\TemporaryDirectory;
use Spatie\TemporaryDirectory\TemporaryDirectory as BaseTemporaryDirectory;

class TemporaryDirectoryAdapter extends BaseTemporaryDirectory implements TemporaryDirectory
{

}
4 changes: 4 additions & 0 deletions src/BackupServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;
use Spatie\Backup\Adapters\TemporaryDirectoryAdapter;
use Spatie\Backup\Commands\BackupCommand;
use Spatie\Backup\Commands\CleanupCommand;
use Spatie\Backup\Commands\ListCommand;
use Spatie\Backup\Commands\MonitorCommand;
use Spatie\Backup\Config\Config;
use Spatie\Backup\Contracts\TemporaryDirectory as TemporaryDirectoryContract;
use Spatie\Backup\Events\BackupZipWasCreated;
use Spatie\Backup\Helpers\ConsoleOutput;
use Spatie\Backup\Listeners\EncryptBackupArchive;
Expand All @@ -18,6 +20,7 @@
use Spatie\Backup\Tasks\Cleanup\CleanupStrategy;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Spatie\TemporaryDirectory\TemporaryDirectory;

class BackupServiceProvider extends PackageServiceProvider
{
Expand Down Expand Up @@ -49,6 +52,7 @@ public function packageRegistered(): void
$this->app->singleton(ConsoleOutput::class);

$this->app->bind(CleanupStrategy::class, config('backup.cleanup.strategy'));
$this->app->bind(TemporaryDirectoryContract::class, fn() => new TemporaryDirectoryAdapter(config('backup.temporary_directory') ?? storage_path('app/backup-temp')));

$this->registerDiscordChannel();

Expand Down
5 changes: 3 additions & 2 deletions src/Commands/BackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Illuminate\Contracts\Console\Isolatable;
use Spatie\Backup\Config\Config;
use Spatie\Backup\Contracts\TemporaryDirectory;
use Spatie\Backup\Events\BackupHasFailed;
use Spatie\Backup\Exceptions\BackupFailed;
use Spatie\Backup\Exceptions\InvalidCommand;
Expand All @@ -19,7 +20,7 @@ class BackupCommand extends BaseCommand implements Isolatable

protected $description = 'Run the backup.';

public function __construct(protected Config $config)
public function __construct(protected Config $config, protected TemporaryDirectory $temporaryDirectory)
{
parent::__construct();
}
Expand All @@ -37,7 +38,7 @@ public function handle(): int
try {
$this->guardAgainstInvalidOptions();

$backupJob = BackupJobFactory::createFromConfig($this->config);
$backupJob = BackupJobFactory::createFromConfig($this->config, $this->temporaryDirectory);

if ($this->option('only-db')) {
$backupJob->dontBackupFilesystem();
Expand Down
12 changes: 12 additions & 0 deletions src/Contracts/TemporaryDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Spatie\Backup\Contracts;

use Spatie\TemporaryDirectory\TemporaryDirectory as BaseTemporaryDirectory;

/**
* @mixin BaseTemporaryDirectory
*/
interface TemporaryDirectory
{
}
10 changes: 3 additions & 7 deletions src/Tasks/Backup/BackupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Collection;
use Spatie\Backup\BackupDestination\BackupDestination;
use Spatie\Backup\Config\Config;
use Spatie\Backup\Contracts\TemporaryDirectory;
use Spatie\Backup\Events\BackupManifestWasCreated;
use Spatie\Backup\Events\BackupWasSuccessful;
use Spatie\Backup\Events\BackupZipWasCreated;
Expand All @@ -20,7 +21,6 @@
use Spatie\DbDumper\Databases\Sqlite;
use Spatie\DbDumper\DbDumper;
use Spatie\SignalAwareCommand\Facades\Signal;
use Spatie\TemporaryDirectory\TemporaryDirectory;

class BackupJob
{
Expand All @@ -36,13 +36,11 @@ class BackupJob

protected string $filename;

protected TemporaryDirectory $temporaryDirectory;

protected bool $sendNotifications = true;

protected bool $signals = true;

public function __construct(protected Config $config)
public function __construct(protected Config $config, protected TemporaryDirectory $temporaryDirectory)
{
$this
->dontBackupFilesystem()
Expand Down Expand Up @@ -146,9 +144,7 @@ public function setBackupDestinations(Collection $backupDestinations): self
/** @throws Exception */
public function run(): void
{
$temporaryDirectoryPath = $this->config->backup->temporaryDirectory ?? storage_path('app/backup-temp');

$this->temporaryDirectory = (new TemporaryDirectory($temporaryDirectoryPath))
$this->temporaryDirectory
->name('temp')
->force()
->create()
Expand Down
5 changes: 3 additions & 2 deletions src/Tasks/Backup/BackupJobFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
use Spatie\Backup\BackupDestination\BackupDestinationFactory;
use Spatie\Backup\Config\Config;
use Spatie\Backup\Config\SourceFilesConfig;
use Spatie\Backup\Contracts\TemporaryDirectory;
use Spatie\DbDumper\DbDumper;

class BackupJobFactory
{
public static function createFromConfig(Config $config): BackupJob
public static function createFromConfig(Config $config, TemporaryDirectory $temporaryDirectory): BackupJob
{
return (new BackupJob($config))
return (new BackupJob($config, $temporaryDirectory))
->setFileSelection(static::createFileSelection($config->backup->source->files))
->setDbDumpers(static::createDbDumpers($config->backup->source->databases))
->setBackupDestinations(BackupDestinationFactory::createFromArray($config));
Expand Down