1
|
|
|
<?php namespace Laravel\Installer\Console; |
|
|
|
|
2
|
|
|
|
3
|
|
|
ini_set('memory_limit', '1G'); |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Event\ProgressEvent; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Process\Process; |
14
|
|
|
use ZipArchive; |
15
|
|
|
|
16
|
|
|
class NewCommand extends Command { |
17
|
|
|
|
18
|
|
|
private $input; |
19
|
|
|
|
20
|
|
|
private $output; |
21
|
|
|
|
22
|
|
|
private $directory; |
23
|
|
|
|
24
|
|
|
private $zipFile; |
25
|
|
|
|
26
|
|
|
private $progress = 0; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Configure the command options. |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
protected function configure() |
34
|
|
|
{ |
35
|
|
|
$this->setName('new') |
36
|
|
|
->setDescription('Create a new Laravel application.') |
37
|
|
|
->addArgument('name', InputArgument::REQUIRED) |
38
|
|
|
->addOption('slim', null, InputOption::VALUE_NONE) |
39
|
|
|
->addOption('force', null, InputOption::VALUE_NONE) |
40
|
|
|
->addOption('laravelOnly', null, InputOption::VALUE_NONE) |
41
|
|
|
->addOption('lumen', null, InputOption::VALUE_NONE) |
42
|
|
|
->addOption('lumenOnly', null, InputOption::VALUE_NONE); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Execute the command. |
47
|
|
|
* |
48
|
|
|
* @param InputInterface $input |
49
|
|
|
* @param OutputInterface $output |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
54
|
|
|
{ |
55
|
|
|
$this->input = $input; |
56
|
|
|
$this->output = $output; |
57
|
|
|
$this->directory = getcwd() . '/' . $input->getArgument('name'); |
58
|
|
|
$this->zipFile = $this->makeFilename(); |
59
|
|
|
|
60
|
|
|
$output->writeln('<info>Crafting NukaCode application...</info>'); |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
$this->verifyApplicationDoesntExist(); |
64
|
|
|
|
65
|
|
|
$this->download(); |
66
|
|
|
|
67
|
|
|
$this->extract(); |
68
|
|
|
|
69
|
|
|
// Lumen does not have composer post install scripts. |
70
|
|
|
if (!$this->input->getOption('lumen') && !$this->input->getOption('lumenOnly')) { |
71
|
|
|
$this->runComposerCommands(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$output->writeln('<comment>Application ready! Build something amazing.</comment>'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Verify that the application does not already exist. |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
protected function verifyApplicationDoesntExist() |
83
|
|
|
{ |
84
|
|
|
$this->output->writeln('<info>Checking application path for existing site...</info>'); |
85
|
|
|
|
86
|
|
|
if (is_dir($this->directory)) { |
87
|
|
|
$this->output->writeln('<error>Application already exists!</error>'); |
88
|
|
|
|
89
|
|
|
exit(1); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->output->writeln('<info>Check complete...</info>'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Download the temporary Zip to the given file. |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
protected function download() |
101
|
|
|
{ |
102
|
|
|
$buildUrl = $this->getBuildFileLocation(); |
103
|
|
|
|
104
|
|
|
if ($this->input->getOption('force')) { |
105
|
|
|
$this->output->writeln('<info>--force command given. Deleting old build files...</info>'); |
106
|
|
|
|
107
|
|
|
$this->cleanUp(); |
108
|
|
|
|
109
|
|
|
$this->output->writeln('<info>complete...</info>'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($this->checkIfServerHasNewerBuild()) { |
113
|
|
|
$this->cleanUp(); |
114
|
|
|
$this->downloadFileWithProgressBar($buildUrl); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Extract the zip file into the given directory. |
122
|
|
|
* |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
protected function extract() |
127
|
|
|
{ |
128
|
|
|
$this->output->writeln('<info>Extracting files...</info>'); |
129
|
|
|
|
130
|
|
|
$archive = new ZipArchive; |
131
|
|
|
|
132
|
|
|
$archive->open($this->zipFile); |
133
|
|
|
|
134
|
|
|
$archive->extractTo($this->directory); |
135
|
|
|
|
136
|
|
|
$archive->close(); |
137
|
|
|
|
138
|
|
|
$this->output->writeln('<info>Extracting complete...</info>'); |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Run post install composer commands |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
protected function runComposerCommands() |
149
|
|
|
{ |
150
|
|
|
$this->output->writeln('<info>Running post install scripts...</info>'); |
151
|
|
|
|
152
|
|
|
$composer = $this->findComposer(); |
153
|
|
|
|
154
|
|
|
$commands = [ |
155
|
|
|
$composer . ' run-script post-install-cmd', |
156
|
|
|
$composer . ' run-script post-create-project-cmd', |
157
|
|
|
]; |
158
|
|
|
|
159
|
|
|
$process = new Process(implode(' && ', $commands), $this->directory, null, null, null); |
160
|
|
|
|
161
|
|
|
$process->run(function ($type, $line) { |
162
|
|
|
$this->output->write($line); |
163
|
|
|
}); |
164
|
|
|
|
165
|
|
|
$this->output->writeln('<info>Scripts complete...</info>'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Clean-up the Zip file. |
170
|
|
|
* |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
|
|
protected function cleanUp() |
174
|
|
|
{ |
175
|
|
|
@chmod($this->zipFile, 0777); |
176
|
|
|
@unlink($this->zipFile); |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get the composer command for the environment. |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
protected function findComposer() |
187
|
|
|
{ |
188
|
|
|
if (file_exists(getcwd() . '/composer.phar')) { |
189
|
|
|
return '"' . PHP_BINARY . '" composer.phar'; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return 'composer'; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Generate a random temporary filename. |
197
|
|
|
* |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
protected function makeFilename() |
201
|
|
|
{ |
202
|
|
|
if ($this->input->getOption('lumen')) { |
203
|
|
|
return dirname(__FILE__) . '/lumen.zip'; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if ($this->input->getOption('lumenOnly')) { |
207
|
|
|
return dirname(__FILE__) . '/lumen_only.zip'; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if ($this->input->getOption('laravelOnly')) { |
211
|
|
|
return dirname(__FILE__) . '/laravel_only.zip'; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if ($this->input->getOption('slim')) { |
215
|
|
|
return dirname(__FILE__) . '/laravel_slim.zip'; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return dirname(__FILE__) . '/laravel_full.zip'; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get the build file location based on the flags passed in. |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
protected function getBuildFileLocation() |
227
|
|
|
{ |
228
|
|
|
if ($this->input->getOption('lumen')) { |
229
|
|
|
return 'http://builds.nukacode.com/lumen/latest.zip'; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if ($this->input->getOption('lumenOnly')) { |
233
|
|
|
return 'http://cabinet.laravel.com/latest_lumen.zip'; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if ($this->input->getOption('laravelOnly')) { |
237
|
|
|
return 'http://cabinet.laravel.com/latest.zip'; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if ($this->input->getOption('slim')) { |
241
|
|
|
return 'http://builds.nukacode.com/slim/latest.zip'; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return 'http://builds.nukacode.com/full/latest.zip'; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Download the nukacode build files and display progress bar. |
249
|
|
|
* |
250
|
|
|
* @param $buildUrl |
251
|
|
|
*/ |
252
|
|
View Code Duplication |
protected function downloadFileWithProgressBar($buildUrl) |
|
|
|
|
253
|
|
|
{ |
254
|
|
|
$this->output->writeln('<info>Begin file download...</info>'); |
255
|
|
|
|
256
|
|
|
$progressBar = new ProgressBar($this->output, 100); |
257
|
|
|
$progressBar->start(); |
258
|
|
|
|
259
|
|
|
$client = new Client(); |
260
|
|
|
$request = $client->createRequest('GET', $buildUrl); |
261
|
|
|
$request->getEmitter()->on('progress', function (ProgressEvent $e) use ($progressBar) { |
262
|
|
|
if ($e->downloaded > 0) { |
263
|
|
|
$localProgress = floor(($e->downloaded / $e->downloadSize * 100)); |
264
|
|
|
|
265
|
|
|
if ($localProgress != $this->progress) { |
266
|
|
|
$this->progress = (integer)$localProgress; |
267
|
|
|
$progressBar->advance(); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
}); |
271
|
|
|
|
272
|
|
|
$response = $client->send($request); |
273
|
|
|
|
274
|
|
|
$progressBar->finish(); |
275
|
|
|
|
276
|
|
|
file_put_contents($this->zipFile, $response->getBody()); |
277
|
|
|
|
278
|
|
|
$this->output->writeln("\n<info>File download complete...</info>"); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Check if the server has a newer version of the nukacode build. |
283
|
|
|
* |
284
|
|
|
* @return bool |
285
|
|
|
*/ |
286
|
|
|
protected function checkIfServerHasNewerBuild() |
287
|
|
|
{ |
288
|
|
|
if (file_exists($this->zipFile)) { |
289
|
|
|
$client = new Client(); |
290
|
|
|
$response = $client->get('http://builds.nukacode.com/files.php'); |
291
|
|
|
|
292
|
|
|
// The downloaded copy is the same as the one on the server. |
293
|
|
|
if (in_array(md5_file($this->zipFile), $response->json())) { |
294
|
|
|
return false; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
return true; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.