improved config loading and validation

This commit is contained in:
Steffen Schröder 2014-05-27 08:57:26 +02:00
parent 2818192102
commit f54ee6df64
3 changed files with 50 additions and 40 deletions

View File

@ -159,8 +159,8 @@ class ManiaControl implements CommandListener, TimerListener {
if (!$this->config) { if (!$this->config) {
$this->quit("Error loading Configuration XML-File! ('{$configFileName}')", true); $this->quit("Error loading Configuration XML-File! ('{$configFileName}')", true);
} }
if (!$this->config->server->port || $this->config->server->port == 'port') { if ($this->config->count() < 3) {
$this->quit("Your Configuration File ('{$configFileName}') doesn't seem to be maintained. Please check it again!", true); $this->quit("Your Configuration File ('{$configFileName}') doesn't seem to be maintained properly. Please check it again!", true);
} }
} }

View File

@ -29,10 +29,28 @@ class Config {
* @param string $pass XmlRpc Password * @param string $pass XmlRpc Password
*/ */
public function __construct($id = null, $host = null, $port = null, $login = null, $pass = null) { public function __construct($id = null, $host = null, $port = null, $login = null, $pass = null) {
$this->id = $id; $this->id = (string)$id;
$this->host = $host; $this->host = (string)$host;
$this->port = $port; $this->port = (int)$port;
$this->login = $login; $this->login = (string)$login;
$this->pass = $pass; $this->pass = (string)$pass;
}
/**
* Validate the Config Data
*
* @return bool
*/
public function validate() {
$invalid = false;
if (!$this->host) {
$invalid = true;
} else if (!$this->port || $this->port === 'port') {
$invalid = true;
}
if ($invalid) {
return false;
}
return true;
} }
} }

View File

@ -98,64 +98,56 @@ class Server implements CallbackListener {
// Server id parameter // Server id parameter
$serverId = CommandLineHelper::getParameter('-id'); $serverId = CommandLineHelper::getParameter('-id');
// Xml server tag with given id // Server xml element with given id
$serverTag = null; $serverElement = null;
if ($serverId) { if ($serverId) {
$serverTags = $this->maniaControl->config->xpath("server[@id='{$serverId}']"); $serverElements = $this->maniaControl->config->xpath("server[@id='{$serverId}']");
if ($serverTags) { if (!$serverElements) {
$serverTag = $serverTags[0];
}
if (!$serverTag) {
trigger_error("No Server configured with the ID '{$serverId}'!", E_USER_ERROR); trigger_error("No Server configured with the ID '{$serverId}'!", E_USER_ERROR);
} }
$serverElement = $serverElements[0];
} else { } else {
$serverTags = $this->maniaControl->config->xpath('server'); $serverElements = $this->maniaControl->config->xpath('server');
if ($serverTags) { if (!$serverElements) {
$serverTag = $serverTags[0];
}
if (!$serverTag) {
trigger_error('No Server configured!', E_USER_ERROR); trigger_error('No Server configured!', E_USER_ERROR);
} }
$serverElement = $serverElements[0];
} }
// Host // Host
$host = $serverTag->xpath('host'); $hostElements = $serverElement->xpath('host');
if ($host) { if (!$hostElements) {
$host = (string)$host[0];
}
if (!$host) {
trigger_error("Invalid server configuration (host).", E_USER_ERROR); trigger_error("Invalid server configuration (host).", E_USER_ERROR);
} }
$host = (string)$hostElements[0];
// Port // Port
$port = $serverTag->xpath('port'); $portElements = $serverElement->xpath('port');
if ($port) { if (!$portElements) {
$port = (string)$port[0];
}
if (!$port) {
trigger_error("Invalid server configuration (port).", E_USER_ERROR); trigger_error("Invalid server configuration (port).", E_USER_ERROR);
} }
$port = (string)$portElements[0];
// Login // Login
$login = $serverTag->xpath('login'); $loginElements = $serverElement->xpath('login');
if ($login) { if (!$loginElements) {
$login = (string)$login[0];
}
if (!$login) {
trigger_error("Invalid server configuration (login).", E_USER_ERROR); trigger_error("Invalid server configuration (login).", E_USER_ERROR);
} }
$login = (string)$loginElements[0];
// Password // Password
$pass = $serverTag->xpath('pass'); $passElements = $serverElement->xpath('pass');
if ($pass) { if (!$passElements) {
$pass = (string)$pass[0];
}
if (!$pass) {
trigger_error("Invalid server configuration (password).", E_USER_ERROR); trigger_error("Invalid server configuration (password).", E_USER_ERROR);
} }
$pass = (string)$passElements[0];
// Create config object // Create config object
$this->config = new Config($serverId, $host, $port, $login, $pass); $config = new Config($serverId, $host, $port, $login, $pass);
if (!$config->validate()) {
$this->maniaControl->quit("Your Configuration File doesn't seem to be maintained properly. Please check it again!", true);
}
$this->config = $config;
} }
/** /**