logger)) { try { return call_user_func_array (array ($this->logger (), $name), $arguments); } catch (Zend_Log_Exception $e) { } } } else { throw new Exception ("Invalid method `$name`."); } } function getCommandLineOptions () { } protected function isCli () { if ((PHP_SAPI == 'cgi-fcgi') && (defined ('WEB_PREFIX') == false)) { return true; } return (PHP_SAPI == 'cli'); } function run () { set_error_handler (array ($this, '_errorHandler')); error_reporting (E_ALL); try { if (! $this->isCli ()) { echo '
This script is not supposed to be executed from browser.
'; exit; } $this->logger (new Zend_Log ()); if ($this->getCreateLogDuringSetup ()) { $this->setupLogger (); } $this->parseCommandLineParameters (); $this->printUsage (); try { $this->body (); } catch (Exception_InvalidIniFile $e) { $this->fatal ('Invalid ini file.'); } } catch (Exception $e) { $this->fatal ($e); } } private function parseCommandLineParameters () { try { $options = new Zend_Console_Getopt ($this->getCommandLineOptions ()); $options->parse (); $this->setCmdLine ($options); } catch (Zend_Console_Getopt_Exception $e) { $this->fatal ($e->getUsageMessage ()); } } function _errorHandler ($errno, $errstr, $errfile, $errline) { if(defined('E_DEPRECATED') && $errno == E_DEPRECATED){ return false; } if (error_reporting () & $errno) { $this->err ("Error $errno. $errstr\n in file $errfile in line $errline\n"); exit; } } protected function setupLogger () { $screen = new Zend_Log_Writer_Stream ('php://output'); $screen->addFilter (new Zend_Log_Filter_Priority (Zend_Log::NOTICE)); $this->logger ()->addWriter ($screen); $log_filename = strtolower (preg_replace ('/.*_/', '', get_class ($this))); $log_filename .= ' '; $log_filename = LOGDIR . "/$log_filename" . date ('Y-m-d_H-i-s') . '.log'; $file = new Zend_Log_Writer_Stream ($log_filename); $this->logger ()->addWriter ($file); } protected function fatal ($note) { if (is_string ($note)) { $this->err ('Fatal error: ' . $note); } elseif (is_object ($note) && $note instanceof Exception) { $this->err ('Fatal error: ' . $note->getMessage () . ' in ' . $note->getFile() . ' line ' .$note->getLine ()); $this->notice ($note->getTraceAsString ()); } $this->notice ('Exiting...'); exit; } function setCmdLine (Zend_Console_Getopt $options) { $this->options = $options; } function getCmdLine () { return $this->options; } function isHelp () { return $this->getCmdLine ()->getOption ('help'); } function printUsage () { if ($this->isHelp ()) { echo $this->getCmdLine ()->getUsageMessage (); exit; } } function logger (Zend_Log $logger = null) { if (isset ($logger)) { $this->logger = $logger; } else { return $this->logger; } } function getHomeDir () { return $_SERVER ['HOME']; } function display ($string = "") { echo $string, "\n"; } function br () { return str_repeat ('-', 78); } function setCreateLogDuringSetup ($create_log_during_setup) { $this->create_log_during_setup = $create_log_during_setup; } function getCreateLogDuringSetup () { return $this->create_log_during_setup; } }