|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Acacha\Llum\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Acacha\Llum\Exceptions\InvalidCommandException; |
|
6
|
|
|
use Acacha\Llum\Traits\LaravelConfigFile; |
|
7
|
|
|
use Illuminate\Config\Repository; |
|
8
|
|
|
use Symfony\Component\Console\Command\Command; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Symfony\Component\Process\Process; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class LlumCommand. |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class LlumCommand extends Command |
|
18
|
|
|
{ |
|
19
|
|
|
use LaravelConfigFile; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The output interface. |
|
23
|
|
|
* |
|
24
|
|
|
* @var OutputInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $output; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Command name. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $commandName; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Command description. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $commandDescription; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Command argument. |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $argument; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Argument type. |
|
51
|
|
|
* |
|
52
|
|
|
* @var int |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $argumentType = InputArgument::REQUIRED; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Command argument description. |
|
58
|
|
|
* |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $argumentDescription; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Method to execute. |
|
65
|
|
|
* |
|
66
|
|
|
* @var string |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $method; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Laravel config file (config/app.php). |
|
72
|
|
|
* |
|
73
|
|
|
* @var string |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $laravel_config_file; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Laravel services file (config/services.php). |
|
79
|
|
|
* |
|
80
|
|
|
* @var string |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $laravel_services_file; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Path to config folder. |
|
86
|
|
|
* |
|
87
|
|
|
* @var string |
|
88
|
|
|
*/ |
|
89
|
|
|
protected $configPath; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Config repository. |
|
93
|
|
|
* |
|
94
|
|
|
* @var Repository |
|
95
|
|
|
*/ |
|
96
|
|
|
protected $config; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Install development version |
|
100
|
|
|
* |
|
101
|
|
|
* @var bool |
|
102
|
|
|
*/ |
|
103
|
|
|
protected $installDev = false; |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* LlumCommand constructor. |
|
107
|
|
|
*/ |
|
108
|
|
|
public function __construct() |
|
109
|
|
|
{ |
|
110
|
|
|
parent::__construct(); |
|
111
|
|
|
$this->configPath = __DIR__.'/../config/'; |
|
112
|
|
|
$this->laravel_config_file = getcwd().'/config/app.php'; |
|
113
|
|
|
$this->laravel_services_file = getcwd().'/config/services.php'; |
|
114
|
|
|
$this->config = $this->obtainConfig(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/* |
|
118
|
|
|
* gets dev option |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function getDevOption() |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->installDev ? ":dev-master" : ""; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Require composer package. |
|
129
|
|
|
* |
|
130
|
|
|
* @param $package |
|
131
|
|
|
*/ |
|
132
|
|
|
private function requireComposerPackage($package) |
|
133
|
|
|
{ |
|
134
|
|
|
$composer = $this->findComposer(); |
|
135
|
|
|
|
|
136
|
|
|
$process = new Process($composer.' require '.$package.'' . $this->getDevOption(), |
|
137
|
|
|
null, null, null, null); |
|
138
|
|
|
$this->output->writeln('<info>Running composer require '.$package.'</info>'); |
|
139
|
|
|
$process->run(function ($type, $line) { |
|
140
|
|
|
$this->output->write($line); |
|
141
|
|
|
}); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get the composer command for the environment. |
|
146
|
|
|
* |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
private function findComposer() |
|
150
|
|
|
{ |
|
151
|
|
|
if (file_exists(getcwd().'/composer.phar')) { |
|
152
|
|
|
return '"'.PHP_BINARY.'" composer.phar"'; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return 'composer'; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* get package from config. |
|
160
|
|
|
* |
|
161
|
|
|
* @param $name |
|
162
|
|
|
* |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
|
|
private function getPackageFromConfig($name) |
|
166
|
|
|
{ |
|
167
|
|
|
//Check if package name is a composer package name |
|
168
|
|
|
if (str_contains($name, '/')) { |
|
169
|
|
|
return $this->config->get($this->getPackageNameByComposerName($name)); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $this->config->get($name); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Installs laravel package form config/packages.php file. |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $name |
|
179
|
|
|
* @return int -1 if error occurred |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function package($name) |
|
182
|
|
|
{ |
|
183
|
|
|
$package = $this->obtainPackage($name); |
|
184
|
|
|
|
|
185
|
|
|
if ($package == -1) { |
|
186
|
|
|
return; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
list($name, $providers, $aliases, $after) = array_fill(0, 4, null); |
|
190
|
|
|
extract($package, EXTR_IF_EXISTS); |
|
191
|
|
|
|
|
192
|
|
|
$this->requireComposerPackage($name); |
|
193
|
|
|
|
|
194
|
|
|
if ($this->setupLaravelConfigFile($providers, $aliases) == -1) { |
|
195
|
|
|
return -1; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
$this->executeScriptAfterPackageInstallation($after); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Obtain package. |
|
203
|
|
|
* |
|
204
|
|
|
* @param $name |
|
205
|
|
|
* @return array|int |
|
206
|
|
|
*/ |
|
207
|
|
|
private function obtainPackage($name) |
|
208
|
|
|
{ |
|
209
|
|
|
$package = $this->getPackageFromConfig($name); |
|
210
|
|
|
|
|
211
|
|
|
if ($package == null) { |
|
212
|
|
|
$this->showPackageNotFoundError($name); |
|
213
|
|
|
|
|
214
|
|
|
return -1; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $package; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Execute post package installation script. |
|
222
|
|
|
* |
|
223
|
|
|
* @param $after |
|
224
|
|
|
*/ |
|
225
|
|
|
private function executeScriptAfterPackageInstallation($after) |
|
226
|
|
|
{ |
|
227
|
|
|
if ($after != null) { |
|
228
|
|
|
passthru($after); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Get config repository. |
|
234
|
|
|
* |
|
235
|
|
|
* @return Repository |
|
236
|
|
|
*/ |
|
237
|
|
|
protected function obtainConfig() |
|
238
|
|
|
{ |
|
239
|
|
|
return new Repository(require $this->configPath.'packages.php'); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Get package name by composer package name. |
|
244
|
|
|
* |
|
245
|
|
|
* @param $composerPackageName |
|
246
|
|
|
* |
|
247
|
|
|
* @return string |
|
248
|
|
|
*/ |
|
249
|
|
|
private function getPackageNameByComposerName($composerPackageName) |
|
250
|
|
|
{ |
|
251
|
|
|
foreach ($this->config->all() as $key => $configItem) { |
|
252
|
|
|
if ($configItem[ 'name' ] == $composerPackageName) { |
|
253
|
|
|
return $key; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
return; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Show package not found error. |
|
262
|
|
|
* |
|
263
|
|
|
* @param $name |
|
264
|
|
|
*/ |
|
265
|
|
|
protected function showPackageNotFoundError($name) |
|
266
|
|
|
{ |
|
267
|
|
|
$this->output->writeln('<error>Package '.$name.' not found in file '.$this->configPath.'packages.php</error>'); |
|
268
|
|
|
|
|
269
|
|
|
return; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Configure the command options. |
|
274
|
|
|
* |
|
275
|
|
|
* @param ConsoleCommand $command |
|
276
|
|
|
* @throws \Acacha\Llum\Exceptions\InvalidCommandException |
|
277
|
|
|
*/ |
|
278
|
|
|
protected function configureCommand(ConsoleCommand $command) |
|
279
|
|
|
{ |
|
280
|
|
|
$this->ignoreValidationErrors(); |
|
281
|
|
|
|
|
282
|
|
|
$name = $command->name(); |
|
283
|
|
|
$description = $command->description(); |
|
284
|
|
|
|
|
285
|
|
|
if (! is_string($name) || ! is_string($description)) { |
|
286
|
|
|
throw new InvalidCommandException; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
$this->setName($name) |
|
290
|
|
|
->setDescription($description); |
|
291
|
|
|
if ($command->argument() != null) { |
|
292
|
|
|
$this->addArgument($command->argument()[ 'name' ], |
|
293
|
|
|
$command->argument()[ 'type' ], |
|
294
|
|
|
$command->argument()[ 'description' ] |
|
295
|
|
|
); |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Execute the command. |
|
301
|
|
|
* |
|
302
|
|
|
* @param InputInterface $input |
|
303
|
|
|
* @param OutputInterface $output |
|
304
|
|
|
* |
|
305
|
|
|
* @return int|null|void |
|
306
|
|
|
*/ |
|
307
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
308
|
|
|
{ |
|
309
|
|
|
$this->output = $output; |
|
310
|
|
|
$method = $this->method; |
|
311
|
|
|
if ($this->argument != null) { |
|
312
|
|
|
$argument = $input->getArgument($this->argument); |
|
313
|
|
|
$this->$method($argument); |
|
314
|
|
|
|
|
315
|
|
|
return; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
$this->$method(); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Configure the command options. |
|
323
|
|
|
*/ |
|
324
|
|
|
protected function configure() |
|
325
|
|
|
{ |
|
326
|
|
|
$command = new ConsoleCommand(); |
|
327
|
|
|
|
|
328
|
|
|
$command->name($this->commandName) |
|
329
|
|
|
->description($this->commandDescription); |
|
330
|
|
|
|
|
331
|
|
|
if ($this->argument != null) { |
|
332
|
|
|
$command->argument([ |
|
333
|
|
|
'name' => $this->argument, |
|
334
|
|
|
'description' => $this->argumentDescription, |
|
335
|
|
|
'type' => $this->argumentType, |
|
336
|
|
|
]); |
|
337
|
|
|
} |
|
338
|
|
|
$this->configureCommand($command); |
|
339
|
|
|
} |
|
340
|
|
|
} |
|
341
|
|
|
|