How can I capture output from a CakePHP Shell

£可爱£侵袭症+ 提交于 2019-12-19 10:14:47

问题


Is there any way to capture a shell's output in CakePHP?

I've written some shells that generates reports for a CakePHP 2.x application. I'm able to run the shells on a command line and view the output, however, now I'd like to email out the results of those shells.

I thought about using another shell as a wrapper and then using $this->dispatchShell('shellname') to capture its output, but it seems dispatchShell just runs the shell and dumps it's output to the CLI.


回答1:


To have Shell output to a file, declare an output stream at your Shell's constructor. Here's an example to have stdout be a log file at your CakePHP's TMP dir (usually app/tmp/) on a file named 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');
    }
}


来源:https://stackoverflow.com/questions/11462829/how-can-i-capture-output-from-a-cakephp-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!