我目前正在Laravel 5.1项目中创建一个php artisan控制台命令,并希望从我的控制台命令调用另一个控制台命令。我要调用的第三方命令不接受任何选项或参数,而是通过交互式问题接收其输入。
我知道我可以用这样的选项和参数来调用命令:
$ this-> call('command:name',['argument'=>'foo',' - option'=>'bar']);
我也知道我可以在命令行中调用交互式命令而不进行这样的交互:
php artisan命令:name --no-interaction
但我怎样才能从命令中回答这些互动问题?
我想做类似下面的事情(伪代码)。
$this->call('command:name', [
'argument' => 'foo',
'--option' => 'bar'
], function($console) {
$console->writeln('Yes'); //answer an interactive question
$console-writeln('No'); //answer an interactive question
$console->writeln(''); //skip answering an interactive question
} );
当然上述方法不起作用,因为$this->call($command, $arguments)
不接受第三个回调参数。
如何从控制台命令调用控制台命令时回答交互式问题?
答案 0 :(得分:3)
我有另一个解决方案,就是调用执行'php artisan'的symfony命令,而不是使用artisan子命令。我认为这比修补第三方代码更好。
这是管理此事的特征。
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
trait ArtisanCommandTrait{
public function executeArtisanCommand($command, $options){
$stmt = 'php artisan '. $command . ' ' . $this->prepareOptions($options);
$process = new Process($stmt);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
return $process->getOutput();
}
public function prepareOptions($options){
$args = [];
$opts = [];
$flags = [];
foreach ($options as $key => $value) {
if(ctype_alpha(substr($key, 0, 1)))
$args[] = $value;
else if(starts_with($key, '--')){
$opts[] = $key. (is_null($value) ? '' : '=' . $value) ;
}
else if(starts_with($key, '-')){
$flags[] = $key;
}
}
return implode(' ', $args) . ' '
.implode(' ', $opts). ' '
.implode(' ', $flags);
}
}
现在,您应该能够通过任何工匠特殊选项,例如无互动。
public function handle(){
$options = [
'argument' => $argument,
'--option' => $options, // options should be preceded by --
'-n' => null // no-interaction option
];
$command = 'your:command';
$output = $this->executeArtisanCommand($command, $options);
echo $output;
}
您可以从此gist
下载特征答案 1 :(得分:1)
我是这样做的。
注意:这会修补核心Symfony类QuestionHelper@doAsk
,尽管此代码运行正常(我目前只是在进行概念验证),但此代码可能无法在任何生产环境中运行。
我还没有接受我自己的答案,想知道是否有更好的方法来做到这一点。
以下假定Laravel 5.1安装。
第一个作曲家 - 需要Patchwork包。我正在使用它来增强该Symfony类方法的功能。
composer require antecedent/patchwork
修改bootstrap/app.php
并在创建应用程序后立即添加以下内容。(拼凑不自动加载)
if($app->runningInConsole()) {
require_once(__DIR__ . '/../vendor/antecedent/patchwork/Patchwork.php');
};
将以下两个use语句添加到控制台命令类的顶部
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
通过在控制台命令类上使用这些帮助程序方法来增强/修补QuestionHelper@doAsk
public function __construct() {
parent::__construct();
$this->patchAskingQuestion();
}
/**
* Patch QuestionHelper@doAsk
* When a key 'qh-patch-answers' is found in the $_REQUEST superglobal,
* We assume this is an array which holds the answers for our interactive questions.
* shift each answer off the array, before answering the corresponding question.
* When an answer has a NULL value, we will just provide the default answer (= skip question)
*/
private function patchAskingQuestion() {
\Patchwork\replace('Symfony\Component\Console\Helper\QuestionHelper::doAsk', function(OutputInterface $output, Question $question) {
$answers = &$_REQUEST['qh-patch-answers'];
//No predefined answer found? Just call the original method
if(empty($answers)) {
return \Patchwork\callOriginal([$output, $question]);
}
//using the next predefined answer, or the default if the predefined answer was NULL
$answer = array_shift($answers);
return ($answer === null) ? $question->getDefault() : $answer;
});
}
private function setPredefinedAnswers($answers) {
$_REQUEST['qh-patch-answers'] = $answers;
}
private function clearPredefinedAnswers() {
unset($_REQUEST['qh-patch-answers']);
}
您现在可以回答此类互动问题
public function fire() {
//predefine the answers to the interactive questions
$this->setPredefinedAnswers([
'Yes', //first question will be answered with 'Yes'
'No', //second question will be answered with 'No'
null, //third question will be skipped (using the default answer)
null, //fourth question will be skipped (using the default answer)
]);
//call the interactive command
$this->call('command:name');
//clean up, so future calls to QuestionHelper@doAsk will definitely call the original method
$this->clearPredefinedAnswers();
}
答案 2 :(得分:1)
Laravel 5.7最终以内置方式处理交互式命令测试彻底解决了该问题!
答案 3 :(得分:0)
使用mpyw/streamable-console: Call interactive artisan command using arbitrary stream:
$this->usingInputStream("yes\nno\n")->call('command:name');