【问题标题】:Symfony 4 Component Process pass arguments for commandSymfony 4 组件进程传递命令的参数
【发布时间】:2020-08-06 19:34:38
【问题描述】:

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 要运行的命令及其参数作为单独的条目列出
公共函数 __construct($command, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60) { .. }

【问题讨论】:

    标签: command-line command symfony4


    【解决方案1】:

    $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();
       }
    

    【讨论】:

      【解决方案2】:
       $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 版本,请使用它

      【讨论】:

        猜你喜欢
        • 2016-12-24
        • 2019-07-01
        • 1970-01-01
        • 2018-05-12
        • 2011-02-16
        • 2016-03-08
        • 1970-01-01
        • 2012-03-25
        • 1970-01-01
        相关资源
        最近更新 更多