1
|
|
|
<?php namespace Laravel\Installer\Console; |
2
|
|
|
|
3
|
|
|
use GuzzleHttp\Client; |
4
|
|
|
use GuzzleHttp\Event\ProgressEvent; |
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
|
10
|
|
|
class DownloadCommand extends Command { |
11
|
|
|
|
12
|
|
|
private $output; |
13
|
|
|
|
14
|
|
|
private $progress = 0; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Configure the command options. |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
protected function configure() |
22
|
|
|
{ |
23
|
|
|
$this->setName('download') |
24
|
|
|
->setDescription('Download laravel builds.'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Execute the command. |
29
|
|
|
* |
30
|
|
|
* @param InputInterface $input |
31
|
|
|
* @param OutputInterface $output |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
36
|
|
|
{ |
37
|
|
|
$this->output = $output; |
38
|
|
|
|
39
|
|
|
$output->writeln('<info>Downloading NukaCode application data...</info>'); |
40
|
|
|
|
41
|
|
|
$this->download(); |
42
|
|
|
|
43
|
|
|
$output->writeln('<comment>Application download complete! Ready to build something amazingly FAST!</comment>'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Download the temporary Zip to the given file. |
48
|
|
|
* |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
protected function download() |
52
|
|
|
{ |
53
|
|
|
$md5HashLocation = 'http://builds.nukacode.com/files.php'; |
54
|
|
|
$slimZipLocation = dirname(__FILE__) . '/laravel_slim.zip'; |
55
|
|
|
$fullZipLocation = dirname(__FILE__) . '/laravel_full.zip'; |
56
|
|
|
$lumenZipLocation = dirname(__FILE__) . '/lumen_full.zip'; |
57
|
|
|
|
58
|
|
|
if ($this->checkIfServerHasNewerBuild($md5HashLocation, $slimZipLocation)) { |
59
|
|
|
$this->cleanUp($slimZipLocation); |
60
|
|
|
$this->downloadFileWithProgressBar('http://builds.nukacode.com/slim/latest.zip', $slimZipLocation); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($this->checkIfServerHasNewerBuild($md5HashLocation, $fullZipLocation)) { |
64
|
|
|
$this->cleanUp($fullZipLocation); |
65
|
|
|
$this->downloadFileWithProgressBar('http://builds.nukacode.com/full/latest.zip', $fullZipLocation); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($this->checkIfServerHasNewerBuild($md5HashLocation, $lumenZipLocation)) { |
69
|
|
|
$this->cleanUp($lumenZipLocation); |
70
|
|
|
$this->downloadFileWithProgressBar('http://builds.nukacode.com/lumen/latest.zip', $lumenZipLocation); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Clean-up the Zip file. |
78
|
|
|
* |
79
|
|
|
* @param the zip file to remove |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
protected function cleanUp($zipFile) |
84
|
|
|
{ |
85
|
|
|
@chmod($zipFile, 0777); |
86
|
|
|
@unlink($zipFile); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Download the nukacode build files and display progress bar. |
94
|
|
|
* |
95
|
|
|
* @param $buildUrl |
96
|
|
|
* @param $zipFile |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
protected function downloadFileWithProgressBar($buildUrl, $zipFile) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$this->output->writeln('<info>Begin file download...</info>'); |
101
|
|
|
|
102
|
|
|
$progressBar = new ProgressBar($this->output, 100); |
103
|
|
|
$progressBar->start(); |
104
|
|
|
|
105
|
|
|
$client = new Client(); |
106
|
|
|
$request = $client->createRequest('GET', $buildUrl); |
107
|
|
|
$request->getEmitter()->on('progress', function (ProgressEvent $e) use ($progressBar) { |
108
|
|
|
if ($e->downloaded > 0) { |
109
|
|
|
$localProgress = floor(($e->downloaded / $e->downloadSize * 100)); |
110
|
|
|
|
111
|
|
|
if ($localProgress != $this->progress) { |
112
|
|
|
$this->progress = (integer)$localProgress; |
113
|
|
|
$progressBar->advance(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
}); |
117
|
|
|
|
118
|
|
|
$response = $client->send($request); |
119
|
|
|
|
120
|
|
|
$progressBar->finish(); |
121
|
|
|
|
122
|
|
|
file_put_contents($zipFile, $response->getBody()); |
123
|
|
|
|
124
|
|
|
$this->output->writeln("\n<info>File download complete...</info>"); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Check if the server has a newer version of the nukacode build. |
129
|
|
|
* |
130
|
|
|
* @param string $md5CheckPath The url to check for file md5 hash |
131
|
|
|
* @param string $zipFile The zip file we are checking |
132
|
|
|
* |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
protected function checkIfServerHasNewerBuild($md5CheckPath, $zipFile) |
136
|
|
|
{ |
137
|
|
|
if (file_exists($zipFile)) { |
138
|
|
|
$client = new Client(); |
139
|
|
|
$response = $client->get($md5CheckPath); |
140
|
|
|
|
141
|
|
|
// The downloaded copy is the same as the one on the server. |
142
|
|
|
if (in_array(md5_file($zipFile), $response->json())) { |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return true; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.