如何从CakePHP Shell捕获输出

时间:2012-07-13 01:13:00

标签: cakephp cakephp-2.0

有没有办法在CakePHP中捕获shell的输出?

我编写了一些shell,为CakePHP 2.x应用程序生成报告。我能够在命令行上运行shell并查看输出,但是,现在我想通过电子邮件发送这些shell的结果。

我考虑过使用另一个shell作为包装器然后使用$this->dispatchShell('shellname')捕获其输出,但似乎dispatchShell只运行shell并将其输出转储到CLI。

1 个答案:

答案 0 :(得分:2)

要将Shell输出到文件,请在Shell的构造函数中声明输出流。这是一个示例,让stdout成为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');
    }
}
相关问题