我在Symfony2中有一些控制台命令,我需要从一个带有一些参数的命令执行一个命令.
在成功执行第二个命令后,我需要获得结果(例如,作为数组),而不是显示输出.
我怎样才能做到这一点?
j0k*_*j0k 25
在这里,您可以在命令中使用基本命令.第二个命令的输出可以是json,然后你只需要解码输出json来检索你的数组.
$command = $this->getApplication()->find('doctrine:fixtures:load');
$arguments = array(
//'--force' => true
''
);
$input = new ArrayInput($arguments);
$returnCode = $command->run($input, $output);
if($returnCode != 0) {
$text .= 'fixtures successfully loaded ...';
$output = json_decode(rtrim($output));
}
Run Code Online (Sandbox Code Playgroud)
小智 22
你必须在arguments数组中传递命令,并避免在doctrine中的确认对话框:fixtures:load你必须通过--append而不是--force
$arguments = array(
'command' => 'doctrine:fixtures:load',
//'--append' => true
''
);
Run Code Online (Sandbox Code Playgroud)
或者它将失败并显示错误消息"参数不足".
One*_*ema 11
有一个新的Output类(从v2.4.0开始)调用BufferedOutput.
这是一个非常简单的类,它将在fetch调用方法时返回并清除缓冲的输出:
$output = new BufferedOutput();
$input = new ArrayInput($arguments);
$code = $command->run($input, $output);
if($code == 0) {
$outputText = $output->fetch();
echo $outputText;
}
Run Code Online (Sandbox Code Playgroud)