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
|
|
|
//end __construct() |
31
|
|
|
|
32
|
|
|
public function fire() |
33
|
|
|
{ |
34
|
|
|
$succes = $this->handleOptions(); |
|
|
|
|
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
|
|
|
$disk = $this->getDiskName(); |
44
|
|
|
$this->blockMessage('Success!', "Everything uploaded to $disk filesystem!"); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
//end fire() |
48
|
|
|
|
49
|
|
|
protected function getOptions() |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
|
|
[ |
53
|
|
|
'migrations', |
54
|
|
|
'm', |
55
|
|
|
InputOption::VALUE_NONE, |
56
|
|
|
'Upload the migrations to a storage.', |
57
|
|
|
null, |
58
|
|
|
], |
59
|
|
|
[ |
60
|
|
|
'seeds', |
61
|
|
|
's', |
62
|
|
|
InputOption::VALUE_NONE, |
63
|
|
|
'Upload the seeds to the remote host.', |
64
|
|
|
null, |
65
|
|
|
], |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
//end getOptions() |
70
|
|
|
|
71
|
|
|
protected function handleOptions() |
72
|
|
|
{ |
73
|
|
|
$options = $this->option(); |
74
|
|
|
foreach ($options as $key => $value) { |
75
|
|
|
if ($value) { |
76
|
|
|
$this->upload($key); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function upload($what) |
82
|
|
|
{ |
83
|
|
|
$localPath = Config::get('db-exporter.export_path.'.$what); |
84
|
|
|
$dir = scandir($localPath); |
85
|
|
|
$remotePath = Config::get('db-exporter.remote.'.$what); |
86
|
|
|
$this->line("\n"); |
87
|
|
|
$this->info(ucfirst($what)); |
88
|
|
|
// Reset file coounter |
89
|
|
|
static::$filesCount = 0; |
90
|
|
|
// Prepare the progress bar |
91
|
|
|
array_walk($dir, function ($file) { |
92
|
|
|
if ($this->ignoredFile($file)) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
++static::$filesCount; |
96
|
|
|
}); |
97
|
|
|
$progress = $this->output->createProgressBar(static::$filesCount); |
98
|
|
|
foreach ($dir as $file) { |
99
|
|
|
if ($this->ignoredFile($file)) { |
100
|
|
|
$this->comment("---> ignoring $file "); |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Capture the uploaded files for displaying later |
105
|
|
|
$this->uploadedFiles[$what][] = $remotePath.$file; |
106
|
|
|
|
107
|
|
|
// Copy the files |
108
|
|
|
Storage::disk($this->getDiskName())->put( |
109
|
|
|
$remotePath.$file, |
110
|
|
|
$localPath.'/'.$file |
111
|
|
|
); |
112
|
|
|
$progress->advance(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$progress->finish(); |
116
|
|
|
|
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
//end upload() |
121
|
|
|
|
122
|
|
|
protected function getDiskName() |
123
|
|
|
{ |
124
|
|
|
// For now static from he config file. |
125
|
|
|
return Config::get('db-exporter.backup.disk'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Determine if a file should be ignored. |
130
|
|
|
* |
131
|
|
|
* @param string $file filename |
132
|
|
|
* |
133
|
|
|
* @return bool |
134
|
|
|
**/ |
135
|
|
|
protected function ignoredFile($file) |
136
|
|
|
{ |
137
|
|
|
if (in_array($file, $this->ignoredFiles)) { |
138
|
|
|
return true; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
//end getDiskName() |
145
|
|
|
}//end class |
146
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.