-
Notifications
You must be signed in to change notification settings - Fork 258
/
NewCommand.php
228 lines (190 loc) · 6.33 KB
/
NewCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace Laravel\Installer\Console;
use ZipArchive;
use RuntimeException;
use GuzzleHttp\Client;
use Symfony\Component\Process\Process;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
class NewCommand extends Command
{
/**
* Configure the command options.
*
* @return void
*/
protected function configure()
{
$this
->setName('new')
->setDescription('Create a new Laravel application')
->addArgument('name', InputArgument::OPTIONAL)
->addOption('dev', null, InputOption::VALUE_NONE, 'Installs the latest "development" release')
->addOption('force', 'f', InputOption::VALUE_NONE, 'Forces install even if the directory already exists');
}
/**
* Execute the command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (! extension_loaded('zip')) {
throw new RuntimeException('The Zip PHP extension is not installed. Please install it and try again.');
}
$directory = ($input->getArgument('name')) ? getcwd().'/'.$input->getArgument('name') : getcwd();
if (! $input->getOption('force')) {
$this->verifyApplicationDoesntExist($directory);
}
$output->writeln('<info>Crafting application...</info>');
$version = $this->getVersion($input);
$this->download($zipFile = $this->makeFilename(), $version)
->extract($zipFile, $directory)
->prepareWritableDirectories($directory, $output)
->cleanUp($zipFile);
$composer = $this->findComposer();
$commands = [
$composer.' install --no-scripts',
$composer.' run-script post-root-package-install',
$composer.' run-script post-create-project-cmd',
$composer.' run-script post-autoload-dump',
];
if ($input->getOption('no-ansi')) {
$commands = array_map(function ($value) {
return $value.' --no-ansi';
}, $commands);
}
if ($input->getOption('quiet')) {
$commands = array_map(function ($value) {
return $value.' --quiet';
}, $commands);
}
$process = new Process(implode(' && ', $commands), $directory, null, null, null);
if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
$process->setTty(true);
}
$process->run(function ($type, $line) use ($output) {
$output->write($line);
});
$output->writeln('<comment>Application ready! Build something amazing.</comment>');
}
/**
* Verify that the application does not already exist.
*
* @param string $directory
* @return void
*/
protected function verifyApplicationDoesntExist($directory)
{
if ((is_dir($directory) || is_file($directory)) && $directory != getcwd()) {
throw new RuntimeException('Application already exists!');
}
}
/**
* Generate a random temporary filename.
*
* @return string
*/
protected function makeFilename()
{
return getcwd().'/laravel_'.md5(time().uniqid()).'.zip';
}
/**
* Download the temporary Zip to the given file.
*
* @param string $zipFile
* @param string $version
* @return $this
*/
protected function download($zipFile, $version = 'master')
{
switch ($version) {
case 'develop':
$filename = 'latest-develop.zip';
break;
case 'master':
$filename = 'latest.zip';
break;
}
$response = (new Client)->get('http://cabinet.laravel.com/'.$filename);
file_put_contents($zipFile, $response->getBody());
return $this;
}
/**
* Extract the Zip file into the given directory.
*
* @param string $zipFile
* @param string $directory
* @return $this
*/
protected function extract($zipFile, $directory)
{
$archive = new ZipArchive;
$archive->open($zipFile);
$archive->extractTo($directory);
$archive->close();
return $this;
}
/**
* Clean-up the Zip file.
*
* @param string $zipFile
* @return $this
*/
protected function cleanUp($zipFile)
{
@chmod($zipFile, 0777);
@unlink($zipFile);
return $this;
}
/**
* Make sure the storage and bootstrap cache directories are writable.
*
* @param string $appDirectory
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return $this
*/
protected function prepareWritableDirectories($appDirectory, OutputInterface $output)
{
$filesystem = new Filesystem;
try {
$filesystem->chmod($appDirectory.DIRECTORY_SEPARATOR."bootstrap/cache", 0755, 0000, true);
$filesystem->chmod($appDirectory.DIRECTORY_SEPARATOR."storage", 0755, 0000, true);
} catch (IOExceptionInterface $e) {
$output->writeln('<comment>You should verify that the "storage" and "bootstrap/cache" directories are writable.</comment>');
}
return $this;
}
/**
* Get the version that should be downloaded.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @return string
*/
protected function getVersion(InputInterface $input)
{
if ($input->getOption('dev')) {
return 'develop';
}
return 'master';
}
/**
* Get the composer command for the environment.
*
* @return string
*/
protected function findComposer()
{
if (file_exists(getcwd().'/composer.phar')) {
return '"'.PHP_BINARY.'" composer.phar';
}
return 'composer';
}
}