Brian Wigginton
Brian Wigginton

Reputation: 2652

How can I capture output from a CakePHP Shell

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.

Upvotes: 3

Views: 2039

Answers (1)

uzyn
uzyn

Reputation: 6683

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');
    }
}

Upvotes: 2

Related Questions