1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Console;
|
4
|
|
5
|
use Illuminate\Contracts\Support\Arrayable;
|
6
|
use Symfony\Component\Console\Helper\Table;
|
7
|
use Symfony\Component\Console\Input\ArrayInput;
|
8
|
use Symfony\Component\Console\Output\NullOutput;
|
9
|
use Symfony\Component\Console\Question\Question;
|
10
|
use Symfony\Component\Console\Input\InputInterface;
|
11
|
use Symfony\Component\Console\Output\OutputInterface;
|
12
|
use Symfony\Component\Console\Question\ChoiceQuestion;
|
13
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
14
|
use Symfony\Component\Console\Command\Command as SymfonyCommand;
|
15
|
|
16
|
class Command extends SymfonyCommand
|
17
|
{
|
18
|
/**
|
19
|
* The Laravel application instance.
|
20
|
*
|
21
|
* @var \Illuminate\Contracts\Foundation\Application
|
22
|
*/
|
23
|
protected $laravel;
|
24
|
|
25
|
/**
|
26
|
* The input interface implementation.
|
27
|
*
|
28
|
* @var \Symfony\Component\Console\Input\InputInterface
|
29
|
*/
|
30
|
protected $input;
|
31
|
|
32
|
/**
|
33
|
* The output interface implementation.
|
34
|
*
|
35
|
* @var \Illuminate\Console\OutputStyle
|
36
|
*/
|
37
|
protected $output;
|
38
|
|
39
|
/**
|
40
|
* The name and signature of the console command.
|
41
|
*
|
42
|
* @var string
|
43
|
*/
|
44
|
protected $signature;
|
45
|
|
46
|
/**
|
47
|
* The console command name.
|
48
|
*
|
49
|
* @var string
|
50
|
*/
|
51
|
protected $name;
|
52
|
|
53
|
/**
|
54
|
* The console command description.
|
55
|
*
|
56
|
* @var string
|
57
|
*/
|
58
|
protected $description;
|
59
|
|
60
|
/**
|
61
|
* The default verbosity of output commands.
|
62
|
*
|
63
|
* @var int
|
64
|
*/
|
65
|
protected $verbosity = OutputInterface::VERBOSITY_NORMAL;
|
66
|
|
67
|
/**
|
68
|
* The mapping between human readable verbosity levels and Symfony's OutputInterface.
|
69
|
*
|
70
|
* @var array
|
71
|
*/
|
72
|
protected $verbosityMap = [
|
73
|
'v' => OutputInterface::VERBOSITY_VERBOSE,
|
74
|
'vv' => OutputInterface::VERBOSITY_VERY_VERBOSE,
|
75
|
'vvv' => OutputInterface::VERBOSITY_DEBUG,
|
76
|
'quiet' => OutputInterface::VERBOSITY_QUIET,
|
77
|
'normal' => OutputInterface::VERBOSITY_NORMAL,
|
78
|
];
|
79
|
|
80
|
/**
|
81
|
* Create a new console command instance.
|
82
|
*
|
83
|
* @return void
|
84
|
*/
|
85
|
public function __construct()
|
86
|
{
|
87
|
// We will go ahead and set the name, description, and parameters on console
|
88
|
// commands just to make things a little easier on the developer. This is
|
89
|
// so they don't have to all be manually specified in the constructors.
|
90
|
if (isset($this->signature)) {
|
91
|
$this->configureUsingFluentDefinition();
|
92
|
} else {
|
93
|
parent::__construct($this->name);
|
94
|
}
|
95
|
|
96
|
$this->setDescription($this->description);
|
97
|
|
98
|
if (! isset($this->signature)) {
|
99
|
$this->specifyParameters();
|
100
|
}
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Configure the console command using a fluent definition.
|
105
|
*
|
106
|
* @return void
|
107
|
*/
|
108
|
protected function configureUsingFluentDefinition()
|
109
|
{
|
110
|
list($name, $arguments, $options) = Parser::parse($this->signature);
|
111
|
|
112
|
parent::__construct($name);
|
113
|
|
114
|
foreach ($arguments as $argument) {
|
115
|
$this->getDefinition()->addArgument($argument);
|
116
|
}
|
117
|
|
118
|
foreach ($options as $option) {
|
119
|
$this->getDefinition()->addOption($option);
|
120
|
}
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* Specify the arguments and options on the command.
|
125
|
*
|
126
|
* @return void
|
127
|
*/
|
128
|
protected function specifyParameters()
|
129
|
{
|
130
|
// We will loop through all of the arguments and options for the command and
|
131
|
// set them all on the base command instance. This specifies what can get
|
132
|
// passed into these commands as "parameters" to control the execution.
|
133
|
foreach ($this->getArguments() as $arguments) {
|
134
|
call_user_func_array([$this, 'addArgument'], $arguments);
|
135
|
}
|
136
|
|
137
|
foreach ($this->getOptions() as $options) {
|
138
|
call_user_func_array([$this, 'addOption'], $options);
|
139
|
}
|
140
|
}
|
141
|
|
142
|
/**
|
143
|
* Run the console command.
|
144
|
*
|
145
|
* @param \Symfony\Component\Console\Input\InputInterface $input
|
146
|
* @param \Symfony\Component\Console\Output\OutputInterface $output
|
147
|
* @return int
|
148
|
*/
|
149
|
public function run(InputInterface $input, OutputInterface $output)
|
150
|
{
|
151
|
$this->input = $input;
|
152
|
|
153
|
$this->output = new OutputStyle($input, $output);
|
154
|
|
155
|
return parent::run($input, $output);
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* Execute the console command.
|
160
|
*
|
161
|
* @param \Symfony\Component\Console\Input\InputInterface $input
|
162
|
* @param \Symfony\Component\Console\Output\OutputInterface $output
|
163
|
* @return mixed
|
164
|
*/
|
165
|
protected function execute(InputInterface $input, OutputInterface $output)
|
166
|
{
|
167
|
$method = method_exists($this, 'handle') ? 'handle' : 'fire';
|
168
|
|
169
|
return $this->laravel->call([$this, $method]);
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
* Call another console command.
|
174
|
*
|
175
|
* @param string $command
|
176
|
* @param array $arguments
|
177
|
* @return int
|
178
|
*/
|
179
|
public function call($command, array $arguments = [])
|
180
|
{
|
181
|
$instance = $this->getApplication()->find($command);
|
182
|
|
183
|
$arguments['command'] = $command;
|
184
|
|
185
|
return $instance->run(new ArrayInput($arguments), $this->output);
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* Call another console command silently.
|
190
|
*
|
191
|
* @param string $command
|
192
|
* @param array $arguments
|
193
|
* @return int
|
194
|
*/
|
195
|
public function callSilent($command, array $arguments = [])
|
196
|
{
|
197
|
$instance = $this->getApplication()->find($command);
|
198
|
|
199
|
$arguments['command'] = $command;
|
200
|
|
201
|
return $instance->run(new ArrayInput($arguments), new NullOutput);
|
202
|
}
|
203
|
|
204
|
/**
|
205
|
* Determine if the given argument is present.
|
206
|
*
|
207
|
* @param string|int $name
|
208
|
* @return bool
|
209
|
*/
|
210
|
public function hasArgument($name)
|
211
|
{
|
212
|
return $this->input->hasArgument($name);
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* Get the value of a command argument.
|
217
|
*
|
218
|
* @param string $key
|
219
|
* @return string|array
|
220
|
*/
|
221
|
public function argument($key = null)
|
222
|
{
|
223
|
if (is_null($key)) {
|
224
|
return $this->input->getArguments();
|
225
|
}
|
226
|
|
227
|
return $this->input->getArgument($key);
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Determine if the given option is present.
|
232
|
*
|
233
|
* @param string $name
|
234
|
* @return bool
|
235
|
*/
|
236
|
public function hasOption($name)
|
237
|
{
|
238
|
return $this->input->hasOption($name);
|
239
|
}
|
240
|
|
241
|
/**
|
242
|
* Get the value of a command option.
|
243
|
*
|
244
|
* @param string $key
|
245
|
* @return string|array
|
246
|
*/
|
247
|
public function option($key = null)
|
248
|
{
|
249
|
if (is_null($key)) {
|
250
|
return $this->input->getOptions();
|
251
|
}
|
252
|
|
253
|
return $this->input->getOption($key);
|
254
|
}
|
255
|
|
256
|
/**
|
257
|
* Confirm a question with the user.
|
258
|
*
|
259
|
* @param string $question
|
260
|
* @param bool $default
|
261
|
* @return bool
|
262
|
*/
|
263
|
public function confirm($question, $default = false)
|
264
|
{
|
265
|
return $this->output->confirm($question, $default);
|
266
|
}
|
267
|
|
268
|
/**
|
269
|
* Prompt the user for input.
|
270
|
*
|
271
|
* @param string $question
|
272
|
* @param string $default
|
273
|
* @return string
|
274
|
*/
|
275
|
public function ask($question, $default = null)
|
276
|
{
|
277
|
return $this->output->ask($question, $default);
|
278
|
}
|
279
|
|
280
|
/**
|
281
|
* Prompt the user for input with auto completion.
|
282
|
*
|
283
|
* @param string $question
|
284
|
* @param array $choices
|
285
|
* @param string $default
|
286
|
* @return string
|
287
|
*/
|
288
|
public function anticipate($question, array $choices, $default = null)
|
289
|
{
|
290
|
return $this->askWithCompletion($question, $choices, $default);
|
291
|
}
|
292
|
|
293
|
/**
|
294
|
* Prompt the user for input with auto completion.
|
295
|
*
|
296
|
* @param string $question
|
297
|
* @param array $choices
|
298
|
* @param string $default
|
299
|
* @return string
|
300
|
*/
|
301
|
public function askWithCompletion($question, array $choices, $default = null)
|
302
|
{
|
303
|
$question = new Question($question, $default);
|
304
|
|
305
|
$question->setAutocompleterValues($choices);
|
306
|
|
307
|
return $this->output->askQuestion($question);
|
308
|
}
|
309
|
|
310
|
/**
|
311
|
* Prompt the user for input but hide the answer from the console.
|
312
|
*
|
313
|
* @param string $question
|
314
|
* @param bool $fallback
|
315
|
* @return string
|
316
|
*/
|
317
|
public function secret($question, $fallback = true)
|
318
|
{
|
319
|
$question = new Question($question);
|
320
|
|
321
|
$question->setHidden(true)->setHiddenFallback($fallback);
|
322
|
|
323
|
return $this->output->askQuestion($question);
|
324
|
}
|
325
|
|
326
|
/**
|
327
|
* Give the user a single choice from an array of answers.
|
328
|
*
|
329
|
* @param string $question
|
330
|
* @param array $choices
|
331
|
* @param string $default
|
332
|
* @param mixed $attempts
|
333
|
* @param bool $multiple
|
334
|
* @return string
|
335
|
*/
|
336
|
public function choice($question, array $choices, $default = null, $attempts = null, $multiple = null)
|
337
|
{
|
338
|
$question = new ChoiceQuestion($question, $choices, $default);
|
339
|
|
340
|
$question->setMaxAttempts($attempts)->setMultiselect($multiple);
|
341
|
|
342
|
return $this->output->askQuestion($question);
|
343
|
}
|
344
|
|
345
|
/**
|
346
|
* Format input to textual table.
|
347
|
*
|
348
|
* @param array $headers
|
349
|
* @param \Illuminate\Contracts\Support\Arrayable|array $rows
|
350
|
* @param string $style
|
351
|
* @return void
|
352
|
*/
|
353
|
public function table(array $headers, $rows, $style = 'default')
|
354
|
{
|
355
|
$table = new Table($this->output);
|
356
|
|
357
|
if ($rows instanceof Arrayable) {
|
358
|
$rows = $rows->toArray();
|
359
|
}
|
360
|
|
361
|
$table->setHeaders($headers)->setRows($rows)->setStyle($style)->render();
|
362
|
}
|
363
|
|
364
|
/**
|
365
|
* Write a string as information output.
|
366
|
*
|
367
|
* @param string $string
|
368
|
* @param null|int|string $verbosity
|
369
|
* @return void
|
370
|
*/
|
371
|
public function info($string, $verbosity = null)
|
372
|
{
|
373
|
$this->line($string, 'info', $verbosity);
|
374
|
}
|
375
|
|
376
|
/**
|
377
|
* Write a string as standard output.
|
378
|
*
|
379
|
* @param string $string
|
380
|
* @param string $style
|
381
|
* @param null|int|string $verbosity
|
382
|
* @return void
|
383
|
*/
|
384
|
public function line($string, $style = null, $verbosity = null)
|
385
|
{
|
386
|
$styled = $style ? "<$style>$string</$style>" : $string;
|
387
|
|
388
|
$this->output->writeln($styled, $this->parseVerbosity($verbosity));
|
389
|
}
|
390
|
|
391
|
/**
|
392
|
* Write a string as comment output.
|
393
|
*
|
394
|
* @param string $string
|
395
|
* @param null|int|string $verbosity
|
396
|
* @return void
|
397
|
*/
|
398
|
public function comment($string, $verbosity = null)
|
399
|
{
|
400
|
$this->line($string, 'comment', $verbosity);
|
401
|
}
|
402
|
|
403
|
/**
|
404
|
* Write a string as question output.
|
405
|
*
|
406
|
* @param string $string
|
407
|
* @param null|int|string $verbosity
|
408
|
* @return void
|
409
|
*/
|
410
|
public function question($string, $verbosity = null)
|
411
|
{
|
412
|
$this->line($string, 'question', $verbosity);
|
413
|
}
|
414
|
|
415
|
/**
|
416
|
* Write a string as error output.
|
417
|
*
|
418
|
* @param string $string
|
419
|
* @param null|int|string $verbosity
|
420
|
* @return void
|
421
|
*/
|
422
|
public function error($string, $verbosity = null)
|
423
|
{
|
424
|
$this->line($string, 'error', $verbosity);
|
425
|
}
|
426
|
|
427
|
/**
|
428
|
* Write a string as warning output.
|
429
|
*
|
430
|
* @param string $string
|
431
|
* @param null|int|string $verbosity
|
432
|
* @return void
|
433
|
*/
|
434
|
public function warn($string, $verbosity = null)
|
435
|
{
|
436
|
if (! $this->output->getFormatter()->hasStyle('warning')) {
|
437
|
$style = new OutputFormatterStyle('yellow');
|
438
|
|
439
|
$this->output->getFormatter()->setStyle('warning', $style);
|
440
|
}
|
441
|
|
442
|
$this->line($string, 'warning', $verbosity);
|
443
|
}
|
444
|
|
445
|
/**
|
446
|
* Get the verbosity level in terms of Symfony's OutputInterface level.
|
447
|
*
|
448
|
* @param string|int $level
|
449
|
* @return int
|
450
|
*/
|
451
|
protected function parseVerbosity($level = null)
|
452
|
{
|
453
|
if (isset($this->verbosityMap[$level])) {
|
454
|
$level = $this->verbosityMap[$level];
|
455
|
} elseif (! is_int($level)) {
|
456
|
$level = $this->verbosity;
|
457
|
}
|
458
|
|
459
|
return $level;
|
460
|
}
|
461
|
|
462
|
/**
|
463
|
* Set the verbosity level.
|
464
|
*
|
465
|
* @param string|int $level
|
466
|
* @return void
|
467
|
*/
|
468
|
protected function setVerbosity($level)
|
469
|
{
|
470
|
$this->verbosity = $this->parseVerbosity($level);
|
471
|
}
|
472
|
|
473
|
/**
|
474
|
* Get the console command arguments.
|
475
|
*
|
476
|
* @return array
|
477
|
*/
|
478
|
protected function getArguments()
|
479
|
{
|
480
|
return [];
|
481
|
}
|
482
|
|
483
|
/**
|
484
|
* Get the console command options.
|
485
|
*
|
486
|
* @return array
|
487
|
*/
|
488
|
protected function getOptions()
|
489
|
{
|
490
|
return [];
|
491
|
}
|
492
|
|
493
|
/**
|
494
|
* Get the output implementation.
|
495
|
*
|
496
|
* @return \Symfony\Component\Console\Output\OutputInterface
|
497
|
*/
|
498
|
public function getOutput()
|
499
|
{
|
500
|
return $this->output;
|
501
|
}
|
502
|
|
503
|
/**
|
504
|
* Get the Laravel application instance.
|
505
|
*
|
506
|
* @return \Illuminate\Contracts\Foundation\Application
|
507
|
*/
|
508
|
public function getLaravel()
|
509
|
{
|
510
|
return $this->laravel;
|
511
|
}
|
512
|
|
513
|
/**
|
514
|
* Set the Laravel application instance.
|
515
|
*
|
516
|
* @param \Illuminate\Contracts\Container\Container $laravel
|
517
|
* @return void
|
518
|
*/
|
519
|
public function setLaravel($laravel)
|
520
|
{
|
521
|
$this->laravel = $laravel;
|
522
|
}
|
523
|
}
|