PHP-cli-factorio-updater/scripts/class.FactorioServer.php

172 lines
4.0 KiB
PHP

<?php
/**
* Namespace for classes and functions related to the game Factorio.
* @package NAE\Factorio
*/
namespace NAE\Factorio;
/**
* Class for managing a headless server for the game Factorio.
* @author "Marcel Naeve" <php@naeve.info>
* @license MIT License (http://www.opensource.org/licenses/mit)
*/
class FactorioServer {
/**
* @var FactorioVersion Current Version of Server
*/
private $version;
/**
* @var FactorioMod For Managing Server Mods
*/
private $mods;
/**
* @var string Name of the Save to load when starting the server
*/
private $saveName;
/**
* @var callcable Custom function for stopping the server (return bool)
*/
private $customStop = null;
/**
* @var callable Custom function for starting the server (return bool)
*/
private $customStart = null;
/**
* @var array Server Settings
*/
private $settings = [];
/**
* @var array Environment Variables for the Server
*/
private $env = [];
/**
* @var array Server Admin List
*/
private $adminList = [];
/**
* Constructor
*
* @param array $settings Settings for the server
*/
public function __construct(array $settings) {
// TODO: Implement
}
/**
* Updates the Server with optional feedback on cli.
* @param bool $silent Feedback on cli wanted? (default: false)
* @return bool Returns true on success, false on failure
*/
public function updateCli(bool $silent=false) : bool {
// TODO: Implement
return false;
}
/**
* Install a new Server with the current Settings with optional feedback on cli.
* @param string $version Which version should be installed? (default: latest)
* @param bool $silent Feedback on cli wanted? (default: false)
* @return bool Returns true on success, false on failure
*/
public function installCli(string $version="latest", bool $silent=false) : bool {
// TODO: Implement
return false;
}
/**
* Sets a custom function for stopping the server.
* @param callable function that is beeing called for stopping the server
* @return FactorioServer $this for chaining.
*/
public function setCustomStop(callable $customStop) : FactorioServer {
$this->customStop = $customStop;
return $this;
}
/**
* Sets a custom function for starting the server.
* @param callable function that is beeing called for starting the server
* @return FactorioServer $this for chaining.
*/
public function setCustomStart(callable $customStart) : FactorioServer {
$this->customStart = $customStart;
return $this;
}
/**
* Stops the server.
* @return bool true on success, false on failure
*/
public function stop() : bool {
if(is_callable($this->customStop)) {
return call_user_func($this->customStop);
}
$command = "systemctl stop factorio-server";
exec($command, $output, $return);
return ( $return === 0 );
}
/**
* Starts the server.
* @return bool true on success, false on failure
*/
public function start() : bool {
if(is_callable($this->customStart)) {
return call_user_func($this->customStart);
}
$command = "systemctl start factorio-server";
exec($command, $output, $return);
return ( $return === 0 );
}
/**
* Restarts the server.
* @return bool true on success, false on failure
*/
public function restart() : bool {
exec("systemctl restart factorio-server > /dev/null", $output, $return);
return ( $return === 0 );
}
/**
* Check if the Service is running
*
* @return bool True if the service is running, false otherwise
*/
public function isRunning() : bool {
exec("systemctl is-active factorio-server", $output, $return);
return ( $return === 0 );
}
}