-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[6.x] Split Console InteractsWithIO to external trait. (#31376)
Signed-off-by: Mior Muhammad Zaki <[email protected]>
- v12.1.1
- v12.1.0
- v12.0.1
- v12.0.0
- v11.44.1
- v11.44.0
- v11.43.2
- v11.43.1
- v11.43.0
- v11.42.1
- v11.42.0
- v11.41.3
- v11.41.2
- v11.41.1
- v11.41.0
- v11.40.0
- v11.39.1
- v11.39.0
- v11.38.2
- v11.38.1
- v11.38.0
- v11.37.0
- v11.36.1
- v11.36.0
- v11.35.1
- v11.35.0
- v11.34.2
- v11.34.1
- v11.34.0
- v11.33.2
- v11.33.1
- v11.33.0
- v11.32.0
- v11.31.0
- v11.30.0
- v11.29.0
- v11.28.1
- v11.28.0
- v11.27.2
- v11.27.1
- v11.27.0
- v11.26.0
- v11.25.0
- v11.24.1
- v11.24.0
- v11.23.5
- v11.23.4
- v11.23.3
- v11.23.2
- v11.23.1
- v11.23.0
- v11.22.0
- v11.21.0
- v11.20.0
- v11.19.0
- v11.18.1
- v11.18.0
- v11.17.0
- v11.16.0
- v11.15.0
- v11.14.0
- v11.13.0
- v11.12.0
- v11.11.1
- v11.11.0
- v11.10.0
- v11.9.2
- v11.9.1
- v11.9.0
- v11.8.0
- v11.7.0
- v11.6.0
- v11.5.0
- v11.4.0
- v11.3.1
- v11.3.0
- v11.2.0
- v11.1.1
- v11.1.0
- v11.0.8
- v11.0.7
- v11.0.6
- v11.0.5
- v11.0.4
- v11.0.3
- v11.0.2
- v11.0.1
- v11.0.0
- v10.48.28
- v10.48.27
- v10.48.26
- v10.48.25
- v10.48.24
- v10.48.23
- v10.48.22
- v10.48.21
- v10.48.20
- v10.48.19
- v10.48.18
- v10.48.17
- v10.48.16
- v10.48.15
- v10.48.14
- v10.48.13
- v10.48.12
- v10.48.11
- v10.48.10
- v10.48.9
- v10.48.8
- v10.48.7
- v10.48.6
- v10.48.5
- v10.48.4
- v10.48.3
- v10.48.2
- v10.48.1
- v10.48.0
- v10.47.0
- v10.46.0
- v10.45.1
- v10.45.0
- v10.44.0
- v10.43.0
- v10.42.0
- v10.41.0
- v10.40.0
- v10.39.0
- v10.38.2
- v10.38.1
- v10.38.0
- v10.37.3
- v10.37.2
- v10.37.1
- v10.37.0
- v10.36.0
- v10.35.0
- v10.34.2
- v10.34.1
- v10.34.0
- v10.33.0
- v10.32.1
- v10.32.0
- v10.31.0
- v10.30.1
- v10.30.0
- v10.29.0
- v10.28.0
- v10.27.0
- v10.26.2
- v10.26.1
- v10.26.0
- v10.25.2
- v10.25.1
- v10.25.0
- v10.24.0
- v10.23.1
- v10.23.0
- v10.22.0
- v10.21.1
- v10.21.0
- v10.20.0
- v10.19.0
- v10.18.0
- v10.17.1
- v10.17.0
- v10.16.1
- v10.16.0
- v10.15.0
- v10.14.1
- v10.14.0
- v10.13.5
- v10.13.3
- v10.13.2
- v10.13.1
- v10.13.0
- v10.12.0
- v10.11.0
- v10.10.1
- v10.10.0
- v10.9.0
- v10.8.0
- v10.7.1
- v10.7.0
- v10.6.2
- v10.6.1
- v10.6.0
- v10.5.1
- v10.5.0
- v10.4.1
- v10.4.0
- v10.3.3
- v10.3.2
- v10.3.1
- v10.3.0
- v10.2.0
- v10.1.5
- v10.1.4
- v10.1.3
- v10.1.2
- v10.1.1
- v10.1.0
- v10.0.3
- v10.0.2
- v10.0.1
- v10.0.0
- v9.52.20
- v9.52.19
- v9.52.18
- v9.52.17
- v9.52.16
- v9.52.15
- v9.52.14
- v9.52.13
- v9.52.12
- v9.52.11
- v9.52.10
- v9.52.9
- v9.52.8
- v9.52.7
- v9.52.6
- v9.52.5
- v9.52.4
- v9.52.3
- v9.52.2
- v9.52.1
- v9.52.0
- v9.51.0
- v9.50.2
- v9.50.1
- v9.50.0
- v9.49.0
- v9.48.0
- v9.47.0
- v9.46.0
- v9.45.1
- v9.45.0
- v9.44.0
- v9.43.0
- v9.42.2
- v9.42.1
- v9.42.0
- v9.41.0
- v9.40.1
- v9.40.0
- v9.39.0
- v9.38.0
- v9.37.0
- v9.36.4
- v9.36.3
- v9.36.2
- v9.36.1
- v9.36.0
- v9.35.1
- v9.35.0
- v9.34.0
- v9.33.0
- v9.32.0
- v9.31.0
- v9.30.1
- v9.30.0
- v9.29.0
- v9.28.0
- v9.27.0
- v9.26.1
- v9.26.0
- v9.25.1
- v9.25.0
- v9.24.0
- v9.23.0
- v9.22.1
- v9.22.0
- v9.21.6
- v9.21.5
- v9.21.4
- v9.21.3
- v9.21.2
- v9.21.1
- v9.21.0
- v9.20.0
- v9.19.0
- v9.18.0
- v9.17.0
- v9.16.0
- v9.15.0
- v9.14.1
- v9.14.0
- v9.13.0
- v9.12.2
- v9.12.1
- v9.12.0
- v9.11.0
- v9.10.1
- v9.10.0
- v9.9.0
- v9.8.1
- v9.8.0
- v9.7.0
- v9.6.0
- v9.5.1
- v9.5.0
- v9.4.1
- v9.4.0
- v9.3.1
- v9.3.0
- v9.2.0
- v9.1.0
- v9.0.2
- v9.0.1
- v9.0.0
- v9.0.0-beta.5
- v9.0.0-beta.4
- v9.0.0-beta.3
- v9.0.0-beta.2
- v9.0.0-beta.1
- v8.83.29
- v8.83.28
- v8.83.27
- v8.83.26
- v8.83.25
- v8.83.24
- v8.83.23
- v8.83.22
- v8.83.21
- v8.83.20
- v8.83.19
- v8.83.18
- v8.83.17
- v8.83.16
- v8.83.15
- v8.83.14
- v8.83.13
- v8.83.12
- v8.83.11
- v8.83.10
- v8.83.9
- v8.83.8
- v8.83.7
- v8.83.6
- v8.83.5
- v8.83.4
- v8.83.3
- v8.83.2
- v8.83.1
- v8.83.0
- v8.82.0
- v8.81.0
- v8.80.0
- v8.79.0
- v8.78.1
- v8.78.0
- v8.77.1
- v8.77.0
- v8.76.2
- v8.76.1
- v8.76.0
- v8.75.0
- v8.74.0
- v8.73.2
- v8.73.1
- v8.73.0
- v8.72.0
- v8.71.0
- v8.70.2
- v8.70.1
- v8.70.0
- v8.69.0
- v8.68.1
- v8.68.0
- v8.67.0
- v8.66.0
- v8.65.0
- v8.64.0
- v8.63.0
- v8.62.0
- v8.61.0
- v8.60.0
- v8.59.0
- v8.58.0
- v8.57.0
- v8.56.0
- v8.55.0
- v8.54.0
- v8.53.1
- v8.53.0
- v8.52.0
- v8.51.0
- v8.50.0
- v8.49.2
- v8.49.1
- v8.49.0
- v8.48.2
- v8.48.1
- v8.48.0
- v8.47.0
- v8.46.0
- v8.45.1
- v8.45.0
- v8.44.0
- v8.43.0
- v8.42.1
- v8.42.0
- v8.41.0
- v8.40.0
- v8.39.0
- v8.38.0
- v8.37.0
- v8.36.2
- v8.36.1
- v8.36.0
- v8.35.1
- v8.35.0
- v8.34.0
- v8.33.1
- v8.33.0
- v8.32.1
- v8.32.0
- v8.31.0
- v8.30.1
- v8.30.0
- v8.29.0
- v8.28.1
- v8.28.0
- v8.27.0
- v8.26.1
- v8.26.0
- v8.25.0
- v8.24.0
- v8.23.1
- v8.23.0
- v8.22.1
- v8.22.0
- v8.21.0
- v8.20.1
- v8.20.0
- v8.19.0
- v8.18.1
- v8.18.0
- v8.17.2
- v8.17.1
- v8.17.0
- v8.16.1
- v8.16.0
- v8.15.0
- v8.14.0
- v8.13.0
- v8.12.3
- v8.12.2
- v8.12.1
- v8.12.0
- v8.11.2
- v8.11.1
- v8.11.0
- v8.10.0
- v8.9.0
- v8.8.0
- v8.7.1
- v8.7.0
- v8.6.0
- v8.5.0
- v8.4.0
- v8.3.0
- v8.2.0
- v8.1.0
- v8.0.4
- v8.0.3
- v8.0.2
- v8.0.1
- v8.0.0
- v7.30.7
- v7.30.6
- v7.30.5
- v7.30.4
- v7.30.3
- v7.30.2
- v7.30.1
- v7.30.0
- v7.29.3
- v7.29.2
- v7.29.1
- v7.29.0
- v7.28.4
- v7.28.3
- v7.28.2
- v7.28.1
- v7.28.0
- v7.27.0
- v7.26.1
- v7.26.0
- v7.25.0
- v7.24.0
- v7.23.2
- v7.23.1
- v7.23.0
- v7.22.4
- v7.22.3
- v7.22.2
- v7.22.1
- v7.22.0
- v7.21.0
- v7.20.0
- v7.19.1
- v7.19.0
- v7.18.0
- v7.17.2
- v7.17.1
- v7.17.0
- v7.16.1
- v7.16.0
- v7.15.0
- v7.14.1
- v7.14.0
- v7.13.0
- v7.12.0
- v7.11.0
- v7.10.3
- v7.10.2
- v7.10.1
- v7.10.0
- v7.9.2
- v7.9.1
- v7.9.0
- v7.8.1
- v7.8.0
- v7.7.1
- v7.7.0
- v7.6.2
- v7.6.1
- v7.6.0
- v7.5.2
- v7.5.1
- v7.5.0
- v7.4.0
- v7.3.0
- v7.2.2
- v7.2.1
- v7.2.0
- v7.1.3
- v7.1.2
- v7.1.1
- v7.1.0
- v7.0.8
- v7.0.7
- v7.0.6
- v7.0.5
- v7.0.4
- v7.0.3
- v7.0.2
- v7.0.1
- v7.0.0
- v6.20.45
- v6.20.44
- v6.20.43
- v6.20.42
- v6.20.41
- v6.20.40
- v6.20.39
- v6.20.38
- v6.20.37
- v6.20.36
- v6.20.35
- v6.20.34
- v6.20.33
- v6.20.32
- v6.20.31
- v6.20.30
- v6.20.29
- v6.20.28
- v6.20.27
- v6.20.26
- v6.20.25
- v6.20.24
- v6.20.23
- v6.20.22
- v6.20.21
- v6.20.20
- v6.20.19
- v6.20.18
- v6.20.17
- v6.20.16
- v6.20.15
- v6.20.14
- v6.20.13
- v6.20.12
- v6.20.11
- v6.20.10
- v6.20.9
- v6.20.8
- v6.20.7
- v6.20.6
- v6.20.5
- v6.20.4
- v6.20.3
- v6.20.2
- v6.20.1
- v6.20.0
- v6.19.1
- v6.19.0
- v6.18.43
- v6.18.42
- v6.18.41
- v6.18.40
- v6.18.39
- v6.18.38
- v6.18.37
- v6.18.36
- v6.18.35
- v6.18.34
- v6.18.33
- v6.18.32
- v6.18.31
- v6.18.30
- v6.18.29
- v6.18.28
- v6.18.27
- v6.18.26
- v6.18.25
- v6.18.24
- v6.18.23
- v6.18.22
- v6.18.21
- v6.18.20
- v6.18.19
- v6.18.18
- v6.18.17
- v6.18.16
- v6.18.15
- v6.18.14
- v6.18.13
- v6.18.12
- v6.18.11
- v6.18.10
- v6.18.9
- v6.18.8
- v6.18.7
- v6.18.6
- v6.18.5
- v6.18.4
- v6.18.3
- v6.18.2
- v6.18.1
- v6.18.0
- v6.17.1
- v6.17.0
- v6.16.0
- v6.15.1
- v6.15.0
Showing
2 changed files
with
398 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,397 @@ | ||
<?php | ||
|
||
namespace Illuminate\Console\Concerns; | ||
|
||
use Illuminate\Console\OutputStyle; | ||
use Illuminate\Contracts\Support\Arrayable; | ||
use Illuminate\Support\Str; | ||
use Symfony\Component\Console\Formatter\OutputFormatterStyle; | ||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ChoiceQuestion; | ||
use Symfony\Component\Console\Question\Question; | ||
|
||
trait InteractsWithIO | ||
{ | ||
/** | ||
* The input interface implementation. | ||
* | ||
* @var \Symfony\Component\Console\Input\InputInterface | ||
*/ | ||
protected $input; | ||
|
||
/** | ||
* The output interface implementation. | ||
* | ||
* @var \Illuminate\Console\OutputStyle | ||
*/ | ||
protected $output; | ||
|
||
/** | ||
* The default verbosity of output commands. | ||
* | ||
* @var int | ||
*/ | ||
protected $verbosity = OutputInterface::VERBOSITY_NORMAL; | ||
|
||
/** | ||
* The mapping between human readable verbosity levels and Symfony's OutputInterface. | ||
* | ||
* @var array | ||
*/ | ||
protected $verbosityMap = [ | ||
'v' => OutputInterface::VERBOSITY_VERBOSE, | ||
'vv' => OutputInterface::VERBOSITY_VERY_VERBOSE, | ||
'vvv' => OutputInterface::VERBOSITY_DEBUG, | ||
'quiet' => OutputInterface::VERBOSITY_QUIET, | ||
'normal' => OutputInterface::VERBOSITY_NORMAL, | ||
]; | ||
|
||
/** | ||
* Determine if the given argument is present. | ||
* | ||
* @param string|int $name | ||
* @return bool | ||
*/ | ||
public function hasArgument($name) | ||
{ | ||
return $this->input->hasArgument($name); | ||
} | ||
|
||
/** | ||
* Get the value of a command argument. | ||
* | ||
* @param string|null $key | ||
* @return string|array|null | ||
*/ | ||
public function argument($key = null) | ||
{ | ||
if (is_null($key)) { | ||
return $this->input->getArguments(); | ||
} | ||
|
||
return $this->input->getArgument($key); | ||
} | ||
|
||
/** | ||
* Get all of the arguments passed to the command. | ||
* | ||
* @return array | ||
*/ | ||
public function arguments() | ||
{ | ||
return $this->argument(); | ||
} | ||
|
||
/** | ||
* Determine if the given option is present. | ||
* | ||
* @param string $name | ||
* @return bool | ||
*/ | ||
public function hasOption($name) | ||
{ | ||
return $this->input->hasOption($name); | ||
} | ||
|
||
/** | ||
* Get the value of a command option. | ||
* | ||
* @param string|null $key | ||
* @return string|array|bool|null | ||
*/ | ||
public function option($key = null) | ||
{ | ||
if (is_null($key)) { | ||
return $this->input->getOptions(); | ||
} | ||
|
||
return $this->input->getOption($key); | ||
} | ||
|
||
/** | ||
* Get all of the options passed to the command. | ||
* | ||
* @return array | ||
*/ | ||
public function options() | ||
{ | ||
return $this->option(); | ||
} | ||
|
||
/** | ||
* Confirm a question with the user. | ||
* | ||
* @param string $question | ||
* @param bool $default | ||
* @return bool | ||
*/ | ||
public function confirm($question, $default = false) | ||
{ | ||
return $this->output->confirm($question, $default); | ||
} | ||
|
||
/** | ||
* Prompt the user for input. | ||
* | ||
* @param string $question | ||
* @param string|null $default | ||
* @return mixed | ||
*/ | ||
public function ask($question, $default = null) | ||
{ | ||
return $this->output->ask($question, $default); | ||
} | ||
|
||
/** | ||
* Prompt the user for input with auto completion. | ||
* | ||
* @param string $question | ||
* @param array|callable $choices | ||
* @param string|null $default | ||
* @return mixed | ||
*/ | ||
public function anticipate($question, $choices, $default = null) | ||
{ | ||
return $this->askWithCompletion($question, $choices, $default); | ||
} | ||
|
||
/** | ||
* Prompt the user for input with auto completion. | ||
* | ||
* @param string $question | ||
* @param array|callable $choices | ||
* @param string|null $default | ||
* @return mixed | ||
*/ | ||
public function askWithCompletion($question, $choices, $default = null) | ||
{ | ||
$question = new Question($question, $default); | ||
|
||
is_callable($choices) | ||
? $question->setAutocompleterCallback($choices) | ||
: $question->setAutocompleterValues($choices); | ||
|
||
return $this->output->askQuestion($question); | ||
} | ||
|
||
/** | ||
* Prompt the user for input but hide the answer from the console. | ||
* | ||
* @param string $question | ||
* @param bool $fallback | ||
* @return mixed | ||
*/ | ||
public function secret($question, $fallback = true) | ||
{ | ||
$question = new Question($question); | ||
|
||
$question->setHidden(true)->setHiddenFallback($fallback); | ||
|
||
return $this->output->askQuestion($question); | ||
} | ||
|
||
/** | ||
* Give the user a single choice from an array of answers. | ||
* | ||
* @param string $question | ||
* @param array $choices | ||
* @param string|null $default | ||
* @param mixed|null $attempts | ||
* @param bool|null $multiple | ||
* @return string | ||
*/ | ||
public function choice($question, array $choices, $default = null, $attempts = null, $multiple = null) | ||
{ | ||
$question = new ChoiceQuestion($question, $choices, $default); | ||
|
||
$question->setMaxAttempts($attempts)->setMultiselect($multiple); | ||
|
||
return $this->output->askQuestion($question); | ||
} | ||
|
||
/** | ||
* Format input to textual table. | ||
* | ||
* @param array $headers | ||
* @param \Illuminate\Contracts\Support\Arrayable|array $rows | ||
* @param string $tableStyle | ||
* @param array $columnStyles | ||
* @return void | ||
*/ | ||
public function table($headers, $rows, $tableStyle = 'default', array $columnStyles = []) | ||
{ | ||
$table = new Table($this->output); | ||
|
||
if ($rows instanceof Arrayable) { | ||
$rows = $rows->toArray(); | ||
} | ||
|
||
$table->setHeaders((array) $headers)->setRows($rows)->setStyle($tableStyle); | ||
|
||
foreach ($columnStyles as $columnIndex => $columnStyle) { | ||
$table->setColumnStyle($columnIndex, $columnStyle); | ||
} | ||
|
||
$table->render(); | ||
} | ||
|
||
/** | ||
* Write a string as information output. | ||
* | ||
* @param string $string | ||
* @param int|string|null $verbosity | ||
* @return void | ||
*/ | ||
public function info($string, $verbosity = null) | ||
{ | ||
$this->line($string, 'info', $verbosity); | ||
} | ||
|
||
/** | ||
* Write a string as standard output. | ||
* | ||
* @param string $string | ||
* @param string|null $style | ||
* @param int|string|null $verbosity | ||
* @return void | ||
*/ | ||
public function line($string, $style = null, $verbosity = null) | ||
{ | ||
$styled = $style ? "<$style>$string</$style>" : $string; | ||
|
||
$this->output->writeln($styled, $this->parseVerbosity($verbosity)); | ||
} | ||
|
||
/** | ||
* Write a string as comment output. | ||
* | ||
* @param string $string | ||
* @param int|string|null $verbosity | ||
* @return void | ||
*/ | ||
public function comment($string, $verbosity = null) | ||
{ | ||
$this->line($string, 'comment', $verbosity); | ||
} | ||
|
||
/** | ||
* Write a string as question output. | ||
* | ||
* @param string $string | ||
* @param int|string|null $verbosity | ||
* @return void | ||
*/ | ||
public function question($string, $verbosity = null) | ||
{ | ||
$this->line($string, 'question', $verbosity); | ||
} | ||
|
||
/** | ||
* Write a string as error output. | ||
* | ||
* @param string $string | ||
* @param int|string|null $verbosity | ||
* @return void | ||
*/ | ||
public function error($string, $verbosity = null) | ||
{ | ||
$this->line($string, 'error', $verbosity); | ||
} | ||
|
||
/** | ||
* Write a string as warning output. | ||
* | ||
* @param string $string | ||
* @param int|string|null $verbosity | ||
* @return void | ||
*/ | ||
public function warn($string, $verbosity = null) | ||
{ | ||
if (! $this->output->getFormatter()->hasStyle('warning')) { | ||
$style = new OutputFormatterStyle('yellow'); | ||
|
||
$this->output->getFormatter()->setStyle('warning', $style); | ||
} | ||
|
||
$this->line($string, 'warning', $verbosity); | ||
} | ||
|
||
/** | ||
* Write a string in an alert box. | ||
* | ||
* @param string $string | ||
* @return void | ||
*/ | ||
public function alert($string) | ||
{ | ||
$length = Str::length(strip_tags($string)) + 12; | ||
|
||
$this->comment(str_repeat('*', $length)); | ||
$this->comment('* '.$string.' *'); | ||
$this->comment(str_repeat('*', $length)); | ||
|
||
$this->output->newLine(); | ||
} | ||
|
||
/** | ||
* Set the input interface implementation. | ||
* | ||
* @param \Symfony\Component\Console\Input\InputInterface $input | ||
* @return void | ||
*/ | ||
public function setInput(InputInterface $input) | ||
{ | ||
$this->input = $input; | ||
} | ||
|
||
/** | ||
* Set the output interface implementation. | ||
* | ||
* @param \Illuminate\Console\OutputStyle $output | ||
* @return void | ||
*/ | ||
public function setOutput(OutputStyle $output) | ||
{ | ||
$this->output = $output; | ||
} | ||
|
||
/** | ||
* Set the verbosity level. | ||
* | ||
* @param string|int $level | ||
* @return void | ||
*/ | ||
protected function setVerbosity($level) | ||
{ | ||
$this->verbosity = $this->parseVerbosity($level); | ||
} | ||
|
||
/** | ||
* Get the verbosity level in terms of Symfony's OutputInterface level. | ||
* | ||
* @param string|int|null $level | ||
* @return int | ||
*/ | ||
protected function parseVerbosity($level = null) | ||
{ | ||
if (isset($this->verbosityMap[$level])) { | ||
$level = $this->verbosityMap[$level]; | ||
} elseif (! is_int($level)) { | ||
$level = $this->verbosity; | ||
} | ||
|
||
return $level; | ||
} | ||
|
||
/** | ||
* Get the output implementation. | ||
* | ||
* @return \Illuminate\Console\OutputStyle | ||
*/ | ||
public function getOutput() | ||
{ | ||
return $this->output; | ||
} | ||
} |