|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Elimuswift\DbExporter\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Config; |
|
6
|
|
|
use Storage; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
|
|
9
|
|
|
class CopyToRemoteCommand extends GeneratorCommand |
|
10
|
|
|
{ |
|
11
|
|
|
protected $name = 'db-exporter:backup'; |
|
12
|
|
|
|
|
13
|
|
|
protected $description = 'Command to copy the migrations and/or the seeds to a remote host.'; |
|
14
|
|
|
|
|
15
|
|
|
protected $ignoredFiles = [ |
|
16
|
|
|
'..', |
|
17
|
|
|
'.', |
|
18
|
|
|
'.gitkeep', |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
protected $uploadedFiles; |
|
22
|
|
|
|
|
23
|
|
|
protected static $filesCount; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
parent::__construct(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function handle() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->handleOptions(); |
|
33
|
|
|
foreach ($this->uploadedFiles as $type => $files) { |
|
34
|
|
|
$this->line("\n"); |
|
35
|
|
|
$this->info(ucfirst($type)); |
|
36
|
|
|
foreach ($files as $file) { |
|
37
|
|
|
$this->sectionMessage($type, $file.' uploaded.'); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$disk = $this->getDiskName(); |
|
42
|
|
|
$this->blockMessage('Success!', "Everything uploaded to $disk filesystem!"); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
//end fire() |
|
46
|
|
|
|
|
47
|
|
|
protected function getOptions() |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
[ |
|
51
|
|
|
'migrations', |
|
52
|
|
|
'm', |
|
53
|
|
|
InputOption::VALUE_NONE, |
|
54
|
|
|
'Upload the migrations to a storage.', |
|
55
|
|
|
null, |
|
56
|
|
|
], |
|
57
|
|
|
[ |
|
58
|
|
|
'seeds', |
|
59
|
|
|
's', |
|
60
|
|
|
InputOption::VALUE_NONE, |
|
61
|
|
|
'Upload the seeds to the remote host.', |
|
62
|
|
|
null, |
|
63
|
|
|
], |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
//end getOptions() |
|
68
|
|
|
|
|
69
|
|
|
protected function handleOptions() |
|
70
|
|
|
{ |
|
71
|
|
|
$options = $this->option(); |
|
72
|
|
|
foreach ($options as $key => $value) { |
|
|
|
|
|
|
73
|
|
|
if ($value) { |
|
74
|
|
|
$this->upload($key); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function upload($what) |
|
80
|
|
|
{ |
|
81
|
|
|
$localPath = Config::get('db-exporter.export_path.'.$what); |
|
82
|
|
|
$dir = scandir($localPath); |
|
83
|
|
|
$remotePath = Config::get('db-exporter.remote.'.$what); |
|
84
|
|
|
$this->line("\n"); |
|
85
|
|
|
$this->info(ucfirst($what)); |
|
86
|
|
|
// Reset file coounter |
|
87
|
|
|
static::$filesCount = 0; |
|
88
|
|
|
// Prepare the progress bar |
|
89
|
|
|
array_walk($dir, function ($file) { |
|
90
|
|
|
if ($this->ignoredFile($file)) { |
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
++static::$filesCount; |
|
94
|
|
|
}); |
|
95
|
|
|
$progress = $this->output->createProgressBar(static::$filesCount); |
|
96
|
|
|
foreach ($dir as $file) { |
|
97
|
|
|
if ($this->ignoredFile($file)) { |
|
98
|
|
|
$this->comment("---> ignoring $file "); |
|
99
|
|
|
continue; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// Capture the uploaded files for displaying later |
|
103
|
|
|
$this->uploadedFiles[$what][] = $remotePath.$file; |
|
104
|
|
|
|
|
105
|
|
|
// Copy the files |
|
106
|
|
|
Storage::disk($this->getDiskName())->put( |
|
107
|
|
|
$remotePath.$file, |
|
108
|
|
|
$localPath.'/'.$file |
|
109
|
|
|
); |
|
110
|
|
|
$progress->advance(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$progress->finish(); |
|
114
|
|
|
|
|
115
|
|
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
//end upload() |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return string|null |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function getDiskName() |
|
124
|
|
|
{ |
|
125
|
|
|
// For now static from he config file. |
|
126
|
|
|
return Config::get('db-exporter.backup.disk'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Determine if a file should be ignored. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $file filename |
|
133
|
|
|
* |
|
134
|
|
|
* @return bool |
|
135
|
|
|
**/ |
|
136
|
|
|
protected function ignoredFile($file) |
|
137
|
|
|
{ |
|
138
|
|
|
if (in_array($file, $this->ignoredFiles)) { |
|
139
|
|
|
return true; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
//end getDiskName() |
|
146
|
|
|
}//end class |
|
147
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.