Symfony进程-从服务执行命令

7
如何通过 new Process 在服务中运行命令 (app/console execute:my:command)?
我尝试了以下代码:
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

$process = new Process(
    'app/console execute:my:command'
);
$process->start();

但是什么都没有发生... 如果我通过终端手动调用它,它就可以工作:

app/console execute:my:command

我做错了什么?

EDIT - Solution: We need to write the whole path. In my case:

($this->kernelRootDir is : %kernel.root_dir%)
$processString = sprintf(
    'php %s/../app/console %s %s',
    $this->kernelRootDir,
    self::MY_COMMAND,
    $myArgument
);

$process = new Process($processString);
$process->start();

if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

你尝试过在 app/console 后面添加 PHP cli 路径吗? - Weenesta - Mathieu Dormeval
2个回答

12

这基本上就是你需要做的了。我总是设置工作目录并假设这是必要的,以便 Symfony 命令从项目根目录运行。

$process = new Process(
    'app/console execute:my:command'
);
$process->setWorkingDirectory(getcwd() . "../");

$process->start();

为了调试目的,我通常会设置

$process->setOptions(['suppress_errors' => false]);

同样也


0
我在Windows上无法使setWorkingDirectory函数正常工作...
对于Symfony >= 4.4,fromShellCommandline函数对我来说在Windows上更好用:
$cmd = sprintf('php %s/bin/console %s', 
    $this->getParameter('kernel.project_dir'), 
    'app:my:command'
);

Process::fromShellCommandline($cmd)->run();

希望这能帮助到其他人!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接