From 6a7955627cfc9f4476657db82071540a27184b3a Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sat, 4 Jan 2020 13:15:12 +0800 Subject: [PATCH] [6.x] Extract CallOtherCommands feature from Illuminate\Console\Command At the moment Illuminate\Console\Command is tightly couple with Laravel via $this->laravel->make() usage, this changes make it possible to reuse CallOtherCommands feature available for Laravel Command to standalone Symfony command. Signed-off-by: Mior Muhammad Zaki --- src/Illuminate/Console/Command.php | 79 +--------------- .../Console/Concerns/CallOtherCommands.php | 92 +++++++++++++++++++ 2 files changed, 94 insertions(+), 77 deletions(-) create mode 100644 src/Illuminate/Console/Concerns/CallOtherCommands.php diff --git a/src/Illuminate/Console/Command.php b/src/Illuminate/Console/Command.php index dbfd2f65a07d..7d714f47b8ae 100755 --- a/src/Illuminate/Console/Command.php +++ b/src/Illuminate/Console/Command.php @@ -8,18 +8,17 @@ use Symfony\Component\Console\Command\Command as SymfonyCommand; use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Symfony\Component\Console\Helper\Table; -use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ChoiceQuestion; use Symfony\Component\Console\Question\Question; class Command extends SymfonyCommand { - use Macroable; + use Concerns\CallOtherCommands, + Macroable; /** * The Laravel application instance. @@ -202,47 +201,6 @@ protected function execute(InputInterface $input, OutputInterface $output) return $this->laravel->call([$this, 'handle']); } - /** - * Call another console command. - * - * @param \Symfony\Component\Console\Command\Command|string $command - * @param array $arguments - * @return int - */ - public function call($command, array $arguments = []) - { - return $this->runCommand($command, $arguments, $this->output); - } - - /** - * Call another console command silently. - * - * @param \Symfony\Component\Console\Command\Command|string $command - * @param array $arguments - * @return int - */ - public function callSilent($command, array $arguments = []) - { - return $this->runCommand($command, $arguments, new NullOutput); - } - - /** - * Run the given the console command. - * - * @param \Symfony\Component\Console\Command\Command|string $command - * @param array $arguments - * @param \Symfony\Component\Console\Output\OutputInterface $output - * @return int - */ - protected function runCommand($command, array $arguments, OutputInterface $output) - { - $arguments['command'] = $command; - - return $this->resolveCommand($command)->run( - $this->createInputFromArguments($arguments), $output - ); - } - /** * Resolve the console command instance for the given command. * @@ -268,39 +226,6 @@ protected function resolveCommand($command) return $command; } - /** - * 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. * diff --git a/src/Illuminate/Console/Concerns/CallOtherCommands.php b/src/Illuminate/Console/Concerns/CallOtherCommands.php new file mode 100644 index 000000000000..62784d8f9c04 --- /dev/null +++ b/src/Illuminate/Console/Concerns/CallOtherCommands.php @@ -0,0 +1,92 @@ +runCommand($command, $arguments, $this->output); + } + + /** + * Call another console command silently. + * + * @param \Symfony\Component\Console\Command\Command|string $command + * @param array $arguments + * @return int + */ + public function callSilent($command, array $arguments = []) + { + return $this->runCommand($command, $arguments, new NullOutput); + } + + /** + * Run the given the console command. + * + * @param \Symfony\Component\Console\Command\Command|string $command + * @param array $arguments + * @param \Symfony\Component\Console\Output\OutputInterface $output + * @return int + */ + protected function runCommand($command, array $arguments, OutputInterface $output) + { + $arguments['command'] = $command; + + return $this->resolveCommand($command)->run( + $this->createInputFromArguments($arguments), $output + ); + } + + /** + * 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(); + } + + /** + * Resolve the console command instance for the given command. + * + * @param \Symfony\Component\Console\Command\Command|string $command + * @return \Symfony\Component\Console\Command\Command + */ + abstract protected function resolveCommand($command); +}