laravel-6 support
This commit is contained in:
@@ -2,22 +2,17 @@
|
||||
|
||||
namespace Illuminate\Console;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Traits\Macroable;
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Symfony\Component\Console\Helper\Table;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Output\NullOutput;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
use Symfony\Component\Console\Command\Command as SymfonyCommand;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
||||
use Symfony\Component\Console\Command\Command as SymfonyCommand;
|
||||
|
||||
class Command extends SymfonyCommand
|
||||
{
|
||||
use Macroable;
|
||||
use Concerns\CallsCommands,
|
||||
Concerns\HasParameters,
|
||||
Concerns\InteractsWithIO,
|
||||
Macroable;
|
||||
|
||||
/**
|
||||
* The Laravel application instance.
|
||||
@@ -26,20 +21,6 @@ class Command extends SymfonyCommand
|
||||
*/
|
||||
protected $laravel;
|
||||
|
||||
/**
|
||||
* The input interface implementation.
|
||||
*
|
||||
* @var \Symfony\Component\Console\Input\InputInterface
|
||||
*/
|
||||
protected $input;
|
||||
|
||||
/**
|
||||
* The output interface implementation.
|
||||
*
|
||||
* @var \Illuminate\Console\OutputStyle
|
||||
*/
|
||||
protected $output;
|
||||
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
@@ -57,10 +38,17 @@ class Command extends SymfonyCommand
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
* @var string|null
|
||||
*/
|
||||
protected $description;
|
||||
|
||||
/**
|
||||
* The console command help text.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $help;
|
||||
|
||||
/**
|
||||
* Indicates whether the command should be shown in the Artisan command list.
|
||||
*
|
||||
@@ -68,26 +56,6 @@ class Command extends SymfonyCommand
|
||||
*/
|
||||
protected $hidden = false;
|
||||
|
||||
/**
|
||||
* 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,
|
||||
];
|
||||
|
||||
/**
|
||||
* Create a new console command instance.
|
||||
*
|
||||
@@ -107,7 +75,9 @@ class Command extends SymfonyCommand
|
||||
// Once we have constructed the command, we'll set the description and other
|
||||
// related properties of the command. If a signature wasn't used to build
|
||||
// the command we'll set the arguments and the options on this command.
|
||||
$this->setDescription($this->description);
|
||||
$this->setDescription((string) $this->description);
|
||||
|
||||
$this->setHelp((string) $this->help);
|
||||
|
||||
$this->setHidden($this->isHidden());
|
||||
|
||||
@@ -134,25 +104,6 @@ class Command extends SymfonyCommand
|
||||
$this->getDefinition()->addOptions($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the arguments and options on the command.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function specifyParameters()
|
||||
{
|
||||
// We will loop through all of the arguments and options for the command and
|
||||
// set them all on the base command instance. This specifies what can get
|
||||
// passed into these commands as "parameters" to control the execution.
|
||||
foreach ($this->getArguments() as $arguments) {
|
||||
call_user_func_array([$this, 'addArgument'], $arguments);
|
||||
}
|
||||
|
||||
foreach ($this->getOptions() as $options) {
|
||||
call_user_func_array([$this, 'addOption'], $options);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the console command.
|
||||
*
|
||||
@@ -184,381 +135,28 @@ class Command extends SymfonyCommand
|
||||
}
|
||||
|
||||
/**
|
||||
* Call another console command.
|
||||
* Resolve the console command instance for the given command.
|
||||
*
|
||||
* @param string $command
|
||||
* @param array $arguments
|
||||
* @return int
|
||||
* @param \Symfony\Component\Console\Command\Command|string $command
|
||||
* @return \Symfony\Component\Console\Command\Command
|
||||
*/
|
||||
public function call($command, array $arguments = [])
|
||||
protected function resolveCommand($command)
|
||||
{
|
||||
$arguments['command'] = $command;
|
||||
|
||||
return $this->getApplication()->find($command)->run(
|
||||
$this->createInputFromArguments($arguments), $this->output
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call another console command silently.
|
||||
*
|
||||
* @param string $command
|
||||
* @param array $arguments
|
||||
* @return int
|
||||
*/
|
||||
public function callSilent($command, array $arguments = [])
|
||||
{
|
||||
$arguments['command'] = $command;
|
||||
|
||||
return $this->getApplication()->find($command)->run(
|
||||
$this->createInputFromArguments($arguments), new NullOutput
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an input instance from the given arguments.
|
||||
*
|
||||
* @param array $arguments
|
||||
* @return \Symfony\Component\Console\Input\ArrayInput
|
||||
*/
|
||||
protected function createInputFromArguments(array $arguments)
|
||||
{
|
||||
return tap(new ArrayInput(array_merge($this->context(), $arguments)), function ($input) {
|
||||
if ($input->hasParameterOption(['--no-interaction'], true)) {
|
||||
$input->setInteractive(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the context passed to the command.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function context()
|
||||
{
|
||||
return collect($this->option())->only([
|
||||
'ansi',
|
||||
'no-ansi',
|
||||
'no-interaction',
|
||||
'quiet',
|
||||
'verbose',
|
||||
])->filter()->mapWithKeys(function ($value, $key) {
|
||||
return ["--{$key}" => $value];
|
||||
})->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
if (! class_exists($command)) {
|
||||
return $this->getApplication()->find($command);
|
||||
}
|
||||
|
||||
return $this->input->getArgument($key);
|
||||
}
|
||||
$command = $this->laravel->make($command);
|
||||
|
||||
/**
|
||||
* 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();
|
||||
if ($command instanceof SymfonyCommand) {
|
||||
$command->setApplication($this->getApplication());
|
||||
}
|
||||
|
||||
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 $choices
|
||||
* @param string|null $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function anticipate($question, array $choices, $default = null)
|
||||
{
|
||||
return $this->askWithCompletion($question, $choices, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prompt the user for input with auto completion.
|
||||
*
|
||||
* @param string $question
|
||||
* @param array $choices
|
||||
* @param string|null $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function askWithCompletion($question, array $choices, $default = null)
|
||||
{
|
||||
$question = new Question($question, $default);
|
||||
|
||||
$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();
|
||||
if ($command instanceof self) {
|
||||
$command->setLaravel($this->getLaravel());
|
||||
}
|
||||
|
||||
$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 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;
|
||||
return $command;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -579,36 +177,6 @@ class Command extends SymfonyCommand
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command arguments.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getArguments()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the output implementation.
|
||||
*
|
||||
* @return \Illuminate\Console\OutputStyle
|
||||
*/
|
||||
public function getOutput()
|
||||
{
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Laravel application instance.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user