【问题标题】:Calling console command within command and get the output in Symfony2在命令中调用控制台命令并在 Symfony2 中获取输出
【发布时间】:2013-01-21 08:51:22
【问题描述】:

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

成功执行第二条命令后我需要得到结果(例如数组),而不是显示输出。

我该怎么做?

【问题讨论】:

    标签: php symfony


    【解决方案1】:

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

    【讨论】:

    • 如果我在依赖命令OutputInterface 中使用writeln 方法会怎样,所以我有一些字符串,我无法解码。
    • writeLn 只需添加一个换行符,我想你只需要trimrtrim 的结果。
    • 我不想解析结果,我正在寻找另一种方式。或许,我没听懂你的意思。
    • 您在第一个命令中修剪输出,而不是在第二个命令中。我已经更新了我的答案。
    • 第一个命令中的 var_dump($output) 表示这是Symfony\Component\Console\Output\ConsoleOutput 的一个实例。其实我早就料到了这样的结果,没能理解你。
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

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

          $output = new BufferedOutput();
          $input = new ArrayInput($arguments);
          $code = $command->run($input, $output);
      
          if($code == 0) {
              $outputText = $output->fetch();
              echo $outputText;
          } 
      

      【讨论】:

        【解决方案4】:

        我做了以下

        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;
        

        ¡有效!

        【讨论】:

          【解决方案5】:

          我知道这是旧帖子,上面的答案通过一些挖掘解决了这个问题。在 Symfony2.7 中,我在让它工作时遇到了一点问题,所以根据上面的建议,我挖了一点,在这里编译了完整的答案。希望它对某人有用。

          Using Console command under console command

          【讨论】:

            【解决方案6】:

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

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

            这是我用于在 php 中为 Drupal 8.8 项目编写 composer 命令脚本的更新示例。这会以 json 格式获取所有作曲家包的列表,然后将其解码为 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's hint 的帮助下,Google 为我找到了其余的解决方案here

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-02-23
              • 2014-07-14
              • 1970-01-01
              • 1970-01-01
              • 2012-07-20
              • 2013-04-26
              相关资源
              最近更新 更多