From e4aad3329f38dc3f4cb4a7804842e2ad42c01979 Mon Sep 17 00:00:00 2001 From: Muhep Atasoy Date: Tue, 10 Dec 2019 22:23:36 +0300 Subject: [PATCH] Clean Command Added --- app/Commands/DataBackupCommand.php | 11 +++++++--- app/Exceptions/CleanException.php | 8 ++++++++ app/Utils/Builder.php | 32 ++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 app/Exceptions/CleanException.php diff --git a/app/Commands/DataBackupCommand.php b/app/Commands/DataBackupCommand.php index 4bfadfa..e3976e4 100644 --- a/app/Commands/DataBackupCommand.php +++ b/app/Commands/DataBackupCommand.php @@ -15,7 +15,7 @@ class DataBackupCommand extends Command * * @var string */ - protected $signature = 'backup:data {--dataFolder=} {--backupFolder=}'; + protected $signature = 'backup:data {--dataFolder=} {--backupFolder=} {--clean}'; /** * The description of the command. @@ -34,11 +34,16 @@ class DataBackupCommand extends Command { $backupFolder = $this->option('backupFolder'); $dataFolder = $this->option('dataFolder'); - $bm = new BackupManager(); + $clean = $this->option('clean'); $this->line("|-> Backing up data folder..."); + + $bm = new BackupManager(); $builder = new DataBuilder(); - $builder->setDataFolder($dataFolder)->setBackupFolder($backupFolder); + $builder->setDataFolder($dataFolder) + ->setBackupFolder($backupFolder) + ->setClean($clean); $bm->setBuilder($builder)->execute(); + if ($bm->getShell()->getReturnValue() == 0) { $this->line(" |-> Data folder successfully backed up."); } else { diff --git a/app/Exceptions/CleanException.php b/app/Exceptions/CleanException.php new file mode 100644 index 0000000..a09ea35 --- /dev/null +++ b/app/Exceptions/CleanException.php @@ -0,0 +1,8 @@ +clean = false; + } + + public function __destruct() + { + try { + if ($this->clean) { + $files = glob($this->backupFolder . '/*'); + foreach($files as $file){ + is_file($file) ? unlink($file) : null; + } + } + } catch (\Exception $exception) { + throw new CleanException($exception->getMessage()); + } + } /** * @param $backupFolder @@ -49,6 +73,14 @@ abstract class Builder return $this->containerName; } + /** + * @param bool $clean + */ + public function setClean(bool $clean): void + { + $this->clean = $clean; + } + /** * @return CommandBuilder */