1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cornford\Backup\Commands; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
|
9
|
|
|
class BackupCommandExport extends BackupCommandAbstract |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Name. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $name = 'db:export'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Export the default database to `app/storage/backup`'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Handle. |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function handle(): void |
32
|
|
|
{ |
33
|
|
|
$backupInstance = $this->getBackupInstance($this->input->getOption('database')); |
34
|
|
|
$backupInstance->setEnabled(true); |
35
|
|
|
|
36
|
|
View Code Duplication |
if ($this->input->getOption('path') !== null) { |
|
|
|
|
37
|
|
|
$backupInstance->setPath($this->input->getOption('path')); |
38
|
|
|
} else { |
39
|
|
|
$backupInstance->setPath('app/storage/backup/'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($this->input->getOption('compress') !== null) { |
43
|
|
|
$backupInstance->setCompress($this->input->getOption('compress') != 'false' ?: false); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($this->argument('filename') !== null) { |
47
|
|
|
$backupInstance->setFilename($this->argument('filename')); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
if (!$backupInstance->export()) { |
52
|
|
|
throw new Exception(); |
53
|
|
|
} |
54
|
|
|
} catch (Exception $exception) { |
55
|
|
|
$this->error('An error occurred exporting the database.'); |
56
|
|
|
|
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->info('Database exported to file: "' . $backupInstance->getWorkingFilepath() . '".'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the console command arguments. |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
protected function getArguments(): array |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
|
|
['filename', InputArgument::OPTIONAL, 'Filename for the database export file.'] |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the console command options. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function getOptions(): array |
81
|
|
|
{ |
82
|
|
|
return [ |
83
|
|
|
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to backup'], |
84
|
|
|
['path', null, InputOption::VALUE_OPTIONAL, 'The database export path'], |
85
|
|
|
['compress', null, InputOption::VALUE_OPTIONAL, 'Compress the database export using gzip'], |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.