Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取Symfony命令的输出并将其保存到文件中_Php_Symfony_Stream_Command - Fatal编程技术网

Php 获取Symfony命令的输出并将其保存到文件中

Php 获取Symfony命令的输出并将其保存到文件中,php,symfony,stream,command,Php,Symfony,Stream,Command,我正在使用Symfony2.0 我已经在Symfony中创建了一个命令,我想获取它的输出并将其写入一个文件 我所要做的就是把标准输出(控制台上)上写的所有内容都写在一个变量中。我指的是命令中的响应,在命令调用的其他文件中捕获的异常,等等。我希望在屏幕上和变量中都有输出(以便将变量的内容写入文件)。我将在命令的execute()方法的末尾写入文件 大概是这样的: protected function execute(InputInterface $input, OutputInterface $o

我正在使用Symfony2.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???);
}
   $stream  = $output->getStream();
   $content = stream_get_contents($stream, 5);
在我想要的文件中:

[Output from the calls to other services, if any]
The operation was successful.
你能帮帮我吗

我试过这样的方法:

protected function execute(InputInterface $input, OutputInterface $output)
{
    // some logic and calls to services and functions
    echo 'The operation was successful.';

    $this->writeLogToFile($file, $output???);
}
   $stream  = $output->getStream();
   $content = stream_get_contents($stream, 5);

但是命令并没有以这种方式结束(

您可以使用标准的shell方法通过
php app/console:command>output.log
转发命令输出。或者,如果这不是一个选项,您可以为
OutputInterface
引入一个包装器,该包装器将写入流,然后将调用转发到被包装的输出。

在我的工作中,我需要同样的东西在这种情况下,我想通过电子邮件将用于调试和审计的控制台输出发送到电子邮件,因此我制作了一个非PHP类包装器,它存储行数据,然后传递到原始输出实例,这只适用于PHP7+

protected function execute(InputInterface $input, OutputInterface $output) {
    $loggableOutput = new class {
        private $linesData;
        public $output;

        public function write($data) {
            $this->linesData .= $data;
            $this->output->write($data);
        }

        public function writeln($data) {
            $this->linesData .= $data . "\n";
            $this->output->writeln($data);
        }

        public function getLinesData() {
            return $this->linesData;
        }
    };

    $loggableOutput->output = $output;

    //do some work with output

    var_dump($loggableOutput->getLinesData());
}

注意:这将只存储使用
write
writeln
OutputInterface方法写入的数据,不会存储任何PHP警告等。

很抱歉再次出现此问题。 我也遇到了类似的情况,如果您浏览Symfony版本(2.7以后)的代码,就会发现一个错误

您可以很容易地根据您的具体问题进行调整:

    // use Symfony\Component\Console\Output\BufferedOutput;
    // You can use NullOutput() if you don't need the output
    $output = new BufferedOutput();
    $application->run($input, $output);

    // return the output, don't use if you used NullOutput()
    $content = $output->fetch();

这应该可以很好地解决问题。

您可以编写自己的基本
应用程序
类,并实现实现
输出接口的writer
您可以看到一些不幸的事情,管道无法工作,因为Symfony似乎以不同的方式输出内容。。。