Added a way to edit a server's properties - right now it only edits the server-port property

This commit is contained in:
Gregory Ballantine 2022-10-14 11:38:16 -04:00
parent 2297768b57
commit 6c23f3c806
6 changed files with 115 additions and 2 deletions

View File

@ -117,4 +117,36 @@ class ServerController extends Controller {
->withStatus(302); ->withStatus(302);
} }
// GET edit server properties route
public function getEdit(Request $request, Response $response, $args): Response {
$config = $this->get('config');
$serverDir = join('/', array($config->get('server_directory'), $args['serverName']));
$server = new Server($serverDir);
$data = array(
'serverName' => $args['serverName'],
'serverPort' => $server->getProperty('server-port'),
);
$view = Twig::fromRequest($request);
return $view->render($response, 'edit.twig', $data);
}
// POST edit server properties route
public function postEdit(Request $request, Response $response, $args): Response {
// set up the POST parameters and grab our config
$params = (array)$request->getParsedBody();
$config = $this->get('config');
$serverDir = join('/', array($config->get('server_directory'), $args['serverName']));
// create server object and save new value
$server = new Server($serverDir);
$server->updateProperty('server-port', $params['serverPort']);
// redirect the user back to the home page
return $response
->withHeader('Location', '/')
->withStatus(302);
}
} }

View File

@ -84,11 +84,16 @@ class Server {
return -1; return -1;
} }
// get parameter from server properties file // get property from server properties file
public function getProperty(string $param) { public function getProperty(string $param) {
return $this->properties->get($param); return $this->properties->get($param);
} }
// update property in server properties file
public function updateProperty(string $paramName, $paramValue) {
$this->properties->update($paramName, $paramValue);
}
// getters & setters // getters & setters
public function getDirectory(): string { public function getDirectory(): string {
return $this->rootDir; return $this->rootDir;

View File

@ -4,9 +4,13 @@ namespace BitGoblin\MCST\Minecraft;
class ServerConfig { class ServerConfig {
// path to the server.properties file
protected string $propertiesFile;
// loaded properties
protected array $config; protected array $config;
public function __construct($propertiesFile) { public function __construct($propertiesFile) {
$this->propertiesFile = $propertiesFile; // save the properties file
$this->config = array(); // initialize array $this->config = array(); // initialize array
// parse config file // parse config file
@ -15,7 +19,7 @@ class ServerConfig {
$line = fgets($fh); $line = fgets($fh);
$eqpos = strpos($line, '='); $eqpos = strpos($line, '=');
$parameterName = substr($line, 0, $eqpos); $parameterName = substr($line, 0, $eqpos);
$parameterValue = substr($line, ($eqpos + 1)); $parameterValue = trim(substr($line, ($eqpos + 1)));
// assign parameter values to the config array // assign parameter values to the config array
$this->config[$parameterName] = $parameterValue; $this->config[$parameterName] = $parameterValue;
@ -26,4 +30,35 @@ class ServerConfig {
return $this->config[$param]; return $this->config[$param];
} }
public function update(string $paramName, $paramValue) {
// update the local variable value
$this->config[$paramName] = $paramValue;
// update and save the server.properties file
$newFileContent = '';
$fh = fopen($this->propertiesFile, 'r+');
while (!feof($fh)) {
$line = fgets($fh);
if (!str_starts_with($line, '#')) {
$eqpos = strpos($line, '=');
$parameterName = substr($line, 0, $eqpos);
$parameterValue = substr($line, ($eqpos + 1));
// use the new value if it's being updated
if ($parameterName == $paramName) {
$parameterValue = $paramValue . "\n";
}
$newFileContent .= $parameterName . '=' . $parameterValue;
} else {
$newFileContent .= $line;
}
}
// save the new properties file
file_put_contents($this->propertiesFile, $newFileContent);
fclose($fh);
}
} }

View File

@ -21,3 +21,8 @@ $app->get('/server/{serverName}/status', '\\BitGoblin\\MCST\\Controllers\\Server
$app->get('/server/{serverName}/start', '\\BitGoblin\\MCST\\Controllers\\ServerController:getStart')->setName('server.start'); $app->get('/server/{serverName}/start', '\\BitGoblin\\MCST\\Controllers\\ServerController:getStart')->setName('server.start');
// server stop route // server stop route
$app->get('/server/{serverName}/stop', '\\BitGoblin\\MCST\\Controllers\\ServerController:getStop')->setName('server.stop'); $app->get('/server/{serverName}/stop', '\\BitGoblin\\MCST\\Controllers\\ServerController:getStop')->setName('server.stop');
// edit GET route - this page allows a user to edit a server's properties
$app->get('/server/{serverName}/edit', '\\BitGoblin\\MCST\\Controllers\\ServerController:getEdit')->setName('edit');
// edit POST route - processes the edits a server's properties file
$app->post('/server/{serverName}/edit', '\\BitGoblin\\MCST\\Controllers\\ServerController:postEdit');

35
views/edit.twig Normal file
View File

@ -0,0 +1,35 @@
{% extends 'layout.twig' %}
{% block content %}
<!-- page header -->
<header class="row">
<div class="columns twelve">
<h1>Edit server: {{ serverName }}</h1>
</div>
</header>
<!-- create server form -->
<section class="row">
<div class="columns twelve">
<form action="/server/{{ serverName }}/edit" method="POST">
<div class="row">
<div class="columns twelve">
<label for="serverPort">Server port:</label>
<input id="serverPort" name="serverPort" class="u-full-width" type="number" placeholder="25565" required value={{ serverPort }}>
</div>
</div>
<input id="editSubmit" type="submit" value="Submit">
</form>
</div>
</section>
<!-- lower navigation -->
<div class="row">
<div class="columns twelve">
<p><a href="/">Back</a></p>
</div>
</div>
{% endblock %}

View File

@ -37,6 +37,7 @@
<td> <td>
<a href="/server/{{ m.getName() }}/start"><i class="fa-solid fa-play"></i></a> <a href="/server/{{ m.getName() }}/start"><i class="fa-solid fa-play"></i></a>
<a href="/server/{{ m.getName() }}/stop"><i class="fa-solid fa-stop"></i></a> <a href="/server/{{ m.getName() }}/stop"><i class="fa-solid fa-stop"></i></a>
<a href="/server/{{ m.getName() }}/edit"><i class="fa-solid fa-pen-to-square"></i></a>
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}