如何将命令的输出作为另一个的输入传递?

问题描述

我正在使用Symfony控制台应用程序,并试图将PHPstan的输出作为控制台命令的输入:

vendor/bin/PHPstan analyse | bin/wte analyse

当前命令按预期运行,但是如何将PHPstan的输出传递到bin/wte analyse命令中?

// AnalyseCommand.PHP 
final class AnalyseCommand extends Command
{
    public const NAME = 'analyse';

    /**
     * @var SymfonyStyle
     */
    private $symfonyStyle;

    public function __construct(SymfonyStyle $symfonyStyle)
    {
        $this->symfonyStyle = $symfonyStyle;
        parent::__construct();
    }

    public function execute(InputInterface $input,OutputInterface $output): int
    {
        // Get PHPstan output in here. 

        return ShellCode::SUCCESS;
    }

    protected function configure(): void
    {
        $this->setName(self::NAME);
        $this->setDescription('Find an error');

    }
}

解决方法

Symfony控制台没有任何处理标准输入的功能。但这不是必需的,因为您仍在使用PHP,并且可以使用任何native features

public function execute(InputInterface $input,OutputInterface $output): int
{
    $stdin = '';

    while (!feof(STDIN)) {
       $stdin .= fread(STDIN,1024);
    } 
    
    // do what you need with $stdin
}