How to pass the output of command as the input of another?

530 Views Asked by At

I'm using a Symfony console application and trying to passed the output of phpstan as the input of my console command:

vendor/bin/phpstan analyse | bin/wte analyse

Currently the commands run as expected, but how can I pass the output of phpstan into the bin/wte analyse command?

// 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');

    }
}
1

There are 1 best solutions below

0
yivi On

Symfony Console doesn't have any feature dealing with standard input. But it's not needed, because you are still on PHP and can use any native features.

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

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