-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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 <[email protected]>
- Loading branch information
Showing
2 changed files
with
94 additions
and
77 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,92 @@ | ||
<?php | ||
|
||
namespace Illuminate\Console\Concerns; | ||
|
||
use Symfony\Component\Console\Input\ArrayInput; | ||
use Symfony\Component\Console\Output\NullOutput; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
trait CallOtherCommands | ||
{ | ||
/** | ||
* 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 | ||
); | ||
} | ||
|
||
/** | ||
* 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); | ||
} |