繁体   English   中英

在命令中调用控制台命令并在 Symfony2 中获取输出

[英]Calling console command within command and get the output in Symfony2

我在 Symfony2 中有一些控制台命令,我需要使用一些参数从另一个命令执行一个命令。

成功执行第二个命令后,我需要获取结果(例如作为数组) ,而不是显示输出。

我怎样才能做到这一点?

在这里,您可以在命令中包含一个基本命令。 第二个命令的输出可以是 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));
}

您必须在参数数组中传递命令,并且为了避免在 dotric:fixtures:load 中出现确认对话框,您必须传递 --append 而不是 --force

    $arguments = array(
    'command' => 'doctrine:fixtures:load',
    //'--append' => true
    ''
);

否则它将失败并显示错误消息“参数不足”。

有一个名为BufferedOutput的新 Output 类(从v2.4.0 开始)。

这是一个非常简单的类,它将在调用fetch方法时返回并清除缓冲的输出:

    $output = new BufferedOutput();
    $input = new ArrayInput($arguments);
    $code = $command->run($input, $output);

    if($code == 0) {
        $outputText = $output->fetch();
        echo $outputText;
    } 

我做了以下

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\StreamOutput;

$tmpFile = tmpfile();
$output  = new StreamOutput($tmpFile);
$input   = new ArrayInput(array(
    'parameter' => 'value',
));

$command = . . .
$command->run($input, $output);

fseek($tmpFile, 0);
$output = fread($tmpFile, 1024);
fclose($tmpFile);

echo $output;

有用!

我知道这是旧帖子,上面的答案通过一些挖掘解决了问题。 在 Symfony2.7 中,我在使其工作时遇到了一些问题,因此根据上述建议,我进行了一些挖掘并在此处编译了完整答案。 希望它对某人有用。

在控制台命令下使用控制台命令

作为Onema 答案的更新,在 Symfony 3.4.x(由 Drupal 8 使用)中,

  1. 您还需要设置setAutoExit(false)
  2. 如果成功,该命令将返回int(0)

这是我用来在 php 中为 Drupal 8.8 项目编写composer命令脚本的更新示例。 这会以 json 形式获取所有 Composer 包的列表,然后将其解码为 php 对象。

<?php 

require __DIR__.'/vendor/autoload.php';

use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Input\ArrayInput;
use Composer\Console\Application;

$input = new ArrayInput([
  'command' => 'show', 
  '--format'=>'json',
]);

$output = new BufferedOutput();
$application = new Application();

// required to use BufferedOutput()
$application->setAutoExit(false);
// composer package list, formatted as json, will be barfed into $output 
$status = $application->run($input, $output);
if($status === 0) {
  // grab the output from the $output buffer
  $json = $output->fetch();
  // decode the json string into an object
  $list = json_decode($json);
  // Profit!
  print_r($list);
}

?>

输出将是这样的:

stdClass Object
(
    [installed] => Array
        (
            ... omitted ...

            [91] => stdClass Object
                (
                    [name] => drupal/core
                    [version] => 8.9.12
                    [description] => Drupal is an open source content management platform powering millions of websites and applications.
                )
            ... omitted ...
        )

)

Onema 的提示的帮助下,谷歌在 这里为我找到了其余的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM