/** * */ static function run($class) { static $ic = 0; $args = func_get_args(); $args = array_slice($args, 1); Console::debugEx(LOG_EXTENDED, __CLASS__, "Inspecting environment module state:\n%s", ModuleManager::debug()); $ic++; if (class_exists($class)) { $rv = 0; $apptimer = new Timer(); try { $instance = new $class(); if (!$instance instanceof IApplication) { console::warn("FATAL: Application is not instance of IApplication"); return RETURN_ERROR; } Console::debugEx(LOG_BASIC, __CLASS__, "Invoking application instance from %s.", $class); $apptimer->start(); if (is_callable(array($instance, 'run'))) { $rv = call_user_func_array(array($instance, 'run'), $args); } else { console::writeLn("Requested application class %s is not runnable.", $class); } $apptimer->stop(); unset($instance); Console::debugEx(LOG_BASIC, __CLASS__, "Main method exited with code %d after %.2f seconds.", $rv, $apptimer->getElapsed()); } catch (Exception $e) { throw $e; } $ic--; if ($ic == 0) { return $rv; } } else { $ic--; if ($ic == 0) { Console::warn('FATAL: Application class %s not found!', $class); exit(RETURN_ERROR); } else { Console::warn('Application class %s not found!', $class); } } }
/** * @brief Run the application. Invoked by Lepton. * Will parse the arguments and make sure everything is in order. * */ function run() { global $argc, $argv; if (isset($this->arguments)) { if (is_string($this->arguments)) { $strargs = $this->arguments; $longargs = array(); } elseif (is_array($this->arguments)) { $strargs = ''; $longargs = array(); foreach ($this->arguments as $arg) { $strargs .= $arg[0]; // Scope is the : or :: $scope = substr($arg[0], 1); $longargs[] = $arg[1] . $scope; } } elseif (typeOf($this->arguments) == 'AppArgumentList') { $args = $this->arguments->getData(); $strargs = ''; $longargs = array(); foreach ($args as $arg) { $strargs .= $arg[0]; // Scope is the : or :: $scope = substr($arg[0], 1); $longargs[] = $arg[1] . $scope; } } else { console::warn('Application->$arguments is set but format is not understood'); } list($args, $params) = $this->parseArguments($strargs, $longargs); foreach ($args as $arg => $val) { if (in_array($arg, $longargs)) { foreach ($args as $argsrc => $v) { if ($argsrc == $arg) { $args[$argsrc[0]] = $val; $olarg = $argsrc[0]; } } } else { foreach ($args as $argsrc => $v) { if ($argsrc == $arg) { $arg[$argsrc] = $val; $olarg = $argsrc[0]; // Do any matching we need here } } } } $this->_args = $args; $this->_params = $params; } if (isset($args['h'])) { if (method_exists($this, 'usage')) { $this->usage(); } return 1; } return $this->main($argc, $argv); }
public function export() { console::writeLn(__astr('\\b{Exporting tables and data}')); $db = new DatabaseConnection(); $args = func_get_args(); if (count($args) == 0) { $args[] = '%'; } $drop = false; $data = false; foreach ($args as $arg) { try { if (strtolower($arg) == 'drop') { $drop = true; } elseif (strtolower($arg) == 'data') { $data = true; } else { $tbl = $db->getRows("SHOW TABLES LIKE %s", $arg); foreach ($tbl as $tblc) { $c = $tblc[0]; console::write(" [app] %-30s ", $c); // Get the create table syntax $crs = $db->getSingleRow("SHOW CREATE TABLE " . $c); $sqlcreate = $crs; // Get the data $fsql = fopen(expandpath('app:/sql/' . $c . '.sql'), 'w'); if ($drop) { fputs($fsql, "DROP TABLE IF EXISTS " . $c . ";\n"); } fputs($fsql, $crs[1] . "\n"); fclose($fsql); console::write("SQL "); if ($data) { // Export the data as well $drs = $db->getRows("SELECT * FROM " . $c); foreach ($drs[0] as $k => $v) { if ($k != '0' && intval($k) == 0) { $hdr[] = $k; } } $fdat = fopen(expandpath('app:/sql/' . $c . '.csv'), 'w'); fputcsv($fdat, $hdr, ";", '"'); foreach ($drs as $row) { $dout = array(); foreach ($row as $i => $h) { if ($i != '0' && intval($i) == 0) { $dout[] = $h; } } fputcsv($fdat, $dout, ";"); } fclose($fdat); console::write("CSV "); } console::writeLn(); } } } catch (DatabaseException $e) { console::writeLn(); console::warn($e->getMessage()); } } }