質問

I have a CakePHP project with three shell scripts that will be run as cron jobs. The output of these cron jobs will be useful for debugging. We've decided to email this output to the support team. My plan is to create a class CronShell that extends AppShell then extend CronShell for any scripts that will be run as cron jobs. Here's the CronShell class:

class CronShell extends AppShell {
    private $_messages = array();

    public function out($message=null, $newlines=1, $level=Shell::NORMAL) {
        parent::out($message, $newlines, $level);
        $this->_messages[] = $message;
    }   

    public function __destruct() {
        App::uses('CakeEmail', 'Network/Email');
        $email = new CakeEmail('default');
        $email->to(Configure::read('support.addresses'));
        $email->subject(get_class($this).' output');
        $email->send(implode("\n", $this->_messages));
    }
}

I have a simple test implementation that inherits from CronShell:

class TestShell extends CronShell {

    public function startup() {
        // Omit the startup message.
        return;
    }   

    public function main() {
        $this->out('test');
        $this->out('out');
        $this->out('the');
        $this->out('class');
    }
}

But the output is:

PHP Fatal error:  Class 'CronShell' not found in...

How do I make TestShell aware of CronShell?

役に立ちましたか?

解決

Thanks to Abdou Tahiri and Kai for explaining what was missing. I just needed an App::uses call in the scripts that inherit from CronShell. Here's the working TestShell:

App::uses('CronShell', 'Console/Command');

class TestShell extends CronShell {

    public function startup() {
        // Omit the startup message.
        return;
    }

    public function main() {
        $this->out('test');
        $this->out('out');
        $this->out('the');
        $this->out('class');
    }

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top