Added ability to read from a server's properties file, and added server port numbers to the index display

This commit is contained in:
Gregory Ballantine 2022-10-14 10:59:28 -04:00
parent 5c35389ae3
commit 2297768b57
3 changed files with 39 additions and 0 deletions

View File

@ -14,6 +14,8 @@ class Server {
private string $pidFilePath;
// server run status
private bool $state = false;
// server config properties
private ServerConfig $properties;
// class constructor
public function __construct($dir) {
@ -21,6 +23,7 @@ class Server {
$this->rootDir = $dir;
$this->serverName = $dirBits[count($dirBits) - 1];
$this->pidFilePath = $this->rootDir . '/pid.txt';
$this->properties = new ServerConfig($this->rootDir . '/server.properties');
// get server version
$versionFile = join('/', array($this->rootDir, 'current_version.txt'));
@ -81,6 +84,11 @@ class Server {
return -1;
}
// get parameter from server properties file
public function getProperty(string $param) {
return $this->properties->get($param);
}
// getters & setters
public function getDirectory(): string {
return $this->rootDir;

View File

@ -0,0 +1,29 @@
<?php
namespace BitGoblin\MCST\Minecraft;
class ServerConfig {
protected array $config;
public function __construct($propertiesFile) {
$this->config = array(); // initialize array
// parse config file
$fh = fopen($propertiesFile, 'r+');
while (!feof($fh)) {
$line = fgets($fh);
$eqpos = strpos($line, '=');
$parameterName = substr($line, 0, $eqpos);
$parameterValue = substr($line, ($eqpos + 1));
// assign parameter values to the config array
$this->config[$parameterName] = $parameterValue;
}
}
public function get(string $param) {
return $this->config[$param];
}
}

View File

@ -22,6 +22,7 @@
<tr>
<th>Server name</th>
<th>Minecraft version</th>
<th>Port</th>
<th>State</th>
<th>Actions</th>
</tr>
@ -31,6 +32,7 @@
<tr class="serverItem" data-server-name="{{ m.getName() }}">
<td class="serverName">{{ m.getName() }}</td>
<td class="serverVersion">{{ m.getVersion() }}</td>
<td class="serverPort">{{ m.getProperty('server-port') }}</td>
<td class="serverState">{{ m.getState() ? 'Running' : 'Stopped' }}</td>
<td>
<a href="/server/{{ m.getName() }}/start"><i class="fa-solid fa-play"></i></a>