2016-08-17 53 views
2

我需要异步运行一个命令。为此,我试图使用Process Component。Symfony过程组件 - 设置命令的输入参数

我试着开始的命令是调用一个需要一些参数的函数。这些参数由启动Process的Controller给出。

问题是我不知道如何使用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,但过程并不需要数组参数。

回答

1

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

例子:

$process = new Process('ls'); 

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

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

您没有硬编码的路径,看到how-to-get-the-root-dir

您可以从CLI

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

我确实通过parameters正常,就像运行命令你说的话,并如预期在终端我的命令运行。 我尝试使用Process,但它不起作用。 '$ rootDir = $ this-> get('kernel') - > getRootDir();' '$ process = new Process('php'。$ rootDir。'/ bin/console generationpdf:classement'。$ $'$。$。'$。''。$'precision。'');' '$ process-> start();' 过程已启动,但是不成功。 –

+0

'$ process-> run()'正在工作,但它不会在后台执行命令。 –

+0

您可以使用'start()'等待进程完成,然后检查输出和错误输出。应该有失败进程的错误输出,对吧?有关实施,请参阅[文档](http://symfony.com/doc/current/components/process.html#running-processes-asynchronously)。 –

相关问题