2

从 2.1 升级到 2.2 时我遇到了问题

在我的动作控制器中,我正在调用控制台命令并从这样的命令中获取输出。

$input = new ArgvInput(array(
                                'object_id' => $object_id,
                                'client_id' => $client_id,
                                'email_address' => $email
                                )
                           );

    $output = new ConsoleOutput();

    $command = $this->get('mycommand');
    $returnCode = $command->run($input, $output);

    $response = stream_get_contents($output->getStream());

它在 symfony 2.1 中工作,但首先升级到 2.2 后,我得到以下异常“没有足够的参数。”。为了防止这种情况,我在其他人面前添加了一个虚拟参数。

但在此之后命令执行,但我无法读取输出,它总是空的。

有什么解决办法吗?

4

2 回答 2

4

Symfony 2.4 分支添加了一个 BufferedOutput,它完全符合您的要求。

    $input = new ArgvInput(array());
    $output = new BufferedOutput();

    $command = $this->get("command");
    $command->run($input, $output);

    $content = $output->fetch();
于 2014-09-02T10:55:25.883 回答
2

我发现以下要点 将 ConsoleOutput 替换为以下MemoryWriter 类来解决问题。

它还建议使用 Symfony\Bundle\FrameworkBundle\Console\Application 类来避免将命令创建为服务:

$application = new Application($this->getContainer()->get('kernel'));
$application->setAutoExit(false); 

// The input interface should contain the command name, and whatever arguments the command needs to run      
$input = new ArrayInput(array("doctrine:schema:update"));

// Run the command
$retval = $application->run($input, $output);

var_dump($output->getOutput());
于 2013-07-23T09:31:48.153 回答