1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
// use Doctrine\Common\Cache\Version; |
6
|
|
|
use Zip; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Illuminate\Support\Facades\Storage; |
9
|
|
|
use Illuminate\Support\Arr; |
10
|
|
|
|
11
|
|
|
use PragmaRX\Version\Package\Version; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class updateRun extends Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The name and signature of the console command. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $signature = 'tb-update:run'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The console command description. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $description = 'Update the Tech Bench to a newer version'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a new command instance. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
2 |
|
public function __construct() |
36
|
|
|
{ |
37
|
2 |
|
parent::__construct(); |
38
|
2 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Execute the console command. |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function handle() |
46
|
|
|
{ |
47
|
|
|
$this->line(''); |
48
|
|
|
// Select which update file to use |
49
|
|
|
$updateFile = $this->checkForUpdate(); |
50
|
|
|
if($updateFile) |
51
|
|
|
{ |
52
|
|
|
// Open up the file and verify it is at least the same version as the current setup |
53
|
|
|
$valid = $this->openUpdate($updateFile); |
|
|
|
|
54
|
|
|
|
55
|
|
|
|
56
|
|
|
|
57
|
|
|
|
58
|
|
|
|
59
|
|
|
|
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
|
65
|
|
|
|
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Check to see if any backups are in the backup folder |
75
|
|
|
private function checkForUpdate() |
76
|
|
|
{ |
77
|
|
|
$updateList = []; |
78
|
|
|
$updates = Storage::disk('staging')->files('updates'); |
79
|
|
|
|
80
|
|
|
// Cycle through each file in the update directory to see if they are update files |
81
|
|
|
foreach($updates as $update) |
82
|
|
|
{ |
83
|
|
|
$baseName = explode('/', $update)[1]; |
84
|
|
|
|
85
|
|
|
// Verify the file is in the .zip format |
86
|
|
|
$fileParts = pathinfo($baseName); |
87
|
|
|
if($fileParts['extension'] == 'zip') |
88
|
|
|
{ |
89
|
|
|
// Verify this is actually an update file |
90
|
|
|
$zip = Zip::open(config('filesystems.disks.staging.root'). |
91
|
|
|
DIRECTORY_SEPARATOR.'updates'.DIRECTORY_SEPARATOR.$baseName); |
92
|
|
|
$files = $zip->listFiles(); |
93
|
|
|
if(in_array($fileParts['filename'].'/config/version.yml', $files)) |
94
|
|
|
{ |
95
|
|
|
$updateList[] = $baseName; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (empty($updateList)) { |
101
|
|
|
$this->error('No updates have been loaded to the system'); |
102
|
|
|
$this->error('Please upload update package to the Storage/Staging/Updates folder'); |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Determine if there is more than one update that can be applied |
107
|
|
|
if(count($updateList) > 1) |
108
|
|
|
{ |
109
|
|
|
$this->line(''); |
110
|
|
|
|
111
|
|
|
$anticipate = []; |
112
|
|
|
foreach($updateList as $key => $up) |
113
|
|
|
{ |
114
|
|
|
$opt = $key+1; |
115
|
|
|
$anticipate[$opt] = $up; |
116
|
|
|
$this->line('['.$opt.'] '.$up); |
117
|
|
|
} |
118
|
|
|
$updateFile = $this->choice('Please select which update you would like to load', $anticipate); |
119
|
|
|
} |
120
|
|
|
else |
121
|
|
|
{ |
122
|
|
|
$updateFile = $updateList[0]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $updateFile; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Unzip the update and verify it is a newer or the same version |
129
|
|
|
private function openUpdate($file) |
130
|
|
|
{ |
131
|
|
|
$fileParts = pathinfo($file); |
132
|
|
|
$folder = $fileParts['filename']; |
133
|
|
|
|
134
|
|
|
$zip = Zip::open(config('filesystems.disks.staging.root') . |
135
|
|
|
DIRECTORY_SEPARATOR . 'updates' . DIRECTORY_SEPARATOR . $file); |
136
|
|
|
|
137
|
|
|
$zip->extract(config('filesystems.disks.staging.root') . |
138
|
|
|
DIRECTORY_SEPARATOR . 'updates' . DIRECTORY_SEPARATOR .'tmp'); |
139
|
|
|
|
140
|
|
|
$verFile = fopen(config('filesystems.disks.staging.root') . |
141
|
|
|
DIRECTORY_SEPARATOR.'updates'.DIRECTORY_SEPARATOR.'tmp'. |
142
|
|
|
DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR.'config'. |
143
|
|
|
DIRECTORY_SEPARATOR.'version.yml', 'r'); |
144
|
|
|
|
145
|
|
|
$verData = []; |
146
|
|
|
$i = 0; |
147
|
|
|
while(!feof($verFile)) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$line = fgets($verFile); |
|
|
|
|
150
|
|
|
$data = explode(':', $line); |
151
|
|
|
|
152
|
|
|
if(($data[0] === ' major' || $data[0] === ' minor' || $data[0] === ' patch') && $i < 3) |
153
|
|
|
{ |
154
|
|
|
$verData[trim($data[0])] = trim($data[1]); |
155
|
|
|
$i++; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$curVersion = new \PragmaRX\Version\Package\Version(); |
160
|
|
|
|
161
|
|
|
$valid = false; |
162
|
|
|
if($verData['major'] > $curVersion->major()) |
163
|
|
|
{ |
164
|
|
|
$valid = true; |
165
|
|
|
} |
166
|
|
|
else if($verData['minor'] > $curVersion->minor()) |
167
|
|
|
{ |
168
|
|
|
$valid = true; |
169
|
|
|
} |
170
|
|
|
else if($verData['patch'] >= $curVersion->patch()) |
171
|
|
|
{ |
172
|
|
|
$valid = true; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $valid; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|