【问题标题】:Symfony Process Component - Set Input Argument for commandSymfony 进程组件 - 为命令设置输入参数
【发布时间】:2016-12-24 09:45:57
【问题描述】:

我需要异步运行命令。为此,我正在尝试使用流程组件。

我试图启动的命令是调用一个需要一些参数的函数。这些参数由启动进程的控制器给出。

问题是我不知道如何使用 Process Component 将参数传递给我的命令。

命令:

protected function configure()
{
    $this
        ->setName('generationpdf:classement')
        ->setDescription('Génère le PDF d\'un classement.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
    ini_set('memory_limit','4000M');
    $pdf = $this->imprimerAction();
    $this->sendClassement($pdf);
    $output->writeln('PDF envoyé.');
}

控制器:

        $command = new PDFClassementCommand();
        $input = new ArrayInput(array('id' => $id, 'errata' => $errata, 'precision' => $precision, 'group' => $group, 'logoAnnee' => $logoAnnee));
        $output = new NullOutput();

        $process = new Process($command);
        $process->disableOutput();
        $process->start();

我需要使用的参数在 ArrayInput 但 Process 不接受数组参数。

【问题讨论】:

    标签: php asynchronous command symfony


    【解决方案1】:

    process 期望第一个参数是字符串。

    例子:

    $process = new Process('ls');
    

    要运行命令,您需要执行 symfony 控制台命令

    $process = new Process('/var/www/my-project/bin/console generationpdf:classement')
    

    您不必对路径进行硬编码,请参阅how-to-get-the-root-dir

    你可以正常传递parameters,就像从cli运行命令一样

    $process = new Process('/var/www/my-project/bin/console generationpdf:classement --id=5 --errata=someValue')
    

    【讨论】:

    • 我按照你说的做了,我的命令在终端中按预期运行。我尝试使用 Process 但它不起作用。 $rootDir = $this->get('kernel')->getRootDir(); $process = new Process('php '.$rootDir.'/bin/console generationpdf:classement '.$id.' '.$group.' '.$logoAnnee.' '.$errata.' '.$precision.''); $process->start(); 进程已启动但未成功。
    • $process->run() 正在工作,但它不在后台执行命令。
    • 您能否等待该过程完成,使用start(),然后检查输出和错误输出。失败的过程应该有错误输出,对吧?有关实施,请参阅docs
    • 我刚刚在日志中看到这些行:[2016-08-18 19:45:50] php.DEBUG: unlink(C:\WINDOWS\TEMP\sf_proc_00.out): Permission denied {"type":2,"file":"\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\Process\\Pipes\\WindowsPipes.php","line":190,"level":28928} [][2016-08-18 19:45:50] php.DEBUG: unlink(C:\WINDOWS\TEMP\sf_proc_00.err): Permission denied {"type":2,"file":"\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\Process\\Pipes\\WindowsPipes.php","line":190,"level":28928} []
    • 当我添加这些行时:$process->start(); $process->wait() 它也在工作,但它阻塞了主进程。由于某些权限,它看起来不想进入后台,但我不知道如何解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 2017-06-10
    • 2023-03-07
    相关资源
    最近更新 更多