Symfony 4 Component Process 为命令传递参数

Symfony 4 Component Process pass arguments for command

Symfony 组件进程

/**
     * @param array          $command The command to run and its arguments listed as separate entries
     * @param string|null    $cwd     The working directory or null to use the working dir of the current PHP process
     * @param array|null     $env     The environment variables or null to use the same environment as the current PHP process
     * @param mixed|null     $input   The input as stream resource, scalar or \Traversable, or null for no input
     * @param int|float|null $timeout The timeout in seconds or null to disable
     *
     * @throws LogicException When proc_open is not installed
     */
    public function __construct($command, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60)
    { .. }

我创建了一个命令,并在我的控制器中使用了组件 Process。
当我尝试 运行 Process 我得到一个 ProcessFailException
命令“app:load-file foo bar foobar”失败
退出代码:1(一般错误)
.. ErrorOutput: 文件名、目录名或卷标语法不正确

use Symfony\Component\Process\Process;

..
public function loadFile(KernelInterface $kernel) 
{
   $argument1 = 'foo';
   $argument2 = 'bar';
   $argument3 = 'foobar';
   $rootDir = $kernel->getProjectDir();
   $process = new Process(array(
      $rootDir . '/bin/console app:load-file',
      $argument1,
      $argument2,
      $argument3
   ));
   
   $process->mustRun();
}

来自控制器的 运行 命令的正确语法是什么?
/* *@param array $command 运行 的命令及其参数列为单独的条目
public 函数 __construct($command, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60) { .. }

$cwd是构造函数的第二个参数

   $argument1 = 'foo';
   $argument2 = 'bar';
   $argument3 = 'foobar';
   $rootDir = $kernel->getProjectDir();
   $process = new Process(
      "bin/console app:load-file $argument1 $argument2 $argument3",
      $rootDir
   );

   try {
      $process->mustRun();
   } catch(ProcessFailedException $e) {
      echo $e->getMessage();
   }
 $argument1 = 'foo';
 $argument2 = 'bar';
 $argument3 = 'foobar';
 $rootDir = $kernel->getProjectDir();
 $process = new Process(
     [
         $_SERVER['_'],
         'bin/console'
         'app:load-file',
         $argument1,
         $argument2,
         $argument3,
     ],
     $rootDir
 );

$process->mustRun();

$_SERVER['_'] 包含 PHP 解释器可执行文件的路径。如果服务器上有多个 php 版本

,请使用它