/** * Return program options from all envs * @return array A hash of name:value options */ function getOptions() { $options = array(); switch ($this->env) { case self::ENV_WEB: foreach (OptionMap::getInstance() as $option) { if (isset($_REQUEST[$option[0]])) { $options[$option[0]] = empty($option[2]) ? FALSE : $_REQUEST[$option[0]]; } elseif (isset($_REQUEST[$option[1]])) { $options[$option[1]] = empty($option[2]) ? FALSE : $_REQUEST[$option[1]]; } } break; case self::ENV_CLI: default: $short = ''; $long = array(); foreach (OptionMap::getInstance() as $option) { if (empty($option[2])) { // no val $short .= $option[1]; $long[] = $option[0]; } else { $short .= $option[1] . ':'; $long[] = $option[0] . ':'; } } $options = getopt($short, $long); break; } return $options; }
/** * Initialise the Engine object from user input * @throw Exception * @private */ private function startup() { // =================== CONFIGURE LOGFILE ==================== if ($this->environment->isCli()) { // TODO: Pass in debuglog filepath here $logfile = './poop.log'; } else { $logfile = sys_get_temp_dir() . '/poop.log'; } Log::instance()->setFilepath($logfile); // ================ PROCESS STARTUP OPTIONS ================= /* $opt = [0] => file [1] => f [2] => 1 [3] => Path to code file */ foreach (OptionMap::getInstance() as $opt) { switch ($opt[0]) { case 'code': // Code was passed as --code argument if ($option = $this->getOption(array($opt[0], $opt[1]))) { if ($option === true) { // empty --code arg passed $this->setBuffer(''); } else { $this->setBuffer($option); } } break; case 'file': if ($option = $this->getOption(array($opt[0], $opt[1]))) { if (!is_readable($option)) { throw new OptionException('--file not readable: ' . $option); } $this->setBuffer(file_get_contents($option)); } break; case 'debug': if ($option = $this->getOption(array($opt[0], $opt[1]))) { Log::instance()->enableDebug(); } break; default: break; } } // $this->dump(); // ================== INITIAL LOGFILE OUTPUT ====================== $this->debug(__METHOD__, __LINE__); $this->debug(__METHOD__, __LINE__, '======[ O_O ]======'); $this->debug(__METHOD__, __LINE__); $this->debug(__METHOD__, __LINE__, "Using logfile: {$logfile}"); if (Log::instance()->debugEnabled()) { $this->debug(__METHOD__, __LINE__, "Debugging is ENABLED"); } // ================== LOAD EXTENSIONS ====================== $total = $this->getExtensionManager()->load(); $this->debug(__METHOD__, __LINE__, sprintf('Loaded %u extensions', $total)); }
/** * Output program usage text */ public function doHelp() { $cli = $this->environment->isCli(); $buffer = sprintf('%s Usage: %s%s%soption1%s=arg1%s%soption2%s=arg2%s%s ... Options: ', $cli ? '' : '<pre>', $cli ? basename($GLOBALS['argv'][0]) : basename($_SERVER['SCRIPT_FILENAME']), $cli ? ' ' : '?', $cli ? '[' : '', $cli ? '[' : '', $cli ? ']' : '', $cli ? '] [' : '&', $cli ? '[' : '', $cli ? ']' : '', $cli ? ']' : ''); foreach (OptionMap::getInstance() as $option) { if (empty($option[2])) { $buffer2 = $option[0]; } else { $buffer2 = $option[0] . '=' . mb_strtoupper($option[0]); } $buffer .= sprintf(' %s%s %s%-10s %s%s', $cli ? '-' : '$', $option[1], $cli ? '--' : '$', $buffer2, $option[3], PHP_EOL); } $buffer .= sprintf(' %s', $cli ? '' : '</pre>'); // Print output echo $buffer; }