【发布时间】:2014-12-01 15:21:16
【问题描述】:
我正在使用 Symfony 2.0。
我在 Symfony 中创建了一个命令,我想获取它的输出并将其写入文件。
我想要的只是把所有写在标准输出(在控制台上)的东西都放在一个变量中。我的意思是命令中回显的东西,在其他文件中捕获的异常,由命令调用等等。我想要屏幕和变量中的输出(以便将变量的内容写入文件中)。我将在命令的execute() 方法的末尾写入文件。
类似这样的:
protected function execute(InputInterface $input, OutputInterface $output)
{
// some logic and calls to services and functions
echo 'The operation was successful.';
$this->writeLogToFile($file, $output???);
}
在我想要的文件中:
[Output from the calls to other services, if any]
The operation was successful.
你能帮帮我吗?
我尝试过这样的事情:
$stream = $output->getStream();
$content = stream_get_contents($stream, 5);
但是命令并没有以这种方式完成。 :(
【问题讨论】:
-
您可以编写自己的 Base
Application类并实现实现OutputInterface的编写器,您可以看到 here 的内容
标签: php symfony stream command