php - 如何将命令的输出作为另一个命令的输入?

标签 php symfony stdin symfony-console

我正在使用 Symfony 控制台应用程序并尝试将 phpstan 的输出作为控制台命令的输入传递:

vendor/bin/phpstan analyse | bin/wte analyse

目前命令按预期运行,但如何将 phpstan 的输出传递到 bin/wte analysis 命令中?

// 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 Console 没有任何处理标准输入的功能。但这不是必需的,因为您仍在使用 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
}

关于php - 如何将命令的输出作为另一个命令的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63871805/

相关文章:

session - 从 stdin 读取时防止 vim 加载 session

php - 在 Woocommerce 的管理编辑订单页面上显示客户来源国家/地区

php - 从数据库中获取行式数据

php - 如何在 LIMIT 子句中应用 bindValue 方法?

php - 如何在 Doctrine2 查询生成器中使用 'interval'

symfony - 如何在Symfony2中隐藏E_USER_NOTICE错误

c - 使用 stdin 读取单列数据文件并使用 C 中的函数值返回值

php - 每个论坛最新帖子的 MySQL 查询设计

validation - 将验证器添加到自定义表单类型

python - 从管道读取 Python 标准输入,不阻塞空输入