Symfony Process - 从服务执行命令

gol*_*ife 5 php console command process symfony

如何通过新进程在服务中运行命令(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();
Run Code Online (Sandbox Code Playgroud)

但没有任何反应......如果我通过终端手动调用它,它可以工作:

app/console execute:my:command
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

编辑 - 解决方案:我们需要编写整个路径.就我而言:

($this->kernelRootDir is : %kernel.root_dir%)
Run Code Online (Sandbox Code Playgroud)
$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);
}
Run Code Online (Sandbox Code Playgroud)

Pur*_*gon 7

这几乎是你真正需要做的.我总是设置工作目录并假设这是必需的,以便从项目的根目录运行Symfony命令

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

$process->start();
Run Code Online (Sandbox Code Playgroud)

出于调试目的,我通常会设置

$process->setOptions(['suppress_errors' => false]);
Run Code Online (Sandbox Code Playgroud)

同样