有什么方法可以在 CakePHP 中捕获 shell 的输出吗?
我编写了一些 shell,可以为 CakePHP 2.x 应用程序生成报告。我能够在命令行上运行 shell 并查看输出,但是,现在我想通过电子邮件发送这些 shell 的结果。
我考虑过使用另一个 shell 作为包装器,然后使用 $this->dispatchShell('shellname')
来捕获它的输出,但似乎 dispatchShell
只是运行shell 并将其输出转储到 CLI。
最佳答案
要将 Shell 输出到文件,请在 Shell 的构造函数中声明一个输出流。下面是一个将标准输出作为日志文件的示例,该文件位于 CakePHP 的 TMP
目录(通常是 app/tmp/
)中名为 shell.out
的文件中:
<?php
class FooShell extends AppShell {
public function __construct($stdout = null, $stderr = null, $stdin = null) {
// This will cause all Shell outputs, eg. from $this->out(), to be written to
// TMP.'shell.out'
$stdout = new ConsoleOutput('file://'.TMP.'shell.out');
// You can do the same for stderr too if you wish
// $stderr = new ConsoleOutput('file://'.TMP.'shell.err');
parent::__construct($stdout, $stderr, $stdin);
}
public function main() {
// The following output will not be printed on your console
// but be written to TMP.'shell.out'
$this->out('Hello world');
}
}
关于cakephp - 如何捕获 CakePHP Shell 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11462829/