1
|
|
|
<?php |
2
|
|
|
namespace Elimuswift\DbExporter\Commands; |
3
|
|
|
|
4
|
|
|
use Config; |
5
|
|
|
use Storage; |
6
|
|
|
use Elimuswift\DbExporter\Server; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
|
10
|
|
|
class CopyToRemoteCommand extends GeneratorCommand |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
protected $name = 'db-exporter:backup'; |
14
|
|
|
|
15
|
|
|
protected $description = 'Command to copy the migrations and/or the seeds to a remote host.'; |
16
|
|
|
|
17
|
|
|
protected $ignoredFiles = ['..', '.', '.gitkeep']; |
18
|
|
|
|
19
|
|
|
protected $uploadedFiles; |
20
|
|
|
|
21
|
|
|
protected $commandOptions; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
parent::__construct(); |
27
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function fire() |
31
|
|
|
{ |
32
|
|
|
$succes = $this->handleOptions(); |
33
|
|
|
if ($succes) { |
34
|
|
|
// Inform what files have been uploaded |
35
|
|
|
foreach ($this->uploadedFiles as $type => $files) { |
36
|
|
|
$this->line("\n"); |
37
|
|
|
$this->info(ucfirst($type)); |
38
|
|
|
foreach ($files as $file) { |
39
|
|
|
$this->sectionMessage($type, $file . ' uploaded.'); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->blockMessage('Success!', 'Everything uploaded!'); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
protected function getOptions() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
['migrations', 'm', InputOption::VALUE_NONE, 'Upload the migrations to a storage.', null], |
52
|
|
|
['seeds', 's', InputOption::VALUE_NONE, 'Upload the seeds to the remote host.', null] |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function getRemoteName() |
57
|
|
|
{ |
58
|
|
|
// Use default production key |
59
|
|
|
if (!$this->argument('remote')) { |
60
|
|
|
return 'production'; |
61
|
|
|
} else { |
62
|
|
|
return $this->argument('remote'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function handleOptions() |
67
|
|
|
{ |
68
|
|
|
$options = $this->option(); |
69
|
|
|
switch ($options) { |
70
|
|
|
case (($options['seeds'] === true) && ($options['migrations'] === true)): |
71
|
|
|
if (!$this->upload('migrations')) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
return $this->upload('seeds'); |
75
|
|
|
|
76
|
|
|
case $options['migrations'] === true: |
77
|
|
|
$this->commandOptions = 'migrations'; |
78
|
|
|
return $this->upload('migrations'); |
79
|
|
|
|
80
|
|
|
case $options['seeds'] === true: |
81
|
|
|
$this->commandOptions = 'seeds'; |
82
|
|
|
return $this->upload('seeds'); |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function upload($what) |
88
|
|
|
{ |
89
|
|
|
$localPath = Config::get('db-exporter.export_path.'.$what); |
90
|
|
|
$dir = scandir($localPath); |
91
|
|
|
$remotePath = Config::get('db-exporter.remote.' . $what); |
92
|
|
|
$this->line("\n"); |
93
|
|
|
$this->info(ucfirst($what)); |
94
|
|
|
// Prepare the progress bar |
95
|
|
|
|
96
|
|
|
$filesCount = count($dir) - count($this->ignoredFiles); |
97
|
|
|
$progress = $this->output->createProgressBar($filesCount); |
98
|
|
View Code Duplication |
foreach ($dir as $file) { |
|
|
|
|
99
|
|
|
if (in_array($file, $this->ignoredFiles)) { |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Capture the uploaded files for displaying later |
104
|
|
|
$this->uploadedFiles[$what][] = $remotePath . $file; |
105
|
|
|
|
106
|
|
|
// Copy the files |
107
|
|
|
Storage::disk($this->getDiskName())->put( |
108
|
|
|
$remotePath . $file, |
109
|
|
|
$localPath . '/' . $file |
110
|
|
|
); |
111
|
|
|
$progress->advance(); |
112
|
|
|
} |
113
|
|
|
$progress->finish(); |
114
|
|
|
|
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
private function getDiskName() |
118
|
|
|
{ |
119
|
|
|
// For now static from he config file. |
120
|
|
|
return Config::get('db-exporter.remote.disk'); |
121
|
|
|
} |
122
|
|
|
} |
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.